A simple example plugin demonstrating basic Cortex plugin capabilities.
- Command:
/hello [name]- Greets the user with a customizable message - Hook:
tool_execute_before- Logs all tool executions - Configuration:
greeting_prefixsetting for customizing greetings
hello-world/
├── plugin.toml # Plugin manifest (metadata, commands, hooks, config)
├── Cargo.toml # Rust build configuration
├── src/
│ └── lib.rs # Plugin implementation
└── README.md # This file
- Install Rust: https://rustup.rs/
- Add the WASM target:
rustup target add wasm32-wasi
cd examples/plugins/hello-world
cargo build --target wasm32-wasi --releaseThe compiled WASM module will be at:
target/wasm32-wasi/release/hello_world_plugin.wasm
Copy the compiled WASM and manifest to your Cortex plugins directory:
# Create plugin directory
mkdir -p ~/.cortex/plugins/hello-world
# Copy manifest
cp plugin.toml ~/.cortex/plugins/hello-world/
# Copy compiled WASM
cp target/wasm32-wasi/release/hello_world_plugin.wasm ~/.cortex/plugins/hello-world/plugin.wasmAdd to your Cortex configuration to customize the plugin:
[plugins.hello-world]
enabled = true
greeting_prefix = "Howdy" # Default: "Hello"Once installed and enabled, use the command in Cortex:
/hello # Output: "Hello, World!"
/hello Alice # Output: "Hello, Alice!"
/hi Bob # Alias also works: "Hello, Bob!"
The plugin.toml file contains:
- [plugin]: Metadata (id, name, version, description, authors)
- capabilities: What the plugin can do (commands, hooks, config)
- [[commands]]: Command definitions with arguments
- [[hooks]]: Hook registrations with priorities
- [config]: Configuration schema with validation
- [wasm]: WASM runtime settings (memory, timeout)
The tool_execute_before hook logs every tool execution:
[DEBUG] Tool execution intercepted by hello-world plugin
[INFO] Tool about to execute - logging from hello-world plugin
This is useful for debugging and auditing tool usage.
- Host function imports: Functions provided by Cortex (
log,get_context, etc.) - Lifecycle functions:
init()andshutdown()for plugin lifecycle - Command handlers:
cmd_<name>()functions for each command - Hook handlers:
hook_<type>()functions for each hook
-
Add the command definition in
plugin.toml:[[commands]] name = "mycommand" description = "My new command"
-
Implement the handler in
src/lib.rs:#[no_mangle] pub extern "C" fn cmd_mycommand() -> i32 { log_info("My command executed!"); 0 }
-
Add the hook definition in
plugin.toml:[[hooks]] hook_type = "session_start" priority = 100
-
Implement the handler in
src/lib.rs:#[no_mangle] pub extern "C" fn hook_session_start() -> i32 { log_info("Session started!"); 0 }
MIT License - See LICENSE file for details.