Skip to main content

Crate mcp_host_macros

Crate mcp_host_macros 

Source
Expand description

Procedural macros for mcp-host

Provides attribute macros for ergonomic MCP server definition.

§Usage

use mcp_host::prelude::*;
use schemars::JsonSchema;

#[derive(Deserialize, JsonSchema)]
struct CalcParams { x: f64, y: f64 }

#[mcp_router]
impl MyServer {
    #[mcp_tool(name = "calculate")]
    async fn calculate(&self, ctx: Ctx, params: Parameters<CalcParams>) -> ToolResult {
        Ok(ToolOutput::text(format!("Result: {}", params.0.x + params.0.y)))
    }

    #[mcp_prompt(name = "greeting", argument(name = "name", required = true))]
    async fn greeting(&self, ctx: Ctx, args: Value) -> PromptResult {
        let name = args.get("name").and_then(|v| v.as_str()).unwrap_or("World");
        prompt_messages(vec![user_message(format!("Hello, {name}!"))])
    }

    #[mcp_resource(uri = "config:///app", name = "config")]
    async fn config(&self, ctx: Ctx) -> ResourceResult {
        Ok(vec![text_resource("config:///app", "{\"version\": \"1.0\"}")])
    }
}

// Registration:
let server = Arc::new(MyServer::new());
MyServer::tool_router().register_all(&tool_registry, server.clone());
MyServer::prompt_router().register_all(&prompt_manager, server.clone());
MyServer::resource_router().register_all(&resource_manager, server);

Attribute Macros§

mcp_prompt
Mark an async function as an MCP prompt handler
mcp_resource
Mark an async function as an MCP resource handler
mcp_resource_template
Mark an async function as an MCP resource template handler
mcp_router
Router macro for MCP servers
mcp_tool
Mark an async function as an MCP tool handler