内容
# rust-analyzer-mcp
MCP(模型上下文协议)服务器,封装了 rust-analyzer LSP,用于 AI 助手,如 Claude、Cursor 和其他 MCP 客户端。
## 功能
- **完整的 LSP 集成**:通过语言服务器协议与 rust-analyzer 进行通信
- **MCP 工具**:15+ 个代码分析工具(跳转到定义、查找引用、悬停、补全等)
- **自动更新**:自动下载最新的 rust-analyzer nightly 版本
- **工作区管理**:支持多项目工作区
- **缓存**:内置结果缓存以提高性能
## 安装方法
### 1. 克隆和构建
```bash
git clone https://github.com/username/rust-analyzer-mcp.git
cd rust-analyzer-mcp
cargo build --release
```
二进制文件位于 `target/release/rust-analyzer-mcp.exe`(Windows)或 `target/release/rust-analyzer-mcp`(Linux/Mac)。
### 2. 配置 MCP 客户端
#### VS Code / Cursor / Windsurf
添加到 `.vscode/mcp.json`:
```json
{
"mcpServers": {
"rust-analyzer": {
"command": "path/to/rust-analyzer-mcp.exe",
"args": ["--project-root", "${workspaceFolder}"]
}
}
}
```
#### Claude Desktop
添加到 `~/Library/Application Support/Claude/claude_desktop_config.json`(Mac)或 `%APPDATA%\Claude\claude_desktop_config.json`(Windows):
```json
{
"mcpServers": {
"rust-analyzer": {
"command": "path/to/rust-analyzer-mcp.exe",
"args": ["--project-root", "${workspaceFolder}"]
}
}
}
```
#### OpenCode
添加到 `~/.config/opencode/opencode.json`:
```json
{
"mcp": {
"rust-analyzer": {
"command": ["path/to/rust-analyzer-mcp.exe"],
"args": ["--project-root", "${workspaceFolder}"],
"enabled": true,
"type": "local"
}
}
}
```
## 使用方法
### 命令行选项
```bash
rust-analyzer-mcp [选项]
选项:
--ra-path <路径> rust-analyzer 二进制文件的路径
--project-root <路径> 被分析的项目根目录
-c, --config <文件> 配置文件(默认:config/default.toml)
--no-auto-update 禁用自动更新
--log-level <级别> 日志级别(默认:info)
--health-check 健康检查模式(检查后退出)
-h, --help 打印帮助信息
```
### 环境变量
| 变量 | 描述 | 默认值 |
| --- | --- | --- |
| `RUST_ANALYZER_PATH` | 自定义 rust-analyzer 路径 | 自动检测 |
| `RUST_PROJECT_ROOT` | 默认项目根目录 | - |
| `RA_MCP_AUTO_UPDATE` | 启用自动更新 | true |
| `RA_MCP_LOG_LEVEL` | 日志级别 | info |
### 配置文件
查看 `config/default.toml` 获取所有选项:
```toml
[rust_analyzer]
auto_update = true
update_channel = "nightly"
[lsp]
timeout_seconds = 30
max_retries = 3
crash_recovery = true
[cache]
enabled = true
ttl_seconds = 300
max_entries = 1000
[security]
max_file_size_mb = 10
prevent_path_traversal = true
```
## MCP 工具
| 工具 | 描述 |
| --- | --- |
| `open_project` | 打开 Rust 项目工作区 |
| `status` | 获取服务器状态和版本 |
| `goto_definition` | 跳转到符号定义 |
| `find_references` | 查找符号的所有引用 |
| `hover` | 获取悬停信息 |
| `completions` | 获取自动补全 |
| `diagnostics` | 获取编译错误/警告 |
| `code_action` | 获取可用的代码操作 |
| `document_symbol` | 列出文件中的符号 |
| `workspace_symbol` | 在整个工作区搜索符号 |
| `inlay_hints` | 获取 inlay hints |
| `expand_macro` | 展开光标处的宏 |
| `format` | 使用 rustfmt 格式化文件 |
| `rename` | 重命名符号 |
## MCP 资源
| 资源 | 描述 |
| --- | --- |
| `rust://version` | 当前 rust-analyzer 版本 |
| `rust://config` | 服务器配置 |
## MCP 提示
| 提示 | 描述 |
| --- | --- |
| `analyze_code` | 分析当前位置的代码 |
| `explain_error` | 解释编译错误 |
## 开发
### 运行测试
```bash
cargo test
```
### 运行调试日志
```bash
RUST_LOG=debug cargo run
```
### 健康检查
```bash
cargo run -- --health-check
```
## 故障排除
### rust-analyzer 未找到
```bash
# 安装 rust-analyzer
rustup component add rust-analyzer
# 或通过系统包管理器安装
brew install rust-analyzer # macOS
sudo apt install rust-analyzer # Linux
```
### MCP 未连接
1. 确保二进制文件已正确构建
2. 使用 `--log-level debug` 检查日志
3. 验证 rust-analyzer 可访问:`cargo run -- --health-check`
### 工作区未检测到
确保在要打开的项目根目录下存在 `Cargo.toml`。
## 架构
```
JSON-RPC
┌─────────────┐ ┌─────────────┐
│ MCP │ ◄──► │ rust- │
│ 客户端 │ │ analyzer │
└─────────────┘ └─────────────┘
│
▼
┌─────────────────────────────────────────────┐
│ McpServer │
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
│ │ 工具 │ │ 资源 │ │ 提示 │ │
│ └─────────┘ └─────────┘ └─────────────┘ │
│ ┌─────────────────────────────────────────┐│
│ │ LspClient (LSP 协议) ││
│ └─────────────────────────────────────────┘│
└─────────────────────────────────────────────┘
```
## 许可证
MIT OR Apache-2.0