0175: The native MCP controller is the one owner of the managed MCP server; CLI and web ask it¶
- Implementation partial
Context and Problem Statement¶
OwnedMcpRuntime (desktop/managed, ADR 0087)
starts the fixed Core CLI's authenticated HTTP MCP server in a fresh plugin-free private profile per generation,
mints its read token through the CLI (0142),
proves an authenticated initialize and tool exchange, and stops only its retained child. Only native code can reach
it. Head parity (0019) asks that the CLI and the signed-in web app can start, check and
stop that same server. An earlier unmerged design ran a second owner in Python under the user's launchd. How do the
CLI and the web app control the server without a second owner?
Decision Drivers¶
- One owner of the server's lifecycle: two owners disagree about which child, port and token are current.
- The CLI path, profile, endpoint, port and token are chosen natively; no caller may supply them.
- No numeric PID or process group is ever signalled; a receipt never claims a whole process tree was cleaned up.
- No login item, launchd job or other persistent service is added.
Considered Options¶
- The native controller serves start, check and stop on a private local socket; CLI and web are its clients (chosen)
- A second lifecycle in Python under the user's launchd (the reviewed branch for this bead): it duplicates the 0087 owner, adds certificates, a probe credential kept on disk and launchd output parsing, and the two owners can both believe they hold the server.
- Native UI only: breaks head parity; the CLI could not stop a server it can see.
- Extend the supervised web service's control socket: ties the server to the optional persistent web service.
Decision Outcome¶
Chosen option: the native controller serves a private control socket, because the server keeps exactly one owner and the CLI and web app become thin clients that can only ask it.
- Socket:
mcp-owner/control.sockin hz's configuration folder, found by the app exactly as Core finds it (HZ_CONFIG_HOME, thenXDG_CONFIG_HOME/hz, then the platform'shzfolder;~is the home folder). A relative value or~useris refused by both. An app started from the Dock does not see a shell's variables, so a CLI that sets them looks elsewhere; itsunavailableanswer names the folder it looked in. The folder is the caller's own, not a link, mode 0700, checked before and after use. A host first takes an exclusive kernel lock onmcp-owner/owner.lock(lockfon macOS,flockon Linux, held by a helper that ends with the host's pipe), so only one host owns the folder; a second is refused. Under that lock it binds a fresh name in the folder, makes it 0600 and renames it over any stale socket; the process umask is never changed. A path there that is not this user's socket is never replaced. A host that loses the lock stops answering and takes it back when it can. - Requests: one line of JSON per connection,
{"version": 1, "op": "start" | "check" | "stop"}, nothing else, at most 256 bytes. No executable, profile, endpoint, port, token or environment is accepted. Requests run one at a time; more than a few waiting are refused as busy.quitstays native. - Answers: the controller's typed receipt only: generation, loopback endpoint (no token), readiness, ownership,
tool count, termination and
descendants: "unverified"; a refusal says onlyfailed,busyornot-running. The read token never leaves the native parent. - Clients:
hz system serve mcp-owned-start,mcp-owned-check,mcp-owned-stopand Settings, MCP, Owned server (routes under/api/_web/mcp/owned, not operations, so no MCP head offers them). Start and Stop ask for confirmation. Clients check the folder and socket owner and mode before connecting and validate every answer against the receipt schema; anything else isunverified. No host answering isunavailable. - Preview: the
managed_mcpflag, off by default, gates Start from CLI and web; Check and Stop stay available so a running server can always be stopped. - Lifetime: the server runs with
--exit-with-stdinon a pipe only the app holds, so it ends when the app quits, crashes or is killed. A quit whose stop is unconfirmed is asked three times. Each verified stop removes that generation's private profile. While a server runs, the owner keepsmcp-owner/running.json(0600): its pid, start time, command line, port and profile folder, and the owner's own pid and start time, never the token. The next owner, holding the lock, stops a recorded server only while that pid still has the same start time and an ownedhz system serve mcpcommand line on that port, and only when its owner is gone (the server is an orphan, or the recorded owner no longer runs); a live owner that merely lost its lock keeps its server. It signals SIGINT, then SIGKILL; anything else is left alone. This is the one place a remembered pid is signalled, and only after those checks. A leftover's profile is removed only when it is exactlyrealpath(parent profile)/development/mcp-<24 hex>, a real folder of this user; any other recorded path is kept and logged. One asynchronouspscall reads pid, parent, start time and command together; only a pid ps does not find and the kernel confirms missing counts as gone. Apsthat fails, times out or answers unexpectedly keeps the record (set aside asleftover.json) and the profile, and the owner tries again a few times while the app runs.
Consequences¶
- Good: one owner, one current generation; CLI, web and native UI see the same receipt.
- Good: no persistent service, certificate or stored credential is added.
- The desktop app is the native host: it starts the owner when it is ready and quits the server with it. The
server lives only while the app runs; with no app the CLI and web app say
unavailable. - Bad: a server that ignores both its closed pipe and SIGKILL outlives the app until the next owner finds it.
- Bad, and accepted: any process of the same user can ask the socket to start or stop the server. It could do as
much by running
hz system serve mcpitself, and it learns no token. Other users cannot reach the 0700 folder.
Confirmation¶
make managed-backend-check runs the control socket against a fake runtime and the real Python client: strict request
parsing, refused overrides, a second host and an outside lock holder, stale-socket takeover, a lost lock, no umask
change, a foreign or loose folder, busy, and that no answer carries a token. It also runs the real hz CLI
through the desktop owner to a real Core server, kills a fictional app and checks its server ends, and compares the
app's configuration folder with Core's under one environment matrix.
Python unit and integration tests check the client's folder and socket checks, receipt validation, the flag and
the CLI and web answers. Packaged acceptance and real clients remain open.
More Information¶
Builds bead 4pih.3 of the harness epic (0171). The Python launchd design stays unmerged.