mcp_server_component

Macro mcp_server_component 

Source
macro_rules! mcp_server_component {
    (
        $(
            $method:expr => $handler:expr
        ),*
        $(,)?
    ) => { ... };
    (
        tools: [
            $($tool_type:ty),* $(,)?
        ]
        $(,)?
    ) => { ... };
    (
        tools: [
            $($tool_type:ty),* $(,)?
        ],
        overrides: {
            $(
                $override_name:ident: $override_type:ty
            ),* $(,)?
        }
        $(,)?
    ) => { ... };
}
Expand description

Macro for creating an MCP server component

This macro simplifies the creation of MCP server components by automatically generating the necessary handlers and message processing logic.

ยงExample

use mcp_sdk::mcp_server_component;
use serde_json::Value;
 
struct MyTool;
 
impl ToolHandler for MyTool {
    fn handle(&self, params: &Value) -> Result<Value, JsonRpcError> {
        // Handle tool call
        Ok(json!({ "result": "success" }))
    }
     
    fn name(&self) -> &'static str {
        "my_tool"
    }
     
    fn description(&self) -> &'static str {
        "A custom tool"
    }
     
    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "param1": {
                    "type": "string"
                }
            }
        })
    }
}
 
mcp_server_component! {
    tools: [
        MyTool
    ]
}