The Rust Backend
The Rust half of your plugin is a dynamic library (cdylib) that Griffon loads at runtime. You write it with the plugin SDK (plugin_sdk). You write plain Rust functions that take and return serde types, and the SDK generates everything the host needs:
- the exported
init(), which reports your plugin's metadata and its list of functions; - the exported
handle_message(), which decodes incoming calls, routes them to your functions and encodes the reply; - the
abi_stableroot module (get_library()) that the loader looks for.
The manifest never talks to your code directly. An execute_function step sends a call, and stores the JSON your function returns.
plugins/plugin_template is the smallest working plugin. Copy it, rename it, and replace its functions.
1. Cargo setup
[lib]
crate-type = ["cdylib"]
[dependencies]
plugin_sdk = { workspace = true }
# Required: the code generated by export_plugin! refers to `::abi_stable` directly.
abi_stable = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
That is all a plugin needs. Don't depend on internal crates such as plugin_interface, plugin_host or logger.
abi_stable as a direct dependencyexport_plugin! uses abi_stable's attribute macros, and those expand to ::abi_stable::... paths. Without a direct abi_stable dependency, your plugin won't compile.
2. Write your functions
Each function your manifest can call is an ordinary Rust function, with this signature:
fn my_function(params: P) -> Result<O, E>
Pis anyserde::Deserializetype. It's built from the JSON payload of the call.Ois anyserde::Serializetype. It's sent back as JSON, as is, with no wrapper, which is what the manifest'sstorekey receives.EisStringor&str. AnErrbecomes an error reply (see Errors).
use plugin_sdk::params::NoParams;
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct ScanRequest {
target: String,
#[serde(default)]
ports: Option<String>,
}
#[derive(Serialize)]
struct ScanResult {
target: String,
open_ports: Vec<u16>,
}
fn scan(req: ScanRequest) -> Result<ScanResult, String> {
if req.target.is_empty() {
return Err("target is required".into());
}
Ok(ScanResult { target: req.target, open_ports: vec![22, 443] })
}
fn check(_: NoParams) -> Result<&'static str, String> {
Ok("ready")
}
Use one small struct per function, with #[serde(default)] on optional fields. For a function that takes no arguments, use NoParams: it accepts an empty payload, null, {} or [].
3. Register them and export the plugin
use plugin_sdk::{export_plugin, PluginRouter};
fn build_router() -> PluginRouter {
PluginRouter::new()
.method("scan", scan)
.method("check", check)
}
export_plugin! {
meta: PluginMeta {
name: "My Plugin",
uuid: "123e4567-e89b-12d3-a456-426614174000", // same as `uuid` in your .toml
author: "Developer",
description: "Custom analysis plugin",
},
router: build_router,
on_init: start_engine, // optional: fn(), runs once when the plugin is loaded
}
The names you pass to .method(...) are the names your manifest uses in fn = "...". The SDK reports them to Griffon automatically, so there is no separate function list to keep in sync.
init() returns these keys to the host:
| Key | Value |
|---|---|
name, author, description | From meta |
UUID | meta.uuid |
function | Every registered method, in registration order, joined with / |
sdk_version | The SDK version the plugin was built with |
If on_init panics, init() returns an error instead of crashing the host.
4. The wire format
You normally don't need this, but it helps when debugging. The host sends one string per call, and the SDK accepts any of these forms:
| Input | Method | Params |
|---|---|---|
fn:scan {"target":"x"} | scan | {"target":"x"} |
fn:check | check | null |
scan {"target":"x"} (no fn:) | scan | {"target":"x"} |
{"method":"scan","params":{"target":"x"}} | scan | {"target":"x"} |
The input is trimmed and split on the first whitespace only, because the JSON params can contain spaces. Empty params become null.
5. Errors
Every failure produces the same reply shape:
{"ok": false, "code": -32602, "message": "invalid params: missing field `target`"}
| Code | Meaning |
|---|---|
-32601 | Unknown method |
-32602 | Invalid params: the payload isn't JSON, or doesn't match your struct |
-32603 | Internal error: the result couldn't be serialized, or your function panicked |
1 | Your function returned Err(...) |
A panic inside a handler is caught and turned into a -32603 reply, so it doesn't take down the runner. Still, return Err for expected failures, and keep panics for real bugs.
6. State across calls
Each call is independent. If you need state that lasts between calls (a loaded engine, a running scan, a background thread), store it in a static (OnceLock, Mutex, RwLock or an atomic).
- Stateless plugins compute everything from the payload. You don't need globals.
- Long-running work: keep each call fast. Start a thread, and expose a separate function to poll its status.
plugin_templateshows this withstartandstop. - Thread safety: handlers must be
Send + Sync. Today the runner calls one method at a time, but don't rely on that.
7. Testing without loading the .so
PluginRouter::call_json runs the full decode → call → encode pipeline in a plain unit test:
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn scan_rejects_empty_target() {
let err = build_router().call_json("scan", json!({ "target": "" })).unwrap_err();
assert_eq!(err.code, 1);
assert_eq!(err.message, "target is required");
}
}
For the integration and contract tests every plugin needs, see Testing.
Common mistakes
- Forgetting
crate-type = ["cdylib"]: no.sois produced. - Removing the direct
abi_stabledependency:export_plugin!stops compiling. - A
uuidin the manifest that differs frommeta.uuid: Griffon can't match the manifest to the library. - A
fn = "..."in the manifest with no matching.method(...): the call returns-32601.
Next: Packaging & Loading: turning your .so and .toml into an installable plugin.