| Crates.io | mcprotocol-rs |
| lib.rs | mcprotocol-rs |
| version | 0.1.5 |
| created_at | 2025-03-11 15:53:24.980526+00 |
| updated_at | 2025-03-14 16:29:36.582892+00 |
| description | A Rust implementation of the Model Context Protocol (MCP) |
| homepage | |
| repository | https://github.com/Adiao1973/mcprotocol-rs |
| max_upload_size | |
| id | 1588072 |
| size | 163,038 |
⚠️ 开发状态: 本项目目前处于积极开发中,API 可能会发生变化。
⚠️ Development Status: This project is under active development and the API may change.
mcprotocol-rs 是 Model Context Protocol (MCP) 的 Rust 实现。它提供了一个完整的框架来实现 MCP 客户端和服务器。
mcprotocol-rs is a Rust implementation of the Model Context Protocol (MCP). It provides a complete framework for implementing MCP clients and servers.
完整实现 MCP 2024-11-05 规范
支持多种传输层
异步 API 设计
完整的类型安全
内置错误处理
可扩展的架构
Complete implementation of MCP 2024-11-05 specification
Multiple transport layer support
Asynchronous API design
Complete type safety
Built-in error handling
Extensible architecture
将以下内容添加到你的 Cargo.toml:
Add this to your Cargo.toml:
[dependencies]
mcprotocol-rs = "0.1.5"
use mcprotocol_rs::{
transport::{
ServerTransportFactory,
TransportConfig,
TransportType,
},
Result,
};
use std::net::SocketAddr;
#[tokio::main]
async fn main() -> Result<()> {
// 配置 HTTP 服务器
// Configure HTTP server
let addr = "127.0.0.1:3000".parse::<SocketAddr>().unwrap();
let config = TransportConfig {
transport_type: TransportType::Http {
base_url: addr.to_string(),
auth_token: Some("your-auth-token".to_string()),
},
parameters: None,
};
// 使用工厂创建服务器
// Create server using factory
let factory = ServerTransportFactory;
let mut server = factory.create(config)?;
// 初始化并启动服务器
// Initialize and start server
server.initialize().await?;
println!("Server started on {}", addr);
// 保持服务器运行
// Keep server running
tokio::signal::ctrl_c().await?;
Ok(())
}
use mcprotocol_rs::{
transport::{ClientTransportFactory, TransportConfig, TransportType},
protocol::{Message, Method, Request, RequestId},
Result,
};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<()> {
// 配置 HTTP 客户端
// Configure HTTP client
let config = TransportConfig {
transport_type: TransportType::Http {
base_url: "http://127.0.0.1:3000".to_string(),
auth_token: Some("your-auth-token".to_string()),
},
parameters: None,
};
// 创建客户端实例
// Create client instance
let factory = ClientTransportFactory;
let mut client = factory.create(config)?;
// 初始化客户端
// Initialize client
client.initialize().await?;
println!("Client connected to server");
// 创建并发送请求
// Create and send request
let request = Request::new(
Method::Ping,
None,
RequestId::String("ping-1".to_string()),
);
client.send(Message::Request(request)).await?;
// 接收响应
// Receive response
let response = client.receive().await?;
println!("Received response: {:?}", response);
// 关闭客户端
// Close client
client.close().await?;
Ok(())
}
查看完整示例代码 See full example code
查看完整示例代码 See full example code
基于 SSE 的客户端连接管理
自动清理断开的连接
保持连接活跃检测
支持客户端重连机制
SSE-based client connection management
Automatic cleanup of disconnected clients
Keep-alive connection detection
Support for client reconnection
基于请求 ID 的消息路由
支持请求-响应模式
支持通知消息
自动清理断开的连接
Request ID based message routing
Support for request-response pattern
Support for notification messages
Automatic cleanup of disconnected connections
支持 Bearer Token 认证
安全的消息传输
连接状态监控
Bearer Token authentication support
Secure message transmission
Connection state monitoring
你可以通过实现 Transport trait 来创建自己的传输层:
You can create your own transport layer by implementing the Transport trait:
use mcprotocol_rs::{
transport::Transport,
protocol::Message,
Result,
};
use async_trait::async_trait;
#[derive(Clone)]
struct MyTransport {
// 你的传输层字段
// Your transport fields
}
#[async_trait]
impl Transport for MyTransport {
async fn initialize(&mut self) -> Result<()> {
// 实现初始化逻辑
// Implement initialization logic
}
async fn send(&self, message: Message) -> Result<()> {
// 实现发送逻辑
// Implement send logic
}
async fn receive(&self) -> Result<Message> {
// 实现接收逻辑
// Implement receive logic
}
async fn close(&mut self) -> Result<()> {
// 实现关闭逻辑
// Implement close logic
}
}
src/
├── protocol/ # MCP 协议实现 | MCP protocol implementation
├── transport/ # 传输层实现 | Transport layer implementation
│ ├── http/ # HTTP/SSE 传输 | HTTP/SSE transport
│ │ ├── client.rs # HTTP 客户端 | HTTP client
│ │ └── server.rs # HTTP 服务器 | HTTP server
│ └── stdio/ # 标准输入/输出传输 | Stdio transport
│ ├── client.rs # Stdio 客户端 | Stdio client
│ └── server.rs # Stdio 服务器 | Stdio server
├── client_features/ # 客户端特性实现 | Client features implementation
├── server_features/ # 服务器特性实现 | Server features implementation
├── error.rs # 错误类型定义 | Error type definitions
└── lib.rs # 库入口和导出 | Library entry and exports
examples/
├── ping_example.rs # Ping/Pong 示例 | Ping/Pong example
├── lifecycle_client.rs # 生命周期客户端示例 | Lifecycle client example
├── lifecycle_server.rs # 生命周期服务器示例 | Lifecycle server example
├── stdio_client.rs # 标准输入/输出客户端示例 | Stdio client example
└── stdio_server.rs # 标准输入/输出服务器示例 | Stdio server example
欢迎提交 Pull Requests!对于重大更改,请先开 issue 讨论您想要更改的内容。
Pull Requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
本项目采用 MIT 许可证 - 详见 LICENSE 文件
This project is licensed under the MIT License - see the LICENSE file for details