Understanding WebAssembly Plugins in WASImancer
π§ work in progress
What are WebAssembly Plugins?
In WASImancer, WebAssembly (WASM) plugins are the building blocks that enable the MCP server to execute tools. These plugins are compiled WebAssembly modules that:
- Contain one or more functions that can be called by the MCP server
- Run in a sandboxed environment for security
- Execute at near-native speed
- Can be written in multiple programming languages
WebAssembly serves as a portable compilation target, allowing developers to write code in their preferred language (Go, Rust, C++, etc.) and compile it to a standard binary format that can be executed in WASImancer.
How Plugins Work in WASImancer
Plugin Architecture
WASImancer uses the Extism framework to load and execute WebAssembly plugins. The architecture follows these principles:
- Isolation: Each plugin runs in its own isolated environment
- Communication: Data is passed between the host (WASImancer) and the plugin via function parameters and return values
- Registration: Plugins are registered with the MCP server and exposed as tools
- Execution: When a client requests a tool, the server calls the corresponding function in the WASM plugin
Plugin Definition in YAML
Plugins are defined in the plugins.yml
file, which maps WebAssembly modules to MCP tools:
This configuration: 1. Tells WASImancer where to find the WASM module 2. Defines which functions to expose as tools 3. Specifies the expected input parameters 4. Provides descriptions for documentation
Plugin Development
Supported Languages
You can develop plugins for WASImancer in any language that compiles to WebAssembly with WASI support. Commonly used languages include:
- Go (using TinyGo)
- Rust
- C/C++
- AssemblyScript
Development Workflow
- Write your plugin code in your preferred language
- Compile to WebAssembly using the appropriate toolchain
- Test locally using the Extism CLI
- Deploy to WASImancer by placing the WASM file in the plugins directory
- Configure in plugins.yml to expose your functions as tools
Example: Go Plugin
Here's a simple plugin written in Go that rolls dice:
Compiling with TinyGo
To compile a Go plugin, use TinyGo with the WASI target:
JSON Communication Protocol
Plugins in WASImancer communicate using a JSON-based protocol:
- Input: Function arguments are passed as JSON strings
- Output: Function results are returned as text (often JSON or simple values)
For example, when calling the rollDice
function:
- Input: {"numFaces": 6, "numDice": 2}
- Output: "8"
(the sum of the dice roll)
Plugin Lifecycle Management
WASImancer provides several REST API endpoints to manage plugins at runtime:
Upload a New Plugin
Replace an Existing Plugin
Remove a Plugin
These APIs enable hot-reloading of plugins without restarting the server.
Best Practices for Plugin Development
- Keep plugins focused: Each plugin should have a specific purpose
- Handle errors gracefully: Return meaningful error messages when things go wrong
- Validate inputs: Check input parameters for validity before processing
- Use appropriate types: Match function parameter types with their expected usage
- Document thoroughly: Provide clear descriptions for your plugin and its functions
- Test independently: Use the Extism CLI to test plugins before deploying to WASImancer
Security Considerations
WebAssembly plugins run in a sandboxed environment, but you should still follow these security guidelines:
- Limit permissions: Only grant the permissions a plugin needs (e.g., network access, file system access)
- Validate inputs: Always validate and sanitize inputs to prevent injection attacks
- Protect sensitive data: Be careful about handling sensitive information within plugins
- Update dependencies: Keep your plugin dependencies up to date to address security vulnerabilities
Debugging Plugins
When developing plugins, you can use:
-
Extism CLI: Test your plugin locally before deploying
-
Inspector Tool: Test your plugins once deployed to WASImancer
By embracing WebAssembly plugins, WASImancer provides a powerful, flexible, and secure way to extend MCP servers with custom functionality in any programming language.