Files
magpie/internal/library/mcp_local_test.go
T
yetone 4054251865 library: an MCP server can be kept to this computer, never synced
huhuy72219405 on X: "能不能把不同设备的mcp区分一下,有些mcp属于本地的,
同步之后其他设备不应该读取到". A server in Library > MCP now has a
"This computer only" switch in its editor (Server.Local; CLI
`magpie library mcp add ... --local` and `magpie library mcp local
<name> on|off`; the TUI keeps it on edit and marks the row).

- Collect leaves local servers out (carriedServers), so neither a sync
  nor a backup file takes one to another computer. Turning a shared
  server local takes it out of what is carried, so the other computers
  drop it at their next sync; the flag itself is never carried.
- Put (sync, join, Restore, Undo, backup file) keeps every local
  server, with its agents, and clears Local on what comes in.
- Another computer's server by a local server's name goes to
  Library.Away: given to no agent, not in the hub, carried on as it
  came, so a push from here never removes it from the computers that
  have it. settleAway (every change) promotes it once the name is gone
  here, and drops it once the name is shared here again.
- The hub serves local servers like any other (it runs here);
  subagents aren't carried, so one naming a local server keeps it.

Docs: docs/subsystems/library.md, docs/subsystems/sync-backup.md.

Run at f5dd4e142 + this change; after rebasing on d3898ca8d (gui/provider only, then 2a1ef15ba, routing), vet, the five new Go tests, mcp-local-server and i18n-keys again, ok:
- mote: gofmt -l clean; go vet of library/backup/davsync/cli/tui;
  GOOS=linux and GOOS=windows go vet -tags nogui of the same; go test
  of TestLocalServerNotCarried, TestPutKeepsLocalServer,
  TestPutSameNameAsLocal (library), TestRestoreKeepsLocalServer
  (backup), TestLocalServerThroughSync (davsync, two computers through
  the fake S3) ok.
- mote GUI, Chromium and WebKit: mcp-local-server.test.cjs (new; every
  language, 1100 and 390 wide, switch saves local true/false, row tag,
  no sideways scroll), i18n-keys, mcp-editor-cancel,
  mcp-startup-timeout, every library-mcp-*, mcp-oauth-app, mcp-signin*,
  library-items, library-servers-bulk, library-subagents,
  library-pick-remove, library-env-refs, library-zed-mcp,
  library-loading: all pass (126 of 126 in the last batch).
- Full Go suite, -tags nogui -timeout 45m, on the Linux box (wsl-run;
  mote had another session's full suite running): every package ok
  but internal/agent, whose TestInstalledVersion, TestGrokInstalled,
  TestOmpDetectedWithPiDir and TestZedAppPath fail the same at
  origin/main 9b4d0bda8 on that box; all four are #4033.
- Real CLI in a sandbox HOME on mote: mcp add --local, local on/off,
  `magpie library` shows "this computer only", library.json has
  "local": true; an unknown name and a bad value are refused.

Broken by hand: putServers not keeping local servers turned
TestPutKeepsLocalServer, TestPutSameNameAsLocal,
TestRestoreKeepsLocalServer and TestLocalServerThroughSync red;
carriedServers carrying local servers turned TestLocalServerNotCarried,
TestPutSameNameAsLocal, TestRestoreKeepsLocalServer and
TestLocalServerThroughSync red.

Not tried: a sync between two real computers; the real GUI app (the
Playwright run fakes the API); the full Go suite on macOS or Windows.
2026-10-30 02:31:08 +08:00

150 lines
5.7 KiB
Go

package library
import (
"path/filepath"
"slices"
"strings"
"testing"
)
func serverIn(t *testing.T, name string) *Server {
t.Helper()
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
for _, s := range v.Servers {
if s.Name == name {
return s.Server
}
}
return nil
}
func carriedNames(b Bundle) []string {
var out []string
for _, s := range b.MCP {
out = append(out, s.Name)
}
return out
}
// A server kept to this computer (huhuy72219405 on X: "有些mcp属于本地的,
// 同步之后其他设备不应该读取到") is never in what a sync or a backup
// carries, and adding one changes nothing a sync uploads.
func TestLocalServerNotCarried(t *testing.T) {
sandbox(t)
ok(t)(SaveServer("", Server{Name: "gh", Transport: "stdio", Command: "gh-mcp", Agents: []string{"claude"}}))
before := mustJSON(t, carry(t))
ok(t)(SaveServer("", Server{Name: "here", Transport: "stdio", Command: "/opt/mine/mcp", Agents: []string{"claude"}, Local: true}))
if s := serverIn(t, "here"); s == nil || !s.Local {
t.Fatalf("the local server as saved: %+v", s)
}
b := carry(t)
if got := carriedNames(b); !slices.Equal(got, []string{"gh"}) {
t.Fatalf("carried %v, want gh alone", got)
}
if after := mustJSON(t, b); after != before {
t.Fatalf("a local server changed what a sync carries:\n%s\n%s", before, after)
}
// a shared server turned local leaves what is carried; shared again, it is back
ok(t)(ServerLocal("gh", true))
if got := carriedNames(carry(t)); len(got) != 0 {
t.Fatalf("gh kept here is still carried: %v", got)
}
ok(t)(ServerLocal("gh", false))
if got := mustJSON(t, carry(t)); got != before {
t.Fatalf("gh shared again isn't carried as before:\n%s\n%s", before, got)
}
}
// A bundle coming in from another computer, without the server, keeps it
// here, kept to this computer and in the agents it was given to; the
// incoming servers go in beside it.
func TestPutKeepsLocalServer(t *testing.T) {
sandbox(t)
ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "fs-mcp", Agents: []string{"claude"}}))
other := carry(t) // another computer's library: fs alone
h := sandbox(t)
ok(t)(SaveServer("", Server{Name: "here", Transport: "stdio", Command: "/opt/mine/mcp", Agents: []string{"claude"}, Local: true}))
ok(t)(SaveServer("", Server{Name: "old", Transport: "stdio", Command: "old-mcp", Agents: []string{"claude"}}))
ok(t)(Put(&other))
if s := serverIn(t, "here"); s == nil || !s.Local || !slices.Equal(s.Agents, []string{"claude"}) {
t.Fatalf("the local server after a sync from another computer: %+v", s)
}
if serverIn(t, "fs") == nil || serverIn(t, "old") != nil {
t.Fatal("the incoming library wasn't taken in beside it")
}
c := read(t, filepath.Join(h, ".claude.json"))
if !strings.Contains(c, "/opt/mine/mcp") || !strings.Contains(c, "fs-mcp") || strings.Contains(c, "old-mcp") {
t.Fatalf("Claude Code's servers after the sync: %s", c)
}
// a bundle that says it is local (only this computer says that) is taken in as shared
in := carry(t)
for _, s := range in.MCP {
s.Local = true
}
ok(t)(Put(&in))
if s := serverIn(t, "fs"); s == nil || s.Local {
t.Fatalf("an incoming server's local flag was taken: %+v", s)
}
}
// Another computer's server by the name of one kept to this computer
// doesn't take its place here: it is given to no agent here, and carried
// on as it came, so a sync from here doesn't take it from the computers
// that have it. Once the one here is shared again, or gone, the name is
// one server again.
func TestPutSameNameAsLocal(t *testing.T) {
sandbox(t)
ok(t)(SaveServer("", Server{Name: "db", Transport: "stdio", Command: "their-db", Agents: []string{"codex"}}))
theirs := carry(t)
want := mustJSON(t, theirs)
h := sandbox(t)
ok(t)(SaveServer("", Server{Name: "db", Transport: "stdio", Command: "/opt/my-db", Agents: []string{"claude"}, Local: true}))
ok(t)(Put(&theirs))
s := serverIn(t, "db")
if s == nil || !s.Local || s.Command != "/opt/my-db" || !slices.Equal(s.Agents, []string{"claude"}) {
t.Fatalf("this computer's db after theirs came in: %+v", s)
}
if c := read(t, filepath.Join(h, ".claude.json")); !strings.Contains(c, "/opt/my-db") || strings.Contains(c, "their-db") {
t.Fatalf("Claude Code's servers: %s", c)
}
if c := read(t, filepath.Join(h, ".codex/config.toml")); strings.Contains(c, "their-db") {
t.Fatalf("theirs was given to Codex here: %s", c)
}
if got := mustJSON(t, carry(t)); got != want {
t.Fatalf("theirs isn't carried on as it came:\n%s\n%s", want, got)
}
// edits here change nothing carried
ok(t)(ServerAgents("db", []string{"claude", "codex"}))
if got := mustJSON(t, carry(t)); got != want {
t.Fatalf("an edit of the local db changed what is carried:\n%s\n%s", want, got)
}
// a bundle without db lets theirs go; the local one stays
none := Bundle{MCP: []*Server{}, Skills: []*CarriedSkill{}}
ok(t)(Put(&none))
if s := serverIn(t, "db"); s == nil || s.Command != "/opt/my-db" {
t.Fatalf("this computer's db after a bundle without it: %+v", s)
}
if got := carriedNames(carry(t)); len(got) != 0 {
t.Fatalf("carried %v after theirs was taken out", got)
}
// theirs again; removed here, theirs comes in as a sync would bring it
ok(t)(Put(&theirs))
ok(t)(RemoveServer("db"))
if s := serverIn(t, "db"); s == nil || s.Local || s.Command != "their-db" {
t.Fatalf("after the local db was removed: %+v", s)
}
// the local one shared again is the one carried
ok(t)(SaveServer("db", Server{Name: "db", Transport: "stdio", Command: "/opt/my-db", Agents: []string{"claude"}, Local: true}))
ok(t)(Put(&theirs))
ok(t)(ServerLocal("db", false))
if b := carry(t); len(b.MCP) != 1 || b.MCP[0].Command != "/opt/my-db" {
t.Fatalf("shared again, carried %s", mustJSON(t, b))
}
}