Expand description
§MCP - Model Context Protocol SDK for Rust
A Rust SDK for the Model Context Protocol that simplifies server development
through a unified #[mcp_server] macro.
§Features
- Reduced boilerplate via unified
#[mcp_server]macro - Type-safe state machines via typestate pattern for connection lifecycle
- Rich error handling with context chains and miette diagnostics
- Full MCP 2025-11-25 protocol coverage including Tasks
- Runtime-agnostic async support
§Quick Start
ⓘ
use mcpkit::prelude::*;
use mcpkit::transport::stdio::StdioTransport;
struct Calculator;
#[mcp_server(name = "calculator", version = "1.0.0")]
impl Calculator {
#[tool(description = "Add two numbers")]
async fn add(&self, a: f64, b: f64) -> ToolOutput {
ToolOutput::text((a + b).to_string())
}
#[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
}§Comparison with rmcp
| Aspect | rmcp | This SDK |
|---|---|---|
| Macros | 4 interdependent | 1 unified #[mcp_server] |
| Boilerplate | Manual router wiring | Zero initialization |
| Parameters | Parameters<T> wrapper | Direct from signature |
| Error types | 3 nested layers | 1 unified McpError |
| Tasks | Not implemented | Full support |
§Crate Organization
mcpkit_core- Protocol types and traits (no async runtime)mcpkit_transport- Transport abstractions (stdio, HTTP, WebSocket)mcpkit_server- Server implementation with composable handlersmcpkit_client- Client implementationmcpkit_macros- Procedural macros for#[mcp_server]etc.
Modules§
- auth
- OAuth 2.1 Authorization for MCP.
- capability
- Capability negotiation types.
- client
- Client module re-exports.
- debug
- Debug and tracing utilities for MCP protocol development.
- error
- Error types and handling.
- extension
- Protocol extension infrastructure.
- prelude
- Prelude module for convenient imports.
- protocol
- JSON-RPC 2.0 protocol types for the Model Context Protocol.
- protocol_
version - Protocol version types and negotiation.
- schema
- JSON Schema utilities for MCP type validation.
- server
- Server module re-exports.
- state
- Typestate pattern for connection lifecycle management.
- transport
- Transport module re-exports.
- types
- MCP type definitions.
Structs§
- Cancellation
Token - A cancellation token for tracking request cancellation.
- Cancelled
Future - A future that completes when cancellation is requested.
- Client
Capabilities - Client capabilities advertised during initialization.
- Client
Info - Client information provided during initialization.
- Closing
- Marker type for closing state (shutdown in progress).
- Connected
- Marker type for connected state (transport established).
- Connection
- A connection in a specific state.
- Context
- Request context passed to handler methods.
- Context
Data - Owned data for creating contexts.
- Disconnected
- Marker type for disconnected state.
- Initialize
Request - Initialize request parameters.
- Initialize
Result - Initialize response.
- Initializing
- Marker type for initializing state (handshake in progress).
- Json
RpcError - A JSON-RPC error response object.
- NoOp
Peer - A no-op peer implementation for testing.
- Notification
- A JSON-RPC 2.0 notification message.
- Ready
- Marker type for ready state (fully operational).
- Request
- A JSON-RPC 2.0 request message.
- Response
- A JSON-RPC 2.0 response message.
- Server
- A configured MCP server ready to serve requests.
- Server
Builder - Builder for constructing MCP servers with specific capabilities.
- Server
Capabilities - Server capabilities advertised during initialization.
- Server
Info - Server information provided during initialization.
- Transport
Metadata - Metadata about a transport connection.
Enums§
- LogLevel
- Log levels for MCP logging.
- McpError
- The primary error type for the MCP SDK.
- Message
- A JSON-RPC 2.0 message (request, response, or notification).
- Progress
Token - A progress token for tracking long-running operations.
- Protocol
Version - MCP protocol versions in chronological order.
- Request
Id - A JSON-RPC request ID.
- Version
Negotiation Result - Protocol version negotiation result.
Constants§
- PROTOCOL_
VERSION - The latest protocol version supported by this implementation.
- SUPPORTED_
PROTOCOL_ VERSIONS - All protocol versions supported by this implementation.
Traits§
- Completion
Handler - Handler for completion suggestions.
- Elicitation
Handler - Handler for elicitation requests (structured user input).
- Logging
Handler - Handler for logging operations.
- McpResult
Ext - Extension trait for adding context to
Resulttypes. - Peer
- Trait for sending messages to the peer (client or server).
- Prompt
Handler - Handler for prompt-related operations.
- Resource
Handler - Handler for resource-related operations.
- Sampling
Handler - Handler for sampling requests (server-initiated LLM calls).
- Server
Handler - Core server handler trait - required for all MCP servers.
- Task
Handler - Handler for task-related operations.
- Tool
Handler - Handler for tool-related operations.
- Transport
- Core transport trait for MCP communication.
- Transport
Listener - Listener trait for server-side transports.
Functions§
- is_
version_ supported - Check if a protocol version is supported by this implementation.
- negotiate_
version - Negotiate a protocol version between client and server.
- negotiate_
version_ detailed - Perform version negotiation and return detailed result.
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.