Skip to content

Remote Tools over MCP

An agent's tool surface extends past its own process. build_mcp_servers() on the spec returns a list of Model Context Protocol server configs, and the tools those servers publish join the agent's registry next to the local ones:

from mash.mcp.types import MCPServerConfig

class PilotSpec(AgentSpec):
    def build_mcp_servers(self):
        return [
            MCPServerConfig(
                name="github",
                url="http://github-mcp:3000/sse",
                description="GitHub issue and PR tools",
                headers={"Authorization": f"Bearer {token}"},
                allowed_tools=["search_issues", "get_pull_request"],
            ),
        ]

The config is the whole integration surface: a name that namespaces everything the server contributes, the url, optional headers for auth, and an optional allowed_tools filter for servers that publish more than you want to expose. Everything past this dataclass is handled by the runtime.

Discovery at startup

When the agent's process starts, the runtime opens and an MCPManager connects to each configured server and lists its tools once. The lists stay in memory for the life of the process. Each time the runtime builds an agent for a step, it wraps every listed tool in an adapter that satisfies the same four-attribute Tool protocol from the previous post, without another network call:

# src/mash/runtime/factory.py (trimmed)
for server_name, tools in manager.get_all_tools().items():
    for mcp_tool in tools:
        original_name = mcp_tool["metadata"]["original_name"]
        adapter = MCPToolAdapter.from_mcp_tool(
            mcp_tool=mcp_tool,
            executor=_mcp_executor(manager, server_name, original_name),
        )
        agent.tools.register(adapter)

A server that adds tools later shows them after the agent's process restarts, with the host or a deploy.

Remote tools get namespaced names, like mcp_github_search_issues for a search_issues tool on the github server, with the original name and server kept in metadata for routing the call back. The prefix lets the model tell remote tools apart from local ones (and from the same tool name on two different servers), and it keeps the registry's flat namespace collision-free.

flowchart LR
    subgraph proc ["agent process (mash agent serve)"]
        A["agent"]
        R["ToolRegistry\nbash · UpdateDocs ·\nmcp_github_search_issues ·\nmcp_github_get_pull_request"]
        M["MCPManager"]
        A --> R
        R -- "mcp_* calls" --> M
    end
    M -- "HTTP / SSE" --> S1["MCP server: github"]
    M -- "HTTP / SSE" --> S2["MCP server: …"]

The manager runs in the agent's process, a subprocess of the host, so the URL has to be reachable from where the host runs: a service name on the mash Docker network, as above, for a host in a container, or localhost for a host on your machine. Header tokens stay in that process's environment. Agents' code runs in the user's sandbox, which never sees them.

To the model, a remote tool is indistinguishable from a local one; the same {name, description, input_schema} triple rides on every LLM request. The only difference is that execute() crosses the network. The adapter routes the call through the manager to the owning server over an async HTTP client, so other requests in the agent's process keep running while it waits, and parallel calls overlap. The response text comes back as an ordinary ToolResult.

Web search runs on the same machinery. A WebSearchProvider resolves to a single MCPServerConfig, and the runtime connects it through the same MCPManager. To enable it you must explicitly specify a provider by returning one from build_web_search() rather than writing a server config by hand. There's no default, so you always know who is handling your search data. Mash ships one WebSearchProvider, ParallelSearchProvider, which offers web_search and web_fetch and requires an API key.

Naming is the one place it diverges. These tools register under their plain names, web_search and web_fetch, not the mcp_* form. They're a named capability rather than an arbitrary remote server, so the wiring keeps the original names while reusing the connection, auth headers, allowed_tools filter, and call routing that every MCP server gets.

Failure stays inside the tool result

Remote calls add failure modes like an unreachable server or expired auth, and the integration keeps all of them inside the existing tool contract. A tool call that fails returns an error ToolResult, the same shape a local tool produces, and the model reasons about it on the next think phase. A server that fails during startup discovery is logged as a warning and skipped, and the agent starts with the tools that did resolve. It stays skipped until the agent restarts.

Because an MCP call is just a tool call, the durability machinery from the durable loop post applies unchanged: each call records its result as its own event, completed calls are skipped on resume, and transient failures get the standard retry treatment.

Beyond tools

Besides tools, the client handles one other MCP surface, elicitation: a server can ask for structured input mid-call, and the client surfaces those requests for the host to answer. It doesn't implement resources or prompts.

The package boundary in src/mash/mcp keeps protocol details in one place: types.py for config, client.py for the wire protocol, and manager.py for server lifecycle and tool routing. Runtime and tool code consume MCP through this package, so protocol changes land in one module.

The same observability

MCP activity shows up alongside everything else in the event stream: the manager writes mcp.client.connect / connected / error for its connection lifecycle, each carrying server_name and the current trace_id, and a remote tool call is the request's execute_tool span like any local one. In trace analysis, remote calls show up as execute_tool spans under their mcp_-prefixed names, so a slow remote server surfaces in the per-tool stats table like any other expensive tool.

Local and remote tool calls land in the same event log as everything else a request does. The next post reads that log as a trace.

Next: Reading a Trace.