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.
- Build:
cargo build --release - Configure your MCP client to use
target/release/rustmcp - Use through AI assistants with natural language prompts like "Generate a User struct with Debug and Clone derives"
find_definition- Navigate to symbol definitionsfind_references- Find all symbol usesget_diagnostics- Get compiler errors/warnings with fixesworkspace_symbols- Search project symbols
generate_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 templates
rename_symbol- Rename with scope awarenessextract_function- Extract code into functionsinline_function- Inline function callsorganize_imports- Sort and organize use statementsformat_code- Apply rustfmt formatting
apply_clippy_suggestions- Apply clippy automatic fixesvalidate_lifetimes- Check lifetime and borrow checker issues
analyze_manifest- Parse and analyze Cargo.tomlrun_cargo_check- Execute cargo check with error parsing
get_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 files
change_signature- Modify function signatures safely
- Rust toolchain (1.70+)
- rust-analyzer installed (defaults to
~/.cargo/bin/rust-analyzer) - An MCP-compatible client (Claude, Roo, etc.)
- Clone this repository:
git clone <repository-url>
cd rust-mcp- Build the server:
cargo build --release- The server binary will be available at
target/release/rustmcp
The 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/rustmcpOr 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/rustmcpThe 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 tools
src/analyzer/- rust-analyzer LSP client integrationclient.rs- LSP client implementation and protocol handling
src/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 runThe server exposes all tools through the MCP protocol. For debugging, you can:
- Run the server:
cargo run - Send MCP messages via stdin (JSON-RPC format)
- Check server logs and responses
- Create the tool implementation function in the appropriate
src/tools/*.rsfile - Add parameter struct to
src/server/parameters.rs - Add the tool to the
execute_toolmatch statement insrc/tools/types.rs - Add tool definition to
get_tools()function insrc/tools/types.rs - Add the corresponding
#[tool]method toRustMcpServerinsrc/server/handler.rs - Add analyzer client method to
src/analyzer/client.rsif needed
Ensure rust-analyzer is installed and accessible. The server will look for rust-analyzer in the following order:
- The path specified by the
RUST_ANALYZER_PATHenvironment variable - Default location:
~/.cargo/bin/rust-analyzer
To use a custom path, set the environment variable:
export RUST_ANALYZER_PATH=/custom/path/to/rust-analyzerOr configure it in your MCP client configuration (see Configuration section above).
- Verify the server binary path in your MCP client configuration
- Check that the binary has execute permissions:
chmod +x target/release/rustmcp - Ensure no other processes are using the same MCP server name
- Verify rust-analyzer works independently:
rust-analyzer --version - Check that your Rust project has a valid
Cargo.toml - Ensure the workspace path is correct when calling tools
- Fork the repository
- Create a feature branch
- Implement your changes with tests
- Submit a pull request