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§
- Tool
Input - Derive macro for tool input types.