Crate mcpkit_macros

Crate mcpkit_macros 

Source
Expand description

Procedural macros for the MCP SDK.

This crate provides the unified #[mcp_server] macro that simplifies MCP server development.

§Overview

The macro system provides:

  • #[mcp_server] - Transform an impl block into a full MCP server
  • #[tool] - Mark a method as an MCP tool
  • #[resource] - Mark a method as an MCP resource handler
  • #[prompt] - Mark a method as an MCP prompt handler

§Example

use mcpkit::prelude::*;
use mcpkit::transport::stdio::StdioTransport;

struct Calculator;

#[mcp_server(name = "calculator", version = "1.0.0")]
impl Calculator {
    /// Add two numbers together
    #[tool(description = "Add two numbers")]
    async fn add(&self, a: f64, b: f64) -> ToolOutput {
        ToolOutput::text((a + b).to_string())
    }

    /// Multiply two numbers
    #[tool(description = "Multiply two numbers")]
    async fn multiply(&self, a: f64, b: f64) -> ToolOutput {
        ToolOutput::text((a * b).to_string())
    }
}

#[tokio::main]
async fn main() -> Result<(), McpError> {
    let transport = StdioTransport::new();
    let server = ServerBuilder::new(Calculator)
        .with_tools(Calculator)
        .build();
    server.serve(transport).await
}

§Code Reduction

This single macro replaces 4 separate macros:

  • #[derive(Clone)] with manual router field
  • #[tool_router]
  • #[tool_handler]
  • Manual new() constructor

Result: Reduced boilerplate code.

Attribute Macros§

elicitation
Mark a method as an elicitation handler.
mcp_client
The unified MCP client macro.
mcp_server
The unified MCP server macro.
on_connected
Mark a method as the connection established handler.
on_disconnected
Mark a method as the disconnection handler.
on_prompts_list_changed
Mark a method as a prompts list change notification handler.
on_resource_updated
Mark a method as a resource update notification handler.
on_resources_list_changed
Mark a method as a resources list change notification handler.
on_task_progress
Mark a method as a task progress notification handler.
on_tools_list_changed
Mark a method as a tools list change notification handler.
prompt
Mark a method as an MCP prompt handler.
resource
Mark a method as an MCP resource handler.
roots
Mark a method as a roots handler.
sampling
Mark a method as a sampling handler.
tool
Mark a method as an MCP tool.

Derive Macros§

ToolInput
Derive macro for tool input types.