Get the FREE Ultimate OpenClaw Setup Guide →

clap

A Rust framework that bridges clap command-line applications with the Model Context Protocol (MCP)

Installation
Run this command in your terminal to add the MCP server to Claude Code.
Run in terminal:
Command
claude mcp add --transport stdio gakonst-clap-mcp cargo run --bin <your-binary-name> \
  --env RUST_LOG="info"

How to use

clap-mcp enables converting any Clap-based CLI into an MCP server. By deriving McpMode on your CLI, every subcommand becomes an MCP tool automatically, and you can choose to run the CLI as a normal command or expose its functionality over MCP via an HTTP server or stdio channel. To use it, add clap-mcp to your Rust project and annotate your CLI struct with the McpMode derive, then launch your binary in MCP mode (via the --mcp flag). The macro will map each Clap subcommand argument to an MCP tool parameter, preserving types and defaults. This means tools like add, multiply, or any custom subcommands become independently accessible as MCP tools without writing extra glue code. When running in MCP mode, you can opt to serve over HTTP on a port with --mcp-port, or fall back to stdio for local inter-process communication.

How to install

Prerequisites:

  • Rust toolchain (rustc, cargo)
  • A Rust project configured to use Clap and clap-mcp
  1. Install Rust (if not already installed):

  2. Create or update your Cargo.toml to include clap-mcp: [dependencies] clap = "4" # or your existing Clap version clap-mcp = "0.1"

  3. Build and run your project in MCP mode:

    • Build: cargo build
    • Run in MCP mode (example): cargo run --bin <your-binary-name> -- --mcp
  4. If you want HTTP-based MCP, pass the port: cargo run --bin <your-binary-name> -- --mcp --mcp-port 8080

  5. Verify the MCP server is listening by connecting an MCP client or hitting the HTTP endpoint if enabled.

Notes:

  • Replace <your-binary-name> with the actual binary name defined in your Cargo.toml or your project’s build setup.
  • The clap-mcp derive macro automatically exposes each subcommand as an MCP tool, so no extra wiring is required.

Additional notes

Tips and considerations:

  • If you run in HTTP mode, ensure the port you choose is not blocked by a firewall and is accessible to MCP clients.
  • When debugging, enable logging via RUST_LOG=info to trace MCP interactions.
  • The MCP tools preserve CLI argument types; long/short flags, positional args, and subcommand structures map directly to tool parameters.
  • If your CLI uses complex clap validators or custom value parsers, they should generally carry over to MCP tool parameters as long as types are compatible.
  • For environment-specific configurations, you can inject environment variables into your MCP server process as needed (e.g., database URLs, API keys) and read them in your CLI as usual.

Related MCP Servers

Sponsor this space

Reach thousands of developers