๐ MCPX: Model Context Protocol SDK for Rust
Hey there! Welcome to MCPX, your friendly neighborhood Rust implementation of the Model Context Protocol (MCP). We've built this library to be super robust, blazing fast, and delightfully easy to use whether you're building client apps or server systems.
๐ Overview
The Model Context Protocol (MCP) is the cool new standard for communication between Large Language Models (LLMs) and the applications that feed them context data. Think of it as the universal translator that helps your AI models and your apps have meaningful conversations.
This library gives you everything you need to implement the MCP specification (as of 2025-03-26), with full support for all the message types, capabilities, and flows that make the protocol tick. Whether you're building the next revolutionary AI assistant or creating tools that enhance existing models, MCPX has got your back.
โจ Features
- ๐ Complete MCP Implementation: Full support for the latest MCP specification (2025-03-26)
- โก Async All The Way: Built on Tokio for maximum performance and scalability
- ๐ก๏ธ Type-Safe API: Rust's type system keeps your code safe and predictable
- ๐งฉ Dual-Role Support: Easily implement clients, servers, or both
- ๐ Smart Connection Management: Handles reconnections and state management
- ๐ฆ Comprehensive Error Handling: Detailed error types for easy debugging
- ๐งฐ Extensible Architecture: Design your own extensions or customize existing ones
- ๐ Multiple Transports: Ready-to-use WebSocket and HTTP implementations included
๐ฆ Installation
Getting started with MCPX is super easy! Just add it to your Cargo.toml
:
[]
= "0.1.0"
= { = "1.0", = ["full"] } # You'll need Tokio for async goodness
If you're planning to use the WebSocket transport (which most folks do), you might also want:
= "0.26.0"
= "0.3"
๐ Quick Start
Let's dive right in with some examples to get you up and running!
๐งโ๐ป Client Example
Here's how to create a simple MCP client that connects to a server, lists available resources, and gracefully disconnects:
use ;
use mpsc;
async
// Handle events from the server
async
๐ฅ๏ธ Server Example
On the flip side, here's how to create an MCP server that provides resources, prompts, and tools to clients:
use ;
use async_trait;
use HashMap;
use mpsc;
// Our custom service implementation
// Function to handle server events
async
async
These examples should give you a good starting point, but there's so much more you can do with MCPX! Check out the examples directory for more complete implementations.
๐ง Protocol Support
MCPX implements all the cool features of the MCP protocol:
-
๐ Resources: Everything you need to work with resources
- List all available resources
- Read resource contents (text and binary)
- Work with resource templates
- Subscribe to resource updates
- Get notified when resources change
-
๐ฌ Prompts: Powerful prompt management
- List available prompts
- Get prompt templates with arguments
- Process templates with variable substitution
- Handle different prompt formats
-
๐ ๏ธ Tools: Extend your AI's capabilities
- List available tools
- Call tools with arguments
- Process tool results
- Handle tool errors gracefully
-
๐ Logging: Keep track of what's happening
- Set logging levels
- Send and receive log messages
- Filter logs by severity
-
๐ Completion: Smart suggestions
- Get completion suggestions for arguments
- Support for different completion types
- Handle partial inputs
-
๐ Roots: Organize your resources
- List available roots
- Get notified when roots change
- Navigate resource hierarchies
-
๐ฒ Sampling: Get AI responses
- Request completions from LLMs
- Stream responses in real-time
- Support for different content types
๐ Transport Options
MCPX comes with multiple ready-to-use transport implementations that make network communication a breeze:
WebSocket Transport
For real-time, bidirectional communication (recommended for most use cases):
// Client-side WebSocket setup
let client = new
.with_websocket_url
.build?;
HTTP Transport
For simpler REST-style communication or when WebSockets aren't available:
// Client-side HTTP setup
let client = new
.with_http_url
.build?;
Custom Transport
Want to use a different transport mechanism? No problem! You can implement your own custom transport by implementing the Transport
trait:
๐ Documentation
Want to dive deeper? Generate the full API documentation with:
This will build the docs and open them in your browser. The documentation includes detailed explanations of all types, methods, and examples of how to use them.
๐งช Examples
The repository includes several examples to help you get started:
- ๐ simple_client.rs: A basic MCP client that connects to a server and lists resources
- ๐ฅ๏ธ simple_server.rs: A basic MCP server that provides resources, prompts, and tools
- ๐ websocket_server.rs: A complete WebSocket MCP server implementation
- ๐ full_client.rs: A comprehensive client that demonstrates all client capabilities
To run an example:
๐ค Contributing
Contributions are always welcome! Whether it's bug reports, feature requests, or code contributions, we appreciate all help in making MCPX better.
- 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
๐ Roadmap
Here's what we're planning for future releases:
- Additional transport implementations (HTTP, gRPC)
- More comprehensive examples
- Performance optimizations
- Extended testing suite
- Integration with popular AI frameworks
๐ License
This project is licensed under the MIT License - see the LICENSE file for details. Feel free to use it in your projects, commercial or otherwise!
๐ Acknowledgements
- The MCP specification team for creating such a useful protocol
- The Rust community for the amazing ecosystem
- All contributors who have helped make this library better (right now, that's just me! ๐)
Happy coding! If you build something cool with MCPX, we'd love to hear about it! ๐