使用说明
简单MCP服务器
Rust中一个简洁的、生产就绪的模型上下文协议(MCP)服务器实现,演示了构建具有双传输支持的MCP工具的最佳实践。
快速开始
cargo run --release
# Server starts on http://localhost:8080/mcp
特性
- 简单工具 –添加整数和带有结构化JSON响应的回显消息
- 类型安全 –使用Rust的类型系统和
rmcp编译时安全宏 - 双重运输 –支持stdio JSON-RPC和HTTP流媒体
- 证据充分的 –清晰的示例和全面的文档
- 可观察对象 –通过结构化日志记录
tracing具有正确的stderr输出
需求
- 锈蚀1.75+ (在1.90+上测试)
- 货物 (包含在Rust中)
- 平台:macOS、Linux或Windows
建筑
src/
├── main.rs # Transport layer and runtime configuration
├── models.rs # CLI arguments and data schemas
└── server.rs # MCP server implementation and tool handlers
运行服务器
HTTP传输(默认)
# Start server with HTTP transport on port 8080
cargo run --release
# or explicitly
cargo run --release -- --stdio false
🔗 端点: http://localhost:8080/mcp
标准运输
# Start server with stdio transport for MCP clients
cargo run --release -- --stdio true
服务器从STDIN读取JSON-RPC消息并将响应写入STDOUT, 使其与Claude Desktop和Cursor等符合MCP的客户端兼容。
lsof -ti:8080 | xargs kill -9 # macOS/Linux
可用工具
| 工具 | 描述 | 输入模式 | 输出模式 |
|---|---|---|---|
add |
将两个整数相加 | { "a": i32, "b": i32 } |
{ "result": i32, "operation": "a + b" } |
echo |
回一条消息 | { "message": string } |
{ "message": "Echo: <input>" } |
所有回复都用 rmcp::handler::server::wrapper::Json,提供
为客户端提供JSON模式元数据的结构化内容。
示例用法
# HTTP API example
curl -X POST http://localhost:8080/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "add",
"arguments": {"a": 5, "b": 3}
}
}'
可观测性
- 日志记录:通过结构化日志记录
tracing到 标准错误 (保留JSON-RPC的stdout) - HTTP模式:启动日志包括绑定地址和传输详细信息
- 错误处理:全面的错误报告
McpError类型
# Enable debug logging
RUST_LOG=debug cargo run --release
扩展服务器
-
定义模型 -在中添加请求/响应类型
models.rs:#[derive(Deserialize, schemars::JsonSchema)] struct MyRequest { /* fields */ } -
工具 -添加
#[tool]方法inserver.rs:#[tool(name = "my_tool", description = "My custom tool")] async fn my_tool(&self, params: Parameters<MyRequest>) -> Result<Json<MyResponse>, McpError> { // Implementation } -
返回结构化结果 -始终返回
Result<Json<_>, McpError> -
格式和棉绒 -快跑
cargo fmt && cargo clippy保持代码质量
🔌 客户端配置
克劳德桌面
增添 ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"simple-mcp": {
"command": "cargo",
"args": ["run", "--release", "--", "--stdio", "true"],
"cwd": "/absolute/path/to/simple-mcp"
}
}
}
或
{
"mcpServers": {
"simple-mcp": {
"url": "http://localhost:8080/mcp",
}
}
}
光标
- 打开设置→ 特性→ MCP
- 使用以下命令添加新服务器:
cargo run --release -- --stdio true
MCP检查员
npx @modelcontextprotocol/inspector cargo run --release -- --stdio true
📚 其他资源
- 在Rust指南中构建MCP -综合教程
- MCP规范 -官方协议文件
- rmcp文件 -Rust实现细节
🤝 贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 如果适用,添加测试
- 提交拉取请求
📄 许可证
该项目目前未获得许可。请随意在内部进行调整;添加一个 在外部分发之前,请先明确许可证文件。
内置于❤️ 使用Rust和模型上下文协议