MCP Protocol Client
Client library for the Model Context Protocol (MCP)
This crate provides a high-level, ergonomic API for building MCP clients in Rust. It handles connection management, capability negotiation, and transport abstraction, allowing you to focus on using MCP servers' capabilities.
โจ Features
- ๐ฆ Pure Rust - Zero-cost abstractions with memory safety
- ๐ฏ Type-Safe - Compile-time guarantees using mcp-protocol-types
- ๐ Async/Await - Built on Tokio for high performance
- ๐ Multiple Transports - STDIO, HTTP, WebSocket support
- ๐ ๏ธ Complete MCP Support - Tools, resources, prompts, logging
- ๐ฆ Lightweight - Minimal dependencies for fast builds
- ๐งช Well Tested - Comprehensive test suite
- ๐ Great Documentation - Examples and guides
๐ Quick Start
Add to your Cargo.toml:
[]
= "0.1.0"
= "0.1.0"
= { = "1.0", = ["full"] }
= "1.0"
Basic Client Example
use ;
use *;
async
๐๏ธ Architecture
The client library provides a layered architecture:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your Application โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ MCP Protocol Client โ โ This crate
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ MCP Protocol Types โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Transport Layer โ (STDIO, HTTP, WebSocket)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Core Concepts
Client Builder
The ClientBuilder provides a fluent API for configuring your client:
use ClientBuilder;
let client = new
.with_description
.with_timeout
.with_retry_policy
.build;
Connection Management
Connect to servers using different transports:
// STDIO transport (for local servers)
client.connect_stdio.await?;
// HTTP transport (for remote servers)
client.connect_http.await?;
// WebSocket transport (for real-time communication)
client.connect_websocket.await?;
Server Interaction
Use the client to interact with MCP servers:
// Initialize connection and exchange capabilities
let init_result = client.initialize.await?;
// List available tools
let tools = client.list_tools.await?;
// Call a tool
let result = client.call_tool.await?;
// List resources
let resources = client.list_resources.await?;
// Read a resource
let content = client.read_resource.await?;
// Get prompt templates
let prompts = client.list_prompts.await?;
// Get a prompt with arguments
let prompt = client.get_prompt.await?;
๐ง Feature Flags
| Feature | Description | Default |
|---|---|---|
stdio |
STDIO transport support | โ |
http |
HTTP transport support | โ |
websocket |
WebSocket transport support | โ |
๐ Usage Patterns
Tool Discovery and Execution
// Discover what tools are available
let tools = client.list_tools.await?;
// Find a specific tool
let calc_tool = tools.iter
.find
.ok_or?;
// Examine tool schema
println!;
// Call the tool with proper parameters
let result = client.call_tool.await?;
Resource Access
// List available resources
let resources = client.list_resources.await?;
// Read configuration files
let config = client.read_resource.await?;
// Process resource content
match config.contents.first
Error Handling
use ;
async
๐งช Testing
use MockServer;
async
๐ ๏ธ Development
# Build the crate
# Run tests
# Run with all features
# Generate documentation
๐ Related Crates
mcp-protocol-types- Core protocol typesmcp-protocol-server- Server librarymcp-protocol-sdk- Full-featured SDK
๐ค Contributing
This crate is part of the MCP Rust ecosystem. Contributions are welcome!
Guidelines
- API Design - Keep the API simple and ergonomic
- Performance - Optimize for low latency and memory usage
- Documentation - All public APIs need examples
- Testing - Comprehensive test coverage required
๐ Protocol Compliance
โ MCP 2024-11-05 Specification
This library implements the complete MCP client specification:
- JSON-RPC 2.0 protocol handling
- Capability negotiation and initialization
- Tool discovery and execution
- Resource access and content retrieval
- Prompt template processing
- Logging and debugging support
- Error handling and recovery
๐ License
Licensed under the MIT License.
๐ Acknowledgments
- Anthropic - For creating the MCP specification
- Tokio Team - For the excellent async runtime
- Rust Community - For the amazing ecosystem
Lightweight MCP client library for Rust ๐ฆ