This is a Model Context Protocol (MCP) server that provides integration with rust-analyzer, allowing AI assistants to analyze Rust code, get hover information, find definitions, references, and more. Written in Rust for optimal performance and native integration.
-
rust-analyzer: Make sure rust-analyzer is installed and available in your PATH
# Install via rustup (recommended) rustup component add rust-analyzer # Or install directly cargo install rust-analyzer # Verify installation rust-analyzer --version
-
Rust: Version 1.70 or higher with Cargo
-
A Rust project: The server works best with a valid Rust workspace (containing
Cargo.toml)
This Rust implementation offers several advantages over alternative implementations:
- Performance: Native Rust binary with minimal overhead
- Memory Safety: No runtime errors from memory issues
- Ecosystem Integration: Perfect fit for Rust development workflows
- Small Binary Size: Optimized release builds with LTO
- Concurrent Safety: Tokio async runtime handles multiple requests efficiently
- Native LSP Handling: Direct integration with rust-analyzer's protocol
Install directly from crates.io:
cargo install rust-analyzer-mcpThe binary will be installed to your Cargo bin directory (usually ~/.cargo/bin/rust-analyzer-mcp).
-
Clone the repository:
git clone https://github.com/zeenix/rust-analyzer-mcp.git cd rust-analyzer-mcp -
Build the project:
cargo build --release
-
The binary will be available at
target/release/rust-analyzer-mcp
Add an MCP server configuration to one of these locations:
Option 1: Project-specific (.mcp.json in your Rust project root):
{
"mcpServers": {
"rust-analyzer": {
"command": "rust-analyzer-mcp"
}
}
}Option 2: User-wide (~/.claude.json or ~/.claude/settings.json):
{
"mcpServers": {
"rust-analyzer": {
"command": "rust-analyzer-mcp"
}
}
}Note: If you installed from crates.io, the command will be in your PATH. If you built from source, use the full path to the binary. You can also configure servers using Claude Code's CLI wizard too.
Add this to your Claude Desktop configuration (claude_desktop_config.json):
{
"mcpServers": {
"rust-analyzer": {
"command": "rust-analyzer-mcp"
}
}
}Note: If installed from crates.io, the command will be in your PATH. For Claude Desktop, you
may want to specify a cwd parameter if you want to analyze a specific project by default.
Add the server to Codex:
# If installed from crates.io
codex mcp add rust-analyzer -- rust-analyzer-mcp
# If built from source
codex mcp add rust-analyzer -- /full/path/to/rust-analyzer-mcpYou can verify the entry with:
codex mcp listFor other MCP clients, run the server with:
./target/release/rust-analyzer-mcpOr during development:
cargo runThe server communicates via stdio and follows the MCP protocol.
Get all symbols (functions, structs, enums, etc.) in a file.
Parameters:
file_path: Path to the Rust file
Find the definition of a symbol at a specific position.
Parameters:
file_path: Path to the Rust fileline: Line number (0-based)character: Character position (0-based)
Find all references to a symbol at a specific position.
Parameters:
file_path: Path to the Rust fileline: Line number (0-based)character: Character position (0-based)
Get hover information (documentation, type info) for a symbol at a specific position.
Parameters:
file_path: Path to the Rust file (relative to workspace)line: Line number (0-based)character: Character position (0-based)
Get code completion suggestions at a specific position.
Parameters:
file_path: Path to the Rust fileline: Line number (0-based)character: Character position (0-based)
Format a Rust file using rust-analyzer's formatter. Returns an array of text edits to apply.
Parameters:
file_path: Path to the Rust file
Returns an empty array if the file is already formatted, or an array of edits with ranges and new text to apply.
Get available code actions (quick fixes, refactorings) for a range.
Parameters:
file_path: Path to the Rust fileline: Start line number (0-based)character: Start character position (0-based)end_line: End line number (0-based)end_character: End character position (0-based)
Note: Code actions availability depends on:
- rust-analyzer being fully indexed
- Having actual code issues or refactoring opportunities in the selected range
- May return empty array if no actions are applicable
Get diagnostics (errors, warnings, hints) for a specific file.
Parameters:
file_path: Path to the Rust file
Returns diagnostics with severity levels (error, warning, hint, information), messages, and location ranges. Includes a summary count of diagnostics by severity.
Get all diagnostics across the entire workspace.
Parameters: None
Returns aggregated diagnostics for all files in the workspace with file paths, severity levels, messages, and a summary of total counts by severity.
Change the workspace root directory.
Parameters:
workspace_path: Path to the new workspace root
Here are some example prompts you can use with Claude when this MCP server is configured:
-
Code Analysis:
Can you analyze the main function in src/main.rs and tell me what it does? Use the rust analyzer tools to get hover information and symbols. -
Finding Definitions:
I'm looking at a function call on line 25 of src/lib.rs at character position 10. Can you find its definition using rust-analyzer? -
Code Completion:
I'm writing code at line 15, character 8 in src/main.rs. What completion suggestions are available? -
Refactoring Help:
What code actions are available for the code between line 10-15 in src/utils.rs? -
Error Checking:
Can you get the diagnostics for src/main.rs and tell me about any errors or warnings? -
Workspace Analysis:
Show me all the diagnostics across the entire workspace using rust-analyzer.
rust-analyzer-mcp-server/
├── src/
│ └── main.rs # Main MCP server implementation
├── Cargo.toml # Rust dependencies and metadata
└── README.md # This file
To run in development mode:
cargo runTo build for release:
cargo build --releaseTo run tests:
cargo testTo check code without building:
cargo checkFor verbose logging during development:
RUST_LOG=debug cargo runTo run with release optimizations in dev:
cargo run --release- Ensure rust-analyzer is in your PATH:
which rust-analyzer - Try reinstalling:
rustup component add rust-analyzer
- Make sure you're running the server in a valid Rust workspace (with Cargo.toml)
- Check that the file paths are correct and relative to the workspace root
- Make sure the server has read access to your Rust files
- Check that rust-analyzer has permission to analyze your project
- The server handles LSP protocol automatically
- Check console output for any rust-analyzer errors (use
RUST_LOG=debugfor verbose logging) - Ensure your Rust project compiles successfully
- Make sure you have Rust 1.70+ installed:
rustc --version - Try
cargo cleanand rebuild if you encounter dependency issues
- rust-analyzer may take time to initially index large projects
- Subsequent requests should be much faster
- Consider excluding large target/ directories if needed
This is a foundation implementation that covers the most common rust-analyzer features. Contributions are welcome for:
- Additional LSP methods (workspace symbols, rename, etc.)
- Better error handling and diagnostics
- Configuration options (via CLI args or config files)
- Performance optimizations and async improvements
- Integration tests and benchmarks
- Better LSP message parsing and error recovery
- Support for additional rust-analyzer features
- Use
cargo fmtfor consistent formatting - Run
cargo clippyfor linting - Add tests for new functionality
- Update documentation for new tools
- Follow Rust async best practices with Tokio
This project is licensed under the MIT License - see the LICENSE file for details.