一个基于 tokio 异步运行时的官方 Model Context Protocol SDK 实现。
本项目使用了以下开源库:
- rmcp: 实现 RMCP 协议的核心库 (详见:rmcp)
- rmcp-macros: 一个用于生成 RMCP 工具实现的过程宏库。 (详见:rmcp-macros)
rmcp = { version = "0.2.0", features = ["server"] }
## 或使用最新开发版本
rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", branch = "main" }基本依赖:
构建客户端
use rmcp::{ServiceExt, transport::{TokioChildProcess, ConfigureCommandExt}};
use tokio::process::Command;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ().serve(TokioChildProcess::new(Command::new("npx").configure(|cmd| {
cmd.arg("-y").arg("@modelcontextprotocol/server-everything");
}))?).await?;
Ok(())
}构建传输层
use tokio::io::{stdin, stdout};
let transport = (stdin(), stdout());构建服务
You can easily build a service by using ServerHandler or ClientHandler.
let service = common::counter::Counter::new();启动服务端
// this call will finish the initialization process
let server = service.serve(transport).await?;与服务端交互
Once the server is initialized, you can send requests or notifications:
// request
let roots = server.list_roots().await?;
// or send notification
server.notify_cancelled(...).await?;等待服务停止
let quit_reason = server.waiting().await?;
// 或将其取消
let quit_reason = server.cancel().await?;查看 examples
- containerd-mcp-server - 基于 containerd 实现的 MCP 服务
如果你想使用 Dev Container,查看 docs/DEVCONTAINER.md 获取开发指南。