Files
magpie/internal/cli/library_hub_test.go
T
yetone e0ea15b8e8 library: an MCP hub, one magpie server in place of every library server (#2679)
With Library → MCP → "MCP hub" on (off by default), each agent that takes
an MCP server over HTTP is given one entry, magpie, at
/mcp/magpie/hub/<agent>, in place of the library servers ticked for it.
magpie runs those servers itself, once for every agent: a command starts
on its first call and ends after 10 idle minutes, a URL server is reached
with its headers and MCP sign-in. Tools are <server>__<tool>; a tool off
in the library is neither listed nor run. Any other app connects at
/mcp/magpie/hub, which serves every library server. Both are guarded as
/mcp/magpie/tools is (local or a gateway key, no foreign page, 4 MiB,
the key's limit per call). An agent that reaches no server over HTTP
(Claude Desktop), has a magpie entry of its own, or sits beside a library
server called magpie keeps each server's own entry, and the page says why.
Off gives every agent its servers back and ends every process the hub
started.

GUI: a switch under the MCP list, a line of what it serves, the address,
and a Connect… sheet. The sheet lists every agent magpie detects that
takes MCP (not a fixed four), each with where it stands; one given none of
the hub's servers has a Connect that ticks it for every server it can
reach (servers/agents-all), so it gets the entry, and the sheet stays
open. Then any other app: Cursor (when it isn't a detected agent), VS
Code, Other, with the JSON and, in the app, "Add to …" through the app's
own install link built in Go (servers/hub-open; the page never sends a
link). CLI: magpie library mcp hub on|off. Design: docs/subsystems/mcp-hub.md.

With #3885 (62045475b): with the hub on, Codex is given the one magpie
URL entry, and the servers behind it never reach runVia, which takes a
command server only; with the hub off Codex gets /mcp/run/<name> as before.
Rebased onto it; the one conflict was both commits declaring base in
syncMCP.

Run:
- At 62045475b + this (the pushed tree), on mote: gofmt -l, go vet,
  GOOS=windows go vet, GOOS=linux go vet -tags nogui clean; go test
  -timeout 45m internal/library (108s), gateway (510s), cli (50s) ok.
  The GUI files and internal/gui weren't run again: 62045475b touches
  neither, and the rebase changed nothing in them.
- At ddf31ca30 + this: go test -timeout 45m for
  internal/library, gateway, cli and gui on mote; gofmt -l; go vet,
  GOOS=windows go vet, GOOS=linux go vet -tags nogui; win-run of
  library-mcp-hub and i18n-keys (both engines, en zh zh-TW ja de, 420px).
  gofmt and the three vets clean; library, gateway (554s) and cli ok;
  gui ok but TestMainWindowCorners, red the same on origin/main ddf31ca30
  on mote (#3949); GUI 26 of 26 pass.
- Earlier base (582bd8036, before TestHubConnectAnAgent and the oneport
  consts): full go test ./... on mote, every package ok but gateway's
  TestClientPathCoversHandler (fixed here: HubMCPPath, HubMCPAll and
  {agent} added to its consts; passes) and internal/gui's AppKit tests.
  Of those, TestMainWindowCorners fails the same on origin/main ddf31ca30
  on mote's macOS 27 (#3949); the other six (dock, panel, plugin page,
  right-to-left) passed rerun alone at load ~95 (#2645).
- win-run of every library-*, gui-*, i18n*, click-scroll and menu-scroll
  GUI file: 49 files, 839 tests, 839 pass.
- Real use in WSL (magpie-desktop), sandbox HOME, real echo MCP server:
  hub on, Cursor given servers → ~/.cursor/mcp.json holds only magpie at
  /mcp/magpie/hub/cursor; initialize, tools/list (echo__echo) and two
  calls ran on one server pid; a foreign Origin got 403; hub off → the
  echo process ended, tools [].

Broken by hand: Connect button removed → 6 GUI cases red; the old row
wrap rule → 6 red (icon on its own line at 420px); EveryServerAgents
call removed from TestHubConnectAnAgent → red (hub "").

Not run: the whole GUI suite (~960 files); Windows agents (VS Code, Cursor
on Windows) against a real hub; an "Add to Cursor" click in the real app
(the link is built and tested, the app's own confirm wasn't driven).
2026-10-29 03:17:58 +08:00

70 lines
2.0 KiB
Go

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)
}
}