mcprotocol-rs
⚠️ 开发状态: 本项目目前处于积极开发中,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.
特性 | Features
-
完整实现 MCP 2024-11-05 规范
-
支持多种传输层
- HTTP/SSE 传输
- 基于 axum 的高性能服务器实现
- 支持 SSE 实时消息推送
- 内置认证支持
- 标准输入/输出传输
- 符合 MCP 规范的子进程管理
- 支持服务器日志捕获
- 自动处理进程生命周期
- HTTP/SSE 传输
-
异步 API 设计
- 基于 tokio 的异步运行时
- 完整的 Stream 支持
- 非阻塞 I/O 操作
-
完整的类型安全
-
内置错误处理
-
可扩展的架构
- 模块化的传输层设计
- 支持自定义传输实现
- 工厂模式创建实例
-
Complete implementation of MCP 2024-11-05 specification
-
Multiple transport layer support
- HTTP/SSE transport
- High-performance server implementation based on axum
- SSE real-time message push support
- Built-in authentication support
- Standard input/output transport
- MCP-compliant subprocess management
- Server log capture support
- Automatic process lifecycle handling
- HTTP/SSE transport
-
Asynchronous API design
- Based on tokio runtime
- Complete Stream support
- Non-blocking I/O operations
-
Complete type safety
-
Built-in error handling
-
Extensible architecture
- Modular transport layer design
- Custom transport implementation support
- Factory pattern for instance creation
安装 | Installation
将以下内容添加到你的 Cargo.toml
:
Add this to your Cargo.toml
:
[]
= "0.1.2"
快速开始 | Quick Start
HTTP 服务器示例 | HTTP Server Example
use ;
async
Stdio 传输示例 | Stdio Transport Example
首先创建服务器程序 examples/stdio_server.rs
:
First create the server program examples/stdio_server.rs
:
use ;
use json;
async
然后创建客户端程序 examples/stdio_client.rs
:
Then create the client program examples/stdio_client.rs
:
use ;
use json;
use ;
use sleep;
async
运行示例 | Running the example:
# 1. 首先编译服务器程序 | First, build the server
# 2. 然后运行客户端程序 | Then run the client
客户端会自动启动服务器进程并通过标准输入/输出进行通信。 The client will automatically start the server process and communicate through stdin/stdout.
自定义传输实现 | Custom Transport Implementation
你可以通过实现 Transport
trait 来创建自己的传输层:
You can create your own transport layer by implementing the Transport
trait:
use ;
use async_trait;
项目结构 | Project Structure
src/
├── client/ # 客户端实现 | Client implementation
├── server/ # 服务器实现 | Server implementation
├── 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
贡献 | Contributing
欢迎提交 Pull Requests!对于重大更改,请先开 issue 讨论您想要更改的内容。
Pull Requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
许可证 | License
本项目采用 MIT 许可证 - 详见 LICENSE 文件
This project is licensed under the MIT License - see the LICENSE file for details