Model Context Protocol (MCP)
A comprehensive Rust implementation of the Model Context Protocol (MCP) for AI tool integration.
Overview
This workspace provides a complete MCP implementation for building MCP servers and clients in Rust. The Model Context Protocol enables seamless communication between AI models and tool providers.
Crates
| Crate | Description |
|---|---|
model-context-protocol |
Core MCP implementation with transports, protocol types, and hub |
model-context-protocol-macros |
Procedural macros for declarative server/tool definitions |
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
Function-Based Tools
The simplest way to create an MCP server - just annotate functions:
use mcp_tool;
use ;
async
Struct-Based Servers
For more complex servers with shared state:
use mcp_server;
use ;
;
async
Connecting to Servers
Use McpHub to manage multiple MCP server connections:
use ;
async
Features
- JSON-RPC 2.0 Protocol: Full implementation of the MCP JSON-RPC protocol
- Multiple Transports: Support for stdio and HTTP-based MCP servers
- McpHub: Central hub for managing multiple MCP server connections
- Procedural Macros:
#[mcp_server],#[mcp_tool], and#[param]for declarative definitions - Parameter Descriptions:
#[param("description")]provides LLM-friendly parameter documentation - Tool Groups: Organize tools by group for auto-discovery
- Tool Routing: Automatic routing of tool calls to the correct server
- Error Handling: Built-in
ToolResult<T>for ergonomic error handling
Feature Flags
| Feature | Default | Description |
|---|---|---|
stdio |
✓ | Stdio transport for spawning server processes |
http |
✓ | HTTP transport for connecting to HTTP servers |
macros |
✓ | Procedural macros for defining tools and servers |
http-server |
HTTP server support with actix-web |
Examples
The workspace includes several example servers:
| Example | Description |
|---|---|
calculator-server |
Function-based tools with stdio transport |
macro-calculator |
Struct-based server using #[mcp_server] |
text-tools-server |
HTTP transport with text manipulation tools |
notes-server |
ToolProvider pattern with shared state |
Run an example:
Protocol Version
This crate implements MCP protocol version 2024-11-05.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.