# mcp

URL: /milo-cli/api/milo/mcp/
Section: milo
Description: MCP server — expose CLI commands as tools via JSON-RPC on stdin/stdout.

---

> For a complete page index, fetch /milo-cli/llms.txt.

Open LLM text
(/milo-cli/api/milo/mcp/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fmilo-cli%2Fapi%2Fmilo%2Fmcp%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fmilo-cli%2Fapi%2Fmilo%2Fmcp%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fmilo-cli%2Fapi%2Fmilo%2Fmcp%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fmilo-cli%2Fapi%2Fmilo%2Fmcp%2Findex.txt)

Module

#
`mcp`

MCP server — expose CLI commands as tools via JSON-RPC on stdin/stdout.

1Class18Functions

## Classes

`_CLIHandler`

8

▼

MCPHandler implementation for a single CLI (leaf server).

MCPHandler implementation for a single CLI (leaf server).

#### Methods

`initialize`

1

`dict[str, Any]`

▼

`def initialize(self, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`params`
`—`

##### Returns

`dict[str, Any]`

`list_tools`

1

`dict[str, Any]`

▼

`def list_tools(self, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`params`
`—`

##### Returns

`dict[str, Any]`

`call_tool`

1

`dict[str, Any]`

▼

`def call_tool(self, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`params`
`—`

##### Returns

`dict[str, Any]`

`list_resources`

1

`dict[str, Any]`

▼

`def list_resources(self, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`params`
`—`

##### Returns

`dict[str, Any]`

`read_resource`

1

`dict[str, Any]`

▼

`def read_resource(self, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`params`
`—`

##### Returns

`dict[str, Any]`

`list_prompts`

1

`dict[str, Any]`

▼

`def list_prompts(self, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`params`
`—`

##### Returns

`dict[str, Any]`

`get_prompt`

1

`dict[str, Any]`

▼

`def get_prompt(self, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`params`
`—`

##### Returns

`dict[str, Any]`

Internal Methods
1

▼

`__init__`

2

▼

`def __init__(self, cli: CLI, cached_tools: list[dict[str, Any]] | None = None) -> None`

##### Parameters

Name
Type
Description

`cli`
`—`

`cached_tools`
`—`

Default:`None`

## Functions

`_to_text`

1

`str`

▼

Convert a result to text for MCP content responses.

`def _to_text(result: Any) -> str`

##### Parameters

Name
Type
Description

`result`
`Any`

##### Returns

`str`

`run_mcp_server`

1

`None`

▼

Run MCP JSON-RPC server on stdin/stdout.

Implements the MCP protocol (initiali…

`def run_mcp_server(cli: CLI) -> None`

Run MCP JSON-RPC server on stdin/stdout.

Implements the MCP protocol (initialize, tools/list, tools/call,
resources/list, resources/read, prompts/list, prompts/get).

##### Parameters

Name
Type
Description

`cli`
`CLI`

`_classify_exception`

1

`tuple[int, dict[str, Any…`

▼

Map an exception to a JSON-RPC error code and optional data payload.

Returns (…

`def _classify_exception(exc: Exception) -> tuple[int, dict[str, Any] | None]`

Map an exception to a JSON-RPC error code and optional data payload.

Returns (code, data) where:

- MiloError with validation/config codes -> -32602 (Invalid params)

- MiloError with not-found codes -> -32601 (Method not found)

- All others -> -32603 (Internal error) with traceback in data

When the MiloError carries`argument` or `constraint`context, those
fields are included in the data payload so callers can tell agents which
parameter failed and what constraint was violated.

##### Parameters

Name
Type
Description

`exc`
`Exception`

##### Returns

`tuple[int, dict[str, Any] | None]`

`_milo_error_data`

1

`dict[str, Any]`

▼

Build the structured data payload for a MiloError.

`def _milo_error_data(exc: Any) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`exc`
`Any`

##### Returns

`dict[str, Any]`

`_constraint_example`

1

`Any`

▼

Derive an example value from a JSON-Schema-style constraint dict.

Never uses u…

`def _constraint_example(constraint: dict[str, Any]) -> Any`

Derive an example value from a JSON-Schema-style constraint dict.

Never uses user input. Returns`None`when no safe example applies.

##### Parameters

Name
Type
Description

`constraint`
`dict[str, Any]`

##### Returns

`Any`

`_builtin_resources`

0

`list[dict[str, Any]]`

▼

Built-in MCP resources provided by the milo runtime.

`def _builtin_resources() -> list[dict[str, Any]]`

##### Returns

`list[dict[str, Any]]`

`_stats_resource`

1

`dict[str, Any]`

▼

Return server statistics as an MCP resource.

`def _stats_resource(logger: RequestLogger) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`logger`
`RequestLogger`

##### Returns

`dict[str, Any]`

`_pipeline_timeline_resource`

0

`dict[str, Any]`

▼

Return the active pipeline's execution timeline as an MCP resource.

`def _pipeline_timeline_resource() -> dict[str, Any]`

##### Returns

`dict[str, Any]`

`_list_tools`

1

`list[dict[str, Any]]`

▼

Generate MCP tools/list response from all commands including groups.

Group com…

`def _list_tools(cli: CLI) -> list[dict[str, Any]]`

Generate MCP tools/list response from all commands including groups.

Group commands use dot-notation names:`site.build`, `site.config.show`.
Includes outputSchema when return type annotations are available.
Skips commands that fail to import with a warning.

##### Parameters

Name
Type
Description

`cli`
`CLI`

##### Returns

`list[dict[str, Any]]`

`_tool_title`

1

`str`

▼

Derive a human-readable title for a tool.

Uses the handler's docstring first l…

`def _tool_title(cmd: CommandDef | LazyCommandDef) -> str`

Derive a human-readable title for a tool.

Uses the handler's docstring first line if available and different
from the description. Falls back to a title-cased command name.

##### Parameters

Name
Type
Description

`cmd`
`CommandDef | LazyCommandDef`

##### Returns

`str`

`_output_schema`

1

`dict[str, Any] | None`

▼

Generate outputSchema from handler return type annotation.

Returns None for la…

`def _output_schema(cmd: CommandDef | LazyCommandDef) -> dict[str, Any] | None`

Generate outputSchema from handler return type annotation.

Returns None for lazy commands that haven't been resolved (avoids imports).

##### Parameters

Name
Type
Description

`cmd`
`CommandDef | LazyCommandDef`

##### Returns

`dict[str, Any] | None`

`_call_tool`

2

`dict[str, Any]`

▼

Handle tools/call by dispatching to the command handler.

Routes through the CL…

`def _call_tool(cli: CLI, params: dict[str, Any]) -> dict[str, Any]`

Handle tools/call by dispatching to the command handler.

Routes through the CLI's middleware stack when present, so middleware
can intercept MCP-originated calls just like CLI-originated ones.

If the handler returns a generator yielding Progress objects, each
Progress is emitted as a`notifications/progress`JSON-RPC
notification before the final result is returned.

##### Parameters

Name
Type
Description

`cli`
`CLI`

`params`
`dict[str, Any]`

##### Returns

`dict[str, Any]`

`_tool_error_response`

3

`dict[str, Any]`

▼

Build the ``tools/call`` error response with structured context.

MiloError sub…

`def _tool_error_response(exc: Exception, tool_name: str, cli: CLI) -> dict[str, Any]`

Build the`tools/call`error response with structured context.

MiloError subclasses contribute their`argument`, `constraint`,
`suggestion`, and `errorCode` fields. Plain `TypeError`
messages about missing or unexpected keyword arguments are parsed so
agents see which argument was wrong, and the`schema`field points
them at the tool's declared parameter schema for repair.

##### Parameters

Name
Type
Description

`exc`
`Exception`

`tool_name`
`str`

`cli`
`CLI`

##### Returns

`dict[str, Any]`

`_tool_schema`

2

`dict[str, Any] | None`

▼

Return the input schema for a named tool, if known.

`def _tool_schema(cli: CLI, tool_name: str) -> dict[str, Any] | None`

##### Parameters

Name
Type
Description

`cli`
`CLI`

`tool_name`
`str`

##### Returns

`dict[str, Any] | None`

`_list_resources`

1

`list[dict[str, Any]]`

▼

Generate MCP resources/list response from registered resources.

`def _list_resources(cli: CLI) -> list[dict[str, Any]]`

##### Parameters

Name
Type
Description

`cli`
`CLI`

##### Returns

`list[dict[str, Any]]`

`_read_resource`

2

`dict[str, Any]`

▼

Handle resources/read by calling the resource handler.

`def _read_resource(cli: CLI, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`cli`
`CLI`

`params`
`dict[str, Any]`

##### Returns

`dict[str, Any]`

`_list_prompts`

1

`list[dict[str, Any]]`

▼

Generate MCP prompts/list response from registered prompts.

`def _list_prompts(cli: CLI) -> list[dict[str, Any]]`

##### Parameters

Name
Type
Description

`cli`
`CLI`

##### Returns

`list[dict[str, Any]]`

`_get_prompt`

2

`dict[str, Any]`

▼

Handle prompts/get by calling the prompt handler.

`def _get_prompt(cli: CLI, params: dict[str, Any]) -> dict[str, Any]`

##### Parameters

Name
Type
Description

`cli`
`CLI`

`params`
`dict[str, Any]`

##### Returns

`dict[str, Any]`
