Creating Rust Plugins for WASImancer: A Step-by-Step Guide
๐ง work in progress
This guide will walk you through the process of creating, building, and running a WebAssembly plugin for WASImancer using Rust. We'll create a simple calculator plugin that can add two numbers.
Prerequisites
To follow this guide, you'll need:
- Rust and Cargo installed
- wasm32-unknown-unknown target for WebAssembly compilation
- Extism CLI for testing
Alternatively, you can use the Docker image provided by WASImancer:
Project Setup
Let's create a new Rust library project:
Creating the Rust Project
Initialize a new Rust library project:
Configuring the Project for WebAssembly
First, edit the Cargo.toml
file to set up your project for WebAssembly compilation:
Key points about this configuration:
- crate-type = ["cdylib"]
specifies that we're building a dynamic library suitable for WebAssembly
- extism-pdk
is the Extism Plugin Development Kit for Rust
- serde
is used for serializing and deserializing JSON data
Next, create a .cargo/config.toml
file to set the default target:
Writing the Plugin Code
Replace the contents of src/lib.rs
with the following code:
Understanding the Code
-
We import the necessary components from
extism_pdk
andserde
to handle WebAssembly interaction and JSON serialization. -
We define two structs:
Add
: Represents the input with two integers (left
andright
)-
Sum
: Represents the output with a single integer (value
) -
The
#[plugin_fn]
macro marks ouradd
function as an exported plugin function. -
The
add
function takes anAdd
input, performs the addition, and returns aSum
result wrapped inFnResult
. -
Both structs use the
#[encoding(Json)]
attribute to specify that they should be encoded/decoded as JSON.
Building the Plugin
Using Local Rust
To compile your Rust code to WebAssembly:
The compiled WebAssembly file will be located at target/wasm32-unknown-unknown/release/wasimancer_plugin_addition.wasm
.
Copy it to your project directory:
Using Docker
If you're using the WASImancer's Docker image:
Testing the Plugin Locally
You can test your plugin using the Extism CLI before deploying it to WASImancer:
Using Local Extism CLI
Using Docker
You should see output like: {"value":42}
, indicating that the addition function correctly added 30 and 12.
Creating the Plugin Configuration
To use your plugin with WASImancer, you need to create a configuration in the plugins.yml
file. Add the following entry:
Deploying the Plugin to WASImancer
Method 1: Manual Deployment
-
Copy your WebAssembly file to the WASImancer plugins directory:
-
Restart WASImancer or use the hot-reload API (next method).
Method 2: Using the Upload API
You can use the WASImancer API to upload your plugin without restarting the server:
- Create a shell script named
publish.sh
with the following content:
- Make the script executable and run it:
Testing the Plugin in WASImancer
You can test your deployed plugin using the MCP Inspector:
-
Start the Inspector:
-
Connect to your WASImancer instance (typically at
http://localhost:3001/sse
). -
Navigate to the "Tools" tab, find your plugin, and click "Run Tool".
Debugging Tips for Rust Plugins
-
Extism Logging: The Extism PDK provides logging functions:
-
Test with
--log-level "debug"
or--log-level "trace"
flags: When using the Extism CLI, increase the log level for more details. -
Validate JSON Parsing: If you're having issues with JSON parsing, try adding explicit error handling:
Best Practices for Rust Plugins
-
Leverage Rust's Type System: Use Rust's strong type system to prevent bugs and make your code more maintainable.
-
Use
Result
for Error Handling: Return meaningful errors rather than panicking, which can cause unexpected behavior in WebAssembly. -
Keep Dependencies Minimal: Large dependencies can increase your WebAssembly module size and slow down loading times.
-
Optimize for Size: Use build flags to optimize for smaller WebAssembly modules:
-
Use Rust's Memory Management: Take advantage of Rust's ownership model to prevent memory leaks, which is especially important in long-running WebAssembly modules.
-
Document Your Code: Add thorough documentation comments to make your plugins easier to maintain.
Build Script for Convenience
Create a build.sh
script to simplify the build process:
And a run.sh
script to test your plugin:
Make them executable:
Conclusion
You've now learned how to create, build, test, and deploy a Rust plugin for WASImancer. Rust's combination of safety, performance, and WebAssembly support makes it an excellent choice for developing WASImancer plugins. By leveraging Rust's rich ecosystem and type system, you can create powerful, secure tools that enhance your AI applications through the Model Context Protocol.