mcp-kit
An ergonomic, type-safe Rust library for building Model Context Protocol (MCP) servers.
MCP enables AI assistants to securely access tools, data sources, and prompts through a standardized protocol. This library provides a modern, async-first implementation with powerful procedural macros for rapid development.
Features
- 🚀 Async-first — Built on Tokio for high-performance concurrent operations
- 🛡️ Type-safe — Leverage Rust's type system with automatic JSON Schema generation
- 🎯 Ergonomic macros —
#[tool],#[resource],#[prompt]attributes for minimal boilerplate - 🔌 Multiple transports — stdio (default) and SSE/HTTP support
- 🧩 Modular — Feature-gated architecture, WASM-compatible core
- 📦 Batteries included — State management, error handling, tracing integration
- 🎨 Flexible APIs — Choose between macro-based or manual builder patterns
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
= { = "1", = ["derive"] }
= "0.8"
= "1" # For error handling
Minimum Supported Rust Version (MSRV): 1.80
Quick Start
Using Macros (Recommended)
The fastest way to build an MCP server with automatic schema generation:
use *;
/// Add two numbers
async
async
Manual API
For more control over schema and behavior:
use *;
use JsonSchema;
use Deserialize;
async
Core Concepts
Tools
Tools are functions that AI models can invoke. Define them with the #[tool] macro or manually:
// Macro approach
async
// Manual approach
let schema = to_value?;
builder.tool;
Error Handling:
async
Resources
Resources expose data (files, APIs, databases) to AI models:
// Static resource
async
// Template resource (dynamic URIs)
async
Prompts
Prompts provide reusable templates for AI interactions:
async
Transports
Stdio (Default)
Standard input/output transport for local process communication:
server.serve_stdio.await?;
SSE (Server-Sent Events)
HTTP-based transport for web clients:
// Requires the "sse" feature
server.serve_sse.await?;
Enable in Cargo.toml:
[]
= { = "0.1", = ["sse"] }
Advanced Features
State Management
Share state across tool invocations:
use Arc;
use Mutex;
// In your tool handler
let state = AppState ;
builder.tool;
Logging
Integrate with tracing for structured logging:
fmt
.with_writer // Log to stderr for stdio transport
.with_env_filter
.init;
info!;
Set log level:
RUST_LOG=my_server=debug
Error Handling
The library uses McpResult<T> and McpError:
use ;
async
Macro Reference
#[tool]
Generate tools from async functions:
async
Attributes:
description = "..."— Tool description (required)name = "..."— Tool name (optional, defaults to function name)
Supported return types:
String→ Converted toCallToolResult::textCallToolResult→ Used directlyResult<T, E>→ Error handling support
#[resource]
Generate resource handlers:
async
URI Templates:
Use {variable} syntax for dynamic resources:
#[prompt]
Generate prompt handlers:
async
Builder API Reference
builder
// Server metadata
.name
.version
.instructions
// Register tools
.tool // Manual API
.tool_def // From #[tool] macro
// Register resources
.resource // Static resource
.resource_template // URI template
.resource_def // From #[resource] macro
// Register prompts
.prompt
.prompt_def // From #[prompt] macro
.build
Examples
Run the included examples to see all features in action:
# Comprehensive showcase - all features
# Showcase with SSE transport on port 3000
# Macro-specific examples
Example Features:
- ✅ Multiple tool types (math, async, state management)
- ✅ Static and template resources
- ✅ Prompts with arguments
- ✅ Error handling patterns
- ✅ State sharing between requests
- ✅ JSON content types
- ✅ Both stdio and SSE transports
Source code: examples/
Feature Flags
Control which features to compile:
[]
= { = "0.1", = false, = ["server", "stdio"] }
Available features:
full(default) — All features enabledserver— Core server functionalitystdio— Standard I/O transportsse— HTTP Server-Sent Events transport
WASM compatibility:
Use default-features = false for WASM targets (only core protocol types).
Architecture
mcp-kit/
├── src/
│ ├── lib.rs # Public API and re-exports
│ ├── error.rs # Error types
│ ├── protocol.rs # JSON-RPC 2.0 implementation
│ ├── types/ # MCP protocol types
│ ├── server/ # Server implementation [feature = "server"]
│ └── transport/ # Transport implementations
└── macros/ # Procedural macros crate
Crate structure:
mcp-kit— Main librarymcp-kit-kit-macros— Procedural macros (#[tool], etc.)
Testing
# Run all tests
# Check formatting
# Run lints
# Check MSRV
Resources
- MCP Specification: https://modelcontextprotocol.io/
- Documentation: https://docs.rs/mcp-kit
- Repository: https://github.com/KSD-CO/mcp-kit
- Examples:
examples/ - CI/CD: GitHub Actions
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes with tests
- Ensure
cargo fmtandcargo clippypass - Submit a pull request
See AGENTS.md for development guidelines.
License
This project is licensed under the MIT License.
Changelog
See GitHub Releases for version history.
Built with ❤️ in Rust