MCPR - Model Context Protocol for Rust
A Rust implementation of Anthropic's Model Context Protocol (MCP), an open standard for connecting AI assistants to data sources and tools.
Features
- Schema Definitions: Complete implementation of the MCP schema
- Transport Layer: Multiple transport options including stdio, SSE, and WebSocket (all tested and working)
- High-Level Client/Server: Easy-to-use client and server implementations
- CLI Tools: Generate server and client stubs
- Project Generator: Quickly scaffold new MCP projects
Installation
Add MCPR to your Cargo.toml:
[]
= "0.2.0"
For CLI tools, install globally:
Usage
High-Level Client
The high-level client provides a simple interface for communicating with MCP servers:
use ;
// Create a client with stdio transport
let transport = new;
let mut client = new;
// Initialize the client
client.initialize?;
// Call a tool
let request = MyToolRequest ;
let response: MyToolResponse = client.call_tool?;
// Shutdown the client
client.shutdown?;
High-Level Server
The high-level server makes it easy to create MCP-compatible servers:
use ;
// Configure the server
let server_config = new
.with_name
.with_version
.with_tool;
// Create the server
let mut server = new;
// Register tool handlers
server.register_tool_handler?;
// Start the server with stdio transport
let transport = new;
server.start?;
Creating MCP Projects
MCPR includes a project generator to quickly scaffold new MCP projects with different transport types.
Using the CLI
# Generate a project with stdio transport
# Generate a project with SSE transport
# Generate a project with WebSocket transport
Project Structure
Each generated project includes:
my-project/
├── client/ # Client implementation
│ ├── src/
│ │ └── main.rs # Client code
│ └── Cargo.toml # Client dependencies
├── server/ # Server implementation
│ ├── src/
│ │ └── main.rs # Server code
│ └── Cargo.toml # Server dependencies
├── test.sh # Combined test script
├── test_server.sh # Server-only test script
├── test_client.sh # Client-only test script
└── run_tests.sh # Script to run all tests
Building Projects
# Build the server
# Build the client
Running Projects
Stdio Transport
For stdio transport, you typically run the server and pipe its output to the client:
# Run the server and pipe to client
|
Or use the client to connect to the server:
# Run the server in one terminal
# Run the client in another terminal
SSE Transport
For SSE transport, you run the server first, then connect with the client:
# Run the server (default port is 8080)
# In another terminal, run the client
Interactive Mode
Clients support an interactive mode for manual testing:
Running Tests
Each generated project includes test scripts:
# Run all tests
# Run only server tests
# Run only client tests
# Run the combined test (original test script)
Transport Options
MCPR supports multiple transport options:
Stdio Transport
The simplest transport, using standard input/output:
use StdioTransport;
let transport = new;
SSE Transport
Server-Sent Events transport for web-based applications:
use SSETransport;
// For server
let transport = new;
// For client
let transport = new;
WebSocket Transport
WebSocket transport for full-duplex communication:
use WebSocketTransport;
// For server
let transport = new;
// For client
let transport = new;
Debugging
Enable debug logging for detailed information:
# Set log level to debug
RUST_LOG=debug
# Capture logs to a file
RUST_LOG=debug
Contributing
Contributions are welcome! Here are some ways you can contribute:
- Implement additional transport options
- Add more examples
- Improve documentation
- Fix bugs and add features
License
This project is licensed under the MIT License - see the LICENSE file for details.