Skip to main content

declare_tools

Macro declare_tools 

Source
macro_rules! declare_tools {
    (tools: [ $($tool:expr),* $(,)? ]) => { ... };
}
Expand description

Declare tools and auto-generate list_tools and execute_tool functions

This macro takes a list of Tool definitions and generates:

  • A static tool registry (HashMap for O(1) lookup)
  • The generated_list_tools function
  • The generated_execute_tool function

These generated functions can be used directly in the declare_plugin! macro.

§Example

use mcp_plugin_api::*;
use serde_json::{json, Value};

fn handle_hello(args: &Value) -> Result<Value, String> {
    let name = args["name"].as_str().unwrap_or("World");
    Ok(json!({ "message": format!("Hello, {}!", name) }))
}

fn handle_goodbye(args: &Value) -> Result<Value, String> {
    Ok(json!({ "message": "Goodbye!" }))
}

declare_tools! {
    tools: [
        Tool::new("hello", "Say hello")
            .param_string("name", "Name to greet", false)
            .handler(handle_hello),
         
        Tool::new("goodbye", "Say goodbye")
            .handler(handle_goodbye),
    ]
}

declare_plugin! {
    list_tools: generated_list_tools,
    execute_tool: generated_execute_tool,
    free_string: mcp_plugin_api::utils::standard_free_string
}