Skip to main content
The 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.
name
string
required
The MCP server name
version
string
required
Server version
commands
Map<string, any>
required
Map of commands to expose as tools
options
serve.Options
Server configuration options
return
Promise<void>
Promise that resolves when the server connects and starts

Example

Types

serve.Options

Options for the MCP server.
input
Readable
Override input stream. Defaults to process.stdin.
output
Writable
Override output stream. Defaults to process.stdout.

How It Works

The MCP server:
  1. Flattens command hierarchy - Nested commands become flat tools with underscore-separated names (e.g., db_migrate)
  2. Merges schemas - Combines args and options into a single inputSchema for each tool
  3. Handles streaming - Supports async generator commands with progress notifications
  4. 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’s inputSchema merges both positional arguments and options into a flat object:

Streaming Commands

Async generator commands send progress notifications: