providers: a Remote magpie's MCP servers go to the Library only when switched on, and follow its rename

treeman_26897 (Discord) on v0.1.3127's bridge MCP auto-discovery:

- Renamed (name and id), a Remote magpie's auto-added MCP server went:
  adoptBridges keyed it on the provider id, and after provider.Rename
  that id is gone, so the server was taken out as the provider's. Now
  RemoteHub carries the provider's Was, and adoptBridges moves the
  server's bridge, bridgesOn and bridgesOffered from an id it had to
  the id it has (one that another provider has now stays that one's).
  The server keeps its name.
- The add dialog had only the address and key. A new Remote magpie's
  editor has a Name and an ID under the key; blank, they are the
  preset's as before (provider.Add numbers them when taken), and the
  ID follows the name until one is typed.
- Adding its MCP servers is opt-in: a switch, "Add its MCP servers to
  the Library", in the add dialog (off) and in a saved one's editor.
  It lives in library.json (bridgesOn); /api/providers says it as
  libraryMCP and the save takes it (library.SetRemoteMCP). On asks the
  remote at once (provider.AskRemoteHub) when no model list is fetched
  on that save; off takes its server out. Only one switched on is
  asked (bridgeWanted). A server auto-added before this (no bridgesOn)
  is kept, and its switch shows on. A server removed in the Library
  turns its switch off; the provider gone takes its switch with it.

Run (mote, at this tree):
- go test ./internal/library ./internal/provider ./internal/gui:
  library ok; provider ok but TestRenewLoginsKeepsClaudeReading
  (#5481, red the same at the base); gui ok but TestMainWindowCorners
  (#3949). The first gui run had TestCoreScriptBudget red (app.js
  over by ~1.2KB); the new code was made compact and seven editor
  comment blocks cut to one line each. Rebased on 09812e66b the core
  was 68 bytes over again, so three more comments near the editor
  (decidesModel, modelAPIs, pickedOf) were cut: app.js is 161 bytes
  smaller than at its base, and the budget (not raised) passes there:
  go test -run 'TestCoreScriptBudget|Provider' ./internal/gui ok.
- go vet ./..., GOOS=windows go vet ./..., GOOS=linux go vet -tags
  nogui ./...: ok; gofmt -l clean.
- node --test remote-magpie (chromium + webkit, en + zh), i18n-keys,
  i18n-en-no-cjk, gui-de, gui-ja, gui-zh-tw, preset-name,
  name-suffix-remote, remote-self, provider-unmasked, key-row-chips-fit,
  ctx-head-narrow, routing-remote-windows, usage-remote-cards,
  old-webkit: all
  green except reds that are red the same at the base 1ccad582d and
  filed: remote-magpie's "asks for the other magpie's address" fails
  at its decision-model chips before reaching anything this changes
  (#5013, #4398), and i18n-keys' "a plugin's words are its own"
  (#5332, #5426, #5482). With that one chips assertion bypassed in a
  local copy, remote-magpie's new assertions are 12 of 12 green.
  name-suffix-remote went red in Chromium (zh, zh-TW, de) beside 10
  other files, its scrollends.js import failing (#5147); alone it is
  10 of 10 green. The switch's label in the editor is the existing
  "MCP servers".

Broken by hand:
- the Was mapping off: TestRemoteMagpieMCPFollowsRename red ("renamed
  home, read before a sync: switch map[]").
- bridgeWanted/adoptBridge not asking bridgesOn: TestRemoteMagpieMCPIsOptIn
  red ("switched off, asked").
- the new editor's switch defaulting on: remote-magpie red ("off when
  added") in both engines.

Not tried: a real rename against a real second magpie (the library
tests fake RemoteHubs; the provider test renames a real providers.json
in a sandbox and checks Was reaches RemoteHubs). The CLI (provider
add/set remote-magpie) and the TUI have no switch: from there a new
Remote magpie gets no MCP server until it is switched on in the GUI.

Reported-by: treeman_26897 (Discord)
This commit is contained in:
yetone
2026-11-06 15:41:43 +08:00
parent 09812e66bc
commit 4c8e1cfdfc
11 changed files with 384 additions and 56 deletions
+2 -2
View File
@@ -19,7 +19,7 @@ With the Library's switch "MCP router" on (#2679, @minggang123 on X), each agent
| 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`](../../internal/library/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`](../../internal/gui/assets/plugins/library/client.js), [`library.css`](../../internal/gui/assets/plugins/library/library.css), [`i18n.js`](../../internal/gui/assets/plugins/library/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 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. Offered once: a user who removes it or switches it off keeps it so until the provider is removed and added again. 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`](../../internal/library/bridge_mcp.go); [`remote_hub.go`](../../internal/provider/remote_hub.go); `RouterHeader`, `hubServes` in [`hub_mcp.go`](../../internal/gateway/hub_mcp.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`](../../internal/library/bridge_mcp.go); [`remote_hub.go`](../../internal/provider/remote_hub.go); `RouterHeader`, `hubServes` in [`hub_mcp.go`](../../internal/gateway/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`](../../internal/cli/library_cli.go)).
@@ -50,7 +50,7 @@ The gateway can't import the library (the library imports the gateway), so the g
```sh
go test -tags nogui ./internal/gui -run 'TestHubOpen|TestHubNote|TestHubAllTools'
go test -tags nogui ./internal/library -run 'TestHub|TestRunDirect|TestFakeMCPServer|TestCheck|TestPluginTools|TestRemoteMagpieRouter|TestCarriedServerIsNoBridge'
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'