Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-8397: fix(daemon): parse OpenCode 2.x nested MCP server config (#9713)
Martin Montescd768e979395
diff --git a/server/internal/daemon/runtime_mcp.go b/server/internal/daemon/runtime_mcp.goindex 41aefae0d8..04d3ca05e4 100644--- a/server/internal/daemon/runtime_mcp.go+++ b/server/internal/daemon/runtime_mcp.go@@ -315,8 +315,13 @@ func loadRuntimeMcpServerConfigs(provider string) (map[string]any, bool, error)if err != nil {return nil, true, err}- if configured, ok := nestedRuntimeMcpMap(cfg, key); ok {+ if configured, ok := runtimeMcpServersForProvider(provider, key, cfg); ok {for name, entry := range configured {+ // OpenCode 2.x uses disabled, but the task adapters only read+ // enabled. Keep disabled entries in inventory, not in task config.+ if server, ok := entry.(map[string]any); provider == "opencode" && ok && server["disabled"] == true {+ continue+ }servers[name] = normalizeRuntimeMcpEntry(provider, entry)}}@@ -464,7 +469,7 @@ func listRuntimeLocalMcpServers(provider string) ([]runtimeLocalMcpServerSummaryif err != nil {return nil, true, err}- if servers, ok := nestedRuntimeMcpMap(cfg, key); ok {+ if servers, ok := runtimeMcpServersForProvider(provider, key, cfg); ok {out = append(out, runtimeMcpSummaries(servers, source)...)}} else if !os.IsNotExist(err) {@@ -545,6 +550,40 @@ func listClaudePluginMcpServers(home string) []runtimeLocalMcpServerSummary {return out}+// runtimeMcpServersForProvider resolves the user-scope MCP server map for a+// provider. OpenCode is the only provider that stores the same field under two+// shapes depending on which version wrote the config file, so it gets a+// dedicated resolver; every other provider uses the dotted-key lookup.+func runtimeMcpServersForProvider(provider, key string, cfg map[string]any) (map[string]any, bool) {+ if provider == "opencode" {+ return opencodeNativeMcpServers(cfg, key)+ }+ return nestedRuntimeMcpMap(cfg, key)+}++// opencodeNativeMcpServers returns the OpenCode user MCP server map. OpenCode+// 1.x stores servers directly under "mcp" while 2.x nests them under+// "mcp.servers"; the shape is decided by whichever version the user runs, so it+// is detected from the content rather than a fixed key.+func opencodeNativeMcpServers(cfg map[string]any, key string) (map[string]any, bool) {+ mcp, ok := cfg[key].(map[string]any)+ if !ok {+ return nil, false+ }+ if servers, ok := mcp["servers"].(map[string]any); ok {+ // A flat server named "servers" has entry fields; a nested server+ // with one of these names has an object as its value instead.+ for _, field := range []string{"type", "url", "command", "enabled", "disabled"} {+ switch servers[field].(type) {+ case string, bool, []any:+ return mcp, true+ }+ }+ return servers, true+ }+ return mcp, true+}+func nestedRuntimeMcpMap(cfg map[string]any, path string) (map[string]any, bool) {current := cfgparts := strings.Split(path, ".")diff --git a/server/internal/daemon/runtime_mcp_test.go b/server/internal/daemon/runtime_mcp_test.goindex 2daab8cd39..5afbd902ca 100644--- a/server/internal/daemon/runtime_mcp_test.go+++ b/server/internal/daemon/runtime_mcp_test.go@@ -602,6 +602,280 @@ func TestListRuntimeLocalMcpServersKimi(t *testing.T) {}}+// OpenCode 1.x stores user MCP servers directly under "mcp"; 2.x nests them+// under "mcp.servers". The shape is written by whichever version the user runs,+// so the daemon must detect it from the content. The regression this guards: a+// 2.x config was previously read as a single literal "servers" entry.+func TestOpencodeNativeMcpServers(t *testing.T) {+ cases := []struct {+ name string+ cfgJSON string+ wantOK bool+ want []string+ }{+ {+ name: "v2 nested under mcp.servers",+ cfgJSON: `{"mcp":{"servers":{"github":{"type":"remote","url":"https://g.example/mcp"},"grafana":{"type":"local","command":["grafana-mcp"]}}}}`,+ wantOK: true,+ want: []string{"github", "grafana"},+ },+ {+ name: "v1 flat under mcp",+ cfgJSON: `{"mcp":{"github":{"type":"remote","url":"https://g.example/mcp"},"grafana":{"type":"local","command":["grafana-mcp"]}}}`,+ wantOK: true,+ want: []string{"github", "grafana"},+ },+ {+ name: "v1 local server named servers",+ cfgJSON: `{"mcp":{"servers":{"type":"local","command":["local-mcp"]},"github":{"type":"remote","url":"https://g.example/mcp"}}}`,+ wantOK: true,+ want: []string{"servers", "github"},+ },+ {+ name: "v1 remote server named servers",+ cfgJSON: `{"mcp":{"servers":{"type":"remote","url":"https://g.example/mcp"}}}`,+ wantOK: true,+ want: []string{"servers"},+ },+ {+ name: "v1 enabled override named servers",+ cfgJSON: `{"mcp":{"servers":{"enabled":false}}}`,+ wantOK: true,+ want: []string{"servers"},+ },+ {+ name: "v2 server names matching entry fields",+ cfgJSON: `{"mcp":{"servers":{"type":{"type":"local","command":["a"]},"url":{"type":"local","command":["b"]},"command":{"type":"local","command":["c"]},"enabled":{"type":"local","command":["d"]},"disabled":{"type":"local","command":["e"]}}}}`,+ wantOK: true,+ want: []string{"type", "url", "command", "enabled", "disabled"},+ },+ {+ name: "no mcp key",+ cfgJSON: `{"$schema":"https://opencode.ai/config.json"}`,+ wantOK: false,+ want: nil,+ },+ {+ name: "mcp is not a map",+ cfgJSON: `{"mcp":"oops"}`,+ wantOK: false,+ want: nil,+ },+ {+ name: "v2 empty servers",+ cfgJSON: `{"mcp":{"servers":{}}}`,+ wantOK: true,+ want: nil,+ },+ {+ name: "v1 empty mcp",+ cfgJSON: `{"mcp":{}}`,+ wantOK: true,+ want: nil,+ },+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ var cfg map[string]any+ if err := json.Unmarshal([]byte(tc.cfgJSON), &cfg); err != nil {+ t.Fatalf("unmarshal fixture: %v", err)+ }+ got, ok := opencodeNativeMcpServers(cfg, "mcp")+ if ok != tc.wantOK {+ t.Fatalf("ok = %v, want %v (got=%#v)", ok, tc.wantOK, got)+ }+ if !ok {+ return+ }+ if len(got) != len(tc.want) {+ t.Fatalf("got %d servers %#v, want %d %v", len(got), got, len(tc.want), tc.want)+ }+ for _, name := range tc.want {+ if _, exists := got[name]; !exists {+ t.Fatalf("server %q missing: %#v", name, got)+ }+ }+ })+ }+}++// Both OpenCode layouts must yield actual server names before task-local merging.+func TestLoadRuntimeMcpServerConfigsOpencode(t *testing.T) {+ cases := []struct {+ name string+ config string+ want []string+ }{+ {+ name: "v2 nested config parses real server names",+ config: `{"$schema":"https://opencode.ai/config.json","mcp":{"servers":{` ++ `"github":{"type":"remote","url":"https://g.example/mcp","headers":{"Authorization":"{env:GITHUB_MCP_TOKEN}"}},` ++ `"grafana":{"type":"local","command":["grafana-mcp","start"]}}}}`,+ want: []string{"github", "grafana"},+ },+ {+ name: "v1 flat config still parses",+ config: `{"mcp":{"github":{"type":"remote","url":"https://g.example/mcp"},"grafana":{"type":"local","command":["grafana-mcp","start"]}}}`,+ want: []string{"github", "grafana"},+ },+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ redirectTestHome(t, t.TempDir())+ configDir := t.TempDir()+ t.Setenv("XDG_CONFIG_HOME", configDir)+ opencodeDir := filepath.Join(configDir, "opencode")+ if err := os.MkdirAll(opencodeDir, 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(opencodeDir, "opencode.json"), []byte(tc.config), 0o600); err != nil {+ t.Fatal(err)+ }++ servers, supported, err := loadRuntimeMcpServerConfigs("opencode")+ if err != nil {+ t.Fatal(err)+ }+ if !supported {+ t.Fatal("opencode reported unsupported")+ }+ if _, misparsed := servers["servers"]; misparsed {+ t.Fatalf("config was mis-parsed as a literal %q server: %#v", "servers", servers)+ }+ if len(servers) != len(tc.want) {+ t.Fatalf("got %d servers, want %d: %#v", len(servers), len(tc.want), servers)+ }+ for _, name := range tc.want {+ if _, ok := servers[name]; !ok {+ t.Fatalf("server %q missing: %#v", name, servers)+ }+ }+ })+ }+}++func TestMergeRuntimeAndAgentMcpConfigOpencodeDisabledServers(t *testing.T) {+ cases := []struct {+ name string+ agentConfig string+ want []string+ }{+ {+ name: "disabled runtime servers never reach the task",+ agentConfig: `{"mcpServers":{"managed":{"command":"managed-mcp"}}}`,+ want: []string{"local-on", "remote-on", "managed"},+ },+ {+ name: "explicit agent entry still wins a same-name collision",+ agentConfig: `{"mcpServers":{"local-off":{"command":"managed-override"}}}`,+ want: []string{"local-on", "remote-on", "local-off"},+ },+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ writeOpencodeRuntimeConfig(t, `{"servers":{+ "local-off":{"type":"local","command":["disabled-local"],"disabled":true},+ "remote-off":{"type":"remote","url":"https://disabled.example.invalid/mcp","disabled":true},+ "local-on":{"type":"local","command":["enabled-local"]},+ "remote-on":{"type":"remote","url":"https://enabled.example.invalid/mcp","disabled":false}+ }}`)++ inventory, supported, err := listRuntimeLocalMcpServers("opencode")+ if err != nil {+ t.Fatal(err)+ }+ if !supported || len(inventory) != 4 {+ t.Fatalf("supported=%v inventory=%#v", supported, inventory)+ }+ for _, server := range inventory {+ wantEnabled := server.Name == "local-on" || server.Name == "remote-on"+ if server.Enabled != wantEnabled {+ t.Errorf("inventory server %q enabled=%v, want %v", server.Name, server.Enabled, wantEnabled)+ }+ }++ merged, err := mergeRuntimeAndAgentMcpConfig("opencode", json.RawMessage(tc.agentConfig))+ if err != nil {+ t.Fatal(err)+ }+ var document struct {+ Servers map[string]map[string]any `json:"mcpServers"`+ }+ if err := json.Unmarshal(merged, &document); err != nil {+ t.Fatal(err)+ }+ if len(document.Servers) != len(tc.want) {+ t.Fatalf("task servers=%s, want only %v", merged, tc.want)+ }+ for _, name := range tc.want {+ if _, ok := document.Servers[name]; !ok {+ t.Errorf("task server %q missing: %s", name, merged)+ }+ }+ if entry, ok := document.Servers["local-off"]; ok && entry["command"] != "managed-override" {+ t.Fatalf("agent override lost: %#v", entry)+ }+ })+ }+}++// The same shape detection must hold on the public inventory path that feeds+// the agent capabilities endpoint.+func TestListRuntimeLocalMcpServersOpencode(t *testing.T) {+ cases := []struct {+ name string+ config string+ want []string+ }{+ {+ name: "v2 nested config lists real server names",+ config: `{"mcp":{"servers":{` ++ `"github":{"type":"remote","url":"https://g.example/mcp","disabled":false},` ++ `"grafana":{"type":"local","command":["grafana-mcp","start"]}}}}`,+ want: []string{"github", "grafana"},+ },+ {+ name: "v1 flat config lists real server names",+ config: `{"mcp":{"github":{"type":"remote","url":"https://g.example/mcp"},"grafana":{"type":"local","command":["grafana-mcp","start"]}}}`,+ want: []string{"github", "grafana"},+ },+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ redirectTestHome(t, t.TempDir())+ configDir := t.TempDir()+ t.Setenv("XDG_CONFIG_HOME", configDir)+ opencodeDir := filepath.Join(configDir, "opencode")+ if err := os.MkdirAll(opencodeDir, 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(opencodeDir, "opencode.json"), []byte(tc.config), 0o600); err != nil {+ t.Fatal(err)+ }++ servers, supported, err := listRuntimeLocalMcpServers("opencode")+ if err != nil {+ t.Fatal(err)+ }+ if !supported {+ t.Fatal("opencode reported unsupported")+ }+ if len(servers) != len(tc.want) {+ t.Fatalf("got %d servers, want %d: %#v", len(servers), len(tc.want), servers)+ }+ byName := make(map[string]runtimeLocalMcpServerSummary, len(servers))+ for _, server := range servers {+ byName[server.Name] = server+ }+ for _, name := range tc.want {+ if _, ok := byName[name]; !ok {+ t.Fatalf("server %q missing: %#v", name, servers)+ }+ }+ })+ }+}+// kimi merges <KIMI_CODE_HOME>/mcp.json with the ephemeral `mcpServers` array// the ACP backend sends in session/new, and a duplicate name spawns the server// twice. So kimi is inventory-only: the task-local merge must leave the agent's