diff --git a/internal/library/library_test.go b/internal/library/library_test.go index 6e8430648..751b67453 100644 --- a/internal/library/library_test.go +++ b/internal/library/library_test.go @@ -875,6 +875,7 @@ func TestReadNoAgents(t *testing.T) { func TestPiMCP(t *testing.T) { h := sandbox(t) p := filepath.Join(h, ".pi/agent/mcp.json") + write(t, filepath.Join(h, ".pi/agent/settings.json"), `{"packages": ["npm:pi-mcp-adapter@2.9.1"]}`) write(t, p, `{"mcpServers": {"supabase": {"transport": "streamable-http", "url": "https://mcp.supabase.com/mcp", "lifecycle": "eager"}}}`) tg := targetByID("pi") if tg == nil || tg.MCP == nil || tg.MCP.Path != p { @@ -909,6 +910,7 @@ func TestPiMCPAdapter3(t *testing.T) { d := filepath.Join(h, ".pi/agent") old, adapter := filepath.Join(d, "mcp.json"), filepath.Join(d, "mcp-adapter.json") pkg := filepath.Join(d, "npm/node_modules/pi-mcp-adapter/package.json") + write(t, filepath.Join(d, "settings.json"), `{"packages": ["npm:pi-mcp-adapter"]}`) write(t, old, `{"mcpServers": {"mine": {"command": "npx", "args": ["x"]}}}`) write(t, pkg, `{"name": "pi-mcp-adapter", "version": "2.9.1"}`) if tg := targetByID("pi"); tg.MCP.Path != old { diff --git a/internal/library/pimcp.go b/internal/library/pimcp.go index 39111f1f1..3ae0ffddc 100644 --- a/internal/library/pimcp.go +++ b/internal/library/pimcp.go @@ -42,7 +42,10 @@ import ( // warns "pi-mcp-adapter no longer reads …mcp.json" while that file has // any server (or settings, imports, claudePlugins) — whether or not // mcp-adapter.json is there too (config.ts, -// getLegacyMcpMigrationNotices). Before 3 it read mcp.json. +// getLegacyMcpMigrationNotices). Before 3 it read mcp.json. From 5.0.0 +// (2026-10-01), on a Pi with MCP of its own, it reads Pi's mcp.json +// again, below mcp-adapter.json, and warns only of the adapter's +// keys there; it turns Pi's own off with "-builtin:mcp". // - pi-mcp-extension 1.5 reads ~/.pi/agent/mcp.json only — its home's, // not PI_CODING_AGENT_DIR's (src/config.ts, loadConfig). // @@ -114,6 +117,14 @@ func piPackageName(spec string) (name, pin string) { return strings.TrimSuffix(name, ".git"), strings.TrimPrefix(pin, "v") } +// piDetect is which MCP extension Pi loads, as Pi's package manager decides +// it (core/package-manager.js resolve, the same in 0.87.1 and 1.1.0): the +// packages settings.json lists, its extensions entries, and what its +// extensions folder has. A package left in npm/node_modules, git/ or npm's +// global folder that settings.json doesn't list isn't loaded (heliar-k on +// #1097, sydney on Discord: a pi-mcp-adapter left there with `packages: []` +// sent the servers to mcp-adapter.json, which Pi 1.0 never reads); those +// folders only tell a loaded adapter's version. func piDetect(d string) piPlugins { var p piPlugins var settings struct { @@ -126,8 +137,14 @@ func piDetect(d string) piPlugins { var specs []string for _, r := range settings.Packages { var s string - var o struct{ Source string } + var o struct { + Source string + Extensions *[]string + } if json.Unmarshal(r, &s) != nil && json.Unmarshal(r, &o) == nil { + if o.Extensions != nil && len(*o.Extensions) == 0 { + continue // "extensions": [] loads none of the package's + } s = o.Source } specs = append(specs, s) @@ -148,7 +165,13 @@ func piDetect(d string) piPlugins { } var pin string var dirs []string + off := map[string]bool{} // what an entry turns off ("-path", "!pattern") for _, s := range specs { + if t := strings.TrimSpace(s); strings.HasPrefix(t, "-") || strings.HasPrefix(t, "!") { + name, _ := piPackageName(t[1:]) + off[name] = true + continue + } name, v := piPackageName(s) switch name { case "pi-mcp-extension": @@ -164,10 +187,18 @@ func piDetect(d string) piPlugins { } } } - if exists(filepath.Join(d, "npm", "node_modules", "pi-mcp-extension")) || exists(filepath.Join(d, "extensions", "pi-mcp-extension")) { + // Pi loads what its extensions folder has without a settings entry + // (addAutoDiscoveredResources), unless an entry turns it off + if !off["pi-mcp-extension"] && exists(filepath.Join(d, "extensions", "pi-mcp-extension")) { p.ext = true } - // Pi loads what its extensions folder has without a settings entry + if !off["pi-mcp-adapter"] && exists(filepath.Join(d, "extensions", "pi-mcp-adapter")) { + p.adapter = true + } + if !p.adapter { + return p + } + // where a loaded adapter's version is read dirs = append(dirs, filepath.Join(d, "extensions", "pi-mcp-adapter")) dirs = append(dirs, filepath.Join(d, "npm", "node_modules", "pi-mcp-adapter")) git, _ := filepath.Glob(filepath.Join(d, "git", "*", "*", "pi-mcp-adapter")) @@ -178,7 +209,6 @@ func piDetect(d string) piPlugins { found := "" for _, dir := range dirs { if found = version(dir); found != "" { - p.adapter = true break } } @@ -194,18 +224,23 @@ func piDetect(d string) piPlugins { // piMCP is the file Pi's MCP servers go in, and the extension Pi reads them // through ("" for its own), h being the home, d Pi's agent folder and // version the Pi on PATH's: -// - Pi 0.99 or later with no MCP extension installed and its own MCP on: -// mcp.json, as Pi has it. The servers mcp-adapter.json has, which no -// one reads now (magpie's for pi-mcp-adapter 3, or the user's), are -// moved into it, the file backed up; one mcp.json has differently, and -// the adapter's own settings, stay there, still found. An mcp.json -// written for pi-mcp-adapter before 3 is read by Pi as it is (the -// adapter's httpTransport ignored). -// - otherwise, as before 0.99 — pi-mcp-adapter, still installed, stands -// in for Pi's own MCP (piFiles). +// - Pi 0.99 or later, or a Pi whose version isn't known, with no MCP +// extension loaded and its own MCP on: mcp.json, as Pi has it. With +// nothing loaded only Pi's own MCP reads servers, and every Pi since +// 0.99 (1.1.0 now) reads mcp.json (extensions/mcp/config.js, +// loadMcpConfig); a Pi before it has no MCP to read either file. The +// servers mcp-adapter.json has, which no one reads now (magpie's for +// pi-mcp-adapter 3, or the user's), are moved into it, the file backed +// up; one mcp.json has differently, and the adapter's own settings, +// stay there, still found. An mcp.json written for pi-mcp-adapter +// before 3 is read by Pi as it is (the adapter's httpTransport +// ignored). +// - otherwise, as before 0.99 — pi-mcp-adapter, loaded, stands in for +// Pi's own MCP (piFiles). func piMCP(h, d, version string) (*mcpFile, string) { p := piDetect(d) - if piNative(version) && !p.adapter && !p.ext && !p.noBuiltin { + native := piNative(version) || version == "" + if native && !p.adapter && !p.ext && !p.noBuiltin { native, adapter := filepath.Join(d, "mcp.json"), filepath.Join(d, "mcp-adapter.json") piMove(adapter, native, true) f := &mcpFile{Path: native, Format: fmtPiNative} @@ -218,7 +253,7 @@ func piMCP(h, d, version string) (*mcpFile, string) { if p.ext && !p.adapter { via = "pi-mcp-extension" } - f := piFiles(h, d, p) + f := piFiles(h, d, p, native) // pi-mcp-extension sends its config as written ("no env var // interpolation — WYSIWYG config", src/config.ts), and with it there // the servers reach its file too; pi-mcp-adapter 5 reads ${NAME} in @@ -227,19 +262,25 @@ func piMCP(h, d, version string) (*mcpFile, string) { return f, via } -// piFiles is the file an extension reads Pi's MCP servers from: +// piFiles is the file an extension reads Pi's MCP servers from, native +// saying the Pi may be 0.99 or later: // - pi-mcp-adapter 3, or one whose version isn't known (a fresh install -// is 3), or mcp-adapter.json already there: mcp-adapter.json. The -// servers magpie or the user put in mcp.json are moved over, as the -// adapter asks, unless pi-mcp-extension reads that file or the -// adapter isn't found (mcp.json may then be Pi 0.99's own); +// is 5), or mcp-adapter.json already there: mcp-adapter.json. The +// servers magpie or the user put in mcp.json are moved over, as +// adapter 3 and 4 ask, unless pi-mcp-extension reads that file, no +// adapter is loaded (mcp.json may then be Pi 0.99's own), or the +// adapter is 5 (or not known) on a Pi that may be 0.99: adapter 5 +// reads Pi's mcp.json there too, below mcp-adapter.json (config.ts +// getPiMcpGlobalConfigPath; CHANGELOG 5.0.0), and `pi mcp` reads only +// mcp.json, so it stays; // - with pi-mcp-extension too, both files: each extension runs its own // servers, and the adapter's warning stays while mcp.json has any, // which is the extension's file; // - pi-mcp-adapter before 3, or only pi-mcp-extension: mcp.json; -// - neither found: mcp.json if there is one (nothing says whose it is, -// so it isn't moved), mcp-adapter.json if not. -func piFiles(h, d string, p piPlugins) *mcpFile { +// - neither loaded (a Pi before 0.99, or Pi's own MCP turned off): +// mcp.json if there is one (nothing says whose it is, so it isn't +// moved), mcp-adapter.json if not. +func piFiles(h, d string, p piPlugins, native bool) *mcpFile { adapter, old := filepath.Join(d, "mcp-adapter.json"), filepath.Join(d, "mcp.json") extFile := filepath.Join(h, ".pi", "agent", "mcp.json") v3 := exists(adapter) || p.major >= 3 || (p.adapter && p.major == 0) @@ -256,9 +297,10 @@ func piFiles(h, d string, p piPlugins) *mcpFile { f.Also = []string{extFile} } if !p.ext || old != extFile { - // only for an adapter that's there: without it, mcp.json may be - // Pi 0.99's own, its version not known (not on PATH) - if p.adapter { + // only for an adapter Pi loads, and one that doesn't read mcp.json + // itself: without it, mcp.json may be Pi 0.99's own + readsPi := native && (p.major >= 5 || p.major == 0) + if p.adapter && !readsPi { piMove(old, adapter, false) } if exists(old) { diff --git a/internal/library/pimcp_test.go b/internal/library/pimcp_test.go index be0e587cc..f46394db1 100644 --- a/internal/library/pimcp_test.go +++ b/internal/library/pimcp_test.go @@ -9,6 +9,7 @@ import ( "github.com/tidwall/jsonc" + "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/testenv" ) @@ -103,9 +104,19 @@ func TestPiMCPGlobalAdapterMerge(t *testing.T) { } } -// Listed but not found (a git or odd install): taken as the current 3. +// setPiVersion makes v the version of the Pi on PATH. +func setPiVersion(t *testing.T, v string) { + t.Helper() + was := piVersion + piVersion = func(*agent.Agent) string { return v } + t.Cleanup(func() { piVersion = was }) +} + +// Listed but not found (a git or odd install): taken as the current one, +// which on a Pi before 0.99 reads mcp-adapter.json only. func TestPiMCPAdapterListed(t *testing.T) { h := sandbox(t) + setPiVersion(t, "0.98.2") d := filepath.Join(h, ".pi/agent") write(t, filepath.Join(d, "settings.json"), `{"packages": [{"source": "npm:pi-mcp-adapter"}]}`) write(t, filepath.Join(d, "mcp.json"), `{"mcpServers": {"mine": {"command": "x", "lifecycle": "eager"}}}`) @@ -163,11 +174,12 @@ func TestPiMCPBoth(t *testing.T) { } // pi-mcp-extension alone reads its home's ~/.pi/agent/mcp.json, whatever -// PI_CODING_AGENT_DIR says; with nothing installed, mcp-adapter.json. +// PI_CODING_AGENT_DIR says; with nothing installed and Pi's version not +// known, Pi's own mcp.json: only Pi's own MCP could read a server then. func TestPiMCPExtension(t *testing.T) { h := sandbox(t) - if tg := targetByID("pi"); tg.MCP.Path != filepath.Join(h, ".pi/agent/mcp-adapter.json") { - t.Errorf("nothing installed: %s", tg.MCP.Path) + if tg := targetByID("pi"); tg.MCP.Path != filepath.Join(h, ".pi/agent/mcp.json") || tg.MCP.Format != fmtPiNative { + t.Errorf("nothing installed: %+v", tg.MCP) } d := filepath.Join(h, "pi-dir") t.Setenv("PI_CODING_AGENT_DIR", d) @@ -314,14 +326,13 @@ func TestPiNativeVersions(t *testing.T) { } } -// heliar-k on #1097: the adapter found, mcp.json was moved whole to -// mcp-adapter.json with no backup, the servers the user added with -// `pi mcp add` leaving the file Pi reads. Whatever moves it, the user's -// mcp.json is copied aside first, byte for byte, and its servers survive. +// #1097: when pi-mcp-adapter 4 is loaded and mcp.json is moved whole to +// mcp-adapter.json, the user's mcp.json is copied aside first, byte for +// byte, and its servers survive. func TestPiMCPWholeMoveKeepsABackup(t *testing.T) { h := sandbox(t) d := filepath.Join(h, ".pi/agent") - write(t, filepath.Join(d, "settings.json"), `{ "quietStartup": false, "packages": [] }`) + write(t, filepath.Join(d, "settings.json"), `{ "quietStartup": false, "packages": ["npm:pi-mcp-adapter"] }`) write(t, filepath.Join(d, "npm/node_modules/pi-mcp-adapter/package.json"), `{"name": "pi-mcp-adapter", "version": "4.0.0"}`) old, adapter := filepath.Join(d, "mcp.json"), filepath.Join(d, "mcp-adapter.json") mine := `{ "mcpServers": { "demo": { "url": "https://mcp.exa.ai/mcp" } } } @@ -343,3 +354,110 @@ func TestPiMCPWholeMoveKeepsABackup(t *testing.T) { t.Errorf("demo neither in mcp.json nor in mcp-adapter.json: %s", read(t, adapter)) } } + +// sydney on Discord, heliar-k on #1097: Pi 1.0 with a pi-mcp-adapter left +// in npm/node_modules that settings.json doesn't list, so Pi doesn't load +// it (core/package-manager.js resolve). Pi's own MCP reads mcp.json, and +// the Library's servers go there, not to mcp-adapter.json; the user's +// mcp.json stays where it is, and what an earlier magpie moved into +// mcp-adapter.json comes back, both files backed up first. +func TestPiMCPUnloadedAdapter(t *testing.T) { + for _, v := range []string{"1.0.3", ""} { + t.Run("pi "+v, func(t *testing.T) { + h := sandbox(t) + setPiVersion(t, v) + d := filepath.Join(h, ".pi/agent") + native, adapter := filepath.Join(d, "mcp.json"), filepath.Join(d, "mcp-adapter.json") + write(t, filepath.Join(d, "settings.json"), `{ "quietStartup": false, "packages": [] }`) + write(t, filepath.Join(d, "npm/node_modules/pi-mcp-adapter/package.json"), `{"name": "pi-mcp-adapter", "version": "4.0.0"}`) + write(t, native, `{ "mcpServers": { "demo": { "url": "https://mcp.exa.ai/mcp" } } }`) + write(t, adapter, `{"mcpServers": {"moved": {"command": "npx", "args": ["-y", "moved"]}}}`) + tg := targetByID("pi") + if tg.MCP.Path != native || tg.MCP.Format != fmtPiNative || tg.MCPVia != "" { + t.Fatalf("target: %+v via %q", tg.MCP, tg.MCPVia) + } + got := piServers(t, native) + if got["demo"]["url"] != "https://mcp.exa.ai/mcp" || got["moved"]["command"] != "npx" { + t.Errorf("mcp.json: %s", read(t, native)) + } + for _, f := range []string{"mcp.json", "mcp-adapter.json"} { + if bs, _ := filepath.Glob(filepath.Join(BackupDir(), "*", "pi", f)); len(bs) != 1 { + t.Errorf("backups of %s: %v", f, bs) + } + } + ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "fs"}, Agents: []string{"pi"}})) + if s := piServers(t, native)["fs"]; s["command"] != "npx" || s["transport"] != nil { + t.Errorf("fs: %v", s) + } + if exists(adapter) && piServers(t, adapter)["fs"] != nil { + t.Errorf("fs written to mcp-adapter.json: %s", read(t, adapter)) + } + }) + } +} + +// Pi loads a listed package unless its extensions are filtered to none, +// and its extensions folder unless an entry turns that off. +func TestPiDetectLoaded(t *testing.T) { + h := sandbox(t) + d := filepath.Join(h, ".pi/agent") + write(t, filepath.Join(d, "npm/node_modules/pi-mcp-adapter/package.json"), `{"name": "pi-mcp-adapter", "version": "5.2.0"}`) + write(t, filepath.Join(d, "npm/node_modules/pi-mcp-extension/package.json"), `{"name": "pi-mcp-extension", "version": "1.5.0"}`) + for settings, want := range map[string]piPlugins{ + `{}`: {}, + `{"packages": []}`: {}, + `{"packages": ["npm:pi-mcp-adapter"]}`: {adapter: true, major: 5}, + `{"packages": ["npm:pi-mcp-extension"]}`: {ext: true}, + `{"packages": [{"source": "npm:pi-mcp-adapter", "extensions": []}]}`: {}, + `{"packages": [{"source": "npm:pi-mcp-adapter", "skills": []}]}`: {adapter: true, major: 5}, + `{"packages": ["npm:pi-mcp-adapter"], "extensions": ["-builtin:mcp"]}`: {adapter: true, major: 5, noBuiltin: true}, + } { + write(t, filepath.Join(d, "settings.json"), settings) + if got := piDetect(d); got != want { + t.Errorf("%s: %+v, want %+v", settings, got, want) + } + } + write(t, filepath.Join(d, "extensions/pi-mcp-adapter/package.json"), `{"name": "pi-mcp-adapter", "version": "3.1.0"}`) + write(t, filepath.Join(d, "settings.json"), `{}`) + if got := piDetect(d); !got.adapter || got.major != 3 { + t.Errorf("extensions folder: %+v", got) + } + write(t, filepath.Join(d, "settings.json"), `{"extensions": ["-extensions/pi-mcp-adapter"]}`) + if got := piDetect(d); got.adapter { + t.Errorf("turned off: %+v", got) + } +} + +// pi-mcp-adapter 5 on Pi 0.99 or later reads Pi's mcp.json too, below +// mcp-adapter.json, and `pi mcp` reads only mcp.json: the servers go in the +// adapter's file, and mcp.json isn't moved away. +func TestPiMCPAdapter5KeepsPiFile(t *testing.T) { + h := sandbox(t) + setPiVersion(t, "1.0.4") + d := filepath.Join(h, ".pi/agent") + native, adapter := filepath.Join(d, "mcp.json"), filepath.Join(d, "mcp-adapter.json") + write(t, filepath.Join(d, "settings.json"), `{"packages": ["npm:pi-mcp-adapter"], "extensions": ["-builtin:mcp"]}`) + write(t, filepath.Join(d, "npm/node_modules/pi-mcp-adapter/package.json"), `{"name": "pi-mcp-adapter", "version": "5.2.0"}`) + mine := `{"mcpServers": {"demo": {"url": "https://mcp.exa.ai/mcp"}}}` + write(t, native, mine) + tg := targetByID("pi") + if tg.MCP.Path != adapter || tg.MCPVia != "pi-mcp-adapter" { + t.Fatalf("target: %+v via %q", tg.MCP, tg.MCPVia) + } + if read(t, native) != mine { + t.Errorf("mcp.json changed: %s", read(t, native)) + } + if len(tg.MCP.Extra) != 1 || tg.MCP.Extra[0] != native { + t.Errorf("extra: %v", tg.MCP.Extra) + } + ok(t)(SaveServer("", Server{Name: "ev", Transport: "sse", URL: "https://example.com/sse", Agents: []string{"pi"}})) + if piServers(t, adapter)["ev"] == nil || read(t, native) != mine { + t.Errorf("mcp-adapter.json %s, mcp.json %s", read(t, adapter), read(t, native)) + } + // the same adapter on a Pi before 0.99 reads only its own file + setPiVersion(t, "0.98.2") + targetByID("pi") + if exists(native) || piServers(t, adapter)["demo"] == nil { + t.Errorf("0.98: mcp.json not moved: %s", read(t, adapter)) + } +}