# Architecture ## Decision Browser control is an MCP server, not a skill. MCP supplies callable tools, image content, persistent state, and native transport. Optional skills can add workflows without owning browser execution. ## Process topology ```text MCP client (one or more agents) another MCP client | MCP JSON-RPC over stdio | MCP JSON-RPC over stdio v v chrome_control_mcp (server A) chrome_control_mcp (server B) | BrowserControl, refs bound per tab | | Windows: protected named pipe | | Linux/macOS: protected Unix socket | v v chrome_control_mcp (relay for A) chrome_control_mcp (relay for B) | Chrome native messaging, length-prefixed JSON, one port per server v Chrome Control MCP extension service worker: one session per port | chrome.debugger / Chrome DevTools Protocol, one attachment per held tab v the tabs each session holds ``` One executable serves both roles. Normal launch serves MCP. Chrome supplies the pinned extension origin when it launches the native host, which selects relay mode. Any number of servers can drive one browser at once; each has exactly one relay, and Chrome starts one relay process per port the extension opens. ## Platform backends | Concern | Windows | Linux / macOS | | ------------------- | ----------------------------------------------------------- | ------------------------------------------------ | | Bridge transport | Named pipe | Unix-domain socket | | Endpoint protection | Current-user DACL + Medium-integrity no-write-up label | Private runtime directory + mode `0600` socket | | Peer identity | Process token, session, image path, Chrome ancestor | Peer UID/PID, image path, Chrome ancestor | | Rendezvous | `%LOCALAPPDATA%/ChromeControlMCP/browser_bridge-.json` | Private runtime directory, same per-pid name | | Native-host install | HKCU Chrome NativeMessagingHosts key + generated manifest | Chrome `NativeMessagingHosts` manifest directory | Linux uses `SO_PEERCRED`. macOS uses `getpeereid` plus `LOCAL_PEERPID`. Both sides require the same effective user, the recorded process, the same executable image, the nonce token, and the bridge protocol version (2 since 1.8.0). The server also requires a Chrome process among the relay's ancestors; that check is on in production and relaxed only by the unit tests. ## Servers, relays, and ports Each server publishes its own rendezvous record, named for the process that published it (`browser_bridge-.json`), so several can advertise at once. `stop()` removes a record only while it still names the exiting process, and a server sweeps records left by processes that are gone when it starts. `ensurePublished` re-publishes this server's record before a browser tool call while no relay is connected, so a record lost to anything short of the server exiting heals without a restart. The Chrome-launched relay cannot be told which server it is for on argv, so it asks. It lists the records of servers that are still running, sends them to the extension as `bridge_offers`, and connects to the one the extension names in `attach_session`. Only running servers are offered because a crashed server's record stays on disk until another server starts and sweeps it, and the extension cannot tell a dead pid from a live one. The relay speaks first because the extension has no filesystem access and so has no server to name until it is offered one. A server the extension asks for that is no longer published yields no connection rather than a substitute. The frames on the relay's stdio, in the order they can appear: | Frame | Direction | When | | ---------------------------------------------------------------- | ----------------- | ---------------------------------------------------------- | | `bridge_offers {protocol, servers}` | relay → extension | first, before anything else | | `attach_session {session, version, profile_name, profile_email}` | extension → relay | only in answer to `bridge_offers` | | `bridge_ready {protocol, version}` | relay → extension | the chosen server welcomed the relay; pumping starts | | `bridge_unavailable {error}` | relay → extension | instead of `bridge_ready`, after which the relay exits | | `bridge_servers {servers}` | relay → extension | while pumping, whenever the set of running servers changes | | `command`, `cancel` | relay → extension | forwarded from the server | | `progress`, then one `result` or `error` | extension → relay | only in answer to a `command` | Protocol 2 command frames may name the tab they act on (`tab_id`), and replies say which tab they acted on (`tabId`, with that tab's own `domEpoch`) and which tabs the session stopped holding (`goneTabIds`). A protocol-1 extension would ignore `tab_id` and act on its single current tab -- the wrong page, silently -- which is why this is a version and not an optional field. A mismatch refuses every command and says which side to update. When the server's protocol is the newer one, the extension reloads itself once for that protocol, since Chrome does not reload an unpacked extension when its files change. Versions travel with the handshake too (1.9.0 and later). `attach_session` carries the running extension's manifest version; the relay passes it to the server in its hello as `extension_version`, with its own as `relay_version` -- the relay is started from the installed copy, so that is the installed version -- and the server's welcome and the relay's `bridge_ready` each carry their sender's `version`. Every field is optional on every hop and must be `major.minor.patch`, so an older peer ignores them and a missing one means "older than 1.9.0". From these the server tells the agent, once per connection and in a reply block of its own, when the extension, the installed copy and the server disagree and what fixes it, and `browser_extension_status` reports them under `running`. The extension reloads itself once per newer installed version, but only when no session holds a tab, no command is running and none has run for 10 s, so an update that keeps the protocol no longer leaves Chrome running the old extension until someone reloads it by hand. The design is in [plans/extension-version.md](plans/extension-version.md). **Several relays per server, one per Chrome profile (1.10.0).** Chrome gives each profile its own instance of the extension, each with its own relay. The server's acceptor hands every relay that connects to a thread of its own, which handshakes, verifies and serves it with the same silence budget, progress beats, idle checks and cancel the single connection had; up to eight are admitted, and the ninth is refused with a reason. On Windows each connection gets its own pipe instance (the first created with `FILE_FLAG_FIRST_PIPE_INSTANCE`, the rest joining it); on POSIX each is an accepted socket. `attach_session` carries the profile's name and email, which the relay passes on as `profile_name` and `profile_email` and the server cleans as untrusted text. Tab and window ids are unique only within one Chrome process, so a relay from a second, still-running Chrome -- found the same way the server already finds the Chrome above each relay -- is refused while another is served. A port the extension has no server for -- every offered server already has one of its ports -- answers the offer with no session, and its relay connects nowhere; a relay older than 1.10.0 connected it to the newest server anyway, so such a relay is served only alone from its Chrome. `BrowserControl` keeps, per connection, its name and which tabs and windows it holds, learned from every reply; it routes each call by its tab, its ref's tab, the `profile` it names, or the current profile, fans listings and release out to every connection and merges them, and asks each extension `profileState` (name, focus) and `findTab` when it has to. A connection that goes away takes the refs and routing of its own tabs only. The design is in [plans/chrome-profiles.md](plans/chrome-profiles.md). `bridge_servers` is how a server started later is found. A relay offers its list once, so before it existed a second editor opening was noticed only when the extension's 30 s alarm next opened a port. While it pumps, the relay lists the running servers again every second and sends the whole set, newest first, whenever it differs from the set it last reported -- which starts as the offer. Order alone is not a change: a server re-publishing its record moves it to the front of the listing without anything having started or stopped. The frame asks for no answer and must not get one, because nothing ever travels from the extension to the relay unprompted -- an older relay would read such a frame as the reply to a command it had not sent. An extension that predates the frame logs an unexpected type and ignores it. Every frame the relay writes -- the pump's, a forwarded cancel, this notice, and the exit path's last flush -- goes out under one lock, so two threads can never interleave a frame. The extension holds one native port per server and one session per port -- Chrome starts a separate host process per port, and the relay behind it attaches to exactly one server. Each port's listeners close over their session, so an arriving frame never has to be matched back to one; only the browser's own events need routing, because they name a tab and nothing else. Every port is a process, so how often the extension opens one is rationed: - With nothing attached, a failed attempt waits before the next, doubling from 2 s to a 2-minute ceiling, and the wait resets the moment a bridge answers. Tab events and a 30 s alarm also re-arm the connection, and obey the same wait. - With a relay attached, new servers arrive through `bridge_servers`, which starts no process. The alarm opens a probe port only as a five-minute safety net, or when a server that refused the extension is due to be tried again; a refusing server is set aside on a doubling interval from one minute to thirty. - The toolbar button pauses the bridge: open ports close, the browser is released, and nothing reaches for a server until it is resumed. The paused state survives a browser restart. A server serves one relay at a time on an endpoint with a single instance, so a relay that has gone must be let go promptly or the next one is turned away -- `ERROR_PIPE_BUSY` on Windows, parked unanswered in the listen backlog elsewhere. The relay speaks on the pipe only to answer a command, so while it is idle the server looks at the connection every 250 ms without reading from it: a hangup ends the connection, and so do bytes arriving with no command outstanding, which a relay keeping to the protocol never sends and which would otherwise be read as the reply to the next command. Before this check a server whose relay died between commands -- Chrome restarting, the extension reloading -- held the endpoint until a command happened along to fail; one was observed unreachable for a whole day. The extension reaches two native hosts: this project's, and S.A.K. Utility's (`com.sak.browsercontrol`), the other application the extension serves. Each is a hub in `background.js` with its own ports, sessions, server ids (S.A.K.'s are kept as `sak:` so equal pids never collide), reconnect interval and tab-group names. A session of one hub sees nothing of the other's: its tabs are left out of every listing and refused by every command, and every frame goes back on the port it came in on. S.A.K.'s hub requires an administrator's policy (`sakHostEnabled` in `chrome.storage.managed`, `managed_schema.json`): without it the hub never opens a port, and the policy's removal while Chrome runs ends its sessions. It is optional as well; while it is not installed it is asked for after ten minutes, doubling up to six hours, and never logged. A listing's indices count only the tabs the asking session may see (`visibleTabs`). ## Sessions, tabs, and agents A session is one server's port. It holds tabs, not one tab: those it opened (`browser_new_tab`, `browser_window new`, a popup its own command's page opened) and those it was given (the user's active tab on its first call, one it took with `browser_select_tab`, one the user dragged into its tab group). Every held tab has its own debugger attachment and its own page state -- snapshot generation, screenshot transform, dialog arm, console and network logs, emulation, HTTP-auth credentials, caught file picker -- so working in one tab never disturbs another. Holding is exclusive. A second session that reaches for a held tab is refused, naming the holder. Chrome already allows one debugger client per target, which covers the DevTools protocol; the lease covers `chrome.tabs`, `chrome.windows`, cookies and downloads, where Chrome would let two sessions collide without complaint. Several agents can share one server -- sub-agents, parallel tasks of one client -- and the server cannot tell them apart, because they arrive over one MCP connection. So the tab is addressed per call: every tab-scoped tool takes `tab_id`, and every element ref is bound to the tab its snapshot came from and is never reused. An agent that names itself (`agent`) gets its own tab group and colour. One server still carries one command at a time, so agents behind it are isolated from each other, not concurrent; separate servers run in parallel. Tabs are let go of when the agent calls `browser_release`, when its server exits, and -- partly -- after ten minutes without a command. [MULTI_SESSION.md](MULTI_SESSION.md) describes which tabs count as the assistant's, how popups are claimed, what each kind of release does, and the design record behind all of it. ## Extension preparation `browser_extension_install` verifies the staged extension files and executable, then installs only the current-user Chrome native-host registration: - Windows: generated manifest plus HKCU registration - Linux: `${XDG_CONFIG_HOME:-~/.config}/google-chrome/NativeMessagingHosts` - macOS: `~/Library/Application Support/Google/Chrome/NativeMessagingHosts` `chrome_control_mcp --install` does the same from a shell, after first putting the copy into the managed install layout (see [TOOLS.md](TOOLS.md#updating-this-server)); the VS Code extension runs it on every editor start. A build older than the version already current leaves that version in place and says so, so starting an editor cannot roll an update back. For isolated tests, `CHROME_CONTROL_MCP_NATIVE_HOST_DIR` overrides the Unix manifest directory, and `CHROME_CONTROL_MCP_NATIVE_HOST_KEY` and `CHROME_CONTROL_MCP_DATA_DIR` give Windows the same separation. Chrome itself reads only the fixed registration. The committed extension manifest contains public identity material so unpacked loads keep a stable ID. There is no private key, package signing, browser-store dependency, or enterprise-policy write. ## Request path 1. Assistant sends `tools/call` over MCP stdio. 2. MCP dispatch validates the JSON-RPC envelope, security profile, arguments, and schema. 3. `BrowserBridgeSession` translates the tool call. An element ref resolves only against the latest snapshot of the tab it came from, and the command is sent to that tab; a `tab_id` that names a different tab is refused. 4. The platform bridge sends one correlated command to the authenticated relay. 5. The relay forwards it through native messaging (Chrome, Edge, Brave, or Chromium). 6. The extension acts through CDP -- routing main-frame commands on the root session and OOPIF subframe commands on flattened child sessions attached via `Target.setAutoAttach` -- and returns exactly one result or error, beating `progress` every 5 s while it works (forwarded as MCP `notifications/progress` when the client supplied `_meta.progressToken`, and abortable via `notifications/cancelled`). 7. The bridge correlates the reply, updates that tab and frame's snapshot and ref state, drops refs bound to tabs the session let go of, records an optional JSONL audit entry, and returns MCP text or image content. ## Browser model - `browser_snapshot` joins the accessibility tree and DOM geometry into filtered roles, names, values, and `[ref=eN]` handles in a single concurrent CDP wave (extracting `