diff --git a/internal/library/library_test.go b/internal/library/library_test.go index e9f067f92..d9cdb78b2 100644 --- a/internal/library/library_test.go +++ b/internal/library/library_test.go @@ -1209,6 +1209,45 @@ command = "gh-mcp" } } +// Another app's server in Codex's config, run from inside that app's +// bundle, is one to bring in, its empty args and inline env table read as +// they are (iomect's #2635, the TOML as the report gave it). Only the +// agent's own app's servers are its own. +func TestFoundServerFromAnotherApp(t *testing.T) { + h := sandbox(t) + write(t, filepath.Join(h, ".codex/config.toml"), `[mcp_servers.netcatty-external] +command = "/Applications/Netcatty.app/Contents/Resources/app.asar.unpacked/electron/cli/netcatty-external-mcp" +args = [] +env = { NETCATTY_EXTERNAL_MCP_DISCOVERY_FILE = "/Users/****/Library/Application Support/netcatty/external-mcp/discovery.json" } + +[mcp_servers.computer-use] +command = "/Applications/Codex.app/Contents/Resources/SkyComputerUseClient" +`) + v, err := Read(nil) + if err != nil { + t.Fatal(err) + } + var got *Found + own := map[string]bool{} + for i, f := range v.FoundServers { + own[f.Server.Name] = f.Own + if f.Server.Name == "netcatty-external" { + got = &v.FoundServers[i] + } + } + if got == nil || got.Own { + t.Fatalf("netcatty-external found as %+v; own %v", got, own) + } + s := got.Server + if s.Command != "/Applications/Netcatty.app/Contents/Resources/app.asar.unpacked/electron/cli/netcatty-external-mcp" || len(s.Args) != 0 || + s.Env["NETCATTY_EXTERNAL_MCP_DISCOVERY_FILE"] != "/Users/****/Library/Application Support/netcatty/external-mcp/discovery.json" || !slices.Contains(s.Agents, "codex") { + t.Errorf("netcatty-external read as %+v", s) + } + if !own["computer-use"] { + t.Errorf("the Codex app's own server isn't its own: %v", own) + } +} + // TestInstructionSets: several sets are kept, one is on, and the agents // read that one; switching rewrites their files (#106). func TestInstructionSets(t *testing.T) { diff --git a/internal/library/mcp.go b/internal/library/mcp.go index c90627001..e54eea19f 100644 --- a/internal/library/mcp.go +++ b/internal/library/mcp.go @@ -20,6 +20,7 @@ import ( "github.com/tidwall/jsonc" "gopkg.in/yaml.v3" + "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/imagemcp" @@ -1333,17 +1334,39 @@ type Found struct { Own bool `json:"own,omitempty"` } -// appOwned is whether a server runs from inside an app's own install — a -// Mac app bundle, a Microsoft Store app, the Codex app's runtimes — as the -// servers an agent's app writes into its config itself do. -func appOwned(s *Server) bool { - if s.Remote() { +// appOwned is whether a server runs from inside the agent's own app +// install — its Mac app bundle, its Microsoft Store app, the Codex app's +// runtimes — as the servers an agent's app writes into its config itself +// do. Another app's server in the agent's config (Netcatty's, #2635) is +// one the user added there: it is brought in like any other. +func appOwned(s *Server, a *agent.Agent) bool { + if s.Remote() || a == nil { return false } cmd := strings.ToLower(strings.ReplaceAll(s.Command, `\`, "/")) - for _, in := range []string{".app/contents/", "/windowsapps/", "/cua_node/", "/openai/codex/runtimes/"} { - if strings.Contains(cmd, in) { - return true + if a.ID == "codex" && (strings.Contains(cmd, "/cua_node/") || strings.Contains(cmd, "/openai/codex/runtimes/")) { + return true + } + names := []string{strings.ToLower(a.ID), strings.ToLower(a.Name)} + if id, _, ok := strings.Cut(names[0], "-"); ok { + names = append(names, id) + } + if n, _, ok := strings.Cut(names[1], " "); ok { + names = append(names, n) + } + segs := strings.Split(cmd, "/") + for i, seg := range segs { + // an app is a bundle (Codex.app) or a Store package + // (WindowsApps/OpenAI.Codex_1.0_x64) the command is inside + app := strings.HasSuffix(seg, ".app") && i+1 < len(segs) && segs[i+1] == "contents" + app = app || (i > 0 && segs[i-1] == "windowsapps" && i+1 < len(segs)) + if !app { + continue + } + for _, n := range names { + if n != "" && strings.Contains(seg, n) { + return true + } } } return false @@ -1372,7 +1395,7 @@ func foundServersOf(l *Library, targets []*Target) []Found { switch { case f == nil: s.Agents = []string{t.Agent.ID} - byName[name] = &Found{Server: s, Own: appOwned(s)} + byName[name] = &Found{Server: s, Own: appOwned(s, t.Agent)} names = append(names, name) case f.Server.same(s): f.Server.Agents = append(f.Server.Agents, t.Agent.ID)