Cargo MCP Server
[!CAUTION] This repository is written by AI as an experiment. Although I (jbr) review every line, the code quality is not necessarily identical to what I would have written. Caveat usor.
A Model Context Protocol (MCP) server that provides safe access to Cargo operations for Rust projects.
Features
This MCP server exposes the following Cargo tools:
- cargo_check - Verify code compiles without producing executables
- cargo_clippy - Run the Clippy linter for code suggestions
- cargo_test - Execute project tests
- cargo_fmt_check - Check code formatting without modifying files
- cargo_build - Build the project (debug or release mode)
- cargo_bench - Run benchmarks
- cargo_add - Add dependencies to Cargo.toml
- cargo_remove - Remove dependencies from Cargo.toml
- cargo_update - Update dependencies
- cargo_clean - Remove artifacts that cargo has generated in the past
All tools support setting custom environment variables via the cargo_env parameter and rust
toolchain with the toolchain parameter.
Installation
Usage with Claude Desktop
Add this to your Claude Desktop MCP configuration:
Optionally, include --default-toolchain TOOLCHAIN in the arguments, where TOOLCHAIN is something
like "stable" or "nightly".
Tool Usage
All tools accept an optional cargo_env parameter to set environment variables for the cargo command:
cargo_check
Verify that your Rust code compiles:
cargo_clippy
Get linting suggestions:
cargo_test
Run tests:
cargo_fmt_check
Check formatting:
cargo_build
Build the project:
cargo_bench
Run benchmarks:
cargo_add
Add dependencies:
cargo_remove
Remove dependencies:
cargo_update
Update dependencies:
cargo_clean
Update dependencies:
Environment Variables
The cargo_env parameter allows you to set environment variables that will be passed to the cargo
command. Common useful environment variables include:
- CARGO_LOG - Set logging level (
trace,debug,info,warn,error) - RUSTFLAGS - Pass flags to the Rust compiler (e.g.,
-C target-cpu=native) - CARGO_TARGET_DIR - Override the target directory for build artifacts
- RUST_TEST_THREADS - Control test parallelism
- CARGO_INCREMENTAL - Enable/disable incremental compilation
Safety Features
- Only whitelisted Cargo commands are available
- Path validation ensures the target is a valid Rust project (has Cargo.toml)
- No arbitrary command execution
- All commands run in the specified project directory
License
MIT or APACHE-2.0