mcp-rust-analyzer

mcp-rust-analyzer is a powerful code analysis tool for the Rust programming language. It provides real-time error checking and code completion features, helping developers improve code quality. This makes Rust development more efficient and enjoyable.

GitHub Stars

3

User Rating

Not Rated

Favorites

0

Views

17

Forks

0

Issues

0

README
MCP Rust Analyzer

πŸ¦€ An intelligent Model Context Protocol (MCP) server that provides comprehensive Rust code analysis, refactoring, and navigation capabilities through rust-analyzer integration.

Rust
License: MIT
MCP

✨ Features
πŸ” Code Analysis
  • Hover Information: Get detailed type information and documentation
  • Find References: Locate all usage of symbols across your project
  • Go to Implementation: Navigate to trait implementations
  • Symbol Analysis: Deep dive into symbol definitions and relationships
⚑ Auto-completion & Navigation
  • Smart Completions: Context-aware code completions
  • Signature Help: Function parameter assistance
  • Diagnostics: Real-time error detection and suggestions
πŸ”§ Refactoring Tools
  • Rename Symbol: Safe rename across entire codebase
  • Extract Function: Extract code into reusable functions
  • Organize Imports: Clean up and optimize import statements
πŸ“Š Project Metrics
  • Code Statistics: Lines of code, complexity metrics
  • Dependency Analysis: Analyze Cargo.toml dependencies
  • Dead Code Detection: Find unused code in your project
  • Project Structure: Analyze module organization
🎯 Smart Templates
  • Code Snippets: Pre-built templates for common patterns
  • Match Expressions: Generate match arms automatically
  • Test Functions: Quick test function templates
πŸš€ Intelligent Operation Modes
  • Claude Code CLI: Automatic direct mode for maximum compatibility
  • Manual Terminal: Smart daemon/client system for optimal performance
  • TTY Detection: Automatically chooses the best mode based on context
πŸš€ Installation
Prerequisites
  • Rust toolchain (1.70+)
  • rust-analyzer component: rustup component add rust-analyzer
Install from Source
git clone https://github.com/pedrozoalencar/mcp-rust-analyzer.git
cd mcp-rust-analyzer
cargo build --release

# Add to PATH (permanent)
echo 'export PATH="/path/to/mcp-rust-analyzer/target/release:$PATH"' >> ~/.bashrc
source ~/.bashrc
πŸ“‹ Usage
With Claude Code CLI
  1. Add MCP Server:
claude mcp add rust-analyzer mcp-rust-analyzer
  1. Verify Installation:
claude mcp list
claude mcp test rust-analyzer
  1. Use in Any Rust Project:
cd your-rust-project/
claude  # Automatically detects and analyzes Rust code!
Manual Usage (Advanced)
Daemon Mode (Recommended for Manual Use)
# Start daemon for current project
mcp-rust-analyzer --daemon

# Check daemon status
mcp-rust-analyzer --status

# Stop daemon
mcp-rust-analyzer --stop

# Use client (auto-connects to daemon)
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | mcp-rust-analyzer
Direct Mode
# Direct stdin/stdout mode
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | mcp-rust-analyzer --server
πŸ› οΈ Architecture
Intelligent Mode Detection
  • Claude Code CLI: Automatically uses direct mode for maximum compatibility
  • Manual Terminal: Uses smart daemon/client system for optimal performance
  • TTY Detection: Automatically chooses the best mode based on context
Dual Operation Modes
Daemon/Client System (Manual Use)
  • Background Daemon: Persistent HTTP server per project
  • Auto-Port Selection: Finds available ports automatically
  • State Management: Tracks daemons across multiple projects
  • Auto-Start: Client automatically starts daemon if needed
Direct Mode (Claude Code CLI)
  • Zero Configuration: Works out-of-the-box
  • LSP Integration: Full rust-analyzer capabilities
  • Fast Startup: Optimized for MCP protocol
LSP Integration
  • Background Initialization: Non-blocking rust-analyzer startup
  • Smart Caching: Reuses LSP connections for performance
  • Error Handling: Graceful fallbacks when LSP unavailable
πŸ“š Available Tools
Tool Description
project_structure Analyze project module organization
code_metrics Get code statistics and metrics
analyze_dependencies View dependency graph
complete Get code completions at a position
get_hover Get type/documentation info
find_references Find all symbol references
rename Rename symbols safely
signature_help Get function signature help
get_diagnostics Get compiler diagnostics
analyze_symbol Analyze a symbol by name
find_implementations Find trait implementations
expand_snippet Expand code snippets
find_dead_code Detect unused code
suggest_improvements Get optimization suggestions
Resources

The server exposes these resources:

  • rust-analyzer://project/structure - Project structure
  • rust-analyzer://project/diagnostics - All diagnostics
  • rust-analyzer://project/dependencies - Dependency graph
  • rust-analyzer://project/symbols - Workspace symbols
Prompts

Pre-configured prompts for common tasks:

  • analyze_code - Comprehensive code analysis
  • refactor_code - Guided refactoring
  • explain_error - Error explanation with fixes
  • optimize_code - Performance optimization
Architecture
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Claude Code   │────▢│   MCP Server    │────▢│  rust-analyzer  β”‚
β”‚      (Host)     │◀────│  (This Project) │◀────│      (LSP)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        JSON-RPC              Rust API               LSP Protocol
Development
Project Structure
src/
β”œβ”€β”€ main.rs           # Entry point
β”œβ”€β”€ server.rs         # MCP server implementation
β”œβ”€β”€ analyzer.rs       # rust-analyzer integration
β”œβ”€β”€ lsp_client.rs     # LSP client for rust-analyzer
└── commands/         # Command handlers
    β”œβ”€β”€ analysis.rs   # Code analysis commands
    β”œβ”€β”€ completion.rs # IntelliSense commands
    β”œβ”€β”€ metrics.rs    # Metrics and structure
    └── refactor.rs   # Refactoring commands
Running Tests
# Run unit tests
cargo test

# Run integration tests
cargo test --test '*'

# Test MCP protocol compliance
./test_mcp_complete.sh
Debug Mode
# Run with debug logging
RUST_LOG=debug cargo run

# Test individual commands
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' | cargo run
Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Guidelines
  1. Follow Rust naming conventions
  2. Add tests for new features
  3. Update documentation
  4. Ensure all tests pass
  5. Format code with cargo fmt
License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Made with ❀️ for the Rust community