Creating Go 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 Go. We'll create a simple plugin that generates random character names.
Prerequisites
To follow this guide, you'll need:
- Go (1.18 or newer) installed
- TinyGo installed (required for WebAssembly compilation)
- Extism CLI installed (for testing)
Alternatively, you can use the Docker image provided by WASImancer:
Project Setup
Let's create a new plugin project:
Creating the Go Module
Initialize a new Go module:
Add the Extism PDK (Plugin Development Kit) as a dependency:
Writing the Plugin Code
Create a file named main.go
with the following content:
Understanding the Code
-
We import the
github.com/extism/go-pdk
package, which provides the interface between our Go code and the WebAssembly host environment. -
We define two slices of strings:
adjectives
andnouns
, which will be used to generate random character names. -
We define a function
GenerateCharacterName
with the//export
comment, which marks this function as exportable from the WebAssembly module. -
Inside the function, we:
- Initialize a random number generator
- Select a random adjective and noun
- Combine them with a hyphen to create a character name
-
Allocate memory for the string and output it to the host
-
The
main()
function is empty but required for compilation.
Building the Plugin
Using Local TinyGo
To compile your Go code to WebAssembly, run:
Using Docker
If you're using the WASImancer's Docker image:
This will create a file called character-name-generator.wasm
in your project directory.
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 a randomly generated character name as output, like brave-dolphin
or tenacious-dragon
.
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
-
Use Simple Data Types: Start with simple data types like strings and integers.
-
Print to Console: The PDK provides functions for debug logging:
-
Test Incrementally: Build and test your plugin after each small change.
-
Check Return Values: Always validate the data returned by your functions.
Best Practices
-
Error Handling: Always handle errors and provide meaningful error messages.
-
Input Validation: Validate all input arguments before processing.
-
Memory Management: The PDK handles memory allocation, but be conscious of large data structures.
-
Statelessness: Design your plugins to be stateless whenever possible.
-
Documentation: Comment your code and provide clear descriptions in your plugin configuration.
Conclusion
You've now learned how to create, build, test, and deploy a Go plugin for WASImancer. By leveraging Go's performance and the WebAssembly sandbox, you can create powerful, secure tools that enhance your AI applications through the Model Context Protocol.