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.
⚠️ IMPORTANT NOTICE: Version 0.2.0 has been yanked due to critical issues with the SSE transport implementation. Please use version 0.2.2 instead, which includes important fixes for client-server communication, message handling, and template generation. If you're already using 0.2.0, we strongly recommend upgrading to 0.2.2 to avoid potential issues.
Examples
Check out our GitHub Tools example for a complete implementation of an MCP client-server application that interacts with GitHub repositories. This example demonstrates how to build a client that can query repository READMEs and search for repositories, with support for multiple servers and client-server disconnection scenarios. It's a great starting point for understanding how to build your own MCP applications.
Features
- Schema Definitions: Complete implementation of the MCP schema
- Transport Layer: Multiple transport options including stdio and SSE
- 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
- Mock Implementations: Built-in mock transports for testing and development
Coming Soon
- WebSocket Transport: WebSocket transport implementation is planned but not yet implemented
Installation
Add MCPR to your Cargo.toml:
[]
= "0.2.2" # Make sure to use 0.2.2 or later, as 0.2.0 has been yanked
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
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. The generated project includes a mock SSE transport implementation for testing:
# Run the server (default port is 8080)
# In another terminal, run the client
The SSE transport supports both interactive and one-shot modes:
# Interactive mode
# One-shot mode
The mock SSE transport implementation includes:
- Automatic response generation for initialization
- Echo-back functionality for tool calls
- Proper error handling and logging
- Support for all MCP message types
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 (Coming Soon)
WebSocket transport for bidirectional communication is currently under development.
Detailed Testing Guide
This section provides comprehensive instructions for generating and testing projects with both stdio and SSE transports.
Generating Projects
When generating projects, make sure to specify the correct transport type and output directory:
# Generate a stdio project
# Generate an SSE project
Note: The --output parameter specifies where to create the project directory. If omitted, the project will be created in the current directory.
Testing Stdio Transport Projects
-
Build the project:
&& && -
Run the server and client together:
|You should see output similar to:
[INFO] Using stdio transport [INFO] Initializing client... [INFO] Server info: {"protocol_version":"2024-11-05","server_info":{"name":"test-stdio-project-server","version":"1.0.0"},"tools":[{"description":"A simple hello world tool","input_schema":{"properties":{"name":{"description":"Name to greet","type":"string"}},"required":["name"],"type":"object"},"name":"hello"}]} [INFO] Running in one-shot mode with name: Default User [INFO] Calling tool 'hello' with parameters: {"name":"Default User"} [INFO] Received message: Hello, Default User! Hello, Default User! [INFO] Shutting down client [INFO] Client shutdown complete -
Run with detailed logging:
RUST_LOG=debug | RUST_LOG=debug -
Run with a custom name:
|
Testing SSE Transport Projects
-
Build the project:
&& && -
Run the server:
RUST_LOG=trace -
In another terminal, run the client:
RUST_LOG=traceYou should see output similar to:
[INFO] Using SSE transport with URI: http://localhost:8084 [INFO] Initializing client... [INFO] Server info: {"protocol_version":"2024-11-05","server_info":{"name":"test-sse-project-server","version":"1.0.0"},"tools":[{"description":"A simple hello world tool","input_schema":{"properties":{"name":{"description":"Name to greet","type":"string"}},"required":["name"],"type":"object"},"name":"hello"}]} [INFO] Running in one-shot mode with name: Test User [INFO] Calling tool 'hello' with parameters: {"name":"Test User"} [INFO] Received message: Hello, Test User! Hello, Test User! [INFO] Shutting down client [INFO] Client shutdown complete
Troubleshooting
Common Issues with Stdio Transport
-
Pipe Connection Issues:
- Ensure that the server output is properly piped to the client input
- Check for any terminal configuration that might interfere with piping
-
Process Termination:
- The server process will terminate after the client disconnects
- For long-running sessions, consider using the interactive mode
Common Issues with SSE Transport
-
Dependency Issues:
If you encounter dependency errors when building generated projects, you may need to update the
Cargo.tomlfiles to point to your local MCPR crate:# For local development, use path dependency: = { = "/path/to/your/mcpr" } -
Port Already in Use:
If the SSE server fails to start with a "port already in use" error, try a different port:
-
Connection Refused:
If the client cannot connect to the server, ensure the server is running and the port is correct:
# Check if the server is listening on the port | -
HTTP Method Not Allowed (405):
If you see HTTP 405 errors, ensure that the server is correctly handling all required HTTP methods (GET and POST) for the SSE transport.
-
Client Registration Issues:
The SSE transport requires client registration before message exchange. Ensure that:
- The client successfully registers with the server
- The client ID is properly passed in polling requests
- The server maintains the client connection state
Interactive Testing
Both transport types support interactive mode for manual testing:
# For stdio transport
# For SSE transport
In interactive mode, you can:
- Enter tool names and parameters manually
- Test different parameter combinations
- Observe the server's responses in real-time
Advanced Testing
For more advanced testing scenarios:
-
Testing with Multiple Clients:
The SSE transport supports multiple concurrent clients:
# Start multiple client instances in different terminals -
Testing Error Handling:
Test how the system handles errors by sending invalid requests:
# In interactive mode, try calling a non-existent tool > call -
Performance Testing:
For performance testing, you can use tools like Apache Bench or wrk to simulate multiple concurrent clients.
Debugging
Enable debug logging for detailed information:
# Set environment variable for debug logging
RUST_LOG=debug
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.