Files
magpie/docs/subsystems/mcp-router.md
T
yetone 483b131ef0 browser: magpie in Docker uses the host's own browser, through a key the page hands out
noahhrreion ran magpie in a container and its Magpie Browser never
connected. The host's Chrome dials ws://127.0.0.1:3425/v1/magpie/browser/
extension, which reaches the container from the bridge (172.17.0.1). That
is not loopback, so the gateway refused it (403). The browser MCP also
refused every caller from elsewhere, and the hub marked the browser
HereOnly. The extension's folder under the container's HOME can't be
loaded by the host's browser either.

Nothing is opened to the network without a key:

- The extension takes a gateway key: its popup has a Gateway key field,
  and config.js carries KEY (extension 0.1.7). From another computer the
  extension connects only with ?key= of a gateway key that is on, and
  only from the extension's own Origin (checked first). Loopback is
  unchanged, and a page's origin, null, a keyless call or a rebound Host
  are all still refused (TestBrowserExtensionOnlyFromItsOrigin).
- A browser that connects with a key belongs to that key. Sessions are
  tagged key:<id>/... (browserOwner, keyedSession), and a remote caller's
  session is forced to its own key's whatever it names. The relay filters
  every lookup (forSession, all, choose) through browserConn.serves. The
  browser MCP admits a remote caller only with a gateway key; a web page
  is refused as before.
- The key the page makes is recorded as this magpie's own browser's: its
  id goes in settings.Dir()/browser-key, written temp-and-rename. This
  goes by where the key came from, not by its name. A browser connecting
  with it serves this computer's sessions too, so agents inside the
  container use it as they would one on loopback. A record that can't be
  read names no key, so no keyed browser is this computer's.
- Router: magpie mcp browser is HubServer.Keyed. A visitor from
  elsewhere gets it only with a key (HubVisitor.Key), and hubCall names
  its session key:<id>/..., so it reaches only that key's browser.
- GUI: a page whose browser can't reach magpie's folders (InContainer, or
  a page from another computer) is "away". The Magpie Browser sheet's
  button then reads Download the extension. It does a POST to
  /api/library/browser/extension with the page's origin, which answers a
  zip pointed at that origin (3430 passes the gateway's routes on) and
  holding the recorded key's secret. The key is made the first time and
  reused while it is on. The steps (unzip, chrome://extensions, Developer
  mode, Load unpacked) are shown in every language. It is a POST so that
  a link from another site can't mint a key.
- Docs: reference.md (Docker), subsystems/library.md and mcp-router.md,
  and site docker.html in en, zh and ja.

Run, at this tree rebased on origin/main 6302f88bf, on mote:
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., and darwin, linux and windows builds:
  clean, all ok
- go test -tags nogui -run 'Browser|Hub|OwnBrowser' ./internal/gateway/,
  and the whole of internal/library, internal/gui and internal/browserext:
  ok (gateway 2.4s, library 166s, gui 104s, browserext 0.3s)
- GUI tests library-browser-away (new: every language, Chromium and
  WebKit, 1100 and 420 wide), library-browser-setup, library-browser-ask,
  i18n-keys and i18n-en-no-cjk: 115 pass. One red, i18n-keys "a plugin's words are its own", is for three
  strings this change doesn't touch and is already filed (#5332, #5426,
  #5482)

Real use, in Docker Desktop on xiaomi-gpu (Windows 11): this tree's
Dockerfile image, run as `web --addr 0.0.0.0:3430` with 3425 and 3430
published to the host's localhost. The host's Node and Playwright Chromium:
- signed in to the page, which says away:true;
- downloaded the zip (GATEWAY "ws://localhost:3530" and a KEY), twice,
  getting the same key both times;
- a keyless /mcp/magpie/browser call from the host got 401;
- loaded the unzipped extension with --load-extension. The container's log
  says "Magpie Browser 0.1.7 connected with gateway key 12d2702a…" and
  /api/library/browser says connected:true;
- an MCP client on the host with that key listed b1 (Chrome 151), opened
  http://localhost:3531/ (served on the host) and read "hello from the
  host";
- a second gateway key made in the container got "isn't connected with
  this gateway key";
- a curl inside the container to 127.0.0.1:3425/mcp/magpie/browser with no
  key listed the host browser's tabs, the host page included.

Broken by hand, each red:
- forSession left unfiltered;
- a remote session not forced to its key;
- no Origin check on a keyed connect;
- hubCall not prefixing the session;
- Keyed given to a visitor with no key;
- the old MCP guard;
- browserKey not reusing the key;
- the away peer check removed;
- serves("") without own (TestOwnBrowserKeyIsThisComputers,
  TestOwnBrowserKeyUnreadable);
- OwnBrowserKey reading the wrong file (the gateway tests and
  TestBrowserExtensionDownload).

Not tried:
- the reporter's own setup (their compose file and Chrome);
- a real agent's MCP client calling the browser from the host. Node's
  fetch sends Sec-Fetch-Mode: cors, which fromBrowser counts as a web page
  and refuses. The e2e used node's http instead. Whether an agent's MCP
  client built on fetch is hit by this is not checked; filed as #5522.
- Edge, Brave and Firefox;
- a remote page over https/wss through a proxy.

What the user does: update magpie and publish 3425 and 3430. In the web
UI open Library -> Magpie Browser -> Download the extension, unzip it and
Load unpacked in chrome://extensions. An extension 0.1.7 already loaded
can take the address and key in its popup instead.

Reported-by: noahhrreion (Discord)
2026-11-06 20:06:15 +08:00

29 KiB

MCP router

With the Library's switch "MCP router" on (#2679, @minggang123 on X), each agent is given one MCP server, magpie, in place of the Library's MCP servers ticked for it. magpie serves it at /mcp/magpie/hub/<agent> (Streamable HTTP) and hands each call on to the Library server behind it. Any other app (Cursor, VS Code, Windsurf, an app on another computer) connects at /mcp/magpie/hub, which serves every server in the Library. A server written once is then set up once, for every agent and app, and a command server runs once, not once in each. The servers themselves are the Library's; the endpoint is the gateway's, guarded as Plugin tools' /mcp/magpie/tools is. The switch is off by default.

Responsibilities and sources of truth

Part Responsibility Source
Switch Library.MCPHub (mcpHub in library.json) is the source of truth. SetMCPHub sets it through change, so every agent is synced at once; turning it off also ends every server the router started (hubStopAll). The page posts servers/hub {on}. SetMCPHub in mcp_hub.go; servers/hub in gui/library.go
Entry syncMCP gives an agent hubServerFor(t), named magpie (HubEntry), in place of each server it would have written, and records it in Applied.MCP like any server, so turning the switch off takes out only that entry and writes each server's own again. The URL is the gateway as that agent reaches it (gatewayOf), so a WSL distro or a LAN computer gets its address, with Authorization: Bearer <key> when that address needs a key (agent.KeyAt). A LAN key or relay change re-syncs (LANChanged). magpie's own image server (isSelf) is behind the router too, except for an agent in a WSL distro (its id has @wsl:), which keeps its own entry to the magpie in that distro. A server the user keeps direct is not (Server.Hub = direct, hub in library.json; #4382, cuipengcx90): each agent given it keeps its own entry, written as with the router off, beside the router's entry for the rest, and started by that agent: Codex on magpie, which is otherwise given a command server as the gateway's /mcp/run/<name> that magpie starts from home (mcp_run.go, #3885), is given a direct one's command, args and env with no cwd, so Codex starts it in the thread's folder (codex-mcp's LocalStdioServerLauncher falls back to the session's cwd; TestRunDirectServerIsCodexsCommand); an agent whose every server is direct is given no hub entry. inHub is the one test, so the router neither lists nor runs a direct server, at /mcp/magpie/hub either, and hubPrune ends one it held. The default (""; shared is saved as "") is the router's as before. Switching it (ServerHub, or a save) goes through change, so each agent is written again at once and the entry it had the other way is taken out (TestHubDirectServer). The policy is the server's, so a sync carries it. A per-project or per-session backend behind the router (Option B in #4382, keying hubPool by roots) is not built. hubFor, hubServerFor, inHub in mcp_hub.go; syncMCP in sync.go; mcp_signin.go
Who keeps their entries An agent keeps each server's own entry, and AgentView.Hub says why: no-remote (it reaches no server over HTTP: mcpFile.supports refuses the entry, as for Claude Desktop), taken (a Library server is itself called magpie), own (the agent has an entry called magpie that magpie didn't write; it is left as it is). on is an agent given the router's entry. An agent given none of the router's servers has no value. hubFor in mcp_hub.go; AgentView.Hub, View.MCPHub, View.HubPath in sync.go
Serve tools/list lists the tools of the servers ticked for that agent (at /mcp/magpie/hub, agent "": every server in the Library, ticked for any agent or none), but those in DisabledTools, as <server>__<tool>: each part in [A-Za-z0-9_-] (other characters _), at most 64 characters (cutName), _2, _3 on a name two would share. Each tool's description and inputSchema go on as the server listed them (an object schema with no properties when it has none). An agent that searches its MCP tools itself is given every tool in full, and its tool search keeps them out of each request: Claude Code (ENABLE_TOOL_SEARCH, which magpie turns on, see agent wiring), Claude Desktop, Codex (tool_search on magpie's catalog), Pi, Droid (which defers every MCP tool to its ToolSearch) and omp (which names each in its catalog by its description's first line) (hubSearchesItself, by the agent's id without its @wsl:<distro>). So is the address for any other app (agent "") and an agent given 10 tools or fewer. Any other agent is given its tools on demand (below). tools/call runs one; an unknown name, or another agent's, is error -32602. initialize and ping are answered; other methods are -32601. Tools only: no resources or prompts. hubMCPCall, hubNamed in hub_mcp.go
What it serves An agent that shows the router as one line, and its tools only once they are searched for, learns from that line alone which servers are behind it (a Pi asked about a GitHub repository cloned it rather than ask the DeepWiki behind the router, told only "The tools of the MCP servers in magpie's Library", 11-01). initialize's instructions start with HubLine: Tools of <name> (<what it is for>), …, every server by name, each one's first clause (hubClause) cut at a word to an equal share of 250 characters (hubLineMax, Pi's MAX_SERVER_DESCRIPTION_CHARS), the short ones whole; names that don't fit are counted (and N more). Then, for an agent given several routers to tell them apart (packinggoodsllc_07668: magpies that only share MCP servers), the router's name sentence when it has one (Router name, below), then a line for each server (hubDetails): - <name> (<its title>): <what it is for>. Tools: <tool> (<first clause of its description>), …, the tools the Library turned off left out and a router's [<name>] dropped; a line too long for its share of the room says its tools by name only, then as many names as fit and and N more. The whole is at most 2000 characters (hubSayMax, half the 4096 Claude Code keeps), the closing text kept whole. What a server is for is the market's (serverAbout): the featured or registry server it is by serverKey, whatever it is named, or the featured one it is named after; otherwise the server's own words, its serverInfo.description or else its instructions' first line (saysOf); another magpie's Tools of a (…), b. is said with Tools of once. Its title is its serverInfo.title. These and its tools are what the server last answered to initialize and tools/list (noteCard), kept in mcp-hub-servers.json beside settings.json (not in the synced library folder) by server name and cardKey (a hash of hubKey: an edit or a sign-in since is another server), written through a temporary file only when they change; a router started again describes its servers before running any. A server with no card for it as it is now (Known false) is listed by initialize, which waits up to 5 s (hubDescribeWait) for it. No model writes any of it. Which agents show the instructions, from their installed source (11-02): Claude Code (a # MCP Server Instructions message, 4096 characters), Codex (the namespace's description), OpenCode (<mcp_instructions>), Gemini CLI (in a trusted folder only), Qwen Code (the opening history), Pi (the first line in its prompt, the whole through codemode); Kimi CLI reads none, and none of them shows serverInfo.title to the model. Pi doesn't wait for a codemode server before writing the first prompt (waitForDirectServers waits for direct ones), so its instructions miss that prompt; Pi's own mcp.json (fmtPiNative) gets the same line as the hub entry's description, which Pi shows before connecting (serverSummary). It is magpie's on the hub entry only (mine), read back to tell a changed list of servers (Server.about, same), and any other entry's description is the user's. HubLine, hubAbout, hubDetails, hubServerLine, hubServersFor in hub_mcp.go; theHub.Servers, hubAbouts, hubServerFor in mcp_hub.go; hubServerOf, noteCard, saysOf in mcp_hub_cards.go; serverAbout in market.go
Tools on demand An agent with no tool search of its own sends every tool, with its inputSchema, on every request (ArvinCharl on X, 11-04: the tools filled the context). Past hubOnDemandOver (10) tools, hubOnDemand gives it two in their place: find_tools {query, server?} scores each tool on the query's words (a word in its router name 3, in its description 1, the whole query in its name 5 more; hubMatches) and answers the best 5 with their name, description and inputSchema, then up to 15 more by name with their first clause; an empty query, or one nothing matches, names every tool. find_tools' description starts with HubLine (each server and what it is for: an agent such as omp shows a tool by its description's first line, and some, such as Droid, show no instructions), then how to search, then each server's tools (hubDetails within hubFindSayMax, 1200 characters; hubFindSay): told only how to search, a model asked for a picture searched and stopped (tao5010 on Discord, 11-04). call_tool {name, arguments} runs the tool by its router name as tools/call finds it (hubToolNamed), else the one tool with that name on its server in any case (hubCallNamed); arguments given as a string holding an object is that object. It runs through the same caller, roots and key limit as tools/call (hubCall), and its request-log row names the tool that ran (mcpUse.named). A name nothing gives is an error naming the close ones. tools/call takes both names whatever was listed, and still runs a tool by its own name. initialize's instructions add Its tools are given on demand: … (counted from HubServer.Tools), and still name each server's tools (hubDetails), so the model knows what there is to find. The Library's switch Tools on demand (on by default) turns it off for every agent: settings.RouterAllTools, this computer's own (KeepOwn), set alone (SetRouterAllTools) and kept by the Settings page's save. hubOnDemand, hubFind, hubCallByName in hub_ondemand.go; hubCall in hub_mcp.go; RouterAllTools, SetRouterAllTools in settings.go; servers/hub-tools in gui/library.go; View.HubAllTools in sync.go
Router name What a router says of itself, for an agent given the same tools by two routers (packinggoodsllc_07668 on Discord: one magpie in China, one in Los Angeles) to pick one. hubWhere: the user's words (settings.RouterNote, one line, 80 characters at most, CleanRouterNote), to every caller; with none, this computer's name (hostName) to a caller that isn't local, and nothing to one that is (one router at hand; the computer's name stays off its prompts). With a value, initialize's instructions say This router: <name>. … on their second line (at the end when no server is behind it), serverInfo.title is Magpie MCP hub · <name>, and each tool's description starts [<name>] (once: a description that starts so already, another magpie's router behind this one, isn't given it again). It is this computer's own (KeepOwn): a sync never brings another's. Set in the Library's MCP tab (below) or settings.json; the Settings page's save keeps it (POST /api/settings). hubWhere in hub_mcp.go; RouterNote, SetRouterNote in settings.go; servers/hub-note in gui/library.go; View.HubNote, View.HubName in sync.go
Magpie Image's project Magpie Image saves into the project of the agent that called it, though one server serves every agent. Its tools are HubTool.Roots: for such a call the router answers the POST as an event stream (mcpExchange) and asks the agent roots/list on it, waiting hubRootsWait (3s); Claude Code answers with its folder. An agent that answers no roots, or none (Codex answers []), is given the workspaces the call's _meta names (Codex's x-codex-turn-metadata.workspaces, keyed by the folder of each git repository it works in, absolute paths only), and failing those the folder of the call's _meta.threadId in Codex's sessions (sessions.CodexThreadCwd, its rollout's cwd), only with consent to read them; with neither (a folder that isn't a repository, no consent), none. A caller that isn't local gets no roots. The call goes on with _meta magpie/roots (file URIs, never null) and magpie/client (the first word of the agent's User-Agent, e.g. claude-code/2.1.314). The image server, seeing magpie/roots, saves into the first root, or ~/Pictures/Magpie with none (never its own folder), draws as <client> magpie-image/1, and sends no magpie/toolCalled report, since the router's row is the call's: its result's _meta magpie/model (provider/model) names the model it drew with, for that row (HubModelMeta). hubRoots, hubClient, WithHubCaller in hub_mcp.go; mcp_ask.go; imageMeta in mcp_hub.go; project, rootOf in imagemcp.go; codex_cwd.go
Guard serveMCP, as /mcp/magpie/tools: a local request, or one with a gateway key; a browser page's Origin that corsAllowed doesn't allow is 403; POST only; a 4 MiB body (hubBody). The outer lanGuard/corsGuard already refuse Origin: null, a foreign site and a rebound Host. Each call is held to the calling key's limit (reserveFor) before anything runs. hubMCP, hubMCPRun in hub_mcp.go; serveMCP in search_mcp.go
Each caller's Magpie Browser session One magpie mcp browser backend serves every agent behind the router, so it alone would be one browser session: every agent in one tab (packinggoodsllc_07668 on Discord). serveMCP gives a client that initializes an Mcp-Session-Id (newMCPSession; magpie keeps nothing by it and refuses nothing for it), and each tools/call is given the caller's session (hubSession): the _meta magpie/session a router in front of this one named, else hub-<agent>-<Mcp-Session-Id>, else none. The router passes it on in _meta magpie/session (HubSessionMeta) only to magpie mcp browser and to another magpie's router (keepsSession), and the bridge sends it as the X-Magpie-Browser-Session of that call (callerSession), so two agents, or two windows of one, work in tabs of their own; a call that names none goes as the bridge's own session, as before. hubSession, newMCPSession in hub_mcp.go and search_mcp.go; keepsSession in mcp_hub.go; callerSession in browsermcp.go
Backends One backend per Library server, shared by every agent (hubPool, keyed by the server's name; an edit or a sign-in since makes it a new one and ends the old). A command is started as CheckServers starts one (startServer: proc tree, home as its folder, ${NAME} env expanded), on its first list or call, and initialized once. A URL (http, sse) is reached with its headers and the Library's MCP sign-in (mcpauth), and a session the server forgot is opened again once. A notifications/tools/list_changed from a server makes the next list ask it again. backendOf, connect, dialHub, dialStdio, dialSSE, dialHTTP in mcp_hub.go; startServer, httpSession in mcp_check.go
Lifetime A server with no call for hubIdle (10 minutes) is ended; the tools it listed are kept, so a list doesn't start it again, and the next call does. A command that died is started again on the next call. Turning the switch off, an edit, and the gateway stopping (StopHub on drain) end them: stdin closed, then the process tree. The switch can be turned off, or a server edited or unticked, by another magpie (the window while magpie serve runs, the CLI), and an agent given each server's own entry again never asks the router, so the serving gateway also prunes every hubPruneEvery (5s) while it holds a server: each one the library no longer has as it was started, or every one once the switch is off, is ended (hubPrune). A server ticked for no agent is still served at /mcp/magpie/hub, so it is kept. use, done, stop, hubStopAll, hubPrune in mcp_hub.go; StopHub, keepHubPruned in hub_mcp.go
GUI Below the server list in the Library's MCP tab: a row with the switch (.lib-switch, role=switch) saying what it does; on, a line counting what it serves (.lib-hubstats: servers, tools, the agents given it), then a row "Other apps" with the address (.lib-hubURL, copied with a click) and Connect…, then a Router name row (.lib-hubnoterow) with a field (.lib-hubname, no native control) holding hubNote and hubName as its placeholder, saved on Enter or leaving it through servers/hub-note {note} (Escape puts the saved words back; an input method's Enter is its own), then a Tools on demand row (.lib-hubtoolsrow) whose switch is on while hubAllTools is false and posts servers/hub-tools {on} (on: every tool whole), and under them the agents that keep their own entries, grouped by why (.lib-hub-why[data-why]). The address is mcpAddress: the public one in a browser, the gateway in the app. Connect… opens a sheet (connectHub, .lib-hubsheet) of two tabs. Agents: agents → magpie → servers, drawn as icons with magpie's bird in the middle, a line of where things stand, then every agent mcpAgents lists (each detected, shown agent that takes MCP servers) in groups (.lib-hubgrp[data-group]): off (not connected), on (connected), kept (keeps each server's own entry). Each row (.lib-hubagent[data-state]: on, none, own, taken, no-remote) has a switch that posts servers/agents-all {agents:[id], on}, as By agent's Turn all on/off does: on gives it every Library server, so hubFor gives it the entry; off takes every Library server from it. A kept agent has a word for why and the reason in full behind (i) (.lib-hubinfo, .lib-hubwhy); an agent that reaches no server over HTTP says so even with none ticked. magpie-image is drawn and counted with the others. Connect all (.lib-huball) posts every none agent at once. With more than 8 agents a search (.lib-hubsearch) narrows every group, and the connected and the kept fold to one row each (.lib-hubfolded) until unfolded. The sheet stays open while it changes, and an agent is never handed to the user as a command to run. Other apps: the address (.lib-hubaddr), and a row per app (.lib-hubapp[data-app]): Cursor (only when it isn't one of the agents), VS Code, Windsurf and any JSON app, each with where its entry goes and a Copy entry (.lib-hubcopy) that copies its JSON. In the app, Cursor and VS Code also get Add (.lib-hubadd): the page posts servers/hub-open {app} and the window opens that app's install link (HubLink), which the app asks the user to confirm. The page never sends a link; an app with none is an error, and nothing is opened. In a browser there is no Add. With the router on, the server editor has a switch "Through the MCP router" (on for shared; not shown with the router off) saved as hub, a direct server's row has a tag (.lib-tag.lib-direct), and the line under the switch and the sheet name the direct servers as given directly (inHub, directNames in client.js; mcp-hub-direct.test.cjs). The TUI keeps the policy on edit. With no agent to write to, or in gateway mode (where no agent is shown), the Library is its MCP tab alone (agentless in client.js): the servers without agent chips, found servers or projects, the router's switch even before the first server, its lines saying any app connects at the address, the status saying the router is on or off, the servers it serves checked once it is on, and Connect… on Other apps alone. A server for remote clients is set up this way on a headless magpie (treeman_26897 on Discord); the remote client still needs a gateway key, through the same serveMCP guard (TestHubMCPRemoteEntryNeedsKey, library-mcp-agentless.test.cjs). hubPart, hubStats, connectHub in client.js, library.css, i18n.js; HubLink in mcp_hub.go; servers/hub-open in gui/library.go
A Remote magpie's router With another magpie as a provider (preset remote-magpie), its router's servers took adding that magpie again under Library → MCP (treeman_26897 on Discord). The gateway's info (GET /v1) says "mcp_hub": true to a caller it serves MCP to (local or sharedWith) while the switch is on with a server that isn't another magpie's router (hubServes). A fetch of a Remote magpie's models (fetch → noteRemoteHub, not one found to be this magpie) asks that info with the provider's key, only while the library would give it a server (RemoteHubAsk: its id is in bridgesOn and isn't in bridgesOffered), and RemoteHubFound then adds one server: named for the provider (bridgeName), http at <root>/mcp/magpie/hub with Authorization: Bearer <key>, bridge = the provider's id, local, on for the agents given the hub's entry. It is the user's call (treeman_26897 again): the Remote magpie's editor has a switch, "Add its MCP servers to the Library" (libraryMCP in /api/providers and the save; library.RemoteMCPOn, library.SetRemoteMCP), off when one is added. On puts its id in bridgesOn and asks at once (provider.AskRemoteHub); off takes its server out. A server added before the switch (no bridgesOn) is kept and shows the switch on. Offered once each time it is switched on: a server removed in the Library turns the switch off (removeServer), one switched off for an agent stays off. Renamed, the provider's Was carries its server's bridge, bridgesOn and bridgesOffered to its id now (adoptBridges); the server keeps its name. A server the user has at that magpie's router already (same origin, the router's path) is the one, and none is added. The provider gone takes the server out (adoptBridges, at Sync and the View read); providers.json unreadable takes nothing out; the provider's address and key changed change the server's; edited to another address or key it is the user's own (keptBridge). No loop: every dial to a URL under /mcp/magpie/hub carries X-Magpie-Router (remoteHeaders), and a request with it (gateway.FromRouter) is given no bridge server, neither listed (Tools, hubServersOf) nor run (Call, hubToolNamed). bridge_mcp.go; remote_hub.go; RouterHeader, hubServes in hub_mcp.go

The CLI's magpie library mcp router on|off (hub, its name before, still works) calls SetMCPHub, and magpie library mcp router <name> shared|direct calls ServerHub. magpie library lists the agents given magpie's entry and those that keep their own, with why (libraryStatus in library_cli.go).

The gateway can't import the library (the library imports the gateway), so the gateway declares Hub and MCPHub, and the library sets MCPHub in its init.

Runtime path

  1. The user turns the switch on. SetMCPHub(true) saves mcpHub and syncs: each agent that can take it gets the one magpie entry, and the servers behind it are taken out of that agent's file. Nothing is started yet.
  2. The agent starts and posts initialize, then tools/list, to /mcp/magpie/hub/<agent>. hubMCP passes the guard. Tools reads library.json for the servers ticked for that agent, and lists each in parallel, by hubListWait (40s): a server not reached by then is left out of that list, not an error. Another app does the same at /mcp/magpie/hub, under the same guard: from another computer it needs a gateway key, as every LAN request does.
  3. tools/call finds the tool by its router name in the same list, holds it to the key's limit, and calls Call under hubCallWait (50s, under the 60s an MCP client gives a call). The server's result goes back as it gave it. A Magpie Image call first asks the agent its roots on the call's stream (above), and the result goes back on that stream.
  4. An error comes back as isError text naming the server: The MCP server S couldn't run T: <why>, where why is how it exited and its last line on stderr, a sign-in it needs, its command not found, or it didn't answer in 50s.
  5. Each call is a row of the request log (serveMCP's callMCPTool): the router's tool name (for call_tool, the tool it ran), the Library server as its host, the agent of the path, how long it took and its error, never its arguments. It counts in no total (Calls to magpie's own MCP tools).

Constraints and failure behavior

  • Off means off: no tool is listed, a call says the router is off, and no server is started (TestHubMCPOff, TestHubToolsAndCalls).
  • magpie takes out only the magpie entry it wrote (Applied.MCP). An entry called magpie of the agent's own, or a Library server called magpie, keeps every server's own entry for that agent, said on the page.
  • A tool turned off in the Library is neither listed nor run, for every agent, including those that have no setting of their own for it.
  • A call that times out leaves the server running for the next; a server that died is started again. A server that can't be started is an error on each call, not a retry loop.
  • Every process the router starts is in a proc tree magpie ends when the switch goes off, the server is edited, it has been idle, or the gateway stops. A test ends them in cleanup (hubSandbox).
  • A caller from another computer (a LAN key, a tunnel or proxy here) or a web page is never given a server that acts as this computer's user (HubServer.HereOnly): Magpie Browser, which /mcp/magpie/browser refuses it directly, magpie's image server, which draws with this magpie's own token past the key's models, accounts and spend, and another magpie's router, which passes a call on to its own browser. They are neither listed nor run, in the gateway (hubServersOf, hubTools) and again in the library (visitorServers, Call) (TestHubKeepsThisComputersOwnServersHere, both packages). Such a caller uses /mcp/magpie/image directly, held to its key. The exception is a keyed visitor's own browser: magpie mcp browser is HubServer.Keyed, given to a caller from elsewhere only when it came with a gateway key (HubVisitor.Key), and hubCall names its session key:<id>/…, so it reaches only a browser that connected with that same key (TestHubGivesAKeyItsBrowser); this computer's own browser stays here.
  • A gateway key with no hold reaches the Library's other command servers through the router from another computer while it is on, as it reaches plugin tools. They run with the user's rights on this computer. A key held to some models, accounts or a spend limit (nativeHeld) is given only the HTTP servers elsewhere (HubServer.Program false), not a program run here.
  • A tools plugin's server (plugin set, Plugin tools) is behind the router as any server is, but its tools are listed and run in this process through the plugin host, not over its URL, and they aren't cached: the host has them.
  • A Remote magpie's server is offered once and asked for only then; a remote whose router is turned off later keeps its server (its tools list empty). The provider's own extra headers (a proxy's) aren't carried to it, and a remote with several keys is asked with its first (p.Key). A server the user added at another magpie's router also sends X-Magpie-Router, so that router leaves out its own Remote magpies' servers for it.
  • The tools a server listed are cached until it says they changed, it is edited, or the switch is turned off. A server that changes its tools without saying so shows them after one of those.

Verification

go test -tags nogui ./internal/gui -run 'TestHubOpen|TestHubNote|TestHubAllTools'
go test -tags nogui ./internal/library -run 'TestHub|TestRunDirect|TestFakeMCPServer|TestCheck|TestPluginTools|TestRemoteMagpieRouter|TestRemoteMagpieMCP|TestCarriedServerIsNoBridge'
go test -tags nogui ./internal/settings -run TestRouterNote
go test -tags nogui ./internal/gateway -run 'HubMCP|OwnMCPRejects|TestHubLeavesRemoteRouterToARouter|TestInfoSaysMCPHub' -timeout 45m
go test -tags nogui ./internal/provider -run 'TestRemoteHubNoted|TestRemoteSelf'
go test -tags nogui ./internal/cli -run TestLibraryMCPHub
node --test internal/gui/tests/library-mcp-hub.test.cjs internal/gui/tests/mcp-hub-direct.test.cjs internal/gui/tests/library-mcp-router-note.test.cjs internal/gui/tests/library-mcp-router-tools.test.cjs

The library tests run a fake stdio MCP server (TestFakeMCPServer in mcp_check_test.go: a tool that dies, one that never answers) and an httptest Streamable HTTP one (mcpHandler). The gateway tests use a fake Hub.