Skip to main content

Module mcp

Module mcp 

Source
Expand description

MCP: model tools from a server that speaks JSON-RPC, over a pipe or over HTTP.

Three messages do all the work — initialize, tools/list, tools/call — and everything else in the protocol (resources, prompts, sampling, roots) is deliberately not implemented: this build adds tools, and a half-implemented capability is worse than an absent one.

Two transports, one shape. transport = "stdio" (the default) starts a process per [[mcp]] entry and speaks newline-delimited JSON-RPC to its stdin/stdout. transport = "http" posts the same messages to url — Streamable HTTP, a JSON body or an event stream back, Mcp-Session-Id carried once the server hands one out (#502). The reason is not symmetry: a server that speaks only HTTP (Home Assistant’s mcp_server) cannot be reached at all otherwise, and the alternative — spawning uvx mcp-proxy to fake a pipe over HTTP — is a second runtime dependency for a transport this build can speak itself.

Three decisions worth knowing:

  • One connection, serialised. A stdio server is a single pipe: two questions on it at once have no way to say which answer is whose beyond the id, and most servers are single-threaded anyway. Calls take a mutex, so [tools] concurrency buys nothing for MCP and nothing breaks.
  • The server outlives the call, not the process. It is spawned on first use and killed when the agent process ends (or when a call times out, which poisons the pipe). The next call after a crash starts it again, so a server that segfaults costs one failed call, not a dead session.
  • Blocking I/O, called from spawn_blocking. The registry is async; the pipe is not. Reading a line has no timeout in std, so the timeout is a recv_timeout on a channel fed by a reader thread — and on timeout the child is killed, which is the only way to unblock a stuck read without leaking a thread per hung server.

Structs§

McpTool
A tool the agent can call, implemented by a remote MCP server.
RemoteTool
A tool as the server described it, before this build puts it in the registry.
Server
One MCP server, shared by the tools it offers.
Spec
One [[mcp]] entry — in a plugin manifest, or in config.toml (#352).

Enums§

TokenSource
Where a token_env variable resolves, as a word (#517). The value itself is never carried past Spec::token.
Transport
How this build reaches a server (#502).

Constants§

DESCRIPTION_MAX
What a remote description is cut to. A description is worth a sentence or two of prompt; a server that sends its whole README should not get to charge that to every request.
PROTOCOL
The MCP revision this client was written against. A server that answers with another one is used as it is: tools/list and tools/call have not changed shape, and refusing an unknown revision would break on every minor revision for no gain.
SCHEMA_MAX
What a remote input schema may serialize to. See McpTool::new for what happens past it.
TOOL_NAME_MAX
The most a tool name may be. OpenAI-compatible endpoints cap it at 64 characters and reject the whole request past it — the same ceiling the config’s tool ids live under.

Functions§

sanitize_tool_name
A remote tool name, made safe to put in a chat request.

Type Aliases§

Secrets
The .env values the config loaded, as a token_env variable resolves them (#516).