Expand description
§clap-mcp
Expose your clap CLI as an MCP (Model Context Protocol) server over stdio.
§Quick start
Prefer a single run function with #[clap_mcp_output_from = "run"] so CLI and MCP
share one implementation (no duplicated logic).
ⓘ
use clap::Parser;
use clap_mcp::ClapMcp;
#[derive(Parser, ClapMcp)]
#[clap_mcp(reinvocation_safe, parallel_safe = false)]
#[clap_mcp_output_from = "run"]
enum Cli {
Greet { #[arg(long)] name: Option<String> },
}
fn run(cmd: Cli) -> String {
match cmd {
Cli::Greet { name } => format!("Hello, {}!", name.as_deref().unwrap_or("world")),
}
}
fn main() {
let cli = clap_mcp::parse_or_serve_mcp_attr::<Cli>();
println!("{}", run(cli));
}Run with --mcp to start the MCP server instead of executing the CLI.
Modules§
- content
- Custom MCP resources and prompts, and skill export. Custom MCP resources, prompts, and agent skills export.
Macros§
- clap_
mcp_ main - Convenience macro for struct root + subcommand CLIs: parse root then run.
- output_
schema_ one_ of - Builds a JSON schema with
oneOffor the given types. Used by the derive macro when#[clap_mcp_output_one_of = "T1, T2, T3"]is set. Requires theoutput-schemafeature and each type must implementschemars::JsonSchema.
Structs§
- AsStructured
- Wrapper for structured (JSON) output when using
#[clap_mcp_output_from]. Use when yourrunfunction returns a type that implementsSerializebut is notString/&str. - ClapArg
- Serializable representation of a clap argument.
- Clap
Command - A command or subcommand in the schema.
- Clap
McpConfig - Configuration for execution safety when exposing a CLI over MCP.
- Clap
McpSchema Metadata - Metadata for filtering and adjusting the MCP schema.
- Clap
McpServe Options - Optional configuration for MCP serve behavior (logging, etc.).
- Clap
McpTool Error - Error produced when a tool’s
runfunction returnsErr(e)(e.g.Result<O, E>). - Clap
Schema - Serializable schema extracted from a clap
Command. Used to build MCP tools and invoke the CLI.
Enums§
- Clap
McpError - Errors that can occur when running the MCP server.
- Clap
McpTool Output - Output produced by a CLI command for MCP tool results.
Constants§
- EXPORT_
SKILLS_ FLAG_ LONG - Long flag that triggers Agent Skills export (generates SKILL.md). Add via
command_with_export_skills_flag. - LOGGING_
GUIDE_ CONTENT - Full content for the logging guide prompt (returned when clients request
PROMPT_LOGGING_GUIDE). - LOG_
INTERPRETATION_ INSTRUCTIONS - Log interpretation hint for MCP clients (included in
instructionswhen logging is enabled). - MCP_
FLAG_ LONG - Long flag that triggers MCP server mode. Add to your CLI via
command_with_mcp_flag. - MCP_
RESOURCE_ URI_ SCHEMA - URI for the clap schema resource exposed by the MCP server.
- PROMPT_
LOGGING_ GUIDE - Name of the logging guide prompt.
Traits§
- Clap
McpConfig Provider - Provides MCP execution safety configuration from
#[clap_mcp(...)]attributes. Implemented by the#[derive(ClapMcp)]macro. - Clap
McpRunnable - Produces the output string for a parsed CLI value.
Used for in-process MCP tool execution when
reinvocation_safeis true. Implemented by the#[derive(ClapMcp)]macro via the blanket impl forClapMcpToolExecutor. - Clap
McpSchema Metadata Provider - Provides MCP schema metadata (skip, requires) from
#[clap_mcp(skip)]and#[clap_mcp(requires = "arg_name")]attributes. - Clap
McpTool Executor - Produces MCP tool output (text or structured) for a parsed CLI value.
- Into
Clap McpResult - Converts the return value of a
runfunction (used with#[clap_mcp_output_from]) into MCP tool output or error. - Into
Clap McpTool Error - Converts an error type from a
runfunction intoClapMcpToolError. Used whenrunreturnsResult<O, E>and theErrbranch is taken. - Parse
OrServe Mcp - Canonical entrypoint for derive-based CLIs: parse (or serve if
--mcp) and return self.
Functions§
- command_
with_ export_ skills_ flag - Adds a root-level
--export-skillsflag (optional value for output directory) to aclap::Command. - command_
with_ mcp_ and_ export_ skills_ flags - Adds both
--mcpand--export-skillsflags to the command. Use this so schema extraction omits both; check for export-skills before mcp in the parse flow. - command_
with_ mcp_ flag - Adds a root-level
--mcpflag to aclap::Command(imperative clap usage). - get_
matches_ or_ serve_ mcp - Imperative clap entrypoint.
- get_
matches_ or_ serve_ mcp_ with_ config - Imperative clap entrypoint with execution safety configuration.
- get_
matches_ or_ serve_ mcp_ with_ config_ and_ metadata - Imperative clap entrypoint with execution safety configuration and schema metadata.
- output_
schema_ for_ type - parse_
or_ serve_ mcp - High-level helper for
clapderive-based CLIs. - parse_
or_ serve_ mcp_ attr - High-level helper for
clapderive-based CLIs with config from#[clap_mcp(...)]attributes. - parse_
or_ serve_ mcp_ with_ config - High-level helper for
clapderive-based CLIs with execution safety configuration. - parse_
or_ serve_ mcp_ with_ config_ and_ options - Like
parse_or_serve_mcp_with_configbut with custom serve options (e.g. logging). - run_
async_ tool - Runs an async future for MCP tool execution, respecting
share_runtimein config. - run_
or_ serve_ mcp - Run parsed CLI through a closure, or serve MCP if
--mcpis present. - schema_
from_ command - Extracts a serializable schema from a
clap::Command(imperative clap usage). - schema_
from_ command_ with_ metadata - Extracts a schema from a
clap::Commandwith MCP metadata applied. - serve_
schema_ json_ over_ stdio - Starts an MCP server over stdio exposing
clap://schemawith the provided JSON payload. - serve_
schema_ json_ over_ stdio_ blocking - Convenience wrapper for
serve_schema_json_over_stdiothat blocks on a tokio runtime. - tools_
from_ schema - Builds MCP tools from a clap schema.
- tools_
from_ schema_ with_ config - Builds MCP tools from a clap schema with execution safety annotations.
- tools_
from_ schema_ with_ config_ and_ metadata - Builds MCP tools from a clap schema with config and optional metadata.
When
metadata.output_schemais set, each tool’soutput_schemafield is set to that value. Whenmetadata.skip_root_command_when_subcommandsis true and the root has subcommands, the root command is excluded from the tool list (only subcommands become tools).
Type Aliases§
- InProcess
Tool Handler - Type for in-process tool execution handler.
Derive Macros§
- ClapMcp
- Derive macro for
ClapMcpConfigProviderandClapMcpToolExecutor.