library: Pi's MCP servers go to ~/.pi/agent/mcp.json, the file Pi 0.99+ loads, unless pi-mcp-adapter is really loaded; an adapter left installed but not listed no longer sends them to mcp-adapter.json (sydney on Discord)

sydney reported that the Library wrote Pi's MCP servers to
~/.pi/agent/mcp-adapter.json while Pi 1.0 loads ~/.pi/agent/mcp.json.

Pi's own source (npm @earendil-works/pi-coding-agent):
- 0.99.0 to 1.1.0 have built-in MCP: dist/extensions/mcp/config.js
  loadMcpConfig reads <agentDir>/mcp.json (and a trusted project's
  .pi/mcp.json). An extension registering /mcp (pi-mcp-adapter) replaces
  it; "extensions": ["-builtin:mcp"] turns it off.
- 0.87.1 and earlier have no MCP; pi-mcp-adapter 3.x/4.x reads
  mcp-adapter.json only, adapter 5.0.0+ also reads Pi's mcp.json on a Pi
  with registerMcpServer.
- core/package-manager.js resolve() loads only settings.json "packages",
  "extensions" entries and <agentDir>/extensions/. npm/node_modules, git/
  and the global npm root are install places, not loaded on their own.

magpie counted any pi-mcp-adapter folder (npm/node_modules, git, global
root) as loaded, so an adapter left behind with "packages": [] made it
write mcp-adapter.json, which nothing reads, and move mcp.json away
(heliar-k's #1097 is the same case). It also fell back to the adapter file
when Pi's version was unknown.

Now:
- piDetect follows Pi's loader: a package is loaded only when listed (an
  object entry with "extensions": [] loads none), "-"/"!" entries turn one
  off, and <agentDir>/extensions/<name> loads; the other folders only give
  the version.
- An unknown Pi version is taken as one with built-in MCP.
- With no adapter loaded, servers in mcp-adapter.json are moved into
  mcp.json (both backed up first); nothing in either file is dropped.
- With adapter 5+ loaded on a Pi with built-in MCP, mcp.json is left
  where it is (the adapter reads it) and new servers go to the adapter's
  file; with adapter 3/4, or on Pi before 0.99, the old move is kept.

Tests: TestPiMCPUnloadedAdapter, TestPiDetectLoaded and
TestPiMCPAdapter5KeepsPiFile fail on the old pimcp.go and pass now;
older tests that relied on an unlisted adapter now list it.

Run: gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui
./internal/library/; go test -tags nogui ./internal/library/
./internal/agent/ under a temp HOME. Real Pi in a sandbox HOME:
- Pi 1.0.4, reporter's layout (adapter 5.2.0 in npm/node_modules,
  "packages": []): `magpie library mcp add` wrote mcp.json and `pi mcp
  list` shows both the user's server and the new one connected. The old
  binary wrote mcp-adapter.json and Pi listed no servers. `magpie library
  sync` with pi off PATH (version unknown) moved that home's
  mcp-adapter.json back into mcp.json, with backups, and Pi connected both.
- Pi 1.0.4 with `pi install npm:pi-mcp-adapter@5.2.0`: mcp.json is kept,
  the new server goes to mcp-adapter.json, and `pi-mcp-adapter doctor`
  connects both.
- Pi 0.87.1: still mcp-adapter.json, as before.
Not run: Windows or WSL Pi, project-level .pi/settings.json, a real
interactive Pi session.

Reported by sydney on Discord; same cause as #1097 (heliar-k).
This commit is contained in:
yetone
2026-10-10 10:18:16 +08:00
parent 666c0fa4fd
commit 47a313f618
3 changed files with 198 additions and 36 deletions
+2
View File
@@ -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 {
+69 -27
View File
@@ -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) {
+127 -9
View File
@@ -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))
}
}