Mcp module starts a stdio MCP server that exposes CLI commands as tools, enabling AI agents to discover and call your commands through the Model Context Protocol.
Functions
serve
Starts a stdio MCP server that exposes commands as tools.The MCP server name
Server version
Map of commands to expose as tools
Server configuration options
Promise that resolves when the server connects and starts
Example
Types
serve.Options
Options for the MCP server.Override input stream. Defaults to
process.stdin.Override output stream. Defaults to
process.stdout.How It Works
The MCP server:- Flattens command hierarchy - Nested commands become flat tools with underscore-separated names (e.g.,
db_migrate) - Merges schemas - Combines
argsandoptionsinto a singleinputSchemafor each tool - Handles streaming - Supports async generator commands with progress notifications
- Error handling - Returns structured errors using
context.error()or catches exceptions
Tool Naming
Commands are converted to tool names by joining the command path with underscores:Input Schema
The tool’sinputSchema merges both positional arguments and options into a flat object:
Streaming Commands
Async generator commands send progress notifications:Related
- SyncMcp - Register CLI as MCP server with AI agents
- Model Context Protocol - Official MCP documentation

