ADR-023: MCP servers run under golem's own small client, and their tools ask first
Date: 2026-10-23 Status: accepted
The idea
An MCP server is a program that offers tools. Golem starts each server from [mcp.<name>] in config.toml, or reaches it over HTTP, and turns each of its tools into a golem tool. From there a server's tool is like any other: the tools node runs it (ADR-011), the rules decide (ADR-012), and an approval can pause the run (ADR-013).
flowchart LR config["[mcp.git] in config.toml"] --> supervisor[McpSupervisor] supervisor -->|"start, initialize, tools/list, ping"| server["git server<br/>(child process or URL)"] supervisor -->|"the session's mcp__*"| registry["ToolRegistry.scoped(session)"] registry --> node["tools node:<br/>rules, approval"] node -->|tools/call| server
The client is golem's own. MCP is JSON-RPC 2.0, and golem needs five messages of it: initialize, notifications/initialized, tools/list, tools/call, and ping. A transport carries them: a child process's stdin and stdout, one message per line, or Streamable HTTP.
Which tools a session sees depends on the session. A server is on for every session (what the catalog adds), on demand for the sessions that turn it on, or off. A trusted project can add servers for the sessions in its folder.
What the code does today
- The client (
golem/mcp/client.py) sendsinitializewith protocol2025-06-18and no client capabilities, and lists tools across pages. A request the server sends gets "method not found", exceptping. A call that takes longer thantimeout_secondsgetsnotifications/cancelledand fails. - Transports (
golem/mcp/transport.py):- stdio starts the server in its own process group. Its stderr goes to the daemon log, and its last line explains an exit. Stopping closes stdin, then sends SIGTERM, then SIGKILL, to the whole group, so a server started through
npxtakes itsnodewith it. - HTTP posts each message to the server's URL. An answer comes back as JSON or as an event stream that may carry the server's own requests first. The
Mcp-Session-Idthe server gives, and the protocol version, go with every later message. An HTTP error fails that call. A 404 on a session, or a server that cannot be reached, closes the transport, and the supervisor starts over. Golem does not open the optional GET stream, so a server's own notifications reach it only inside an answer.
- stdio starts the server in its own process group. Its stderr goes to the daemon log, and its last line explains an exit. Stopping closes stdin, then sends SIGTERM, then SIGKILL, to the whole group, so a server started through
- The tools (
golem/mcp/tools.py) are namedmcp__<server>__<tool>. Providers allow only letters, digits,_, and-in a tool name, at most 64 of them, so the server's name for the tool is cleaned to fit, and the call goes out under the original. A server name has no_, so the name splits back without guessing. A tool's JSON Schema goes to the model as it is, minus$schema. The server checks the arguments. - Trust. A server's tool is never
read_only, whatever it says about itself, so the built-in rules ask before every call. (A server you set toapproval = "read-only"is the one exception, see ADR-048.) The subject a rule sees is the arguments as one line of JSON, so "always allow" covers that exact call, and a rule ontool = "mcp__git__*"covers a whole server. The output reaches the model inside<mcp_output server=… tool=…>, after a note that it is data, the same way a web page is marked (ADR-017). Images and other binary items are named, not inlined. - Scoping.
ToolRegistryholds golem's own tools. Each model call and each tool call asks it forscoped(ToolScope(root, enabled)), the session's folder and the on-demand servers it turned on, and the supervisor answers with those servers' tools.enabled = trueservers start with the daemon and reach every session."on_demand"ones start when a session first runs/mcp enable <name>(mcp.enable), and reach only sessions that did. The list is stored assession.updatedwithmcp_servers, projected intosessions.mcp_servers(schema 7), and survives a restart.falseones never start.- A project's
.golem/config.tomladds[mcp.*]servers for sessions in that folder, only when it is intrusted_projects: a project names programs to run. Its server takes the place of the user's server of the same name there, and runs in the project folder. - The tools node refuses a call to a tool the session does not have, before the rules, with a message that names
/mcp enable. An old reply that asks for a server since turned off does not run it.
- The supervisor (
golem/mcp/supervisor.py) starts servers in the background, so the daemon answers in under a second whilenpxdownloads one. A model call waits up to 10 seconds for a server its session needs that is still starting.- Health: each running server answers
pingevery 30 seconds. One that exits or stops answering isfailed, and starts again after 1 second, then 2, 4, up to a minute between tries, back to 1 after a minute of running. - Reload: it reads
config.tomland each project's file again when they change. A changed server restarts, a new one starts, and a removed one stops. A file that does not validate keeps the last good servers. - A server that says
notifications/tools/list_changedis listed again.
- Health: each running server answers
- Environment and secrets (
golem/mcp/launch.py). launchd gives the daemon a shortPATH, so a command is also looked for in/opt/homebrew/bin,/usr/local/bin, and~/.local/bin. A stdio server getsHOME,PATH,USER,LOGNAME,SHELL,TERM,LANG, andTMPDIR, plus its ownenv, and nothing else from the daemon's environment.- A value in
envorheaderscan namekeychain:mcp/<name>, alone or inside the text (Bearer keychain:mcp/github), and the secret replaces it when the server starts.golem auth set mcp/<name>or/login mcp/<name>stores it, restarts the servers that use it, andgolem auth listshows whether each one is stored. - A variable or header whose name looks like a secret must name one, and a reference outside
mcp/does not load, so a project's server can never be handed a model provider's key.
- A value in
- On the wire (protocol minors 19 to 21):
mcp.listreturns each server with its state (idle,starting,running,failed,disabled), command line or URL, tool names, rough schema tokens, error, and seconds until the next try. Withsession_idit adds the session's project servers and whether each is on for it.mcp.enableandmcp.disableturn an on-demand server on or off for a session.mcp.toolslists a server's tools with their descriptions,mcp.restartstarts it again, andmcp.set_enabledwritesenabledinto the file that names it (golem/config_edit.py, which keeps comments) and applies it at once.mcp.server.statusgoes to every connection when a server changes, likemodel.pull.progress, and is not stored. - In the TUI,
/mcpopens a manager (mcp-manager.ts): the servers grouped by the file that names them, then one server's status, command, file, tools, and token cost, with View tools, Turn on or off for this chat, Reconnect, and Disable everywhere (or Enable, for a disabled one)./mcp enable|disable <name>stays as a shortcut, and the status line showsmcp ✗ <name>in red while a server the session uses is down.golem doctorchecks each server that may start: its command is found, and its secrets are stored.
Other shapes that lost
The mcp Python SDK. It is the reference client. It also brings a server stack the daemon never uses (Starlette, Uvicorn, SSE), which PyInstaller would pack into every release, and pywin32 on Windows. A company proxy that blocks one of those packages blocks the whole install. Golem's client covers the five messages it uses, over both transports, and is tested against a fake stdio server and a fake HTTP one.
langchain-mcp-adapters. It returns LangChain BaseTools that call the server themselves. The tools node would have to wrap them from outside for the rules, the approval, the output marking, and the artifact store, and the tools would be LangChain objects, against ADR-002.
mcp.<server>.<tool> names. They read better. OpenAI and Anthropic refuse a dot in a tool name.
Trusting readOnlyHint. A server can mark a tool read-only, and golem could allow those without asking. The hint is the server's own word, and a wrong one runs a tool nobody approved. A rule the user writes says the same thing, and the user chose to trust it.
One global tool list, filtered when the model is offered tools. Two projects can both name a server git, and a project's server can replace the user's. A global list keyed by tool name cannot hold both. A scoped registry also lets the rules, "always allow", and edited arguments see exactly the tools of the session they belong to.
Project servers in every folder. A repository would run whatever its .golem/config.toml names as soon as golem opens a session there. trusted_projects already decides whether a project may allow tools (ADR-012), and it decides this too.
Starting servers before the socket listens. The daemon would wait for the slowest server before any client could connect. A slow npx download would look like a daemon that does not start.
What it costs
A model call can wait up to 10 seconds for a server its session needs. A server that never comes up costs that wait on every call until it does, or until the session turns it off.
schema_tokens is the length of the tool schemas divided by four, not a real token count. It is enough to see which server is expensive.
A project server keeps running after the last session in that folder ends, until the daemon restarts or the file changes.
Golem's client follows one protocol version. A server that answers with another is used anyway, and a later MCP change that breaks the five messages above needs a change here.
Addendum: the model can change a session's scope, with approval
A session's enabled list used to change only when the user ran /mcp enable. The model can now ask: the built-in mcp_enable(name, reason) is offered while the session has an on-demand server that is off, and its description lists those servers. It goes through the same permission policy as any tool, so the user approves each turn-on and sees the reason. On allow, it takes the same path as mcp.enable, so the list is stored in session.updated and survives a restart, and the tools are in scope for the next model call of the same turn.
Only a server the model was told about can be turned on. auto_enable = false in [mcp.<name>] removes a server from that list and from mcp_enable, which leaves it to the user. Approving mcp_enable for a server also approves that server's tools for the rest of the session (the allowance mcp__<name>__*, with the other session approvals), because asking twice for one intent, "open this page", is noise. Only a call the user answered creates it; deny rules still win; it is never written to a config file. Scope is still per session: no server turns on for another session, and a server that is enabled = true or false is never the model's to change.