使用说明
Rust MCP服务器
Rust中的模型上下文协议(MCP)服务器实现,提供了各种与系统交互的工具。
特性
- JSON-RPC 2.0协议:在stdio上全面实施MCP
- 内置工具:
echo:回显提供的文本get_system_info:获取基本系统信息list_files:列出目录中的文件read_file:读取文件内容(有大小限制)execute_command:执行安全系统命令(限制列表)
- 异步/等待:与Tokio一起构建,用于异步操作
- 日志记录:带跟踪的结构化日志记录
- 安全:出于安全考虑,限制命令执行
安装
确保你已经安装了Rust,然后:
cargo build --release
使用Claude桌面应用程序
🚀 快速开始:参见 CLAUDE_SETUP.md 以获得简化的设置指南!
步骤1:构建服务器
首先,以发布模式构建服务器:
cargo build --release
这将在以下位置创建可执行文件 target/release/rust-mcp-server (或 target/release/rust-mcp-server.exe 在Windows上)。
步骤2:配置Claude桌面
快速设置:使用提供的辅助脚本生成配置:
./claude_config_helper.sh
此脚本将向您显示要使用的确切路径和配置。
手动设置:将MCP服务器添加到Claude Desktop配置中。配置文件的位置取决于您的操作系统:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%\Claude\claude_desktop_config.json
将此配置添加到您的 claude_desktop_config.json:
{
"mcpServers": {
"rust-mcp-server": {
"command": "/full/path/to/your/rust-mcp/target/release/rust-mcp-server",
"args": ["--name", "rust-tools"],
"env": {}
}
}
}
重要:替换 /full/path/to/your/rust-mcp/ 使用项目目录的实际绝对路径。
步骤3:示例配置
以下是一个完整的配置示例:
{
"mcpServers": {
"rust-mcp-server": {
"command": "/Users/username/projects/rust-mcp/target/release/rust-mcp-server",
"args": ["--name", "rust-tools", "--quiet"],
"env": {}
}
}
}
重要:始终包括 --quiet 在与Claude Desktop一起使用时,使用标记以防止日志输出干扰JSON响应。
步骤4:重新启动克劳德桌面
添加配置后:
- 完全退出 Claude Desktop(不仅仅是关闭窗口)
- 重启 克劳德桌面
- 寻找🔌 Claude界面中的(插入)图标,表示MCP服务器已连接
步骤5:使用Claude中的工具
连接后,您可以让Claude使用这些工具:
- “你能帮我回应一下《Hello World》吗?” -使用回声工具
- “我的系统信息是什么?” -使用get_system_info
- “列出我当前目录中的文件” -使用列表文件
- “阅读我的README.md文件的内容” -使用read_file
- “执行'date'命令” -使用execute_command
故障排除
服务器未连接:
- 验证可执行路径是否正确且绝对
- 检查可执行文件是否具有适当的权限(
chmod +x在Unix系统上) - 查看Claude Desktop的日志以获取错误消息
工具不工作:
- 启用调试模式
--debug旗帜 - 检查服务器日志是否有错误
- 验证当前工作目录是否具有适当的权限
查找可执行文件路径:
# From your project directory
pwd && echo "/target/release/rust-mcp-server"
# This will show you the full path to use in the config
高级配置
您可以使用其他参数自定义服务器行为:
{
"mcpServers": {
"rust-mcp-server": {
"command": "/path/to/rust-mcp/target/release/rust-mcp-server",
"args": [
"--name", "my-rust-tools",
"--version", "1.0.0",
"--debug"
],
"env": {
"RUST_LOG": "debug"
}
}
}
}
命令行用法
运行服务器
# Run with default settings
cargo run
# Run with debug logging
cargo run -- --debug
# Custom server name and version
cargo run -- --name "my-server" --version "1.0.0"
命令行选项
--debug, -d:启用调试日志记录--name, -n:设置服务器名称(默认:“rust-mcp server”)--version, -v:设置服务器版本(默认值:“0.1.0”)--help, -h:显示帮助消息
使用MCP客户端进行测试
您可以使用任何兼容MCP的客户端测试服务器。以下是JSON-RPC消息的示例:
- 初始化服务器:
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "test-client",
"version": "1.0.0"
}
}
}
- 确认初始化:
{
"jsonrpc": "2.0",
"id": 2,
"method": "initialized"
}
- 列出可用工具:
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/list"
}
- 调用工具:
{
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {
"name": "echo",
"arguments": {
"text": "Hello, World!"
}
}
}
可用工具
回声
回显提供的文本。
参数:
text(字符串,必填):要回显的文本
例子:
{
"name": "echo",
"arguments": {
"text": "Hello, World!"
}
}
get_system_info
获取基本的系统信息,包括操作系统、体系结构和主机名。
参数: 无
列表文件
列出指定路径中的文件和目录。
参数:
path(字符串,可选):列表的目录路径(默认值:“.”)
例子:
{
"name": "list_files",
"arguments": {
"path": "/tmp"
}
}
read_file
为了安全起见,读取有大小限制的文件内容。
参数:
path(string,必填):要读取的文件的路径max_size(整数,可选):最大文件大小(以字节为单位)(默认值:1MB)
例子:
{
"name": "read_file",
"arguments": {
"path": "./Cargo.toml"
}
}
execute_命令
从受限白名单中执行安全系统命令。
允许的命令: echo、date、whoami、pwd、ls、cat、head、tail、wc
参数:
command(string,必填):要执行的命令args(字符串数组,可选):命令参数
例子:
{
"name": "execute_command",
"arguments": {
"command": "ls",
"args": ["-la"]
}
}
建筑
服务器分为几个模块:
main.rs:CLI界面和应用程序入口点types.rs:MCP协议类型定义mcp.rs:核心MCP服务器实现server.rs:标准传输层tools.rs:工具注册表和实施
安全
此服务器实施了多种安全措施:
- 命令限制:只能执行白名单中的命令
- 文件大小限制:文件读取受到限制,以防止内存耗尽
- 输入验证:所有输入在处理前都经过验证
- 错误处理:全面的错误处理可防止崩溃
发展
添加新工具
要添加新工具,请执行以下操作:
- 实施
ToolHandler特质在tools.rs - 在中注册该工具
ToolRegistry::new() - 重建和测试
例子:
struct MyTool;
#[async_trait::async_trait]
impl ToolHandler for MyTool {
fn description(&self) -> String {
"Description of my tool".to_string()
}
fn input_schema(&self) -> Value {
json!({
"type": "object",
"properties": {
"param": {
"type": "string",
"description": "Parameter description"
}
},
"required": ["param"]
})
}
async fn call(&self, args: Value) -> Result<CallToolResponse> {
// Implementation here
}
}
测试
# Run tests
cargo test
# Test with Python client
cd examples
python3 test_client.py
# Run with logging
RUST_LOG=debug cargo test
# Check formatting
cargo fmt --check
# Run clippy
cargo clippy
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 添加测试
- 提交拉取请求
故障排除
常见问题
- 权限不足:确保可执行文件具有适当的权限
- 未找到命令:确保安装了所有依赖项
- JSON解析错误:验证JSON-RPC消息格式
调试模式
与一起跑步 --debug 标记以查看详细日志记录:
cargo run -- --debug
这将显示所有传入和传出的消息,从而更容易调试问题。
