语言服务器MCP
通过MCP工具提供锈蚀分析仪功能的模型上下文协议(MCP)服务器。
设置
- 安装锈蚀分析仪:
rustup component add rust-analyzer- 构建MCP服务器:
cargo build --release使用Claude Desktop
配置
Claude Desktop使用配置文件注册MCP服务器。位置因平台而异:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - 视窗:
%APPDATA%\Claude\claude_desktop_config.json
将此配置添加到您的Claude Desktop配置文件中:
{
"mcpServers": {
"rust-analyzer": {
"command": "/path/to/language-server-mcp/target/release/language-server-mcp",
"args": ["/path/to/your/rust/workspace"]
}
}
}重要提示:
- 替换
/path/to/language-server-mcp此存储库的实际路径 - 替换
/path/to/your/rust/workspace通往Rust项目的路径 - 确保在您的PATH中安装并提供锈蚀分析仪
- 编辑配置后,完全退出并重新启动Claude Desktop
- 查找右下角的MCP服务器指示灯(锤子/工具图标)
使用桌面扩展(2025)
为了便于安装,您可以创建一个捆绑此MCP服务器的桌面扩展(.dxt文件)。这消除了对手动配置文件的需要。
使用Claude Code CLI
安装
首先,确保安装了Claude Code CLI:
npm install -g @anthropic-ai/claude-code配置方法1:CLI向导
使用内置的MCP配置向导:
claude mcp add rust-analyzer -s user -- /path/to/language-server-mcp/target/release/language-server-mcp /path/to/your/rust/workspace配置方法2:直接配置文件
要获得更多控制,请直接编辑Claude Code配置文件。在适合您平台的位置创建或编辑配置文件,并添加:
{
"mcpServers": {
"rust-analyzer": {
"type": "stdio",
"command": "/path/to/language-server-mcp/target/release/language-server-mcp",
"args": ["/path/to/your/rust/workspace"]
}
}
}验证
检查MCP服务器是否已连接:
# Inside Claude Code, run:
/mcp您应该看到:
⎿ MCP Server Status ⎿
⎿ • rust-analyzer: connected ⎿使用示例
配置后,您可以使用自然语言命令和Claude Code:
# Get diagnostics
"Check for errors in src/main.rs"
# Get type information
"What's the type of the variable at line 25, column 10?"
# Get completions
"What methods are available on this struct?"
# Find definitions
"Where is this function defined?"
# Search workspace
"Find all functions named 'parse' in the codebase"可用工具
悬停
在特定位置获取类型信息和文档。
参数:
file_path:Rust文件的路径line:行号(0索引)column:列号(0索引)
例子:
{
"file_path": "/home/user/project/src/main.rs",
"line": 10,
"column": 15
}完成
在特定位置获取代码补全。
参数:
file_path:Rust文件的路径line:行号(0索引)column:列号(0索引)
诊断
获取文件的编译错误和警告。
参数:
file_path:Rust文件的路径
goto定义
查找符号的定义位置。
参数:
file_path:Rust文件的路径line:行号(0索引)column:列号(0索引)
查找引用
查找对符号的所有引用。
参数:
file_path:Rust文件的路径line:行号(0索引)column:列号(0索引)include_declaration:在结果中包含声明(可选,默认值:true)
format_document
使用rustfmt格式化Rust文件。
参数:
file_path:Rust文件的路径
重命名
安全地重命名整个工作区中的符号。
参数:
file_path:Rust文件的路径line:行号(0索引)column:列号(0索引)new_name:符号的新名称
code_actions
在特定位置获取可用的快速修复和重构。
参数:
file_path:Rust文件的路径line:行号(0索引)column:列号(0索引)
工作空间_符号
在整个工作空间中搜索符号。
参数:
query:搜索查询字符串(符号名称模式)
例子:
{
"query": "Result"
}inlay_hints
获取文件的类型和参数提示。
参数:
file_path:Rust文件的路径
expand_macro
展开Rust宏以查看生成的代码。
参数:
file_path:Rust文件的路径line:行号(0索引)column:列号(0索引)
工作流示例
当使用像Claude这样的人工智能助手时:
- 获取文件的诊断信息:
“你能检查src/main.rs中的错误吗?”
- 获取类型信息:
src/lib.rs中第25行第10列的变量类型是什么
- 获得完成:
第30行第15列的对象有哪些可用方法
- 在工作区中搜索符号:
“在代码库中查找所有名为'parse'的函数”
- 获取镶嵌提示以更好地理解代码:
“显示src/main.rs的类型提示”
- 展开宏以理解生成的代码:
“在第15行,宏观展开到什么?”
- 获取快速修复和重构建议:
“此错误有哪些可用的代码操作?”
- 在整个工作区安全重命名符号:
“在所有使用此函数的地方将其重命名为'process_data'”
测试服务器
您可以通过发送JSON-RPC请求手动测试服务器:
# Start the server
cargo run
# In another terminal, send a request
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | cargo run发展
服务器由三个主要组件组成:
- MCP服务器 (
src/main.rs):处理MCP协议和工具路由 - LSP客户端 (
src/lsp_client.rs):管理锈蚀分析仪子流程 - 工具 (
src/tools.rs):定义可用的MCP工具
要添加新的LSP功能,请扩展工具定义并在中实现相应的处理程序 call_tool().
