macro_rules! declare_plugin_init {
($native_fn:ident) => { ... };
}Expand description
Declare a plugin initialization function with automatic wrapper generation
This macro takes a native Rust function and wraps it as an extern "C" function
that can be used in declare_plugin!. The native function should have the signature:
โ
fn my_init() -> Result<(), String>The macro generates a C ABI wrapper function named plugin_init that:
- Calls your native init function
- Handles the FFI error reporting
- Returns appropriate error codes
ยงExample
โ
use mcp_plugin_api::*;
use once_cell::sync::OnceCell;
static DB_POOL: OnceCell<DatabasePool> = OnceCell::new();
// Native Rust init function
fn init() -> Result<(), String> {
let config = get_config();
// Initialize database
let pool = connect_to_db(&config.database_url)
.map_err(|e| format!("Failed to connect: {}", e))?;
// Validate connection
pool.ping()
.map_err(|e| format!("DB ping failed: {}", e))?;
DB_POOL.set(pool)
.map_err(|_| "DB already initialized".to_string())?;
Ok(())
}
// Generate the C ABI wrapper
declare_plugin_init!(init);
// Use in plugin declaration
declare_plugin! {
list_tools: generated_list_tools,
execute_tool: generated_execute_tool,
free_string: utils::standard_free_string,
configure: plugin_configure,
init: plugin_init // โ Generated by declare_plugin_init!
}