🦀 一个智能的模型上下文协议(MCP)服务器,通过集成 rust-analyzer 提供全面的 Rust 代码分析、重构和导航功能。
rust-analyzer 组件:rustup component add rust-analyzergit clone https://github.com/pedrozoalencar/mcp-rust-analyzer.git
cd mcp-r
cargo build --release
# 添加到 PATH(永久)
echo 'export PATH="/path/to/mcp-rust-analyzer/target/release:$PATH"' >> ~/.bashrc
source ~/.bashrc
claude mcp add rust-analyzer mcp-rust-analyzer
claude mcp list
claude mcp test rust-analyzer
cd your-rust-project/
claude # 自动检测并分析 Rust 代码!
# 启动当前项目的守护进程
mcp-rust-analyzer --daemon
# 检查守护进程状态
mcp-rust-analyzer --status
# 停止守护进程
mcp-rust-analyzer --stop
# 使用客户端(自动连接到守护进程)
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | mcp-rust-analyzer
# 直接 stdin/stdout 模式
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | mcp-rust-analyzer --server
| 工具 | 描述 |
|---|---|
project_structure | 分析项目模块组织 |
code_metrics | 获取代码统计和指标 |
analyze_dependencies | 查看依赖图 |
complete | 在位置获取代码补全 |
get_hover | 获取类型/文档信息 |
find_references | 查找所有符号引用 |
rename | 安全地重命名符号 |
signature_help | 获取函数签名帮助 |
get_diagnostics | 获取编译器诊断 |
analyze_symbol | 根据名称分析符号 |
find_implementations | 查找特征实现 |
expand_snippet | 展开代码片段 |
find_dead_code | 检测未使用的代码 |
suggest_improvements | 获取优化建议 |
该服务器暴露了这些资源:
rust-analyzer://project/structure - 项目结构rust-analyzer://project/diagnostics - 所有诊断rust-analyzer://project/dependencies - 依赖图rust-analyzer://project/symbols - 工作区符号预配置提示用于常见任务:
analyze_code - 综合代码分析refactor_code - 引导式重构explain_error - 错误解释及修复optimize_code - 性能优化┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Claude Code │────▶│ MCP Server │────▶│ rust-analyzer │
│ (主机) │◀────│ (本项目) │◀────│ (LSP) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
JSON-RPC Rust API LSP 协议
src/
├── main.rs # 入口点
├── server.rs # MCP 服务器实现
├── analyzer.rs # rust-analyzer 集成
├── lsp_client.rs # rust-analyzer 的 LSP 客户端
└── commands/ # 命令处理器
├── analysis.rs # 代码分析命令
├── completion.rs # IntelliSense 命令
├── metrics.rs # 度量和结构
└── refactor.rs # 重构命令
# 运行单元测试
cargo test
# 运行集成测试
cargo test --test '*'
# 测试 MCP 协议合规性
./test_mcp_complete.sh
# 使用调试日志运行
RUST_LOG=debug cargo run
# 测试单独命令
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' | cargo run
欢迎贡献!请随时提交拉取请求。对于重大更改,请先打开一个问题来讨论您想要更改的内容。
cargo fmt 格式化代码该项目采用 MIT 许可证 - 详情见 LICENSE 文件。
为 Rust 社区制作 ❤️