A comprehensive Model Context Protocol (MCP) server that provides rust-analyzer integration for LLM-assisted Rust development. This server enables AI tools like Claude to work with Rust code idiomatically through rust-analyzer's Language Server Protocol capabilities, avoiding string manipulation and providing intelligent code analysis and refactoring.
cargo build --releasetarget/release/rustmcpfind_definition - Navigate to symbol definitionsfind_references - Find all symbol uses get_diagnostics - Get compiler errors/warnings with fixesworkspace_symbols - Search project symbolsgenerate_struct - Create structs with derives and constructorsgenerate_enum - Create enums with variantsgenerate_trait_impl - Generate trait implementations with stubsgenerate_tests - Create unit or integration test templatesrename_symbol - Rename with scope awarenessextract_function - Extract code into functionsinline_function - Inline function callsorganize_imports - Sort and organize use statementsformat_code - Apply rustfmt formattingapply_clippy_suggestions - Apply clippy automatic fixesvalidate_lifetimes - Check lifetime and borrow checker issuesanalyze_manifest - Parse and analyze Cargo.tomlrun_cargo_check - Execute cargo check with error parsingget_type_hierarchy - Get type relationships for symbolssuggest_dependencies - Recommend crates based on code patternscreate_module - Create new Rust modules with visibility controlmove_items - Move code items between fileschange_signature - Modify function signatures safely~/.cargo/bin/rust-analyzer)git clone <repository-url>
cd rust-mcp
cargo build --release
target/release/rustmcpThe server supports the following environment variables:
RUST_ANALYZER_PATH - Path to rust-analyzer binary (default: ~/.cargo/bin/rust-analyzer)You can set this when running the server:
RUST_ANALYZER_PATH=/usr/local/bin/rust-analyzer ./target/release/rustmcp
Or set it in your MCP client configuration (see examples below).
Add the following to your Claude Desktop MCP configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"rust-analyzer": {
"command": "/path/to/rust-mcp/target/release/rustmcp",
"args": [],
"env": {
"RUST_ANALYZER_PATH": "/custom/path/to/rust-analyzer"
}
}
}
}
If rust-analyzer is in the default location (~/.cargo/bin/rust-analyzer), you can omit the env section:
{
"mcpServers": {
"rust-analyzer": {
"command": "/path/to/rust-mcp/target/release/rustmcp",
"args": []
}
}
}
Add to your Roo configuration file (typically ~/.roo/config.json):
{
"mcp_servers": [
{
"name": "rust-analyzer",
"command": "/path/to/rust-mcp/target/release/rustmcp",
"args": [],
"env": {
"RUST_ANALYZER_PATH": "/custom/path/to/rust-analyzer"
}
}
]
}
For default rust-analyzer location, you can use an empty env object:
{
"mcp_servers": [
{
"name": "rust-analyzer",
"command": "/path/to/rust-mcp/target/release/rustmcp",
"args": [],
"env": {}
}
]
}
For any MCP-compatible client, configure it to run:
/path/to/rust-mcp/target/release/rustmcp
The server uses stdio transport and will be ready to accept MCP protocol messages.
Once configured, you can use the tools through your AI assistant. Here are some example prompts:
"Find all references to the `Config` struct in this Rust project"
"Show me the definition of the `parse_args` function"
"Check for compiler errors in src/main.rs"
"Search for all symbols matching 'user' in the workspace"
"Generate a struct called `User` with fields: name (String), age (u32), email (String), with Debug and Clone derives"
"Create an enum called `HttpStatus` with variants: Ok, NotFound, ServerError"
"Generate unit tests for the `calculate_total` function"
"Generate a Display trait implementation for the User struct"
"Rename the variable `data` to `user_input` throughout the codebase"
"Extract this code block into a separate function called `validate_input`"
"Inline the `helper_function` call on line 42"
"Organize all import statements in src/lib.rs"
"Format all the code in src/lib.rs"
"Run clippy and apply all automatic fixes to improve code quality"
"Check for any lifetime or borrow checker issues in src/auth.rs"
"Analyze the Cargo.toml file and show dependency information"
"Run cargo check and report any compilation errors"
"Show me the type hierarchy for the symbol at line 15, character 8 in src/main.rs"
"Suggest crate dependencies for HTTP client functionality in this workspace"
"Create a new public module called 'auth' in src/auth.rs"
"Move the User struct and validate_user function from src/main.rs to src/user.rs"
"Change the signature of the process_data function to accept a reference instead of ownership"
The server is built with a modular architecture:
src/main.rs - Entry point and server initializationsrc/lib.rs - Module declarationssrc/server/ - MCP server implementationhandler.rs - Tool handlers and MCP server logic using rmcp crateparameters.rs - Parameter type definitions for all toolssrc/analyzer/ - rust-analyzer LSP client integrationclient.rs - LSP client implementation and protocol handlingsrc/tools/ - Modular tool implementationstypes.rs - Tool dispatcher and definitionsanalysis.rs - Code analysis tools (find_definition, find_references, etc.)generation.rs - Code generation tools (generate_struct, generate_enum, etc.)refactoring.rs - Refactoring tools (rename_symbol, extract_function, etc.)formatting.rs - Code formatting toolsquality.rs - Quality assurance tools (clippy, lifetimes)cargo.rs - Project management toolsnavigation.rs - Navigation tools (workspace_symbols)advanced.rs - Advanced features (type hierarchy, dependencies, modules)cargo run
The server exposes all tools through the MCP protocol. For debugging, you can:
cargo runsrc/tools/*.rs filesrc/server/parameters.rsexecute_tool match statement in src/tools/types.rsget_tools() function in src/tools/types.rs#[tool] method to RustMcpServer in src/server/handler.rssrc/analyzer/client.rs if neededEnsure rust-analyzer is installed and accessible. The server will look for rust-analyzer in the following order:
RUST_ANALYZER_PATH environment variable~/.cargo/bin/rust-analyzerTo use a custom path, set the environment variable:
export RUST_ANALYZER_PATH=/custom/path/to/rust-analyzer
Or configure it in your MCP client configuration (see Configuration section above).
chmod +x target/release/rustmcprust-analyzer --versionCargo.toml$ claude mcp add rust-mcp \
-- python -m otcore.mcp_server <graph>