Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

library: OpenCode 2's mcp.servers is read, written and emptied as servers, not taken for one

yetonefce0d84756d6
diff --git a/internal/library/mcp.go b/internal/library/mcp.go
index 26094add5..effbcdcbb 100644
--- a/internal/library/mcp.go
+++ b/internal/library/mcp.go
@@ -977,7 +977,15 @@ func (f *mcpFile) parseEntries(raw []byte) (map[string]map[string]any, error) {
for _, k := range strings.Split(f.key(), ".") {
all, _ = all[k].(map[string]any)
}
+ v2 := openCodeServers(f.Format, all)
for name, v := range all {
+ if m, ok := v.(map[string]any); ok && (v2 == nil || name != "servers") {
+ out[name] = m
+ }
+ }
+ // OpenCode 2's own entries win a name over a V1 one, as OpenCode
+ // reads them
+ for name, v := range v2 {
if m, ok := v.(map[string]any); ok {
out[name] = m
}
@@ -985,6 +993,77 @@ func (f *mcpFile) parseEntries(raw []byte) (map[string]map[string]any, error) {
return out, nil
}
+// openCodeServers is OpenCode 2's mcp.servers, where its native config and
+// its own `opencode mcp add` keep the servers ("Configure MCP servers under
+// `mcp.servers`", opencode 2.0.25), or nil. OpenCode 2 reads the V1 shape,
+// mcp.<name>, beside it, so one file can hold both, and "servers" there is
+// that object, not a server called servers: one with a type, command or
+// url of its own is a V1 server by that name.
+func openCodeServers(format mcpFormat, mcp map[string]any) map[string]any {
+ if format != fmtOpenCode {
+ return nil
+ }
+ m, ok := mcp["servers"].(map[string]any)
+ if !ok {
+ return nil
+ }
+ for _, k := range []string{"type", "command", "url"} {
+ if _, own := m[k]; own {
+ return nil
+ }
+ }
+ return m
+}
+
+// openCodeLayout is what the OpenCode file at path holds under mcp: the
+// names of its V1 entries (mcp.<name>), those of OpenCode 2's own
+// (mcp.servers.<name>), and whether it has mcp.servers at all.
+func openCodeLayout(path string) (v1, v2 map[string]bool, native bool, err error) {
+ raw, err := edit.Read(path)
+ if errors.Is(err, fs.ErrNotExist) {
+ return nil, nil, false, nil
+ }
+ if err != nil {
+ return nil, nil, false, err
+ }
+ raw = edit.TrimBOM(raw)
+ if len(bytes.TrimSpace(raw)) == 0 {
+ return nil, nil, false, nil
+ }
+ var doc map[string]any
+ if err := json.Unmarshal(jsonc.ToJSON(raw), &doc); err != nil {
+ return nil, nil, false, fmt.Errorf("%s: %w", path, err)
+ }
+ all, _ := doc["mcp"].(map[string]any)
+ servers := openCodeServers(fmtOpenCode, all)
+ v1, v2 = map[string]bool{}, map[string]bool{}
+ for name, v := range all {
+ if _, ok := v.(map[string]any); ok && (servers == nil || name != "servers") {
+ v1[name] = true
+ }
+ }
+ for name, v := range servers {
+ if _, ok := v.(map[string]any); ok {
+ v2[name] = true
+ }
+ }
+ return v1, v2, servers != nil, nil
+}
+
+// openCodePath is the key path of the server's entry in the OpenCode file:
+// OpenCode 2's mcp.servers.<name> once the file keeps its servers there
+// (the entry there already, or a new one), else mcp.<name>.
+func (f *mcpFile) openCodePath(name string) (string, bool, error) {
+ v1, v2, native, err := openCodeLayout(f.Path)
+ if err != nil {
+ return "", false, err
+ }
+ if native && (v2[name] || !v1[name]) {
+ return "mcp.servers." + name, true, nil
+ }
+ return "mcp." + name, false, nil
+}
+
// cloneAny is a deep copy of a parsed value, so a caller can change what
// entries gives without changing what the next one gets.
func cloneAny(v any) any {
@@ -1235,7 +1314,19 @@ func (f *mcpFile) putEntry(s *Server, o ordered) error {
return err
}
}
- return edit.SetJSON(f.Path, edit.KV{Path: f.key() + "." + s.Name, Value: o})
+ key := f.key() + "." + s.Name
+ if f.Format == fmtOpenCode {
+ p, native, err := f.openCodePath(s.Name)
+ if err != nil {
+ return err
+ }
+ // OpenCode 2's servers have no enabled: on unless disabled,
+ // which is the user's to set
+ if key = p; native {
+ o = slices.DeleteFunc(slices.Clone(o), func(e kv) bool { return e.k == "enabled" })
+ }
+ }
+ return edit.SetJSON(f.Path, edit.KV{Path: key, Value: o})
}, f.files()...)
}
@@ -1262,6 +1353,22 @@ func (f *mcpFile) del(name string) error {
return err
}
}
+ if f.Format == fmtOpenCode {
+ // from wherever OpenCode reads it: mcp.<name>, OpenCode 2's
+ // mcp.servers.<name>, or both, and never mcp.servers itself
+ v1, v2, _, err := openCodeLayout(f.Path)
+ if err != nil {
+ return err
+ }
+ var paths []string
+ if v1[name] {
+ paths = append(paths, "mcp."+name)
+ }
+ if v2[name] {
+ paths = append(paths, "mcp.servers."+name)
+ }
+ return edit.DelJSON(f.Path, paths...)
+ }
return edit.DelJSON(f.Path, f.key()+"."+name)
}, f.files()...)
}
diff --git a/internal/library/mcp_opencode2_test.go b/internal/library/mcp_opencode2_test.go
new file mode 100644
index 000000000..f85d75ad5
--- /dev/null
+++ b/internal/library/mcp_opencode2_test.go
@@ -0,0 +1,112 @@
+package library
+
+import (
+ "encoding/json"
+ "path/filepath"
+ "slices"
+ "strings"
+ "testing"
+)
+
+// OpenCode 2 keeps its servers under mcp.servers (its own `opencode mcp
+// add` writes them there), beside the V1 mcp.<name> entries it still
+// reads. liam_8829 (Discord #feedback, 10-27) turned off every MCP server
+// in magpie and found a "servers" node with no type and no enabled left
+// in opencode.json: magpie read mcp.servers as nothing at all, so the
+// servers in it were never found, never taken off, and magpie's own went
+// in beside them in the other shape.
+func TestOpenCode2Servers(t *testing.T) {
+ h := sandbox(t)
+ oc := filepath.Join(h, ".config", "opencode", "opencode.json")
+ // as opencode 2.0.25's mcp add --global wrote it, with a V1 server
+ // and a V2 one the user keeps for themselves
+ write(t, oc, `{
+ "$schema": "https://opencode.ai/config.json",
+ "mcp": {
+ "mine": {"type": "local", "command": ["node", "x.js"], "enabled": false},
+ "servers": {
+ "ctx7": {"type": "remote", "url": "https://mcp.context7.com/mcp"},
+ "fs": {
+ "type": "local",
+ "command": ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
+ "environment": {"A": "b"},
+ "timeout": 9000
+ },
+ "own": {"type": "local", "command": ["uvx", "own-mcp"], "disabled": true}
+ }
+ }
+}
+`)
+ l, err := load()
+ if err != nil {
+ t.Fatal(err)
+ }
+ var found []string
+ for _, f := range foundServersOf(l, Targets()) {
+ found = append(found, f.Server.Name)
+ }
+ if want := []string{"ctx7", "fs", "mine", "own"}; !slices.Equal(found, want) {
+ t.Fatalf("found %v, want %v (mcp.servers' own, never a server called servers)", found, want)
+ }
+ for _, n := range []string{"ctx7", "fs"} {
+ ok(t)(ImportServer(n))
+ }
+ ok(t)(SaveServer("", Server{Name: "gh", Transport: "http", URL: "https://api.githubcopilot.com/mcp/", Agents: []string{"opencode"}}))
+
+ var doc struct {
+ MCP map[string]json.RawMessage `json:"mcp"`
+ }
+ parse := func() map[string]map[string]any {
+ t.Helper()
+ doc.MCP = nil
+ if err := json.Unmarshal([]byte(read(t, oc)), &doc); err != nil {
+ t.Fatalf("opencode.json: %v\n%s", err, read(t, oc))
+ }
+ var servers map[string]map[string]any
+ if err := json.Unmarshal(doc.MCP["servers"], &servers); err != nil {
+ t.Fatalf("mcp.servers: %v\n%s", err, read(t, oc))
+ }
+ return servers
+ }
+ servers := parse()
+ gh := servers["gh"]
+ if gh == nil || gh["url"] != "https://api.githubcopilot.com/mcp/" {
+ t.Fatalf("gh isn't written where the file keeps its servers:\n%s", read(t, oc))
+ }
+ if _, set := gh["enabled"]; set {
+ t.Errorf("an OpenCode 2 server has no enabled (it's on unless disabled):\n%s", read(t, oc))
+ }
+ if _, v1 := doc.MCP["gh"]; v1 {
+ t.Errorf("gh written in the V1 shape too:\n%s", read(t, oc))
+ }
+ if fs := servers["fs"]; fs["timeout"] != float64(9000) {
+ t.Errorf("fs lost the user's timeout:\n%s", read(t, oc))
+ }
+
+ // every server off, as the page's "servers/agents-all" does
+ ok(t)(EveryServerAgents([]string{"opencode"}, false))
+ servers = parse()
+ for _, n := range []string{"ctx7", "fs", "gh"} {
+ if servers[n] != nil || doc.MCP[n] != nil {
+ t.Errorf("%s is still in opencode.json after every server was turned off:\n%s", n, read(t, oc))
+ }
+ }
+ if !strings.Contains(read(t, oc), `"own": {"type": "local", "command": ["uvx", "own-mcp"], "disabled": true}`) ||
+ !strings.Contains(read(t, oc), `"mine": {"type": "local", "command": ["node", "x.js"], "enabled": false}`) {
+ t.Errorf("the user's own servers changed:\n%s", read(t, oc))
+ }
+ // what is left is what OpenCode reads: a server under mcp is one with
+ // a type, and mcp.servers is the servers' object
+ for name, raw := range doc.MCP {
+ var m map[string]any
+ _ = json.Unmarshal(raw, &m)
+ if name != "servers" && m["type"] == nil {
+ t.Errorf("mcp.%s has no type:\n%s", name, read(t, oc))
+ }
+ }
+ for name, m := range servers {
+ if m["type"] == nil {
+ t.Errorf("mcp.servers.%s has no type:\n%s", name, read(t, oc))
+ }
+ }
+}