MCP防锈剂
    
Rust中一个功能完整的模型上下文协议(MCP)服务器模板。这个初学者用干净、地道的Rust代码演示了MCP的所有主要功能。
📚 文档
✨ 特性
| 类别 | 功能 | 描述 |
|---|---|---|
| 工具 | hello | 基本问候工具 |
get_weather | 返回结构化JSON的工具 | |
calculator | 基本算术运算 | |
| 资源 | info://about | 静态信息资源 |
file://example.md | 基于文件的标记资源 | |
| 模板 | greeting://{name} | 个性化问候 |
data://items/{id} | 按ID查找数据 | |
| 提示 | greet | 各种风格的问候 |
code_review | 重点领域代码审查 |
注: mcp规范v0.1.0中尚未提供工具注释(readOnlyHint等)。 工具行为提示记录在描述字段中,直到板条箱更新。
🚀 快速开始
先决条件
安装
# Clone the repository
git clone https://github.com/SamMorrowDrums/mcp-rust-starter.git
cd mcp-rust-starter
# Build in debug mode
cargo build
# Build in release mode (optimized)
cargo build --release运行服务器
stdio传输 (地方发展):
cargo run --bin mcp-rust-starter-stdioHTTP传输 (用于远程/web部署):
cargo run --bin mcp-rust-starter-http
# Or with custom port:
PORT=8080 cargo run --bin mcp-rust-starter-http
# Server runs on http://localhost:3000 by default🔧 VS代码集成
该项目包括用于无缝开发的VS代码配置:
- 在VS Code中打开项目
- MCP配置位于
.vscode/mcp.json - 构建于
Ctrl+Shift+B(或Cmd+Shift+B在Mac上) - 使用F5进行调试(两种传输的配置)
- 使用VS Code的MCP工具测试服务器
使用DevContainers
- 安装 开发容器扩展
- 打开命令面板:“开发容器:在容器中重新打开”
- 一切都是预先配置好的,随时可以使用!
📁 项目结构
.
├── Cargo.toml # Package manifest with dependencies
├── rust-toolchain.toml # Rust toolchain pinning
├── rustfmt.toml # Formatter configuration
├── clippy.toml # Linter configuration
├── src/
│ ├── lib.rs # Server orchestration (Router impl)
│ ├── tools.rs # Tool definitions (hello, get_weather, etc.)
│ ├── resources.rs # Resource and template definitions
│ ├── prompts.rs # Prompt definitions
│ └── bin/
│ ├── stdio.rs # stdio transport entrypoint
│ └── http.rs # HTTP transport entrypoint
├── .vscode/
│ ├── mcp.json # MCP server configuration
│ ├── tasks.json # Build/run tasks
│ ├── launch.json # Debug configurations
│ └── extensions.json
├── .devcontainer/
│ └── devcontainer.json
└── LICENSE🛠️ 发展
# Development with live reload
cargo watch -x 'run --bin mcp-rust-starter-stdio'
# Requires cargo-watch: cargo install cargo-watch
# Build
cargo build
# Run tests
cargo test
# Format code (auto-fix)
cargo fmt
# Lint code
cargo clippy
# Lint with auto-fix
cargo clippy --fix
# Check without building
cargo check
# Build release
cargo build --release实时重新加载
安装 货物值班 对于自动重建:
cargo install cargo-watch
cargo watch -x 'run --bin mcp-rust-starter-stdio'对任何源文件的更改都将自动重建并重新启动服务器。
🔍 MCP检查员
这 MCP检查员 是测试和调试MCP服务器的重要开发工具。
运行检查器
npx @modelcontextprotocol/inspector -- cargo run --bin mcp-rust-starter-stdio检查员提供什么
- 工具选项卡:列出并调用所有已注册的带有参数的工具
- 资源选项卡:浏览和阅读资源和模板
- 提示选项卡:查看和测试提示模板
- 日志选项卡:请参阅客户端和服务器之间的JSON-RPC消息
- 模式验证:验证工具输入/输出模式
调试提示
- 在连接IDE/客户端之前启动检查器
- 使用“日志”选项卡查看确切的请求/响应有效载荷
- 测试工具注释(ToolAnnotations)正确显示
- 验证路由器特性实现是否正常工作
- 检查所有工具是否返回正确的内容类型
📖 功能示例
带注释的工具
Tool {
name: "hello".to_string(),
description: Some("A friendly greeting tool".to_string()),
input_schema: json!({
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name to greet"
}
},
"required": ["name"]
}),
annotations: Some(ToolAnnotations {
title: Some("Say Hello".to_string()),
read_only_hint: Some(true),
..Default::default()
}),
}资源实施
async fn read_resource(&self, uri: &str) -> Result, ResourceError> {
match uri {
"info://about" => Ok(vec![ResourceContents::text(
uri,
"MCP Rust Starter v1.0.0",
Some("text/plain"),
)]),
_ => Err(ResourceError::NotFound(uri.to_string())),
}
}快速定义
Prompt {
name: "greet".to_string(),
description: Some("Generate a greeting".to_string()),
arguments: Some(vec![
PromptArgument {
name: "name".to_string(),
description: Some("Name to greet".to_string()),
required: Some(true),
},
]),
}🔐 配置
环境变量:
PORT-HTTP服务器端口(默认值:3000)RUST_LOG-日志级别(默认值:info)
🧹 代码质量
此项目使用Rust的标准工具:
- rustfmt -代码格式(
cargo fmt) - Clippy -被迂腐的规则所困扰(
cargo clippy)
两者都在CI中自动运行,并在各自的CI中进行配置 .toml 文件夹。
🤝 贡献
欢迎投稿!请确保您的更改:
- 通过
cargo fmt --check - 通过
cargo clippy - 通过
cargo test - 与其他语言初学者保持功能对等
📄 许可证
MIT许可证-请参阅 许可证 了解详情。
