Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
library: an MCP hub, one magpie server in place of every library server (#2679)
yetonee0ea15b8e882
diff --git a/docs/reference.md b/docs/reference.mdindex 668a55c3a..48515ee3f 100644--- a/docs/reference.md+++ b/docs/reference.md@@ -1953,6 +1953,21 @@ It exposes:| `/v1/models`, `/v1beta/models` | the catalog || `/v1/codex/models` | the catalog as Codex's `model_catalog_url` reads it ([Codex on another computer](#providers-and-the-gateway)) || `/api/version`, `/api/tags`, `/api/show` | the catalog as an Ollama server lists it, for a client that takes a custom endpoint only as Ollama (below) |+| `/mcp/magpie/hub` | MCP (Streamable HTTP): every MCP server in the Library, as one server, for any app, while *Library → MCP → MCP hub* is on |+| `/mcp/magpie/hub/{agent}` | MCP (Streamable HTTP): the Library's MCP servers ticked for that agent, as one server, while *Library → MCP → MCP hub* is on |++With that switch on, each agent that takes an MCP server over HTTP is+given one entry, `magpie`, in place of the Library's servers, and magpie+runs those servers itself, once for every agent: a command is started on+its first call and ended after 10 minutes with none. Each tool is named+`<server>__<tool>`, and a tool turned off in the Library isn't listed. An+agent that reaches no server over HTTP (Claude Desktop), or has an entry+called `magpie` of its own, keeps each server's own entry, and the page+says so. An agent in WSL or on another computer is given the address it+reaches magpie at, with its key. Turning the switch off gives every agent+each server's own entry again. It is guarded as the gateway is: a local+request, or one with a gateway key, and no other web page+([MCP hub](subsystems/mcp-hub.md)).Each `/v1/models` entry includes `reasoning` and `supported_reasoning_levels`(`[{"effort":"low"}, ...]`), and the same levels as plain names indiff --git a/docs/subsystems/README.md b/docs/subsystems/README.mdindex b9cbfb23b..75bda3211 100644--- a/docs/subsystems/README.md+++ b/docs/subsystems/README.md@@ -15,6 +15,7 @@ These pages describe current responsibilities, runtime paths, state, and contrac| Connecting an agent's config to the gateway, drift, disconnect, WSL twins | [Agent wiring](agent-wiring.md) || Providers, keys, subscription accounts, refresh and allowance | [Providers and accounts](providers-accounts.md) || Instructions, MCP servers, skills and RTK written into agents | [Library](library.md) |+| One MCP server, magpie, given to every agent in place of the Library's servers | [MCP hub](mcp-hub.md) || Tray, panel, window, `magpie web` and the page's JSON API | [GUI app shell](gui-shell.md) || Encrypted backups and setup synchronization over WebDAV, S3 or GitHub | [Sync and backup](sync-backup.md) |diff --git a/docs/subsystems/library.md b/docs/subsystems/library.mdindex fe50d28a9..cc519d84a 100644--- a/docs/subsystems/library.md+++ b/docs/subsystems/library.md@@ -20,6 +20,7 @@ agents RTK, a CLI that cuts shell output down before the model reads it.| Projects | A project (`Project`) is a folder given some of the library's skills and MCP servers, each for some agents. A skill is linked (or copied) into the folder the agent reads a project's skills from (`projectSkillsDirs`): `.claude/skills` for Claude Code, `.qoder/skills` for Qoder and Qoder CN, `.codebuddy/skills` for CodeBuddy Code, `.commandcode/skills` for Command Code, and `.agents/skills` for Codex, Gemini CLI, OpenCode, Pi, Cursor, Copilot CLI, Antigravity, Kimi, Goose, Grok, Droid and Cline. A server is written into the file the agent reads a project's servers from (`projectMCPFiles`), in the shape its user-wide file has: `.mcp.json` for Claude Code and CodeBuddy Code (CodeBuddy's `mcp.json` when the project has that and no `.mcp.json`, since it reads only the first of the two), `.codex/config.toml`, `.gemini/settings.json`, `.cursor/mcp.json`, `opencode.json`, `.zcode/config.json`, `.pi/mcp.json`, `.qoder/settings.json`'s `mcpServers` for Qoder and Qoder CN, and `.factory/mcp.json` for Droid. Claude Code, Qoder and CodeBuddy ask the user to approve a project's server before using it. Only targets whose vendor source or docs name them are listed (each map's comment cites them); an agent with none (Copilot CLI's MCP, Zed, WorkBuddy, Command Code's MCP, whose `.mcp.json` has its own `transport` shape) can't be picked for a project (eirc on Discord). `Placed`, `Wrote`, `Made` and `MadeFiles` record what magpie put there, the only things it takes away, and what it made is listed in the project's `.gitignore`. | [`projects.go`](../../internal/library/projects.go), [`project_mcp.go`](../../internal/library/project_mcp.go) || Instructions | magpie's part of an agent's instructions file sits between two marker lines. The rest of the file is the user's. Several named sets can be switched between (#106). A set can be kept in a file of the user's own instead of `instruction-sets/` (`InstrSet.File`, 新v学员 on Discord): `~/.agents/AGENTS.md`, which Droid and omp read too. `textPath` reads it, `putText` keeps it aside and writes it but never removes it, removing the set lets go of it, and `linkable` refuses a file magpie writes into (an agent's own instructions, a file with the marker line), the library's own and another set's. The app's `KeepLinked` looks at the file every 30 s and writes a change into the agents (`SyncLinked`). A set's text is read without magpie's part (`setText`), and an agent whose instructions file is, links followed, the file the set on is kept in (`isSetFile`: `instructions.md` linked to `~/.claude/CLAUDE.md`, or both linked to one dotfile) gets no part written into it, and one written before is taken out with every part nested in it: read with its part, each sync wrote the whole file into that part again and it doubled, to gigabytes (#2349, `TestInstructionsSetLinkedToAgentsFile`). `Collect` carries the set's text, not where its file is; `Put` keeps this computer's file for the set and never empties it for a text not carried. Alma's file is `~/.config/alma/USER.md`, which its main chat reads into its system prompt as its owner's profile (0.4.164's `out/main/index.js`; a workspace's `USER.md` is read instead), and whose leading front matter (`name`, `telegram_id`, `discord_id`) is who its bots answer to (#2315, Jie211). The target is a `Profile`: magpie's part goes after the user's, so the front matter stays first, and `ImportInstructions` refuses it, as moving it out would take that front matter out of the file and into every agent's (`TestAlmaInstructionsKeepTheProfile`, `TestAlmaProfileIsNotImported`, `library-alma-profile.test.cjs`). | [`instructions.go`](../../internal/library/instructions.go) || MCP servers | A `Server` is a command or a URL (`http`, `sse`). `mcpFile` encodes it in each agent's own format (one `mcpFormat` per agent family) and merges it with the user's own fields for that entry. A server's `StartupTimeout` (#3168) goes to Codex alone, as `startup_timeout_sec` in seconds (`RawMcpServerConfig` in codex-rs/config's mcp_types.rs; 0 leaves Codex's own 30s), in the user-wide config.toml and a project's alike. magpie keeps the value it wrote (`Applied.Startup`, `Project.Startup`): one in the entry other than that, or a `startup_timeout_ms`, is the user's and stays as it is (`startupTheirs`), and once it is taken out magpie's goes in again. The editor shows the field only when an agent that takes it is here (`AgentView.Startup`, `mcp_startup_test.go`, `mcp-startup-timeout.test.cjs`). `Also` files get the same servers; `Extra` files are only read when importing. `CheckServers` connects as an agent would and keeps the result in memory only: with each tool's name and the first paragraph of its description (`ToolList`, at most `maxToolList`, a description cut at `maxToolDesc` runes), which the page lists when the server's "N tools" is clicked, following tools/list's `nextCursor` up to `maxToolPages` pages (`listTools`). Each step (initialize, notifications/initialized, tools/list; connect for SSE) has its own budget, 60s for a URL and 30s for a command (`remoteStep`, `stdioStep`), and a timeout names its `Step` and how long it `Waited`; a timeout isn't kept, so the next look asks again (#1467). A command named bare that magpie's PATH lacks is started from where `proc.FindTool` finds it (fnm's Nodes on Windows among the folders), with that folder on the check's PATH, since npx's package runs the node beside it by name (`stdioProgram`, #2685, `TestStdioProgramFindsWhatPathLacks`). The page asks once a session for the servers some agent or project has (`inUse` in plugins/library/client.js); one no agent has shows "not checked" and is checked only when its status is clicked (#1597, `library-mcp-health.test.cjs`). A server checked once is checked again when it changes, its sign-in included, so a server signed in to after a click's "needs sign-in" isn't left showing both (新v学员 on Discord, `mcp-signin-health.test.cjs`). The check sends magpie's sign-in as the gateway does: on a 401 it renews the token once and asks again, and a server that refuses even the renewed token has the sign-in marked run out (`mcpauth.Rejected`, also from the gateway's `/mcp/<name>`), which `ServerHealth.SignIn` carries back so the row's tag says "Sign-in ran out" rather than "Signed in" beside "needs sign-in". A server that refuses the check (401 or 403) but that an agent signed in to on its own is named so (`ServerHealth.AgentSignIn`, `readOwnSignIns` in `mcp_agent_signin.go`): each Claude Code target's `.credentials.json` beside its settings.json, a WSL distro's under the distro's home, is read for a `mcpOAuth` entry of the server's name and URL with a token, and the row says "signed in in Claude Code (Ubuntu)" instead of "needs sign-in" (CyberCaius on Discord: signed in to with Claude Code's /mcp in WSL). The tokens are never sent or kept; a file that can't be read or parsed names nobody, a stopped distro isn't started for it, and on a Mac only the file is read, never the Keychain Claude Code keeps them in. They are part of the check's key, so a sign-in in the agent since is checked anew (`TestCheckNamesAgentsSignedInThemselves`, `library-mcp-health-wsl.test.cjs`). A server that needs OAuth is signed in once in magpie, and agents reach it through the gateway's `/mcp/<name>` (#615). The editor's Sign in saves the form first only when the sign-in is to another server than the saved one (a new server, a name or URL changed), and its button then reads "Save and sign in"; anything else edited (headers, env, arguments) stays unsaved until Save, so Cancel leaves the server as it was (新v学员 on Discord, `mcp-editor-cancel.test.cjs`). A server whose authorization server refuses magpie's dynamic client registration with 401 or 403 admits only the clients it lists (Figma's, #1619): the sign-in fails with `mcpauth.ErrClientRefused`, which says so and that each agent signs in itself, and the agents keep the server's own address. A server whose authorization server has no registration endpoint at all (GitHub's remote server, 新v学员 on Discord) fails with `mcpauth.ErrNoRegistration`, which says to make an OAuth app with the vendor and give it in the editor; the editor then opens its "OAuth app" fields. The app is the server's `oauth` (`OAuthApp`: `clientId`, `clientSecret`, `callbackPort`, Claude Code's names for `--client-id`, `--client-secret` and `--callback-port`), checked on save (HTTP only, a client ID, a port from 1 to 65535), and `SignInServer` signs in as it through `mcpauth.StartWith` without registering. The callback is Claude Code's, `http://localhost:<port>/callback` (its docs' "Use pre-configured OAuth credentials"; 2.1.296 builds it so), listened for on 127.0.0.1 alone as Claude Code does, so an app made for Claude Code signs magpie in as it is; a server that matches the registered URL exactly refuses another host, which is why Claude Code's 2.1.231 went back to `localhost` from 2.1.229's `127.0.0.1` (`TestAppMadeForClaudeCode`). Without a `callbackPort` the port is any free one, which a loopback callback registered without a port takes (GitHub's, RFC 8252); a fixed `callbackPort` is for a vendor that matches the port too. magpie's own registration, when a server has one, still names `127.0.0.1`. The client secret is kept in library.json, as a header's or env's value is: a backup without keys leaves it out, and `WithSecrets` puts back this computer's secret only for the same client ID; no agent is given the app, and the TUI's editor, which doesn't show it, keeps it. GitHub answers a token request's error with a 200 and `error` in the body (`bad_refresh_token`, `incorrect_client_credentials`), which `tokenRequest` reads as the error it is, and a dead refresh token marks the sign-in run out (`github_app_test.go`, `mcp-oauth-app.test.cjs`). magpie never registers under another client's name; one that reaches the gateway from another machine (a WSL distro under NAT) is given it with the LAN sharing key as its `Authorization` (`agent.KeyAt`), as its models are, which the gateway takes in place of a sign-in and never passes on; turning sharing on or off, or a new key (`access.LANChanged`), writes such entries again. `ConfigOfServers` is the servers picked as an agent's project file (`projectMCPFiles`) would hold them, written by the same code into a folder of its own and read back, with those the agent can't take there named; the page's Copy config… puts it on the clipboard for a project magpie doesn't keep (#1478). Each listed tool has a switch: the tools turned off are the server's `DisabledTools`, set by `ServerTools`, and written into each agent given the server that can leave out a single tool (see the constraint below). A server run as `npx -y <package> args` (a registry package, no other npx flag) is given to an agent on this machine as `node <the package's bin> args` (`npxDirect`): magpie installs the package with npm into a folder of its own under `mcp-npm` in the background, the next sync writes the node form, and until then, or when npm fails (tried again after an hour), the agent keeps npx. npx otherwise stays as the server's parent for its whole life, a node and two cmd.exe beside each instance on Windows (#3885). An unpinned spec is installed again after a day, as npx would fetch the newer one; an install no library server runs goes a day after its last. The library, a server's check, Copy config… and agents in WSL keep npx (`TestNpxServerRunsByNode`, `TestReadNpx`). | [`mcp.go`](../../internal/library/mcp.go), [`mcp_npx.go`](../../internal/library/mcp_npx.go), [`mcp_check.go`](../../internal/library/mcp_check.go), [`mcp_signin.go`](../../internal/library/mcp_signin.go), [`mcp_agent_signin.go`](../../internal/library/mcp_agent_signin.go), [`mcp_copy.go`](../../internal/library/mcp_copy.go), [`mcp_tools_off.go`](../../internal/library/mcp_tools_off.go) |+| MCP hub | With `MCPHub` on, `syncMCP` gives each agent that can take it one entry, `magpie`, at the gateway's `/mcp/magpie/hub/<agent>`, in place of the servers ticked for it; magpie runs those servers itself, once for every agent. | [MCP hub](mcp-hub.md), [`mcp_hub.go`](../../internal/library/mcp_hub.go) || Skills | A skill is a folder with a `SKILL.md`. It comes from GitHub, from any other git repository, from a folder on this machine (linked, so editing the folder edits the skill) or from the market. A git repository (`kind: git`, sunset on Discord: GitLab, a company's own GitLab) is any https, http, ssh or `file://` address, `git@host:group/repo.git`, a bare `host.name/group/repo`, or a GitLab or Gitea page of a folder (`/-/tree/<ref>/<path>`, `/-/blob/…`, `/src/branch/…`; a branch with a `/` in its name is read as its first segment) (`gitSource`). It is fetched by the user's own `git` (`proc.FindTool`), so their credential helpers and SSH keys reach an intranet host: `git init`, then `fetch --depth 1 --no-tags -- <url> <ref>` and `checkout FETCH_HEAD` into a `magpie-skill-*` folder, then its `.git` is removed (`fetchGit`). What the user typed is data: an address starting with `-` and a `transport::` address (`ext::`, `fd::`, `file::`) are refused before git runs, the address follows `--`, git runs with `protocol.ext.allow=never` and `core.symlinks=false`, no shell, `GIT_TERMINAL_PROMPT=0` and (unless the user set an SSH command) `ssh -o BatchMode=yes`, so a sign-in it can't make fails instead of waiting. An address with a password or token in it is refused: library.json is shown and backed up. Git missing, a sign-in refused, a host key unknown, a branch missing and a host unreachable each say so (`gitError`; `TestGitSourceReads`, `TestInstallFromGitRepo`, `TestGitTreeURLInstallsFolder`, `TestGitMissingSaysSo`, `TestGitSignInRefusedSaysSo`, `TestGitRefusesOptionsAndTransports`, `TestGitErrorsSaid`). Each agent gets a link or a marked copy (#896, `SkillHow`). An agent in a WSL distro always gets a copy. A copy's mark keeps the hash of what was copied, so a sync knows a copy unchanged since then is current without reading its files (`copyIsFresh`, #1031); other copies are hashed. A skill an agent has of its own (`foundSkills`, the page's "In your agents") is brought in with `ImportSkill`, or taken out of every agent that has it with `RemoveFoundSkill` (#1303): its folders and copies of the very same files, its shared `~/.agents/skills` entry and a folder of it in the library's own folder go to the backups, its links are taken away, and a folder elsewhere it only links to, or another skill by that name, stays. Several go at once with `RemoveFoundSkills` (`POST /api/library/skills/remove-found-some`, wongxy on Discord): the "In your agents" heading's own Select picks them, and Remove… asks once, naming the count, before one change takes each out as `RemoveFoundSkill` would. A name that can't be taken out is left as it was, in `Result.Unremoved`, and stays picked with its error in its row's place (`foundErrs`); when none can go the change fails and nothing is touched. This Select and the library's are never on together: while one is on the other is dimmed, saying why, so neither click takes the other's bar away from above the pointer (`TestRemoveFoundSkills`, `found-skill-pick-remove.test.cjs`). A repository is fetched as one tarball of at most `maxFetch` (200 MB); a bigger one is fetched file by file from its tree (`fetchSkillFiles`), only as far as the caller reads: a link to a folder and an update ask GitHub for that folder's own list (`treeUnder`, git's `ref:path`; the whole list when that fails), the market fetches only the skills whose folder is the one it installs (#2794, sickn33/agentic-awesome-skills' 8,589 skills are 287 MB together), and a fetcher keeps such a part apart from another skill's of the same repository, which would otherwise read as removed (`fetcher.get`, `TestMarketSkillFromAHugeRepository`). GitHub's list is read up to `maxTree` (64 MB; that repository's is 11 MB) and an answer over the cap a caller gives `apiGet`, or one cut off, is an error saying so, never a body cut short for JSON to trip on (ye4241's `TestRepoTreeBigList`, `TestApiGetWholeAnswer`); a list GitHub truncated itself is too big (`TestTruncatedListIsTooBig`). And a skill over 200 MB on its own is left out rather than failing the rest: the probe lists the others and names it in `Probe.Left`, under the list in the page and in the TUI's flash. Only when every skill is too big does the fetch fail (#2062, mikubaka88/CCFA-Skills' 297 MB ccf-paper-writer; `TestOneSkillTooBigLeavesTheOthers`, `skill-too-big.test.cjs`). A probe keeps its download for the install from it for 15 minutes (`keepProbe`, `probeLife`), and removes it when it is probed again or that time is up, not at the next probe, which may never come. A download a magpie that quit left in the temp dir goes at the next probe once it is an hour old (`sweepStaleDownloads`). The package's TestMain gives the tests a temp dir of their own and fails a run that leaves a `magpie-skill-*` in it (#2321, `TestProbeDownloadEndsWithIt`, `TestStaleDownloadsSwept`). | [`skilltree.go`](../../internal/library/skilltree.go), [`skills.go`](../../internal/library/skills.go), [`skillgit.go`](../../internal/library/skillgit.go), [`skill_how.go`](../../internal/library/skill_how.go), [`skill_edits.go`](../../internal/library/skill_edits.go), [`skill_from.go`](../../internal/library/skill_from.go) || Skills market | `MarketSkills` lists skills.sh's most installed and what its search finds. magpie's own skills (`featuredSkills`), in [magpie-community/plugins](https://github.com/magpie-community/plugins/tree/main/skills), go first, marked official: `magpie-setup` has an agent set magpie up as the user asks through its commands (providers, accounts, agents, routing groups and rules, gateway keys and limits, models, the library), and `magpie-quota` lets an agent read `magpie quota --json` and wait with `magpie quota wait`. Their commands live in magpie's CLI, so a change to a command either uses gets the skill updated in that repository. Until skills.sh lists one it has no count of installs and no skills.sh link (`featured`). `InstallMarketSkill` installs any of them from its repository. | [`market.go`](../../internal/library/market.go) || Skills a repository added | A check for updates (`CheckSkills`) also asks, for each GitHub repository and ref the library has skills from, GitHub's list of its files (`repoTree`, one API request, free when the ETag still matches). Only when that list has a skill folder beside the installed ones (under `newBase`, their deepest common folder) that the library doesn't have and hasn't offered does `findNew` fetch the repository for the skills' names and descriptions. `SeenSkills` records per `repo@ref` the paths the install picker showed and the ones set aside, so they aren't offered again. The finds live in memory (`newFound`) and reach the page as `View.NewSkills`; `AddNewSkills` gives each to the agents that have its repository's other skills, and `IgnoreNewSkills` sets it aside (`/api/library/skills/{add-new,ignore-new}`). | [`skillnew.go`](../../internal/library/skillnew.go), [`skillcheck.go`](../../internal/library/skillcheck.go) |@@ -57,6 +58,7 @@ agents RTK, a CLI that cuts shell output down before the model reads it.- OpenCode: `"<server>_<tool>": false` in the top-level `tools` of opencode.json (opencode.ai/docs/mcp-servers, "Manage"), the names spelt as `name()` in `packages/core/src/tool/mcp.ts` does; `migrateTools` in `packages/core/src/config/normalize.ts` makes each a deny rule (sst/opencode 0d61729b).- Every other agent (Cursor, Copilot and Goose, whose lists are allow-lists only, Zed, Kimi, Pi, Grok, Devin, Antigravity, Alma, dsh, omp, ZCode, Command Code, Cline, WorkBuddy, CodeBuddy, Droid, Claude Desktop, MiMo Code, DevEco Code, a plugin's agent) gets the server whole. `AgentView.ToolsOff` is false for it, and the open list of tools names those given the server as unable to turn off single tools. A project's file and Copy config… carry no tools turned off either.magpie records what it added (`Applied.ToolsOff` per server for a list in an entry, `Applied.ToolRules` for Claude Code's rules and OpenCode's keys) and takes back only that (`offList`): a name, rule or key the user had already is theirs and stays when the tool is turned on again, and an OpenCode key the user set to `true` is never written over. A list or `permissions.deny` that isn't a list of names is a problem, never taken as empty, and the file is left as it is. The key, and Claude Code's `permissions` or OpenCode's `tools`, goes when magpie's were the last in it. The editor's save sends no tools, so `SaveServer` keeps those turned off.+- With the MCP hub on (`MCPHub`), an agent that takes a Streamable HTTP server is given magpie's one `magpie` entry in place of the Library servers ticked for it, and each server's own entry again when it is turned off. One that can't, or has an entry called magpie of its own, or whose library has a server called magpie, keeps each server's own entry, and the page says why ([MCP hub](mcp-hub.md)).- magpie's own MCP server (`magpie-image`, and any `mcp …` run by a program named magpie) runs this computer's magpie (#1439). `InstallServer` gives it magpie by full path (`update.Program`: the AppImage, not its mount; Homebrew's opt link, not the Cellar), and that path is right only on this computer. When the library is read, and after every change, `heal` points such a server whose program isn't on this computer (another system's path, a magpie moved out of Downloads, a bare `magpie`) at this magpie, so the agents and `CheckServers` get it; a magpie program that is here (another build) and a server run by any other program stay as they are. A carried library (`Collect`, for a backup or a WebDAV or S3 sync) names it as plain `magpie`, so two computers carry the same bundle and a sync doesn't write one's path over the other's; `Put` then reads it as that computer's magpie. [`self_here.go`](../../internal/library/self_here.go)- A carried library (`Collect`) has every file of each skill, its templates and other binaries included, up to `maxCarried` (16 MiB) a file and `maxCarriedAll` (32 MiB) in all, chosen smallest first across the skills (`readSkills`), so every skill's text goes before any big file. The total keeps a sealed backup under the 64 MiB a sync reads: the files are base64 twice in it. What is left out is named in `Left`, and `Put` keeps this computer's copy of such a file in the skill it puts (`keepLeft`). [`carry.go`](../../internal/library/carry.go)- WorkBuddy's servers go in `$WORKBUDDY_CONFIG_DIR/mcp.json` (default `~/.workbuddy`; WorkBuddy AI, the international build, keeps its own in `~/.workbuddy-ai`, #1494), and CodeBuddy Code's go in the first of `.mcp.json`, `mcp.json` and `~/.codebuddy.json` that exists, as `resolveMcpFilePath` reads them (#1266). WorkBuddy connects a server only after the user has switched it on once in its own MCP settings. It keeps that approval in `mcp-approvals.json`, keyed by the command, args and env names or the URL's origin, so changing those asks again. magpie never writes the approval, and the servers page says so, by the build's name, when a server is given to WorkBuddy or WorkBuddy AI.@@ -76,11 +78,11 @@ agents RTK, a CLI that cuts shell output down before the model reads it.## Verification```sh-go test -tags nogui ./internal/library -run 'TestGit|TestInstallFromGitRepo|TestReadLooksAtEachFolderOnce|TestLinkChangedBetweenVisitsShows|TestUnreadablePartIsLookedAtAgain|TestCheckSkillsFindsNewOnes|TestNewBase|TestUpdateKeeps|TestCheckDue|TestCheckSkillsMarksItDone|TestTargets|TestWSLPiMCP|TestWSLMCPSiblings|TestServer|TestImportServer|TestCheck|TestInstructions|TestInstructionsSetLinkedToAgentsFile|TestInstructionSets|TestTakesAlma|TestAlma|TestLinkedSet|TestLinkRefused|TestSkill|TestRemoveFoundSkill|TestInstallSet|TestInstallLists|TestEverySkillAgents|TestEveryServerAgents|TestSomeServersAgents|TestRemoveServers|TestRTK|TestEnvRefs|TestDshRefRoundTrip|TestDshImportKeepsJS|TestAgentMCPFiles|TestCodeBuddyMCPFile|TestZedMCP|TestOwnServerFromElsewhereRunsThisMagpie|TestUsersOwnServerStaysAsItIs|TestCarriedOwnServerIsEachComputersMagpie|TestCarry|TestPageReadDoesntReadUnchangedSkillsAgain|TestJustWrittenSkillIsReadAgain|TestUnreadableSkillIsntRememberedAsDifferent|TestGlanceLeavesOutTheAgentsSkills|TestPluginAgent|TestPluginMCPFormatsAreWritten|TestToolsOff|TestOffList|TestHooks|TestSubagent|TestCheckRenewsMagpiesSignIn|TestCheckWSLOnlyServerInItsDistro|TestCheckWindowsServerHere|TestCheckMixedServerEachSide|TestCheckSaysSignInRanOut|TestCheckNamesAgentsSignedInThemselves|TestSignInAsTheUsersOwnApp|TestOAuthAppChecked|TestOAuthAppSecretCarried|TestGoneAgentListedToTakeOut|TestUpdateFindsSkillsRemovedUpstream|TestGoneFromNeedsTheSourceRead'+go test -tags nogui ./internal/library -run 'TestGit|TestInstallFromGitRepo|TestReadLooksAtEachFolderOnce|TestLinkChangedBetweenVisitsShows|TestUnreadablePartIsLookedAtAgain|TestCheckSkillsFindsNewOnes|TestNewBase|TestUpdateKeeps|TestCheckDue|TestCheckSkillsMarksItDone|TestTargets|TestWSLPiMCP|TestWSLMCPSiblings|TestServer|TestHub|TestImportServer|TestCheck|TestInstructions|TestInstructionsSetLinkedToAgentsFile|TestInstructionSets|TestTakesAlma|TestAlma|TestLinkedSet|TestLinkRefused|TestSkill|TestRemoveFoundSkill|TestInstallSet|TestInstallLists|TestEverySkillAgents|TestEveryServerAgents|TestSomeServersAgents|TestRemoveServers|TestRTK|TestEnvRefs|TestDshRefRoundTrip|TestDshImportKeepsJS|TestAgentMCPFiles|TestCodeBuddyMCPFile|TestZedMCP|TestOwnServerFromElsewhereRunsThisMagpie|TestUsersOwnServerStaysAsItIs|TestCarriedOwnServerIsEachComputersMagpie|TestCarry|TestPageReadDoesntReadUnchangedSkillsAgain|TestJustWrittenSkillIsReadAgain|TestUnreadableSkillIsntRememberedAsDifferent|TestGlanceLeavesOutTheAgentsSkills|TestPluginAgent|TestPluginMCPFormatsAreWritten|TestToolsOff|TestOffList|TestHooks|TestSubagent|TestCheckRenewsMagpiesSignIn|TestCheckWSLOnlyServerInItsDistro|TestCheckWindowsServerHere|TestCheckMixedServerEachSide|TestCheckSaysSignInRanOut|TestCheckNamesAgentsSignedInThemselves|TestSignInAsTheUsersOwnApp|TestOAuthAppChecked|TestOAuthAppSecretCarried|TestGoneAgentListedToTakeOut|TestUpdateFindsSkillsRemovedUpstream|TestGoneFromNeedsTheSourceRead'go test -tags nogui ./internal/cli -run 'TestLibrarySkillRm'go test -race ./internal/mcpauthgo test -tags nogui ./internal/library-node --test internal/gui/tests/library-items.test.cjs internal/gui/tests/library-alma-profile.test.cjs internal/gui/tests/library-rtk-gain.test.cjs internal/gui/tests/library-rtk-nohook.test.cjs internal/gui/tests/library-servers-bulk.test.cjs internal/gui/tests/library-pick-remove.test.cjs internal/gui/tests/library-env-refs.test.cjs internal/gui/tests/library-zed-mcp.test.cjs internal/gui/tests/library-workbuddy-trust.test.cjs internal/gui/tests/skill-new-in-repo.test.cjs internal/gui/tests/skill-update-edited.test.cjs internal/gui/tests/skill-gone.test.cjs internal/gui/tests/skill-git-source.test.cjs internal/gui/tests/remove-found-skill.test.cjs internal/gui/tests/found-skill-pick-remove.test.cjs internal/gui/tests/library-loading.test.cjs internal/gui/tests/library-mcp-tools.test.cjs internal/gui/tests/library-mcp-tool-switch.test.cjs internal/gui/tests/library-hooks.test.cjs internal/gui/tests/library-subagents.test.cjs internal/gui/tests/library-mcp-health.test.cjs internal/gui/tests/library-mcp-health-wsl.test.cjs internal/gui/tests/mcp-signin-health.test.cjs internal/gui/tests/mcp-signin.test.cjs internal/gui/tests/mcp-editor-cancel.test.cjs internal/gui/tests/mcp-oauth-app.test.cjs internal/gui/tests/library-ime-scroll.test.cjs+node --test internal/gui/tests/library-items.test.cjs internal/gui/tests/library-mcp-hub.test.cjs internal/gui/tests/library-alma-profile.test.cjs internal/gui/tests/library-rtk-gain.test.cjs internal/gui/tests/library-rtk-nohook.test.cjs internal/gui/tests/library-servers-bulk.test.cjs internal/gui/tests/library-pick-remove.test.cjs internal/gui/tests/library-env-refs.test.cjs internal/gui/tests/library-zed-mcp.test.cjs internal/gui/tests/library-workbuddy-trust.test.cjs internal/gui/tests/skill-new-in-repo.test.cjs internal/gui/tests/skill-update-edited.test.cjs internal/gui/tests/skill-gone.test.cjs internal/gui/tests/skill-git-source.test.cjs internal/gui/tests/remove-found-skill.test.cjs internal/gui/tests/found-skill-pick-remove.test.cjs internal/gui/tests/library-loading.test.cjs internal/gui/tests/library-mcp-tools.test.cjs internal/gui/tests/library-mcp-tool-switch.test.cjs internal/gui/tests/library-hooks.test.cjs internal/gui/tests/library-subagents.test.cjs internal/gui/tests/library-mcp-health.test.cjs internal/gui/tests/library-mcp-health-wsl.test.cjs internal/gui/tests/mcp-signin-health.test.cjs internal/gui/tests/mcp-signin.test.cjs internal/gui/tests/mcp-editor-cancel.test.cjs internal/gui/tests/mcp-oauth-app.test.cjs internal/gui/tests/library-ime-scroll.test.cjs```The package's `TestMain` runs in a home of its own (`testenv`), never thediff --git a/docs/subsystems/mcp-hub.md b/docs/subsystems/mcp-hub.mdnew file mode 100644index 000000000..4db225667--- /dev/null+++ b/docs/subsystems/mcp-hub.md@@ -0,0 +1,50 @@+# MCP hub++With the Library's switch "MCP hub" 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](library.md)'s; the endpoint is the gateway's, guarded as [Plugin tools](plugin-tools.md)' `/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 hub started (`hubStopAll`). The page posts `servers/hub` `{on}`. | `SetMCPHub` in [`mcp_hub.go`](../../internal/library/mcp_hub.go); `servers/hub` in [`gui/library.go`](../../internal/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 never put behind the hub. | `hubFor`, `hubServerFor`, `inHub` in `mcp_hub.go`; `syncMCP` in [`sync.go`](../../internal/library/sync.go); [`mcp_signin.go`](../../internal/library/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 hub's entry. An agent given none of the hub'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). `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`](../../internal/gateway/hub_mcp.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`](../../internal/gateway/search_mcp.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`](../../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 hub, 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…, 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`). First come the agents magpie looks after: every one `mcpAgents` lists (each detected, shown agent that takes MCP servers), each with where it stands (`.lib-hubagent[data-state]`): `on` (connected, and how many servers it gets through magpie), `own`, `taken` or `no-remote` (why it keeps each server's own entry; an agent that reaches no server over HTTP says so even with none ticked), or `none`, given none of the hub's servers, which has a Connect (`.lib-hubgo`) that posts `servers/agents-all` `{agents:[id], on:true}`, as By agent's Turn all on does, so `hubFor` gives it the entry. The sheet stays open and the row then says connected. An agent is never handed to the user as a command to run. Then any other app: the address, and a picker of Cursor (only when it isn't one of the agents), VS Code and Other (any JSON app), with each one's JSON copied with a click. In the app, Cursor and VS Code also get "Add to {app}": 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 such button. | `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` | `hubPart` 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) |++The CLI's `magpie library mcp hub on|off` calls `SetMCPHub`, and `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)).++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 hub 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.+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`.++## Constraints and failure behavior++- Off means off: no tool is listed, a call says the hub 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 hub 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 gateway key holder on the LAN reaches the Library's command servers through the hub while it is on, as they reach plugin tools. They run with the user's rights on this computer.+- 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++```sh+go test -tags nogui ./internal/gui -run TestHubOpen+go test -tags nogui ./internal/library -run 'TestHub|TestFakeMCPServer|TestCheck'+go test -tags nogui ./internal/gateway -run 'HubMCP|OwnMCPRejects' -timeout 45m+go test -tags nogui ./internal/cli -run TestLibraryMCPHub+node --test internal/gui/tests/library-mcp-hub.test.cjs+```++The library tests run a fake stdio MCP server (`TestFakeMCPServer` in [`mcp_check_test.go`](../../internal/library/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`.diff --git a/docs/subsystems/plugin-tools.md b/docs/subsystems/plugin-tools.mdindex 1e958fbfd..ec3ff8547 100644--- a/docs/subsystems/plugin-tools.md+++ b/docs/subsystems/plugin-tools.md@@ -32,7 +32,7 @@ A tool's answer goes back to the agent that called it, on this computer or on a- A plugin's tool runs in the plugin host with the user's rights, as its provider code does. Installing a plugin is trusting it.- One host serves every plugin. A tool that never answers is answered at `ToolTimeout`, and its call's slot in the host is left for its own abort to end; the host goes on serving (`TestPluginTools`).- Names are stable for a given set of plugins on. Turning on a plugin earlier in plugins.json that gives a tool of the same MCP name renumbers the later one.-- Wiring is the user's: the Plugins page copies the address, and magpie writes no agent's MCP config for it. There is no Library entry yet.+- Wiring is the user's: the Plugins page copies the address, and magpie writes no agent's MCP config for it. There is no Library entry yet. The Library's own servers have an endpoint of the same kind beside this one, `/mcp/magpie/hub/<agent>`, which magpie does write into agents ([MCP hub](mcp-hub.md)); plugin tools aren't in it.## Verificationdiff --git a/internal/cli/library_cli.go b/internal/cli/library_cli.goindex 24be504ec..2b6b74258 100644--- a/internal/cli/library_cli.go+++ b/internal/cli/library_cli.go@@ -22,6 +22,7 @@ const libraryUsage = `magpie library what the library givesmagpie library mcp add <name> <url | command args…> [agents=a,b…]magpie library mcp agents <name> <a,b…|none>magpie library mcp rm <name>+ magpie library mcp hub on|off give each agent one MCP server, magpie, in place of the library's (magpie runs them, once for every agent)magpie library skill agents <name> <a,b…|none>magpie library skill rm <name> (skills are installed from the app's Library page)magpie library skill rm --all [--yes] every skill out of the library and the agents (asks first; --yes doesn't)@@ -117,6 +118,8 @@ func libraryCmd(args []string) error {}case len(rest) == 2 && rest[0] == "rm":res, err = library.RemoveServer(rest[1])+ case len(rest) == 2 && rest[0] == "hub" && (rest[1] == "on" || rest[1] == "off"):+ res, err = library.SetMCPHub(rest[1] == "on")default:return fmt.Errorf("usage:\n %s", libraryUsage)}@@ -407,6 +410,31 @@ func libraryStatus() error {fmt.Println(" ", amber.Render("!"), a, muted.Render(e))}}+ if v.MCPHub {+ var given []string+ keep := map[string][]string{}+ for _, a := range v.Agents {+ switch a.Hub {+ case "":+ case "on":+ given = append(given, a.ID)+ default:+ keep[a.Hub] = append(keep[a.Hub], a.ID)+ }+ }+ fmt.Printf(" %s → %s\n", bold.Render("magpie")+muted.Render(" (one server in place of these, at "+v.HubPath+"<agent>)"), on(given))+ for _, why := range []string{"no-remote", "taken", "own"} {+ if len(keep[why]) == 0 {+ continue+ }+ say := map[string]string{+ "no-remote": "reaches no MCP server over HTTP, so it keeps each server's own entry",+ "taken": "a library server is called magpie, so each agent keeps each server's own entry",+ "own": "has an MCP server called magpie of its own, so it keeps each server's own entry",+ }[why]+ fmt.Println(" ", amber.Render("!"), strings.Join(keep[why], ", "), muted.Render(say))+ }+ }var found, own []stringfor _, f := range v.FoundServers {if f.Own {diff --git a/internal/cli/library_hub_test.go b/internal/cli/library_hub_test.gonew file mode 100644index 000000000..0b113aa42--- /dev/null+++ b/internal/cli/library_hub_test.go@@ -0,0 +1,69 @@+package cli++import (+ "encoding/json"+ "os"+ "path/filepath"+ "slices"+ "strings"+ "testing"+)++// magpie library mcp hub on|off (#2679): on, the agent is given magpie's+// one MCP server in place of the library's; off, each server's own again.+// Any other word is refused, and writes nothing.+func TestLibraryMCPHub(t *testing.T) {+ h := t.TempDir()+ for k, v := range map[string]string{"HOME": h, "USERPROFILE": h, "XDG_CONFIG_HOME": filepath.Join(h, ".config"), "CLAUDE_CONFIG_DIR": "", "CODEX_HOME": "", "APPDATA": "", "LOCALAPPDATA": ""} {+ t.Setenv(k, v)+ }+ if err := os.MkdirAll(filepath.Join(h, ".claude"), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(h, ".claude/settings.json"), []byte("{}"), 0o644); err != nil {+ t.Fatal(err)+ }+ names := func() []string {+ t.Helper()+ b, err := os.ReadFile(filepath.Join(h, ".claude.json"))+ if err != nil {+ t.Fatal(err)+ }+ var c struct {+ MCPServers map[string]map[string]any `json:"mcpServers"`+ }+ if err := json.Unmarshal(b, &c); err != nil {+ t.Fatal(err)+ }+ var out []string+ for k := range c.MCPServers {+ out = append(out, k)+ }+ slices.Sort(out)+ return out+ }+ if err := libraryCmd([]string{"library", "mcp", "add", "fs", "npx", "-y", "fs", "agents=claude"}); err != nil {+ t.Fatal(err)+ }+ if got := names(); !slices.Equal(got, []string{"fs"}) {+ t.Fatalf("before: %v", got)+ }+ if err := libraryCmd([]string{"library", "mcp", "hub", "on"}); err != nil {+ t.Fatal(err)+ }+ if got := names(); !slices.Equal(got, []string{"magpie"}) {+ t.Fatalf("hub on: %v", got)+ }+ if err := libraryCmd([]string{"library", "mcp", "hub", "yes"}); err == nil || !strings.Contains(err.Error(), "usage") {+ t.Fatalf("hub yes: %v", err)+ }+ if got := names(); !slices.Equal(got, []string{"magpie"}) {+ t.Fatalf("after a word it doesn't know: %v", got)+ }+ if err := libraryCmd([]string{"library", "mcp", "hub", "off"}); err != nil {+ t.Fatal(err)+ }+ if got := names(); !slices.Equal(got, []string{"fs"}) {+ t.Fatalf("hub off: %v", got)+ }+}diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex 1e6be71f6..2a35e7a23 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -631,6 +631,8 @@ func (s *Server) ListenAndServe(ctx context.Context) error {// loops' next look (#3185)go filememo.Watch(freshCtx, pluginrt.Path(), settings.Path(), provider.Path(), plugin.ListPath(), plugin.ToolsPath(), access.Path())go pluginrt.KeepFresh(freshCtx, filememo.OthersEvery, settings.Path(), provider.Path(), plugin.ListPath(), plugin.ToolsPath(), access.Path())+ // and the MCP hub's servers that the Library no longer gives are ended+ go keepHubPruned(freshCtx)srv := &http.Server{Handler: s.closesWhenHandedOff(s.Front()), ReadHeaderTimeout: 30 * time.Second, IdleTimeout: 5 * time.Minute, ConnState: s.unread.note}// the magpie serving the gateway, and only it, keeps the saved accounts// signed in, so two never refresh one sign-in at once@@ -705,6 +707,8 @@ func (s *Server) ListenAndServe(ctx context.Context) error {s.subscription.abortAll()provider.FlushServed() // what answered last, kept for the next start and the CLIflushSticks() // who answered which conversation, for the next start or the magpie handed over to+ // the MCP servers the hub started end with the gateway+ StopHub()}()for {err := srv.Serve(ln)@@ -815,6 +819,8 @@ func (s *Server) Handler() http.Handler {mux.HandleFunc(ImageMCPPath, s.imageMCP)mux.HandleFunc(ImageMCPFiles+"{name}", s.imageMCPFile)mux.HandleFunc(ToolsMCPPath, s.toolsMCP)+ mux.HandleFunc(HubMCPPath+"{agent}", s.hubMCP)+ mux.HandleFunc(HubMCPAll, s.hubMCP)mux.HandleFunc(CodexPath+"/", s.codexBackend)mux.HandleFunc("GET "+CodexCatalogPath, s.codexCatalog)mux.HandleFunc("GET /v1beta/models", s.geminiModels)@@ -851,7 +857,7 @@ var catalogSize stamped[struct{}, int]func (s *Server) info(w http.ResponseWriter, r *http.Request) {out := map[string]any{"name": "magpie", "version": Version, "models": catalogSize.get(struct{}{}, func() int { return len(provider.Catalog()) }), "window": Window,- "apis": []string{"/v1/chat/completions", "/v1/responses", "/v1/messages", "/v1/systemone", "/v1beta/models/{model}:generateContent", "/v1/images/generations", "/v1/images/edits", "/v1/videos", "/v1/embeddings", "/v1/rerank", "/v1/audio/transcriptions", "/v1/audio/translations", "/v1/magpie/quotas", "/v1/magpie/quotas/history", "/v1/magpie/usage", "/v1/magpie/usage/requests", "/v1/magpie/route", "/v1/magpie/trace", SearchMCPPath, VisionMCPPath, ImageMCPPath, ToolsMCPPath}}+ "apis": []string{"/v1/chat/completions", "/v1/responses", "/v1/messages", "/v1/systemone", "/v1beta/models/{model}:generateContent", "/v1/images/generations", "/v1/images/edits", "/v1/videos", "/v1/embeddings", "/v1/rerank", "/v1/audio/transcriptions", "/v1/audio/translations", "/v1/magpie/quotas", "/v1/magpie/quotas/history", "/v1/magpie/usage", "/v1/magpie/usage/requests", "/v1/magpie/route", "/v1/magpie/trace", SearchMCPPath, VisionMCPPath, ImageMCPPath, ToolsMCPPath, HubMCPAll, HubMCPPath + "{agent}"}}// which process serves, for another magpie on this computer to name it// on its Routing page (#2606); not told to another machineif local(r) {diff --git a/internal/gateway/hub_mcp.go b/internal/gateway/hub_mcp.gonew file mode 100644index 000000000..b72b7e576--- /dev/null+++ b/internal/gateway/hub_mcp.go@@ -0,0 +1,230 @@+package gateway++import (+ "context"+ "encoding/json"+ "net/http"+ "strconv"+ "time"++ "github.com/yetone/magpie/internal/access"+)++// The MCP hub (#2679): with the Library's switch on, magpie is the one MCP+// server an agent is given, at HubMCPPath<agent>, and it hands each call+// on to the Library server behind it, as Composio does. Its tools are+// those of the Library servers ticked for that agent, each named+// <server>__<tool>; the servers themselves (a command started once for+// every agent, a URL reached with magpie's sign-in) are the library's to+// run (library/mcp_hub.go), which sets MCPHub. It is guarded as+// magpie's other MCP servers are (serveMCP), and each call is held to the+// calling gateway key's limit (reserveFor). Tools only, for now: no+// resources or prompts.++// HubMCPPath is where the hub is served, the agent's id after it.+const HubMCPPath = "/mcp/magpie/hub/"++// HubMCPAll is the hub for any other app (Cursor, VS Code, a script): one+// address with every server in the Library, ticked for an agent or not.+// It is the agent "" to Hub.+const HubMCPAll = "/mcp/magpie/hub"++// HubTool is one tool a Library server lists, as the hub gives it.+type HubTool struct {+ Server string // the Library server's name+ Tool string // the tool's name on that server+ // Spec is the tool as the server listed it: its description,+ // inputSchema and the rest go to the agent as they are, its name+ // replaced+ Spec map[string]json.RawMessage+}++// Hub is the servers behind the hub: the library's.+type Hub interface {+ // On is whether the Library's switch is on: off, the hub lists no+ // tool and runs none+ On() bool+ // Tools are the tools of the servers ticked for agent (every server+ // in the Library for agent "", HubMCPAll's), but those turned off; a+ // server that couldn't be reached by ctx's end is left out+ Tools(ctx context.Context, agent string) []HubTool+ // Call runs one tool of server for agent, and is its result as the+ // server gave it; an error is why it couldn't be run+ Call(ctx context.Context, agent, server, tool string, args json.RawMessage) (json.RawMessage, error)+ // Stop ends every server the hub started+ Stop()+ // Prune ends each server the hub started that the Library no longer+ // gives: the switch turned off, the server removed or edited, here or+ // by another magpie (the window, the CLI)+ Prune()+}++// MCPHub is set by the library; nil, the hub has no server.+var MCPHub Hub++// The hub's budgets: tools/list waits for the servers this long (a server+// still starting is left out, and listed once it has), and a call this+// long, under the 60s an MCP client waits for an answer, for the agent to+// be told which server didn't answer rather than its own timeout.+var (+ hubListWait = 40 * time.Second+ hubCallWait = 50 * time.Second+)++const hubBody = 4 << 20++const errHubOff = "magpie's MCP hub is off, so it runs no server's tools: turn it on in magpie's Library → MCP"++func (s *Server) hubMCP(w http.ResponseWriter, r *http.Request) {+ agent := r.PathValue("agent")+ serveMCP(w, r, "magpie's MCP hub", hubBody, func(r *http.Request, m mcpMessage) (any, map[string]any) {+ return s.hubMCPCall(r, agent, m)+ })+}++// hubTool is one tool as the hub names it.+type hubTool struct {+ HubTool+ name string+}++// hubSep is between a server's name and its tool's in the hub's names.+const hubSep = "__"++// hubNamed names ts <server>__<tool>, in the characters MCP clients take+// and 64 of them at most, a name two would share told apart by a number.+func hubNamed(ts []HubTool) []hubTool {+ out := make([]hubTool, 0, len(ts))+ seen := map[string]bool{}+ for _, t := range ts {+ base := unsafeToolChars.ReplaceAllString(t.Server, "-") + hubSep + unsafeToolChars.ReplaceAllString(t.Tool, "-")+ name := cutName(base, 64)+ for i := 2; seen[name]; i++ {+ suf := "_" + strconv.Itoa(i)+ name = cutName(base, 64-len(suf)) + suf+ }+ seen[name] = true+ out = append(out, hubTool{HubTool: t, name: name})+ }+ return out+}++func hubTools(ctx context.Context, agent string) []hubTool {+ if MCPHub == nil || !MCPHub.On() {+ return nil+ }+ ctx, cancel := context.WithTimeout(ctx, hubListWait)+ defer cancel()+ return hubNamed(MCPHub.Tools(ctx, agent))+}++func (s *Server) hubMCPCall(r *http.Request, agent string, m mcpMessage) (any, map[string]any) {+ switch m.Method {+ case "initialize":+ say := "The tools of the MCP servers in magpie's Library that are switched on for this agent, each named <server>__<tool>. Add servers, or switch them on for an agent, in magpie's Library → MCP."+ if agent == "" {+ say = "The tools of every MCP server in magpie's Library, each named <server>__<tool>. Add servers in magpie's Library → MCP."+ }+ if MCPHub == nil || !MCPHub.On() {+ say = errHubOff+ }+ return map[string]any{+ "protocolVersion": mcpVersion(m),+ "capabilities": map[string]any{"tools": map[string]any{}},+ "serverInfo": map[string]any{"name": "magpie", "title": "Magpie MCP hub", "version": Version},+ "instructions": say,+ }, nil+ case "ping":+ return map[string]any{}, nil+ case "tools/list":+ ts := hubTools(r.Context(), agent)+ list := make([]any, 0, len(ts))+ for _, t := range ts {+ spec := make(map[string]json.RawMessage, len(t.Spec)+1)+ for k, v := range t.Spec {+ spec[k] = v+ }+ name, _ := json.Marshal(t.name)+ spec["name"] = name+ if s, ok := spec["inputSchema"]; !ok || !json.Valid(s) {+ spec["inputSchema"] = json.RawMessage(`{"type":"object","properties":{}}`)+ }+ list = append(list, spec)+ }+ return map[string]any{"tools": list}, nil+ case "tools/call":+ var p struct {+ Name string `json:"name"`+ Arguments json.RawMessage `json:"arguments"`+ }+ if err := json.Unmarshal(m.Params, &p); err != nil {+ return nil, map[string]any{"code": -32602, "message": "invalid params: " + err.Error()}+ }+ if MCPHub == nil || !MCPHub.On() {+ return toolResult(errHubOff, true), nil+ }+ for _, t := range hubTools(r.Context(), agent) {+ if t.name == p.Name {+ return s.hubMCPRun(r, agent, t, p.Arguments), nil+ }+ }+ if agent == "" {+ return nil, map[string]any{"code": -32602, "message": "unknown tool " + p.Name + ": no MCP server in magpie's Library gives it"}+ }+ return nil, map[string]any{"code": -32602, "message": "unknown tool " + p.Name + ": no MCP server switched on for " + agent + " in magpie's Library gives it"}+ }+ return nil, map[string]any{"code": -32601, "message": "method not found: " + m.Method}+}++// hubMCPRun runs one tool on its server, held to the calling gateway key's+// limit. A server that is down or doesn't answer in time is an error+// result naming it, never a wait past the client's.+func (s *Server) hubMCPRun(r *http.Request, agent string, t hubTool, args json.RawMessage) any {+ release, err := reserveFor(access.Caller(r.Context()), int64(len(args)), "", limitClock())+ if err != nil {+ return toolResult(err.Error(), true)+ }+ defer release()+ if len(args) == 0 || string(args) == "null" {+ args = json.RawMessage(`{}`)+ }+ ctx, cancel := context.WithTimeout(r.Context(), hubCallWait)+ defer cancel()+ res, err := MCPHub.Call(ctx, agent, t.Server, t.Tool, args)+ if err != nil {+ why := err.Error()+ if ctx.Err() != nil && r.Context().Err() == nil {+ why = "it didn't answer in " + hubCallWait.String()+ }+ return toolResult("The MCP server "+t.Server+" couldn't run "+t.Tool+": "+why, true)+ }+ return res+}++// hubPruneEvery is how often the gateway ends the servers the hub no+// longer gives. An agent given the servers' own entries again never asks+// the hub, so it is looked for, not waited for.+var hubPruneEvery = 5 * time.Second++// keepHubPruned prunes the hub's servers while ctx lasts.+func keepHubPruned(ctx context.Context) {+ tick := time.NewTicker(hubPruneEvery)+ defer tick.Stop()+ for {+ select {+ case <-ctx.Done():+ return+ case <-tick.C:+ if MCPHub != nil {+ MCPHub.Prune()+ }+ }+ }+}++// StopHub ends the servers the hub started: magpie is stopping.+func StopHub() {+ if MCPHub != nil {+ MCPHub.Stop()+ }+}diff --git a/internal/gateway/hub_mcp_test.go b/internal/gateway/hub_mcp_test.gonew file mode 100644index 000000000..a68e18218--- /dev/null+++ b/internal/gateway/hub_mcp_test.go@@ -0,0 +1,347 @@+package gateway++import (+ "context"+ "encoding/json"+ "errors"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync"+ "testing"+ "time"++ "github.com/yetone/magpie/internal/access"+)++// fakeHub is the library's servers, as the gateway asks for them.+type fakeHub struct {+ mu sync.Mutex+ on bool+ tools map[string][]HubTool // by agent+ calls []string+ call func(ctx context.Context, server, tool string) (json.RawMessage, error)+}++func (f *fakeHub) On() bool { f.mu.Lock(); defer f.mu.Unlock(); return f.on }++func (f *fakeHub) Tools(_ context.Context, agent string) []HubTool {+ f.mu.Lock()+ defer f.mu.Unlock()+ return f.tools[agent]+}++func (f *fakeHub) Call(ctx context.Context, agent, server, tool string, args json.RawMessage) (json.RawMessage, error) {+ f.mu.Lock()+ f.calls = append(f.calls, agent+" "+server+" "+tool+" "+string(args))+ call := f.call+ f.mu.Unlock()+ if call != nil {+ return call(ctx, server, tool)+ }+ return json.RawMessage(`{"content":[{"type":"text","text":"ran ` + server + ` ` + tool + `"}]}`), nil+}++func (f *fakeHub) Stop() {}+func (f *fakeHub) Prune() {}++func (f *fakeHub) ran() []string {+ f.mu.Lock()+ defer f.mu.Unlock()+ return append([]string(nil), f.calls...)+}++func hubSpec(name string) map[string]json.RawMessage {+ return map[string]json.RawMessage{+ "name": json.RawMessage(`"` + name + `"`),+ "description": json.RawMessage(`"Does ` + name + `."`),+ "inputSchema": json.RawMessage(`{"type":"object","properties":{"q":{"type":"string"}},"required":["q"]}`),+ }+}++func withHub(t *testing.T, f *fakeHub) {+ t.Helper()+ old := MCPHub+ MCPHub = f+ t.Cleanup(func() { MCPHub = old })+}++func hubPost(h http.Handler, agent, addr, body string, hdr ...string) *httptest.ResponseRecorder {+ return hubPostAt(h, HubMCPPath+agent, addr, body, hdr...)+}++func hubPostAt(h http.Handler, path, addr, body string, hdr ...string) *httptest.ResponseRecorder {+ r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))+ r.RemoteAddr, r.Host = addr, "127.0.0.1:3425"+ r.Header.Set("Content-Type", "application/json")+ r.Header.Set("Accept", "application/json, text/event-stream")+ for i := 0; i+1 < len(hdr); i += 2 {+ r.Header.Set(hdr[i], hdr[i+1])+ }+ w := httptest.NewRecorder()+ h.ServeHTTP(w, r)+ return w+}++const here = "127.0.0.1:50000"++// The hub lists the tools of the servers ticked for the agent in its path,+// each <server>__<tool> in the characters a client takes, 64 at most, two+// of one name told apart, and runs each call on the server it is of.+func TestHubMCPListsAndRoutes(t *testing.T) {+ fresh(t)+ long := strings.Repeat("x", 70)+ f := &fakeHub{on: true, tools: map[string][]HubTool{+ "claude": {+ {Server: "github", Tool: "create_issue", Spec: hubSpec("create_issue")},+ {Server: "my server", Tool: "do.it", Spec: hubSpec("do.it")},+ {Server: "my.server", Tool: "do it", Spec: hubSpec("do it")},+ {Server: "fs", Tool: long, Spec: map[string]json.RawMessage{"name": json.RawMessage(`"` + long + `"`)}},+ },+ "codex": {{Server: "fs", Tool: "read", Spec: hubSpec("read")}},+ }}+ withHub(t, f)+ h := lanGuard(New().Handler())++ init := readMCP(t, hubPost(h, "claude", here, `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`))+ if init.Error != nil || init.Result.ServerInfo.Name != "magpie" || init.Result.ProtocolVersion != "2025-06-18" {+ t.Fatalf("initialize: %+v", init)+ }+ m := readMCP(t, hubPost(h, "claude", here, toolsList))+ var names []string+ for _, x := range m.Result.Tools {+ names = append(names, x.Name)+ if len(x.Name) > 64 {+ t.Errorf("%q is past 64 characters", x.Name)+ }+ }+ want := []string{"github__create_issue", "my-server__do-it", "my-server__do-it_2", "fs__" + strings.Repeat("x", 60)}+ if strings.Join(names, " ") != strings.Join(want, " ") {+ t.Fatalf("tools = %v, want %v", names, want)+ }+ if r := m.Result.Tools[0].InputSchema.Required; len(r) != 1 || r[0] != "q" {+ t.Errorf("schema not passed on: %v", r)+ }+ // a tool listed with no schema is given an empty one+ w := hubPost(h, "claude", here, toolsList)+ if !strings.Contains(w.Body.String(), `"inputSchema":{"type":"object","properties":{}}`) {+ t.Errorf("no default schema: %s", w.Body)+ }++ m = readMCP(t, hubPost(h, "claude", here, toolsCall("my-server__do-it_2", `{"q":"x"}`)))+ if m.Result.IsError || m.Result.Content[0].Text != "ran my.server do it" {+ t.Fatalf("call: %+v", m)+ }+ m = readMCP(t, hubPost(h, "codex", here, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"fs__read"}}`))+ if m.Result.IsError || m.Result.Content[0].Text != "ran fs read" {+ t.Fatalf("codex's call: %+v", m)+ }+ // a tool the agent isn't given is unknown, even another agent's+ for _, c := range []struct{ agent, tool string }{{"codex", "github__create_issue"}, {"claude", "nope__x"}, {"gemini", "fs__read"}} {+ m := readMCP(t, hubPost(h, c.agent, here, toolsCall(c.tool, `{}`)))+ if m.Error == nil || m.Error.Code != -32602 || !strings.Contains(m.Error.Message, "unknown tool "+c.tool) {+ t.Errorf("%s's %s: %+v", c.agent, c.tool, m)+ }+ }+ if got := f.ran(); strings.Join(got, "|") != `claude my.server do it {"q":"x"}|codex fs read {}` {+ t.Errorf("ran %q", got)+ }+ if m := readMCP(t, hubPost(h, "claude", here, `{"jsonrpc":"2.0","id":4,"method":"resources/list"}`)); m.Error == nil || m.Error.Code != -32601 {+ t.Errorf("resources/list: %+v", m)+ }+}++// HubMCPAll, the one address any other app is given, is the hub for agent+// "": every server in the Library, with its own instructions and its own+// word for a tool it doesn't have; an agent's path still lists only its own.+func TestHubMCPForAnyApp(t *testing.T) {+ fresh(t)+ t.Setenv("MAGPIE_ADDR", "")+ t.Setenv("MAGPIE_PUBLIC_URL", "")+ f := &fakeHub{on: true, tools: map[string][]HubTool{+ "": {{Server: "github", Tool: "create_issue", Spec: hubSpec("create_issue")}, {Server: "notes", Tool: "find", Spec: hubSpec("find")}},+ "claude": {{Server: "github", Tool: "create_issue", Spec: hubSpec("create_issue")}},+ }}+ withHub(t, f)+ h := lanGuard(New().Handler())++ w := hubPostAt(h, HubMCPAll, here, `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`)+ if init := readMCP(t, w); init.Error != nil || init.Result.ServerInfo.Name != "magpie" {+ t.Fatalf("initialize: %+v", init)+ }+ if !strings.Contains(w.Body.String(), "The tools of every MCP server in magpie's Library") {+ t.Errorf("another app's instructions: %s", w.Body)+ }+ var names []string+ for _, x := range readMCP(t, hubPostAt(h, HubMCPAll, here, toolsList)).Result.Tools {+ names = append(names, x.Name)+ }+ if strings.Join(names, " ") != "github__create_issue notes__find" {+ t.Fatalf("another app's tools %v", names)+ }+ m := readMCP(t, hubPostAt(h, HubMCPAll, here, toolsCall("notes__find", `{"q":"x"}`)))+ if m.Result.IsError || m.Result.Content[0].Text != "ran notes find" {+ t.Fatalf("another app's call: %+v", m)+ }+ m = readMCP(t, hubPostAt(h, HubMCPAll, here, toolsCall("nope__x", `{}`)))+ if m.Error == nil || m.Error.Code != -32602 || m.Error.Message != "unknown tool nope__x: no MCP server in magpie's Library gives it" {+ t.Errorf("another app's unknown tool: %+v", m)+ }+ // claude's own path doesn't reach a server it isn't given+ m = readMCP(t, hubPost(h, "claude", here, toolsCall("notes__find", `{}`)))+ if m.Error == nil || !strings.Contains(m.Error.Message, "no MCP server switched on for claude") {+ t.Errorf("claude reached notes: %+v", m)+ }+ if got := f.ran(); strings.Join(got, "|") != ` notes find {"q":"x"}` {+ t.Errorf("ran %q", got)+ }+ // guarded as the agents' paths are: no web page, and no other computer+ // while magpie isn't shared+ if w := hubPostAt(h, HubMCPAll, here, toolsList, "Origin", "https://evil.example", "Sec-Fetch-Site", "cross-site", "Sec-Fetch-Mode", "cors"); w.Code != http.StatusForbidden {+ t.Errorf("a foreign page's tools/list: %d %s", w.Code, w.Body)+ }+ if w := hubPostAt(h, HubMCPAll, "192.0.2.7:4000", toolsCall("notes__find", `{}`)); w.Code != http.StatusForbidden {+ t.Errorf("another computer, not shared: %d %s", w.Code, w.Body)+ }+ if got := f.ran(); len(got) != 1 {+ t.Errorf("ran for a request turned away: %q", got)+ }+}++// A server that fails, or doesn't answer in the hub's time, is an error+// result naming it, never a wait past the client's.+func TestHubMCPServerDown(t *testing.T) {+ fresh(t)+ f := &fakeHub{on: true, tools: map[string][]HubTool{"claude": {+ {Server: "db", Tool: "query", Spec: hubSpec("query")},+ {Server: "slow", Tool: "wait", Spec: hubSpec("wait")},+ }}}+ f.call = func(ctx context.Context, server, tool string) (json.RawMessage, error) {+ if server == "db" {+ return nil, errors.New("it exited (5): Error: lost the database")+ }+ <-ctx.Done()+ return nil, errors.New("it didn't answer in time")+ }+ withHub(t, f)+ wait := hubCallWait+ hubCallWait = 200 * time.Millisecond+ t.Cleanup(func() { hubCallWait = wait })+ h := lanGuard(New().Handler())+ m := readMCP(t, hubPost(h, "claude", here, toolsCall("db__query", `{}`)))+ if !m.Result.IsError || m.Result.Content[0].Text != "The MCP server db couldn't run query: it exited (5): Error: lost the database" {+ t.Errorf("a server down: %+v", m)+ }+ start := time.Now()+ m = readMCP(t, hubPost(h, "claude", here, toolsCall("slow__wait", `{}`)))+ if !m.Result.IsError || m.Result.Content[0].Text != "The MCP server slow couldn't run wait: it didn't answer in 200ms" {+ t.Errorf("a server that doesn't answer: %+v", m)+ }+ if d := time.Since(start); d > 30*time.Second {+ t.Errorf("waited %v", d)+ }+}++// Off, the hub lists no tool and runs none, and says how to turn it on.+func TestHubMCPOff(t *testing.T) {+ fresh(t)+ f := &fakeHub{tools: map[string][]HubTool{"claude": {{Server: "fs", Tool: "read", Spec: hubSpec("read")}}}}+ withHub(t, f)+ h := lanGuard(New().Handler())+ if m := readMCP(t, hubPost(h, "claude", here, toolsList)); len(m.Result.Tools) != 0 || m.Error != nil {+ t.Errorf("off, listed %+v", m)+ }+ m := readMCP(t, hubPost(h, "claude", here, toolsCall("fs__read", `{}`)))+ if !m.Result.IsError || !strings.Contains(m.Result.Content[0].Text, "MCP hub is off") {+ t.Errorf("off, a call said %+v", m)+ }+ if len(f.ran()) != 0 {+ t.Errorf("off, ran %q", f.ran())+ }+ MCPHub = nil+ if m := readMCP(t, hubPost(h, "claude", here, toolsList)); len(m.Result.Tools) != 0 || m.Error != nil {+ t.Errorf("no hub, listed %+v", m)+ }+}++// The hub is guarded as magpie's other MCP servers are: another computer+// only while shared and with a key, and no web page — a sandboxed frame's+// null Origin, another site's, one on this computer, one rebound to+// 127.0.0.1 — even from this computer. No tool runs for one turned away.+func TestHubMCPGuarded(t *testing.T) {+ fresh(t)+ t.Setenv("MAGPIE_ADDR", "")+ t.Setenv("MAGPIE_PUBLIC_URL", "")+ f := &fakeHub{on: true, tools: map[string][]HubTool{"claude": {{Server: "fs", Tool: "read", Spec: hubSpec("read")}}}}+ withHub(t, f)+ h := lanGuard(New().Handler())+ call := toolsCall("fs__read", `{"q":"x"}`)+ const there = "192.0.2.7:4000"+ if w := hubPost(h, "claude", there, call); w.Code != http.StatusForbidden {+ t.Fatalf("not shared: %d %s", w.Code, w.Body)+ }+ _, secrets := newCaller(t, "Laptop") // shares magpie+ if w := hubPost(h, "claude", there, call); w.Code != http.StatusUnauthorized {+ t.Fatalf("shared, no key: %d %s", w.Code, w.Body)+ }+ if w := hubPost(h, "claude", there, call, "Authorization", "Bearer sk-magpie-wrong"); w.Code != http.StatusUnauthorized {+ t.Fatalf("shared, a wrong key: %d %s", w.Code, w.Body)+ }+ for _, hdr := range [][]string{+ {"Origin", "null", "Sec-Fetch-Mode", "cors"},+ {"Origin", "https://evil.example", "Sec-Fetch-Site", "cross-site", "Sec-Fetch-Mode", "cors"},+ {"Origin", "http://localhost:5173", "Sec-Fetch-Site", "same-site", "Sec-Fetch-Mode", "cors"},+ } {+ if w := hubPost(h, "claude", here, call, hdr...); w.Code != http.StatusForbidden {+ t.Errorf("a page %v: %d %s", hdr, w.Code, w.Body)+ }+ }+ r := httptest.NewRequest(http.MethodPost, HubMCPPath+"claude", strings.NewReader(call))+ r.RemoteAddr, r.Host = here, "rebind.attacker.example:3425"+ r.Header.Set("Origin", "http://rebind.attacker.example:3425")+ r.Header.Set("Sec-Fetch-Site", "same-origin")+ rec := httptest.NewRecorder()+ h.ServeHTTP(rec, r)+ if rec.Code != http.StatusForbidden {+ t.Errorf("a rebound page: %d %s", rec.Code, rec.Body)+ }+ if w := httptest.NewRecorder(); true {+ h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, HubMCPPath+"claude", nil))+ if w.Code == http.StatusOK {+ t.Errorf("a GET: %d", w.Code)+ }+ }+ if got := f.ran(); len(got) != 0 {+ t.Fatalf("ran for a request turned away: %q", got)+ }+ m := readMCP(t, hubPost(h, "claude", there, call, "Authorization", "Bearer "+secrets[0]))+ if m.Result.IsError || m.Result.Content[0].Text != "ran fs read" {+ t.Fatalf("shared, with its key: %+v", m)+ }+ m = readMCP(t, hubPost(h, "claude", here, call))+ if m.Result.IsError || len(f.ran()) != 2 {+ t.Fatalf("this computer: %+v, ran %q", m, f.ran())+ }+}++// A gateway key's limit holds each call: a key held to a limit nothing+// serves is refused, and the server isn't asked.+func TestHubMCPHeldToKeyLimit(t *testing.T) {+ fresh(t)+ f := &fakeHub{on: true, tools: map[string][]HubTool{"claude": {{Server: "fs", Tool: "read", Spec: hubSpec("read")}}}}+ withHub(t, f)+ withoutLimits(t)+ keys, _ := newCaller(t, "Capped")+ setLimit(t, keys[0].ID, &access.Limit{Period: "day", Tokens: 700})+ capped := access.Identity{KeyID: keys[0].ID, KeyName: "Capped", Limit: &access.Limit{Period: "day", Tokens: 700}}+ s := New()+ r := httptest.NewRequest("POST", HubMCPPath+"claude", nil).WithContext(access.WithIdentity(t.Context(), capped))+ res, rpcErr := s.hubMCPCall(r, "claude", mcpMessage{ID: json.RawMessage("1"), Method: "tools/call", Params: json.RawMessage(`{"name":"fs__read","arguments":{}}`)})+ b, _ := json.Marshal(res)+ if rpcErr != nil || !strings.Contains(string(b), `"isError":true`) || !strings.Contains(string(b), "Key limits plugin") {+ t.Errorf("a limited key with limits off: %s %v", b, rpcErr)+ }+ if len(f.ran()) != 0 {+ t.Errorf("ran %q", f.ran())+ }+}diff --git a/internal/gateway/oneport_test.go b/internal/gateway/oneport_test.goindex 82fbd0540..fbd246244 100644--- a/internal/gateway/oneport_test.go+++ b/internal/gateway/oneport_test.go@@ -31,7 +31,7 @@ func TestClientPathCoversHandler(t *testing.T) {"AlphaSearchPath": AlphaSearchPath, "HeapPath": HeapPath, "CPUPath": CPUPath,"GoroutinesPath": GoroutinesPath, "ClaudeRunsPath": ClaudeRunsPath, "SearchMCPPath": SearchMCPPath, "VisionMCPPath": VisionMCPPath,"ImageMCPPath": ImageMCPPath, "ImageMCPFiles": ImageMCPFiles,- "ToolsMCPPath": ToolsMCPPath, "provider.RemoteCardsPath": provider.RemoteCardsPath,+ "ToolsMCPPath": ToolsMCPPath, "HubMCPPath": HubMCPPath, "HubMCPAll": HubMCPAll, "provider.RemoteCardsPath": provider.RemoteCardsPath,"provider.RemoteRefreshPath": provider.RemoteRefreshPath,}re := regexp.MustCompile(`mux\.HandleFunc\(((?:"[^"]*"|[A-Za-z.]+)(?:\s*\+\s*(?:"[^"]*"|[A-Za-z.]+))*),`)@@ -51,7 +51,7 @@ func TestClientPathCoversHandler(t *testing.T) {if i := strings.IndexByte(pat, ' '); i >= 0 {pat = pat[i+1:]}- p := strings.NewReplacer("{$}", "", "{id...}", "x", "{call...}", "x", "{id}", "x", "{token}", "x", "{name}", "x").Replace(pat)+ p := strings.NewReplacer("{$}", "", "{id...}", "x", "{call...}", "x", "{id}", "x", "{token}", "x", "{name}", "x", "{agent}", "x").Replace(pat)n++stays := p == "/" || strings.HasPrefix(p, "/api/") || strings.HasPrefix(p, "/_magpie/")if ClientPath(p) == stays {diff --git a/internal/gateway/search_mcp_bounds_test.go b/internal/gateway/search_mcp_bounds_test.goindex 0d28d32ac..30bc4a174 100644--- a/internal/gateway/search_mcp_bounds_test.go+++ b/internal/gateway/search_mcp_bounds_test.go@@ -23,6 +23,7 @@ func TestOwnMCPRejectsOversizedBody(t *testing.T) {{"vision", VisionMCPPath, visionMCPBody},{"search", SearchMCPPath, 1 << 20},{"tools", ToolsMCPPath, toolsMCPBody},+ {"hub", HubMCPPath + "claude", hubBody},} {t.Run(tc.name, func(t *testing.T) {s, upstream := eyed(t)diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.jsindex 03ec5d07a..d69f516e5 100644--- a/internal/gui/assets/i18n.js+++ b/internal/gui/assets/i18n.js@@ -4,6 +4,8 @@// works. {name} slots are filled by t(str, vars).const I18N = {zh: {+ "MCP hub": "MCP 聚合",+ "in {agents}": "用于 {agents}","Browser access": "浏览器访问","Open magpie in a browser": "在浏览器中打开 magpie","The app serves this window’s page with a key in its link, as magpie web does, while it runs. For a tunnel such as Cloudflare Tunnel, point it at http://127.0.0.1:{port}": "App 运行时会把本窗口的页面提供给浏览器,链接里带着 Key,和 magpie web 一样。用 Cloudflare Tunnel 这类隧道时,把它指向 http://127.0.0.1:{port}",@@ -4057,6 +4059,8 @@ const I18N = {"Where": "位置",},"zh-TW": {+ "MCP hub": "MCP 聚合",+ "in {agents}": "用於 {agents}","Browser access": "瀏覽器存取","Open magpie in a browser": "在瀏覽器中開啟 magpie","The app serves this window’s page with a key in its link, as magpie web does, while it runs. For a tunnel such as Cloudflare Tunnel, point it at http://127.0.0.1:{port}": "App 執行時會把本視窗的頁面提供給瀏覽器,連結裡帶著 Key,和 magpie web 一樣。用 Cloudflare Tunnel 這類通道時,把它指向 http://127.0.0.1:{port}",@@ -8095,6 +8099,8 @@ const I18N = {"Where": "位置",},ja: {+ "MCP hub": "MCP ハブ",+ "in {agents}": "{agents} で使用","Browser access": "ブラウザからのアクセス","Open magpie in a browser": "ブラウザで magpie を開く","The app serves this window’s page with a key in its link, as magpie web does, while it runs. For a tunnel such as Cloudflare Tunnel, point it at http://127.0.0.1:{port}": "アプリの実行中、このウィンドウのページをブラウザにも提供します。magpie web と同じく、リンクにキーが含まれます。Cloudflare Tunnel などのトンネルは http://127.0.0.1:{port} に向けてください",@@ -12135,6 +12141,8 @@ const I18N = {"Where": "場所",},de: {+ "MCP hub": "MCP-Hub",+ "in {agents}": "in {agents}","Browser access": "Browserzugriff","Open magpie in a browser": "magpie im Browser öffnen","The app serves this window’s page with a key in its link, as magpie web does, while it runs. For a tunnel such as Cloudflare Tunnel, point it at http://127.0.0.1:{port}": "Solange die App läuft, liefert sie die Seite dieses Fensters auch an Browser aus, mit einem Schlüssel im Link, wie magpie web. Einen Tunnel wie Cloudflare Tunnel richtest du auf http://127.0.0.1:{port}",diff --git a/internal/gui/assets/plugins/library/client.js b/internal/gui/assets/plugins/library/client.jsindex 21cce8d6b..5467c642d 100644--- a/internal/gui/assets/plugins/library/client.js+++ b/internal/gui/assets/plugins/library/client.js@@ -50,6 +50,8 @@ magpie.plugin("library", (ctx) => {subagent: "M6 3a2.2 2.2 0 1 0 0 4.4A2.2 2.2 0 0 0 6 3z M2 13c.5-2.2 2-3.4 4-3.4s3.5 1.2 4 3.4 M11.5 7.5a1.5 1.5 0 1 0 0 .01 M10.2 12.5c.3-1.3 1.1-2 2.3-2s1.6.5 1.9 1.4",// a clock with an arrow going back round it, as macOS draws backupsbackup: "M3.2 8a4.8 4.8 0 1 0 1.4-3.4 M2.8 2.6v2.4h2.4 M8 5.3V8l1.9 1.2",+ // one point that four lines meet at: the MCP hub (#2679)+ hub: "M8 6.2a1.8 1.8 0 1 0 0 3.6 1.8 1.8 0 0 0 0-3.6z M8 2.5v3.7 M8 9.8v3.7 M2.5 8h3.7 M9.8 8h3.7",};const tilde = (p) => (lib?.home && p?.startsWith(lib.home) ? "~" + p.slice(lib.home.length) : p || "");@@ -1500,6 +1502,9 @@ magpie.plugin("library", (ctx) => {const after = el("div", "after-list");after.append(button(t("+ Add server"), "", () => editServer(null)));body.append(after);+ // kept while picking too, as Add server is: the page stays as tall,+ // so Select doesn't move under the pointer (library-servers-bulk)+ body.append(...hubPart());// each server is checked once a session, and again only once it// changed; once the rows are on the page, for them to show it. One// that no agent and no project has is started for nobody, so it is@@ -1533,6 +1538,186 @@ magpie.plugin("library", (ctx) => {body.append(discover("mcp"));}+ // The MCP hub (#2679): magpie as each agent's one MCP server, handing+ // each call on to the library's servers ticked for it, and one address+ // for any other app with every server in the library. A switch, off+ // till turned on; on, what it serves, the address with Connect, and the+ // agents that keep each server's own entry, with why.+ function hubPart() {+ const list = el("div", "list lib-list lib-hublist");+ const row = el("div", "row lib-row lib-hub");+ const who = el("div", "who");+ who.append(el("div", "name", t("MCP hub")));+ who.append(el("div", "sub wraps", lib.mcpHub+ ? t("Each agent is given one server, magpie, in place of these. magpie runs each server once for all of them and hands each call to it, as server__tool. Restart an agent for it to read the change.")+ : t("Serve every server through magpie: each agent is given one server, magpie, in place of these, and any other app connects at one address.")));+ if (lib.mcpHub) who.append(el("div", "sub lib-hubstats", hubStats()));+ row.append(glyph(GLYPH.hub), who, toggle(!!lib.mcpHub, t("MCP hub"), (on) =>+ change("servers/hub", { on }, on+ ? t("The agents are given magpie's one MCP server — restart them to use it")+ : t("The agents are given each server's own entry again — restart them to use it"))));+ list.append(row);+ const out = [list];+ if (!lib.mcpHub) return out;+ // the one address any other app connects at+ const at = el("div", "row lib-row lib-hubat");+ const aw = el("div", "who");+ aw.append(el("div", "name", t("Other apps")));+ const u = el("div", "sub mono lib-hubURL", hubURL());+ u.title = hubURL();+ aw.append(u);+ const connect = button(t("Connect…"), "", () => connectHub());+ connect.classList.add("lib-hubconnect");+ at.append(glyph(GLYPH.web), aw, copyBtn(hubURL(), t("Address")), connect);+ list.append(at);+ const names = (why) => mcpAgents().filter((a) => a.hub === why).map((a) => a.name).join(", ");+ const why = [+ ["no-remote", "{agents} reaches no MCP server over HTTP, so it keeps each server's own entry."],+ ["taken", "A server in the library is called magpie, so {agents} keeps each server's own entry: give that server another name for the hub to be used."],+ ["own", "{agents} has an MCP server called magpie of its own, which magpie leaves as it is, so it keeps each server's own entry."],+ ];+ for (const [k, text] of why) {+ const n = names(k);+ if (n) {+ const p = el("p", "lib-aside lib-hub-why", t(text, { agents: n }));+ p.dataset.why = k;+ out.push(p);+ }+ }+ return out;+ }+ // what the hub serves: its servers, their tools as far as checked, and+ // the agents given it+ function hubStats() {+ // magpie's own image server is given as it is, never through the hub+ const servers = lib.servers.filter((s) => !(s.transport === "stdio" && s.args?.[0] === "mcp" && s.args?.[1] === "image"));+ const parts = [servers.length === 1 ? t("1 server") : t("{n} servers", { n: servers.length })];+ const known = servers.filter((s) => health.get(s.name)?.h?.state === "ok");+ if (known.length) {+ const n = known.reduce((sum, s) => sum + Math.max(0, (health.get(s.name).h.tools || 0) - (s.disabledTools?.length || 0)), 0);+ parts.push(n === 1 ? t("1 tool") : t("{n} tools", { n }));+ }+ const given = mcpAgents().filter((a) => a.hub === "on").map((a) => a.name);+ if (given.length) parts.push(t("in {agents}", { agents: given.length > 3 ? t("{first} and {n} more", { first: given.slice(0, 2).join(", "), n: given.length - 2 }) : given.join(", ") }));+ return parts.join(" · ");+ }+ const hubURL = () => mcpAddress(state.settings || {}, lib.hubPath.replace(/\/+$/, ""));++ // Connect to the hub: every agent magpie looks after first, each with+ // where it stands (connected, connected with a click, or why it keeps+ // each server's own entry), then the one address any other app connects+ // at, with what adds it there. The agents are magpie's to connect, never+ // a command for the user to run; Cursor is among them when it's here,+ // and only an app magpie doesn't look after is set up by hand: by its+ // install link, which magpie builds (servers/hub-open) and the app+ // confirms, or by copying its entry.+ let hubApp = "";+ function connectHub() {+ const url = hubURL();+ const json = (o) => JSON.stringify(o, null, 2);+ const ed = el("div", "editor lib-editor lib-hubsheet");+ const head = el("div", "ehead");+ head.append(glyph(GLYPH.hub), el("b", "", t("Connect an app")));+ ed.append(head);+ ed.append(el("p", "lib-confirm lib-hublead", t("magpie connects the agents on this computer itself. Any other app that speaks MCP connects at the address below.")));++ // the agents: every one here that takes MCP servers+ const list = el("div", "list lib-list lib-hubagents");+ let busy = false;+ const served = () => lib.servers.filter((s) => !(s.transport === "stdio" && s.args?.[0] === "mcp" && s.args?.[1] === "image"));+ const drawAgents = () => list.replaceChildren(...mcpAgents().map((a) => {+ // one given none of the hub's servers has no state of its own; one+ // that reaches no server over HTTP couldn't take it either way+ const st = a.hub || (a.noRemote ? "no-remote" : "");+ const row = el("div", "row lib-row lib-hubagent");+ row.dataset.agent = a.id;+ row.dataset.state = st || "none";+ const who = el("div", "who");+ who.append(el("div", "name", a.name));+ const n = served().filter((s) => s.agents?.includes(a.id)).length;+ const line = {+ on: n === 1 ? t("1 server, through magpie") : t("{n} servers, through magpie", { n }),+ "no-remote": t("It reaches no MCP server over HTTP, so it keeps each server's own entry"),+ own: t("It has a server called magpie of its own, which magpie leaves as it is"),+ taken: t("A server in the library is called magpie: give it another name to connect"),+ "": served().length ? t("None of the library's servers is on for it yet") : t("The library has no MCP servers yet"),+ }[st];+ who.append(el("div", "sub", a.aside && st === "on" ? line + " · " + a.aside : line));+ row.append(agentIcon(a.icon), who);+ if (st === "on") {+ const ok = el("span", "lib-hubok", t("On the hub"));+ ok.prepend(svg("M3.5 8.5l3 3 6-7", 12, 1.8));+ row.append(ok);+ } else if (!st && served().length) {+ const go = button(t("Connect"), "action lib-updall lib-hubgo", async (e, b) => {+ if (busy) return;+ busy = true;+ for (const x of list.querySelectorAll("button")) x.disabled = true;+ b.classList.add("busy");+ try {+ take(await api("library/servers/agents-all", { agents: [a.id], on: true }));+ const now = mcpAgents().find((x) => x.id === a.id);+ if (now?.hub === "on") status(t("{agent} is connected — restart it to use magpie's tools", { agent: a.name }), "ok");+ else status(t("{agent} couldn't be connected", { agent: a.name }), "warn", 8000);+ render();+ } catch (e) {+ status(e.message, "err", 6000);+ }+ busy = false;+ if (list.isConnected) drawAgents();+ });+ go.title = t("Give {agent} every server it can reach, through magpie", { agent: a.name });+ row.append(go);+ }+ return row;+ }));+ if (mcpAgents().length) {+ ed.append(el("div", "lib-hubsec", t("Agents magpie looks after")), list);+ drawAgents();+ }++ // any other app: the address, and what adds it+ ed.append(el("div", "lib-hubsec", t("Any other app")));+ const addr = el("div", "lib-hubaddr");+ addr.append(el("code", "", url), copyBtn(url, t("Address")));+ ed.append(addr);+ const apps = {+ ...(mcpAgents().some((a) => a.id === "cursor") ? {} : { cursor: { name: "Cursor", link: true, where: t("Or add it to ~/.cursor/mcp.json:"), text: json({ mcpServers: { magpie: { url } } }) } }),+ vscode: { name: "VS Code", link: true, where: t("Or add it to .vscode/mcp.json, or to your user mcp.json:"), text: json({ servers: { magpie: { type: "http", url } } }) },+ json: { name: t("Other"), where: t("For any app that takes MCP servers as JSON (Windsurf, Cline, LM Studio and more):"), text: json({ mcpServers: { magpie: { type: "http", url } } }) },+ };+ if (!apps[hubApp]) hubApp = Object.keys(apps)[0];+ const pane = el("div", "lib-hubpane");+ const draw = () => {+ const a = apps[hubApp];+ pane.replaceChildren();+ if (a.link && !web) {+ const go = button(t("Add to {app}", { app: a.name }), "primary lib-hubadd", async () => {+ go.disabled = true;+ try {+ await api("library/servers/hub-open", { app: hubApp });+ status(t("{app} asks you to confirm adding magpie", { app: a.name }));+ } catch (e) { status(e.message, "err"); }+ go.disabled = false;+ });+ pane.append(go);+ }+ pane.append(el("p", "lib-hubwhere", a.where));+ const block = el("div", "lib-hubcode");+ block.append(el("pre", "", a.text), copyBtn(a.text, a.name));+ pane.append(block);+ if (hubApp === "json") pane.append(el("p", "lib-aside", t("Its transport is Streamable HTTP. From another computer, use the address magpie is reached at there, with a gateway key: Authorization: Bearer <key>.")));+ };+ ed.append(segs(Object.entries(apps).map(([k, a]) => [k, a.name]), hubApp, (k) => { hubApp = k; draw(); }), pane);+ draw();+ const bar = el("div", "bar");+ const done = button(t("Done"), "primary", () => closeLibModal(true));+ bar.append(el("span", "grow"), done);+ ed.append(bar);+ modal = { save: () => done.click() };+ openLib(ed);+ }+function serverRow(s, all) {const row = el("div", "row lib-row click lib-server");const who = el("div", "who");@@ -1804,6 +1989,9 @@ magpie.plugin("library", (ctx) => {// paintHealth fills a row's status in place, by the button or the// server's name, so a check ending moves nothing on the pagefunction paintHealth(x) {+ // the hub's count of tools follows each check+ const stats = page.querySelector(".lib-hubstats");+ if (stats) stats.textContent = hubStats();const boxes = typeof x === "string" ? page.querySelectorAll(`.lib-health[data-server="${CSS.escape(x)}"]`) : [x];for (const b of boxes) {const e = health.get(b.dataset.server);diff --git a/internal/gui/assets/plugins/library/i18n.js b/internal/gui/assets/plugins/library/i18n.jsindex 7247fbadd..212a47a38 100644--- a/internal/gui/assets/plugins/library/i18n.js+++ b/internal/gui/assets/plugins/library/i18n.js@@ -30,6 +30,37 @@ magpie.strings({"What the subagent is told before its task": "子代理开始任务前收到的指示","You are a careful code reviewer…": "你是一位细致的代码审阅者……","Give skills as": "Skills 启用方式",+ "Each agent is given one server, magpie, in place of these. magpie runs each server once for all of them and hands each call to it, as server__tool. Restart an agent for it to read the change.": "每个 Agent 只拿到一个服务器 magpie,代替这些服务器。magpie 为所有 Agent 只运行每个服务器一次,把每次调用转给它,工具名为 server__tool。重启 Agent 后生效。",+ "Serve every server through magpie: each agent is given one server, magpie, in place of these, and any other app connects at one address.": "让所有服务器都经由 magpie 提供:每个 Agent 只拿到一个服务器 magpie,代替这些服务器;其他应用也只需连接一个地址。",+ "1 server": "1 个服务器",+ "{first} and {n} more": "{first} 等 {n} 个",+ "Other apps": "其他应用",+ "Connect…": "连接…",+ "Connect an app": "连接应用",+ "magpie connects the agents on this computer itself. Any other app that speaks MCP connects at the address below.": "这台电脑上的 Agent 由 magpie 直接连好。其他支持 MCP 的应用,连接下面这个地址即可。",+ "Agents magpie looks after": "magpie 管理的 Agent",+ "Any other app": "其他任何应用",+ "1 server, through magpie": "1 个服务器,经由 magpie",+ "{n} servers, through magpie": "{n} 个服务器,经由 magpie",+ "It reaches no MCP server over HTTP, so it keeps each server's own entry": "它不能通过 HTTP 连接 MCP 服务器,所以保留每个服务器各自的条目",+ "It has a server called magpie of its own, which magpie leaves as it is": "它自己有一个名为 magpie 的服务器,magpie 不会改动它",+ "A server in the library is called magpie: give it another name to connect": "资源库里有一个名为 magpie 的服务器:给它换个名字即可连接",+ "None of the library's servers is on for it yet": "资源库里的服务器还都没有给它开启",+ "On the hub": "已连接",+ "{agent} is connected — restart it to use magpie's tools": "{agent} 已连接——重启它即可使用 magpie 的工具",+ "{agent} couldn't be connected": "{agent} 没能连接",+ "Give {agent} every server it can reach, through magpie": "经由 magpie,把它能用的服务器全部给 {agent}",+ "Add to {app}": "添加到 {app}",+ "{app} asks you to confirm adding magpie": "{app} 会请你确认添加 magpie",+ "Or add it to ~/.cursor/mcp.json:": "或者添加到 ~/.cursor/mcp.json:",+ "Or add it to .vscode/mcp.json, or to your user mcp.json:": "或者添加到 .vscode/mcp.json,或用户级的 mcp.json:",+ "For any app that takes MCP servers as JSON (Windsurf, Cline, LM Studio and more):": "适用于任何以 JSON 配置 MCP 服务器的应用(Windsurf、Cline、LM Studio 等):",+ "Its transport is Streamable HTTP. From another computer, use the address magpie is reached at there, with a gateway key: Authorization: Bearer <key>.": "传输方式为 Streamable HTTP。从另一台电脑连接时,请使用那台电脑访问 magpie 的地址,并带上网关密钥:Authorization: Bearer <key>。",+ "The agents are given magpie's one MCP server — restart them to use it": "各 Agent 现在拿到的是 magpie 这一个 MCP 服务器——重启它们后生效",+ "The agents are given each server's own entry again — restart them to use it": "各 Agent 重新拿到每个服务器各自的条目——重启它们后生效",+ "{agents} reaches no MCP server over HTTP, so it keeps each server's own entry.": "{agents} 不能通过 HTTP 连接 MCP 服务器,所以仍保留每个服务器各自的条目。",+ "A server in the library is called magpie, so {agents} keeps each server's own entry: give that server another name for the hub to be used.": "资源库里有一个名为 magpie 的服务器,所以 {agents} 仍保留每个服务器各自的条目:给那个服务器换个名字即可使用聚合。",+ "{agents} has an MCP server called magpie of its own, which magpie leaves as it is, so it keeps each server's own entry.": "{agents} 自己有一个名为 magpie 的 MCP 服务器,magpie 不会改动它,所以仍保留每个服务器各自的条目。","The agents get copies of their skills now": "现在各 Agent 拿到的是 Skills 的副本","The agents get links to their skills now": "现在各 Agent 拿到的是指向资源库的符号链接","By agent ({n})": "按 Agent({n})",@@ -664,6 +695,37 @@ magpie.strings({"already in the library: {names}": "資源庫裡已有:{names}","{name} skipped: {error}": "已略過 {name}:{error}","Give skills as": "Skills 啟用方式",+ "Each agent is given one server, magpie, in place of these. magpie runs each server once for all of them and hands each call to it, as server__tool. Restart an agent for it to read the change.": "每個 Agent 只拿到一個伺服器 magpie,代替這些伺服器。magpie 為所有 Agent 只執行每個伺服器一次,把每次呼叫轉給它,工具名稱為 server__tool。重新啟動 Agent 後生效。",+ "Serve every server through magpie: each agent is given one server, magpie, in place of these, and any other app connects at one address.": "讓所有伺服器都經由 magpie 提供:每個 Agent 只拿到一個伺服器 magpie,代替這些伺服器;其他應用程式也只需連線一個位址。",+ "1 server": "1 個伺服器",+ "{first} and {n} more": "{first} 等 {n} 個",+ "Other apps": "其他應用程式",+ "Connect…": "連線…",+ "Connect an app": "連線應用程式",+ "magpie connects the agents on this computer itself. Any other app that speaks MCP connects at the address below.": "這台電腦上的 Agent 由 magpie 直接連好。其他支援 MCP 的應用程式,連線下面這個位址即可。",+ "Agents magpie looks after": "magpie 管理的 Agent",+ "Any other app": "其他任何應用程式",+ "1 server, through magpie": "1 個伺服器,經由 magpie",+ "{n} servers, through magpie": "{n} 個伺服器,經由 magpie",+ "It reaches no MCP server over HTTP, so it keeps each server's own entry": "它無法透過 HTTP 連線 MCP 伺服器,所以保留每個伺服器各自的項目",+ "It has a server called magpie of its own, which magpie leaves as it is": "它自己有一個名為 magpie 的伺服器,magpie 不會更動它",+ "A server in the library is called magpie: give it another name to connect": "資源庫裡有一個名為 magpie 的伺服器:替它換個名稱即可連線",+ "None of the library's servers is on for it yet": "資源庫裡的伺服器都還沒有為它開啟",+ "On the hub": "已連線",+ "{agent} is connected — restart it to use magpie's tools": "{agent} 已連線——重新啟動它即可使用 magpie 的工具",+ "{agent} couldn't be connected": "{agent} 無法連線",+ "Give {agent} every server it can reach, through magpie": "經由 magpie,把它能用的伺服器全部給 {agent}",+ "Add to {app}": "新增到 {app}",+ "{app} asks you to confirm adding magpie": "{app} 會請你確認新增 magpie",+ "Or add it to ~/.cursor/mcp.json:": "或者新增到 ~/.cursor/mcp.json:",+ "Or add it to .vscode/mcp.json, or to your user mcp.json:": "或者新增到 .vscode/mcp.json,或使用者層級的 mcp.json:",+ "For any app that takes MCP servers as JSON (Windsurf, Cline, LM Studio and more):": "適用於任何以 JSON 設定 MCP 伺服器的應用程式(Windsurf、Cline、LM Studio 等):",+ "Its transport is Streamable HTTP. From another computer, use the address magpie is reached at there, with a gateway key: Authorization: Bearer <key>.": "傳輸方式為 Streamable HTTP。從另一台電腦連線時,請使用那台電腦存取 magpie 的位址,並帶上閘道金鑰:Authorization: Bearer <key>。",+ "The agents are given magpie's one MCP server — restart them to use it": "各 Agent 現在拿到的是 magpie 這一個 MCP 伺服器——重新啟動它們後生效",+ "The agents are given each server's own entry again — restart them to use it": "各 Agent 重新拿到每個伺服器各自的項目——重新啟動它們後生效",+ "{agents} reaches no MCP server over HTTP, so it keeps each server's own entry.": "{agents} 無法透過 HTTP 連線 MCP 伺服器,所以仍保留每個伺服器各自的項目。",+ "A server in the library is called magpie, so {agents} keeps each server's own entry: give that server another name for the hub to be used.": "資源庫裡有一個名為 magpie 的伺服器,所以 {agents} 仍保留每個伺服器各自的項目:給那個伺服器換個名稱即可使用聚合。",+ "{agents} has an MCP server called magpie of its own, which magpie leaves as it is, so it keeps each server's own entry.": "{agents} 自己有一個名為 magpie 的 MCP 伺服器,magpie 不會更動它,所以仍保留每個伺服器各自的項目。","The agents get copies of their skills now": "現在各 Agent 拿到的是 Skills 的副本","The agents get links to their skills now": "現在各 Agent 拿到的是指向資源庫的符號連結","By agent ({n})": "按 Agent({n})",@@ -1294,6 +1356,37 @@ magpie.strings({"What the subagent is told before its task": "サブエージェントがタスクの前に受け取る指示","You are a careful code reviewer…": "あなたは注意深いコードレビュアーです…","Give skills as": "スキルの渡し方",+ "Each agent is given one server, magpie, in place of these. magpie runs each server once for all of them and hands each call to it, as server__tool. Restart an agent for it to read the change.": "各エージェントには、これらの代わりに magpie というサーバーが 1 つだけ渡されます。magpie は各サーバーを全エージェントで 1 回だけ起動し、呼び出しを server__tool の名前で中継します。反映するにはエージェントを再起動してください。",+ "Serve every server through magpie: each agent is given one server, magpie, in place of these, and any other app connects at one address.": "すべてのサーバーを magpie 経由で提供します。各エージェントにはこれらの代わりに magpie というサーバーが 1 つだけ渡され、ほかのアプリも 1 つのアドレスで接続できます。",+ "1 server": "1 サーバー",+ "{first} and {n} more": "{first} ほか {n} 件",+ "Other apps": "ほかのアプリ",+ "Connect…": "接続…",+ "Connect an app": "アプリを接続",+ "magpie connects the agents on this computer itself. Any other app that speaks MCP connects at the address below.": "このコンピュータのエージェントは magpie が接続します。MCP に対応したほかのアプリは、下のアドレスに接続してください。",+ "Agents magpie looks after": "magpie が管理するエージェント",+ "Any other app": "ほかのアプリ",+ "1 server, through magpie": "1 個のサーバー、magpie 経由",+ "{n} servers, through magpie": "{n} 個のサーバー、magpie 経由",+ "It reaches no MCP server over HTTP, so it keeps each server's own entry": "HTTP で MCP サーバーに接続できないため、各サーバーのエントリをそのまま使います",+ "It has a server called magpie of its own, which magpie leaves as it is": "独自の magpie という名前のサーバーがあり、magpie はそれを変更しません",+ "A server in the library is called magpie: give it another name to connect": "ライブラリに magpie という名前のサーバーがあります。名前を変えると接続できます",+ "None of the library's servers is on for it yet": "ライブラリのサーバーはまだどれもオンになっていません",+ "On the hub": "接続済み",+ "{agent} is connected — restart it to use magpie's tools": "{agent} を接続しました。再起動すると magpie のツールを使えます",+ "{agent} couldn't be connected": "{agent} を接続できませんでした",+ "Give {agent} every server it can reach, through magpie": "{agent} が使えるサーバーをすべて magpie 経由で渡す",+ "Add to {app}": "{app} に追加",+ "{app} asks you to confirm adding magpie": "{app} で magpie の追加を確認してください",+ "Or add it to ~/.cursor/mcp.json:": "または ~/.cursor/mcp.json に追加:",+ "Or add it to .vscode/mcp.json, or to your user mcp.json:": "または .vscode/mcp.json か、ユーザーの mcp.json に追加:",+ "For any app that takes MCP servers as JSON (Windsurf, Cline, LM Studio and more):": "MCP サーバーを JSON で設定するアプリ向け(Windsurf、Cline、LM Studio など):",+ "Its transport is Streamable HTTP. From another computer, use the address magpie is reached at there, with a gateway key: Authorization: Bearer <key>.": "トランスポートは Streamable HTTP です。別のコンピューターからは、そこから magpie に届くアドレスとゲートウェイキーを使います:Authorization: Bearer <key>。",+ "The agents are given magpie's one MCP server — restart them to use it": "エージェントには magpie の 1 つの MCP サーバーが渡されました — 使うには再起動してください",+ "The agents are given each server's own entry again — restart them to use it": "エージェントには各サーバーのエントリーが再び渡されました — 使うには再起動してください",+ "{agents} reaches no MCP server over HTTP, so it keeps each server's own entry.": "{agents} は HTTP で MCP サーバーに接続できないため、各サーバーのエントリーをそのまま使います。",+ "A server in the library is called magpie, so {agents} keeps each server's own entry: give that server another name for the hub to be used.": "ライブラリに magpie という名前のサーバーがあるため、{agents} は各サーバーのエントリーをそのまま使います。ハブを使うには、そのサーバーの名前を変えてください。",+ "{agents} has an MCP server called magpie of its own, which magpie leaves as it is, so it keeps each server's own entry.": "{agents} には独自の magpie という MCP サーバーがあり、magpie はそれに手を付けないため、各サーバーのエントリーをそのまま使います。","The agents get copies of their skills now": "エージェントにはスキルのコピーを渡すようになりました","The agents get links to their skills now": "エージェントにはスキルへのリンクを渡すようになりました","By agent ({n})": "エージェント別({n})",@@ -1926,6 +2019,37 @@ magpie.strings({"What the subagent is told before its task": "Was der Subagent vor seiner Aufgabe gesagt bekommt","You are a careful code reviewer…": "Sie sind ein sorgfältiger Code-Reviewer …","Give skills as": "Skills geben als",+ "Each agent is given one server, magpie, in place of these. magpie runs each server once for all of them and hands each call to it, as server__tool. Restart an agent for it to read the change.": "Jeder Agent erhält statt dieser einen einzigen Server, magpie. magpie startet jeden Server einmal für alle und reicht jeden Aufruf als server__tool weiter. Starte einen Agent neu, damit er die Änderung liest.",+ "Serve every server through magpie: each agent is given one server, magpie, in place of these, and any other app connects at one address.": "Alle Server über magpie bereitstellen: Jeder Agent erhält statt dieser einen einzigen Server, magpie, und jede andere App verbindet sich über eine Adresse.",+ "1 server": "1 Server",+ "{first} and {n} more": "{first} und {n} weitere",+ "Other apps": "Andere Apps",+ "Connect…": "Verbinden…",+ "Connect an app": "App verbinden",+ "magpie connects the agents on this computer itself. Any other app that speaks MCP connects at the address below.": "Die Agents auf diesem Computer verbindet magpie selbst. Jede andere App, die MCP spricht, verbindet sich mit der Adresse unten.",+ "Agents magpie looks after": "Von magpie verwaltete Agents",+ "Any other app": "Jede andere App",+ "1 server, through magpie": "1 Server, über magpie",+ "{n} servers, through magpie": "{n} Server, über magpie",+ "It reaches no MCP server over HTTP, so it keeps each server's own entry": "Erreicht keinen MCP-Server über HTTP und behält daher die Einträge der einzelnen Server",+ "It has a server called magpie of its own, which magpie leaves as it is": "Hat einen eigenen Server namens magpie, den magpie unverändert lässt",+ "A server in the library is called magpie: give it another name to connect": "Ein Server in der Bibliothek heißt magpie: Benenne ihn um, um zu verbinden",+ "None of the library's servers is on for it yet": "Noch keiner der Server der Bibliothek ist dafür aktiv",+ "On the hub": "Verbunden",+ "{agent} is connected — restart it to use magpie's tools": "{agent} ist verbunden – starte es neu, um die Tools von magpie zu nutzen",+ "{agent} couldn't be connected": "{agent} konnte nicht verbunden werden",+ "Give {agent} every server it can reach, through magpie": "{agent} alle erreichbaren Server über magpie geben",+ "Add to {app}": "Zu {app} hinzufügen",+ "{app} asks you to confirm adding magpie": "{app} bittet dich, das Hinzufügen von magpie zu bestätigen",+ "Or add it to ~/.cursor/mcp.json:": "Oder füge es zu ~/.cursor/mcp.json hinzu:",+ "Or add it to .vscode/mcp.json, or to your user mcp.json:": "Oder füge es zu .vscode/mcp.json oder zu deiner Benutzer-mcp.json hinzu:",+ "For any app that takes MCP servers as JSON (Windsurf, Cline, LM Studio and more):": "Für jede App, die MCP-Server als JSON einträgt (Windsurf, Cline, LM Studio und mehr):",+ "Its transport is Streamable HTTP. From another computer, use the address magpie is reached at there, with a gateway key: Authorization: Bearer <key>.": "Der Transport ist Streamable HTTP. Von einem anderen Computer aus nutze die Adresse, unter der magpie dort erreichbar ist, mit einem Gateway-Schlüssel: Authorization: Bearer <key>.",+ "The agents are given magpie's one MCP server — restart them to use it": "Die Agents erhalten jetzt magpies einen MCP-Server — starte sie neu, um ihn zu nutzen",+ "The agents are given each server's own entry again — restart them to use it": "Die Agents erhalten wieder den eigenen Eintrag jedes Servers — starte sie neu, um sie zu nutzen",+ "{agents} reaches no MCP server over HTTP, so it keeps each server's own entry.": "{agents} erreicht keinen MCP-Server über HTTP und behält daher den eigenen Eintrag jedes Servers.",+ "A server in the library is called magpie, so {agents} keeps each server's own entry: give that server another name for the hub to be used.": "Ein Server in der Bibliothek heißt magpie, daher behält {agents} den eigenen Eintrag jedes Servers: Gib diesem Server einen anderen Namen, um den Hub zu nutzen.",+ "{agents} has an MCP server called magpie of its own, which magpie leaves as it is, so it keeps each server's own entry.": "{agents} hat einen eigenen MCP-Server namens magpie, den magpie unverändert lässt, und behält daher den eigenen Eintrag jedes Servers.","The agents get copies of their skills now": "Die Agenten bekommen jetzt Kopien ihrer Skills","The agents get links to their skills now": "Die Agenten bekommen jetzt Links zu ihren Skills","By agent ({n})": "Nach Agenten ({n})",diff --git a/internal/gui/assets/plugins/library/library.css b/internal/gui/assets/plugins/library/library.cssindex 89c07c693..e30075eee 100644--- a/internal/gui/assets/plugins/library/library.css+++ b/internal/gui/assets/plugins/library/library.css@@ -52,6 +52,44 @@ body.window .lib-head { top: -2px; margin: -2px 0 0; padding: 8px 0 4px; --bleed.lib-intro { margin: 8px 2px 10px; font-size: 12px; line-height: 1.5; color: var(--muted); text-wrap: pretty; }.lib-aside { margin: 8px 4px 0; font-size: 11.5px; line-height: 1.5; color: var(--faint); }.lib-aside.center { text-align: center; margin-top: 14px; }+/* the MCP hub's switch, under the servers (#2679) */+.lib-hublist { margin-top: 16px; }+.lib-hub .lib-switch { align-self: center; }+/* its switch stays beside its text in a narrow window, as a setting's does */+.lib-list .row.lib-hub { flex-wrap: nowrap; }+.lib-hub .name { white-space: normal; }+.lib-hub .lib-hubstats { margin-top: 4px; color: var(--muted); font-variant-numeric: tabular-nums; }+/* the address any other app connects at, under the switch */+.lib-list .row.lib-hubat { flex-wrap: nowrap; align-items: center; }+.lib-hubat > .lib-glyph { align-self: center; margin-top: 0; }+.lib-hubat .who { min-width: 0; }+.lib-hubat .lib-hubURL { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; user-select: text; }+.lib-hubat .lib-hubconnect { flex: none; }+/* Connect an app: the agents here and where each stands, then the address, a picker of apps, and what adds it */+/* one column: the dialog's body (.ebody) is the editor's label/field grid */+.modal .editor.lib-hubsheet > .ebody { grid-template-columns: minmax(0, 1fr); grid-auto-rows: max-content; gap: 12px; }+.lib-hubsheet .ebody > * { min-width: 0; margin-top: 0; margin-bottom: 0; }+.lib-hubsheet .lib-hublead { color: var(--fg-2); text-wrap: pretty; }+.lib-hubsheet .lib-hubsec { margin-top: 6px; font-size: 12px; font-weight: 600; color: var(--muted); }+.lib-hubsheet .lib-hubagents { max-height: min(40vh, 300px); overflow: auto; }+.lib-list .row.lib-hubagent { gap: 10px; flex-wrap: nowrap; align-items: center; }+.lib-hubagent > .ic { width: 20px; height: 20px; flex: none; }+.lib-hubagent .who { flex: 1; min-width: 0; }+.lib-hubagent .sub { white-space: normal; text-wrap: pretty; }+.lib-hubok { flex: none; display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--green); }+.lib-hubagent .lib-hubgo { height: 24px; padding: 0 10px; font-size: 12px; flex: none; }+.lib-hubaddr { display: flex; align-items: center; gap: 6px; min-width: 0; padding: 8px 8px 8px 12px; border-radius: 8px; background: var(--pill); }+.lib-hubaddr code { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12.5px; user-select: all; }+.lib-hubsheet .segs { justify-self: start; max-width: 100%; overflow-x: auto; scrollbar-width: none; }+.lib-hubsheet .segs .opt { flex: none; }+.lib-hubpane { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; min-width: 0; min-height: 132px; }+.lib-hubpane .lib-hubadd { margin-bottom: 2px; }+.lib-hubwhere, .lib-hubdone { margin: 0; font-size: 12.5px; line-height: 1.5; color: var(--fg-2); }+.lib-hubdone { color: var(--muted); }+.lib-hubcode { position: relative; align-self: stretch; min-width: 0; border: 1px solid var(--line-2); border-radius: 8px; background: var(--bg-2, var(--pill)); }+.lib-hubcode pre { margin: 0; padding: 10px 40px 10px 12px; overflow-x: auto; font-family: var(--code-font, var(--code)); font-size: 12px; line-height: 1.55; white-space: pre; user-select: text; }+.lib-hubcode .copy { position: absolute; top: 6px; right: 6px; }+.lib-hubpane .lib-aside { margin: 0; }/* the glyph at the front of a row: drawn, in the text colour */.lib-glyph { flex: none; width: 26px; height: 26px; border-radius: 7px; display: grid; place-items: center; color: var(--fg-2); background: var(--pill); }diff --git a/internal/gui/library.go b/internal/gui/library.goindex 8110d9dfb..1d78b7630 100644--- a/internal/gui/library.go+++ b/internal/gui/library.go@@ -436,6 +436,7 @@ func libraryRoutes(mux *http.ServeMux, w Windows) {// a subagent saved (#2283)Subagent library.SubagentAgent string // the agent whose own skill is in the library's way+ App string // the app the MCP hub is added to (servers/hub-open)ID string // a market server's, or a market skill's in its repositoryValues map[string]string // what a market server needsDir string // a project's folder@@ -465,6 +466,16 @@ func libraryRoutes(mux *http.ServeMux, w Windows) {res, err = library.SaveServer(in.Old, in.Server)case "servers/agents":res, err = library.ServerAgents(in.Name, in.Agents)+ case "servers/hub":+ // magpie as the agents' one MCP server (#2679)+ res, err = library.SetMCPHub(in.On)+ case "servers/hub-open":+ // the hub added to another app through its install link,+ // built here: the page names the app, never the link+ var link string+ if link, err = library.HubLink(in.App); err == nil {+ err = w.OpenURL(link)+ }case "servers/tools":// the tools its agents leave out (nf1qaz2w on Discord)res, err = library.ServerTools(in.Name, in.Names)diff --git a/internal/gui/library_hub_open_test.go b/internal/gui/library_hub_open_test.gonew file mode 100644index 000000000..0c0df529d--- /dev/null+++ b/internal/gui/library_hub_open_test.go@@ -0,0 +1,48 @@+package gui++import (+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/yetone/magpie/internal/library"+)++// opens records the links handed to the system, opening none.+type opens struct {+ webHost+ links []string+}++func (o *opens) OpenURL(u string) error { o.links = append(o.links, u); return nil }++// The Connect sheet's "Add to Cursor" names the app, and the window opens+// that app's install link for the hub, built here; an app with no link is+// an error and nothing is opened. The page never sends a link to open.+func TestHubOpenInstallLink(t *testing.T) {+ sandboxHome(t)+ o := &opens{}+ post := func(body string) *httptest.ResponseRecorder {+ rec := httptest.NewRecorder()+ Handler(o, nil).ServeHTTP(rec, httptest.NewRequest("POST", "/api/library/servers/hub-open", strings.NewReader(body)))+ return rec+ }+ for _, app := range []string{"cursor", "vscode"} {+ if rec := post(`{"app":"` + app + `"}`); rec.Code != http.StatusOK {+ t.Fatalf("%s: %d %s", app, rec.Code, rec.Body)+ }+ want, _ := library.HubLink(app)+ if len(o.links) == 0 || o.links[len(o.links)-1] != want {+ t.Fatalf("%s opened %q, want %q", app, o.links, want)+ }+ }+ for _, body := range []string{`{"app":"javascript:alert(1)"}`, `{"app":"https://evil.example"}`, `{}`} {+ if rec := post(body); rec.Code == http.StatusOK {+ t.Errorf("%s: %d %s", body, rec.Code, rec.Body)+ }+ }+ if len(o.links) != 2 {+ t.Fatalf("opened %q", o.links)+ }+}diff --git a/internal/gui/tests/library-mcp-hub.test.cjs b/internal/gui/tests/library-mcp-hub.test.cjsnew file mode 100644index 000000000..2a3bd057e--- /dev/null+++ b/internal/gui/tests/library-mcp-hub.test.cjs@@ -0,0 +1,262 @@+// Run with Node's test runner and Playwright on the module path; see README.md.+// #2679 (@minggang123 on X): the Library's MCP section has a switch that+// gives each agent one MCP server, magpie, in place of the library's+// servers. Off, it says what it would do; a click posts servers/hub on,+// says the agents are given magpie's one server, and the page then says so.+// Each agent that keeps each server's own entry is named under it with why+// (Claude Desktop reaches no server over HTTP; Codex has a "magpie" of its+// own). On, it counts what it serves (servers, tools, the agents given it)+// and shows the one address any other app connects at, with Connect…: a+// sheet listing every agent magpie found that takes MCP servers, each with+// where it stands (connected, why it keeps each server's own entry, or a+// Connect that gives it every server, posting servers/agents-all, after+// which it says connected), then that address and, for Cursor (when it+// isn't one of the agents), VS Code and any JSON app, what adds it. Cursor+// and VS Code get an "Add to" button in the app (it posts servers/hub-open+// with the app's name, never a link) and none in a browser. No agent is+// told to the user as a command to run. The address is the public one in a+// browser and the gateway in the app. A+// click again turns it off. In every language, both engines, a wide window+// in the app and a narrow one in a browser: no sideways scroll, and the+// switch beside its text.+// No backend: the API is faked here.+const assert = require("node:assert/strict");+const fs = require("node:fs/promises");+const path = require("node:path");+const { test } = require("node:test");+const { chromium, webkit } = require("playwright");+const { strings } = require("./strings.cjs");++const assets = path.resolve(__dirname, "../assets");+const HOME = "/Users/mg";+const fixture = (on, st = {}) => ({+ dir: `${HOME}/.magpie/library`, backups: `${HOME}/.magpie/backups`, home: HOME,+ mcpHub: on, hubPath: "/mcp/magpie/hub/",+ agents: [+ { id: "claude", name: "Claude Code", icon: "claudecode-color", skills: `${HOME}/.claude/skills`, mcp: `${HOME}/.claude.json`, ...(on ? { hub: "on" } : {}) },+ { id: "codex", name: "Codex", icon: "codex-color", mcp: `${HOME}/.codex/config.toml`, ...(on ? { hub: "own" } : {}) },+ { id: "claude-desktop", name: "Claude Desktop", icon: "claude-color", mcp: `${HOME}/Library/Application Support/Claude/claude_desktop_config.json`, noRemote: true, ...(on ? { hub: "no-remote" } : {}) },+ // given none of the library's servers yet: Connect gives it them+ ...(st.cursor ? [{ id: "cursor", name: "Cursor", icon: "cursor", mcp: `${HOME}/.cursor/mcp.json`, ...(on && st.cursorOn ? { hub: "on" } : {}) }] : []),+ ],+ instructions: { agents: [], sets: [] }, foundServers: [], projects: [], foundSkills: [], skills: [], problems: [],+ servers: [+ { name: "my-tools", transport: "stdio", command: "node", args: [`${HOME}/code/my-tools/index.js`], agents: ["claude", "codex", "claude-desktop", ...(st.cursorOn ? ["cursor"] : [])] },+ { name: "notes", transport: "stdio", command: "uvx", args: ["notes-mcp"], agents: ["claude", "codex", ...(st.cursorOn ? ["cursor"] : [])] },+ ],+});++let I18N;+function tr(lang, key, vars = {}) {+ let s = key;+ if (lang !== "en") {+ assert.equal(typeof I18N[lang][key], "string", `${lang} has no ${JSON.stringify(key)}`);+ s = I18N[lang][key];+ }+ return s.replace(/\{(\w+)\}/g, (m, k) => (k in vars ? String(vars[k]) : m));+}++function server(lang, st) {+ return async (route) => {+ const req = route.request(), url = new URL(req.url());+ const json = (data) => route.fulfill({ json: data });+ if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:${st.web}};` });+ if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });+ if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light", gateway: "http://127.0.0.1:3425", publicURL: "https://mac.tail0.ts.net" } });+ if (url.pathname === "/api/library") return json(fixture(st.on, st));+ if (url.pathname === "/api/library/servers/hub") {+ const body = req.postDataJSON();+ st.sent.push(body);+ st.on = body.on;+ return json({ ...fixture(st.on, st), result: { changed: ["claude"] } });+ }+ if (url.pathname === "/api/library/servers/hub-open") {+ st.opened.push(req.postDataJSON());+ return json(fixture(st.on, st));+ }+ if (url.pathname === "/api/library/servers/agents-all") {+ st.all.push(req.postDataJSON());+ st.cursorOn = true;+ return json({ ...fixture(st.on, st), result: { changed: ["cursor"] } });+ }+ if (url.pathname === "/api/library/mcp/check") return json({ servers: Object.fromEntries(req.postDataJSON().names.map((n) => [n, { state: "ok", tools: 2 }])) });+ if (url.pathname === "/api/plugins") return json({ plugins: [] });+ if (url.pathname === "/api/usage/quotas") return json([]);+ if (url.pathname === "/api/groups") return json({ groups: [] });+ if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true } });+ if (url.pathname.startsWith("/api/")) return json({});+ if (url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" });+ const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);+ const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];+ try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }+ };+}++const sideways = (page) => page.evaluate(() => {+ const over = [];+ const w = document.documentElement.clientWidth;+ if (document.documentElement.scrollWidth > w) over.push("page " + document.documentElement.scrollWidth);+ for (const e of document.querySelectorAll("#view-library .lib-hub, #view-library .lib-hub .lib-switch, #view-library .lib-hub-why, #view-library .lib-hubat, #view-library .lib-hubat .lib-hubconnect, #view-library .lib-hubat .copy, .lib-hubsheet, .lib-hubsheet .segs, .lib-hubsheet .lib-hubcode, .lib-hubsheet .lib-hubagent, .lib-hubsheet .lib-hubagent .lib-hubgo, .lib-hubsheet .lib-hubok, .lib-hubsheet .bar button")) {+ const r = e.getBoundingClientRect();+ if (r.width && (r.right > w + 0.5 || r.left < -0.5)) over.push((e.className || e.tagName) + " " + Math.round(r.left) + "–" + Math.round(r.right));+ }+ return over;+});++for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {+ test(engine + ": one MCP server for every agent", async (t) => {+ assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");+ I18N = strings().I18N;+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL || "chromium" }));+ t.after(() => browser.close());++ for (const width of [1100, 420]) for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {+ await t.test(`${lang} ${width}px`, async () => {+ const w = (key, vars) => tr(lang, key, vars);+ const web = width < 800;+ // Cursor is one of the agents in the app, and not in a browser+ const st = { on: false, sent: [], opened: [], all: [], web, cursor: !web };+ const url = (web ? "https://mac.tail0.ts.net" : "http://127.0.0.1:3425") + "/mcp/magpie/hub";+ const errors = [];+ const ctx = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" });+ await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "mcp"); } catch {} });+ const page = await ctx.newPage();+ page.setDefaultTimeout(5000);+ page.on("pageerror", (e) => errors.push(e.message));+ await page.route("**/*", server(lang, st));+ await page.goto("http://magpie.test/");+ await page.locator('button[data-view="library"]').click();+ const v = page.locator("#view-library");+ const hub = v.locator(".lib-hub");+ await hub.waitFor();++ // off: what it would do, and no reasons yet+ assert.equal((await hub.locator(".name").textContent()).trim(), w("MCP hub"));+ assert.equal((await hub.locator(".sub.wraps").textContent()).trim(), w("Serve every server through magpie: each agent is given one server, magpie, in place of these, and any other app connects at one address."));+ assert.equal(await v.locator(".lib-hubstats, .lib-hubat").count(), 0);+ const sw = hub.locator(".lib-switch");+ assert.equal(await sw.getAttribute("role"), "switch");+ assert.equal(await sw.getAttribute("aria-checked"), "false");+ assert.equal(await v.locator(".lib-hub-why").count(), 0);+ // below the servers, not among them+ const below = await page.evaluate(() => {+ const rows = [...document.querySelectorAll("#view-library .lib-server")];+ const hub = document.querySelector("#view-library .lib-hub");+ return rows.length > 0 && rows.every((r) => r.getBoundingClientRect().bottom <= hub.getBoundingClientRect().top);+ });+ assert.ok(below, "the hub's row isn't below the servers");+ assert.deepEqual(await sideways(page), [], "sideways scroll, off");++ // on+ await sw.click();+ await page.waitForFunction((x) => document.querySelector("#status")?.textContent === x, w("The agents are given magpie's one MCP server — restart them to use it"));+ assert.deepEqual(st.sent, [{ on: true }]);+ await page.waitForFunction(() => document.querySelector("#view-library .lib-hub .lib-switch")?.getAttribute("aria-checked") === "true");+ assert.equal((await v.locator(".lib-hub .sub.wraps").textContent()).trim(), w("Each agent is given one server, magpie, in place of these. magpie runs each server once for all of them and hands each call to it, as server__tool. Restart an agent for it to read the change."));+ // what it serves: two servers, 2 tools each once checked, in Claude Code+ await page.waitForFunction((x) => document.querySelector("#view-library .lib-hubstats")?.textContent === x,+ [w("{n} servers", { n: 2 }), w("{n} tools", { n: 4 }), w("in {agents}", { agents: "Claude Code" })].join(" · "));+ // the address any other app connects at+ const at = v.locator(".lib-hubat");+ assert.equal((await at.locator(".name").textContent()).trim(), w("Other apps"));+ assert.equal((await at.locator(".lib-hubURL").textContent()).trim(), url);+ assert.equal((await at.locator(".lib-hubconnect").textContent()).trim(), w("Connect…"));+ const why = v.locator(".lib-hub-why");+ assert.deepEqual(await why.evaluateAll((ps) => ps.map((p) => p.dataset.why)), ["no-remote", "own"]);+ assert.equal((await v.locator('.lib-hub-why[data-why="no-remote"]').textContent()).trim(), w("{agents} reaches no MCP server over HTTP, so it keeps each server's own entry.", { agents: "Claude Desktop" }));+ assert.equal((await v.locator('.lib-hub-why[data-why="own"]').textContent()).trim(), w("{agents} has an MCP server called magpie of its own, which magpie leaves as it is, so it keeps each server's own entry.", { agents: "Codex" }));+ // the switch sits beside its text, inside the row+ const fits = await page.evaluate(() => {+ const row = document.querySelector("#view-library .lib-hub").getBoundingClientRect();+ const s = document.querySelector("#view-library .lib-hub .lib-switch").getBoundingClientRect();+ return s.width > 0 && s.left >= row.left && s.right <= row.right + 0.5 && s.top >= row.top && s.bottom <= row.bottom + 0.5;+ });+ assert.ok(fits, "the switch is outside its row");+ assert.deepEqual(await sideways(page), [], "sideways scroll, on");++ // Connect…: every agent here and where it stands, then the address+ // and what adds it, app by app+ await at.locator(".lib-hubconnect").click();+ const sheet = page.locator(".lib-hubsheet");+ await sheet.waitFor();+ assert.equal((await sheet.locator(".ehead b").textContent()).trim(), w("Connect an app"));+ assert.equal((await sheet.locator(".lib-hublead").textContent()).trim(), w("magpie connects the agents on this computer itself. Any other app that speaks MCP connects at the address below."));+ assert.deepEqual((await sheet.locator(".lib-hubsec").allTextContents()).map((x) => x.trim()), [w("Agents magpie looks after"), w("Any other app")]);+ const rows = sheet.locator(".lib-hubagent");+ const states = () => rows.evaluateAll((rs) => rs.map((r) => [r.dataset.agent, r.dataset.state]));+ assert.deepEqual(await states(), [["claude", "on"], ["codex", "own"], ["claude-desktop", "no-remote"], ...(web ? [] : [["cursor", "none"]])]);+ const line = (id) => sheet.locator(`.lib-hubagent[data-agent="${id}"] .sub`).textContent().then((x) => x.trim());+ assert.equal(await line("claude"), w("{n} servers, through magpie", { n: 2 }));+ assert.equal((await sheet.locator('.lib-hubagent[data-agent="claude"] .lib-hubok').textContent()).trim(), w("On the hub"));+ assert.equal(await line("codex"), w("It has a server called magpie of its own, which magpie leaves as it is"));+ assert.equal(await line("claude-desktop"), w("It reaches no MCP server over HTTP, so it keeps each server's own entry"));+ // only the one with nothing yet can be connected, and nothing is a command to run+ assert.equal(await sheet.locator(".lib-hubgo").count(), web ? 0 : 1);+ assert.equal(await sheet.locator(".lib-hubok").count(), 1);+ assert.doesNotMatch(await sheet.textContent(), /mcp add/);+ if (!web) {+ assert.equal(await line("cursor"), w("None of the library's servers is on for it yet"));+ const go = sheet.locator('.lib-hubagent[data-agent="cursor"] .lib-hubgo');+ assert.equal((await go.textContent()).trim(), w("Connect"));+ await go.click();+ await page.waitForFunction((x) => document.querySelector("#status")?.textContent === x, w("{agent} is connected — restart it to use magpie's tools", { agent: "Cursor" }));+ assert.deepEqual(st.all, [{ agents: ["cursor"], on: true }]);+ await page.waitForFunction(() => document.querySelector('.lib-hubagent[data-agent="cursor"]')?.dataset.state === "on");+ assert.equal(await line("cursor"), w("{n} servers, through magpie", { n: 2 }));+ assert.equal(await sheet.locator(".lib-hubgo").count(), 0);+ // the sheet stays open over the page drawn again+ assert.ok(await sheet.isVisible());+ }+ // each agent's icon beside its name, at every width+ const beside = await rows.evaluateAll((rs) => rs.filter((r) => {+ const i = r.querySelector(":scope > .ic").getBoundingClientRect(), n = r.querySelector(".name").getBoundingClientRect();+ return i.right > n.left || i.bottom < n.top || i.top > n.bottom;+ }).map((r) => r.dataset.agent));+ assert.deepEqual(beside, [], "an agent's icon isn't beside its name");+ assert.deepEqual(await sideways(page), [], "sideways scroll, agents");++ assert.equal((await sheet.locator(".lib-hubaddr code").textContent()).trim(), url);+ const opts = sheet.locator(".segs .opt");+ // Cursor is set up by hand only when magpie doesn't look after it+ assert.deepEqual((await opts.allTextContents()).map((x) => x.trim()), [...(web ? ["Cursor"] : []), "VS Code", w("Other")]);+ const pane = sheet.locator(".lib-hubpane");+ const code = async () => JSON.parse(await pane.locator(".lib-hubcode pre").textContent());+ if (web) {+ assert.equal((await pane.locator(".lib-hubwhere").textContent()).trim(), w("Or add it to ~/.cursor/mcp.json:"));+ assert.deepEqual(await code(), { mcpServers: { magpie: { url } } });+ assert.equal(await pane.locator(".lib-hubadd").count(), 0);+ await opts.nth(1).click();+ }+ // VS Code: its install link in the app, its file either way+ assert.equal((await pane.locator(".lib-hubwhere").textContent()).trim(), w("Or add it to .vscode/mcp.json, or to your user mcp.json:"));+ assert.deepEqual(await code(), { servers: { magpie: { type: "http", url } } });+ if (web) assert.equal(await pane.locator(".lib-hubadd").count(), 0);+ else {+ assert.equal((await pane.locator(".lib-hubadd").textContent()).trim(), w("Add to {app}", { app: "VS Code" }));+ await pane.locator(".lib-hubadd").click();+ await page.waitForFunction((x) => document.querySelector("#status")?.textContent === x, w("{app} asks you to confirm adding magpie", { app: "VS Code" }));+ assert.deepEqual(st.opened, [{ app: "vscode" }]);+ }+ assert.deepEqual(await sideways(page), [], "sideways scroll, VS Code");+ await opts.last().click();+ assert.deepEqual(await code(), { mcpServers: { magpie: { type: "http", url } } });+ assert.equal((await pane.locator(".lib-aside").textContent()).trim(), w("Its transport is Streamable HTTP. From another computer, use the address magpie is reached at there, with a gateway key: Authorization: Bearer <key>."));+ assert.deepEqual(await sideways(page), [], "sideways scroll, Other");+ assert.deepEqual(st.opened, web ? [] : [{ app: "vscode" }]);+ await sheet.locator(".bar button").click();+ await sheet.waitFor({ state: "hidden" });++ // off again+ await v.locator(".lib-hub .lib-switch").click();+ await page.waitForFunction((x) => document.querySelector("#status")?.textContent === x, w("The agents are given each server's own entry again — restart them to use it"));+ assert.deepEqual(st.sent, [{ on: true }, { on: false }]);+ await page.waitForFunction(() => document.querySelector("#view-library .lib-hub .lib-switch")?.getAttribute("aria-checked") === "false");+ assert.equal(await v.locator(".lib-hub-why").count(), 0);+ assert.equal(await v.locator(".lib-hubstats, .lib-hubat").count(), 0);++ assert.deepEqual(errors, []);+ await ctx.close();+ });+ }+ });+}diff --git a/internal/library/library.go b/internal/library/library.goindex c3edf29cb..718b17de3 100644--- a/internal/library/library.go+++ b/internal/library/library.go@@ -46,6 +46,10 @@ type Library struct {// copy (#896, skill_how.go)CopySkills bool `json:"copySkills,omitempty"`SkillHow map[string]string `json:"skillHow,omitempty"`+ // MCPHub gives each agent that can reach it magpie's one MCP server,+ // which hands each call on to the library's servers ticked for it, in+ // place of their own entries (#2679, mcp_hub.go)+ MCPHub bool `json:"mcpHub,omitempty"`// SeenSkills are, by repository and ref ("owner/repo@ref"), the// folders of skills there the page has offered: picked or not at// install, or set aside after a check. A check offers only the othersdiff --git a/internal/library/mcp_check.go b/internal/library/mcp_check.goindex 8b20799b6..9281a7cb0 100644--- a/internal/library/mcp_check.go+++ b/internal/library/mcp_check.go@@ -300,12 +300,14 @@ func request(id int, method string, params any) []byte {return b}-func initializeReq() []byte {- return request(1, "initialize", map[string]any{+func initializeReq() []byte { return request(1, "initialize", initParams()) }++func initParams() map[string]any {+ return map[string]any{"protocolVersion": mcpProtocol,"capabilities": map[string]any{},"clientInfo": map[string]any{"name": "magpie", "version": gateway.Version},- })+ }}var initializedNote = []byte(`{"jsonrpc":"2.0","method":"notifications/initialized"}`)@@ -529,17 +531,23 @@ func withPath(env []string, dir string) []string {return append(out, "PATH="+dir)}-// checkStdioAt starts the command on this machine, or in WSL distro.-func checkStdioAt(ctx context.Context, s *Server, distro string) ServerHealth {- // started as the agents start it: in the environment magpie has, with- // the server's own variables over it+// startServer starts the command s runs as the agents start it, on this+// machine or in WSL distro: in the environment magpie has, with the+// server's own variables over it, in home. Its stdin and stdout are the+// MCP connection; what it writes to stderr is kept in errs; stop ends a+// run in a distro (call it beside tree.Kill). The check starts it so, and+// so does the hub (mcp_hub.go), on this machine.+func startServer(ctx context.Context, s *Server, distro string) (tree *proc.Tree, stdin io.WriteCloser, stdout io.ReadCloser, errs *tail, stop func(), bad *ServerHealth) {+ fail := func(h ServerHealth) (*proc.Tree, io.WriteCloser, io.ReadCloser, *tail, func(), *ServerHealth) {+ return nil, nil, nil, nil, nil, &h+ }env, unset := expandAll(s.Env)if len(unset) > 0 {- return failed("novar", strings.Join(unset, ", "))+ return fail(failed("novar", strings.Join(unset, ", ")))}var cmd *exec.Cmdvar t wslrun.Tool- stop := func() {}+ stop = func() {}found := ""if distro == "" {var prog string@@ -550,11 +558,11 @@ func checkStdioAt(ctx context.Context, s *Server, distro string) ServerHealth {t, err = wslrun.In(distro, s.Command)switch {case errors.Is(err, wslrun.ErrDown):- return failed("wsldown", distro)+ return fail(failed("wsldown", distro))case errors.Is(err, wslrun.ErrNotFound):- return failed("notfound", s.Command)+ return fail(failed("notfound", s.Command))case err != nil:- return failed("start", "wsl.exe -d "+distro+": "+err.Error())+ return fail(failed("start", "wsl.exe -d "+distro+": "+err.Error()))}// its own context, ended with the check, which ends the run inside// the distro too (killing wsl.exe alone leaves it there); not the@@ -563,7 +571,6 @@ func checkStdioAt(ctx context.Context, s *Server, distro string) ServerHealth {rctx, stop = context.WithCancel(context.WithoutCancel(ctx))cmd = wslCommand(rctx, t, s.Args...)}- defer stop()// Agents start MCP servers from a project or home directory. A magpie// launched from a shortcut (Windows Start menu) often has C:\Windows\system32// as its own cwd, which is not writable; inherit that and a server that@@ -592,22 +599,36 @@ func checkStdioAt(ctx context.Context, s *Server, distro string) ServerHealth {slices.Sort(names)cmd.Env = t.Env(cmd.Env, names...)}- stdin, err := cmd.StdinPipe()+ in, err := cmd.StdinPipe()if err != nil {- return failed("start", err.Error())+ stop()+ return fail(failed("start", err.Error()))}- stdout, err := cmd.StdoutPipe()+ out, err := cmd.StdoutPipe()if err != nil {- return failed("start", err.Error())+ stop()+ return fail(failed("start", err.Error()))}- errs := &tail{}+ errs = &tail{}cmd.Stderr = errs- tree, err := proc.StartTree(cmd)+ tree, err = proc.StartTree(cmd)if err != nil {+ h := failed("start", err.Error())if errors.Is(err, exec.ErrNotFound) || errors.Is(err, os.ErrNotExist) {- return failed("notfound", s.Command)+ h = failed("notfound", s.Command)}- return failed("start", err.Error())+ stop()+ return fail(h)+ }+ return tree, in, out, errs, stop, nil+}++// checkStdioAt starts the command on this machine, or in WSL distro, and+// asks it for its tools.+func checkStdioAt(ctx context.Context, s *Server, distro string) ServerHealth {+ tree, stdin, stdout, errs, stop, bad := startServer(ctx, s, distro)+ if bad != nil {+ return *bad}var exitErr errorexited := make(chan struct{})@@ -762,107 +783,157 @@ func statusHealth(res *http.Response) ServerHealth {return h}-func checkHTTP(ctx context.Context, s *Server) ServerHealth {+// httpSession is a connection to a streamable HTTP server: its headers,+// with magpie's token when it is signed in to it, and the session the+// server gave. A check holds one for its few requests, the hub for as long+// as the server is used (mcp_hub.go).+type httpSession struct {+ s *Server++ mu sync.Mutex+ hdr http.Header+ // tok is magpie's own token, when it is signed in: one the server+ // refuses is renewed once and asked again with, as the gateway's+ // relay does, and one refused just after it was renewed is a sign-in+ // that has run out+ tok string+ renewed bool+ session, version string+}++func newHTTPSession(ctx context.Context, s *Server) (*httpSession, *ServerHealth) {hdr, bad := remoteHeaders(ctx, s)if bad != nil {- return *bad+ return nil, bad}- // magpie's own token, when it is signed in: one the server refuses is- // renewed once and asked again with, as the gateway's relay does, and- // one refused just after it was renewed is a sign-in that has run out- tok := ""+ c := &httpSession{s: s, hdr: hdr, version: mcpProtocol}if s.Transport == "http" && mcpauth.SignedIn(s.Name, s.URL) {- tok = strings.TrimPrefix(hdr.Get("Authorization"), "Bearer ")+ c.tok = strings.TrimPrefix(hdr.Get("Authorization"), "Bearer ")}- renewed := false- session, version := "", mcpProtocol- post := func(body []byte) (*http.Response, error) {- req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.URL, bytes.NewReader(body))- if err != nil {- return nil, err- }- for k, v := range hdr {- req.Header[k] = v- }- req.Header.Set("Content-Type", "application/json")- req.Header.Set("Accept", "application/json, text/event-stream")- if session != "" {- req.Header.Set("Mcp-Session-Id", session)- req.Header.Set("MCP-Protocol-Version", version)- }- return checkClient.Do(req)+ return c, nil+}++func (c *httpSession) post(ctx context.Context, body []byte) (*http.Response, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.s.URL, bytes.NewReader(body))+ if err != nil {+ return nil, err}- // call posts a request and reads its reply, as JSON or from an event- // stream- var call func(id int, body []byte) (*rpcMsg, *ServerHealth)- call = func(id int, body []byte) (*rpcMsg, *ServerHealth) {- res, err := post(body)- if err != nil {- h := netHealth(ctx, err)- return nil, &h- }- defer res.Body.Close()- if res.StatusCode == 401 && tok != "" {- if renewed {- mcpauth.Rejected(s.Name, tok)- } else {- renewed = true- io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))- fresh, err := mcpauth.Renew(ctx, s.Name, tok)- if err != nil {- return nil, &ServerHealth{State: "auth", Code: 401, OAuth: true, Detail: err.Error()}- }- tok = fresh- hdr.Set("Authorization", "Bearer "+fresh)- return call(id, body)+ c.mu.Lock()+ for k, v := range c.hdr {+ req.Header[k] = slices.Clone(v)+ }+ session, version := c.session, c.version+ c.mu.Unlock()+ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("Accept", "application/json, text/event-stream")+ if session != "" {+ req.Header.Set("Mcp-Session-Id", session)+ req.Header.Set("MCP-Protocol-Version", version)+ }+ return checkClient.Do(req)+}++// call posts a request and reads its reply, as JSON or from an event+// stream.+func (c *httpSession) call(ctx context.Context, id int, body []byte) (*rpcMsg, *ServerHealth) {+ res, err := c.post(ctx, body)+ if err != nil {+ h := netHealth(ctx, err)+ return nil, &h+ }+ defer res.Body.Close()+ c.mu.Lock()+ tok, renewed := c.tok, c.renewed+ c.mu.Unlock()+ if res.StatusCode == 401 && tok != "" {+ if renewed {+ mcpauth.Rejected(c.s.Name, tok)+ } else {+ io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))+ fresh, err := mcpauth.Renew(ctx, c.s.Name, tok)+ if err != nil {+ return nil, &ServerHealth{State: "auth", Code: 401, OAuth: true, Detail: err.Error()}}+ c.mu.Lock()+ c.renewed, c.tok = true, fresh+ c.hdr.Set("Authorization", "Bearer "+fresh)+ c.mu.Unlock()+ return c.call(ctx, id, body)}- if res.StatusCode == 401 || res.StatusCode == 403 {- h := refused(res)- return nil, &h- }- if res.StatusCode/100 != 2 {- h := statusHealth(res)- return nil, &h- }- if sid := res.Header.Get("Mcp-Session-Id"); sid != "" {- session = sid- }- m, err := replyOf(res, id)- if err != nil {- if ctx.Err() != nil {- h := timedOut(ctx)- return nil, &h- }- h := failed("protocol", err.Error())+ }+ if res.StatusCode == 401 || res.StatusCode == 403 {+ h := refused(res)+ return nil, &h+ }+ if res.StatusCode/100 != 2 {+ h := statusHealth(res)+ return nil, &h+ }+ if sid := res.Header.Get("Mcp-Session-Id"); sid != "" {+ c.mu.Lock()+ c.session = sid+ c.mu.Unlock()+ }+ m, err := replyOf(res, id)+ if err != nil {+ if ctx.Err() != nil {+ h := timedOut(ctx)return nil, &h}- return m, nil+ h := failed("protocol", err.Error())+ return nil, &h}- m, h := call(1, initializeReq())+ return m, nil+}++// initialize opens the session: initialize, and the note that it is.+func (c *httpSession) initialize(ctx context.Context, init []byte) *ServerHealth {+ m, h := c.call(ctx, 1, init)if h != nil {- return *h+ return h}if h := initError(m); h != nil {- return *h+ return h}- var init struct {+ var r struct {ProtocolVersion string `json:"protocolVersion"`}- if json.Unmarshal(m.Result, &init) == nil && init.ProtocolVersion != "" {- version = init.ProtocolVersion- }- if session != "" {- defer endSession(s.URL, hdr, session)+ if json.Unmarshal(m.Result, &r) == nil && r.ProtocolVersion != "" {+ c.mu.Lock()+ c.version = r.ProtocolVersion+ c.mu.Unlock()}stepTo(ctx, "notifications/initialized")- if res, err := post(initializedNote); err == nil {+ if res, err := c.post(ctx, initializedNote); err == nil {io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))res.Body.Close()} else if ctx.Err() != nil {- return timedOut(ctx)+ h := timedOut(ctx)+ return &h+ }+ return nil+}++// end tells the server the session is done with, when it gave one.+func (c *httpSession) end() {+ c.mu.Lock()+ hdr, session := c.hdr.Clone(), c.session+ c.mu.Unlock()+ if session != "" {+ endSession(c.s.URL, hdr, session)+ }+}++func checkHTTP(ctx context.Context, s *Server) ServerHealth {+ c, bad := newHTTPSession(ctx, s)+ if bad != nil {+ return *bad+ }+ if h := c.initialize(ctx, initializeReq()); h != nil {+ return *h}- return listTools(ctx, call)+ defer c.end()+ return listTools(ctx, func(id int, body []byte) (*rpcMsg, *ServerHealth) { return c.call(ctx, id, body) })}// endSession tells the server the check is done with its session, as adiff --git a/internal/library/mcp_check_test.go b/internal/library/mcp_check_test.goindex a7aca6152..0e2909561 100644--- a/internal/library/mcp_check_test.go+++ b/internal/library/mcp_check_test.go@@ -72,9 +72,26 @@ func TestFakeMCPServer(t *testing.T) {var m struct {ID *int `json:"id"`Method string `json:"method"`+ Params struct {+ Name string `json:"name"`+ Arguments json.RawMessage `json:"arguments"`+ } `json:"params"`}json.Unmarshal(sc.Bytes(), &m)switch m.Method {+ case "tools/call":+ // the hub's: "die" ends the server mid-call, "slow" never+ // answers, any other tool says its name, its arguments and the+ // process it ran in+ switch m.Params.Name {+ case "die":+ fmt.Fprintln(os.Stderr, "Error: lost the database")+ os.Exit(5)+ case "slow":+ continue+ }+ text, _ := json.Marshal(fmt.Sprintf("%s %s pid=%d", m.Params.Name, m.Params.Arguments, os.Getpid()))+ fmt.Printf(`{"jsonrpc":"2.0","id":%d,"result":{"content":[{"type":"text","text":%s}]}}`+"\n", *m.ID, text)case "initialize":fmt.Printf(`{"jsonrpc":"2.0","id":%d,"result":{"protocolVersion":"2025-06-18","capabilities":{"tools":{}},"serverInfo":{"name":"fake","version":"1"}}}`+"\n", *m.ID)case "notifications/initialized":@@ -263,6 +280,9 @@ func mcpHandler(t *testing.T, sse bool, seen *http.Header) http.HandlerFunc {}*seen = r.Header.Clone()reply = fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{"tools":[{"name":"a"},{"name":"b"}]}}`, *m.ID)+ case "tools/call":+ // the hub's: the tool's name back, in the session it was sent in+ reply = fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{"content":[{"type":"text","text":"remote %s"}]}}`, *m.ID, r.Header.Get("Mcp-Session-Id"))}if sse {w.Header().Set("Content-Type", "text/event-stream")diff --git a/internal/library/mcp_hub.go b/internal/library/mcp_hub.gonew file mode 100644index 000000000..5a2d03b52--- /dev/null+++ b/internal/library/mcp_hub.go@@ -0,0 +1,932 @@+package library++// The MCP hub (#2679): with the Library's switch on (MCPHub), an agent+// that can reach a streamable HTTP server is given one, magpie's own, at+// gateway.HubMCPPath<agent>, in place of the library's servers ticked for+// it; the gateway lists their tools as one server's and hands each call on+// to the server it is of (gateway/hub_mcp.go). The servers are run here:+//+// - a command is started once, for every agent, as the check starts it+// (startServer: in home, with PWD, magpie's environment with the+// login shell's PATH, the server's own variables with ${NAME}+// expanded), the first time a tool of it is listed or called; it is+// ended once no call has used it for hubIdle, and started again when+// next used, or when it ended by itself. Its stdin is magpie's: a+// magpie that dies without ending it closes it, which ends an MCP+// server, and on Windows its job ends with magpie.+// - a URL is reached as the check reaches it, with magpie's sign-in+// (httpSession), or over its event stream (SSE).+//+// magpie's own image server is never behind the hub: it asks the agent+// that started it for its roots. What a server lists is kept until it says+// its tools changed, or the server is edited.++import (+ "bufio"+ "bytes"+ "context"+ "encoding/base64"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "net/http"+ "net/url"+ "slices"+ "sort"+ "strings"+ "sync"+ "sync/atomic"+ "time"++ "github.com/yetone/magpie/internal/agent"+ "github.com/yetone/magpie/internal/gateway"+ "github.com/yetone/magpie/internal/mcpauth"+)++func init() { gateway.MCPHub = theHub{} }++// HubEntry is the name of the one entry the hub is given to an agent by.+const HubEntry = "magpie"++// hubIdle is how long a server the hub started is kept with no call.+var hubIdle = 10 * time.Minute++// SetMCPHub turns the hub on or off: on, each agent that can reach it is+// given magpie's one entry in place of its servers; off, their own entries+// again, and the servers the hub started are ended.+func SetMCPHub(on bool) (*Result, error) {+ res, err := change(func(l *Library) error {+ l.MCPHub = on+ return nil+ })+ if err == nil && !on {+ hubStopAll()+ }+ return res, err+}++// inHub is whether the hub gives s: every server but magpie's own image+// server, which asks its agent for its roots.+func inHub(s *Server) bool { return !isSelf(s) }++// hubServerFor is the entry agent t is given for the hub, at the address+// the agent reaches the gateway at, with a key when it reaches it from+// another machine (a WSL distro under NAT, a computer on the LAN).+func hubServerFor(t *Target) *Server {+ base := strings.TrimRight(gatewayOf(t), "/")+ s := &Server{Name: HubEntry, Transport: "http", URL: base + gateway.HubMCPPath + url.PathEscape(t.Agent.ID)}+ if k := agent.KeyAt(base); k != gateway.Token {+ s.Headers = map[string]string{"Authorization": "Bearer " + k}+ }+ return s+}++// HubLink is the link that adds the hub to app, at this computer's+// gateway (HubMCPAll, which needs no key here), through the install link+// its vendor documents: Cursor's cursor://anysphere.cursor-deeplink/mcp/install+// (docs.cursor.com, "MCP install links") and VS Code's vscode:mcp/install+// (code.visualstudio.com, "Add an MCP server": URL handler). Each app asks+// its user before it adds the server.+func HubLink(app string) (string, error) {+ u := strings.TrimRight(gateway.URL(), "/") + gateway.HubMCPAll+ switch app {+ case "cursor":+ cfg, _ := json.Marshal(map[string]string{"url": u})+ return "cursor://anysphere.cursor-deeplink/mcp/install?name=" + HubEntry + "&config=" + base64.StdEncoding.EncodeToString(cfg), nil+ case "vscode":+ cfg, _ := json.Marshal(map[string]string{"name": HubEntry, "type": "http", "url": u})+ return "vscode:mcp/install?" + url.QueryEscape(string(cfg)), nil+ }+ return "", fmt.Errorf("magpie has no install link for %q", app)+}++// Why an agent with the hub on keeps each server's own entry (AgentView's+// HubWhy).+const (+ hubNoRemote = "no-remote" // it reaches no server over HTTP (Claude Desktop)+ hubTaken = "taken" // a library server is called magpie+ hubOwn = "own" // the agent has an entry called magpie of its own+)++// hubFor is the hub's entry for t, or nil and why it isn't given one: the+// hub is off, the agent has none of the hub's servers, or it can't take+// the entry (why).+func (l *Library) hubFor(t *Target, a *Applied, entries map[string]map[string]any) (*Server, string) {+ if !l.MCPHub || t.MCP == nil {+ return nil, ""+ }+ id := t.Agent.ID+ if !slices.ContainsFunc(l.MCP, func(s *Server) bool { return slices.Contains(s.Agents, id) && inHub(s) }) {+ return nil, ""+ }+ hs := hubServerFor(t)+ if err := t.MCP.supports(hs); err != nil {+ return nil, hubNoRemote+ }+ if l.server(HubEntry) != nil {+ return nil, hubTaken+ }+ if _, ok := entries[HubEntry]; ok && !slices.Contains(a.MCP, HubEntry) {+ return nil, hubOwn+ }+ if err := t.MCP.foreign(entries[HubEntry]); err != nil {+ return nil, hubOwn+ }+ return hs, ""+}++// ---- what the gateway asks -------------------------------------------------++type theHub struct{}++func (theHub) On() bool {+ mu.Lock()+ l, err := load()+ mu.Unlock()+ return err == nil && l.MCPHub+}++// hubServers are the servers the hub gives agent, by name: for "" (the+// address any other app is given), every server in the library.+func hubServers(agentID string) []*Server {+ mu.Lock()+ l, err := load()+ mu.Unlock()+ if err != nil || !l.MCPHub {+ return nil+ }+ var out []*Server+ for _, s := range l.MCP {+ if (agentID == "" || slices.Contains(s.Agents, agentID)) && inHub(s) {+ out = append(out, s)+ }+ }+ sort.SliceStable(out, func(i, j int) bool { return out[i].Name < out[j].Name })+ return out+}++func (theHub) Tools(ctx context.Context, agentID string) []gateway.HubTool {+ ss := hubServers(agentID)+ lists := make([][]gateway.HubTool, len(ss))+ var wg sync.WaitGroup+ for i, s := range ss {+ wg.Add(1)+ go func() {+ defer wg.Done()+ ts, err := backendOf(s).tools(ctx)+ if err != nil {+ return+ }+ for _, t := range ts {+ var name string+ if json.Unmarshal(t["name"], &name) != nil || name == "" || slices.Contains(s.DisabledTools, name) {+ continue+ }+ lists[i] = append(lists[i], gateway.HubTool{Server: s.Name, Tool: name, Spec: t})+ }+ }()+ }+ wg.Wait()+ var out []gateway.HubTool+ for _, l := range lists {+ out = append(out, l...)+ }+ return out+}++func (theHub) Call(ctx context.Context, agentID, server, tool string, args json.RawMessage) (json.RawMessage, error) {+ ss := hubServers(agentID)+ i := slices.IndexFunc(ss, func(s *Server) bool { return s.Name == server })+ if i < 0 && agentID == "" {+ return nil, errors.New("it isn't in magpie's Library")+ }+ if i < 0 {+ return nil, fmt.Errorf("it isn't switched on for %s in magpie's Library", agentID)+ }+ s := ss[i]+ if slices.Contains(s.DisabledTools, tool) {+ return nil, errors.New("the tool is turned off in magpie's Library")+ }+ return backendOf(s).call(ctx, tool, args)+}++func (theHub) Stop() { hubStopAll() }++func (theHub) Prune() { hubPrune() }++// hubPrune ends each server the hub holds that the library no longer+// gives as it was started: the hub off, the server gone or edited. One+// ticked for no agent is still given to other apps (HubMCPAll), and ends+// when idle. A library that can't be read ends nothing.+func hubPrune() {+ hubPool.Lock()+ held := len(hubPool.m)+ hubPool.Unlock()+ if held == 0 {+ return+ }+ mu.Lock()+ l, err := load()+ mu.Unlock()+ if err != nil {+ return+ }+ given := map[string]string{}+ if l.MCPHub {+ for _, s := range l.MCP {+ if inHub(s) {+ given[s.Name] = hubKey(s)+ }+ }+ }+ var gone []*backend+ hubPool.Lock()+ for name, b := range hubPool.m {+ if k, ok := given[name]; !ok || k != b.key {+ gone = append(gone, b)+ delete(hubPool.m, name)+ }+ }+ hubPool.Unlock()+ for _, b := range gone {+ b.stop()+ }+}++// ---- the servers it runs -----------------------------------------------------++// hubPool is each server the hub uses, by name.+var hubPool = struct {+ sync.Mutex+ m map[string]*backend+}{m: map[string]*backend{}}++// hubKey is the server as it is run: an edit, or a sign-in since, is+// another to start.+func hubKey(s *Server) string {+ c := *s+ c.Agents, c.DisabledTools = nil, nil+ b, _ := json.Marshal(c)+ return fmt.Sprintf("%s|%v", b, s.Transport == "http" && mcpauth.SignedIn(s.Name, s.URL))+}++func backendOf(s *Server) *backend {+ key := hubKey(s)+ hubPool.Lock()+ defer hubPool.Unlock()+ b := hubPool.m[s.Name]+ if b != nil && b.key != key {+ go b.stop()+ b = nil+ }+ if b == nil {+ c := *s+ b = &backend{s: &c, key: key}+ hubPool.m[s.Name] = b+ }+ return b+}++// hubStopAll ends every server the hub started, and waits for them.+func hubStopAll() {+ hubPool.Lock()+ all := hubPool.m+ hubPool.m = map[string]*backend{}+ hubPool.Unlock()+ var wg sync.WaitGroup+ for _, b := range all {+ wg.Add(1)+ go func() { defer wg.Done(); b.stop() }()+ }+ wg.Wait()+}++// hubConn is a connection to a server the hub holds.+type hubConn interface {+ ask(ctx context.Context, method string, params any) (*rpcMsg, error)+ alive() bool+ close()+}++// backend is one server the hub uses: its connection while it has one,+// and the tools it last listed.+type backend struct {+ s *Server+ key string++ mu sync.Mutex+ conn hubConn+ ready chan struct{} // while it is being connected to+ err error // why the last connection failed+ list []map[string]json.RawMessage+ listed bool+ busy int+ idle *time.Timer+ stopped bool+}++var errHubStopped = errors.New("magpie stopped it")++// connect is the connection to the server, made when there is none: a+// command started, a URL initialized. A start goes on when ctx ends, for+// the next to use.+func (b *backend) connect(ctx context.Context) (hubConn, error) {+ b.mu.Lock()+ if b.stopped {+ b.mu.Unlock()+ return nil, errHubStopped+ }+ if b.conn != nil && !b.conn.alive() {+ go b.conn.close()+ b.conn = nil+ }+ if b.conn != nil {+ c := b.conn+ b.mu.Unlock()+ return c, nil+ }+ if b.ready == nil {+ b.ready = make(chan struct{})+ go b.start(b.ready)+ }+ ready := b.ready+ b.mu.Unlock()+ select {+ case <-ready:+ case <-ctx.Done():+ return nil, ctx.Err()+ }+ b.mu.Lock()+ defer b.mu.Unlock()+ if b.conn != nil {+ return b.conn, nil+ }+ if b.err == nil {+ return nil, errHubStopped+ }+ return nil, b.err+}++func (b *backend) start(ready chan struct{}) {+ each := remoteStep+ if b.s.Transport == "stdio" {+ each = stdioStep+ }+ ctx, cancel := context.WithTimeout(context.Background(), each)+ defer cancel()+ c, err := dialHub(ctx, b.s, b.relist)+ b.mu.Lock()+ defer b.mu.Unlock()+ if b.stopped && c != nil {+ go c.close()+ c = nil+ }+ b.conn, b.err, b.ready = c, err, nil+ close(ready)+}++// relist forgets the tools the server listed: it said they changed.+func (b *backend) relist() {+ b.mu.Lock()+ b.listed = false+ b.mu.Unlock()+}++// use and done count the calls using the server; it is ended hubIdle after+// the last.+func (b *backend) use() {+ b.mu.Lock()+ b.busy+++ if b.idle != nil {+ b.idle.Stop()+ b.idle = nil+ }+ b.mu.Unlock()+}++func (b *backend) done() {+ b.mu.Lock()+ defer b.mu.Unlock()+ b.busy--+ if b.busy > 0 || b.stopped {+ return+ }+ var t *time.Timer+ t = time.AfterFunc(hubIdle, func() {+ b.mu.Lock()+ if b.idle != t || b.busy > 0 || b.conn == nil {+ b.mu.Unlock()+ return+ }+ c := b.conn+ b.conn, b.idle = nil, nil+ b.mu.Unlock()+ c.close()+ })+ b.idle = t+}++// stop ends the server for good: it was edited, the hub turned off, or+// magpie is stopping.+func (b *backend) stop() {+ b.mu.Lock()+ b.stopped = true+ if b.idle != nil {+ b.idle.Stop()+ b.idle = nil+ }+ c := b.conn+ b.conn = nil+ b.mu.Unlock()+ if c != nil {+ c.close()+ }+}++// tools are the tools the server lists, every page of them.+func (b *backend) tools(ctx context.Context) ([]map[string]json.RawMessage, error) {+ b.use()+ defer b.done()+ b.mu.Lock()+ if b.listed {+ ts := b.list+ b.mu.Unlock()+ return ts, nil+ }+ b.mu.Unlock()+ c, err := b.connect(ctx)+ if err != nil {+ return nil, err+ }+ var all []map[string]json.RawMessage+ cursor := ""+ for page := 0; page < maxToolPages; page++ {+ var params any+ if cursor != "" {+ params = map[string]any{"cursor": cursor}+ }+ m, err := c.ask(ctx, "tools/list", params)+ if err != nil {+ return nil, err+ }+ if m.Error != nil {+ if m.Error.Code == -32601 || page > 0 {+ break+ }+ return nil, errors.New("tools/list: " + m.Error.Message)+ }+ var r struct {+ Tools []map[string]json.RawMessage `json:"tools"`+ NextCursor string `json:"nextCursor"`+ }+ if err := json.Unmarshal(m.Result, &r); err != nil {+ return nil, errors.New("tools/list: " + err.Error())+ }+ all = append(all, r.Tools...)+ if r.NextCursor == "" || r.NextCursor == cursor {+ break+ }+ cursor = r.NextCursor+ }+ b.mu.Lock()+ b.list, b.listed = all, true+ b.mu.Unlock()+ return all, nil+}++// call runs one tool, and is its result as the server gave it.+func (b *backend) call(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, error) {+ b.use()+ defer b.done()+ c, err := b.connect(ctx)+ if err != nil {+ return nil, err+ }+ m, err := c.ask(ctx, "tools/call", map[string]any{"name": tool, "arguments": args})+ if err != nil {+ return nil, err+ }+ if m.Error != nil {+ return nil, fmt.Errorf("%s (%d)", m.Error.Message, m.Error.Code)+ }+ return m.Result, nil+}++// healthErr is a check's finding as the hub tells an agent.+func healthErr(h ServerHealth) error {+ var s string+ switch {+ case h.State == "auth":+ s = "it asks for a sign-in or a key: sign in to it, or give it its key, in magpie's Library"+ case h.Why == "notfound":+ s = "its command " + h.Detail + " wasn't found"+ case h.Why == "start":+ s = "it couldn't be started: " + h.Detail+ case h.Why == "exited":+ s = "it exited"+ if h.Code != 0 {+ s += fmt.Sprintf(" (%d)", h.Code)+ }+ if h.Detail != "" {+ s += ": " + h.Detail+ }+ case h.Why == "timeout":+ s = "it didn't answer in time"+ if h.Step != "" {+ s = "it didn't answer " + h.Step + " in time"+ }+ case h.Why == "http":+ s = "it answered " + h.Detail+ case h.Why == "refused" || h.Why == "unreachable":+ s = "it couldn't be reached: " + h.Detail+ case h.Why == "novar":+ s = h.Detail + " isn't set where magpie runs"+ default:+ s = h.Detail+ }+ return errors.New(s)+}++// dialHub connects to s, initialized, for the hub; changed is called when+// it says its tools changed.+func dialHub(ctx context.Context, s *Server, changed func()) (hubConn, error) {+ var c *streamConn+ var err error+ switch s.Transport {+ case "stdio":+ c, err = dialStdio(s, changed)+ case "sse":+ c, err = dialSSE(ctx, s, changed)+ default:+ hc, err := dialHTTP(ctx, s)+ if err != nil {+ return nil, err+ }+ return hc, nil+ }+ if err != nil {+ return nil, err+ }+ m, err := c.ask(ctx, "initialize", initParams())+ if err == nil && m.Error != nil {+ err = healthErr(*initError(m))+ }+ if err != nil {+ c.close()+ return nil, err+ }+ if err := c.send(ctx, initializedNote); err != nil {+ c.close()+ return nil, err+ }+ return c, nil+}++// waiters are the requests sent on a stream (a command's stdout, an SSE+// stream) waiting for their replies, by id.+type waiters struct {+ mu sync.Mutex+ next int+ wait map[string]chan *rpcMsg+ gone error+}++func (w *waiters) open() (int, chan *rpcMsg, error) {+ w.mu.Lock()+ defer w.mu.Unlock()+ if w.gone != nil {+ return 0, nil, w.gone+ }+ w.next+++ ch := make(chan *rpcMsg, 1)+ if w.wait == nil {+ w.wait = map[string]chan *rpcMsg{}+ }+ w.wait[fmt.Sprint(w.next)] = ch+ return w.next, ch, nil+}++func (w *waiters) drop(id int) {+ w.mu.Lock()+ delete(w.wait, fmt.Sprint(id))+ w.mu.Unlock()+}++func (w *waiters) deliver(m *rpcMsg) {+ id := string(bytes.TrimSpace(m.ID))+ w.mu.Lock()+ ch := w.wait[id]+ delete(w.wait, id)+ w.mu.Unlock()+ if ch != nil {+ ch <- m+ }+}++// end fails every request waiting, and those after: the stream ended.+func (w *waiters) end(err error) {+ w.mu.Lock()+ defer w.mu.Unlock()+ if w.gone != nil {+ return+ }+ w.gone = err+ for id, ch := range w.wait {+ close(ch)+ delete(w.wait, id)+ }+}++func (w *waiters) err() error {+ w.mu.Lock()+ defer w.mu.Unlock()+ return w.gone+}++// streamConn is a connection whose replies come on a stream: a command's+// stdout or an SSE stream. send writes a message to the server.+type streamConn struct {+ w waiters+ send func(ctx context.Context, b []byte) error+ stopped func()+ once sync.Once+}++// read takes one message off the stream: a reply is its request's, a+// request of the server's own is answered (ping; nothing else is offered),+// a note that its tools changed has them listed again.+func (c *streamConn) read(b []byte, changed func()) {+ var m rpcMsg+ if json.Unmarshal(b, &m) != nil {+ // a line that isn't JSON-RPC is a server logging to the wrong+ // stream; agents pass over it too+ return+ }+ id := bytes.TrimSpace(m.ID)+ switch {+ case m.Method != "" && len(id) > 0 && string(id) != "null":+ reply := map[string]any{"jsonrpc": "2.0", "id": json.RawMessage(id)}+ if m.Method == "ping" {+ reply["result"] = map[string]any{}+ } else {+ reply["error"] = map[string]any{"code": -32601, "message": "magpie's hub doesn't offer " + m.Method}+ }+ out, _ := json.Marshal(reply)+ go func() {+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()+ _ = c.send(ctx, out)+ }()+ case m.Method == "notifications/tools/list_changed":+ changed()+ case m.Method == "":+ c.w.deliver(&m)+ }+}++func (c *streamConn) ask(ctx context.Context, method string, params any) (*rpcMsg, error) {+ id, ch, err := c.w.open()+ if err != nil {+ return nil, err+ }+ defer c.w.drop(id)+ if err := c.send(ctx, request(id, method, params)); err != nil {+ if gone := c.w.err(); gone != nil {+ return nil, gone+ }+ return nil, err+ }+ select {+ case m, ok := <-ch:+ if !ok {+ return nil, c.w.err()+ }+ return m, nil+ case <-ctx.Done():+ // the server can stop working on it+ note, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": map[string]any{"requestId": id, "reason": "the agent's call ended"}})+ go func() {+ c2, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancel()+ _ = c.send(c2, note)+ }()+ return nil, errors.New("it didn't answer in time")+ }+}++func (c *streamConn) alive() bool { return c.w.err() == nil }++func (c *streamConn) close() {+ c.once.Do(func() {+ c.w.end(errHubStopped)+ c.stopped()+ })+}++// dialStdio starts the command s runs, as the check starts it.+func dialStdio(s *Server, changed func()) (*streamConn, error) {+ // the hub's server lives until the hub ends it, not with a request+ tree, stdin, stdout, errs, stop, bad := startServer(context.Background(), s, "")+ if bad != nil {+ return nil, healthErr(*bad)+ }+ var exitErr error+ exited := make(chan struct{})+ go func() { exitErr = tree.Wait(); close(exited) }()+ var wmu sync.Mutex+ c := &streamConn{}+ c.send = func(_ context.Context, b []byte) error {+ wmu.Lock()+ defer wmu.Unlock()+ _, err := stdin.Write(append(b, '\n'))+ return err+ }+ c.stopped = func() {+ // asked to end first, as an agent leaving it does: its stdin+ // closed; then its tree, whatever it started+ stdin.Close()+ select {+ case <-exited:+ case <-time.After(time.Second):+ }+ select {+ case <-tree.Stop(2 * time.Second):+ case <-time.After(5 * time.Second):+ }+ stop()+ }+ go func() {+ sc := bufio.NewScanner(stdout)+ sc.Buffer(make([]byte, 64*1024), 16<<20)+ for sc.Scan() {+ c.read(sc.Bytes(), changed)+ }+ // its output ended: it is ending, and says why once it has+ select {+ case <-exited:+ c.w.end(healthErr(exitHealth(exitErr, errs)))+ case <-time.After(2 * time.Second):+ c.w.end(errors.New("it closed its output: " + errs.last()))+ }+ }()+ return c, nil+}++// dialSSE opens s's event stream, which names where to post to.+func dialSSE(ctx context.Context, s *Server, changed func()) (*streamConn, error) {+ hdr, bad := remoteHeaders(ctx, s)+ if bad != nil {+ return nil, healthErr(*bad)+ }+ stream, cancel := context.WithCancel(context.Background())+ // the stream outlives the dial, which only bounds its start+ unbind := context.AfterFunc(ctx, cancel)+ req, err := http.NewRequestWithContext(stream, http.MethodGet, s.URL, nil)+ if err != nil {+ cancel()+ return nil, err+ }+ for k, v := range hdr {+ req.Header[k] = v+ }+ req.Header.Set("Accept", "text/event-stream")+ res, err := checkClient.Do(req)+ if err != nil {+ cancel()+ return nil, healthErr(netHealth(ctx, err))+ }+ if res.StatusCode/100 != 2 {+ res.Body.Close()+ cancel()+ if res.StatusCode == 401 || res.StatusCode == 403 {+ return nil, healthErr(refused(res))+ }+ return nil, healthErr(statusHealth(res))+ }+ events := sseEvents(res.Body, stream.Done())+ var endpoint *url.URL+ for endpoint == nil {+ e, ok := <-events+ if !ok {+ res.Body.Close()+ cancel()+ if ctx.Err() != nil {+ return nil, errors.New("it didn't open its event stream in time")+ }+ return nil, errors.New("its event stream ended")+ }+ if e.name == "endpoint" {+ base, _ := url.Parse(s.URL)+ if endpoint, err = base.Parse(strings.TrimSpace(e.data)); err != nil {+ res.Body.Close()+ cancel()+ return nil, errors.New("endpoint: " + err.Error())+ }+ }+ }+ unbind()+ c := &streamConn{}+ c.send = func(ctx context.Context, b []byte) error {+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(b))+ if err != nil {+ return err+ }+ for k, v := range hdr {+ req.Header[k] = v+ }+ req.Header.Set("Content-Type", "application/json")+ res, err := checkClient.Do(req)+ if err != nil {+ return healthErr(netHealth(ctx, err))+ }+ io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))+ res.Body.Close()+ if res.StatusCode/100 != 2 {+ if res.StatusCode == 401 || res.StatusCode == 403 {+ return healthErr(refused(res))+ }+ return healthErr(statusHealth(res))+ }+ return nil+ }+ c.stopped = func() { cancel(); res.Body.Close() }+ go func() {+ for e := range events {+ if e.name == "" || e.name == "message" {+ c.read([]byte(e.data), changed)+ }+ }+ c.w.end(errors.New("its event stream ended"))+ }()+ return c, nil+}++// httpConn is a streamable HTTP server's session, held by the hub.+type httpConn struct {+ c *httpSession+ ids atomic.Int64+ // reopen is held while a session the server forgot is opened again+ reopen sync.Mutex+}++func dialHTTP(ctx context.Context, s *Server) (*httpConn, error) {+ c, bad := newHTTPSession(ctx, s)+ if bad != nil {+ return nil, healthErr(*bad)+ }+ if h := c.initialize(ctx, initializeReq()); h != nil {+ return nil, healthErr(*h)+ }+ hc := &httpConn{c: c}+ hc.ids.Store(1)+ return hc, nil+}++func (h *httpConn) ask(ctx context.Context, method string, params any) (*rpcMsg, error) {+ for try := 0; ; try++ {+ id := int(h.ids.Add(1))+ h.c.mu.Lock()+ session := h.c.session+ h.c.mu.Unlock()+ m, bad := h.c.call(ctx, id, request(id, method, params))+ if bad == nil {+ // a token renewed and taken is one to renew again next time+ // it runs out+ h.c.mu.Lock()+ h.c.renewed = false+ h.c.mu.Unlock()+ return m, nil+ }+ // a session the server no longer knows (it restarted) is opened+ // again, once+ if bad.Code == http.StatusNotFound && session != "" && try == 0 {+ h.reopen.Lock()+ h.c.mu.Lock()+ same := h.c.session == session+ if same {+ h.c.session = ""+ }+ h.c.mu.Unlock()+ var again *ServerHealth+ if same {+ again = h.c.initialize(ctx, initializeReq())+ }+ h.reopen.Unlock()+ if again != nil {+ return nil, healthErr(*again)+ }+ continue+ }+ return nil, healthErr(*bad)+ }+}++func (h *httpConn) alive() bool { return true }++func (h *httpConn) close() { h.c.end() }++var (+ _ hubConn = (*streamConn)(nil)+ _ hubConn = (*httpConn)(nil)+)diff --git a/internal/library/mcp_hub_test.go b/internal/library/mcp_hub_test.gonew file mode 100644index 000000000..f3fac5c51--- /dev/null+++ b/internal/library/mcp_hub_test.go@@ -0,0 +1,526 @@+package library++import (+ "context"+ "encoding/base64"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "net/url"+ "path/filepath"+ "slices"+ "strings"+ "testing"+ "time"++ "github.com/yetone/magpie/internal/access"+ "github.com/yetone/magpie/internal/agent"+ "github.com/yetone/magpie/internal/gateway"+)++// hubSandbox is a sandbox with the hub's servers ended when the test is.+func hubSandbox(t *testing.T) string {+ h := sandbox(t)+ t.Cleanup(hubStopAll)+ return h+}++func hubText(t *testing.T, res json.RawMessage) string {+ t.Helper()+ var r struct {+ Content []struct{ Text string } `json:"content"`+ }+ if err := json.Unmarshal(res, &r); err != nil || len(r.Content) == 0 {+ t.Fatalf("result %s: %v", res, err)+ }+ return r.Content[0].Text+}++func hubNames(ts []gateway.HubTool) []string {+ var out []string+ for _, t := range ts {+ out = append(out, t.Server+"/"+t.Tool)+ }+ slices.Sort(out)+ return out+}++// The hub lists the tools of the servers ticked for an agent, but those+// turned off, and runs each call on the server it is of: a command once+// for every agent, a URL in its session.+func TestHubToolsAndCalls(t *testing.T) {+ hubSandbox(t)+ var seen http.Header+ srv := httptest.NewServer(mcpHandler(t, false, &seen))+ t.Cleanup(srv.Close)+ fake := fakeStdio(t, "ok", nil)+ fake.Agents = []string{"claude", "codex"}+ fake.DisabledTools = []string{"b"}+ ok(t)(SaveServer("", *fake))+ ok(t)(SaveServer("", Server{Name: "web", Transport: "http", URL: srv.URL, Agents: []string{"claude"}}))+ ctx := context.Background()+ h := theHub{}++ if h.On() || len(h.Tools(ctx, "claude")) != 0 {+ t.Fatal("the hub lists tools while it is off")+ }+ ok(t)(SetMCPHub(true))+ if !h.On() {+ t.Fatal("the hub isn't on")+ }+ if got, want := hubNames(h.Tools(ctx, "claude")), []string{"fake/a", "fake/c", "web/a", "web/b"}; !slices.Equal(got, want) {+ t.Fatalf("claude's tools %v, want %v (fake's b is turned off)", got, want)+ }+ if got, want := hubNames(h.Tools(ctx, "codex")), []string{"fake/a", "fake/c"}; !slices.Equal(got, want) {+ t.Fatalf("codex's tools %v, want %v", got, want)+ }+ if got := h.Tools(ctx, "gemini"); len(got) != 0 {+ t.Fatalf("gemini, given no server, lists %v", got)+ }+ // any other app (agent "") is given every server in the library, one+ // ticked for no agent too+ ok(t)(SaveServer("", Server{Name: "lone", Transport: "http", URL: srv.URL}))+ if got, want := hubNames(h.Tools(ctx, "")), []string{"fake/a", "fake/c", "lone/a", "lone/b", "web/a", "web/b"}; !slices.Equal(got, want) {+ t.Fatalf("another app's tools %v, want %v", got, want)+ }+ if got := hubNames(h.Tools(ctx, "claude")); slices.Contains(got, "lone/a") {+ t.Fatalf("claude is given lone, ticked for no agent: %v", got)+ }+ if res, err := h.Call(ctx, "", "lone", "b", json.RawMessage(`{}`)); err != nil || !strings.HasPrefix(hubText(t, res), "remote ") {+ t.Fatalf("lone's b for another app: %s %v", res, err)+ }+ if _, err := h.Call(ctx, "", "nope", "a", json.RawMessage(`{}`)); err == nil || err.Error() != "it isn't in magpie's Library" {+ t.Errorf("a server not in the library, for another app: %v", err)+ }+ if _, err := h.Call(ctx, "", "fake", "b", json.RawMessage(`{}`)); err == nil || !strings.Contains(err.Error(), "turned off") {+ t.Errorf("a tool turned off ran for another app: %v", err)+ }+ ok(t)(RemoveServer("lone"))++ res, err := h.Call(ctx, "claude", "fake", "a", json.RawMessage(`{"x":1}`))+ if err != nil {+ t.Fatal(err)+ }+ first := hubText(t, res)+ if !strings.HasPrefix(first, `a {"x":1} pid=`) {+ t.Fatalf("fake's a answered %q", first)+ }+ // one process for every agent+ res, err = h.Call(ctx, "codex", "fake", "c", json.RawMessage(`{}`))+ if err != nil {+ t.Fatal(err)+ }+ if pid := strings.SplitAfter(hubText(t, res), "pid=")[1]; pid != strings.SplitAfter(first, "pid=")[1] {+ t.Errorf("codex's call ran in pid %s, claude's in %s: not one server for both", pid, first)+ }+ res, err = h.Call(ctx, "claude", "web", "b", json.RawMessage(`{}`))+ if err != nil || hubText(t, res) != "remote s-1" {+ t.Fatalf("web's b: %s %v", res, err)+ }+ if _, err := h.Call(ctx, "claude", "fake", "b", json.RawMessage(`{}`)); err == nil || !strings.Contains(err.Error(), "turned off") {+ t.Errorf("a tool turned off ran: %v", err)+ }+ if _, err := h.Call(ctx, "codex", "web", "a", json.RawMessage(`{}`)); err == nil || !strings.Contains(err.Error(), "isn't switched on for codex") {+ t.Errorf("a server not ticked for codex ran: %v", err)+ }++ // off, nothing is listed and the servers are ended+ ok(t)(SetMCPHub(false))+ if len(h.Tools(ctx, "claude")) != 0 {+ t.Error("the hub lists tools after it was turned off")+ }+ hubPool.Lock()+ n := len(hubPool.m)+ hubPool.Unlock()+ if n != 0 {+ t.Errorf("%d servers still held after the hub was turned off", n)+ }+}++// A server that dies, or doesn't answer, is an error naming why, and the+// next call starts it again; one left unused is ended, and started again+// when next called.+func TestHubServerDiesAndIdles(t *testing.T) {+ hubSandbox(t)+ fake := fakeStdio(t, "ok", nil)+ fake.Agents = []string{"claude"}+ ok(t)(SaveServer("", *fake))+ ok(t)(SetMCPHub(true))+ h := theHub{}+ ctx := context.Background()+ pid := func() string {+ t.Helper()+ res, err := h.Call(ctx, "claude", "fake", "a", json.RawMessage(`{}`))+ if err != nil {+ t.Fatal(err)+ }+ return strings.SplitAfter(hubText(t, res), "pid=")[1]+ }+ p1 := pid()++ _, err := h.Call(ctx, "claude", "fake", "die", json.RawMessage(`{}`))+ if err == nil || !strings.Contains(err.Error(), "exited (5)") || !strings.Contains(err.Error(), "lost the database") {+ t.Fatalf("a server that died answered %v, want its exit and last line", err)+ }+ p2 := pid()+ if p2 == p1 {+ t.Fatalf("the call after it died ran in the dead server's pid %s", p1)+ }++ // a call it never answers ends at the caller's deadline, and the+ // server is still used after+ short, cancel := context.WithTimeout(ctx, 300*time.Millisecond)+ _, err = h.Call(short, "claude", "fake", "slow", json.RawMessage(`{}`))+ cancel()+ if err == nil || !strings.Contains(err.Error(), "didn't answer in time") {+ t.Fatalf("a call never answered gave %v", err)+ }+ if p := pid(); p != p2 {+ t.Errorf("a call that timed out restarted the server (%s, then %s)", p2, p)+ }++ // idle: ended, then started again+ idle := hubIdle+ hubIdle = 50 * time.Millisecond+ t.Cleanup(func() { hubIdle = idle })+ if len(h.Tools(ctx, "claude")) != 3 {+ t.Fatal("fake's tools not listed")+ }+ pid()+ b := backendOf(fake)+ for end := time.Now().Add(10 * time.Second); ; {+ b.mu.Lock()+ gone := b.conn == nil+ b.mu.Unlock()+ if gone {+ break+ }+ if time.Now().After(end) {+ t.Fatal("an idle server wasn't ended")+ }+ time.Sleep(10 * time.Millisecond)+ }+ // what it listed is kept: a list doesn't start it again+ hubIdle = idle+ if len(h.Tools(ctx, "claude")) != 3 {+ t.Fatal("fake's tools not listed after the idle stop")+ }+ b.mu.Lock()+ started := b.conn != nil || b.ready != nil+ b.mu.Unlock()+ if started {+ t.Error("listing the tools started an idle server again")+ }+ if p := pid(); p == p2 {+ t.Errorf("the call after the idle stop ran in the ended server's pid %s", p)+ }+}++// With the hub on, an agent is given magpie's one entry in place of the+// servers behind it; off, each server's own again. An agent with a+// "magpie" of its own, or one that can't reach a URL, keeps each server's.+func TestHubEntrySwap(t *testing.T) {+ h := hubSandbox(t)+ claude := filepath.Join(h, ".claude.json")+ write(t, claude, `{"numStartups": 3, "mcpServers": {"mine": {"command": "x"}}}`)+ codex := filepath.Join(h, ".codex/config.toml")+ write(t, codex, "[mcp_servers.magpie]\ncommand = \"their-own\"\n")+ ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "fs"}, Agents: []string{"claude", "codex"}}))+ ok(t)(SaveServer("", Server{Name: "web", Transport: "http", URL: "https://example.com/mcp", Agents: []string{"claude"}}))+ entries := func(id string) map[string]map[string]any {+ t.Helper()+ e, err := targetByID(id).MCP.entries()+ if err != nil {+ t.Fatal(err)+ }+ return e+ }+ names := func(id string) []string {+ var out []string+ for k := range entries(id) {+ out = append(out, k)+ }+ slices.Sort(out)+ return out+ }+ if got := names("claude"); !slices.Equal(got, []string{"fs", "mine", "web"}) {+ t.Fatalf("claude before: %v", got)+ }++ r, err := SetMCPHub(true)+ if err != nil {+ t.Fatal(err)+ }+ for _, p := range r.Problems {+ if p.Agent == "claude" {+ t.Errorf("problem: %+v", p)+ }+ }+ if got := names("claude"); !slices.Equal(got, []string{"magpie", "mine"}) {+ t.Fatalf("claude with the hub on: %v\n%s", got, read(t, claude))+ }+ if u, _ := entries("claude")["magpie"]["url"].(string); !strings.HasSuffix(u, gateway.HubMCPPath+"claude") {+ t.Errorf("claude's magpie entry: %v", entries("claude")["magpie"])+ }+ // codex has a magpie of its own: left as it is, its servers kept+ if got := names("codex"); !slices.Equal(got, []string{"fs", "magpie"}) || entries("codex")["magpie"]["command"] != "their-own" {+ t.Fatalf("codex with the hub on: %v\n%s", got, read(t, codex))+ }+ v, err := Read(nil)+ if err != nil {+ t.Fatal(err)+ }+ if !v.MCPHub || v.HubPath != gateway.HubMCPPath {+ t.Errorf("view: mcpHub %v, hubPath %q", v.MCPHub, v.HubPath)+ }+ for _, a := range v.Agents {+ switch a.ID {+ case "claude":+ if a.Hub != "on" {+ t.Errorf("claude's hub %q, want on", a.Hub)+ }+ case "codex":+ if a.Hub != hubOwn {+ t.Errorf("codex's hub %q, want %q", a.Hub, hubOwn)+ }+ }+ }++ ok(t)(SetMCPHub(false))+ if got := names("claude"); !slices.Equal(got, []string{"fs", "mine", "web"}) {+ t.Fatalf("claude with the hub off again: %v\n%s", got, read(t, claude))+ }+ if !strings.Contains(read(t, claude), `"numStartups": 3`) {+ t.Errorf("claude's own keys lost:\n%s", read(t, claude))+ }+ if got := names("codex"); !slices.Equal(got, []string{"fs", "magpie"}) || entries("codex")["magpie"]["command"] != "their-own" {+ t.Fatalf("codex with the hub off: %v\n%s", got, read(t, codex))+ }+}++// An agent given none of the hub's servers has no magpie entry and no+// state; the Connect sheet's Connect gives it every server+// (EveryServerAgents, servers/agents-all), and it then has the hub's one+// entry and says on.+func TestHubConnectAnAgent(t *testing.T) {+ h := hubSandbox(t)+ claude := filepath.Join(h, ".claude.json")+ write(t, claude, `{"mcpServers": {}}`)+ ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "fs"}}))+ ok(t)(SetMCPHub(true))+ hub := func() string {+ t.Helper()+ v, err := Read(nil)+ if err != nil {+ t.Fatal(err)+ }+ for _, a := range v.Agents {+ if a.ID == "claude" {+ return a.Hub+ }+ }+ t.Fatal("no claude in the view")+ return ""+ }+ if got := hub(); got != "" {+ t.Fatalf("claude given nothing: hub %q", got)+ }+ if strings.Contains(read(t, claude), `"magpie"`) {+ t.Fatalf("claude given nothing has magpie:\n%s", read(t, claude))+ }+ ok(t)(EveryServerAgents([]string{"claude"}, true))+ if got := hub(); got != "on" {+ t.Fatalf("claude connected: hub %q", got)+ }+ e, err := targetByID("claude").MCP.entries()+ if err != nil {+ t.Fatal(err)+ }+ if u, _ := e["magpie"]["url"].(string); !strings.HasSuffix(u, gateway.HubMCPPath+"claude") || e["fs"] != nil {+ t.Fatalf("claude connected:\n%s", read(t, claude))+ }+}++// A library server called magpie keeps every agent on each server's own+// entry, said why.+func TestHubNameTaken(t *testing.T) {+ hubSandbox(t)+ ok(t)(SaveServer("", Server{Name: "magpie", Transport: "stdio", Command: "npx", Args: []string{"x"}, Agents: []string{"claude"}}))+ ok(t)(SetMCPHub(true))+ e, err := targetByID("claude").MCP.entries()+ if err != nil {+ t.Fatal(err)+ }+ if e["magpie"]["command"] != "npx" {+ t.Errorf("the library's own magpie was replaced: %v", e["magpie"])+ }+ v, err := Read(nil)+ if err != nil {+ t.Fatal(err)+ }+ for _, a := range v.Agents {+ if a.ID == "claude" && a.Hub != hubTaken {+ t.Errorf("claude's hub %q, want %q", a.Hub, hubTaken)+ }+ }+}++// A server the library no longer gives is ended by the next prune, though+// no agent asks the hub again: ticked for no agent, edited, or the switch+// turned off by another magpie (here a change that doesn't end them+// itself, as the CLI's doesn't end the gateway's).+func TestHubPrune(t *testing.T) {+ hubSandbox(t)+ fake := fakeStdio(t, "ok", nil)+ fake.Agents = []string{"claude"}+ ok(t)(SaveServer("", *fake))+ ok(t)(SetMCPHub(true))+ h := theHub{}+ ctx := context.Background()+ held := func() hubConn {+ t.Helper()+ hubPool.Lock()+ b := hubPool.m[fake.Name]+ hubPool.Unlock()+ if b == nil {+ return nil+ }+ b.mu.Lock()+ defer b.mu.Unlock()+ return b.conn+ }+ run := func() hubConn {+ t.Helper()+ if _, err := h.Call(ctx, "claude", fake.Name, "a", json.RawMessage(`{}`)); err != nil {+ t.Fatal(err)+ }+ c := held()+ if c == nil {+ t.Fatal("no server held after a call")+ }+ return c+ }++ // ended: its output closed once its process went (the reader marks it)+ ended := func(c hubConn) bool {+ for end := time.Now().Add(10 * time.Second); time.Now().Before(end); time.Sleep(10 * time.Millisecond) {+ if !c.alive() {+ return true+ }+ }+ return false+ }++ c := run()+ hubPrune()+ if held() != c || !c.alive() {+ t.Fatal("a server the library still gives was ended")+ }++ // ticked for no agent, it is still given to other apps (HubMCPAll)+ ok(t)(ServerAgents(fake.Name, nil))+ hubPrune()+ if held() != c || !c.alive() {+ t.Fatal("a server ticked for no agent, still given to other apps, was ended")+ }+ ok(t)(ServerAgents(fake.Name, []string{"claude"}))++ edited := *fake+ edited.Env = map[string]string{"EDITED": "1"}+ for k, v := range fake.Env {+ edited.Env[k] = v+ }+ ok(t)(SaveServer(fake.Name, edited))+ hubPrune()+ if held() != nil || !ended(c) {+ t.Fatal("a server edited since it was started is still running")+ }++ c = run()+ removed := *fake+ ok(t)(RemoveServer(fake.Name))+ hubPrune()+ if held() != nil || !ended(c) {+ t.Fatal("a server taken out of the library is still running")+ }++ ok(t)(SaveServer("", removed))+ c = run()+ ok(t)(change(func(l *Library) error { l.MCPHub = false; return nil }))+ hubPrune()+ if held() != nil || !ended(c) {+ t.Fatal("the hub was turned off and its server is still running")+ }+}++// An agent that reaches the gateway from another machine (a WSL distro+// under NAT, a computer on the LAN) is given the hub at that address, with+// the LAN key while sharing is on; one on this machine, with no key.+func TestHubEntryFromAnotherMachine(t *testing.T) {+ hubSandbox(t)+ const there = "http://172.20.0.1:3425"+ at := func(gw string) *Server {+ return hubServerFor(&Target{Agent: &agent.Agent{ID: "claude", Gateway: func() string { return gw }}})+ }+ if s := at(there); s.URL != there+gateway.HubMCPPath+"claude" || len(s.Headers) != 0 {+ t.Errorf("nothing shared: %+v", s)+ }+ if err := access.ConfigureLAN(true, false); err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { access.ConfigureLAN(false, false) })+ lan := access.LANSecret()+ if !strings.HasPrefix(lan, access.Prefix) {+ t.Fatalf("LAN key %q", lan)+ }+ if s := at(there + "/"); s.URL != there+gateway.HubMCPPath+"claude" || s.Headers["Authorization"] != "Bearer "+lan {+ t.Errorf("from another machine: %+v", s)+ }+ if s := at("http://127.0.0.1:3425"); len(s.Headers) != 0 {+ t.Errorf("on this machine: %+v", s)+ }+}++// HubLink is each app's own install link for the hub at this computer's+// address, and an error for an app with none.+func TestHubLink(t *testing.T) {+ hubSandbox(t)+ want := strings.TrimRight(gateway.URL(), "/") + gateway.HubMCPAll+ link, err := HubLink("cursor")+ if err != nil {+ t.Fatal(err)+ }+ const cursor = "cursor://anysphere.cursor-deeplink/mcp/install?name=magpie&config="+ if !strings.HasPrefix(link, cursor) {+ t.Fatalf("cursor's link %q", link)+ }+ raw, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(link, cursor))+ if err != nil {+ t.Fatal(err)+ }+ var c map[string]string+ if err := json.Unmarshal(raw, &c); err != nil || len(c) != 1 || c["url"] != want {+ t.Fatalf("cursor's config %s (%v), want url %s", raw, err, want)+ }++ link, err = HubLink("vscode")+ if err != nil {+ t.Fatal(err)+ }+ q, found := strings.CutPrefix(link, "vscode:mcp/install?")+ if !found {+ t.Fatalf("vscode's link %q", link)+ }+ q, err = url.QueryUnescape(q)+ if err != nil {+ t.Fatal(err)+ }+ var v map[string]string+ if err := json.Unmarshal([]byte(q), &v); err != nil || v["name"] != "magpie" || v["type"] != "http" || v["url"] != want || len(v) != 3 {+ t.Fatalf("vscode's config %s (%v), want magpie at %s", q, err, want)+ }++ if _, err := HubLink("javascript:alert(1)"); err == nil {+ t.Fatal("an app with no install link was given one")+ }+}diff --git a/internal/library/mcp_signin.go b/internal/library/mcp_signin.goindex 7627bf937..5f9ce2f48 100644--- a/internal/library/mcp_signin.go+++ b/internal/library/mcp_signin.go@@ -22,10 +22,11 @@ import (func init() {mcpauth.Changed = func(string) { _, _ = Sync() }- // an agent beyond loopback is given a relayed server with the LAN key- // (through), which is the one there is no longer+ // an agent beyond loopback is given a relayed server, or the MCP hub,+ // with the LAN key (through, hubServerFor), which is the one there is+ // no longeraccess.LANChanged = func() {- if relaysAny() {+ if relaysAny() || (theHub{}).On() {_, _ = Sync()}}diff --git a/internal/library/sync.go b/internal/library/sync.goindex a375110f6..78edf4ece 100644--- a/internal/library/sync.go+++ b/internal/library/sync.go@@ -6,6 +6,7 @@ import ("os""slices"+ "github.com/yetone/magpie/internal/gateway""github.com/yetone/magpie/internal/mcpauth")@@ -224,9 +225,25 @@ func (l *Library) syncMCP(t *Target, b *backups, res *Result) {res.changed(id)return true}+ // with the hub on, the agent is given magpie's one entry in place of+ // the servers behind it (mcp_hub.go); one that can't take it keeps+ // each server's own+ hub, _ := l.hubFor(t, a, entries)+ if t.Gone {+ // nor is a gone agent given the hub anew+ hub = nil+ if l.MCPHub && slices.Contains(a.MCP, HubEntry) && l.server(HubEntry) == nil &&+ slices.ContainsFunc(l.MCP, func(s *Server) bool { return slices.Contains(s.Agents, id) && inHub(s) }) {+ hub = hubServerFor(t)+ }+ }+ hubbed := func(s *Server) bool { return hub != nil && inHub(s) }var mine []stringfor _, name := range a.MCP {- if s := l.server(name); s != nil && slices.Contains(s.Agents, id) && t.MCP.supports(s) == nil {+ if s := l.server(name); s != nil && slices.Contains(s.Agents, id) && t.MCP.supports(s) == nil && !hubbed(s) {+ continue+ }+ if name == HubEntry && hub != nil {continue}e, ok := entries[name]@@ -241,10 +258,16 @@ func (l *Library) syncMCP(t *Target, b *backups, res *Result) {res.dropped = append(res.dropped, had)}}+ given := []*Server{}for _, s := range l.MCP {- if !slices.Contains(s.Agents, id) {- continue+ if slices.Contains(s.Agents, id) && !hubbed(s) {+ given = append(given, s)}+ }+ if hub != nil {+ given = append(given, hub)+ }+ for _, s := range given {// an agent gone from here is given nothing: what it has still// stays its, until untickedif t.Gone {@@ -255,7 +278,9 @@ func (l *Library) syncMCP(t *Target, b *backups, res *Result) {}continue}- s := through(s, base)+ if s != hub {+ s = through(s, base)+ }if err := t.MCP.supports(s); err != nil {res.fail(id, "mcp:"+s.Name, err)continue@@ -360,6 +385,12 @@ type AgentView struct {// startup_timeout_sec)Startup bool `json:"startup,omitempty"`MCPVia string `json:"mcpVia,omitempty"`+ // Hub, with the MCP hub on, is "on" for an agent given magpie's one+ // entry in place of its servers, or why it keeps theirs: no-remote+ // (it reaches no server over HTTP), taken (a library server is called+ // magpie) or own (it has an entry called magpie of its own); "" for one+ // given none of the hub's servers+ Hub string `json:"hub,omitempty"`// How is the way it is given its skills, link or copy, and HowOwn its// own over the library's; MustCopy is one that can only take copies// (in WSL)@@ -434,6 +465,10 @@ type View struct {Backups string `json:"backups"`SkillGroups []SkillGroup `json:"skillGroups"`CopySkills bool `json:"copySkills"` // the library gives skills as copies (#896)+ // MCPHub: the agents are given magpie's one MCP server in place of+ // the library's (#2679), at HubPath<agent> on the gateway+ MCPHub bool `json:"mcpHub"`+ HubPath string `json:"hubPath"`// CheckDue is set when the skills from GitHub weren't checked for// updates since magpie started, or not for a while: the page checks// them as it shows them (#1449)@@ -473,7 +508,7 @@ func readView(problems []Problem, glance bool) (*View, error) {// the agents are found once for the whole read: finding them looks// through every agent's folders, in WSL too (#1624)targets := Targets()- v := &View{Agents: []AgentView{}, Servers: []ServerView{}, Skills: []SkillView{}, FoundSkills: []FoundSkill{}, Instructions: l.instructionsView(targets), Dir: Dir(), Backups: BackupDir(), CopySkills: l.CopySkills, Partial: glance}+ v := &View{Agents: []AgentView{}, Servers: []ServerView{}, Skills: []SkillView{}, FoundSkills: []FoundSkill{}, Instructions: l.instructionsView(targets), Dir: Dir(), Backups: BackupDir(), CopySkills: l.CopySkills, MCPHub: l.MCPHub, HubPath: gateway.HubMCPPath, Partial: glance}for _, t := range targets {av := AgentView{ID: t.Agent.ID, Name: t.Agent.Name, Icon: t.Agent.Icon, Instructions: t.Instructions, Skills: t.Skills,SkillsAlso: t.SkillsAlso, Note: t.Note, MCPVia: t.MCPVia, ProjectSkills: ProjectSkillsDir(t.Agent.ID), ProjectMCP: ProjectMCPFile(t.Agent.ID),@@ -492,6 +527,18 @@ func readView(problems []Problem, glance bool) (*View, error) {av.NoEnvRefs = t.MCP.refsOf() == envSyntax{}av.ToolsOff = toolsOffOf(t) != toolsNoneav.Startup = t.MCP.Format == fmtCodex+ if l.MCPHub {+ a := l.Applied[t.Agent.ID]+ if a == nil {+ a = &Applied{}+ }+ entries, _ := t.MCP.entries()+ if hs, why := l.hubFor(t, a, entries); hs != nil {+ av.Hub = "on"+ } else {+ av.Hub = why+ }+ }}v.Agents = append(v.Agents, av)}