From 5fa0e4c6ef334d6e7d40a90593f1872709768fc7 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 11:45:13 +0000 Subject: [PATCH 01/21] Add MCP server config writers for coding agents Add a per-agent description of where each coding agent keeps its user-scope MCP server entries, and the writers that add, remove and list the entries this CLI owns without touching anything else in the file. - libs/aitools/mcpconfig: Spec (path, format, root key, entry shape, ownership). - libs/aitools/agents: MCP spec for seven agents, nil for the others; SupportsMCP and MCPAgents. - libs/aitools/mcp/agentconfig: Upsert, Remove and Read over JSON and TOML config files. Writes are atomic, keep the file mode, write through symlinks, and skip the write when the entry already matches. Files that cannot be parsed are refused and left untouched. Entries not written by this CLI are refused unless forced. JSON keeps key order and the bytes of values it does not change. TOML is decoded and re-encoded, so comments are dropped on any rewrite. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 178 ++++++++++ libs/aitools/agents/agents_test.go | 86 +++++ libs/aitools/mcp/agentconfig/json.go | 220 ++++++++++++ .../testdata/antigravity/after.json | 27 ++ .../testdata/antigravity/before.json | 9 + .../testdata/claude-code/after.json | 32 ++ .../testdata/claude-code/before.json | 16 + .../mcp/agentconfig/testdata/codex/after.toml | 13 + .../agentconfig/testdata/codex/before.toml | 9 + .../agentconfig/testdata/copilot/after.json | 34 ++ .../agentconfig/testdata/copilot/before.json | 11 + .../agentconfig/testdata/cursor/after.json | 26 ++ .../agentconfig/testdata/cursor/before.json | 9 + .../agentconfig/testdata/gemini/after.json | 28 ++ .../agentconfig/testdata/gemini/before.json | 11 + .../agentconfig/testdata/opencode/after.json | 31 ++ .../agentconfig/testdata/opencode/before.json | 12 + libs/aitools/mcp/agentconfig/toml.go | 103 ++++++ libs/aitools/mcp/agentconfig/unmanaged.go | 20 ++ .../aitools/mcp/agentconfig/unmanaged_test.go | 50 +++ libs/aitools/mcp/agentconfig/writer.go | 238 +++++++++++++ libs/aitools/mcp/agentconfig/writer_test.go | 323 ++++++++++++++++++ libs/aitools/mcpconfig/spec.go | 90 +++++ libs/aitools/mcpconfig/spec_test.go | 67 ++++ 24 files changed, 1643 insertions(+) create mode 100644 libs/aitools/mcp/agentconfig/json.go create mode 100644 libs/aitools/mcp/agentconfig/testdata/antigravity/after.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/antigravity/before.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/claude-code/after.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/claude-code/before.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/codex/after.toml create mode 100644 libs/aitools/mcp/agentconfig/testdata/codex/before.toml create mode 100644 libs/aitools/mcp/agentconfig/testdata/copilot/after.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/copilot/before.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/cursor/after.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/cursor/before.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/gemini/after.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/gemini/before.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/opencode/after.json create mode 100644 libs/aitools/mcp/agentconfig/testdata/opencode/before.json create mode 100644 libs/aitools/mcp/agentconfig/toml.go create mode 100644 libs/aitools/mcp/agentconfig/unmanaged.go create mode 100644 libs/aitools/mcp/agentconfig/unmanaged_test.go create mode 100644 libs/aitools/mcp/agentconfig/writer.go create mode 100644 libs/aitools/mcp/agentconfig/writer_test.go create mode 100644 libs/aitools/mcpconfig/spec.go create mode 100644 libs/aitools/mcpconfig/spec_test.go diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 92b11077d14..04b5f3c60bc 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -7,6 +7,7 @@ import ( "runtime" "strings" + "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/env" ) @@ -56,6 +57,9 @@ type Agent struct { // Plugin describes the databricks plugin for this agent, or nil when the // agent has no plugin and skills files are its native delivery. Plugin *PluginSpec + // MCP describes where the agent keeps user-scope MCP server entries, or nil + // when this CLI does not write them (Pi, Goose, Kiro). + MCP *mcpconfig.Spec // pluginInstalls reads the databricks plugin's install records from the // agent's own plugin manifest. Each agent's manifest format differs, so it is // set per agent (only for formats we have verified); when nil, @@ -182,6 +186,13 @@ var Registry = []*Agent{ Binary: "claude", Plugin: claudePlugin(), pluginInstalls: claudePluginInstalls, + MCP: &mcpconfig.Spec{ + Path: claudeMCPPath, + Format: mcpconfig.FormatJSON, + Root: "mcpServers", + Entry: claudeMCPEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, }, { Name: NameCursor, @@ -193,6 +204,13 @@ var Registry = []*Agent{ // Cursor has a databricks plugin, but it can't be installed headlessly, so // the CLI treats Cursor as a skills-only agent (Plugin nil) rather than // referencing a plugin it can't act on. + MCP: &mcpconfig.Spec{ + Path: homeSubdir(".cursor", "mcp.json"), + Format: mcpconfig.FormatJSON, + Root: "mcpServers", + Entry: stdioEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, }, { Name: NameCodex, @@ -200,6 +218,13 @@ var Registry = []*Agent{ ConfigDir: homeSubdir(".codex"), Binary: "codex", Plugin: databricksPlugin(), + MCP: &mcpconfig.Spec{ + Path: codexMCPPath, + Format: mcpconfig.FormatTOML, + Root: "mcp_servers", + Entry: stdioEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, }, { Name: NameOpenCode, @@ -209,6 +234,13 @@ var Registry = []*Agent{ // OpenCode exposes an `opencode plugin ` command, but that's an // npm-module system, not the agent-skills marketplace, so OpenCode stays // skills-only (Plugin nil). + MCP: &mcpconfig.Spec{ + Path: openCodeMCPPath, + Format: mcpconfig.FormatJSON, + Root: "mcp.servers", + Entry: openCodeMCPEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, }, { Name: NameCopilot, @@ -216,6 +248,13 @@ var Registry = []*Agent{ ConfigDir: homeSubdir(".copilot"), Binary: "copilot", Plugin: databricksPlugin(), + MCP: &mcpconfig.Spec{ + Path: copilotMCPPath, + Format: mcpconfig.FormatJSON, + Root: "mcpServers", + Entry: copilotMCPEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, }, { Name: NameAntigravity, @@ -223,6 +262,13 @@ var Registry = []*Agent{ ConfigDir: homeSubdir(".gemini", "antigravity"), SkillsSubdir: "global_skills", // Antigravity is IDE-only with no CLI binary, so it has no plugin path. + MCP: &mcpconfig.Spec{ + Path: antigravityMCPPath, + Format: mcpconfig.FormatJSON, + Root: "mcpServers", + Entry: antigravityMCPEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, }, { Name: NamePi, @@ -246,6 +292,13 @@ var Registry = []*Agent{ // Gemini writes projects.json after real use. Antigravity shares ~/.gemini, // and installation_id is not reliable, so detection uses this Gemini-only file. MandatoryFile: "projects.json", + MCP: &mcpconfig.Spec{ + Path: geminiMCPPath, + Format: mcpconfig.FormatJSON, + Root: "mcpServers", + Entry: stdioEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, }, { Name: NameGoose, @@ -374,6 +427,131 @@ func gooseConfigDir(ctx context.Context) (string, error) { return filepath.Join(xdg, "goose"), nil } +// claudeMCPPath returns Claude Code's global config file, which holds user-scope +// mcpServers next to auth state. It sits in the home directory, not in ~/.claude; +// CLAUDE_CONFIG_DIR moves it to /.claude.json. +// https://code.claude.com/docs/en/env-vars +func claudeMCPPath(ctx context.Context) (string, error) { + if env.Get(ctx, "CLAUDE_CONFIG_DIR") != "" { + dir, err := claudeConfigDir(ctx) + if err != nil { + return "", err + } + return filepath.Join(dir, ".claude.json"), nil + } + return homeSubdir(".claude.json")(ctx) +} + +// claudeMCPEntry matches `claude mcp add`, which always writes an env map, even an empty one. +func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { + if len(vars) == 0 { + vars = map[string]string{} + } + return map[string]any{"type": "stdio", "command": cmd, "args": args, "env": vars} +} + +// codexMCPPath returns Codex's config.toml, which honors CODEX_HOME. +func codexMCPPath(ctx context.Context) (string, error) { + if home := env.Get(ctx, "CODEX_HOME"); home != "" { + return filepath.Join(home, "config.toml"), nil + } + return homeSubdir(".codex", "config.toml")(ctx) +} + +// openCodeMCPPath returns OpenCode's config file. The file may contain comments +// (JSONC); a file we cannot parse is refused, never rewritten. +// https://opencode.ai/docs/config/ +func openCodeMCPPath(ctx context.Context) (string, error) { + dir, err := openCodeConfigDir(ctx) + if err != nil { + return "", err + } + return filepath.Join(dir, "opencode.json"), nil +} + +// openCodeMCPEntry follows the current OpenCode schema: servers live under +// mcp.servers, the env map is "environment", and there is no "enabled" key +// (the schema forbids extra keys, and an absent key means enabled). +// https://opencode.ai/docs/mcp-servers/ +func openCodeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { + entry := map[string]any{"type": "local", "command": append([]string{cmd}, args...)} + if len(vars) > 0 { + entry["environment"] = vars + } + return entry +} + +// copilotMCPPath returns Copilot CLI's user config. COPILOT_HOME replaces ~/.copilot. +// https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference +func copilotMCPPath(ctx context.Context) (string, error) { + if home := env.Get(ctx, "COPILOT_HOME"); home != "" { + return filepath.Join(home, "mcp-config.json"), nil + } + return homeSubdir(".copilot", "mcp-config.json")(ctx) +} + +// copilotMCPEntry is the stdio entry Copilot CLI documents, with all tools enabled. +// https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/add-mcp-servers +func copilotMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { + entry := stdioEntry(cmd, args, vars) + entry["type"] = "local" + entry["tools"] = []string{"*"} + return entry +} + +// geminiMCPPath returns Gemini CLI's user settings, which hold mcpServers. The +// settings directory follows geminiConfigDir, which honors GEMINI_CLI_HOME. +// https://geminicli.com/docs/tools/mcp-server/ +func geminiMCPPath(ctx context.Context) (string, error) { + dir, err := geminiConfigDir(ctx) + if err != nil { + return "", err + } + return filepath.Join(dir, "settings.json"), nil +} + +// antigravityMCPPath is the global MCP file in Antigravity's docs, for both the +// CLI and the IDE. It is not the skills directory (~/.gemini/antigravity). +// https://antigravity.google/docs/mcp +func antigravityMCPPath(ctx context.Context) (string, error) { + return homeSubdir(".gemini", "config", "mcp_config.json")(ctx) +} + +// antigravityMCPEntry writes disabled:false explicitly, as the Antigravity docs +// show it. +func antigravityMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { + entry := stdioEntry(cmd, args, vars) + entry["disabled"] = false + return entry +} + +// stdioEntry is the command/args entry used by Cursor, Codex and Gemini. env is +// omitted when empty. +func stdioEntry(cmd string, args []string, vars map[string]string) map[string]any { + entry := map[string]any{"command": cmd, "args": args} + if len(vars) > 0 { + entry["env"] = vars + } + return entry +} + +// SupportsMCP reports whether this CLI writes the agent's MCP server entries. +func (a *Agent) SupportsMCP() bool { + return a.MCP != nil +} + +// MCPAgents returns the agents whose MCP server entries this CLI writes, in +// registry order. +func MCPAgents() []*Agent { + var agents []*Agent + for _, a := range Registry { + if a.SupportsMCP() { + agents = append(agents, a) + } + } + return agents +} + // ByName returns the registry agent with the given name, or nil if not found. func ByName(name string) *Agent { for _, a := range Registry { diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index 79cc0b79f58..8d7b8c0eb72 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -1,6 +1,7 @@ package agents import ( + "context" "path/filepath" "testing" @@ -43,6 +44,91 @@ func TestSupportedNamesMatchesRegistry(t *testing.T) { } } +func TestMCPAgents(t *testing.T) { + var names []string + for _, a := range MCPAgents() { + names = append(names, a.Name) + } + assert.Equal(t, []string{ + NameClaudeCode, NameCursor, NameCodex, NameOpenCode, NameCopilot, NameAntigravity, NameGemini, + }, names) + for _, name := range []string{NamePi, NameGoose, NameKiro} { + assert.False(t, ByName(name).SupportsMCP(), name) + } +} + +// clearMCPOverrides returns ctx with a home dir and no override variables, so +// the path tests do not depend on the developer's shell. +func clearMCPOverrides(t *testing.T) (context.Context, string) { + for _, key := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA"} { + t.Setenv(key, "") + } + home := t.TempDir() + return env.WithUserHomeDir(t.Context(), home), home +} + +func TestMCPPaths(t *testing.T) { + ctx, home := clearMCPOverrides(t) + custom := t.TempDir() + for _, tc := range []struct { + name string + agent string + override map[string]string + want string + }{ + {"claude default", NameClaudeCode, nil, filepath.Join(home, ".claude.json")}, + {"claude config dir", NameClaudeCode, map[string]string{"CLAUDE_CONFIG_DIR": custom}, filepath.Join(custom, ".claude.json")}, + {"cursor", NameCursor, nil, filepath.Join(home, ".cursor", "mcp.json")}, + {"codex default", NameCodex, nil, filepath.Join(home, ".codex", "config.toml")}, + {"codex home", NameCodex, map[string]string{"CODEX_HOME": custom}, filepath.Join(custom, "config.toml")}, + {"opencode default", NameOpenCode, nil, filepath.Join(home, ".config", "opencode", "opencode.json")}, + {"opencode xdg", NameOpenCode, map[string]string{"XDG_CONFIG_HOME": custom}, filepath.Join(custom, "opencode", "opencode.json")}, + {"copilot default", NameCopilot, nil, filepath.Join(home, ".copilot", "mcp-config.json")}, + {"copilot home", NameCopilot, map[string]string{"COPILOT_HOME": custom}, filepath.Join(custom, "mcp-config.json")}, + {"gemini default", NameGemini, nil, filepath.Join(home, ".gemini", "settings.json")}, + {"gemini cli home", NameGemini, map[string]string{"GEMINI_CLI_HOME": custom}, filepath.Join(custom, ".gemini", "settings.json")}, + // The MCP file is the one from the Antigravity docs, not the skills directory. + {"antigravity", NameAntigravity, nil, filepath.Join(home, ".gemini", "config", "mcp_config.json")}, + } { + t.Run(tc.name, func(t *testing.T) { + for k, v := range tc.override { + t.Setenv(k, v) + } + got, err := ByName(tc.agent).MCP.Path(ctx) + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } +} + +func TestMCPEntries(t *testing.T) { + args := []string{"aitools", "mcp", "serve"} + pinned := map[string]string{"DATABRICKS_CONFIG_FILE": "/home/u/.databrickscfg"} + + claude := ByName(NameClaudeCode).MCP.Entry("/bin/databricks", args, nil) + assert.Equal(t, map[string]any{"type": "stdio", "command": "/bin/databricks", "args": args, "env": map[string]string{}}, claude) + + codex := ByName(NameCodex).MCP.Entry("/bin/databricks", args, nil) + assert.Equal(t, map[string]any{"command": "/bin/databricks", "args": args}, codex, "env is omitted when empty") + assert.Equal(t, map[string]any{"command": "/bin/databricks", "args": args, "env": pinned}, + ByName(NameCodex).MCP.Entry("/bin/databricks", args, pinned)) + + copilot := ByName(NameCopilot).MCP.Entry("/bin/databricks", args, nil) + assert.Equal(t, "local", copilot["type"]) + assert.Equal(t, []string{"*"}, copilot["tools"]) + + // OpenCode uses one command array, the "environment" key and no "enabled" key. + opencode := ByName(NameOpenCode).MCP.Entry("/bin/databricks", args, pinned) + assert.Equal(t, map[string]any{ + "type": "local", + "command": []string{"/bin/databricks", "aitools", "mcp", "serve"}, + "environment": pinned, + }, opencode) + + antigravity := ByName(NameAntigravity).MCP.Entry("/bin/databricks", args, nil) + assert.Equal(t, false, antigravity["disabled"]) +} + func TestSkillsOnlyNamesMatchesRegistry(t *testing.T) { names := SkillsOnlyNames() // Skills-only agents (Plugin nil) are listed; plugin agents are not. diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go new file mode 100644 index 00000000000..d87149905a4 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/json.go @@ -0,0 +1,220 @@ +package agentconfig + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "slices" +) + +// jsonObject is a JSON object that keeps its keys in file order and each value +// as the original bytes, so a rewrite changes only the entry it was asked to. +type jsonObject struct { + keys []string + values map[string]json.RawMessage +} + +// parseJSONObject decodes a top-level JSON object. Empty input is an empty object. +func parseJSONObject(data []byte) (*jsonObject, error) { + obj := &jsonObject{values: map[string]json.RawMessage{}} + if len(bytes.TrimSpace(data)) == 0 { + return obj, nil + } + dec := json.NewDecoder(bytes.NewReader(data)) + tok, err := dec.Token() + if err != nil { + return nil, err + } + if tok != json.Delim('{') { + return nil, errors.New("top-level value is not a JSON object") + } + for dec.More() { + tok, err := dec.Token() + if err != nil { + return nil, err + } + key, ok := tok.(string) + if !ok { + return nil, errors.New("object key is not a string") + } + var raw json.RawMessage + if err := dec.Decode(&raw); err != nil { + return nil, err + } + obj.put(key, raw) + } + if _, err := dec.Token(); err != nil { + return nil, err + } + if _, err := dec.Token(); err != io.EOF { + return nil, errors.New("unexpected data after the JSON object") + } + return obj, nil +} + +func (o *jsonObject) put(key string, raw json.RawMessage) { + if _, ok := o.values[key]; !ok { + o.keys = append(o.keys, key) + } + o.values[key] = raw +} + +func (o *jsonObject) del(key string) { + if _, ok := o.values[key]; !ok { + return + } + delete(o.values, key) + o.keys = slices.DeleteFunc(o.keys, func(k string) bool { return k == key }) +} + +// child returns the object under key, or an empty one when key is absent. +func (o *jsonObject) child(key string) (*jsonObject, error) { + raw, ok := o.values[key] + if !ok { + return &jsonObject{values: map[string]json.RawMessage{}}, nil + } + child, err := parseJSONObject(raw) + if err != nil { + return nil, fmt.Errorf("%q is not a JSON object: %w", key, ErrConfigInvalid) + } + return child, nil +} + +// edit applies fn to the object at path, creating missing levels and writing +// every changed level back into its parent. +func (o *jsonObject) edit(path []string, fn func(*jsonObject)) error { + if len(path) == 0 { + fn(o) + return nil + } + child, err := o.child(path[0]) + if err != nil { + return err + } + if err := child.edit(path[1:], fn); err != nil { + return err + } + raw, err := child.compact() + if err != nil { + return err + } + o.put(path[0], raw) + return nil +} + +// compact serializes o without insignificant whitespace. +func (o *jsonObject) compact() ([]byte, error) { + var buf bytes.Buffer + buf.WriteByte('{') + for i, key := range o.keys { + if i > 0 { + buf.WriteByte(',') + } + name, err := json.Marshal(key) + if err != nil { + return nil, err + } + buf.Write(name) + buf.WriteByte(':') + buf.Write(o.values[key]) + } + buf.WriteByte('}') + return buf.Bytes(), nil +} + +// jsonDoc is an agent config file in JSON. +type jsonDoc struct { + root *jsonObject +} + +func parseJSON(data []byte) (document, error) { + root, err := parseJSONObject(data) + if err != nil { + return nil, err + } + return &jsonDoc{root: root}, nil +} + +// table returns the object at root. Missing levels read as empty objects. +func (d *jsonDoc) table(root []string) (*jsonObject, error) { + obj := d.root + for _, key := range root { + next, err := obj.child(key) + if err != nil { + return nil, err + } + obj = next + } + return obj, nil +} + +// asObject decodes raw as a JSON object. Other JSON values are reported as not ok. +func asObject(raw json.RawMessage) (map[string]any, bool) { + var v any + if err := json.Unmarshal(raw, &v); err != nil { + return nil, false + } + obj, ok := v.(map[string]any) + return obj, ok +} + +func (d *jsonDoc) entry(root []string, name string) (map[string]any, bool, error) { + table, err := d.table(root) + if err != nil { + return nil, false, err + } + raw, ok := table.values[name] + if !ok { + return nil, false, nil + } + entry, ok := asObject(raw) + if !ok { + return nil, false, fmt.Errorf("server %q is not a JSON object: %w", name, ErrConfigInvalid) + } + return entry, true, nil +} + +// entries returns the object-valued members under root. Other members are not +// server entries and are skipped. +func (d *jsonDoc) entries(root []string) (map[string]map[string]any, error) { + table, err := d.table(root) + if err != nil { + return nil, err + } + out := map[string]map[string]any{} + for _, key := range table.keys { + if entry, ok := asObject(table.values[key]); ok { + out[key] = entry + } + } + return out, nil +} + +func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { + raw, err := json.Marshal(entry) + if err != nil { + return err + } + return d.root.edit(root, func(table *jsonObject) { table.put(name, raw) }) +} + +func (d *jsonDoc) remove(root []string, name string) error { + return d.root.edit(root, func(table *jsonObject) { table.del(name) }) +} + +// bytes returns the document with two-space indentation and a trailing newline, +// which is what the agents write themselves. +func (d *jsonDoc) bytes() ([]byte, error) { + compact, err := d.root.compact() + if err != nil { + return nil, err + } + var out bytes.Buffer + if err := json.Indent(&out, compact, "", " "); err != nil { + return nil, err + } + out.WriteByte('\n') + return out.Bytes(), nil +} diff --git a/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json new file mode 100644 index 00000000000..502ad483c53 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json @@ -0,0 +1,27 @@ +{ + "mcpServers": { + "other": { + "command": "npx", + "args": [ + "-y", + "other-mcp" + ], + "disabled": false + }, + "genie_one_mcp-dogfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "dogfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "antigravity" + ], + "command": "/opt/databricks/bin/databricks", + "disabled": false + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/antigravity/before.json b/libs/aitools/mcp/agentconfig/testdata/antigravity/before.json new file mode 100644 index 00000000000..eb76a96fb79 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/antigravity/before.json @@ -0,0 +1,9 @@ +{ + "mcpServers": { + "other": { + "command": "npx", + "args": ["-y", "other-mcp"], + "disabled": false + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/claude-code/after.json b/libs/aitools/mcp/agentconfig/testdata/claude-code/after.json new file mode 100644 index 00000000000..865ab39981b --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/claude-code/after.json @@ -0,0 +1,32 @@ +{ + "numStartups": 4, + "theme": "dark", + "userID": "not-a-real-id", + "mcpServers": { + "other": { + "type": "http", + "url": "https://mcp.example.test/mcp" + }, + "genie_one_mcp-dogfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "dogfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "claude-code" + ], + "command": "/opt/databricks/bin/databricks", + "env": {}, + "type": "stdio" + } + }, + "projects": { + "/work": { + "allowedTools": [] + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/claude-code/before.json b/libs/aitools/mcp/agentconfig/testdata/claude-code/before.json new file mode 100644 index 00000000000..3f9213f6012 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/claude-code/before.json @@ -0,0 +1,16 @@ +{ + "numStartups": 4, + "theme": "dark", + "userID": "not-a-real-id", + "mcpServers": { + "other": { + "type": "http", + "url": "https://mcp.example.test/mcp" + } + }, + "projects": { + "/work": { + "allowedTools": [] + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/codex/after.toml b/libs/aitools/mcp/agentconfig/testdata/codex/after.toml new file mode 100644 index 00000000000..6078f359bb5 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/codex/after.toml @@ -0,0 +1,13 @@ +model = "gpt-5" + +[mcp_servers] + [mcp_servers.genie_one_mcp-dogfood] + args = ["aitools", "mcp", "serve", "--profile", "dogfood", "--service", "system.ai.genie_one_mcp", "--agent", "codex"] + command = "/opt/databricks/bin/databricks" + [mcp_servers.other] + args = ["-y", "other-mcp"] + command = "npx" + +[projects] + [projects."/work"] + trust_level = "trusted" diff --git a/libs/aitools/mcp/agentconfig/testdata/codex/before.toml b/libs/aitools/mcp/agentconfig/testdata/codex/before.toml new file mode 100644 index 00000000000..e9be6e9eb28 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/codex/before.toml @@ -0,0 +1,9 @@ +# Codex settings. This comment is dropped on rewrite. +model = "gpt-5" + +[projects."/work"] +trust_level = "trusted" + +[mcp_servers.other] +command = "npx" +args = ["-y", "other-mcp"] diff --git a/libs/aitools/mcp/agentconfig/testdata/copilot/after.json b/libs/aitools/mcp/agentconfig/testdata/copilot/after.json new file mode 100644 index 00000000000..633a33098ab --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/copilot/after.json @@ -0,0 +1,34 @@ +{ + "banner": "never", + "mcpServers": { + "other": { + "type": "local", + "command": "npx", + "args": [ + "-y", + "other-mcp" + ], + "tools": [ + "*" + ] + }, + "genie_one_mcp-dogfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "dogfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "copilot" + ], + "command": "/opt/databricks/bin/databricks", + "tools": [ + "*" + ], + "type": "local" + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/copilot/before.json b/libs/aitools/mcp/agentconfig/testdata/copilot/before.json new file mode 100644 index 00000000000..ade58905e44 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/copilot/before.json @@ -0,0 +1,11 @@ +{ + "banner": "never", + "mcpServers": { + "other": { + "type": "local", + "command": "npx", + "args": ["-y", "other-mcp"], + "tools": ["*"] + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/cursor/after.json b/libs/aitools/mcp/agentconfig/testdata/cursor/after.json new file mode 100644 index 00000000000..278e918d5da --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/cursor/after.json @@ -0,0 +1,26 @@ +{ + "version": 1, + "mcpServers": { + "other": { + "command": "npx", + "args": [ + "-y", + "other-mcp" + ] + }, + "genie_one_mcp-dogfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "dogfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "cursor" + ], + "command": "/opt/databricks/bin/databricks" + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/cursor/before.json b/libs/aitools/mcp/agentconfig/testdata/cursor/before.json new file mode 100644 index 00000000000..d479087758f --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/cursor/before.json @@ -0,0 +1,9 @@ +{ + "version": 1, + "mcpServers": { + "other": { + "command": "npx", + "args": ["-y", "other-mcp"] + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/gemini/after.json b/libs/aitools/mcp/agentconfig/testdata/gemini/after.json new file mode 100644 index 00000000000..68855f01261 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/gemini/after.json @@ -0,0 +1,28 @@ +{ + "general": { + "previewFeatures": true + }, + "mcpServers": { + "other": { + "command": "npx", + "args": [ + "-y", + "other-mcp" + ] + }, + "genie_one_mcp-dogfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "dogfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "gemini" + ], + "command": "/opt/databricks/bin/databricks" + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/gemini/before.json b/libs/aitools/mcp/agentconfig/testdata/gemini/before.json new file mode 100644 index 00000000000..0d9fc9339ef --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/gemini/before.json @@ -0,0 +1,11 @@ +{ + "general": { + "previewFeatures": true + }, + "mcpServers": { + "other": { + "command": "npx", + "args": ["-y", "other-mcp"] + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/opencode/after.json b/libs/aitools/mcp/agentconfig/testdata/opencode/after.json new file mode 100644 index 00000000000..f9c5da49cdc --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/opencode/after.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://opencode.ai/config.json", + "theme": "opencode", + "mcp": { + "servers": { + "other": { + "type": "local", + "command": [ + "npx", + "-y", + "other-mcp" + ] + }, + "genie_one_mcp-dogfood": { + "command": [ + "/opt/databricks/bin/databricks", + "aitools", + "mcp", + "serve", + "--profile", + "dogfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "opencode" + ], + "type": "local" + } + } + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/opencode/before.json b/libs/aitools/mcp/agentconfig/testdata/opencode/before.json new file mode 100644 index 00000000000..2645b38a23d --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/opencode/before.json @@ -0,0 +1,12 @@ +{ + "$schema": "https://opencode.ai/config.json", + "theme": "opencode", + "mcp": { + "servers": { + "other": { + "type": "local", + "command": ["npx", "-y", "other-mcp"] + } + } + } +} diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go new file mode 100644 index 00000000000..1918e62f957 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -0,0 +1,103 @@ +package agentconfig + +import ( + "bytes" + "fmt" + + "github.com/BurntSushi/toml" +) + +// tomlDoc is an agent config file in TOML, decoded into plain maps. Encoding it +// again drops comments and sorts keys; Upsert and Remove write only when an +// entry changes, so an untouched file keeps its comments. +type tomlDoc struct { + root map[string]any +} + +func parseTOML(data []byte) (document, error) { + root := map[string]any{} + if _, err := toml.Decode(string(data), &root); err != nil { + return nil, err + } + return &tomlDoc{root: root}, nil +} + +// table returns the table at root. A missing table reads as empty, and is +// created when create is set. +func (d *tomlDoc) table(root []string, create bool) (map[string]any, error) { + t := d.root + for _, key := range root { + next, ok := t[key] + if !ok { + if !create { + return map[string]any{}, nil + } + m := map[string]any{} + t[key] = m + t = m + continue + } + m, ok := next.(map[string]any) + if !ok { + return nil, fmt.Errorf("%q is not a TOML table: %w", key, ErrConfigInvalid) + } + t = m + } + return t, nil +} + +func (d *tomlDoc) entry(root []string, name string) (map[string]any, bool, error) { + table, err := d.table(root, false) + if err != nil { + return nil, false, err + } + raw, ok := table[name] + if !ok { + return nil, false, nil + } + entry, ok := raw.(map[string]any) + if !ok { + return nil, false, fmt.Errorf("server %q is not a TOML table: %w", name, ErrConfigInvalid) + } + return entry, true, nil +} + +func (d *tomlDoc) entries(root []string) (map[string]map[string]any, error) { + table, err := d.table(root, false) + if err != nil { + return nil, err + } + out := map[string]map[string]any{} + for key, value := range table { + if entry, ok := value.(map[string]any); ok { + out[key] = entry + } + } + return out, nil +} + +func (d *tomlDoc) set(root []string, name string, entry map[string]any) error { + table, err := d.table(root, true) + if err != nil { + return err + } + table[name] = entry + return nil +} + +func (d *tomlDoc) remove(root []string, name string) error { + table, err := d.table(root, false) + if err != nil { + return err + } + delete(table, name) + return nil +} + +func (d *tomlDoc) bytes() ([]byte, error) { + var buf bytes.Buffer + if err := toml.NewEncoder(&buf).Encode(d.root); err != nil { + return nil, err + } + return buf.Bytes(), nil +} diff --git a/libs/aitools/mcp/agentconfig/unmanaged.go b/libs/aitools/mcp/agentconfig/unmanaged.go new file mode 100644 index 00000000000..0ad91b7e134 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/unmanaged.go @@ -0,0 +1,20 @@ +package agentconfig + +import ( + "strings" + + "github.com/databricks/cli/libs/aitools/mcpconfig" +) + +// IsUnmanaged reports whether entry connects to Databricks managed MCP servers +// without having been written by this CLI: the `ug mcp-proxy` bridge, or a +// direct /ai-gateway/mcp-services/ URL. Such entries are listed and never +// modified. +func IsUnmanaged(entry map[string]any) bool { + if argv, ok := mcpconfig.Argv(entry); ok && len(argv) > 1 && + mcpconfig.IsBinary(argv[0], "ug", "ug.exe") && argv[1] == "mcp-proxy" { + return true + } + url, _ := entry["url"].(string) + return strings.Contains(url, "/ai-gateway/mcp-services/") +} diff --git a/libs/aitools/mcp/agentconfig/unmanaged_test.go b/libs/aitools/mcp/agentconfig/unmanaged_test.go new file mode 100644 index 00000000000..76d9621fd2d --- /dev/null +++ b/libs/aitools/mcp/agentconfig/unmanaged_test.go @@ -0,0 +1,50 @@ +package agentconfig + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIsUnmanaged(t *testing.T) { + for _, tc := range []struct { + name string + entry map[string]any + want bool + }{ + { + name: "ug mcp-proxy bridge", + entry: map[string]any{"command": "/usr/local/bin/ug", "args": []any{"mcp-proxy", "--url", "x"}}, + want: true, + }, + { + name: "ug.exe bridge in command array", + entry: map[string]any{"command": []any{`C:\tools\ug.exe`, "mcp-proxy"}}, + want: true, + }, + { + name: "ug without mcp-proxy", + entry: map[string]any{"command": "/usr/local/bin/ug", "args": []any{"claude"}}, + want: false, + }, + { + name: "direct mcp services url", + entry: map[string]any{"type": "http", "url": "https://example.test/ai-gateway/mcp-services/system.ai.dbsql"}, + want: true, + }, + { + name: "unrelated url", + entry: map[string]any{"type": "http", "url": "https://example.test/mcp"}, + want: false, + }, + { + name: "our own entry is not unmanaged", + entry: map[string]any{"command": "/usr/bin/databricks", "args": []any{"aitools", "mcp", "serve"}}, + want: false, + }, + } { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, IsUnmanaged(tc.entry)) + }) + } +} diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go new file mode 100644 index 00000000000..bb417c293da --- /dev/null +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -0,0 +1,238 @@ +// Package agentconfig reads and writes the user-scope MCP server entries of +// coding agents. Each write reads the whole file, changes one entry and writes +// the file back atomically; everything else in the file is kept. +package agentconfig + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "reflect" + "strings" + + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/atomicfile" +) + +// Status is the outcome of Upsert. +type Status string + +const ( + StatusAdded Status = "added" + StatusUpdated Status = "updated" + StatusUnchanged Status = "unchanged" +) + +var ( + // ErrNotOwned means an entry with the name exists and was not written by + // this CLI. Upsert needs force to replace it, and Remove never deletes it. + ErrNotOwned = errors.New("entry was not written by databricks") + // ErrConfigInvalid means an agent config file exists but cannot be parsed. + // Such a file is never written to. + ErrConfigInvalid = errors.New("agent config file cannot be parsed") +) + +// document is an agent config file in memory. +type document interface { + // entry returns the server entry name under root, and whether it exists. + entry(root []string, name string) (map[string]any, bool, error) + // entries returns the object-valued members under root, keyed by name. + entries(root []string) (map[string]map[string]any, error) + // set stores entry as name under root, creating missing tables. + set(root []string, name string, entry map[string]any) error + // remove deletes name under root. It is a no-op when name is absent. + remove(root []string, name string) error + // bytes serializes the document. + bytes() ([]byte, error) +} + +// Upsert writes a stdio server name for agent a, which runs cmd with args and +// env. It replaces an entry this CLI wrote, or any entry when force is set, and +// leaves the file alone when the entry already matches. It returns the config +// file path. +func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []string, env map[string]string, force bool) (string, Status, error) { + spec, err := specOf(a) + if err != nil { + return "", "", err + } + path, err := spec.Path(ctx) + if err != nil { + return "", "", err + } + doc, err := load(spec, path) + if err != nil { + return path, "", err + } + root := rootPath(spec) + want := spec.Entry(cmd, args, env) + current, found, err := doc.entry(root, name) + if err != nil { + return path, "", fmt.Errorf("%s: %w", path, err) + } + if found { + if !spec.IsOurs(current) && !force { + return path, "", fmt.Errorf("%s: server %q: %w", path, name, ErrNotOwned) + } + same, err := sameEntry(current, want) + if err != nil { + return path, "", err + } + if same { + return path, StatusUnchanged, nil + } + } + if err := doc.set(root, name, want); err != nil { + return path, "", fmt.Errorf("%s: %w", path, err) + } + if err := save(path, doc); err != nil { + return path, "", err + } + if found { + return path, StatusUpdated, nil + } + return path, StatusAdded, nil +} + +// Remove deletes server name from agent a's config. It reports false, without +// writing, when there is no such entry, and refuses an entry this CLI did not +// write. +func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { + spec, err := specOf(a) + if err != nil { + return false, err + } + path, err := spec.Path(ctx) + if err != nil { + return false, err + } + doc, err := load(spec, path) + if err != nil { + return false, err + } + root := rootPath(spec) + current, found, err := doc.entry(root, name) + if err != nil { + return false, fmt.Errorf("%s: %w", path, err) + } + if !found { + return false, nil + } + if !spec.IsOurs(current) { + return false, fmt.Errorf("%s: server %q: %w", path, name, ErrNotOwned) + } + if err := doc.remove(root, name); err != nil { + return false, fmt.Errorf("%s: %w", path, err) + } + if err := save(path, doc); err != nil { + return false, err + } + return true, nil +} + +// Read returns every server entry in agent a's config, keyed by name. A missing +// file has no entries. +func Read(ctx context.Context, a *agents.Agent) (map[string]map[string]any, error) { + spec, err := specOf(a) + if err != nil { + return nil, err + } + path, err := spec.Path(ctx) + if err != nil { + return nil, err + } + doc, err := load(spec, path) + if err != nil { + return nil, err + } + entries, err := doc.entries(rootPath(spec)) + if err != nil { + return nil, fmt.Errorf("%s: %w", path, err) + } + return entries, nil +} + +func specOf(a *agents.Agent) (*mcpconfig.Spec, error) { + if a.MCP == nil { + return nil, fmt.Errorf("agent %s does not take MCP servers", a.DisplayName) + } + return a.MCP, nil +} + +func rootPath(spec *mcpconfig.Spec) []string { + return strings.Split(spec.Root, ".") +} + +// load reads and parses the config at path. A missing file is an empty document. +func load(spec *mcpconfig.Spec, path string) (document, error) { + data, err := os.ReadFile(path) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, fmt.Errorf("read %s: %w", path, err) + } + var doc document + switch spec.Format { + case mcpconfig.FormatJSON: + doc, err = parseJSON(data) + case mcpconfig.FormatTOML: + doc, err = parseTOML(data) + default: + return nil, fmt.Errorf("unknown config format %d", spec.Format) + } + if err != nil { + return nil, fmt.Errorf("%s: %w: %w", path, ErrConfigInvalid, err) + } + return doc, nil +} + +// save writes doc to path atomically. The file keeps its mode; a new file gets +// 0600 because agent configs can hold auth state. +func save(path string, doc document) error { + data, err := doc.bytes() + if err != nil { + return fmt.Errorf("serialize %s: %w", path, err) + } + perm := os.FileMode(0o600) + if info, err := os.Stat(path); err == nil { + perm = info.Mode().Perm() + } + // Rename would replace a symlink (dotfile managers link configs) with a + // regular file, so write to the link's target. + target := path + if resolved, err := filepath.EvalSymlinks(path); err == nil { + target = resolved + } + if err := atomicfile.Write(target, data, perm, atomicfile.MkDir(0o755)); err != nil { + return fmt.Errorf("write %s: %w", path, err) + } + return nil +} + +// sameEntry compares two entries as JSON values, so the in-memory Go types of a +// freshly built entry and one decoded from a file compare equal. +func sameEntry(a, b map[string]any) (bool, error) { + na, err := normalize(a) + if err != nil { + return false, err + } + nb, err := normalize(b) + if err != nil { + return false, err + } + return reflect.DeepEqual(na, nb), nil +} + +func normalize(entry map[string]any) (map[string]any, error) { + raw, err := json.Marshal(entry) + if err != nil { + return nil, err + } + var out map[string]any + if err := json.Unmarshal(raw, &out); err != nil { + return nil, err + } + return out, nil +} diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go new file mode 100644 index 00000000000..4dcdc799cd1 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -0,0 +1,323 @@ +package agentconfig + +import ( + "bytes" + "context" + "encoding/json" + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/BurntSushi/toml" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + testName = "genie_one_mcp-dogfood" + testCmd = "/opt/databricks/bin/databricks" +) + +func testArgs(agent string) []string { + return []string{"aitools", "mcp", "serve", "--profile", "dogfood", "--service", "system.ai.genie_one_mcp", "--agent", agent} +} + +// testHome returns a context whose home is a fresh temp dir, with the override +// variables cleared so the test does not depend on the developer's shell. +func testHome(t *testing.T) context.Context { + clearAgentOverrides(t) + return env.WithUserHomeDir(t.Context(), t.TempDir()) +} + +// clearAgentOverrides empties the variables the agent resolvers read. An empty +// value is treated as unset, and t.Setenv restores the originals afterwards. +func clearAgentOverrides(t *testing.T) { + for _, key := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA"} { + t.Setenv(key, "") + } +} + +func testEntry(cmd string, args []string, vars map[string]string) map[string]any { + entry := map[string]any{"command": cmd, "args": args} + if len(vars) > 0 { + entry["env"] = vars + } + return entry +} + +// testAgent is an agent whose config lives at path, for tests that need a +// format or a command that no real agent uses. +func testAgent(path string, format mcpconfig.Format, root string) *agents.Agent { + return &agents.Agent{ + Name: "test", + DisplayName: "Test", + MCP: &mcpconfig.Spec{ + Path: func(context.Context) (string, error) { return path, nil }, + Format: format, + Root: root, + Entry: testEntry, + IsOurs: mcpconfig.IsDatabricksEntry, + }, + } +} + +func readFile(t *testing.T, path string) []byte { + t.Helper() + data, err := os.ReadFile(path) + require.NoError(t, err) + return data +} + +func writeFile(t *testing.T, path string, data []byte, perm os.FileMode) { + t.Helper() + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, data, perm)) +} + +// semantic decodes data into plain Go values, so two files compare equal when +// they hold the same data regardless of formatting, key order or comments. +func semantic(t *testing.T, format mcpconfig.Format, data []byte) map[string]any { + t.Helper() + out := map[string]any{} + if format == mcpconfig.FormatTOML { + _, err := toml.Decode(string(data), &out) + require.NoError(t, err) + return out + } + require.NoError(t, json.Unmarshal(data, &out)) + return out +} + +func TestRoundTripPerAgent(t *testing.T) { + for _, a := range agents.MCPAgents() { + t.Run(a.Name, func(t *testing.T) { + ctx := testHome(t) + path, err := a.MCP.Path(ctx) + require.NoError(t, err) + ext := "json" + if a.MCP.Format == mcpconfig.FormatTOML { + ext = "toml" + } + before := readFile(t, filepath.Join("testdata", a.Name, "before."+ext)) + writeFile(t, path, before, 0o600) + + gotPath, status, err := Upsert(ctx, a, testName, testCmd, testArgs(a.Name), nil, false) + require.NoError(t, err) + assert.Equal(t, path, gotPath) + assert.Equal(t, StatusAdded, status) + after := readFile(t, path) + assert.Equal(t, string(readFile(t, filepath.Join("testdata", a.Name, "after."+ext))), string(after)) + + // The same entry again must not touch the file. + _, status, err = Upsert(ctx, a, testName, testCmd, testArgs(a.Name), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) + assert.Equal(t, after, readFile(t, path)) + + entries, err := Read(ctx, a) + require.NoError(t, err) + require.Contains(t, entries, testName) + assert.True(t, a.MCP.IsOurs(entries[testName])) + assert.Contains(t, entries, "other") + + removed, err := Remove(ctx, a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, semantic(t, a.MCP.Format, before), semantic(t, a.MCP.Format, readFile(t, path))) + }) + } +} + +func TestUpsertRefusesUnparsableFile(t *testing.T) { + for _, tc := range []struct { + name string + format mcpconfig.Format + data string + }{ + {"json syntax", mcpconfig.FormatJSON, "{not json"}, + {"json top level array", mcpconfig.FormatJSON, `[]`}, + {"json root not object", mcpconfig.FormatJSON, `{"mcpServers": 5}`}, + {"json entry not object", mcpconfig.FormatJSON, `{"mcpServers": {"` + testName + `": 5}}`}, + {"json trailing data", mcpconfig.FormatJSON, `{} {}`}, + {"toml syntax", mcpconfig.FormatTOML, "a = ["}, + {"toml root not table", mcpconfig.FormatTOML, "mcp_servers = 5"}, + } { + t.Run(tc.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config") + writeFile(t, path, []byte(tc.data), 0o600) + a := testAgent(path, tc.format, "mcpServers") + if tc.format == mcpconfig.FormatTOML { + a = testAgent(path, tc.format, "mcp_servers") + } + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) + require.ErrorIs(t, err, ErrConfigInvalid) + assert.ErrorContains(t, err, path) + _, err = Remove(t.Context(), a, testName) + require.ErrorIs(t, err, ErrConfigInvalid) + + assert.Equal(t, tc.data, string(readFile(t, path))) + }) + } +} + +func TestUpsertRefusesEntryWeDidNotWrite(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + other := `{"mcpServers":{"` + testName + `":{"command":"/usr/bin/other","args":[]}}}` + writeFile(t, path, []byte(other), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.ErrorIs(t, err, ErrNotOwned) + assert.Equal(t, other, string(readFile(t, path))) + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.True(t, a.MCP.IsOurs(entries[testName])) +} + +func TestRemoveRefusesEntryWeDidNotWrite(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + other := `{"mcpServers":{"` + testName + `":{"command":"/usr/bin/other","args":[]}}}` + writeFile(t, path, []byte(other), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, err := Remove(t.Context(), a, testName) + require.ErrorIs(t, err, ErrNotOwned) + assert.Equal(t, other, string(readFile(t, path))) +} + +func TestRemoveAbsentEntryDoesNotCreateFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "missing.json") + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.False(t, removed) + _, err = os.Stat(path) + assert.ErrorIs(t, err, os.ErrNotExist) +} + +func TestUpsertUnchangedDoesNotRewriteFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + // Compact formatting that a rewrite would change, holding the exact entry. + entry, err := json.Marshal(testEntry(testCmd, testArgs("test"), nil)) + require.NoError(t, err) + data := []byte(`{"mcpServers":{"` + testName + `":` + string(entry) + `}}`) + writeFile(t, path, data, 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) + assert.Equal(t, data, readFile(t, path)) +} + +func TestUpsertKeepsFileModeAndCreatesPrivateFile(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Windows does not honor unix file modes") + } + dir := t.TempDir() + + existing := filepath.Join(dir, "existing.json") + writeFile(t, existing, []byte(`{}`), 0o640) + a := testAgent(existing, mcpconfig.FormatJSON, "mcpServers") + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + info, err := os.Stat(existing) + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o640), info.Mode().Perm()) + + // A missing file and its missing parent directory are created; the file gets 0600. + created := filepath.Join(dir, "new", "config.json") + a = testAgent(created, mcpconfig.FormatJSON, "mcpServers") + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + info, err = os.Stat(created) + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o600), info.Mode().Perm()) +} + +func TestUpsertWritesThroughSymlink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("creating symlinks needs privileges on Windows") + } + dir := t.TempDir() + target := filepath.Join(dir, "dotfiles", "config.json") + writeFile(t, target, []byte(`{}`), 0o600) + link := filepath.Join(dir, "config.json") + require.NoError(t, os.Symlink(target, link)) + a := testAgent(link, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + info, err := os.Lstat(link) + require.NoError(t, err) + assert.NotZero(t, info.Mode()&os.ModeSymlink, "link is replaced by a regular file") + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Contains(t, entries, testName) +} + +func TestWindowsCommandPathRoundTrip(t *testing.T) { + winCmd := `C:\Program Files\databricks\databricks.exe` + for _, tc := range []struct { + name string + format mcpconfig.Format + root string + want string + }{ + {"json", mcpconfig.FormatJSON, "mcpServers", `"command": "C:\\Program Files\\databricks\\databricks.exe"`}, + {"toml", mcpconfig.FormatTOML, "mcp_servers", `command = "C:\\Program Files\\databricks\\databricks.exe"`}, + } { + t.Run(tc.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config") + a := testAgent(path, tc.format, tc.root) + _, _, err := Upsert(t.Context(), a, testName, winCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Contains(t, string(readFile(t, path)), tc.want) + + entries, err := Read(t.Context(), a) + require.NoError(t, err) + require.Contains(t, entries, testName) + assert.Equal(t, winCmd, entries[testName]["command"]) + assert.True(t, mcpconfig.IsDatabricksEntry(entries[testName])) + }) + } +} + +func TestJSONKeepsOtherValuesByteForByte(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + writeFile(t, path, []byte(`{"big": 12345678901234567890, "ratio": 1.50, "text": "caf\u00e9", "mcpServers": {}}`), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + got := readFile(t, path) + assert.Contains(t, string(got), `"big": 12345678901234567890`) + assert.Contains(t, string(got), `"ratio": 1.50`) + assert.Contains(t, string(got), `"text": "caf\u00e9"`) + assert.True(t, bytes.HasSuffix(got, []byte("}\n")), "file ends with a newline") +} + +// TOML comments are lost on rewrite. This test pins that behavior so a change to +// it is deliberate. +func TestTOMLRewriteDropsComments(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte("# keep me?\nmodel = \"gpt-5\"\n"), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + got := string(readFile(t, path)) + assert.NotContains(t, got, "# keep me?") + assert.Contains(t, got, `model = "gpt-5"`) +} diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go new file mode 100644 index 00000000000..7b07e488fbb --- /dev/null +++ b/libs/aitools/mcpconfig/spec.go @@ -0,0 +1,90 @@ +// Package mcpconfig describes where a coding agent keeps its user-scope MCP +// server entries. It is separate from libs/aitools/agents so that the agent +// registry can carry the specs without importing the MCP packages. +package mcpconfig + +import ( + "context" + "slices" + "strings" +) + +// Format is the on-disk format of an agent's MCP config file. +type Format int + +const ( + FormatJSON Format = iota + FormatTOML +) + +// Spec describes an agent's user-scope MCP config file. +type Spec struct { + // Path returns the absolute path of the config file. + Path func(ctx context.Context) (string, error) + // Format is the format of the config file. + Format Format + // Root is the dot-separated key path of the table that holds server entries, + // such as "mcpServers" or "mcp.servers". + Root string + // Entry builds the entry for a stdio server that runs cmd with args and env. + // env is empty when there are no variables to pin. + Entry func(cmd string, args []string, env map[string]string) map[string]any + // IsOurs reports whether an entry was written by this CLI. It receives entries + // as they were decoded from the file. + IsOurs func(entry map[string]any) bool +} + +// Argv returns the command line of a stdio entry as argv[0] and its arguments. +// It accepts the `command` string plus `args` shape used by most agents and the +// single `command` array that OpenCode uses. Entries decoded from JSON or TOML +// carry arguments as []any, so that is the only list type it accepts. +func Argv(entry map[string]any) ([]string, bool) { + switch command := entry["command"].(type) { + case string: + args, _ := entry["args"].([]any) + argv := []string{command} + for _, arg := range args { + s, ok := arg.(string) + if !ok { + return nil, false + } + argv = append(argv, s) + } + return argv, true + case []any: + argv := make([]string, 0, len(command)) + for _, part := range command { + s, ok := part.(string) + if !ok { + return nil, false + } + argv = append(argv, s) + } + if len(argv) == 0 { + return nil, false + } + return argv, true + default: + return nil, false + } +} + +// IsDatabricksEntry reports whether entry runs `databricks aitools mcp serve`, +// which is how we recognize entries we wrote. It is the IsOurs of every agent. +func IsDatabricksEntry(entry map[string]any) bool { + argv, ok := Argv(entry) + if !ok { + return false + } + args := argv[1:] + return IsBinary(argv[0], "databricks", "databricks.exe") && + len(args) >= 3 && args[0] == "aitools" && args[1] == "mcp" && args[2] == "serve" +} + +// IsBinary reports whether cmd, a bare name or a path, has one of the given +// base names. Both separators are accepted so that a Windows path is recognized +// on any host. +func IsBinary(cmd string, names ...string) bool { + base := cmd[strings.LastIndexAny(cmd, `/\`)+1:] + return slices.Contains(names, base) +} diff --git a/libs/aitools/mcpconfig/spec_test.go b/libs/aitools/mcpconfig/spec_test.go new file mode 100644 index 00000000000..f0d32d7729f --- /dev/null +++ b/libs/aitools/mcpconfig/spec_test.go @@ -0,0 +1,67 @@ +package mcpconfig + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestArgv(t *testing.T) { + for _, tc := range []struct { + name string + entry map[string]any + want []string + ok bool + }{ + { + name: "command and args", + entry: map[string]any{"command": "/bin/x", "args": []any{"a", "b"}}, + want: []string{"/bin/x", "a", "b"}, + ok: true, + }, + { + name: "command array", + entry: map[string]any{"command": []any{"/bin/x", "a"}}, + want: []string{"/bin/x", "a"}, + ok: true, + }, + { + name: "empty command array", + entry: map[string]any{"command": []any{}}, + ok: false, + }, + { + name: "url entry has no command", + entry: map[string]any{"url": "https://example.test/ai-gateway/mcp-services/x"}, + ok: false, + }, + } { + t.Run(tc.name, func(t *testing.T) { + argv, ok := Argv(tc.entry) + assert.Equal(t, tc.ok, ok) + assert.Equal(t, tc.want, argv) + }) + } +} + +func TestIsDatabricksEntry(t *testing.T) { + serve := []any{"aitools", "mcp", "serve", "--profile", "p"} + for _, tc := range []struct { + name string + entry map[string]any + want bool + }{ + {"unix path", map[string]any{"command": "/opt/homebrew/bin/databricks", "args": serve}, true}, + {"bare name", map[string]any{"command": "databricks", "args": serve}, true}, + {"windows path", map[string]any{"command": `C:\Program Files\databricks\databricks.exe`, "args": serve}, true}, + {"opencode array", map[string]any{"command": []any{"/usr/bin/databricks", "aitools", "mcp", "serve"}}, true}, + {"other binary", map[string]any{"command": "/usr/bin/other", "args": serve}, false}, + {"other subcommand", map[string]any{"command": "databricks", "args": []any{"aitools", "mcp", "list"}}, false}, + {"suffix is not a basename", map[string]any{"command": "/x/mydatabricks", "args": serve}, false}, + {"no command", map[string]any{"url": "https://example.test"}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, IsDatabricksEntry(tc.entry)) + }) + } +} From f367c1fe96292be65a4099a8d18f0b3d082681fc Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 12:51:04 +0000 Subject: [PATCH 02/21] Edit agent MCP configs in place JSON and JSONC entries are changed with hujson patches, so comments, trailing commas, a leading BOM and the bytes of everything else are kept. TOML entries are changed as text: only the lines of the server's own tables are replaced or removed, and the edited text is decoded and checked before it is written. Upsert refuses a command that is not named databricks, since such an entry could not be recognized later. A symlink whose target is missing is refused rather than replaced. OpenCode prefers an existing opencode.jsonc. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 46 +-- libs/aitools/agents/agents_test.go | 17 + libs/aitools/mcp/agentconfig/json.go | 341 ++++++++++-------- libs/aitools/mcp/agentconfig/json_test.go | 109 ++++++ .../testdata/antigravity/after.json | 5 +- .../mcp/agentconfig/testdata/codex/after.toml | 20 +- .../agentconfig/testdata/codex/before.toml | 2 +- .../agentconfig/testdata/copilot/after.json | 9 +- .../agentconfig/testdata/cursor/after.json | 5 +- .../agentconfig/testdata/gemini/after.json | 5 +- .../agentconfig/testdata/opencode/after.json | 6 +- libs/aitools/mcp/agentconfig/toml.go | 313 ++++++++++++++-- libs/aitools/mcp/agentconfig/toml_test.go | 135 +++++++ libs/aitools/mcp/agentconfig/unmanaged.go | 10 +- .../aitools/mcp/agentconfig/unmanaged_test.go | 10 + libs/aitools/mcp/agentconfig/writer.go | 57 ++- libs/aitools/mcp/agentconfig/writer_test.go | 131 +++++-- libs/aitools/mcpconfig/spec.go | 13 +- libs/aitools/mcpconfig/spec_test.go | 1 + 19 files changed, 958 insertions(+), 277 deletions(-) create mode 100644 libs/aitools/mcp/agentconfig/json_test.go create mode 100644 libs/aitools/mcp/agentconfig/toml_test.go diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 04b5f3c60bc..d4d6b145d59 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -189,9 +189,8 @@ var Registry = []*Agent{ MCP: &mcpconfig.Spec{ Path: claudeMCPPath, Format: mcpconfig.FormatJSON, - Root: "mcpServers", + Root: mcpServersKey, Entry: claudeMCPEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, }, { @@ -207,9 +206,8 @@ var Registry = []*Agent{ MCP: &mcpconfig.Spec{ Path: homeSubdir(".cursor", "mcp.json"), Format: mcpconfig.FormatJSON, - Root: "mcpServers", + Root: mcpServersKey, Entry: stdioEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, }, { @@ -223,7 +221,6 @@ var Registry = []*Agent{ Format: mcpconfig.FormatTOML, Root: "mcp_servers", Entry: stdioEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, }, { @@ -239,7 +236,6 @@ var Registry = []*Agent{ Format: mcpconfig.FormatJSON, Root: "mcp.servers", Entry: openCodeMCPEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, }, { @@ -251,9 +247,8 @@ var Registry = []*Agent{ MCP: &mcpconfig.Spec{ Path: copilotMCPPath, Format: mcpconfig.FormatJSON, - Root: "mcpServers", + Root: mcpServersKey, Entry: copilotMCPEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, }, { @@ -265,9 +260,8 @@ var Registry = []*Agent{ MCP: &mcpconfig.Spec{ Path: antigravityMCPPath, Format: mcpconfig.FormatJSON, - Root: "mcpServers", + Root: mcpServersKey, Entry: antigravityMCPEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, }, { @@ -295,9 +289,8 @@ var Registry = []*Agent{ MCP: &mcpconfig.Spec{ Path: geminiMCPPath, Format: mcpconfig.FormatJSON, - Root: "mcpServers", + Root: mcpServersKey, Entry: stdioEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, }, { @@ -432,11 +425,7 @@ func gooseConfigDir(ctx context.Context) (string, error) { // CLAUDE_CONFIG_DIR moves it to /.claude.json. // https://code.claude.com/docs/en/env-vars func claudeMCPPath(ctx context.Context) (string, error) { - if env.Get(ctx, "CLAUDE_CONFIG_DIR") != "" { - dir, err := claudeConfigDir(ctx) - if err != nil { - return "", err - } + if dir := env.Get(ctx, "CLAUDE_CONFIG_DIR"); dir != "" { return filepath.Join(dir, ".claude.json"), nil } return homeSubdir(".claude.json")(ctx) @@ -451,6 +440,7 @@ func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[strin } // codexMCPPath returns Codex's config.toml, which honors CODEX_HOME. +// https://learn.chatgpt.com/docs/config-file/config-advanced func codexMCPPath(ctx context.Context) (string, error) { if home := env.Get(ctx, "CODEX_HOME"); home != "" { return filepath.Join(home, "config.toml"), nil @@ -458,14 +448,20 @@ func codexMCPPath(ctx context.Context) (string, error) { return homeSubdir(".codex", "config.toml")(ctx) } -// openCodeMCPPath returns OpenCode's config file. The file may contain comments -// (JSONC); a file we cannot parse is refused, never rewritten. +// openCodeMCPPath returns OpenCode's config file. OpenCode reads opencode.json or +// opencode.jsonc, and the docs do not say which wins when both exist, so an +// existing opencode.jsonc is used rather than creating a second file beside it. +// The file may contain comments; a file we cannot parse is refused, never rewritten. // https://opencode.ai/docs/config/ func openCodeMCPPath(ctx context.Context) (string, error) { dir, err := openCodeConfigDir(ctx) if err != nil { return "", err } + jsonc := filepath.Join(dir, "opencode.jsonc") + if _, err := os.Stat(jsonc); err == nil { + return jsonc, nil + } return filepath.Join(dir, "opencode.json"), nil } @@ -474,7 +470,7 @@ func openCodeMCPPath(ctx context.Context) (string, error) { // (the schema forbids extra keys, and an absent key means enabled). // https://opencode.ai/docs/mcp-servers/ func openCodeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { - entry := map[string]any{"type": "local", "command": append([]string{cmd}, args...)} + entry := map[string]any{"type": localServerType, "command": append([]string{cmd}, args...)} if len(vars) > 0 { entry["environment"] = vars } @@ -494,7 +490,7 @@ func copilotMCPPath(ctx context.Context) (string, error) { // https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/add-mcp-servers func copilotMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { entry := stdioEntry(cmd, args, vars) - entry["type"] = "local" + entry["type"] = localServerType entry["tools"] = []string{"*"} return entry } @@ -535,6 +531,14 @@ func stdioEntry(cmd string, args []string, vars map[string]string) map[string]an return entry } +// mcpServersKey is the key of the server table in the JSON MCP config files +// that use it (every agent but Codex and OpenCode). +const mcpServersKey = "mcpServers" + +// localServerType is the "type" of a local stdio server in the agents that +// write one (Claude Code, Copilot, OpenCode). +const localServerType = "local" + // SupportsMCP reports whether this CLI writes the agent's MCP server entries. func (a *Agent) SupportsMCP() bool { return a.MCP != nil diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index 8d7b8c0eb72..bf8435fe9a8 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -2,6 +2,7 @@ package agents import ( "context" + "os" "path/filepath" "testing" @@ -101,6 +102,22 @@ func TestMCPPaths(t *testing.T) { } } +// OpenCode reads opencode.json or opencode.jsonc. An existing .jsonc is the +// file to edit, so a second file is not created beside it. +func TestOpenCodeMCPPathPrefersJSONC(t *testing.T) { + ctx, home := clearMCPOverrides(t) + dir := filepath.Join(home, ".config", "opencode") + path, err := ByName(NameOpenCode).MCP.Path(ctx) + require.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "opencode.json"), path) + + require.NoError(t, os.MkdirAll(dir, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "opencode.jsonc"), []byte("{}"), 0o600)) + path, err = ByName(NameOpenCode).MCP.Path(ctx) + require.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "opencode.jsonc"), path) +} + func TestMCPEntries(t *testing.T) { args := []string{"aitools", "mcp", "serve"} pinned := map[string]string{"DATABRICKS_CONFIG_FILE": "/home/u/.databrickscfg"} diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index d87149905a4..ad3694f24d2 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -3,218 +3,271 @@ package agentconfig import ( "bytes" "encoding/json" - "errors" "fmt" - "io" - "slices" + "strings" + + "github.com/tailscale/hujson" ) -// jsonObject is a JSON object that keeps its keys in file order and each value -// as the original bytes, so a rewrite changes only the entry it was asked to. -type jsonObject struct { - keys []string - values map[string]json.RawMessage +// jsonDoc is an agent config file in JSON with comments (JSONC) and trailing +// commas also accepted. Edits are RFC 6902 patches applied to the parsed tree, +// so every byte outside the changed member is kept. +type jsonDoc struct { + bom bool + // fresh is set when the file had no members, so its layout is ours to + // choose: the output is re-indented with two spaces. + fresh bool + root hujson.Value } -// parseJSONObject decodes a top-level JSON object. Empty input is an empty object. -func parseJSONObject(data []byte) (*jsonObject, error) { - obj := &jsonObject{values: map[string]json.RawMessage{}} +func parseJSON(data []byte) (document, error) { + data, bom := stripBOM(data) if len(bytes.TrimSpace(data)) == 0 { - return obj, nil + data = []byte("{}") } - dec := json.NewDecoder(bytes.NewReader(data)) - tok, err := dec.Token() + root, err := hujson.Parse(data) if err != nil { return nil, err } - if tok != json.Delim('{') { - return nil, errors.New("top-level value is not a JSON object") + obj, ok := root.Value.(*hujson.Object) + if !ok { + return nil, fmt.Errorf("top-level value is not a JSON object: %w", ErrConfigInvalid) } - for dec.More() { - tok, err := dec.Token() - if err != nil { - return nil, err + return &jsonDoc{bom: bom, fresh: len(obj.Members) == 0, root: root}, nil +} + +// object returns the object at path, or nil when a level is missing. A level +// that exists but is not an object is an error. +func (d *jsonDoc) object(path []string) (*hujson.Object, error) { + obj := d.root.Value.(*hujson.Object) + for i, key := range path { + member := findMember(obj, key) + if member == nil { + return nil, nil } - key, ok := tok.(string) + next, ok := member.Value.Value.(*hujson.Object) if !ok { - return nil, errors.New("object key is not a string") - } - var raw json.RawMessage - if err := dec.Decode(&raw); err != nil { - return nil, err + return nil, fmt.Errorf("%q is not a JSON object: %w", strings.Join(path[:i+1], "."), ErrConfigInvalid) } - obj.put(key, raw) - } - if _, err := dec.Token(); err != nil { - return nil, err - } - if _, err := dec.Token(); err != io.EOF { - return nil, errors.New("unexpected data after the JSON object") + obj = next } return obj, nil } -func (o *jsonObject) put(key string, raw json.RawMessage) { - if _, ok := o.values[key]; !ok { - o.keys = append(o.keys, key) +func findMember(obj *hujson.Object, name string) *hujson.ObjectMember { + for i := range obj.Members { + if memberName(obj.Members[i]) == name { + return &obj.Members[i] + } } - o.values[key] = raw + return nil +} + +func memberName(m hujson.ObjectMember) string { + return m.Name.Value.(hujson.Literal).String() } -func (o *jsonObject) del(key string) { - if _, ok := o.values[key]; !ok { - return +// decodeObject returns the value as a JSON object, ignoring comments and +// trailing commas. The tree itself is not changed. +func decodeObject(v *hujson.Value) (map[string]any, bool) { + std, err := hujson.Standardize(v.Pack()) + if err != nil { + return nil, false + } + var out map[string]any + if err := json.Unmarshal(std, &out); err != nil || out == nil { + return nil, false } - delete(o.values, key) - o.keys = slices.DeleteFunc(o.keys, func(k string) bool { return k == key }) + return out, true } -// child returns the object under key, or an empty one when key is absent. -func (o *jsonObject) child(key string) (*jsonObject, error) { - raw, ok := o.values[key] +func (d *jsonDoc) entry(root []string, name string) (map[string]any, bool, error) { + table, err := d.object(root) + if err != nil || table == nil { + return nil, false, err + } + member := findMember(table, name) + if member == nil { + return nil, false, nil + } + entry, ok := decodeObject(&member.Value) if !ok { - return &jsonObject{values: map[string]json.RawMessage{}}, nil + return nil, false, fmt.Errorf("server %q is not a JSON object: %w", name, ErrConfigInvalid) } - child, err := parseJSONObject(raw) - if err != nil { - return nil, fmt.Errorf("%q is not a JSON object: %w", key, ErrConfigInvalid) + return entry, true, nil +} + +// entries returns the object-valued members under root. Other members are not +// server entries and are skipped. +func (d *jsonDoc) entries(root []string) (map[string]map[string]any, error) { + out := map[string]map[string]any{} + table, err := d.object(root) + if err != nil || table == nil { + return out, err + } + for _, member := range table.Members { + if entry, ok := decodeObject(&member.Value); ok { + out[memberName(member)] = entry + } } - return child, nil + return out, nil } -// edit applies fn to the object at path, creating missing levels and writing -// every changed level back into its parent. -func (o *jsonObject) edit(path []string, fn func(*jsonObject)) error { - if len(path) == 0 { - fn(o) - return nil +// set stores entry as name under root, creating the missing levels of root. The +// layout follows the two-space indentation the agents write: a member sits at +// the depth of its key, and a new member starts its own line. +func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { + n := len(root) + first := n + for i := range root { + obj, err := d.object(root[:i+1]) + if err != nil { + return err + } + if obj == nil { + first = i + break + } } - child, err := o.child(path[0]) + parent, err := d.object(root) if err != nil { return err } - if err := child.edit(path[1:], fn); err != nil { + entryNew := parent == nil || findMember(parent, name) == nil + value, err := encodeIndented(entry, indent(n+1)) + if err != nil { return err } - raw, err := child.compact() + var ops []patch + for i := first; i < n; i++ { + ops = append(ops, patch{op: "add", path: jsonPointer(root[:i+1]...), value: []byte("{\n" + indent(i+1) + "}")}) + } + ops = append(ops, patch{op: "add", path: jsonPointer(append(root, name)...), value: value}) + if err := d.apply(ops); err != nil { + return err + } + for i := first; i < n; i++ { + obj, err := d.object(root[:i]) + if err != nil { + return err + } + place(obj, root[i], i+1, true) + } + obj, err := d.object(root) if err != nil { return err } - o.put(path[0], raw) + place(obj, name, n+1, entryNew) return nil } -// compact serializes o without insignificant whitespace. -func (o *jsonObject) compact() ([]byte, error) { - var buf bytes.Buffer - buf.WriteByte('{') - for i, key := range o.keys { - if i > 0 { - buf.WriteByte(',') - } - name, err := json.Marshal(key) - if err != nil { - return nil, err - } - buf.Write(name) - buf.WriteByte(':') - buf.Write(o.values[key]) +// place lays out a member that set wrote: the value follows the colon after a +// space, and a new member starts its own line at depth. +func place(obj *hujson.Object, name string, depth int, newMember bool) { + m := findMember(obj, name) + m.Value.BeforeExtra = hujson.Extra(" ") + if newMember { + m.Name.BeforeExtra = hujson.Extra("\n" + indent(depth)) } - buf.WriteByte('}') - return buf.Bytes(), nil } -// jsonDoc is an agent config file in JSON. -type jsonDoc struct { - root *jsonObject +// indent returns the two-space indentation of the given depth. +func indent(depth int) string { + return strings.Repeat(" ", depth) } -func parseJSON(data []byte) (document, error) { - root, err := parseJSONObject(data) +func (d *jsonDoc) remove(root []string, name string) error { + return d.apply([]patch{{op: "remove", path: jsonPointer(append(root, name)...)}}) +} + +func (d *jsonDoc) apply(ops []patch) error { + data, err := marshalPatch(ops) if err != nil { - return nil, err + return err } - return &jsonDoc{root: root}, nil + return d.root.Patch(data) } -// table returns the object at root. Missing levels read as empty objects. -func (d *jsonDoc) table(root []string) (*jsonObject, error) { - obj := d.root - for _, key := range root { - next, err := obj.child(key) +// bytes returns the document. A file that had no members is written with +// two-space indentation and a trailing newline, as the agents write their own. +func (d *jsonDoc) bytes() ([]byte, error) { + var out []byte + if d.fresh { + std, err := hujson.Standardize(d.root.Pack()) if err != nil { return nil, err } - obj = next + var buf bytes.Buffer + if err := json.Indent(&buf, std, "", " "); err != nil { + return nil, err + } + buf.WriteByte('\n') + out = buf.Bytes() + } else { + out = d.root.Pack() } - return obj, nil + return withBOM(out, d.bom), nil } -// asObject decodes raw as a JSON object. Other JSON values are reported as not ok. -func asObject(raw json.RawMessage) (map[string]any, bool) { - var v any - if err := json.Unmarshal(raw, &v); err != nil { - return nil, false - } - obj, ok := v.(map[string]any) - return obj, ok +// patch is one RFC 6902 operation. value holds encoded JSON, spliced in as +// given: json.Marshal would escape <, > and & inside it. +type patch struct { + op string + path string + value []byte } -func (d *jsonDoc) entry(root []string, name string) (map[string]any, bool, error) { - table, err := d.table(root) - if err != nil { - return nil, false, err - } - raw, ok := table.values[name] - if !ok { - return nil, false, nil - } - entry, ok := asObject(raw) - if !ok { - return nil, false, fmt.Errorf("server %q is not a JSON object: %w", name, ErrConfigInvalid) +func marshalPatch(ops []patch) ([]byte, error) { + var buf bytes.Buffer + buf.WriteByte('[') + for i, op := range ops { + if i > 0 { + buf.WriteByte(',') + } + path, err := encodeJSON(op.path) + if err != nil { + return nil, err + } + fmt.Fprintf(&buf, `{"op":%q,"path":%s`, op.op, path) + if op.value != nil { + buf.WriteString(`,"value":`) + buf.Write(op.value) + } + buf.WriteByte('}') } - return entry, true, nil + buf.WriteByte(']') + return buf.Bytes(), nil } -// entries returns the object-valued members under root. Other members are not -// server entries and are skipped. -func (d *jsonDoc) entries(root []string) (map[string]map[string]any, error) { - table, err := d.table(root) - if err != nil { - return nil, err +// jsonPointer builds an RFC 6901 JSON Pointer from path segments. +func jsonPointer(segments ...string) string { + var b strings.Builder + r := strings.NewReplacer("~", "~0", "/", "~1") + for _, s := range segments { + b.WriteByte('/') + b.WriteString(r.Replace(s)) } - out := map[string]map[string]any{} - for _, key := range table.keys { - if entry, ok := asObject(table.values[key]); ok { - out[key] = entry - } - } - return out, nil + return b.String() } -func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { - raw, err := json.Marshal(entry) - if err != nil { - return err - } - return d.root.edit(root, func(table *jsonObject) { table.put(name, raw) }) +// encodeJSON encodes v as compact JSON without escaping <, > and &. +func encodeJSON(v any) ([]byte, error) { + return encodeWith(v, "", "") } -func (d *jsonDoc) remove(root []string, name string) error { - return d.root.edit(root, func(table *jsonObject) { table.del(name) }) +// encodeIndented encodes v with two-space indentation. Lines after the first +// start with prefix, the indentation of the member the value belongs to. +func encodeIndented(v any, prefix string) ([]byte, error) { + return encodeWith(v, prefix, " ") } -// bytes returns the document with two-space indentation and a trailing newline, -// which is what the agents write themselves. -func (d *jsonDoc) bytes() ([]byte, error) { - compact, err := d.root.compact() - if err != nil { - return nil, err - } - var out bytes.Buffer - if err := json.Indent(&out, compact, "", " "); err != nil { +func encodeWith(v any, prefix, step string) ([]byte, error) { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + enc.SetEscapeHTML(false) + enc.SetIndent(prefix, step) + if err := enc.Encode(v); err != nil { return nil, err } - out.WriteByte('\n') - return out.Bytes(), nil + return bytes.TrimSuffix(buf.Bytes(), []byte("\n")), nil } diff --git a/libs/aitools/mcp/agentconfig/json_test.go b/libs/aitools/mcp/agentconfig/json_test.go new file mode 100644 index 00000000000..ff4e6217458 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -0,0 +1,109 @@ +package agentconfig + +import ( + "testing" + + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tailscale/hujson" +) + +// semanticJSONC decodes a file with comments and trailing commas. +func semanticJSONC(t *testing.T, data string) map[string]any { + t.Helper() + std, err := hujson.Standardize([]byte(data)) + require.NoError(t, err) + return semantic(t, mcpconfig.FormatJSON, std) +} + +// jsonEdit runs fn on a document parsed from data and returns the result bytes. +func jsonEdit(t *testing.T, data string, fn func(d document)) string { + t.Helper() + doc, err := parseJSON([]byte(data)) + require.NoError(t, err) + fn(doc) + out, err := doc.bytes() + require.NoError(t, err) + return string(out) +} + +func TestJSONKeepsCommentsTrailingCommasAndValues(t *testing.T) { + data := `{ + // Agent settings + "theme": "dark", /* keep */ + "big": 12345678901234567890, "text": "café", + "mcpServers": { + "other": {"command": "npx"}, + }, +} +` + entry := testEntry(testCmd, testArgs("test"), nil) + got := jsonEdit(t, data, func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, testName, entry)) + }) + assert.Contains(t, got, "// Agent settings\n") + assert.Contains(t, got, `"theme": "dark", /* keep */`) + assert.Contains(t, got, `"big": 12345678901234567890, "text": "café",`) + assert.Contains(t, got, "\"other\": {\"command\": \"npx\"},\n \""+testName+"\": {\n") + assert.Contains(t, got, "\n },\n}\n") + + // Removing the entry leaves the other members and the comments in place. + back := jsonEdit(t, got, func(d document) { + require.NoError(t, d.remove([]string{"mcpServers"}, testName)) + }) + assert.Contains(t, back, "// Agent settings\n") + assert.Equal(t, semanticJSONC(t, data), semanticJSONC(t, back)) +} + +func TestJSONCreatesMissingLevels(t *testing.T) { + got := jsonEdit(t, `{"theme": "dark"}`, func(d document) { + require.NoError(t, d.set([]string{"mcp", "servers"}, "x", map[string]any{"type": "local"})) + }) + assert.Equal(t, map[string]any{ + "theme": "dark", + "mcp": map[string]any{"servers": map[string]any{"x": map[string]any{"type": "local"}}}, + }, semantic(t, mcpconfig.FormatJSON, []byte(got))) +} + +func TestJSONBOMIsKept(t *testing.T) { + got := jsonEdit(t, "\xEF\xBB\xBF{\"a\": 1}\n", func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, testName, testEntry(testCmd, nil, nil))) + }) + assert.Equal(t, "\xEF\xBB\xBF", got[:3]) + assert.Contains(t, got, `"a": 1`) + assert.Contains(t, got, testName) +} + +// A new file gets two-space indentation and a trailing newline, and <, > and & +// are written as they are. +func TestJSONNewFileLayout(t *testing.T) { + entry := map[string]any{"command": "/a&c", "args": []string{"x"}} + got := jsonEdit(t, "", func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, "x", entry)) + }) + assert.Equal(t, `{ + "mcpServers": { + "x": { + "args": [ + "x" + ], + "command": "/a&c" + } + } +} +`, got) +} + +func TestJSONRejectsNonObjectTopLevel(t *testing.T) { + _, err := parseJSON([]byte(`"text"`)) + require.ErrorIs(t, err, ErrConfigInvalid) +} + +func TestJSONEntriesSkipNonObjects(t *testing.T) { + doc, err := parseJSON([]byte(`{"mcpServers": {"a": {"command": "x"}, "b": 3}}`)) + require.NoError(t, err) + entries, err := doc.entries([]string{"mcpServers"}) + require.NoError(t, err) + assert.Equal(t, map[string]map[string]any{"a": {"command": "x"}}, entries) +} diff --git a/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json index 502ad483c53..4347c8ac1c4 100644 --- a/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json +++ b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json @@ -2,10 +2,7 @@ "mcpServers": { "other": { "command": "npx", - "args": [ - "-y", - "other-mcp" - ], + "args": ["-y", "other-mcp"], "disabled": false }, "genie_one_mcp-dogfood": { diff --git a/libs/aitools/mcp/agentconfig/testdata/codex/after.toml b/libs/aitools/mcp/agentconfig/testdata/codex/after.toml index 6078f359bb5..db4e5bff87f 100644 --- a/libs/aitools/mcp/agentconfig/testdata/codex/after.toml +++ b/libs/aitools/mcp/agentconfig/testdata/codex/after.toml @@ -1,13 +1,13 @@ +# Codex settings. This comment is kept. model = "gpt-5" -[mcp_servers] - [mcp_servers.genie_one_mcp-dogfood] - args = ["aitools", "mcp", "serve", "--profile", "dogfood", "--service", "system.ai.genie_one_mcp", "--agent", "codex"] - command = "/opt/databricks/bin/databricks" - [mcp_servers.other] - args = ["-y", "other-mcp"] - command = "npx" +[projects."/work"] +trust_level = "trusted" -[projects] - [projects."/work"] - trust_level = "trusted" +[mcp_servers.other] +command = "npx" +args = ["-y", "other-mcp"] + +[mcp_servers.genie_one_mcp-dogfood] +args = ["aitools", "mcp", "serve", "--profile", "dogfood", "--service", "system.ai.genie_one_mcp", "--agent", "codex"] +command = "/opt/databricks/bin/databricks" diff --git a/libs/aitools/mcp/agentconfig/testdata/codex/before.toml b/libs/aitools/mcp/agentconfig/testdata/codex/before.toml index e9be6e9eb28..dcc64749324 100644 --- a/libs/aitools/mcp/agentconfig/testdata/codex/before.toml +++ b/libs/aitools/mcp/agentconfig/testdata/codex/before.toml @@ -1,4 +1,4 @@ -# Codex settings. This comment is dropped on rewrite. +# Codex settings. This comment is kept. model = "gpt-5" [projects."/work"] diff --git a/libs/aitools/mcp/agentconfig/testdata/copilot/after.json b/libs/aitools/mcp/agentconfig/testdata/copilot/after.json index 633a33098ab..2dca4240bd4 100644 --- a/libs/aitools/mcp/agentconfig/testdata/copilot/after.json +++ b/libs/aitools/mcp/agentconfig/testdata/copilot/after.json @@ -4,13 +4,8 @@ "other": { "type": "local", "command": "npx", - "args": [ - "-y", - "other-mcp" - ], - "tools": [ - "*" - ] + "args": ["-y", "other-mcp"], + "tools": ["*"] }, "genie_one_mcp-dogfood": { "args": [ diff --git a/libs/aitools/mcp/agentconfig/testdata/cursor/after.json b/libs/aitools/mcp/agentconfig/testdata/cursor/after.json index 278e918d5da..281a1c106a5 100644 --- a/libs/aitools/mcp/agentconfig/testdata/cursor/after.json +++ b/libs/aitools/mcp/agentconfig/testdata/cursor/after.json @@ -3,10 +3,7 @@ "mcpServers": { "other": { "command": "npx", - "args": [ - "-y", - "other-mcp" - ] + "args": ["-y", "other-mcp"] }, "genie_one_mcp-dogfood": { "args": [ diff --git a/libs/aitools/mcp/agentconfig/testdata/gemini/after.json b/libs/aitools/mcp/agentconfig/testdata/gemini/after.json index 68855f01261..40184dd02ec 100644 --- a/libs/aitools/mcp/agentconfig/testdata/gemini/after.json +++ b/libs/aitools/mcp/agentconfig/testdata/gemini/after.json @@ -5,10 +5,7 @@ "mcpServers": { "other": { "command": "npx", - "args": [ - "-y", - "other-mcp" - ] + "args": ["-y", "other-mcp"] }, "genie_one_mcp-dogfood": { "args": [ diff --git a/libs/aitools/mcp/agentconfig/testdata/opencode/after.json b/libs/aitools/mcp/agentconfig/testdata/opencode/after.json index f9c5da49cdc..1a532dcad2f 100644 --- a/libs/aitools/mcp/agentconfig/testdata/opencode/after.json +++ b/libs/aitools/mcp/agentconfig/testdata/opencode/after.json @@ -5,11 +5,7 @@ "servers": { "other": { "type": "local", - "command": [ - "npx", - "-y", - "other-mcp" - ] + "command": ["npx", "-y", "other-mcp"] }, "genie_one_mcp-dogfood": { "command": [ diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index 1918e62f957..b519ab3f7d1 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -2,40 +2,41 @@ package agentconfig import ( "bytes" + "errors" "fmt" + "maps" + "slices" + "strings" "github.com/BurntSushi/toml" ) -// tomlDoc is an agent config file in TOML, decoded into plain maps. Encoding it -// again drops comments and sorts keys; Upsert and Remove write only when an -// entry changes, so an untouched file keeps its comments. +// tomlDoc is an agent config file in TOML. Reads decode the file. An edit +// replaces, deletes or appends the tables of one server as text, so comments +// and every other line stay as they are. The edited text is decoded again and +// must hold the wanted server and the same other settings, or it is not used. type tomlDoc struct { + bom bool + text []byte // the file without its BOM root map[string]any } func parseTOML(data []byte) (document, error) { + data, bom := stripBOM(data) root := map[string]any{} if _, err := toml.Decode(string(data), &root); err != nil { return nil, err } - return &tomlDoc{root: root}, nil + return &tomlDoc{bom: bom, text: data, root: root}, nil } -// table returns the table at root. A missing table reads as empty, and is -// created when create is set. -func (d *tomlDoc) table(root []string, create bool) (map[string]any, error) { +// table returns the table at root. A missing table reads as empty. +func (d *tomlDoc) table(root []string) (map[string]any, error) { t := d.root for _, key := range root { next, ok := t[key] if !ok { - if !create { - return map[string]any{}, nil - } - m := map[string]any{} - t[key] = m - t = m - continue + return map[string]any{}, nil } m, ok := next.(map[string]any) if !ok { @@ -47,7 +48,7 @@ func (d *tomlDoc) table(root []string, create bool) (map[string]any, error) { } func (d *tomlDoc) entry(root []string, name string) (map[string]any, bool, error) { - table, err := d.table(root, false) + table, err := d.table(root) if err != nil { return nil, false, err } @@ -63,7 +64,7 @@ func (d *tomlDoc) entry(root []string, name string) (map[string]any, bool, error } func (d *tomlDoc) entries(root []string) (map[string]map[string]any, error) { - table, err := d.table(root, false) + table, err := d.table(root) if err != nil { return nil, err } @@ -77,27 +78,289 @@ func (d *tomlDoc) entries(root []string) (map[string]map[string]any, error) { } func (d *tomlDoc) set(root []string, name string, entry map[string]any) error { - table, err := d.table(root, true) - if err != nil { + return d.edit(root, name, entry) +} + +func (d *tomlDoc) remove(root []string, name string) error { + return d.edit(root, name, nil) +} + +// edit replaces the tables of server name under root with the rendering of +// entry, or deletes them when entry is nil. A server that exists without tables +// of its own (an inline table or dotted keys) is an error, not rewritten. +func (d *tomlDoc) edit(root []string, name string, entry map[string]any) error { + nl := "\n" + if bytes.Contains(d.text, []byte("\r\n")) { + nl = "\r\n" + } + lines := splitLines(d.text) + ranges := serverBlocks(lines, root, name) + if _, found, err := d.entry(root, name); err != nil { return err + } else if found && len(ranges) == 0 { + return fmt.Errorf("server %q is not defined by its own [%s] tables (inline table or dotted keys), so it cannot be edited; edit it by hand", + name, strings.Join(append(root, name), ".")) + } + // BurntSushi accepts a table header added to an inline table, which TOML + // does not, so the root table must have a header of its own before we add to it. + if d.defined(root) && !hasHeaderUnder(lines, root) { + return fmt.Errorf("the [%s] table is defined inline or with dotted keys, so this CLI cannot add server %q to it; edit it by hand", + strings.Join(root, "."), name) + } + + var block []byte + if entry != nil { + var err error + if block, err = renderBlock(root, name, entry, nl); err != nil { + return err + } } - table[name] = entry + var out []byte + pos := 0 + placed := block == nil + for _, r := range ranges { + start := r.start + // Removing a block also removes the blank line that separated it from the + // lines before, so the file does not gain a double blank line. + if block == nil && start > pos && strings.TrimSpace(string(lines[start-1])) == "" { + start-- + } + for _, line := range lines[pos:start] { + out = append(out, line...) + } + if !placed { + out = append(out, block...) + placed = true + } + pos = r.end + } + for _, line := range lines[pos:] { + out = append(out, line...) + } + if !placed { + out = appendBlock(out, block, nl) + } + + parsed := map[string]any{} + if _, err := toml.Decode(string(out), &parsed); err != nil { + return fmt.Errorf("server %q: the edited file does not decode: %w", name, err) + } + if err := checkEdit(d.root, parsed, root, name, entry); err != nil { + return fmt.Errorf("server %q: %w", name, err) + } + d.text = out + d.root = parsed return nil } -func (d *tomlDoc) remove(root []string, name string) error { - table, err := d.table(root, false) +// checkEdit verifies an edit: the server is entry (or gone when entry is nil), +// and nothing else in the document changed. +func checkEdit(before, after map[string]any, root []string, name string, entry map[string]any) error { + same, err := sameEntry(withoutServer(before, root, name), withoutServer(after, root, name)) + if err != nil { + return err + } + if !same { + return errors.New("the edit changed other settings") + } + got, found, err := (&tomlDoc{root: after}).entry(root, name) + if err != nil { + return err + } + if entry == nil { + if found { + return errors.New("the edit did not remove the server") + } + return nil + } + if !found { + return errors.New("the edit did not add the server") + } + same, err = sameEntry(got, entry) if err != nil { return err } - delete(table, name) + if !same { + return errors.New("the edit wrote a different server") + } return nil } -func (d *tomlDoc) bytes() ([]byte, error) { +// withoutServer returns doc without the server name under root. The tables on +// the way are copied, so doc is not changed. A table left empty is dropped, so +// a file without the table and one whose table only held the server compare equal. +func withoutServer(doc map[string]any, root []string, name string) map[string]any { + out := maps.Clone(doc) + if len(root) == 0 { + delete(out, name) + return out + } + child, ok := out[root[0]].(map[string]any) + if !ok { + return out + } + rest := withoutServer(child, root[1:], name) + if len(rest) == 0 { + delete(out, root[0]) + } else { + out[root[0]] = rest + } + return out +} + +// defined reports whether the key path root exists in the document. +func (d *tomlDoc) defined(root []string) bool { + t := d.root + for _, key := range root { + next, ok := t[key] + if !ok { + return false + } + m, ok := next.(map[string]any) + if !ok { + return true + } + t = m + } + return true +} + +// hasHeaderUnder reports whether a table header in lines is root or below it. +func hasHeaderUnder(lines [][]byte, root []string) bool { + for _, line := range lines { + parts, ok := tomlTableKey(string(line)) + if !ok || len(parts) < len(root) { + continue + } + if slices.Equal(parts[:len(root)], root) { + return true + } + } + return false +} + +// renderBlock renders the tables of one server: [root.name] and any sub-tables, +// such as [root.name.env]. The table header of root itself is dropped, and each +// line is written without the indentation the encoder adds. +func renderBlock(root []string, name string, entry map[string]any, nl string) ([]byte, error) { + var tree any = map[string]any{name: entry} + for _, key := range slices.Backward(root) { + tree = map[string]any{key: tree} + } var buf bytes.Buffer - if err := toml.NewEncoder(&buf).Encode(d.root); err != nil { + if err := toml.NewEncoder(&buf).Encode(tree); err != nil { return nil, err } - return buf.Bytes(), nil + header := "[" + strings.Join(root, ".") + "]" + var out strings.Builder + for line := range strings.SplitSeq(strings.TrimSuffix(buf.String(), "\n"), "\n") { + line = strings.TrimLeft(line, " \t") + if line == header { + continue + } + out.WriteString(line) + out.WriteString(nl) + } + return []byte(out.String()), nil +} + +// appendBlock appends block after out, separated by one blank line. +func appendBlock(out, block []byte, nl string) []byte { + if len(out) > 0 { + if out[len(out)-1] != '\n' { + out = append(out, nl...) + } + out = append(out, nl...) + } + return append(out, block...) +} + +// lineRange is the half-open range of lines [start, end). +type lineRange struct { + start, end int +} + +// serverBlocks returns the line ranges of the tables that define server name +// under root: its [root.name] table and its sub-tables, each ending before the +// next table header. Blank and comment lines right before that header are not +// part of the range, since they describe the header that follows. +func serverBlocks(lines [][]byte, root []string, name string) []lineRange { + var headers []int + var keys [][]string + for i, line := range lines { + if parts, ok := tomlTableKey(string(line)); ok { + headers = append(headers, i) + keys = append(keys, parts) + } + } + var ranges []lineRange + for h, parts := range keys { + if !isServerTable(parts, root, name) { + continue + } + end := len(lines) + if h+1 < len(headers) { + end = headers[h+1] + } + for end > headers[h]+1 && isBlankOrComment(string(lines[end-1])) { + end-- + } + ranges = append(ranges, lineRange{start: headers[h], end: end}) + } + return ranges +} + +func isServerTable(parts, root []string, name string) bool { + if len(parts) <= len(root) || parts[len(root)] != name { + return false + } + for i := range root { + if parts[i] != root[i] { + return false + } + } + return true +} + +// tomlTableKey returns the key path of a table header line, such as +// [mcp_servers.name] or [[mcp_servers.name]]. Other lines report false. +func tomlTableKey(line string) ([]string, bool) { + s := strings.TrimSpace(line) + if !strings.HasPrefix(s, "[") { + return nil, false + } + s = strings.TrimLeft(s, "[") + end := strings.IndexByte(s, ']') + if end < 0 { + return nil, false + } + var parts []string + for p := range strings.SplitSeq(s[:end], ".") { + parts = append(parts, strings.Trim(strings.TrimSpace(p), `"'`)) + } + return parts, true +} + +func isBlankOrComment(line string) bool { + s := strings.TrimSpace(line) + return s == "" || strings.HasPrefix(s, "#") +} + +// splitLines splits data after each newline, keeping the newlines. +func splitLines(data []byte) [][]byte { + var lines [][]byte + for len(data) > 0 { + i := bytes.IndexByte(data, '\n') + if i < 0 { + lines = append(lines, data) + break + } + lines = append(lines, data[:i+1]) + data = data[i+1:] + } + return lines +} + +func (d *tomlDoc) bytes() ([]byte, error) { + return withBOM(d.text, d.bom), nil } diff --git a/libs/aitools/mcp/agentconfig/toml_test.go b/libs/aitools/mcp/agentconfig/toml_test.go new file mode 100644 index 00000000000..c9701adee22 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -0,0 +1,135 @@ +package agentconfig + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// testBlock is the table block Upsert writes for testName with testArgs("test"). +const testBlock = "[mcp_servers." + testName + "]\n" + + `args = ["aitools", "mcp", "serve", "--profile", "dogfood", "--service", "system.ai.genie_one_mcp", "--agent", "test"]` + "\n" + + `command = "` + testCmd + `"` + "\n" + +func TestTOMLAddKeepsCommentsAndLayout(t *testing.T) { + before := "# Codex settings.\nmodel = \"gpt-5\" # default model\n\n[projects.\"/work\"]\ntrust_level = \"trusted\"\n" + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Equal(t, before+"\n"+testBlock, string(readFile(t, path))) +} + +func TestTOMLUpdateReplacesOnlyOwnedTables(t *testing.T) { + before := "[mcp_servers." + testName + "]\n" + + "# old settings\n" + + "command = \"/old/databricks\"\n" + + "args = [\"aitools\", \"mcp\", \"serve\", \"--profile\", \"old\"]\n" + + "\n" + + "# about other\n" + + "[mcp_servers.other]\n" + + "command = \"npx\"\n" + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, testBlock+"\n# about other\n[mcp_servers.other]\ncommand = \"npx\"\n", string(readFile(t, path))) +} + +func TestTOMLRemoveDeletesOnlyOwnedTables(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + before := "# keep\nmodel = \"x\"\n\n" + testBlock + "\n[mcp_servers.other]\ncommand = \"npx\"\n" + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, "# keep\nmodel = \"x\"\n\n[mcp_servers.other]\ncommand = \"npx\"\n", string(readFile(t, path))) +} + +// A server defined inline or with dotted keys is not rewritten. The error names +// the file and the server. +func TestTOMLRefusesServerWithoutOwnTables(t *testing.T) { + for _, tc := range []struct { + name string + data string + }{ + {"inline table", "[mcp_servers]\n" + testName + ` = { command = "/usr/bin/other", args = [] }` + "\n"}, + {"dotted keys", "[mcp_servers]\n" + testName + `.command = "/usr/bin/other"` + "\n"}, + } { + t.Run(tc.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(tc.data), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) + require.Error(t, err) + assert.ErrorContains(t, err, path) + assert.ErrorContains(t, err, testName) + assert.Equal(t, tc.data, string(readFile(t, path))) + + _, err = Remove(t.Context(), a, testName) + require.Error(t, err) + assert.Equal(t, tc.data, string(readFile(t, path))) + }) + } +} + +// A root table written inline cannot take a [mcp_servers.] table. The +// decoder would accept it, so the edit is refused before anything is written. +func TestTOMLRefusesInlineRootTable(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + data := "mcp_servers = {}\n" + writeFile(t, path, []byte(data), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + assert.ErrorContains(t, err, "defined inline") + assert.Equal(t, data, string(readFile(t, path))) +} + +// The check after an edit compares everything but the server, so a change to +// other settings is refused even when the server itself is right. +func TestCheckEditRejectsOtherChanges(t *testing.T) { + before := map[string]any{"model": "a", "mcp_servers": map[string]any{}} + after := map[string]any{"model": "b", "mcp_servers": map[string]any{testName: testEntry(testCmd, testArgs("test"), nil)}} + err := checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil)) + assert.ErrorContains(t, err, "changed other settings") + + after["model"] = "a" + assert.NoError(t, checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil))) + assert.ErrorContains(t, checkEdit(before, after, []string{"mcp_servers"}, testName, nil), "did not remove") +} + +func TestTOMLKeepsBOMAndCRLF(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte("\xEF\xBB\xBF# c\r\nmodel = \"x\"\r\n"), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + got := string(readFile(t, path)) + assert.True(t, strings.HasPrefix(got, "\xEF\xBB\xBF# c\r\nmodel = \"x\"\r\n\r\n[mcp_servers."), got) + assert.NotContains(t, strings.ReplaceAll(got, "\r\n", ""), "\n", "every newline is CRLF") +} + +func TestTOMLEditOnlyTouchesServerBlockLines(t *testing.T) { + lines := splitLines([]byte("a\nb\n")) + assert.Len(t, lines, 2) + assert.Equal(t, "b\n", string(lines[1])) + _, ok := tomlTableKey(" [[mcp_servers.x]] # c") + assert.True(t, ok) + _, ok = tomlTableKey(`key = "[x]"`) + assert.False(t, ok) +} diff --git a/libs/aitools/mcp/agentconfig/unmanaged.go b/libs/aitools/mcp/agentconfig/unmanaged.go index 0ad91b7e134..b898caa1c14 100644 --- a/libs/aitools/mcp/agentconfig/unmanaged.go +++ b/libs/aitools/mcp/agentconfig/unmanaged.go @@ -15,6 +15,12 @@ func IsUnmanaged(entry map[string]any) bool { mcpconfig.IsBinary(argv[0], "ug", "ug.exe") && argv[1] == "mcp-proxy" { return true } - url, _ := entry["url"].(string) - return strings.Contains(url, "/ai-gateway/mcp-services/") + // Remote entries name their URL differently per agent: url (Cursor, Claude + // Code), httpUrl (Gemini streamable HTTP), serverUrl (Antigravity). + for _, key := range []string{"url", "httpUrl", "serverUrl"} { + if url, _ := entry[key].(string); strings.Contains(url, "/ai-gateway/mcp-services/") { + return true + } + } + return false } diff --git a/libs/aitools/mcp/agentconfig/unmanaged_test.go b/libs/aitools/mcp/agentconfig/unmanaged_test.go index 76d9621fd2d..3c4f2f4590c 100644 --- a/libs/aitools/mcp/agentconfig/unmanaged_test.go +++ b/libs/aitools/mcp/agentconfig/unmanaged_test.go @@ -32,6 +32,16 @@ func TestIsUnmanaged(t *testing.T) { entry: map[string]any{"type": "http", "url": "https://example.test/ai-gateway/mcp-services/system.ai.dbsql"}, want: true, }, + { + name: "gemini httpUrl", + entry: map[string]any{"httpUrl": "https://example.test/ai-gateway/mcp-services/system.ai.dbsql"}, + want: true, + }, + { + name: "antigravity serverUrl", + entry: map[string]any{"serverUrl": "https://example.test/ai-gateway/mcp-services/system.ai.dbsql"}, + want: true, + }, { name: "unrelated url", entry: map[string]any{"type": "http", "url": "https://example.test/mcp"}, diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index bb417c293da..14b1e3e3212 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -1,9 +1,11 @@ // Package agentconfig reads and writes the user-scope MCP server entries of -// coding agents. Each write reads the whole file, changes one entry and writes -// the file back atomically; everything else in the file is kept. +// coding agents. A write changes only the bytes of the one entry it is asked to +// change, writes the file back atomically, and keeps comments and layout +// elsewhere in the file. package agentconfig import ( + "bytes" "context" "encoding/json" "errors" @@ -37,13 +39,15 @@ var ( ErrConfigInvalid = errors.New("agent config file cannot be parsed") ) +var utf8BOM = []byte{0xEF, 0xBB, 0xBF} + // document is an agent config file in memory. type document interface { // entry returns the server entry name under root, and whether it exists. entry(root []string, name string) (map[string]any, bool, error) // entries returns the object-valued members under root, keyed by name. entries(root []string) (map[string]map[string]any, error) - // set stores entry as name under root, creating missing tables. + // set stores entry as name under root, creating missing levels. set(root []string, name string, entry map[string]any) error // remove deletes name under root. It is a no-op when name is absent. remove(root []string, name string) error @@ -53,13 +57,22 @@ type document interface { // Upsert writes a stdio server name for agent a, which runs cmd with args and // env. It replaces an entry this CLI wrote, or any entry when force is set, and -// leaves the file alone when the entry already matches. It returns the config -// file path. +// leaves the file alone when the entry already matches. cmd must be the +// databricks binary, or the entry could never be recognized as ours. It returns +// the config file path. func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []string, env map[string]string, force bool) (string, Status, error) { spec, err := specOf(a) if err != nil { return "", "", err } + want := spec.Entry(cmd, args, env) + ours, err := normalize(want) + if err != nil { + return "", "", err + } + if !mcpconfig.IsDatabricksEntry(ours) { + return "", "", fmt.Errorf("server %q: command %q is not the databricks binary, which must be named databricks or databricks.exe", name, cmd) + } path, err := spec.Path(ctx) if err != nil { return "", "", err @@ -69,13 +82,12 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin return path, "", err } root := rootPath(spec) - want := spec.Entry(cmd, args, env) current, found, err := doc.entry(root, name) if err != nil { return path, "", fmt.Errorf("%s: %w", path, err) } if found { - if !spec.IsOurs(current) && !force { + if !mcpconfig.IsDatabricksEntry(current) && !force { return path, "", fmt.Errorf("%s: server %q: %w", path, name, ErrNotOwned) } same, err := sameEntry(current, want) @@ -122,7 +134,7 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { if !found { return false, nil } - if !spec.IsOurs(current) { + if !mcpconfig.IsDatabricksEntry(current) { return false, fmt.Errorf("%s: server %q: %w", path, name, ErrNotOwned) } if err := doc.remove(root, name); err != nil { @@ -200,10 +212,19 @@ func save(path string, doc document) error { perm = info.Mode().Perm() } // Rename would replace a symlink (dotfile managers link configs) with a - // regular file, so write to the link's target. + // regular file, so write to the link's target. A link whose target does not + // exist is refused: there is no target to write. target := path - if resolved, err := filepath.EvalSymlinks(path); err == nil { + resolved, err := filepath.EvalSymlinks(path) + switch { + case err == nil: target = resolved + case errors.Is(err, fs.ErrNotExist): + if info, lerr := os.Lstat(path); lerr == nil && info.Mode()&os.ModeSymlink != 0 { + return fmt.Errorf("%s is a symlink to a file that does not exist", path) + } + default: + return fmt.Errorf("resolve %s: %w", path, err) } if err := atomicfile.Write(target, data, perm, atomicfile.MkDir(0o755)); err != nil { return fmt.Errorf("write %s: %w", path, err) @@ -211,6 +232,22 @@ func save(path string, doc document) error { return nil } +// stripBOM removes a leading UTF-8 byte order mark, reporting whether there was one. +func stripBOM(data []byte) ([]byte, bool) { + if bytes.HasPrefix(data, utf8BOM) { + return data[len(utf8BOM):], true + } + return data, false +} + +// withBOM puts the byte order mark back in front of data when the file had one. +func withBOM(data []byte, bom bool) []byte { + if !bom { + return data + } + return append(append([]byte{}, utf8BOM...), data...) +} + // sameEntry compares two entries as JSON values, so the in-memory Go types of a // freshly built entry and one decoded from a file compare equal. func sameEntry(a, b map[string]any) (bool, error) { diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 4dcdc799cd1..88fe522f6ee 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -1,7 +1,6 @@ package agentconfig import ( - "bytes" "context" "encoding/json" "os" @@ -13,6 +12,7 @@ import ( "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/testdiff" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -60,7 +60,6 @@ func testAgent(path string, format mcpconfig.Format, root string) *agents.Agent Format: format, Root: root, Entry: testEntry, - IsOurs: mcpconfig.IsDatabricksEntry, }, } } @@ -92,6 +91,9 @@ func semantic(t *testing.T, format mcpconfig.Format, data []byte) map[string]any return out } +// TestRoundTripPerAgent compares against the goldens with newlines normalized, +// so a checkout that turns the fixtures into CRLF still passes. Run with -update +// to regenerate the after files, then review them by hand. func TestRoundTripPerAgent(t *testing.T) { for _, a := range agents.MCPAgents() { t.Run(a.Name, func(t *testing.T) { @@ -110,7 +112,11 @@ func TestRoundTripPerAgent(t *testing.T) { assert.Equal(t, path, gotPath) assert.Equal(t, StatusAdded, status) after := readFile(t, path) - assert.Equal(t, string(readFile(t, filepath.Join("testdata", a.Name, "after."+ext))), string(after)) + goldenAfter := filepath.Join("testdata", a.Name, "after."+ext) + if testdiff.OverwriteMode { + require.NoError(t, os.WriteFile(goldenAfter, after, 0o644)) + } + assert.Equal(t, testdiff.NormalizeNewlines(string(readFile(t, goldenAfter))), testdiff.NormalizeNewlines(string(after))) // The same entry again must not touch the file. _, status, err = Upsert(ctx, a, testName, testCmd, testArgs(a.Name), nil, false) @@ -121,7 +127,7 @@ func TestRoundTripPerAgent(t *testing.T) { entries, err := Read(ctx, a) require.NoError(t, err) require.Contains(t, entries, testName) - assert.True(t, a.MCP.IsOurs(entries[testName])) + assert.True(t, mcpconfig.IsDatabricksEntry(entries[testName])) assert.Contains(t, entries, "other") removed, err := Remove(ctx, a, testName) @@ -180,7 +186,77 @@ func TestUpsertRefusesEntryWeDidNotWrite(t *testing.T) { assert.Equal(t, StatusUpdated, status) entries, err := Read(t.Context(), a) require.NoError(t, err) - assert.True(t, a.MCP.IsOurs(entries[testName])) + assert.True(t, mcpconfig.IsDatabricksEntry(entries[testName])) +} + +// An owned entry with other arguments (say, a different profile) is updated in +// place, and the file is written. +func TestUpsertUpdatesOwnedEntry(t *testing.T) { + for _, tc := range []struct { + name string + format mcpconfig.Format + root string + data string + }{ + {"json", mcpconfig.FormatJSON, "mcpServers", `{"mcpServers": {"` + testName + `": {"command": "` + testCmd + `", "args": ["aitools", "mcp", "serve", "--profile", "old"]}}}`}, + {"toml", mcpconfig.FormatTOML, "mcp_servers", "[mcp_servers." + testName + "]\ncommand = \"" + testCmd + "\"\nargs = [\"aitools\", \"mcp\", \"serve\", \"--profile\", \"old\"]\n"}, + } { + t.Run(tc.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config") + writeFile(t, path, []byte(tc.data), 0o600) + a := testAgent(path, tc.format, tc.root) + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, testArgs("test"), toStrings(t, entries[testName]["args"])) + }) + } +} + +func toStrings(t *testing.T, v any) []string { + t.Helper() + list, ok := v.([]any) + require.True(t, ok, "args is a list") + out := make([]string, 0, len(list)) + for _, item := range list { + s, ok := item.(string) + require.True(t, ok) + out = append(out, s) + } + return out +} + +// The entry we would write must be recognized as ours, or a later Upsert +// needs force and Remove can never delete it. Nothing is written. +func TestUpsertRefusesCommandThatIsNotDatabricks(t *testing.T) { + for _, cmd := range []string{"/usr/local/bin/cli", "/x/mydatabricks"} { + t.Run(cmd, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, cmd, testArgs("test"), nil, true) + assert.ErrorContains(t, err, "must be named databricks") + _, err = os.Stat(path) + assert.ErrorIs(t, err, os.ErrNotExist) + }) + } + + // Windows writes the binary in any case. + path := filepath.Join(t.TempDir(), "config.json") + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + _, status, err := Upsert(t.Context(), a, testName, `C:\Tools\Databricks.EXE`, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) +} + +func TestReadMissingFileHasNoEntries(t *testing.T) { + a := testAgent(filepath.Join(t.TempDir(), "missing.toml"), mcpconfig.FormatTOML, "mcp_servers") + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Empty(t, entries) } func TestRemoveRefusesEntryWeDidNotWrite(t *testing.T) { @@ -267,6 +343,23 @@ func TestUpsertWritesThroughSymlink(t *testing.T) { assert.Contains(t, entries, testName) } +// A link to a missing file is refused rather than replaced by a regular file. +func TestUpsertRefusesDanglingSymlink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("creating symlinks needs privileges on Windows") + } + dir := t.TempDir() + link := filepath.Join(dir, "config.json") + require.NoError(t, os.Symlink(filepath.Join(dir, "missing", "config.json"), link)) + a := testAgent(link, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + assert.ErrorContains(t, err, "does not exist") + info, err := os.Lstat(link) + require.NoError(t, err) + assert.NotZero(t, info.Mode()&os.ModeSymlink, "link is kept") +} + func TestWindowsCommandPathRoundTrip(t *testing.T) { winCmd := `C:\Program Files\databricks\databricks.exe` for _, tc := range []struct { @@ -293,31 +386,3 @@ func TestWindowsCommandPathRoundTrip(t *testing.T) { }) } } - -func TestJSONKeepsOtherValuesByteForByte(t *testing.T) { - path := filepath.Join(t.TempDir(), "config.json") - writeFile(t, path, []byte(`{"big": 12345678901234567890, "ratio": 1.50, "text": "caf\u00e9", "mcpServers": {}}`), 0o600) - a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") - - _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) - require.NoError(t, err) - got := readFile(t, path) - assert.Contains(t, string(got), `"big": 12345678901234567890`) - assert.Contains(t, string(got), `"ratio": 1.50`) - assert.Contains(t, string(got), `"text": "caf\u00e9"`) - assert.True(t, bytes.HasSuffix(got, []byte("}\n")), "file ends with a newline") -} - -// TOML comments are lost on rewrite. This test pins that behavior so a change to -// it is deliberate. -func TestTOMLRewriteDropsComments(t *testing.T) { - path := filepath.Join(t.TempDir(), "config.toml") - writeFile(t, path, []byte("# keep me?\nmodel = \"gpt-5\"\n"), 0o600) - a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - - _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) - require.NoError(t, err) - got := string(readFile(t, path)) - assert.NotContains(t, got, "# keep me?") - assert.Contains(t, got, `model = "gpt-5"`) -} diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 7b07e488fbb..1f61ac2668d 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -29,9 +29,6 @@ type Spec struct { // Entry builds the entry for a stdio server that runs cmd with args and env. // env is empty when there are no variables to pin. Entry func(cmd string, args []string, env map[string]string) map[string]any - // IsOurs reports whether an entry was written by this CLI. It receives entries - // as they were decoded from the file. - IsOurs func(entry map[string]any) bool } // Argv returns the command line of a stdio entry as argv[0] and its arguments. @@ -70,7 +67,7 @@ func Argv(entry map[string]any) ([]string, bool) { } // IsDatabricksEntry reports whether entry runs `databricks aitools mcp serve`, -// which is how we recognize entries we wrote. It is the IsOurs of every agent. +// which is how we recognize entries we wrote. func IsDatabricksEntry(entry map[string]any) bool { argv, ok := Argv(entry) if !ok { @@ -82,9 +79,11 @@ func IsDatabricksEntry(entry map[string]any) bool { } // IsBinary reports whether cmd, a bare name or a path, has one of the given -// base names. Both separators are accepted so that a Windows path is recognized -// on any host. +// base names, ignoring case as Windows does. Both separators are accepted so +// that a Windows path is recognized on any host. func IsBinary(cmd string, names ...string) bool { base := cmd[strings.LastIndexAny(cmd, `/\`)+1:] - return slices.Contains(names, base) + return slices.ContainsFunc(names, func(name string) bool { + return strings.EqualFold(base, name) + }) } diff --git a/libs/aitools/mcpconfig/spec_test.go b/libs/aitools/mcpconfig/spec_test.go index f0d32d7729f..201f7d427d9 100644 --- a/libs/aitools/mcpconfig/spec_test.go +++ b/libs/aitools/mcpconfig/spec_test.go @@ -54,6 +54,7 @@ func TestIsDatabricksEntry(t *testing.T) { {"unix path", map[string]any{"command": "/opt/homebrew/bin/databricks", "args": serve}, true}, {"bare name", map[string]any{"command": "databricks", "args": serve}, true}, {"windows path", map[string]any{"command": `C:\Program Files\databricks\databricks.exe`, "args": serve}, true}, + {"binary name in any case", map[string]any{"command": `C:\Tools\Databricks.EXE`, "args": serve}, true}, {"opencode array", map[string]any{"command": []any{"/usr/bin/databricks", "aitools", "mcp", "serve"}}, true}, {"other binary", map[string]any{"command": "/usr/bin/other", "args": serve}, false}, {"other subcommand", map[string]any{"command": "databricks", "args": []any{"aitools", "mcp", "list"}}, false}, From 0d8f9ed90534c64a8b26d21e23f358367b08f1b5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 13:19:04 +0000 Subject: [PATCH 03/21] Keep user keys and fix JSON layout in agent MCP writers JSON: a comment on the previous member's line stays there when a member is added. An empty table gets its closing brace on its own line. A file with only comments and an empty object keeps its comments. Removing the last member keeps a strict file strict and keeps a trailing comma that was there. Inserted text uses the file's line ending. Upsert updates an entry this CLI wrote only in the keys the builders write, so keys the user added (for example disabled or enabled) survive a re-add. An entry that differs only in those keys is not rewritten. --force still replaces a foreign entry as a whole. Antigravity entries no longer write disabled: false, since the key is optional. Upsert refuses a server name outside [A-Za-z0-9_-] and says whether the command or the args are wrong. Error messages show paths with forward slashes. Tests set the agent overrides on the context instead of the process environment. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 33 +++-- libs/aitools/agents/agents_test.go | 15 ++- libs/aitools/mcp/agentconfig/json.go | 115 ++++++++++++------ libs/aitools/mcp/agentconfig/json_test.go | 115 ++++++++++++++++++ .../testdata/antigravity/after.json | 3 +- libs/aitools/mcp/agentconfig/toml.go | 11 +- libs/aitools/mcp/agentconfig/writer.go | 109 +++++++++++++---- libs/aitools/mcp/agentconfig/writer_test.go | 103 ++++++++++++++-- libs/aitools/mcpconfig/spec.go | 10 ++ libs/aitools/mcpconfig/spec_test.go | 7 ++ 10 files changed, 417 insertions(+), 104 deletions(-) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index d4d6b145d59..ac02ae16d31 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -141,6 +141,16 @@ const ( NameKiro = "kiro" ) +// MCP entry keys shared by the agents' MCP config specs. +const ( + // mcpServersKey is the key of the server table in the JSON MCP config files + // that use it (every agent but Codex and OpenCode). + mcpServersKey = "mcpServers" + // localServerType is the "type" of a local stdio server in Copilot and + // OpenCode. Claude Code writes "stdio" instead. + localServerType = "local" +) + // Databricks plugin identity, shared across the agents that ship a plugin. // The verified install commands are e.g. // @@ -261,7 +271,7 @@ var Registry = []*Agent{ Path: antigravityMCPPath, Format: mcpconfig.FormatJSON, Root: mcpServersKey, - Entry: antigravityMCPEntry, + Entry: stdioEntry, }, }, { @@ -513,16 +523,9 @@ func antigravityMCPPath(ctx context.Context) (string, error) { return homeSubdir(".gemini", "config", "mcp_config.json")(ctx) } -// antigravityMCPEntry writes disabled:false explicitly, as the Antigravity docs -// show it. -func antigravityMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { - entry := stdioEntry(cmd, args, vars) - entry["disabled"] = false - return entry -} - -// stdioEntry is the command/args entry used by Cursor, Codex and Gemini. env is -// omitted when empty. +// stdioEntry is the command/args entry used by Cursor, Codex, Gemini and +// Antigravity. env is omitted when empty. Antigravity's "disabled" key is +// optional and defaults to false, so it is not written. func stdioEntry(cmd string, args []string, vars map[string]string) map[string]any { entry := map[string]any{"command": cmd, "args": args} if len(vars) > 0 { @@ -531,14 +534,6 @@ func stdioEntry(cmd string, args []string, vars map[string]string) map[string]an return entry } -// mcpServersKey is the key of the server table in the JSON MCP config files -// that use it (every agent but Codex and OpenCode). -const mcpServersKey = "mcpServers" - -// localServerType is the "type" of a local stdio server in the agents that -// write one (Claude Code, Copilot, OpenCode). -const localServerType = "local" - // SupportsMCP reports whether this CLI writes the agent's MCP server entries. func (a *Agent) SupportsMCP() bool { return a.MCP != nil diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index bf8435fe9a8..84d2d339eab 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -61,11 +61,12 @@ func TestMCPAgents(t *testing.T) { // clearMCPOverrides returns ctx with a home dir and no override variables, so // the path tests do not depend on the developer's shell. func clearMCPOverrides(t *testing.T) (context.Context, string) { + home := t.TempDir() + ctx := env.WithUserHomeDir(t.Context(), home) for _, key := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA"} { - t.Setenv(key, "") + ctx = env.Set(ctx, key, "") //nolint:fatcontext // each key is set on the same context } - home := t.TempDir() - return env.WithUserHomeDir(t.Context(), home), home + return ctx, home } func TestMCPPaths(t *testing.T) { @@ -92,10 +93,11 @@ func TestMCPPaths(t *testing.T) { {"antigravity", NameAntigravity, nil, filepath.Join(home, ".gemini", "config", "mcp_config.json")}, } { t.Run(tc.name, func(t *testing.T) { + tctx := ctx for k, v := range tc.override { - t.Setenv(k, v) + tctx = env.Set(tctx, k, v) //nolint:fatcontext // each override is set on the same context } - got, err := ByName(tc.agent).MCP.Path(ctx) + got, err := ByName(tc.agent).MCP.Path(tctx) require.NoError(t, err) assert.Equal(t, tc.want, got) }) @@ -142,8 +144,9 @@ func TestMCPEntries(t *testing.T) { "environment": pinned, }, opencode) + // Antigravity's "disabled" key is optional and defaults to false, so the entry does not write it. antigravity := ByName(NameAntigravity).MCP.Entry("/bin/databricks", args, nil) - assert.Equal(t, false, antigravity["disabled"]) + assert.Equal(t, map[string]any{"command": "/bin/databricks", "args": args}, antigravity) } func TestSkillsOnlyNamesMatchesRegistry(t *testing.T) { diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index ad3694f24d2..809825e4118 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -14,26 +14,26 @@ import ( // so every byte outside the changed member is kept. type jsonDoc struct { bom bool - // fresh is set when the file had no members, so its layout is ours to - // choose: the output is re-indented with two spaces. - fresh bool - root hujson.Value + // nl is the file's line ending. Inserted text uses it, so the file keeps a + // single line ending. + nl string + root hujson.Value } func parseJSON(data []byte) (document, error) { data, bom := stripBOM(data) if len(bytes.TrimSpace(data)) == 0 { - data = []byte("{}") + // A missing or blank file is an empty object, written with a trailing newline. + data = []byte("{}\n") } root, err := hujson.Parse(data) if err != nil { return nil, err } - obj, ok := root.Value.(*hujson.Object) - if !ok { + if _, ok := root.Value.(*hujson.Object); !ok { return nil, fmt.Errorf("top-level value is not a JSON object: %w", ErrConfigInvalid) } - return &jsonDoc{bom: bom, fresh: len(obj.Members) == 0, root: root}, nil + return &jsonDoc{bom: bom, nl: lineEnding(data), root: root}, nil } // object returns the object at path, or nil when a level is missing. A level @@ -129,6 +129,13 @@ func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { break } } + // Only the deepest existing object can be empty: the ones above it hold the + // next level of root. + holder, err := d.object(root[:first]) + if err != nil { + return err + } + emptied := len(holder.Members) == 0 parent, err := d.object(root) if err != nil { return err @@ -138,46 +145,103 @@ func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { if err != nil { return err } + value = bytes.ReplaceAll(value, []byte("\n"), []byte(d.nl)) var ops []patch for i := first; i < n; i++ { - ops = append(ops, patch{op: "add", path: jsonPointer(root[:i+1]...), value: []byte("{\n" + indent(i+1) + "}")}) + ops = append(ops, patch{op: "add", path: jsonPointer(root[:i+1]...), value: []byte("{" + d.nl + indent(i+1) + "}")}) } ops = append(ops, patch{op: "add", path: jsonPointer(append(root, name)...), value: value}) if err := d.apply(ops); err != nil { return err } + if emptied { + holder, err := d.object(root[:first]) + if err != nil { + return err + } + holder.AfterExtra = relayout(holder.AfterExtra, d.nl+indent(first)) + } for i := first; i < n; i++ { obj, err := d.object(root[:i]) if err != nil { return err } - place(obj, root[i], i+1, true) + place(obj, root[i], i+1, true, d.nl) } obj, err := d.object(root) if err != nil { return err } - place(obj, name, n+1, entryNew) + place(obj, name, n+1, entryNew, d.nl) return nil } // place lays out a member that set wrote: the value follows the colon after a -// space, and a new member starts its own line at depth. -func place(obj *hujson.Object, name string, depth int, newMember bool) { +// space, and a new member starts its own line at depth. Comments before the +// member stay where they are. +func place(obj *hujson.Object, name string, depth int, newMember bool, nl string) { m := findMember(obj, name) - m.Value.BeforeExtra = hujson.Extra(" ") + if m.Value.BeforeExtra == nil { + m.Value.BeforeExtra = hujson.Extra(" ") + } if newMember { - m.Name.BeforeExtra = hujson.Extra("\n" + indent(depth)) + m.Name.BeforeExtra = relayout(m.Name.BeforeExtra, nl+indent(depth)) } } +// relayout keeps the comments in extra and replaces the whitespace after the +// last one with layout. A line comment ends at its newline, which layout provides. +func relayout(extra hujson.Extra, layout string) hujson.Extra { + kept := bytes.TrimRight(extra, " \t\r\n") + return append(append(hujson.Extra{}, kept...), layout...) +} + // indent returns the two-space indentation of the given depth. func indent(depth int) string { return strings.Repeat(" ", depth) } +// remove deletes name under root. A missing name is a no-op. Removing the last +// member keeps the file's trailing-comma state: a strict file gets no comma after +// the member that becomes last. func (d *jsonDoc) remove(root []string, name string) error { - return d.apply([]patch{{op: "remove", path: jsonPointer(append(root, name)...)}}) + obj, err := d.object(root) + if err != nil || obj == nil || findMember(obj, name) == nil { + return err + } + trailing := hasTrailingComma(obj) + if err := d.apply([]patch{{op: "remove", path: jsonPointer(append(root, name)...)}}); err != nil { + return err + } + obj, err = d.object(root) + if err != nil { + return err + } + setTrailingComma(obj, trailing) + return nil +} + +// hasTrailingComma reports whether the last member of obj is followed by a +// comma. hujson keeps that state in the last member's AfterExtra. +func hasTrailingComma(obj *hujson.Object) bool { + return len(obj.Members) > 0 && obj.Members[len(obj.Members)-1].Value.AfterExtra != nil +} + +// setTrailingComma gives the last member of obj a trailing comma when on is set, +// and removes it otherwise. The whitespace before a removed comma moves to the +// closing brace, so it stays where it was. +func setTrailingComma(obj *hujson.Object, on bool) { + if len(obj.Members) == 0 { + return + } + last := &obj.Members[len(obj.Members)-1].Value + switch { + case on && last.AfterExtra == nil: + last.AfterExtra = hujson.Extra{} + case !on && last.AfterExtra != nil: + obj.AfterExtra = append(append(hujson.Extra{}, last.AfterExtra...), obj.AfterExtra...) + last.AfterExtra = nil + } } func (d *jsonDoc) apply(ops []patch) error { @@ -188,25 +252,8 @@ func (d *jsonDoc) apply(ops []patch) error { return d.root.Patch(data) } -// bytes returns the document. A file that had no members is written with -// two-space indentation and a trailing newline, as the agents write their own. func (d *jsonDoc) bytes() ([]byte, error) { - var out []byte - if d.fresh { - std, err := hujson.Standardize(d.root.Pack()) - if err != nil { - return nil, err - } - var buf bytes.Buffer - if err := json.Indent(&buf, std, "", " "); err != nil { - return nil, err - } - buf.WriteByte('\n') - out = buf.Bytes() - } else { - out = d.root.Pack() - } - return withBOM(out, d.bom), nil + return withBOM(d.root.Pack(), d.bom), nil } // patch is one RFC 6902 operation. value holds encoded JSON, spliced in as diff --git a/libs/aitools/mcp/agentconfig/json_test.go b/libs/aitools/mcp/agentconfig/json_test.go index ff4e6217458..2cbe8243652 100644 --- a/libs/aitools/mcp/agentconfig/json_test.go +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -1,6 +1,8 @@ package agentconfig import ( + "encoding/json" + "strings" "testing" "github.com/databricks/cli/libs/aitools/mcpconfig" @@ -107,3 +109,116 @@ func TestJSONEntriesSkipNonObjects(t *testing.T) { require.NoError(t, err) assert.Equal(t, map[string]map[string]any{"a": {"command": "x"}}, entries) } + +// requireStrictJSON fails unless data is strict JSON, which agents such as +// Claude Code read. +func requireStrictJSON(t *testing.T, data string) { + t.Helper() + var v any + require.NoError(t, json.Unmarshal([]byte(data), &v), data) +} + +// A comment after the last member is on that member's line. Adding a member +// after it keeps the comment there. +func TestJSONAddKeepsCommentOnPreviousMember(t *testing.T) { + data := "{\n \"mcpServers\": {\n \"other\": {\"command\": \"npx\"} // other server\n }\n}\n" + got := jsonEdit(t, data, func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) + }) + assert.Equal(t, "{\n \"mcpServers\": {\n \"other\": {\"command\": \"npx\"}, // other server\n \"x\": {\n \"command\": \"x\"\n }\n }\n}\n", got) +} + +// An empty table gets its member on its own line, and its closing brace on a +// line of its own. +func TestJSONAddToEmptyTable(t *testing.T) { + got := jsonEdit(t, "{\n \"mcpServers\": {}\n}\n", func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) + }) + assert.Equal(t, "{\n \"mcpServers\": {\n \"x\": {\n \"command\": \"x\"\n }\n }\n}\n", got) +} + +// A file with only comments and an empty object keeps its comments. Only the +// layout of the object changes. +func TestJSONAddKeepsCommentsOfEmptyObject(t *testing.T) { + for _, tc := range []struct { + data string + want string + }{ + {"// x\n{}", "// x\n{\n \"mcpServers\": {\n \"x\": {\n \"command\": \"x\"\n }\n }\n}"}, + {"{ /* c */ }", "{\n \"mcpServers\": {\n \"x\": {\n \"command\": \"x\"\n }\n } /* c */\n}"}, + } { + t.Run(tc.data, func(t *testing.T) { + got := jsonEdit(t, tc.data, func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) + }) + assert.Equal(t, tc.want, got) + }) + } +} + +// An empty object file gets one trailing newline, as the file had. +func TestJSONAddToEmptyObjectFile(t *testing.T) { + want := "{\n \"mcpServers\": {\n \"x\": {\n \"command\": \"x\"\n }\n }\n}\n" + for _, data := range []string{"{}\n", "", " \n"} { + got := jsonEdit(t, data, func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) + }) + assert.Equal(t, want, got, "input %q", data) + } +} + +// Removing the last member keeps a strict file strict. The comma that stood +// after the previous member's space is not left behind. +func TestJSONRemoveLastKeepsStrictJSON(t *testing.T) { + for _, data := range []string{ + "{\n \"mcpServers\": {\n \"a\": {} ,\n \"b\": {\"command\": \"x\"}\n }\n}\n", + "{\"mcpServers\": {\"a\": {} , \"b\": {\"command\": \"x\"}}}", + "{\"mcpServers\": {\"a\": {} , \"b\": {\"command\": \"x\"}}}", + } { + got := jsonEdit(t, data, func(d document) { + require.NoError(t, d.remove([]string{"mcpServers"}, "b")) + }) + requireStrictJSON(t, got) + assert.Equal(t, map[string]any{"mcpServers": map[string]any{"a": map[string]any{}}}, semantic(t, mcpconfig.FormatJSON, []byte(got))) + } +} + +// A file with a trailing comma keeps the trailing comma when its last member is removed. +func TestJSONRemoveLastKeepsTrailingComma(t *testing.T) { + got := jsonEdit(t, "{\"mcpServers\": {\"a\": {}, \"b\": {},}}", func(d document) { + require.NoError(t, d.remove([]string{"mcpServers"}, "b")) + }) + assert.Equal(t, "{\"mcpServers\": {\"a\": {},}}", got) +} + +// A CRLF file gets CRLF in inserted text, so the file has one line ending. +func TestJSONKeepsCRLF(t *testing.T) { + data := "{\r\n \"mcpServers\": {\r\n \"other\": {\"command\": \"npx\"}\r\n }\r\n}\r\n" + got := jsonEdit(t, data, func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x", "args": []string{"a"}})) + }) + assert.Equal(t, strings.Count(got, "\n"), strings.Count(got, "\r\n"), got) + assert.Contains(t, got, "\"other\": {\"command\": \"npx\"},\r\n \"x\": {\r\n") + + got = jsonEdit(t, got, func(d document) { + require.NoError(t, d.remove([]string{"mcpServers"}, "x")) + }) + assert.Equal(t, data, got) +} + +// Adding a member to a strict file keeps it strict, whatever its layout. +func TestJSONAddKeepsStrictJSON(t *testing.T) { + for _, data := range []string{ + "{}", + "{\n}\n", + `{"mcpServers": {}}`, + "{\n \"mcpServers\": {\n \"a\": {} , \"b\": {\"command\": \"x\"}\n }\n}\n", + "{\"theme\": \"dark\", \"mcpServers\": {\"a\": {\"command\": \"x\"}}}", + } { + got := jsonEdit(t, data, func(d document) { + require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "y"})) + }) + requireStrictJSON(t, got) + assert.Equal(t, "y", semantic(t, mcpconfig.FormatJSON, []byte(got))["mcpServers"].(map[string]any)["x"].(map[string]any)["command"], data) + } +} diff --git a/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json index 4347c8ac1c4..0607b1f31e2 100644 --- a/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json +++ b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json @@ -17,8 +17,7 @@ "--agent", "antigravity" ], - "command": "/opt/databricks/bin/databricks", - "disabled": false + "command": "/opt/databricks/bin/databricks" } } } diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index b519ab3f7d1..10db496c4a5 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -89,15 +89,14 @@ func (d *tomlDoc) remove(root []string, name string) error { // entry, or deletes them when entry is nil. A server that exists without tables // of its own (an inline table or dotted keys) is an error, not rewritten. func (d *tomlDoc) edit(root []string, name string, entry map[string]any) error { - nl := "\n" - if bytes.Contains(d.text, []byte("\r\n")) { - nl = "\r\n" - } + nl := lineEnding(d.text) lines := splitLines(d.text) ranges := serverBlocks(lines, root, name) - if _, found, err := d.entry(root, name); err != nil { + _, found, err := d.entry(root, name) + if err != nil { return err - } else if found && len(ranges) == 0 { + } + if found && len(ranges) == 0 { return fmt.Errorf("server %q is not defined by its own [%s] tables (inline table or dotted keys), so it cannot be edited; edit it by hand", name, strings.Join(append(root, name), ".")) } diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 14b1e3e3212..86a81471e06 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "io/fs" + "maps" "os" "path/filepath" "reflect" @@ -55,12 +56,21 @@ type document interface { bytes() ([]byte, error) } +// builderKeys are the entry keys the agent builders write. Upsert overwrites +// them. Any other key was added by the user (for example disabled or enabled) +// and is kept when an entry we wrote is updated. +var builderKeys = []string{"command", "args", "env", "environment", "type", "tools"} + // Upsert writes a stdio server name for agent a, which runs cmd with args and -// env. It replaces an entry this CLI wrote, or any entry when force is set, and -// leaves the file alone when the entry already matches. cmd must be the -// databricks binary, or the entry could never be recognized as ours. It returns -// the config file path. +// env. An entry this CLI wrote is updated in its builder keys, and keys the user +// added to it are kept. A foreign entry is replaced only when force is set. The +// file is left alone when the entry already matches. cmd must be the databricks +// binary, or the entry could never be recognized as ours. It returns the config +// file path. func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []string, env map[string]string, force bool) (string, Status, error) { + if !mcpconfig.ValidServerName(name) { + return "", "", fmt.Errorf("server name %q must contain only letters, digits, '-' and '_'", name) + } spec, err := specOf(a) if err != nil { return "", "", err @@ -70,8 +80,11 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err != nil { return "", "", err } + if !mcpconfig.IsBinary(cmd, "databricks", "databricks.exe") { + return "", "", fmt.Errorf("server %q: command %q must be the databricks binary, named databricks or databricks.exe", name, cmd) + } if !mcpconfig.IsDatabricksEntry(ours) { - return "", "", fmt.Errorf("server %q: command %q is not the databricks binary, which must be named databricks or databricks.exe", name, cmd) + return "", "", fmt.Errorf("server %q: args must start with aitools mcp serve", name) } path, err := spec.Path(ctx) if err != nil { @@ -84,22 +97,33 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin root := rootPath(spec) current, found, err := doc.entry(root, name) if err != nil { - return path, "", fmt.Errorf("%s: %w", path, err) + return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } if found { - if !mcpconfig.IsDatabricksEntry(current) && !force { - return path, "", fmt.Errorf("%s: server %q: %w", path, name, ErrNotOwned) - } - same, err := sameEntry(current, want) - if err != nil { - return path, "", err - } - if same { - return path, StatusUnchanged, nil + if mcpconfig.IsDatabricksEntry(current) { + same, err := sameEntry(builderKeysOf(current), builderKeysOf(want)) + if err != nil { + return path, "", err + } + if same { + return path, StatusUnchanged, nil + } + want = keepUserKeys(current, want) + } else { + if !force { + return path, "", fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) + } + same, err := sameEntry(current, want) + if err != nil { + return path, "", err + } + if same { + return path, StatusUnchanged, nil + } } } if err := doc.set(root, name, want); err != nil { - return path, "", fmt.Errorf("%s: %w", path, err) + return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } if err := save(path, doc); err != nil { return path, "", err @@ -129,16 +153,16 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { root := rootPath(spec) current, found, err := doc.entry(root, name) if err != nil { - return false, fmt.Errorf("%s: %w", path, err) + return false, fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } if !found { return false, nil } if !mcpconfig.IsDatabricksEntry(current) { - return false, fmt.Errorf("%s: server %q: %w", path, name, ErrNotOwned) + return false, fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) } if err := doc.remove(root, name); err != nil { - return false, fmt.Errorf("%s: %w", path, err) + return false, fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } if err := save(path, doc); err != nil { return false, err @@ -163,7 +187,7 @@ func Read(ctx context.Context, a *agents.Agent) (map[string]map[string]any, erro } entries, err := doc.entries(rootPath(spec)) if err != nil { - return nil, fmt.Errorf("%s: %w", path, err) + return nil, fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } return entries, nil } @@ -183,7 +207,7 @@ func rootPath(spec *mcpconfig.Spec) []string { func load(spec *mcpconfig.Spec, path string) (document, error) { data, err := os.ReadFile(path) if err != nil && !errors.Is(err, fs.ErrNotExist) { - return nil, fmt.Errorf("read %s: %w", path, err) + return nil, fmt.Errorf("read %s: %w", filepath.ToSlash(path), err) } var doc document switch spec.Format { @@ -195,7 +219,7 @@ func load(spec *mcpconfig.Spec, path string) (document, error) { return nil, fmt.Errorf("unknown config format %d", spec.Format) } if err != nil { - return nil, fmt.Errorf("%s: %w: %w", path, ErrConfigInvalid, err) + return nil, fmt.Errorf("%s: %w: %w", filepath.ToSlash(path), ErrConfigInvalid, err) } return doc, nil } @@ -205,7 +229,7 @@ func load(spec *mcpconfig.Spec, path string) (document, error) { func save(path string, doc document) error { data, err := doc.bytes() if err != nil { - return fmt.Errorf("serialize %s: %w", path, err) + return fmt.Errorf("serialize %s: %w", filepath.ToSlash(path), err) } perm := os.FileMode(0o600) if info, err := os.Stat(path); err == nil { @@ -221,13 +245,13 @@ func save(path string, doc document) error { target = resolved case errors.Is(err, fs.ErrNotExist): if info, lerr := os.Lstat(path); lerr == nil && info.Mode()&os.ModeSymlink != 0 { - return fmt.Errorf("%s is a symlink to a file that does not exist", path) + return fmt.Errorf("%s is a symlink to a file that does not exist", filepath.ToSlash(path)) } default: - return fmt.Errorf("resolve %s: %w", path, err) + return fmt.Errorf("resolve %s: %w", filepath.ToSlash(path), err) } if err := atomicfile.Write(target, data, perm, atomicfile.MkDir(0o755)); err != nil { - return fmt.Errorf("write %s: %w", path, err) + return fmt.Errorf("write %s: %w", filepath.ToSlash(path), err) } return nil } @@ -240,6 +264,14 @@ func stripBOM(data []byte) ([]byte, bool) { return data, false } +// lineEnding returns CRLF when data uses it, and LF otherwise. +func lineEnding(data []byte) string { + if bytes.Contains(data, []byte("\r\n")) { + return "\r\n" + } + return "\n" +} + // withBOM puts the byte order mark back in front of data when the file had one. func withBOM(data []byte, bom bool) []byte { if !bom { @@ -248,6 +280,31 @@ func withBOM(data []byte, bom bool) []byte { return append(append([]byte{}, utf8BOM...), data...) } +// builderKeysOf returns the keys of entry that the agent builders write. +func builderKeysOf(entry map[string]any) map[string]any { + out := map[string]any{} + for _, key := range builderKeys { + if v, ok := entry[key]; ok { + out[key] = v + } + } + return out +} + +// keepUserKeys returns current with its builder keys set to those of want. The +// other keys of current are kept. +func keepUserKeys(current, want map[string]any) map[string]any { + out := maps.Clone(current) + for _, key := range builderKeys { + if v, ok := want[key]; ok { + out[key] = v + } else { + delete(out, key) + } + } + return out +} + // sameEntry compares two entries as JSON values, so the in-memory Go types of a // freshly built entry and one decoded from a file compare equal. func sameEntry(a, b map[string]any) (bool, error) { diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 88fe522f6ee..ef6ac8fb6e5 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -27,18 +27,13 @@ func testArgs(agent string) []string { } // testHome returns a context whose home is a fresh temp dir, with the override -// variables cleared so the test does not depend on the developer's shell. +// variables set to empty so the test does not depend on the developer's shell. func testHome(t *testing.T) context.Context { - clearAgentOverrides(t) - return env.WithUserHomeDir(t.Context(), t.TempDir()) -} - -// clearAgentOverrides empties the variables the agent resolvers read. An empty -// value is treated as unset, and t.Setenv restores the originals afterwards. -func clearAgentOverrides(t *testing.T) { + ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) for _, key := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA"} { - t.Setenv(key, "") + ctx = env.Set(ctx, key, "") //nolint:fatcontext // each key is set on the same context } + return ctx } func testEntry(cmd string, args []string, vars map[string]string) map[string]any { @@ -162,7 +157,7 @@ func TestUpsertRefusesUnparsableFile(t *testing.T) { _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) require.ErrorIs(t, err, ErrConfigInvalid) - assert.ErrorContains(t, err, path) + assert.ErrorContains(t, err, filepath.ToSlash(path)) _, err = Remove(t.Context(), a, testName) require.ErrorIs(t, err, ErrConfigInvalid) @@ -238,7 +233,7 @@ func TestUpsertRefusesCommandThatIsNotDatabricks(t *testing.T) { a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") _, _, err := Upsert(t.Context(), a, testName, cmd, testArgs("test"), nil, true) - assert.ErrorContains(t, err, "must be named databricks") + assert.ErrorContains(t, err, "must be the databricks binary") _, err = os.Stat(path) assert.ErrorIs(t, err, os.ErrNotExist) }) @@ -386,3 +381,89 @@ func TestWindowsCommandPathRoundTrip(t *testing.T) { }) } } + +// A name that is not a valid server name is refused before anything is read or +// written. +func TestUpsertRefusesInvalidServerName(t *testing.T) { + for _, name := range []string{"genie.one", "genie one", "genie/one", ""} { + t.Run(name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, name, testCmd, testArgs("test"), nil, false) + assert.ErrorContains(t, err, "must contain only letters, digits, '-' and '_'") + _, err = os.Stat(path) + assert.ErrorIs(t, err, os.ErrNotExist) + }) + } +} + +// The error names the part that is wrong: the command or the args. +func TestUpsertNamesTheWrongPart(t *testing.T) { + a := testAgent(filepath.Join(t.TempDir(), "config.json"), mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, "/usr/local/bin/cli", testArgs("test"), nil, false) + assert.ErrorContains(t, err, "must be the databricks binary") + + _, _, err = Upsert(t.Context(), a, testName, testCmd, []string{"aitools", "serve"}, nil, false) + assert.ErrorContains(t, err, "args must start with aitools mcp serve") + assert.NotContains(t, err.Error(), "binary") +} + +// Keys the user added to an entry we wrote are kept when it is updated. An entry +// that matches in the keys we write is not rewritten, and a changed arg is +// repaired. +func TestUpsertKeepsUserKeysJSON(t *testing.T) { + path := filepath.Join(t.TempDir(), "mcp_config.json") + entry := testEntry(testCmd, testArgs("test"), nil) + entry["disabled"] = true + data, err := json.Marshal(map[string]any{"mcpServers": map[string]any{testName: entry}}) + require.NoError(t, err) + writeFile(t, path, data, 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) + assert.Equal(t, data, readFile(t, path)) + + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, testArgs("other"), toStrings(t, entries[testName]["args"])) + assert.Equal(t, true, entries[testName]["disabled"]) + + // --force replaces an entry we did not write, user keys included. + foreign := filepath.Join(t.TempDir(), "foreign.json") + writeFile(t, foreign, []byte(`{"mcpServers":{"`+testName+`":{"command":"/usr/bin/other","args":[],"disabled":true}}}`), 0o600) + _, status, err = Upsert(t.Context(), testAgent(foreign, mcpconfig.FormatJSON, "mcpServers"), testName, testCmd, testArgs("test"), nil, true) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err = Read(t.Context(), testAgent(foreign, mcpconfig.FormatJSON, "mcpServers")) + require.NoError(t, err) + assert.NotContains(t, entries[testName], "disabled") +} + +// A Codex block keeps the keys the user added, such as enabled = false, when +// it is updated in place. +func TestUpsertKeepsUserKeysTOML(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + data := testBlock + "enabled = false\n" + writeFile(t, path, []byte(data), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) + assert.Equal(t, data, string(readFile(t, path))) + + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, testArgs("other"), toStrings(t, entries[testName]["args"])) + assert.Equal(t, false, entries[testName]["enabled"]) +} diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 1f61ac2668d..ace123ce3fd 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -5,6 +5,7 @@ package mcpconfig import ( "context" + "regexp" "slices" "strings" ) @@ -87,3 +88,12 @@ func IsBinary(cmd string, names ...string) bool { return strings.EqualFold(base, name) }) } + +// serverNamePattern is the strictest set of server names the agents accept, so a +// name that matches it is valid for every agent. +var serverNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) + +// ValidServerName reports whether name may be used as an MCP server name. +func ValidServerName(name string) bool { + return serverNamePattern.MatchString(name) +} diff --git a/libs/aitools/mcpconfig/spec_test.go b/libs/aitools/mcpconfig/spec_test.go index 201f7d427d9..50369b59f90 100644 --- a/libs/aitools/mcpconfig/spec_test.go +++ b/libs/aitools/mcpconfig/spec_test.go @@ -66,3 +66,10 @@ func TestIsDatabricksEntry(t *testing.T) { }) } } + +func TestValidServerName(t *testing.T) { + assert.True(t, ValidServerName("genie_one-mcp1")) + for _, name := range []string{"", "a.b", "a b", "a/b", "é"} { + assert.False(t, ValidServerName(name), name) + } +} From f580b77b81edb0b70c30750d9a0d53c44381466d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 13:45:53 +0000 Subject: [PATCH 04/21] Fix agent MCP config writer review findings - Update sets only the launch keys (command, args, env, environment, type). Other keys are kept, and defaults such as Copilot's tools list are written only on first add. An entry whose launch keys match is not rewritten. - JSON updates patch the changed launch keys in place, so the other members keep their bytes. Numbers in user values are decoded with UseNumber. - JSON duplicate member names on the root and server path are refused as ErrConfigInvalid. - TOML table headers are found with multi-line strings protected, using localenv's helper, now exported as ProtectMultilineStrings. Quoted keys are parsed, so a quoted "genie.x" is not "genie", and a header-like line inside a string is text. - Removing a table keeps one blank line between its neighbours, and removing the first server leaves no blank first line. - Remove checks the server name as Upsert does. - The OpenCode path returns stat errors other than not-exist. - New config directories are created 0700. - Smaller fixes: top-level JSON error wording, IsUnmanaged comment, mcpconfig.IsDatabricksBinary, BOM strip via bytes.CutPrefix, ValidServerName without a package-level regexp, and a Windows-safe path assertion in toml_test. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 12 +- libs/aitools/agents/agents_test.go | 19 ++ libs/aitools/mcp/agentconfig/json.go | 128 ++++++++++++- libs/aitools/mcp/agentconfig/json_test.go | 2 +- libs/aitools/mcp/agentconfig/toml.go | 198 +++++++++++++++----- libs/aitools/mcp/agentconfig/toml_test.go | 156 ++++++++++++++- libs/aitools/mcp/agentconfig/unmanaged.go | 4 +- libs/aitools/mcp/agentconfig/writer.go | 125 +++++++----- libs/aitools/mcp/agentconfig/writer_test.go | 171 +++++++++++++++++ libs/aitools/mcpconfig/spec.go | 30 ++- libs/aitools/mcpconfig/spec_test.go | 2 +- libs/localenv/merge.go | 8 +- 12 files changed, 734 insertions(+), 121 deletions(-) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index ac02ae16d31..9246b00c5a0 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -2,6 +2,9 @@ package agents import ( "context" + "errors" + "fmt" + "io/fs" "os" "path/filepath" "runtime" @@ -469,10 +472,15 @@ func openCodeMCPPath(ctx context.Context) (string, error) { return "", err } jsonc := filepath.Join(dir, "opencode.jsonc") - if _, err := os.Stat(jsonc); err == nil { + _, err = os.Stat(jsonc) + switch { + case err == nil: return jsonc, nil + case errors.Is(err, fs.ErrNotExist): + return filepath.Join(dir, "opencode.json"), nil + default: + return "", fmt.Errorf("cannot read %s: %w", filepath.ToSlash(jsonc), err) } - return filepath.Join(dir, "opencode.json"), nil } // openCodeMCPEntry follows the current OpenCode schema: servers live under diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index 84d2d339eab..ac418529afa 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "runtime" "testing" "github.com/databricks/cli/libs/env" @@ -163,3 +164,21 @@ func TestSkillsOnlyNamesMatchesRegistry(t *testing.T) { } } } + +// A config file that cannot be inspected is an error, not a missing file. Creating +// opencode.json beside it would leave the user's file unread. +func TestOpenCodeMCPPathReturnsStatError(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("creating symlinks needs privileges on Windows") + } + ctx, home := clearMCPOverrides(t) + dir := filepath.Join(home, ".config", "opencode") + require.NoError(t, os.MkdirAll(dir, 0o755)) + jsonc := filepath.Join(dir, "opencode.jsonc") + require.NoError(t, os.Symlink(jsonc, jsonc)) + + _, err := ByName(NameOpenCode).MCP.Path(ctx) + require.Error(t, err) + _, err = os.Stat(filepath.Join(dir, "opencode.json")) + assert.ErrorIs(t, err, os.ErrNotExist) +} diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index 809825e4118..59dc1f1bfd5 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -3,12 +3,20 @@ package agentconfig import ( "bytes" "encoding/json" + "errors" "fmt" "strings" "github.com/tailscale/hujson" ) +// RFC 6902 operations used to edit a document. +const ( + opAdd = "add" + opReplace = "replace" + opRemove = "remove" +) + // jsonDoc is an agent config file in JSON with comments (JSONC) and trailing // commas also accepted. Edits are RFC 6902 patches applied to the parsed tree, // so every byte outside the changed member is kept. @@ -31,15 +39,20 @@ func parseJSON(data []byte) (document, error) { return nil, err } if _, ok := root.Value.(*hujson.Object); !ok { - return nil, fmt.Errorf("top-level value is not a JSON object: %w", ErrConfigInvalid) + return nil, errors.New("top-level value is not a JSON object") } return &jsonDoc{bom: bom, nl: lineEnding(data), root: root}, nil } // object returns the object at path, or nil when a level is missing. A level -// that exists but is not an object is an error. +// that exists but is not an object is an error. Every object on the way must have +// unique member names, since agents read the last of two equal names while an +// edit would change the first. func (d *jsonDoc) object(path []string) (*hujson.Object, error) { obj := d.root.Value.(*hujson.Object) + if err := checkUnique(obj); err != nil { + return nil, err + } for i, key := range path { member := findMember(obj, key) if member == nil { @@ -49,11 +62,26 @@ func (d *jsonDoc) object(path []string) (*hujson.Object, error) { if !ok { return nil, fmt.Errorf("%q is not a JSON object: %w", strings.Join(path[:i+1], "."), ErrConfigInvalid) } + if err := checkUnique(next); err != nil { + return nil, err + } obj = next } return obj, nil } +func checkUnique(obj *hujson.Object) error { + seen := map[string]bool{} + for _, m := range obj.Members { + name := memberName(m) + if seen[name] { + return fmt.Errorf("duplicate key %q: %w", name, ErrConfigInvalid) + } + seen[name] = true + } + return nil +} + func findMember(obj *hujson.Object, name string) *hujson.ObjectMember { for i := range obj.Members { if memberName(obj.Members[i]) == name { @@ -75,7 +103,7 @@ func decodeObject(v *hujson.Value) (map[string]any, bool) { return nil, false } var out map[string]any - if err := json.Unmarshal(std, &out); err != nil || out == nil { + if err := decodeJSON(std, &out); err != nil || out == nil { return nil, false } return out, true @@ -90,6 +118,11 @@ func (d *jsonDoc) entry(root []string, name string) (map[string]any, bool, error if member == nil { return nil, false, nil } + if obj, ok := member.Value.Value.(*hujson.Object); ok { + if err := checkUnique(obj); err != nil { + return nil, false, err + } + } entry, ok := decodeObject(&member.Value) if !ok { return nil, false, fmt.Errorf("server %q is not a JSON object: %w", name, ErrConfigInvalid) @@ -141,16 +174,15 @@ func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { return err } entryNew := parent == nil || findMember(parent, name) == nil - value, err := encodeIndented(entry, indent(n+1)) + value, err := d.encodeValue(entry, n+1) if err != nil { return err } - value = bytes.ReplaceAll(value, []byte("\n"), []byte(d.nl)) var ops []patch for i := first; i < n; i++ { - ops = append(ops, patch{op: "add", path: jsonPointer(root[:i+1]...), value: []byte("{" + d.nl + indent(i+1) + "}")}) + ops = append(ops, patch{op: opAdd, path: jsonPointer(root[:i+1]...), value: []byte("{" + d.nl + indent(i+1) + "}")}) } - ops = append(ops, patch{op: "add", path: jsonPointer(append(root, name)...), value: value}) + ops = append(ops, patch{op: opAdd, path: jsonPointer(append(root, name)...), value: value}) if err := d.apply(ops); err != nil { return err } @@ -176,6 +208,86 @@ func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { return nil } +// update changes the launch keys of the server name under root to those of +// entry. A launch key that entry lacks is removed, and one that is missing from +// the file is added. A launch key whose value is unchanged is not touched, and +// the other keys of the server keep their bytes. name must exist. +func (d *jsonDoc) update(root []string, name string, entry map[string]any) error { + current, _, err := d.entry(root, name) + if err != nil { + return err + } + depth := len(root) + 2 + var ops []patch + var added, removed []string + for _, key := range launchKeys { + want, inEntry := entry[key] + have, inFile := current[key] + switch { + case inEntry && !inFile: + value, err := d.encodeValue(want, depth) + if err != nil { + return err + } + ops = append(ops, patch{op: opAdd, path: jsonPointer(append(root, name, key)...), value: value}) + added = append(added, key) + case inFile && !inEntry: + removed = append(removed, key) + case inEntry: + same, err := sameEntry(map[string]any{key: have}, map[string]any{key: want}) + if err != nil { + return err + } + if same { + continue + } + value, err := d.encodeValue(want, depth) + if err != nil { + return err + } + ops = append(ops, patch{op: opReplace, path: jsonPointer(append(root, name, key)...), value: value}) + } + } + if len(ops) > 0 { + if err := d.apply(ops); err != nil { + return err + } + } + for _, key := range removed { + if err := d.remove(append(root, name), key); err != nil { + return err + } + } + server, err := d.server(root, name) + if err != nil { + return err + } + for _, key := range added { + place(server, key, depth, true, d.nl) + } + return nil +} + +// server returns the object of the server name under root. The entry must exist +// and be an object. +func (d *jsonDoc) server(root []string, name string) (*hujson.Object, error) { + table, err := d.object(root) + if err != nil { + return nil, err + } + // entry decoded the member as an object, so its value is an object. + return findMember(table, name).Value.Value.(*hujson.Object), nil +} + +// encodeValue encodes v for a member at depth, with the file's line endings. +func (d *jsonDoc) encodeValue(v any, depth int) ([]byte, error) { + value, err := encodeIndented(v, indent(depth)) + if err != nil { + return nil, err + } + return bytes.ReplaceAll(value, []byte("\n"), []byte(d.nl)), nil +} + // place lays out a member that set wrote: the value follows the colon after a // space, and a new member starts its own line at depth. Comments before the // member stay where they are. @@ -210,7 +322,7 @@ func (d *jsonDoc) remove(root []string, name string) error { return err } trailing := hasTrailingComma(obj) - if err := d.apply([]patch{{op: "remove", path: jsonPointer(append(root, name)...)}}); err != nil { + if err := d.apply([]patch{{op: opRemove, path: jsonPointer(append(root, name)...)}}); err != nil { return err } obj, err = d.object(root) diff --git a/libs/aitools/mcp/agentconfig/json_test.go b/libs/aitools/mcp/agentconfig/json_test.go index 2cbe8243652..534d781c1ba 100644 --- a/libs/aitools/mcp/agentconfig/json_test.go +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -99,7 +99,7 @@ func TestJSONNewFileLayout(t *testing.T) { func TestJSONRejectsNonObjectTopLevel(t *testing.T) { _, err := parseJSON([]byte(`"text"`)) - require.ErrorIs(t, err, ErrConfigInvalid) + require.ErrorContains(t, err, "top-level value is not a JSON object") } func TestJSONEntriesSkipNonObjects(t *testing.T) { diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index 10db496c4a5..fdfecbdb40d 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -6,9 +6,11 @@ import ( "fmt" "maps" "slices" + "strconv" "strings" "github.com/BurntSushi/toml" + "github.com/databricks/cli/libs/localenv" ) // tomlDoc is an agent config file in TOML. Reads decode the file. An edit @@ -77,10 +79,17 @@ func (d *tomlDoc) entries(root []string) (map[string]map[string]any, error) { return out, nil } +// set and update rewrite the tables of the server from entry. The user's keys +// keep their values, but the text of those tables is rendered again, so their +// comments and layout inside the server's tables are not kept. func (d *tomlDoc) set(root []string, name string, entry map[string]any) error { return d.edit(root, name, entry) } +func (d *tomlDoc) update(root []string, name string, entry map[string]any) error { + return d.edit(root, name, entry) +} + func (d *tomlDoc) remove(root []string, name string) error { return d.edit(root, name, nil) } @@ -88,9 +97,14 @@ func (d *tomlDoc) remove(root []string, name string) error { // edit replaces the tables of server name under root with the rendering of // entry, or deletes them when entry is nil. A server that exists without tables // of its own (an inline table or dotted keys) is an error, not rewritten. +// Headers are found in the text with multi-line strings protected, so a line +// inside such a string is never taken for a table. func (d *tomlDoc) edit(root []string, name string, entry map[string]any) error { - nl := lineEnding(d.text) - lines := splitLines(d.text) + protected, restore, err := localenv.ProtectMultilineStrings(string(d.text)) + if err != nil { + return err + } + lines := splitLines([]byte(protected)) ranges := serverBlocks(lines, root, name) _, found, err := d.entry(root, name) if err != nil { @@ -107,49 +121,86 @@ func (d *tomlDoc) edit(root []string, name string, entry map[string]any) error { strings.Join(root, "."), name) } - var block []byte + nl := lineEnding(d.text) + var block [][]byte if entry != nil { - var err error - if block, err = renderBlock(root, name, entry, nl); err != nil { + rendered, err := renderBlock(root, name, entry, nl) + if err != nil { return err } + block = splitLines(rendered) + } + text := restore(string(bytes.Join(spliceBlock(lines, ranges, block, nl), nil))) + + parsed := map[string]any{} + if _, err := toml.Decode(text, &parsed); err != nil { + return fmt.Errorf("server %q: the edited file does not decode: %w", name, err) + } + if err := checkEdit(d.root, parsed, root, name, entry); err != nil { + return fmt.Errorf("server %q: %w", name, err) } - var out []byte + d.text = []byte(text) + d.root = parsed + return nil +} + +// spliceBlock returns lines with the tables at ranges replaced by block, or +// removed when block is nil. The first table takes block and the others are +// dropped, each with its separating blank lines. +func spliceBlock(lines [][]byte, ranges []lineRange, block [][]byte, nl string) [][]byte { + var out [][]byte pos := 0 - placed := block == nil + placed := false for _, r := range ranges { - start := r.start - // Removing a block also removes the blank line that separated it from the - // lines before, so the file does not gain a double blank line. - if block == nil && start > pos && strings.TrimSpace(string(lines[start-1])) == "" { - start-- - } - for _, line := range lines[pos:start] { - out = append(out, line...) - } - if !placed { + out = append(out, lines[pos:r.start]...) + pos = r.end + if block != nil && !placed { out = append(out, block...) placed = true + continue } - pos = r.end - } - for _, line := range lines[pos:] { - out = append(out, line...) + out, pos = dropTable(out, lines, pos, nl) } - if !placed { + out = append(out, lines[pos:]...) + if block != nil && !placed { out = appendBlock(out, block, nl) } + return out +} - parsed := map[string]any{} - if _, err := toml.Decode(string(out), &parsed); err != nil { - return fmt.Errorf("server %q: the edited file does not decode: %w", name, err) +// dropTable is called after a table is dropped, with the lines emitted so far +// and the index of the first line after the table. When nothing is emitted yet, +// the blank lines after the table go, so the file does not start with a blank +// line. Otherwise the blank lines around the table become one blank line, when +// there was one on either side of it and something follows. +func dropTable(out, lines [][]byte, pos int, nl string) ([][]byte, int) { + if !hasContent(out) { + for pos < len(lines) && isBlankLine(lines[pos]) { + pos++ + } + return out, pos } - if err := checkEdit(d.root, parsed, root, name, entry); err != nil { - return fmt.Errorf("server %q: %w", name, err) + blank := false + for len(out) > 0 && isBlankLine(out[len(out)-1]) { + out = out[:len(out)-1] + blank = true } - d.text = out - d.root = parsed - return nil + for pos < len(lines) && isBlankLine(lines[pos]) { + pos++ + blank = true + } + if blank && pos < len(lines) { + out = append(out, []byte(nl)) + } + return out, pos +} + +func hasContent(lines [][]byte) bool { + return slices.ContainsFunc(lines, func(line []byte) bool { return !isBlankLine(line) }) +} + +func isBlankLine(line []byte) bool { + return strings.TrimSpace(string(line)) == "" } // checkEdit verifies an edit: the server is entry (or gone when entry is nil), @@ -264,12 +315,12 @@ func renderBlock(root []string, name string, entry map[string]any, nl string) ([ } // appendBlock appends block after out, separated by one blank line. -func appendBlock(out, block []byte, nl string) []byte { +func appendBlock(out, block [][]byte, nl string) [][]byte { if len(out) > 0 { - if out[len(out)-1] != '\n' { - out = append(out, nl...) + if last := out[len(out)-1]; !bytes.HasSuffix(last, []byte("\n")) { + out[len(out)-1] = append(slices.Clip(last), nl...) } - out = append(out, nl...) + out = append(out, []byte(nl)) } return append(out, block...) } @@ -322,24 +373,87 @@ func isServerTable(parts, root []string, name string) bool { } // tomlTableKey returns the key path of a table header line, such as -// [mcp_servers.name] or [[mcp_servers.name]]. Other lines report false. +// [mcp_servers.name] or [[mcp_servers."a.b"]], with the quoted keys unquoted. +// Other lines, including an array element that starts with a bracket, report +// false. func tomlTableKey(line string) ([]string, bool) { s := strings.TrimSpace(line) - if !strings.HasPrefix(s, "[") { + closing := "]" + switch { + case strings.HasPrefix(s, "[["): + s, closing = s[2:], "]]" + case strings.HasPrefix(s, "["): + s = s[1:] + default: return nil, false } - s = strings.TrimLeft(s, "[") - end := strings.IndexByte(s, ']') - if end < 0 { + var parts []string + for { + s = strings.TrimLeft(s, " \t") + key, rest, ok := tomlKey(s) + if !ok { + return nil, false + } + parts = append(parts, key) + s = strings.TrimLeft(rest, " \t") + if !strings.HasPrefix(s, ".") { + break + } + s = s[1:] + } + if !strings.HasPrefix(s, closing) { return nil, false } - var parts []string - for p := range strings.SplitSeq(s[:end], ".") { - parts = append(parts, strings.Trim(strings.TrimSpace(p), `"'`)) + if tail := strings.TrimSpace(s[len(closing):]); tail != "" && !strings.HasPrefix(tail, "#") { + return nil, false } return parts, true } +// tomlKey reads one key at the start of s, a bare key or a basic ("...") or +// literal ('...') quoted key, and returns it with the rest of s. +func tomlKey(s string) (key, rest string, ok bool) { + if s == "" { + return "", "", false + } + switch s[0] { + case '"': + end := 1 + for end < len(s) && s[end] != '"' { + if s[end] == '\\' { + end++ + } + end++ + } + if end >= len(s) { + return "", "", false + } + name, err := strconv.Unquote(s[:end+1]) + if err != nil { + return "", "", false + } + return name, s[end+1:], true + case '\'': + end := strings.IndexByte(s[1:], '\'') + if end < 0 { + return "", "", false + } + return s[1 : end+1], s[end+2:], true + } + end := 0 + for end < len(s) && isBareKeyChar(s[end]) { + end++ + } + if end == 0 { + return "", "", false + } + return s[:end], s[end:], true +} + +func isBareKeyChar(c byte) bool { + return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' || c == '-' || c == '_' +} + func isBlankOrComment(line string) bool { s := strings.TrimSpace(line) return s == "" || strings.HasPrefix(s, "#") diff --git a/libs/aitools/mcp/agentconfig/toml_test.go b/libs/aitools/mcp/agentconfig/toml_test.go index c9701adee22..6bed3f36b7a 100644 --- a/libs/aitools/mcp/agentconfig/toml_test.go +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -75,7 +75,7 @@ func TestTOMLRefusesServerWithoutOwnTables(t *testing.T) { _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) require.Error(t, err) - assert.ErrorContains(t, err, path) + assert.ErrorContains(t, err, filepath.ToSlash(path)) assert.ErrorContains(t, err, testName) assert.Equal(t, tc.data, string(readFile(t, path))) @@ -133,3 +133,157 @@ func TestTOMLEditOnlyTouchesServerBlockLines(t *testing.T) { _, ok = tomlTableKey(`key = "[x]"`) assert.False(t, ok) } + +// blockFor is the table block Upsert writes for testName with the given agent's args. +func blockFor(agent string) string { + return "[mcp_servers." + testName + "]\n" + + `args = ["aitools", "mcp", "serve", "--profile", "dogfood", "--service", "system.ai.genie_one_mcp", "--agent", "` + agent + `"]` + "\n" + + `command = "` + testCmd + `"` + "\n" +} + +// requireTidyTOML fails when the layout has a double blank line, a blank first +// line, or a blank last line that the file did not have. +func requireTidyTOML(t *testing.T, got string) { + t.Helper() + assert.NotContains(t, got, "\n\n\n", "no double blank line") + assert.False(t, strings.HasPrefix(got, "\n"), "no blank first line") +} + +// Env sub-tables are written, changed, dropped and removed with the server. +func TestTOMLEnvTablesLayout(t *testing.T) { + other := "[mcp_servers.other]\ncommand = \"npx\"\n" + envTable := "[mcp_servers." + testName + ".env]\nTOKEN = \"one\"\n" + + t.Run("add with env", func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(other), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"TOKEN": "one"}, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + got := string(readFile(t, path)) + requireTidyTOML(t, got) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, map[string]any{"TOKEN": "one"}, entries[testName]["env"]) + assert.Contains(t, entries, "other") + }) + + t.Run("change env", func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(blockFor("test")+"\n"+envTable+"\n"+other), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"TOKEN": "two"}, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + got := string(readFile(t, path)) + requireTidyTOML(t, got) + assert.Contains(t, got, `TOKEN = "two"`) + assert.NotContains(t, got, `"one"`) + assert.True(t, strings.HasSuffix(got, "\n"+other), got) + }) + + t.Run("drop env", func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(blockFor("test")+"\n"+envTable+"\n"+other), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + got := string(readFile(t, path)) + requireTidyTOML(t, got) + assert.NotContains(t, got, "TOKEN") + assert.Equal(t, blockFor("other")+"\n"+other, got) + }) + + t.Run("remove server with env", func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(blockFor("test")+"\n"+envTable+"\n"+other), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, other, string(readFile(t, path))) + }) + + t.Run("remove first server", func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(blockFor("test")+"\n"+other), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, other, string(readFile(t, path))) + }) +} + +// A table name in quotes can contain dots, and a server named like its prefix +// is a different server. Neither is edited when the other one is. +func TestTOMLQuotedDottedNameIsAnotherServer(t *testing.T) { + before := "[mcp_servers.\"genie.x\"]\ncommand = \"npx\"\n" + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.False(t, removed) + assert.Equal(t, before, string(readFile(t, path))) + + _, status, err := Upsert(t.Context(), a, "genie", testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Equal(t, before+"\n"+strings.Replace(testBlock, testName, "genie", 1), string(readFile(t, path))) +} + +// A line that looks like a table header inside a multi-line string is text, not +// a table, so it is not edited. +func TestTOMLHeaderLikeLineInMultilineString(t *testing.T) { + notes := "[mcp_servers.notes]\ntext = \"\"\"\n[mcp_servers.genie]\ncommand = \"not a header\"\n\"\"\"\n" + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(notes), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, "genie", testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Equal(t, notes+"\n"+strings.Replace(testBlock, testName, "genie", 1), string(readFile(t, path))) + + // Removing our server leaves the string as it was. + removed, err := Remove(t.Context(), a, "genie") + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, notes, string(readFile(t, path))) +} + +// Removing a table between two others keeps one blank line between them when +// the table had a blank line on either side of it. +func TestTOMLRemoveKeepsOneSeparator(t *testing.T) { + other := "[mcp_servers.other]\ncommand = \"npx\"\n" + for _, tc := range []struct { + name string + before string + }{ + {"blank before", "[mcp_servers.first]\ncommand = \"a\"\n\n" + blockFor("test") + other}, + {"blank after", "[mcp_servers.first]\ncommand = \"a\"\n" + blockFor("test") + "\n" + other}, + {"blank both sides", "[mcp_servers.first]\ncommand = \"a\"\n\n" + blockFor("test") + "\n" + other}, + } { + t.Run(tc.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(tc.before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + got := string(readFile(t, path)) + requireTidyTOML(t, got) + assert.Equal(t, "[mcp_servers.first]\ncommand = \"a\"\n\n"+other, got) + }) + } +} diff --git a/libs/aitools/mcp/agentconfig/unmanaged.go b/libs/aitools/mcp/agentconfig/unmanaged.go index b898caa1c14..a4aad7e6ffe 100644 --- a/libs/aitools/mcp/agentconfig/unmanaged.go +++ b/libs/aitools/mcp/agentconfig/unmanaged.go @@ -8,8 +8,8 @@ import ( // IsUnmanaged reports whether entry connects to Databricks managed MCP servers // without having been written by this CLI: the `ug mcp-proxy` bridge, or a -// direct /ai-gateway/mcp-services/ URL. Such entries are listed and never -// modified. +// direct /ai-gateway/mcp-services/ URL. Such entries are listed by `list`, and +// Upsert replaces one only with force. func IsUnmanaged(entry map[string]any) bool { if argv, ok := mcpconfig.Argv(entry); ok && len(argv) > 1 && mcpconfig.IsBinary(argv[0], "ug", "ug.exe") && argv[1] == "mcp-proxy" { diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 86a81471e06..06cfa1b7ac4 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -48,28 +48,34 @@ type document interface { entry(root []string, name string) (map[string]any, bool, error) // entries returns the object-valued members under root, keyed by name. entries(root []string) (map[string]map[string]any, error) - // set stores entry as name under root, creating missing levels. + // set stores entry as name under root, replacing an existing entry and + // creating missing levels. set(root []string, name string, entry map[string]any) error + // update changes the launch keys of the existing entry name under root to + // those of entry. Other keys of the entry keep their bytes where the format + // allows it. + update(root []string, name string, entry map[string]any) error // remove deletes name under root. It is a no-op when name is absent. remove(root []string, name string) error // bytes serializes the document. bytes() ([]byte, error) } -// builderKeys are the entry keys the agent builders write. Upsert overwrites -// them. Any other key was added by the user (for example disabled or enabled) -// and is kept when an entry we wrote is updated. -var builderKeys = []string{"command", "args", "env", "environment", "type", "tools"} +// launchKeys are the entry keys that describe how the server starts. Upsert sets +// them on an update, and an entry whose launch keys match is not rewritten. Any +// other key is the user's after the entry is first added: a default that the +// builder writes (Copilot's tools list) is not written again, and a key the user +// added (disabled, or a tools table) is kept. +var launchKeys = []string{"command", "args", "env", "environment", "type"} // Upsert writes a stdio server name for agent a, which runs cmd with args and -// env. An entry this CLI wrote is updated in its builder keys, and keys the user -// added to it are kept. A foreign entry is replaced only when force is set. The -// file is left alone when the entry already matches. cmd must be the databricks -// binary, or the entry could never be recognized as ours. It returns the config -// file path. +// env. An entry this CLI wrote gets its launch keys updated, and the other keys +// are kept. A foreign entry is replaced only when force is set. The file is left +// alone when the entry already matches. cmd must be the databricks binary, or +// the entry could never be recognized as ours. It returns the config file path. func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []string, env map[string]string, force bool) (string, Status, error) { - if !mcpconfig.ValidServerName(name) { - return "", "", fmt.Errorf("server name %q must contain only letters, digits, '-' and '_'", name) + if err := checkName(name); err != nil { + return "", "", err } spec, err := specOf(a) if err != nil { @@ -80,7 +86,7 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err != nil { return "", "", err } - if !mcpconfig.IsBinary(cmd, "databricks", "databricks.exe") { + if !mcpconfig.IsDatabricksBinary(cmd) { return "", "", fmt.Errorf("server %q: command %q must be the databricks binary, named databricks or databricks.exe", name, cmd) } if !mcpconfig.IsDatabricksEntry(ours) { @@ -99,45 +105,44 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err != nil { return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } - if found { - if mcpconfig.IsDatabricksEntry(current) { - same, err := sameEntry(builderKeysOf(current), builderKeysOf(want)) - if err != nil { - return path, "", err - } - if same { - return path, StatusUnchanged, nil - } - want = keepUserKeys(current, want) - } else { - if !force { - return path, "", fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) - } - same, err := sameEntry(current, want) - if err != nil { - return path, "", err - } - if same { - return path, StatusUnchanged, nil - } + + var status Status + switch { + case !found: + status = StatusAdded + err = doc.set(root, name, want) + case !mcpconfig.IsDatabricksEntry(current) && !force: + return path, "", fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) + case !mcpconfig.IsDatabricksEntry(current): + status = StatusUpdated + err = doc.set(root, name, want) + default: + same, cmpErr := sameEntry(launchKeysOf(current), launchKeysOf(want)) + if cmpErr != nil { + return path, "", cmpErr + } + if same { + return path, StatusUnchanged, nil } + status = StatusUpdated + err = doc.update(root, name, keepUserKeys(current, want)) } - if err := doc.set(root, name, want); err != nil { + if err != nil { return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } if err := save(path, doc); err != nil { return path, "", err } - if found { - return path, StatusUpdated, nil - } - return path, StatusAdded, nil + return path, status, nil } // Remove deletes server name from agent a's config. It reports false, without // writing, when there is no such entry, and refuses an entry this CLI did not // write. func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { + if err := checkName(name); err != nil { + return false, err + } spec, err := specOf(a) if err != nil { return false, err @@ -192,6 +197,15 @@ func Read(ctx context.Context, a *agents.Agent) (map[string]map[string]any, erro return entries, nil } +// checkName refuses a name that no agent would accept. Upsert and Remove both +// check it, so a name that cannot be written is never looked up either. +func checkName(name string) error { + if !mcpconfig.ValidServerName(name) { + return fmt.Errorf("server name %q must contain only letters, digits, '-' and '_'", name) + } + return nil +} + func specOf(a *agents.Agent) (*mcpconfig.Spec, error) { if a.MCP == nil { return nil, fmt.Errorf("agent %s does not take MCP servers", a.DisplayName) @@ -225,7 +239,7 @@ func load(spec *mcpconfig.Spec, path string) (document, error) { } // save writes doc to path atomically. The file keeps its mode; a new file gets -// 0600 because agent configs can hold auth state. +// 0600 and a new directory 0700, because agent configs can hold auth state. func save(path string, doc document) error { data, err := doc.bytes() if err != nil { @@ -250,7 +264,7 @@ func save(path string, doc document) error { default: return fmt.Errorf("resolve %s: %w", filepath.ToSlash(path), err) } - if err := atomicfile.Write(target, data, perm, atomicfile.MkDir(0o755)); err != nil { + if err := atomicfile.Write(target, data, perm, atomicfile.MkDir(0o700)); err != nil { return fmt.Errorf("write %s: %w", filepath.ToSlash(path), err) } return nil @@ -258,10 +272,8 @@ func save(path string, doc document) error { // stripBOM removes a leading UTF-8 byte order mark, reporting whether there was one. func stripBOM(data []byte) ([]byte, bool) { - if bytes.HasPrefix(data, utf8BOM) { - return data[len(utf8BOM):], true - } - return data, false + rest, ok := bytes.CutPrefix(data, utf8BOM) + return rest, ok } // lineEnding returns CRLF when data uses it, and LF otherwise. @@ -280,10 +292,10 @@ func withBOM(data []byte, bom bool) []byte { return append(append([]byte{}, utf8BOM...), data...) } -// builderKeysOf returns the keys of entry that the agent builders write. -func builderKeysOf(entry map[string]any) map[string]any { +// launchKeysOf returns the launch keys of entry. +func launchKeysOf(entry map[string]any) map[string]any { out := map[string]any{} - for _, key := range builderKeys { + for _, key := range launchKeys { if v, ok := entry[key]; ok { out[key] = v } @@ -291,11 +303,12 @@ func builderKeysOf(entry map[string]any) map[string]any { return out } -// keepUserKeys returns current with its builder keys set to those of want. The -// other keys of current are kept. +// keepUserKeys returns current with its launch keys set to those of want, and +// removes a launch key that want lacks. The other keys of current are kept, and +// a key that want has but current lacks is not added. func keepUserKeys(current, want map[string]any) map[string]any { out := maps.Clone(current) - for _, key := range builderKeys { + for _, key := range launchKeys { if v, ok := want[key]; ok { out[key] = v } else { @@ -325,8 +338,16 @@ func normalize(entry map[string]any) (map[string]any, error) { return nil, err } var out map[string]any - if err := json.Unmarshal(raw, &out); err != nil { + if err := decodeJSON(raw, &out); err != nil { return nil, err } return out, nil } + +// decodeJSON decodes data into v. Numbers stay as their text, so a value read +// from a file is not rounded on the way. +func decodeJSON(data []byte, v any) error { + dec := json.NewDecoder(bytes.NewReader(data)) + dec.UseNumber() + return dec.Decode(v) +} diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index ef6ac8fb6e5..348667d8182 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "testing" "github.com/BurntSushi/toml" @@ -315,6 +316,9 @@ func TestUpsertKeepsFileModeAndCreatesPrivateFile(t *testing.T) { info, err = os.Stat(created) require.NoError(t, err) assert.Equal(t, os.FileMode(0o600), info.Mode().Perm()) + dirInfo, err := os.Stat(filepath.Dir(created)) + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o700), dirInfo.Mode().Perm()) } func TestUpsertWritesThroughSymlink(t *testing.T) { @@ -467,3 +471,170 @@ func TestUpsertKeepsUserKeysTOML(t *testing.T) { assert.Equal(t, testArgs("other"), toStrings(t, entries[testName]["args"])) assert.Equal(t, false, entries[testName]["enabled"]) } + +// Copilot narrows the tools of an entry we wrote after the first add. The list +// is the user's from then on: a re-add with the same launch keys leaves the file +// alone, and a changed argument keeps the list. +func TestUpsertKeepsToolsNarrowedByUser(t *testing.T) { + path := filepath.Join(t.TempDir(), "mcp-config.json") + entry := testEntry(testCmd, testArgs("test"), nil) + entry["tools"] = []string{"search"} + data, err := json.Marshal(map[string]any{"mcpServers": map[string]any{testName: entry}}) + require.NoError(t, err) + writeFile(t, path, data, 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) + assert.Equal(t, data, readFile(t, path)) + + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, []any{"search"}, entries[testName]["tools"]) +} + +// An update writes only the launch keys that changed. The other members keep +// their bytes: comments, key order, number text and the user's keys. Only the +// command and args are replaced, in place. +func TestUpsertPatchesOnlyChangedKeysJSON(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + before := `{ + "mcpServers": { + "` + testName + `": { + "disabled": false, + // written by hand + "timeout": 1.50, + "command": "/old/databricks", + "args": ["aitools", "mcp", "serve", "--profile", "old"], + "big": 12345678901234567890 + }, + "other": {"command": "npx"} + } +} +` + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, `{ + "mcpServers": { + "`+testName+`": { + "disabled": false, + // written by hand + "timeout": 1.50, + "command": "/opt/databricks/bin/databricks", + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "dogfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "test" + ], + "big": 12345678901234567890 + }, + "other": {"command": "npx"} + } +} +`, string(readFile(t, path))) +} + +// A launch key that the file lacks is added on update, and one that the entry +// no longer has is removed. Either way the file stays strict JSON, whether the +// key was the last member or not. +func TestUpsertUpdateAddsAndRemovesLaunchKeys(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + writeFile(t, path, []byte(`{"mcpServers":{"`+testName+`":{"command":"`+testCmd+`","args":["aitools","mcp","serve","--agent","test"]}}}`), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"B": "2"}, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, map[string]any{"B": "2"}, entries[testName]["env"]) + requireStrictJSON(t, string(readFile(t, path))) + + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err = Read(t.Context(), a) + require.NoError(t, err) + assert.NotContains(t, entries[testName], "env") + requireStrictJSON(t, string(readFile(t, path))) +} + +// Keys of an entry we wrote that the user keeps in TOML are kept on update, +// including sub-tables such as [mcp_servers..tools.]. +func TestTOMLUpdateKeepsUserTables(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + data := testBlock + "\n[mcp_servers." + testName + ".tools.search]\napproval_mode = \"approve\"\n" + writeFile(t, path, []byte(data), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, map[string]any{"search": map[string]any{"approval_mode": "approve"}}, entries[testName]["tools"]) +} + +// Duplicate member names on the path to the server are refused. Agents read the +// last of two equal names while an edit would change the first, so the file is +// left alone. +func TestUpsertAndRemoveRefuseDuplicateKeysJSON(t *testing.T) { + argsJSON, err := json.Marshal(testArgs("test")) + require.NoError(t, err) + ours := `{"command": "` + testCmd + `", "args": ` + string(argsJSON) + `}` + for _, data := range []string{ + `{"mcpServers": {}, "mcpServers": {"` + testName + `": ` + ours + `}}`, + `{"mcpServers": {"` + testName + `": ` + ours + `, "` + testName + `": {"command": "/usr/bin/other", "args": []}}}`, + `{"mcpServers": {"` + testName + `": {"command": "` + testCmd + `", "args": ` + string(argsJSON) + `, "command": "/usr/bin/other"}}}`, + } { + t.Run(data, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + writeFile(t, path, []byte(data), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) + require.ErrorIs(t, err, ErrConfigInvalid) + _, err = Remove(t.Context(), a, testName) + require.ErrorIs(t, err, ErrConfigInvalid) + assert.Equal(t, data, string(readFile(t, path))) + }) + } +} + +// A file whose top-level value is not an object names the problem once. +func TestUpsertNonObjectTopLevelNamesProblemOnce(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + writeFile(t, path, []byte(`[]`), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.ErrorIs(t, err, ErrConfigInvalid) + assert.Equal(t, 1, strings.Count(err.Error(), ErrConfigInvalid.Error()), err.Error()) +} + +// Remove checks the name the way Upsert does, so an invalid name is an error +// and nothing is read as a match. +func TestRemoveRefusesInvalidServerName(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + data := `{"mcpServers":{"` + testName + `":{"command":"` + testCmd + `","args":[]}}}` + writeFile(t, path, []byte(data), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, err := Remove(t.Context(), a, "genie.one") + assert.ErrorContains(t, err, "must contain only letters, digits, '-' and '_'") + assert.Equal(t, data, string(readFile(t, path))) +} diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index ace123ce3fd..9eb80bab97d 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -5,7 +5,6 @@ package mcpconfig import ( "context" - "regexp" "slices" "strings" ) @@ -75,10 +74,16 @@ func IsDatabricksEntry(entry map[string]any) bool { return false } args := argv[1:] - return IsBinary(argv[0], "databricks", "databricks.exe") && + return IsDatabricksBinary(argv[0]) && len(args) >= 3 && args[0] == "aitools" && args[1] == "mcp" && args[2] == "serve" } +// IsDatabricksBinary reports whether cmd names the databricks binary, as a bare +// name or a path. +func IsDatabricksBinary(cmd string) bool { + return IsBinary(cmd, "databricks", "databricks.exe") +} + // IsBinary reports whether cmd, a bare name or a path, has one of the given // base names, ignoring case as Windows does. Both separators are accepted so // that a Windows path is recognized on any host. @@ -89,11 +94,20 @@ func IsBinary(cmd string, names ...string) bool { }) } -// serverNamePattern is the strictest set of server names the agents accept, so a -// name that matches it is valid for every agent. -var serverNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) - -// ValidServerName reports whether name may be used as an MCP server name. +// ValidServerName reports whether name may be used as an MCP server name: one or +// more ASCII letters, digits, '-' or '_'. That is the strictest set of names the +// agents accept, so a name that passes is valid for every agent. func ValidServerName(name string) bool { - return serverNamePattern.MatchString(name) + if name == "" { + return false + } + for i := range len(name) { + c := name[i] + switch { + case 'a' <= c && c <= 'z', 'A' <= c && c <= 'Z', '0' <= c && c <= '9', c == '-', c == '_': + default: + return false + } + } + return true } diff --git a/libs/aitools/mcpconfig/spec_test.go b/libs/aitools/mcpconfig/spec_test.go index 50369b59f90..c8483918cde 100644 --- a/libs/aitools/mcpconfig/spec_test.go +++ b/libs/aitools/mcpconfig/spec_test.go @@ -69,7 +69,7 @@ func TestIsDatabricksEntry(t *testing.T) { func TestValidServerName(t *testing.T) { assert.True(t, ValidServerName("genie_one-mcp1")) - for _, name := range []string{"", "a.b", "a b", "a/b", "é"} { + for _, name := range []string{"", "a.b", "a b", "a/b", "é", "genie\n"} { assert.False(t, ValidServerName(name), name) } } diff --git a/libs/localenv/merge.go b/libs/localenv/merge.go index ab784564206..95828bcec4d 100644 --- a/libs/localenv/merge.go +++ b/libs/localenv/merge.go @@ -89,7 +89,7 @@ func planDBConnect(target []byte, c Constraints, opts MergeOptions) dbconnectPla return dbconnectPlan{} } // Mirror MergeManaged's own preprocessing so the same lines are inspected. - protected, _, err := protectMultilineStrings(strings.ReplaceAll(string(target), "\r\n", "\n")) + protected, _, err := ProtectMultilineStrings(strings.ReplaceAll(string(target), "\r\n", "\n")) if err != nil { return dbconnectPlan{} } @@ -303,7 +303,7 @@ func transformLines(target []byte, fn func([]string) ([]string, error)) ([]byte, s = strings.ReplaceAll(s, "\r\n", "\n") } - protected, restore, err := protectMultilineStrings(s) + protected, restore, err := ProtectMultilineStrings(s) if err != nil { return nil, err } @@ -1144,12 +1144,12 @@ func markerAttachedToToolUv(lines []string, start int) bool { // [tool.uv] table, capturing its leading whitespace. var constraintDepsRe = regexp.MustCompile(`^\s*constraint-dependencies\s*=`) -// protectMultilineStrings replaces TOML multi-line strings with unique ordinary +// ProtectMultilineStrings replaces TOML multi-line strings with unique ordinary // string values while the line-based merge runs. This prevents string contents // that resemble tables, assignments, brackets, comments, or managed markers // from affecting the merge. The returned restore function puts each original // string back byte-for-byte after the managed edits are complete. -func protectMultilineStrings(s string) (protected string, restore func(string) string, err error) { +func ProtectMultilineStrings(s string) (protected string, restore func(string) string, err error) { type replacement struct { placeholder string original string From ad586944c06d8d548e694465832b1d8e508ce51d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:17:45 +0000 Subject: [PATCH 05/21] Fix agent MCP config writer review findings (pass 4) Update only what the CLI owns in an existing entry. Launch keys are the ones the agent's builder writes, so a key the builder never writes (type, for most agents) is kept. In env tables only DATABRICKS_CONFIG_FILE is the CLI's, and user variables are kept. Compare TOML edits as TOML text, so inf and nan in an unrelated table no longer block an edit. Add blank-line layout that keeps one blank line before a new server and restores the file's own bytes when the server is removed again. Refuse OpenCode when both opencode.json and opencode.jsonc exist, and treat a dangling opencode.jsonc link as the file to edit, so the write is refused. Keep CRLF on the newlines hujson writes for a new member. Collapse the TOML set/update/remove wrappers into one put, and fix the test comments and assertions flagged in review. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 59 ++++-- libs/aitools/agents/agents_test.go | 50 ++++- libs/aitools/mcp/agentconfig/json.go | 146 ++++++++----- libs/aitools/mcp/agentconfig/json_test.go | 73 ++++--- libs/aitools/mcp/agentconfig/toml.go | 150 ++++++++------ libs/aitools/mcp/agentconfig/toml_test.go | 111 +++++++--- libs/aitools/mcp/agentconfig/writer.go | 132 ++++++++---- libs/aitools/mcp/agentconfig/writer_test.go | 214 ++++++++++++++++---- 8 files changed, 673 insertions(+), 262 deletions(-) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 9246b00c5a0..a75d07d2a63 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -436,7 +436,7 @@ func gooseConfigDir(ctx context.Context) (string, error) { // claudeMCPPath returns Claude Code's global config file, which holds user-scope // mcpServers next to auth state. It sits in the home directory, not in ~/.claude; // CLAUDE_CONFIG_DIR moves it to /.claude.json. -// https://code.claude.com/docs/en/env-vars +// See https://code.claude.com/docs/en/env-vars. func claudeMCPPath(ctx context.Context) (string, error) { if dir := env.Get(ctx, "CLAUDE_CONFIG_DIR"); dir != "" { return filepath.Join(dir, ".claude.json"), nil @@ -445,6 +445,7 @@ func claudeMCPPath(ctx context.Context) (string, error) { } // claudeMCPEntry matches `claude mcp add`, which always writes an env map, even an empty one. +// See https://code.claude.com/docs/en/mcp. func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { if len(vars) == 0 { vars = map[string]string{} @@ -453,7 +454,7 @@ func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[strin } // codexMCPPath returns Codex's config.toml, which honors CODEX_HOME. -// https://learn.chatgpt.com/docs/config-file/config-advanced +// See https://learn.chatgpt.com/docs/config-file/config-advanced. func codexMCPPath(ctx context.Context) (string, error) { if home := env.Get(ctx, "CODEX_HOME"); home != "" { return filepath.Join(home, "config.toml"), nil @@ -461,32 +462,56 @@ func codexMCPPath(ctx context.Context) (string, error) { return homeSubdir(".codex", "config.toml")(ctx) } -// openCodeMCPPath returns OpenCode's config file. OpenCode reads opencode.json or -// opencode.jsonc, and the docs do not say which wins when both exist, so an -// existing opencode.jsonc is used rather than creating a second file beside it. -// The file may contain comments; a file we cannot parse is refused, never rewritten. -// https://opencode.ai/docs/config/ +// openCodeMCPPath returns OpenCode's config file: opencode.jsonc when it exists, +// and opencode.json otherwise. A symbolic link counts as existing, so a dangling +// link is chosen and the write refuses it. When both files exist, the CLI refuses +// to choose, since it cannot tell which one OpenCode reads. The file may contain +// comments; a file we cannot parse is refused, never rewritten. +// See https://opencode.ai/docs/config/. func openCodeMCPPath(ctx context.Context) (string, error) { dir, err := openCodeConfigDir(ctx) if err != nil { return "", err } - jsonc := filepath.Join(dir, "opencode.jsonc") - _, err = os.Stat(jsonc) + jsonPath := filepath.Join(dir, "opencode.json") + jsoncPath := filepath.Join(dir, "opencode.jsonc") + hasJSON, err := linkExists(jsonPath) + if err != nil { + return "", err + } + hasJSONC, err := linkExists(jsoncPath) + if err != nil { + return "", err + } + switch { + case hasJSON && hasJSONC: + return "", fmt.Errorf("%s and %s both exist; remove one, since this CLI cannot tell which one OpenCode reads", + filepath.ToSlash(jsonPath), filepath.ToSlash(jsoncPath)) + case hasJSONC: + return jsoncPath, nil + default: + return jsonPath, nil + } +} + +// linkExists reports whether path exists. It uses Lstat, so a symbolic link counts +// even when its target is missing. +func linkExists(path string) (bool, error) { + _, err := os.Lstat(path) switch { case err == nil: - return jsonc, nil + return true, nil case errors.Is(err, fs.ErrNotExist): - return filepath.Join(dir, "opencode.json"), nil + return false, nil default: - return "", fmt.Errorf("cannot read %s: %w", filepath.ToSlash(jsonc), err) + return false, fmt.Errorf("cannot read %s: %w", filepath.ToSlash(path), err) } } // openCodeMCPEntry follows the current OpenCode schema: servers live under // mcp.servers, the env map is "environment", and there is no "enabled" key // (the schema forbids extra keys, and an absent key means enabled). -// https://opencode.ai/docs/mcp-servers/ +// See https://opencode.ai/docs/mcp-servers/. func openCodeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { entry := map[string]any{"type": localServerType, "command": append([]string{cmd}, args...)} if len(vars) > 0 { @@ -496,7 +521,7 @@ func openCodeMCPEntry(cmd string, args []string, vars map[string]string) map[str } // copilotMCPPath returns Copilot CLI's user config. COPILOT_HOME replaces ~/.copilot. -// https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference +// See https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference. func copilotMCPPath(ctx context.Context) (string, error) { if home := env.Get(ctx, "COPILOT_HOME"); home != "" { return filepath.Join(home, "mcp-config.json"), nil @@ -505,7 +530,7 @@ func copilotMCPPath(ctx context.Context) (string, error) { } // copilotMCPEntry is the stdio entry Copilot CLI documents, with all tools enabled. -// https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/add-mcp-servers +// See https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/add-mcp-servers. func copilotMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { entry := stdioEntry(cmd, args, vars) entry["type"] = localServerType @@ -515,7 +540,7 @@ func copilotMCPEntry(cmd string, args []string, vars map[string]string) map[stri // geminiMCPPath returns Gemini CLI's user settings, which hold mcpServers. The // settings directory follows geminiConfigDir, which honors GEMINI_CLI_HOME. -// https://geminicli.com/docs/tools/mcp-server/ +// See https://geminicli.com/docs/tools/mcp-server/. func geminiMCPPath(ctx context.Context) (string, error) { dir, err := geminiConfigDir(ctx) if err != nil { @@ -526,7 +551,7 @@ func geminiMCPPath(ctx context.Context) (string, error) { // antigravityMCPPath is the global MCP file in Antigravity's docs, for both the // CLI and the IDE. It is not the skills directory (~/.gemini/antigravity). -// https://antigravity.google/docs/mcp +// See https://antigravity.google/docs/mcp. func antigravityMCPPath(ctx context.Context) (string, error) { return homeSubdir(".gemini", "config", "mcp_config.json")(ctx) } diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index ac418529afa..4fc4e43026c 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -105,8 +105,9 @@ func TestMCPPaths(t *testing.T) { } } -// OpenCode reads opencode.json or opencode.jsonc. An existing .jsonc is the -// file to edit, so a second file is not created beside it. +// TestOpenCodeMCPPathPrefersJSONC checks that OpenCode reads opencode.json or +// opencode.jsonc, and that an existing .jsonc is the file to edit, so a second +// file is not created beside it. func TestOpenCodeMCPPathPrefersJSONC(t *testing.T) { ctx, home := clearMCPOverrides(t) dir := filepath.Join(home, ".config", "opencode") @@ -165,20 +166,51 @@ func TestSkillsOnlyNamesMatchesRegistry(t *testing.T) { } } -// A config file that cannot be inspected is an error, not a missing file. Creating -// opencode.json beside it would leave the user's file unread. +// TestOpenCodeMCPPathReturnsStatError checks that a config path that cannot be +// inspected is an error, not a missing file. Creating opencode.json beside it +// would leave the user's file unread. func TestOpenCodeMCPPathReturnsStatError(t *testing.T) { if runtime.GOOS == "windows" { - t.Skip("creating symlinks needs privileges on Windows") + t.Skip("the blocking file has a different error on Windows") } + ctx, home := clearMCPOverrides(t) + // A regular file where the OpenCode directory should be makes the lookup fail with ENOTDIR. + require.NoError(t, os.MkdirAll(filepath.Join(home, ".config"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(home, ".config", "opencode"), []byte("x"), 0o600)) + + _, err := ByName(NameOpenCode).MCP.Path(ctx) + require.Error(t, err) +} + +// TestOpenCodeMCPPathRefusesBothFiles checks that the CLI does not pick between +// opencode.json and opencode.jsonc when both exist, and names both files. +func TestOpenCodeMCPPathRefusesBothFiles(t *testing.T) { ctx, home := clearMCPOverrides(t) dir := filepath.Join(home, ".config", "opencode") require.NoError(t, os.MkdirAll(dir, 0o755)) - jsonc := filepath.Join(dir, "opencode.jsonc") - require.NoError(t, os.Symlink(jsonc, jsonc)) + jsonPath := filepath.ToSlash(filepath.Join(dir, "opencode.json")) + jsoncPath := filepath.ToSlash(filepath.Join(dir, "opencode.jsonc")) + require.NoError(t, os.WriteFile(jsonPath, []byte("{}"), 0o600)) + require.NoError(t, os.WriteFile(jsoncPath, []byte("{}"), 0o600)) _, err := ByName(NameOpenCode).MCP.Path(ctx) require.Error(t, err) - _, err = os.Stat(filepath.Join(dir, "opencode.json")) - assert.ErrorIs(t, err, os.ErrNotExist) + assert.ErrorContains(t, err, jsonPath+" and "+jsoncPath) +} + +// TestOpenCodeMCPPathDanglingJSONCLink checks that a dangling opencode.jsonc link +// counts as the file, so the write refuses it instead of creating opencode.json. +func TestOpenCodeMCPPathDanglingJSONCLink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("creating symlinks needs privileges on Windows") + } + ctx, home := clearMCPOverrides(t) + dir := filepath.Join(home, ".config", "opencode") + require.NoError(t, os.MkdirAll(dir, 0o755)) + jsonc := filepath.Join(dir, "opencode.jsonc") + require.NoError(t, os.Symlink(filepath.Join(dir, "missing.jsonc"), jsonc)) + + path, err := ByName(NameOpenCode).MCP.Path(ctx) + require.NoError(t, err) + assert.Equal(t, jsonc, path) } diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index 59dc1f1bfd5..0e680b41f0d 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "fmt" + "maps" + "slices" "strings" "github.com/tailscale/hujson" @@ -29,7 +31,7 @@ type jsonDoc struct { } func parseJSON(data []byte) (document, error) { - data, bom := stripBOM(data) + data, bom := bytes.CutPrefix(data, utf8BOM) if len(bytes.TrimSpace(data)) == 0 { // A missing or blank file is an empty object, written with a trailing newline. data = []byte("{}\n") @@ -208,75 +210,96 @@ func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { return nil } -// update changes the launch keys of the server name under root to those of -// entry. A launch key that entry lacks is removed, and one that is missing from -// the file is added. A launch key whose value is unchanged is not touched, and -// the other keys of the server keep their bytes. name must exist. -func (d *jsonDoc) update(root []string, name string, entry map[string]any) error { +// put writes entry as server name under root. A nil entry removes it. With keys, +// an existing server changes only those keys, and its other keys keep their bytes. +func (d *jsonDoc) put(root []string, name string, entry map[string]any, keys []string) error { + switch { + case entry == nil: + return d.remove(root, name) + case keys == nil: + return d.set(root, name, entry) + default: + return d.update(root, name, entry, keys) + } +} + +// update changes the keys of the server name under root that are listed in keys. +// A key that entry lacks is removed, and one that is missing from the file is +// added. Members that match keep their bytes, and so do the other keys of the +// server. name must exist. +func (d *jsonDoc) update(root []string, name string, entry map[string]any, keys []string) error { current, _, err := d.entry(root, name) if err != nil { return err } - depth := len(root) + 2 - var ops []patch - var added, removed []string - for _, key := range launchKeys { - want, inEntry := entry[key] + server := slices.Concat(root, []string{name}) + for _, key := range keys { have, inFile := current[key] - switch { - case inEntry && !inFile: - value, err := d.encodeValue(want, depth) - if err != nil { - return err - } - ops = append(ops, patch{op: opAdd, path: jsonPointer(append(root, name, key)...), value: value}) - added = append(added, key) - case inFile && !inEntry: - removed = append(removed, key) - case inEntry: - same, err := sameEntry(map[string]any{key: have}, map[string]any{key: want}) - if err != nil { - return err - } - if same { - continue - } - value, err := d.encodeValue(want, depth) - if err != nil { + want, inEntry := entry[key] + if err := d.sync(slices.Concat(server, []string{key}), have, inFile, want, inEntry, len(root)+2); err != nil { + return err + } + } + return nil +} + +// sync makes the member at path match the entry. It is removed when the entry +// lacks it, added when the file lacks it, and replaced when the values differ. +// Two objects are synced member by member, so the members that match keep their +// bytes. depth is the indentation depth of the member. +func (d *jsonDoc) sync(path []string, have any, inFile bool, want any, inWant bool, depth int) error { + parent, key := path[:len(path)-1], path[len(path)-1] + if !inWant { + return d.remove(parent, key) + } + haveObj, haveIsObj := have.(map[string]any) + wantObj, wantIsObj := want.(map[string]any) + if inFile && haveIsObj && wantIsObj { + for _, name := range unionKeys(haveObj, wantObj) { + hv, hin := haveObj[name] + wv, win := wantObj[name] + if err := d.sync(slices.Concat(path, []string{name}), hv, hin, wv, win, depth+1); err != nil { return err } - ops = append(ops, patch{op: opReplace, path: jsonPointer(append(root, name, key)...), value: value}) } + return nil } - if len(ops) > 0 { - if err := d.apply(ops); err != nil { + if inFile { + same, err := sameEntry(map[string]any{key: have}, map[string]any{key: want}) + if err != nil || same { return err } - } - for _, key := range removed { - if err := d.remove(append(root, name), key); err != nil { + value, err := d.encodeValue(want, depth) + if err != nil { return err } + return d.apply([]patch{{op: opReplace, path: jsonPointer(path...), value: value}}) } - server, err := d.server(root, name) + value, err := d.encodeValue(want, depth) if err != nil { return err } - for _, key := range added { - place(server, key, depth, true, d.nl) + if err := d.apply([]patch{{op: opAdd, path: jsonPointer(path...), value: value}}); err != nil { + return err + } + obj, err := d.object(parent) + if err != nil { + return err } + place(obj, key, depth, true, d.nl) return nil } -// server returns the object of the server name under root. The entry must exist -// and be an object. -func (d *jsonDoc) server(root []string, name string) (*hujson.Object, error) { - table, err := d.object(root) - if err != nil { - return nil, err +// unionKeys returns the keys of a and b, sorted and each once. +func unionKeys(a, b map[string]any) []string { + seen := map[string]bool{} + for k := range a { + seen[k] = true } - // entry decoded the member as an object, so its value is an object. - return findMember(table, name).Value.Value.(*hujson.Object), nil + for k := range b { + seen[k] = true + } + return slices.Sorted(maps.Keys(seen)) } // encodeValue encodes v for a member at depth, with the file's line endings. @@ -292,6 +315,7 @@ func (d *jsonDoc) encodeValue(v any, depth int) ([]byte, error) { // space, and a new member starts its own line at depth. Comments before the // member stay where they are. func place(obj *hujson.Object, name string, depth int, newMember bool, nl string) { + useLineEnding(obj, nl) m := findMember(obj, name) if m.Value.BeforeExtra == nil { m.Value.BeforeExtra = hujson.Extra(" ") @@ -301,6 +325,32 @@ func place(obj *hujson.Object, name string, depth int, newMember bool, nl string } } +// useLineEnding makes the newlines in the layout of obj CRLF in a CRLF file. hujson +// writes its own newlines when it adds a member, which would otherwise stay LF. +func useLineEnding(obj *hujson.Object, nl string) { + if nl != "\r\n" { + return + } + obj.AfterExtra = setNewlines(obj.AfterExtra, nl) + for i := range obj.Members { + m := &obj.Members[i] + m.Name.BeforeExtra = setNewlines(m.Name.BeforeExtra, nl) + m.Name.AfterExtra = setNewlines(m.Name.AfterExtra, nl) + m.Value.BeforeExtra = setNewlines(m.Value.BeforeExtra, nl) + m.Value.AfterExtra = setNewlines(m.Value.AfterExtra, nl) + } +} + +// setNewlines replaces each newline in extra with nl. An extra without a newline +// is returned as it is, since a nil and an empty extra mean different things. +func setNewlines(extra hujson.Extra, nl string) hujson.Extra { + if !bytes.Contains(extra, []byte("\n")) { + return extra + } + lf := bytes.ReplaceAll(extra, []byte("\r\n"), []byte("\n")) + return hujson.Extra(bytes.ReplaceAll(lf, []byte("\n"), []byte(nl))) +} + // relayout keeps the comments in extra and replaces the whitespace after the // last one with layout. A line comment ends at its newline, which layout provides. func relayout(extra hujson.Extra, layout string) hujson.Extra { diff --git a/libs/aitools/mcp/agentconfig/json_test.go b/libs/aitools/mcp/agentconfig/json_test.go index 534d781c1ba..d1254cc5568 100644 --- a/libs/aitools/mcp/agentconfig/json_test.go +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -42,7 +42,7 @@ func TestJSONKeepsCommentsTrailingCommasAndValues(t *testing.T) { ` entry := testEntry(testCmd, testArgs("test"), nil) got := jsonEdit(t, data, func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, testName, entry)) + require.NoError(t, d.put([]string{"mcpServers"}, testName, entry, nil)) }) assert.Contains(t, got, "// Agent settings\n") assert.Contains(t, got, `"theme": "dark", /* keep */`) @@ -52,7 +52,7 @@ func TestJSONKeepsCommentsTrailingCommasAndValues(t *testing.T) { // Removing the entry leaves the other members and the comments in place. back := jsonEdit(t, got, func(d document) { - require.NoError(t, d.remove([]string{"mcpServers"}, testName)) + require.NoError(t, d.put([]string{"mcpServers"}, testName, nil, nil)) }) assert.Contains(t, back, "// Agent settings\n") assert.Equal(t, semanticJSONC(t, data), semanticJSONC(t, back)) @@ -60,7 +60,7 @@ func TestJSONKeepsCommentsTrailingCommasAndValues(t *testing.T) { func TestJSONCreatesMissingLevels(t *testing.T) { got := jsonEdit(t, `{"theme": "dark"}`, func(d document) { - require.NoError(t, d.set([]string{"mcp", "servers"}, "x", map[string]any{"type": "local"})) + require.NoError(t, d.put([]string{"mcp", "servers"}, "x", map[string]any{"type": "local"}, nil)) }) assert.Equal(t, map[string]any{ "theme": "dark", @@ -70,19 +70,19 @@ func TestJSONCreatesMissingLevels(t *testing.T) { func TestJSONBOMIsKept(t *testing.T) { got := jsonEdit(t, "\xEF\xBB\xBF{\"a\": 1}\n", func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, testName, testEntry(testCmd, nil, nil))) + require.NoError(t, d.put([]string{"mcpServers"}, testName, testEntry(testCmd, nil, nil), nil)) }) assert.Equal(t, "\xEF\xBB\xBF", got[:3]) assert.Contains(t, got, `"a": 1`) assert.Contains(t, got, testName) } -// A new file gets two-space indentation and a trailing newline, and <, > and & -// are written as they are. +// TestJSONNewFileLayout checks that a new file gets two-space indentation and a +// trailing newline, and <, > and & are written as they are. func TestJSONNewFileLayout(t *testing.T) { entry := map[string]any{"command": "/a&c", "args": []string{"x"}} got := jsonEdit(t, "", func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, "x", entry)) + require.NoError(t, d.put([]string{"mcpServers"}, "x", entry, nil)) }) assert.Equal(t, `{ "mcpServers": { @@ -118,27 +118,31 @@ func requireStrictJSON(t *testing.T, data string) { require.NoError(t, json.Unmarshal([]byte(data), &v), data) } -// A comment after the last member is on that member's line. Adding a member -// after it keeps the comment there. +// TestJSONAddKeepsCommentOnPreviousMember checks that a comment after the last +// member stays on that member's line when a member is added after it, in LF and +// CRLF files. func TestJSONAddKeepsCommentOnPreviousMember(t *testing.T) { - data := "{\n \"mcpServers\": {\n \"other\": {\"command\": \"npx\"} // other server\n }\n}\n" - got := jsonEdit(t, data, func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) - }) - assert.Equal(t, "{\n \"mcpServers\": {\n \"other\": {\"command\": \"npx\"}, // other server\n \"x\": {\n \"command\": \"x\"\n }\n }\n}\n", got) + for _, nl := range []string{"\n", "\r\n"} { + data := strings.ReplaceAll("{\n \"mcpServers\": {\n \"other\": {\"command\": \"npx\"} // other server\n }\n}\n", "\n", nl) + got := jsonEdit(t, data, func(d document) { + require.NoError(t, d.put([]string{"mcpServers"}, "x", map[string]any{"command": "x"}, nil)) + }) + want := strings.ReplaceAll("{\n \"mcpServers\": {\n \"other\": {\"command\": \"npx\"}, // other server\n \"x\": {\n \"command\": \"x\"\n }\n }\n}\n", "\n", nl) + assert.Equal(t, want, got, "line ending %q", nl) + } } -// An empty table gets its member on its own line, and its closing brace on a -// line of its own. +// TestJSONAddToEmptyTable checks that an empty table gets its member on its own +// line, and its closing brace on a line of its own. func TestJSONAddToEmptyTable(t *testing.T) { got := jsonEdit(t, "{\n \"mcpServers\": {}\n}\n", func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) + require.NoError(t, d.put([]string{"mcpServers"}, "x", map[string]any{"command": "x"}, nil)) }) assert.Equal(t, "{\n \"mcpServers\": {\n \"x\": {\n \"command\": \"x\"\n }\n }\n}\n", got) } -// A file with only comments and an empty object keeps its comments. Only the -// layout of the object changes. +// TestJSONAddKeepsCommentsOfEmptyObject checks that a file with only comments and +// an empty object keeps its comments. Only the layout of the object changes. func TestJSONAddKeepsCommentsOfEmptyObject(t *testing.T) { for _, tc := range []struct { data string @@ -149,26 +153,28 @@ func TestJSONAddKeepsCommentsOfEmptyObject(t *testing.T) { } { t.Run(tc.data, func(t *testing.T) { got := jsonEdit(t, tc.data, func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) + require.NoError(t, d.put([]string{"mcpServers"}, "x", map[string]any{"command": "x"}, nil)) }) assert.Equal(t, tc.want, got) }) } } -// An empty object file gets one trailing newline, as the file had. +// TestJSONAddToEmptyObjectFile checks that an empty object file gets one trailing +// newline, as the file had. func TestJSONAddToEmptyObjectFile(t *testing.T) { want := "{\n \"mcpServers\": {\n \"x\": {\n \"command\": \"x\"\n }\n }\n}\n" for _, data := range []string{"{}\n", "", " \n"} { got := jsonEdit(t, data, func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x"})) + require.NoError(t, d.put([]string{"mcpServers"}, "x", map[string]any{"command": "x"}, nil)) }) assert.Equal(t, want, got, "input %q", data) } } -// Removing the last member keeps a strict file strict. The comma that stood -// after the previous member's space is not left behind. +// TestJSONRemoveLastKeepsStrictJSON checks that removing the last member keeps a +// strict file strict. The comma that stood after the previous member's space is +// not left behind. func TestJSONRemoveLastKeepsStrictJSON(t *testing.T) { for _, data := range []string{ "{\n \"mcpServers\": {\n \"a\": {} ,\n \"b\": {\"command\": \"x\"}\n }\n}\n", @@ -176,37 +182,40 @@ func TestJSONRemoveLastKeepsStrictJSON(t *testing.T) { "{\"mcpServers\": {\"a\": {} , \"b\": {\"command\": \"x\"}}}", } { got := jsonEdit(t, data, func(d document) { - require.NoError(t, d.remove([]string{"mcpServers"}, "b")) + require.NoError(t, d.put([]string{"mcpServers"}, "b", nil, nil)) }) requireStrictJSON(t, got) assert.Equal(t, map[string]any{"mcpServers": map[string]any{"a": map[string]any{}}}, semantic(t, mcpconfig.FormatJSON, []byte(got))) } } -// A file with a trailing comma keeps the trailing comma when its last member is removed. +// TestJSONRemoveLastKeepsTrailingComma checks that a file with a trailing comma +// keeps the trailing comma when its last member is removed. func TestJSONRemoveLastKeepsTrailingComma(t *testing.T) { got := jsonEdit(t, "{\"mcpServers\": {\"a\": {}, \"b\": {},}}", func(d document) { - require.NoError(t, d.remove([]string{"mcpServers"}, "b")) + require.NoError(t, d.put([]string{"mcpServers"}, "b", nil, nil)) }) assert.Equal(t, "{\"mcpServers\": {\"a\": {},}}", got) } -// A CRLF file gets CRLF in inserted text, so the file has one line ending. +// TestJSONKeepsCRLF checks that a CRLF file gets CRLF in inserted text, so the +// file has one line ending. func TestJSONKeepsCRLF(t *testing.T) { data := "{\r\n \"mcpServers\": {\r\n \"other\": {\"command\": \"npx\"}\r\n }\r\n}\r\n" got := jsonEdit(t, data, func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "x", "args": []string{"a"}})) + require.NoError(t, d.put([]string{"mcpServers"}, "x", map[string]any{"command": "x", "args": []string{"a"}}, nil)) }) assert.Equal(t, strings.Count(got, "\n"), strings.Count(got, "\r\n"), got) assert.Contains(t, got, "\"other\": {\"command\": \"npx\"},\r\n \"x\": {\r\n") got = jsonEdit(t, got, func(d document) { - require.NoError(t, d.remove([]string{"mcpServers"}, "x")) + require.NoError(t, d.put([]string{"mcpServers"}, "x", nil, nil)) }) assert.Equal(t, data, got) } -// Adding a member to a strict file keeps it strict, whatever its layout. +// TestJSONAddKeepsStrictJSON checks that adding a member to a strict file keeps it +// strict, whatever its layout. func TestJSONAddKeepsStrictJSON(t *testing.T) { for _, data := range []string{ "{}", @@ -216,7 +225,7 @@ func TestJSONAddKeepsStrictJSON(t *testing.T) { "{\"theme\": \"dark\", \"mcpServers\": {\"a\": {\"command\": \"x\"}}}", } { got := jsonEdit(t, data, func(d document) { - require.NoError(t, d.set([]string{"mcpServers"}, "x", map[string]any{"command": "y"})) + require.NoError(t, d.put([]string{"mcpServers"}, "x", map[string]any{"command": "y"}, nil)) }) requireStrictJSON(t, got) assert.Equal(t, "y", semantic(t, mcpconfig.FormatJSON, []byte(got))["mcpServers"].(map[string]any)["x"].(map[string]any)["command"], data) diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index fdfecbdb40d..99da49be832 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -24,7 +24,7 @@ type tomlDoc struct { } func parseTOML(data []byte) (document, error) { - data, bom := stripBOM(data) + data, bom := bytes.CutPrefix(data, utf8BOM) root := map[string]any{} if _, err := toml.Decode(string(data), &root); err != nil { return nil, err @@ -32,19 +32,33 @@ func parseTOML(data []byte) (document, error) { return &tomlDoc{bom: bom, text: data, root: root}, nil } -// table returns the table at root. A missing table reads as empty. -func (d *tomlDoc) table(root []string) (map[string]any, error) { - t := d.root +// node returns the value at the key path root, and whether the path exists. A +// value that is not a table ends the walk, and is returned as it is. +func (d *tomlDoc) node(root []string) (any, bool) { + var v any = d.root for _, key := range root { - next, ok := t[key] + t, ok := v.(map[string]any) if !ok { - return map[string]any{}, nil + return v, true } - m, ok := next.(map[string]any) + next, ok := t[key] if !ok { - return nil, fmt.Errorf("%q is not a TOML table: %w", key, ErrConfigInvalid) + return nil, false } - t = m + v = next + } + return v, true +} + +// table returns the table at root. A missing table reads as empty. +func (d *tomlDoc) table(root []string) (map[string]any, error) { + v, found := d.node(root) + if !found { + return map[string]any{}, nil + } + t, ok := v.(map[string]any) + if !ok { + return nil, fmt.Errorf("%q is not a TOML table: %w", strings.Join(root, "."), ErrConfigInvalid) } return t, nil } @@ -79,27 +93,15 @@ func (d *tomlDoc) entries(root []string) (map[string]map[string]any, error) { return out, nil } -// set and update rewrite the tables of the server from entry. The user's keys -// keep their values, but the text of those tables is rendered again, so their -// comments and layout inside the server's tables are not kept. -func (d *tomlDoc) set(root []string, name string, entry map[string]any) error { - return d.edit(root, name, entry) -} - -func (d *tomlDoc) update(root []string, name string, entry map[string]any) error { - return d.edit(root, name, entry) -} - -func (d *tomlDoc) remove(root []string, name string) error { - return d.edit(root, name, nil) -} - -// edit replaces the tables of server name under root with the rendering of -// entry, or deletes them when entry is nil. A server that exists without tables -// of its own (an inline table or dotted keys) is an error, not rewritten. -// Headers are found in the text with multi-line strings protected, so a line -// inside such a string is never taken for a table. -func (d *tomlDoc) edit(root []string, name string, entry map[string]any) error { +// put replaces the tables of server name under root with the rendering of +// entry, or deletes them when entry is nil. The user's keys keep their values, +// but the text of the server's tables is rendered again, so comments and layout +// inside those tables are not kept. keys is not used, since the tables are always +// rendered from entry. A server that exists without tables of its own (an inline +// table or dotted keys) is an error, not rewritten. Headers are found in the text +// with multi-line strings protected, so a line inside such a string is never +// taken for a table. +func (d *tomlDoc) put(root []string, name string, entry map[string]any, _ []string) error { protected, restore, err := localenv.ProtectMultilineStrings(string(d.text)) if err != nil { return err @@ -168,11 +170,11 @@ func spliceBlock(lines [][]byte, ranges []lineRange, block [][]byte, nl string) return out } -// dropTable is called after a table is dropped, with the lines emitted so far -// and the index of the first line after the table. When nothing is emitted yet, -// the blank lines after the table go, so the file does not start with a blank -// line. Otherwise the blank lines around the table become one blank line, when -// there was one on either side of it and something follows. +// dropTable is called after a table is dropped, with the lines emitted so far and +// the index of the first line after the table. Before any content, the blank lines +// after the table go, so the file does not start with a blank line. At the end of +// the file, the blank lines after the table stay, and those before it go. Between +// content, the blank lines around the table become one blank line. func dropTable(out, lines [][]byte, pos int, nl string) ([][]byte, int) { if !hasContent(out) { for pos < len(lines) && isBlankLine(lines[pos]) { @@ -185,14 +187,17 @@ func dropTable(out, lines [][]byte, pos int, nl string) ([][]byte, int) { out = out[:len(out)-1] blank = true } - for pos < len(lines) && isBlankLine(lines[pos]) { - pos++ - blank = true + end := pos + for end < len(lines) && isBlankLine(lines[end]) { + end++ + } + if end == len(lines) { + return out, pos } - if blank && pos < len(lines) { + if blank || end > pos { out = append(out, []byte(nl)) } - return out, pos + return out, end } func hasContent(lines [][]byte) bool { @@ -206,7 +211,7 @@ func isBlankLine(line []byte) bool { // checkEdit verifies an edit: the server is entry (or gone when entry is nil), // and nothing else in the document changed. func checkEdit(before, after map[string]any, root []string, name string, entry map[string]any) error { - same, err := sameEntry(withoutServer(before, root, name), withoutServer(after, root, name)) + same, err := sameTOML(withoutServer(before, root, name), withoutServer(after, root, name)) if err != nil { return err } @@ -226,7 +231,7 @@ func checkEdit(before, after map[string]any, root []string, name string, entry m if !found { return errors.New("the edit did not add the server") } - same, err = sameEntry(got, entry) + same, err = sameTOML(got, entry) if err != nil { return err } @@ -236,6 +241,28 @@ func checkEdit(before, after map[string]any, root []string, name string, entry m return nil } +// sameTOML compares two tables by their TOML text. JSON has no inf or nan, which +// TOML allows in any table, so the comparison is made in TOML. +func sameTOML(a, b map[string]any) (bool, error) { + ta, err := tomlText(a) + if err != nil { + return false, err + } + tb, err := tomlText(b) + if err != nil { + return false, err + } + return ta == tb, nil +} + +func tomlText(table map[string]any) (string, error) { + var buf bytes.Buffer + if err := toml.NewEncoder(&buf).Encode(table); err != nil { + return "", err + } + return buf.String(), nil +} + // withoutServer returns doc without the server name under root. The tables on // the way are copied, so doc is not changed. A table left empty is dropped, so // a file without the table and one whose table only held the server compare equal. @@ -258,21 +285,11 @@ func withoutServer(doc map[string]any, root []string, name string) map[string]an return out } -// defined reports whether the key path root exists in the document. +// defined reports whether the key path root exists in the document. A table on the +// way that is not a table is taken as defined, since it blocks the path. func (d *tomlDoc) defined(root []string) bool { - t := d.root - for _, key := range root { - next, ok := t[key] - if !ok { - return false - } - m, ok := next.(map[string]any) - if !ok { - return true - } - t = m - } - return true + _, found := d.node(root) + return found } // hasHeaderUnder reports whether a table header in lines is root or below it. @@ -314,15 +331,24 @@ func renderBlock(root []string, name string, entry map[string]any, nl string) ([ return []byte(out.String()), nil } -// appendBlock appends block after out, separated by one blank line. +// appendBlock adds block after the content of out, with one blank line between. +// Blank lines at the end of the file stay last, so removing the block again gives +// back the file's own ending. func appendBlock(out, block [][]byte, nl string) [][]byte { - if len(out) > 0 { - if last := out[len(out)-1]; !bytes.HasSuffix(last, []byte("\n")) { - out[len(out)-1] = append(slices.Clip(last), nl...) + end := len(out) + for end > 0 && isBlankLine(out[end-1]) { + end-- + } + var res [][]byte + res = append(res, out[:end]...) + if end > 0 { + if last := res[len(res)-1]; !bytes.HasSuffix(last, []byte("\n")) { + res[len(res)-1] = append(slices.Clip(last), nl...) } - out = append(out, []byte(nl)) + res = append(res, []byte(nl)) } - return append(out, block...) + res = append(res, block...) + return append(res, out[end:]...) } // lineRange is the half-open range of lines [start, end). diff --git a/libs/aitools/mcp/agentconfig/toml_test.go b/libs/aitools/mcp/agentconfig/toml_test.go index 6bed3f36b7a..96e720a244a 100644 --- a/libs/aitools/mcp/agentconfig/toml_test.go +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -58,8 +58,8 @@ func TestTOMLRemoveDeletesOnlyOwnedTables(t *testing.T) { assert.Equal(t, "# keep\nmodel = \"x\"\n\n[mcp_servers.other]\ncommand = \"npx\"\n", string(readFile(t, path))) } -// A server defined inline or with dotted keys is not rewritten. The error names -// the file and the server. +// TestTOMLRefusesServerWithoutOwnTables checks that a server defined inline or +// with dotted keys is not rewritten. The error names the file and the server. func TestTOMLRefusesServerWithoutOwnTables(t *testing.T) { for _, tc := range []struct { name string @@ -86,8 +86,9 @@ func TestTOMLRefusesServerWithoutOwnTables(t *testing.T) { } } -// A root table written inline cannot take a [mcp_servers.] table. The -// decoder would accept it, so the edit is refused before anything is written. +// TestTOMLRefusesInlineRootTable checks that a root table written inline cannot +// take a [mcp_servers.] table. The decoder would accept it, so the edit is +// refused before anything is written. func TestTOMLRefusesInlineRootTable(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") data := "mcp_servers = {}\n" @@ -99,8 +100,9 @@ func TestTOMLRefusesInlineRootTable(t *testing.T) { assert.Equal(t, data, string(readFile(t, path))) } -// The check after an edit compares everything but the server, so a change to -// other settings is refused even when the server itself is right. +// TestCheckEditRejectsOtherChanges checks that the check after an edit compares +// everything but the server, so a change to other settings is refused even when +// the server itself is right. func TestCheckEditRejectsOtherChanges(t *testing.T) { before := map[string]any{"model": "a", "mcp_servers": map[string]any{}} after := map[string]any{"model": "b", "mcp_servers": map[string]any{testName: testEntry(testCmd, testArgs("test"), nil)}} @@ -141,32 +143,33 @@ func blockFor(agent string) string { `command = "` + testCmd + `"` + "\n" } -// requireTidyTOML fails when the layout has a double blank line, a blank first -// line, or a blank last line that the file did not have. +// requireTidyTOML fails when the layout has a double blank line or a blank first +// line. func requireTidyTOML(t *testing.T, got string) { t.Helper() - assert.NotContains(t, got, "\n\n\n", "no double blank line") - assert.False(t, strings.HasPrefix(got, "\n"), "no blank first line") + require.NotContains(t, got, "\n\n\n", "no double blank line") + require.False(t, strings.HasPrefix(got, "\n"), "no blank first line") } -// Env sub-tables are written, changed, dropped and removed with the server. +// TestTOMLEnvTablesLayout checks that env sub-tables are written, changed, dropped +// and removed with the server. func TestTOMLEnvTablesLayout(t *testing.T) { other := "[mcp_servers.other]\ncommand = \"npx\"\n" - envTable := "[mcp_servers." + testName + ".env]\nTOKEN = \"one\"\n" + envTable := "[mcp_servers." + testName + ".env]\nDATABRICKS_CONFIG_FILE = \"one\"\n" t.Run("add with env", func(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") writeFile(t, path, []byte(other), 0o600) a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"TOKEN": "one"}, false) + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"DATABRICKS_CONFIG_FILE": "one"}, false) require.NoError(t, err) assert.Equal(t, StatusAdded, status) got := string(readFile(t, path)) requireTidyTOML(t, got) entries, err := Read(t.Context(), a) require.NoError(t, err) - assert.Equal(t, map[string]any{"TOKEN": "one"}, entries[testName]["env"]) + assert.Equal(t, map[string]any{"DATABRICKS_CONFIG_FILE": "one"}, entries[testName]["env"]) assert.Contains(t, entries, "other") }) @@ -175,12 +178,12 @@ func TestTOMLEnvTablesLayout(t *testing.T) { writeFile(t, path, []byte(blockFor("test")+"\n"+envTable+"\n"+other), 0o600) a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"TOKEN": "two"}, false) + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"DATABRICKS_CONFIG_FILE": "two"}, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) got := string(readFile(t, path)) requireTidyTOML(t, got) - assert.Contains(t, got, `TOKEN = "two"`) + assert.Contains(t, got, `DATABRICKS_CONFIG_FILE = "two"`) assert.NotContains(t, got, `"one"`) assert.True(t, strings.HasSuffix(got, "\n"+other), got) }) @@ -195,7 +198,7 @@ func TestTOMLEnvTablesLayout(t *testing.T) { assert.Equal(t, StatusUpdated, status) got := string(readFile(t, path)) requireTidyTOML(t, got) - assert.NotContains(t, got, "TOKEN") + assert.NotContains(t, got, "DATABRICKS_CONFIG_FILE") assert.Equal(t, blockFor("other")+"\n"+other, got) }) @@ -222,8 +225,9 @@ func TestTOMLEnvTablesLayout(t *testing.T) { }) } -// A table name in quotes can contain dots, and a server named like its prefix -// is a different server. Neither is edited when the other one is. +// TestTOMLQuotedDottedNameIsAnotherServer checks that a table name in quotes can +// contain dots, and a server named like its prefix is a different server. Neither +// is edited when the other one is. func TestTOMLQuotedDottedNameIsAnotherServer(t *testing.T) { before := "[mcp_servers.\"genie.x\"]\ncommand = \"npx\"\n" path := filepath.Join(t.TempDir(), "config.toml") @@ -241,8 +245,9 @@ func TestTOMLQuotedDottedNameIsAnotherServer(t *testing.T) { assert.Equal(t, before+"\n"+strings.Replace(testBlock, testName, "genie", 1), string(readFile(t, path))) } -// A line that looks like a table header inside a multi-line string is text, not -// a table, so it is not edited. +// TestTOMLHeaderLikeLineInMultilineString checks that a line that looks like a +// table header inside a multi-line string is text, not a table, so it is not +// edited. func TestTOMLHeaderLikeLineInMultilineString(t *testing.T) { notes := "[mcp_servers.notes]\ntext = \"\"\"\n[mcp_servers.genie]\ncommand = \"not a header\"\n\"\"\"\n" path := filepath.Join(t.TempDir(), "config.toml") @@ -261,8 +266,9 @@ func TestTOMLHeaderLikeLineInMultilineString(t *testing.T) { assert.Equal(t, notes, string(readFile(t, path))) } -// Removing a table between two others keeps one blank line between them when -// the table had a blank line on either side of it. +// TestTOMLRemoveKeepsOneSeparator checks that removing a table between two others +// keeps one blank line between them when the table had a blank line on either side +// of it. func TestTOMLRemoveKeepsOneSeparator(t *testing.T) { other := "[mcp_servers.other]\ncommand = \"npx\"\n" for _, tc := range []struct { @@ -287,3 +293,62 @@ func TestTOMLRemoveKeepsOneSeparator(t *testing.T) { }) } } + +// TestTOMLEditWithNonFiniteFloatElsewhere checks that inf and nan in a table the +// edit does not touch do not stop the edit, and that the file comes back as it was. +func TestTOMLEditWithNonFiniteFloatElsewhere(t *testing.T) { + before := "[limits]\nmax = inf\nmin = -inf\nratio = nan\n\n[mcp_servers.other]\ncommand = \"npx\"\n" + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, before, string(readFile(t, path))) +} + +// TestTOMLAddKeepsTrailingBlankLineOnce checks that a file ending in a blank line +// gets one blank line before the new server, and keeps its blank line at the end. +func TestTOMLAddKeepsTrailingBlankLineOnce(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte("model = \"x\"\n\n"), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, "model = \"x\"\n\n"+testBlock+"\n", string(readFile(t, path))) +} + +// TestTOMLAddThenRemoveRestoresBytes checks that adding a server and removing it +// again gives back the file's own bytes, whatever blank lines the file ends with. +func TestTOMLAddThenRemoveRestoresBytes(t *testing.T) { + for _, tc := range []struct { + name string + before string + }{ + {"empty", ""}, + {"no trailing blank", "model = \"x\"\n"}, + {"one trailing blank", "model = \"x\"\n\n"}, + {"two trailing blanks", "model = \"x\"\n\n\n"}, + } { + t.Run(tc.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(tc.before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, tc.before, string(readFile(t, path))) + }) + } +} diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 06cfa1b7ac4..db334fa2287 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -15,6 +15,7 @@ import ( "os" "path/filepath" "reflect" + "slices" "strings" "github.com/databricks/cli/libs/aitools/agents" @@ -48,28 +49,44 @@ type document interface { entry(root []string, name string) (map[string]any, bool, error) // entries returns the object-valued members under root, keyed by name. entries(root []string) (map[string]map[string]any, error) - // set stores entry as name under root, replacing an existing entry and - // creating missing levels. - set(root []string, name string, entry map[string]any) error - // update changes the launch keys of the existing entry name under root to - // those of entry. Other keys of the entry keep their bytes where the format - // allows it. - update(root []string, name string, entry map[string]any) error - // remove deletes name under root. It is a no-op when name is absent. - remove(root []string, name string) error + // put writes entry as server name under root, creating missing levels and + // replacing an existing server. A nil entry removes the server, and removing + // an absent one is a no-op. With keys, an existing server changes only those + // keys, and its other keys keep their bytes where the format allows it. + put(root []string, name string, entry map[string]any, keys []string) error // bytes serializes the document. bytes() ([]byte, error) } -// launchKeys are the entry keys that describe how the server starts. Upsert sets -// them on an update, and an entry whose launch keys match is not rewritten. Any -// other key is the user's after the entry is first added: a default that the -// builder writes (Copilot's tools list) is not written again, and a key the user -// added (disabled, or a tools table) is kept. +// launchKeys are the entry keys that describe how a server starts. An agent's +// builder writes only some of them (see ownedKeys). var launchKeys = []string{"command", "args", "env", "environment", "type"} +// envKeys are the entry keys that hold environment variables: env, and +// environment in OpenCode. +var envKeys = []string{"env", "environment"} + +// pinnedEnv is the environment variable that this CLI pins into an entry when it +// is set (SPEC 7.3). A write that does not pin it removes it from the entry. Other +// variables are the user's, and are kept. +const pinnedEnv = "DATABRICKS_CONFIG_FILE" + +// ownedKeys returns the launch keys that the agent's builder writes. Upsert +// updates these keys and keeps every other key of the entry. The probe passes a +// variable, since a builder writes its env key only when it has variables. +func ownedKeys(spec *mcpconfig.Spec) []string { + probe := spec.Entry("", nil, map[string]string{pinnedEnv: ""}) + var keys []string + for _, key := range launchKeys { + if _, ok := probe[key]; ok { + keys = append(keys, key) + } + } + return keys +} + // Upsert writes a stdio server name for agent a, which runs cmd with args and -// env. An entry this CLI wrote gets its launch keys updated, and the other keys +// env. An entry this CLI wrote gets its launch keys updated, and its other keys // are kept. A foreign entry is replaced only when force is set. The file is left // alone when the entry already matches. cmd must be the databricks binary, or // the entry could never be recognized as ours. It returns the config file path. @@ -106,18 +123,23 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } + keys := ownedKeys(spec) var status Status switch { case !found: status = StatusAdded - err = doc.set(root, name, want) + err = doc.put(root, name, want, nil) case !mcpconfig.IsDatabricksEntry(current) && !force: return path, "", fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) case !mcpconfig.IsDatabricksEntry(current): status = StatusUpdated - err = doc.set(root, name, want) + err = doc.put(root, name, want, nil) default: - same, cmpErr := sameEntry(launchKeysOf(current), launchKeysOf(want)) + merged, mergeErr := keepUserKeys(current, ours, keys) + if mergeErr != nil { + return path, "", mergeErr + } + same, cmpErr := sameEntry(launchKeysOf(current, keys), launchKeysOf(merged, keys)) if cmpErr != nil { return path, "", cmpErr } @@ -125,7 +147,7 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin return path, StatusUnchanged, nil } status = StatusUpdated - err = doc.update(root, name, keepUserKeys(current, want)) + err = doc.put(root, name, merged, keys) } if err != nil { return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) @@ -166,7 +188,7 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { if !mcpconfig.IsDatabricksEntry(current) { return false, fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) } - if err := doc.remove(root, name); err != nil { + if err := doc.put(root, name, nil, nil); err != nil { return false, fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } if err := save(path, doc); err != nil { @@ -270,12 +292,6 @@ func save(path string, doc document) error { return nil } -// stripBOM removes a leading UTF-8 byte order mark, reporting whether there was one. -func stripBOM(data []byte) ([]byte, bool) { - rest, ok := bytes.CutPrefix(data, utf8BOM) - return rest, ok -} - // lineEnding returns CRLF when data uses it, and LF otherwise. func lineEnding(data []byte) string { if bytes.Contains(data, []byte("\r\n")) { @@ -292,10 +308,10 @@ func withBOM(data []byte, bom bool) []byte { return append(append([]byte{}, utf8BOM...), data...) } -// launchKeysOf returns the launch keys of entry. -func launchKeysOf(entry map[string]any) map[string]any { +// launchKeysOf returns the keys of entry that are listed in keys. +func launchKeysOf(entry map[string]any, keys []string) map[string]any { out := map[string]any{} - for _, key := range launchKeys { + for _, key := range keys { if v, ok := entry[key]; ok { out[key] = v } @@ -303,19 +319,61 @@ func launchKeysOf(entry map[string]any) map[string]any { return out } -// keepUserKeys returns current with its launch keys set to those of want, and -// removes a launch key that want lacks. The other keys of current are kept, and -// a key that want has but current lacks is not added. -func keepUserKeys(current, want map[string]any) map[string]any { +// keepUserKeys returns current with the keys listed in keys set to those of want, +// and every other key of current kept. A listed key that want lacks is removed. +// The env tables are merged by mergeEnv. +func keepUserKeys(current, want map[string]any, keys []string) (map[string]any, error) { out := maps.Clone(current) - for _, key := range launchKeys { - if v, ok := want[key]; ok { - out[key] = v + for _, key := range keys { + value, ok := want[key] + if slices.Contains(envKeys, key) { + var err error + value, ok, err = mergeEnv(current[key], value, ok) + if err != nil { + return nil, err + } + } + if ok { + out[key] = value } else { delete(out, key) } } - return out + return out, nil +} + +// mergeEnv returns the env table after a write. It holds the variables of want, +// and the variables of have other than pinnedEnv. It reports false when the table +// is absent: want lacks it and no variable is left. +func mergeEnv(have, want any, inWant bool) (any, bool, error) { + haveEnv, err := envTable(have) + if err != nil { + return nil, false, err + } + wantEnv, _ := want.(map[string]any) + out := map[string]any{} + for k, v := range haveEnv { + if k != pinnedEnv { + out[k] = v + } + } + maps.Copy(out, wantEnv) + if len(out) == 0 && !inWant { + return nil, false, nil + } + return out, true, nil +} + +// envTable returns the variables of an env table, or nil when v is absent. +func envTable(v any) (map[string]any, error) { + if v == nil { + return nil, nil + } + table, ok := v.(map[string]any) + if !ok { + return nil, fmt.Errorf("environment variables are not a table: %w", ErrConfigInvalid) + } + return table, nil } // sameEntry compares two entries as JSON values, so the in-memory Go types of a diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 348667d8182..f0ab74fb6e2 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -185,8 +185,8 @@ func TestUpsertRefusesEntryWeDidNotWrite(t *testing.T) { assert.True(t, mcpconfig.IsDatabricksEntry(entries[testName])) } -// An owned entry with other arguments (say, a different profile) is updated in -// place, and the file is written. +// TestUpsertUpdatesOwnedEntry checks that an owned entry with other arguments +// (say, a different profile) is updated in place, and the file is written. func TestUpsertUpdatesOwnedEntry(t *testing.T) { for _, tc := range []struct { name string @@ -225,8 +225,9 @@ func toStrings(t *testing.T, v any) []string { return out } -// The entry we would write must be recognized as ours, or a later Upsert -// needs force and Remove can never delete it. Nothing is written. +// TestUpsertRefusesCommandThatIsNotDatabricks checks that the entry we would write +// must be recognized as ours, or a later Upsert needs force and Remove can never +// delete it. Nothing is written. func TestUpsertRefusesCommandThatIsNotDatabricks(t *testing.T) { for _, cmd := range []string{"/usr/local/bin/cli", "/x/mydatabricks"} { t.Run(cmd, func(t *testing.T) { @@ -342,7 +343,8 @@ func TestUpsertWritesThroughSymlink(t *testing.T) { assert.Contains(t, entries, testName) } -// A link to a missing file is refused rather than replaced by a regular file. +// TestUpsertRefusesDanglingSymlink checks that a link to a missing file is refused +// rather than replaced by a regular file. func TestUpsertRefusesDanglingSymlink(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("creating symlinks needs privileges on Windows") @@ -386,8 +388,8 @@ func TestWindowsCommandPathRoundTrip(t *testing.T) { } } -// A name that is not a valid server name is refused before anything is read or -// written. +// TestUpsertRefusesInvalidServerName checks that a name that is not a valid server +// name is refused before anything is read or written. func TestUpsertRefusesInvalidServerName(t *testing.T) { for _, name := range []string{"genie.one", "genie one", "genie/one", ""} { t.Run(name, func(t *testing.T) { @@ -402,21 +404,22 @@ func TestUpsertRefusesInvalidServerName(t *testing.T) { } } -// The error names the part that is wrong: the command or the args. +// TestUpsertNamesTheWrongPart checks that the error names the part that is +// wrong: the command or the args. func TestUpsertNamesTheWrongPart(t *testing.T) { a := testAgent(filepath.Join(t.TempDir(), "config.json"), mcpconfig.FormatJSON, "mcpServers") _, _, err := Upsert(t.Context(), a, testName, "/usr/local/bin/cli", testArgs("test"), nil, false) - assert.ErrorContains(t, err, "must be the databricks binary") + require.ErrorContains(t, err, "must be the databricks binary") _, _, err = Upsert(t.Context(), a, testName, testCmd, []string{"aitools", "serve"}, nil, false) - assert.ErrorContains(t, err, "args must start with aitools mcp serve") + require.ErrorContains(t, err, "args must start with aitools mcp serve") assert.NotContains(t, err.Error(), "binary") } -// Keys the user added to an entry we wrote are kept when it is updated. An entry -// that matches in the keys we write is not rewritten, and a changed arg is -// repaired. +// TestUpsertKeepsUserKeysJSON checks that keys the user added to an entry we wrote +// are kept when it is updated. An entry that matches in the keys we write is not +// rewritten, and a changed arg is repaired. func TestUpsertKeepsUserKeysJSON(t *testing.T) { path := filepath.Join(t.TempDir(), "mcp_config.json") entry := testEntry(testCmd, testArgs("test"), nil) @@ -450,8 +453,8 @@ func TestUpsertKeepsUserKeysJSON(t *testing.T) { assert.NotContains(t, entries[testName], "disabled") } -// A Codex block keeps the keys the user added, such as enabled = false, when -// it is updated in place. +// TestUpsertKeepsUserKeysTOML checks that a Codex block keeps the keys the user +// added, such as enabled = false, when it is updated in place. func TestUpsertKeepsUserKeysTOML(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") data := testBlock + "enabled = false\n" @@ -472,9 +475,10 @@ func TestUpsertKeepsUserKeysTOML(t *testing.T) { assert.Equal(t, false, entries[testName]["enabled"]) } -// Copilot narrows the tools of an entry we wrote after the first add. The list -// is the user's from then on: a re-add with the same launch keys leaves the file -// alone, and a changed argument keeps the list. +// TestUpsertKeepsToolsNarrowedByUser checks that Copilot narrows the tools of an entry we +// wrote after the first add. The list is the user's from then on: a re-add with +// the same launch keys leaves the file alone, and a changed argument keeps the +// list. func TestUpsertKeepsToolsNarrowedByUser(t *testing.T) { path := filepath.Join(t.TempDir(), "mcp-config.json") entry := testEntry(testCmd, testArgs("test"), nil) @@ -497,9 +501,10 @@ func TestUpsertKeepsToolsNarrowedByUser(t *testing.T) { assert.Equal(t, []any{"search"}, entries[testName]["tools"]) } -// An update writes only the launch keys that changed. The other members keep -// their bytes: comments, key order, number text and the user's keys. Only the -// command and args are replaced, in place. +// TestUpsertPatchesOnlyChangedKeysJSON checks that an update writes only the +// launch keys that changed. The other members keep their bytes: comments, key +// order, number text and the user's keys. Only the command and args are replaced, +// in place. func TestUpsertPatchesOnlyChangedKeysJSON(t *testing.T) { path := filepath.Join(t.TempDir(), "config.json") before := `{ @@ -548,20 +553,21 @@ func TestUpsertPatchesOnlyChangedKeysJSON(t *testing.T) { `, string(readFile(t, path))) } -// A launch key that the file lacks is added on update, and one that the entry -// no longer has is removed. Either way the file stays strict JSON, whether the -// key was the last member or not. +// TestUpsertUpdateAddsAndRemovesLaunchKeys checks that a launch key that the file +// lacks is added on update, and one that the entry no longer has is removed. +// Either way the file stays strict JSON, whether the key was the last member or +// not. func TestUpsertUpdateAddsAndRemovesLaunchKeys(t *testing.T) { path := filepath.Join(t.TempDir(), "config.json") writeFile(t, path, []byte(`{"mcpServers":{"`+testName+`":{"command":"`+testCmd+`","args":["aitools","mcp","serve","--agent","test"]}}}`), 0o600) a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") - _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"B": "2"}, false) + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{pinnedEnv: "/cfg"}, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) entries, err := Read(t.Context(), a) require.NoError(t, err) - assert.Equal(t, map[string]any{"B": "2"}, entries[testName]["env"]) + assert.Equal(t, map[string]any{pinnedEnv: "/cfg"}, entries[testName]["env"]) requireStrictJSON(t, string(readFile(t, path))) _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) @@ -573,8 +579,9 @@ func TestUpsertUpdateAddsAndRemovesLaunchKeys(t *testing.T) { requireStrictJSON(t, string(readFile(t, path))) } -// Keys of an entry we wrote that the user keeps in TOML are kept on update, -// including sub-tables such as [mcp_servers..tools.]. +// TestTOMLUpdateKeepsUserTables checks that keys of an entry we wrote that the +// user keeps in TOML are kept on update, including sub-tables such as +// [mcp_servers..tools.]. func TestTOMLUpdateKeepsUserTables(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") data := testBlock + "\n[mcp_servers." + testName + ".tools.search]\napproval_mode = \"approve\"\n" @@ -589,9 +596,9 @@ func TestTOMLUpdateKeepsUserTables(t *testing.T) { assert.Equal(t, map[string]any{"search": map[string]any{"approval_mode": "approve"}}, entries[testName]["tools"]) } -// Duplicate member names on the path to the server are refused. Agents read the -// last of two equal names while an edit would change the first, so the file is -// left alone. +// TestUpsertAndRemoveRefuseDuplicateKeysJSON checks that duplicate member names on +// the path to the server are refused. Agents read the last of two equal names +// while an edit would change the first, so the file is left alone. func TestUpsertAndRemoveRefuseDuplicateKeysJSON(t *testing.T) { argsJSON, err := json.Marshal(testArgs("test")) require.NoError(t, err) @@ -615,7 +622,8 @@ func TestUpsertAndRemoveRefuseDuplicateKeysJSON(t *testing.T) { } } -// A file whose top-level value is not an object names the problem once. +// TestUpsertNonObjectTopLevelNamesProblemOnce checks that a file whose top-level +// value is not an object names the problem once. func TestUpsertNonObjectTopLevelNamesProblemOnce(t *testing.T) { path := filepath.Join(t.TempDir(), "config.json") writeFile(t, path, []byte(`[]`), 0o600) @@ -626,8 +634,8 @@ func TestUpsertNonObjectTopLevelNamesProblemOnce(t *testing.T) { assert.Equal(t, 1, strings.Count(err.Error(), ErrConfigInvalid.Error()), err.Error()) } -// Remove checks the name the way Upsert does, so an invalid name is an error -// and nothing is read as a match. +// TestRemoveRefusesInvalidServerName checks that Remove validates the name the +// way Upsert does, so an invalid name is an error and nothing is read as a match. func TestRemoveRefusesInvalidServerName(t *testing.T) { path := filepath.Join(t.TempDir(), "config.json") data := `{"mcpServers":{"` + testName + `":{"command":"` + testCmd + `","args":[]}}}` @@ -638,3 +646,141 @@ func TestRemoveRefusesInvalidServerName(t *testing.T) { assert.ErrorContains(t, err, "must contain only letters, digits, '-' and '_'") assert.Equal(t, data, string(readFile(t, path))) } + +// entryOf returns the entry of server name in agent a's config. +func entryOf(t *testing.T, ctx context.Context, a *agents.Agent, name string) map[string]any { + t.Helper() + entries, err := Read(ctx, a) + require.NoError(t, err) + require.Contains(t, entries, name) + return entries[name] +} + +// TestUpsertKeepsUserEnvJSON checks that a variable the user added to env +// survives an update, with its bytes. The pinned variable takes the value the +// caller passes, and is removed when the caller passes none. +func TestUpsertKeepsUserEnvJSON(t *testing.T) { + path := filepath.Join(t.TempDir(), "mcp_config.json") + entry := testEntry(testCmd, testArgs("test"), map[string]string{"DATABRICKS_CONFIG_FILE": "/old.cfg", "HTTPS_PROXY": "http://proxy.test"}) + data, err := json.Marshal(map[string]any{"mcpServers": map[string]any{testName: entry}}) + require.NoError(t, err) + writeFile(t, path, data, 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"DATABRICKS_CONFIG_FILE": "/new.cfg"}, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, map[string]any{"DATABRICKS_CONFIG_FILE": "/new.cfg", "HTTPS_PROXY": "http://proxy.test"}, entryOf(t, t.Context(), a, testName)["env"]) + assert.Contains(t, string(readFile(t, path)), `"HTTPS_PROXY":"http://proxy.test"`) + + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, map[string]any{"HTTPS_PROXY": "http://proxy.test"}, entryOf(t, t.Context(), a, testName)["env"]) + requireStrictJSON(t, string(readFile(t, path))) +} + +// TestUpsertKeepsUserEnvTOML is TestUpsertKeepsUserEnvJSON for a TOML env table. +func TestUpsertKeepsUserEnvTOML(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + before := testBlock + "\n[mcp_servers." + testName + ".env]\nDATABRICKS_CONFIG_FILE = \"/old.cfg\"\nHTTPS_PROXY = \"http://proxy.test\"\n" + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{"DATABRICKS_CONFIG_FILE": "/new.cfg"}, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, map[string]any{"DATABRICKS_CONFIG_FILE": "/new.cfg", "HTTPS_PROXY": "http://proxy.test"}, entryOf(t, t.Context(), a, testName)["env"]) + + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, map[string]any{"HTTPS_PROXY": "http://proxy.test"}, entryOf(t, t.Context(), a, testName)["env"]) +} + +// TestUpsertKeepsUserEnvironmentOpenCode is TestUpsertKeepsUserEnvJSON for +// OpenCode, whose variables are under "environment". +func TestUpsertKeepsUserEnvironmentOpenCode(t *testing.T) { + ctx := testHome(t) + a := agents.ByName(agents.NameOpenCode) + path, err := a.MCP.Path(ctx) + require.NoError(t, err) + entry := map[string]any{ + "type": "local", + "command": append([]string{testCmd}, testArgs("test")...), + "environment": map[string]string{"DATABRICKS_CONFIG_FILE": "/old.cfg", "HTTPS_PROXY": "http://proxy.test"}, + } + data, err := json.Marshal(map[string]any{"mcp": map[string]any{"servers": map[string]any{testName: entry}}}) + require.NoError(t, err) + writeFile(t, path, data, 0o600) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, map[string]any{"HTTPS_PROXY": "http://proxy.test"}, entryOf(t, ctx, a, testName)["environment"]) +} + +// TestUpsertKeepsTypeWhenBuilderDoesNotWriteIt checks that a "type" key is the +// user's for agents whose builder never writes one, so an update keeps it. +func TestUpsertKeepsTypeWhenBuilderDoesNotWriteIt(t *testing.T) { + for _, name := range []string{agents.NameCursor, agents.NameGemini, agents.NameAntigravity, agents.NameCodex} { + t.Run(name, func(t *testing.T) { + ctx := testHome(t) + a := agents.ByName(name) + path, err := a.MCP.Path(ctx) + require.NoError(t, err) + var before []byte + if a.MCP.Format == mcpconfig.FormatTOML { + before = []byte(testBlock + "type = \"stdio\"\n") + } else { + before, err = json.Marshal(map[string]any{"mcpServers": map[string]any{testName: map[string]any{"command": testCmd, "args": testArgs("test"), "type": "stdio"}}}) + require.NoError(t, err) + } + writeFile(t, path, before, 0o600) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + entry := entryOf(t, ctx, a, testName) + assert.Equal(t, "stdio", entry["type"]) + assert.Contains(t, entry["args"], "other") + }) + } +} + +// TestUpsertRefusesDanglingOpenCodeLink checks that a dangling opencode.jsonc link +// is refused, and that opencode.json is not created beside it. +func TestUpsertRefusesDanglingOpenCodeLink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("creating symlinks needs privileges on Windows") + } + ctx := testHome(t) + a := agents.ByName(agents.NameOpenCode) + path, err := a.MCP.Path(ctx) + require.NoError(t, err) + dir := filepath.Dir(path) + require.NoError(t, os.MkdirAll(dir, 0o755)) + jsonc := filepath.Join(dir, "opencode.jsonc") + require.NoError(t, os.Symlink(filepath.Join(dir, "missing.jsonc"), jsonc)) + + _, _, err = Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + assert.ErrorContains(t, err, "does not exist") + _, err = os.Lstat(filepath.Join(dir, "opencode.json")) + assert.ErrorIs(t, err, os.ErrNotExist) + info, err := os.Lstat(jsonc) + require.NoError(t, err) + assert.NotZero(t, info.Mode()&os.ModeSymlink, "link is kept") +} + +// TestUpsertRefusesEnvThatIsNotATable checks that an env value that is not a table +// is refused and not overwritten. +func TestUpsertRefusesEnvThatIsNotATable(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + data := `{"mcpServers":{"` + testName + `":{"command":"` + testCmd + `","args":["aitools","mcp","serve","--agent","test"],"env":"x"}}}` + writeFile(t, path, []byte(data), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{pinnedEnv: "/cfg"}, false) + require.ErrorIs(t, err, ErrConfigInvalid) + assert.Equal(t, data, string(readFile(t, path))) +} From f178f46e455d8f53efb4bae7355928b5e1524fe6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:18:29 +0000 Subject: [PATCH 06/21] Drop an internal spec reference from a code comment Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/writer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index db334fa2287..b1b7ad26bce 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -67,8 +67,8 @@ var launchKeys = []string{"command", "args", "env", "environment", "type"} var envKeys = []string{"env", "environment"} // pinnedEnv is the environment variable that this CLI pins into an entry when it -// is set (SPEC 7.3). A write that does not pin it removes it from the entry. Other -// variables are the user's, and are kept. +// is set in the caller's environment. A write that does not pin it removes it from +// the entry. Other variables are the user's, and are kept. const pinnedEnv = "DATABRICKS_CONFIG_FILE" // ownedKeys returns the launch keys that the agent's builder writes. Upsert From 90579475bd176692232c569bfbf00ae41f77a9fe Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 12:54:33 +0000 Subject: [PATCH 07/21] Drop containers emptied by removing the last agent MCP server Removing the last server left empty containers behind: "mcpServers": {} in JSON, the emptied levels of "mcp.servers", and a [mcp_servers] table in TOML. An add to a file without them therefore did not return the file to its earlier bytes, and an empty object was written with an odd layout. Remove now drops each object that the removal leaves empty, up to the root object, and drops the [mcp_servers] table when the server was its only key. The root object stays, and it is written as {} when nothing but whitespace is left in it. Each object that changes is also laid out with the file's line ending. Trade-off: an empty container that the user wrote explicitly is dropped too, when it is emptied by this removal. It means the same for every agent, since an empty container and a missing one are read alike. A comment placed inside a dropped container goes with it. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/json.go | 15 ++++++++++- libs/aitools/mcp/agentconfig/json_test.go | 32 +++++++++++++++++++++++ libs/aitools/mcp/agentconfig/toml.go | 20 +++++++++++++- libs/aitools/mcp/agentconfig/toml_test.go | 29 ++++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index 0e680b41f0d..68a33cfcaea 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -365,7 +365,9 @@ func indent(depth int) string { // remove deletes name under root. A missing name is a no-op. Removing the last // member keeps the file's trailing-comma state: a strict file gets no comma after -// the member that becomes last. +// the member that becomes last. An object that the removal leaves empty is +// removed from its parent too, and so on up to the root object, which stays. A +// file that had no such object before an add gets back its bytes. func (d *jsonDoc) remove(root []string, name string) error { obj, err := d.object(root) if err != nil || obj == nil || findMember(obj, name) == nil { @@ -380,6 +382,17 @@ func (d *jsonDoc) remove(root []string, name string) error { return err } setTrailingComma(obj, trailing) + useLineEnding(obj, d.nl) + if len(obj.Members) == 0 { + if len(root) > 0 { + return d.remove(root[:len(root)-1], root[len(root)-1]) + } + // The root object is left empty. Its layout goes, so it reads {} as an + // empty file did, unless a comment sits inside it. + if len(bytes.TrimSpace(obj.AfterExtra)) == 0 { + obj.AfterExtra = nil + } + } return nil } diff --git a/libs/aitools/mcp/agentconfig/json_test.go b/libs/aitools/mcp/agentconfig/json_test.go index d1254cc5568..30f241cc241 100644 --- a/libs/aitools/mcp/agentconfig/json_test.go +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -231,3 +231,35 @@ func TestJSONAddKeepsStrictJSON(t *testing.T) { assert.Equal(t, "y", semantic(t, mcpconfig.FormatJSON, []byte(got))["mcpServers"].(map[string]any)["x"].(map[string]any)["command"], data) } } + +// TestJSONRemoveLastDropsEmptyContainers checks that removing the last member of +// a container drops the container, and each container above it that it leaves +// empty. A file gets back the bytes it had before the add, in LF and CRLF files. +func TestJSONRemoveLastDropsEmptyContainers(t *testing.T) { + for _, tc := range []struct { + name string + root []string + data string + }{ + {"one level", []string{"mcpServers"}, "{\n \"theme\": \"dark\"\n}\n"}, + {"one level compact", []string{"mcpServers"}, `{"theme": "dark"}`}, + {"one level before a member", []string{"mcpServers"}, "{\n \"theme\": \"dark\",\n \"model\": \"x\"\n}\n"}, + {"two levels", []string{"mcp", "servers"}, "{\n \"theme\": \"dark\"\n}\n"}, + {"two levels compact", []string{"mcp", "servers"}, `{"theme": "dark"}`}, + {"empty file object", []string{"mcpServers"}, "{}\n"}, + {"sibling of the emptied level stays", []string{"mcp", "servers"}, `{"mcp": {"other": 1}}`}, + } { + for _, nl := range []string{"\n", "\r\n"} { + t.Run(tc.name, func(t *testing.T) { + data := strings.ReplaceAll(tc.data, "\n", nl) + added := jsonEdit(t, data, func(d document) { + require.NoError(t, d.put(tc.root, "x", map[string]any{"command": "x"}, nil)) + }) + got := jsonEdit(t, added, func(d document) { + require.NoError(t, d.put(tc.root, "x", nil, nil)) + }) + assert.Equal(t, data, got, "line ending %q", nl) + }) + } + } +} diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index 99da49be832..a0d2872510e 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -112,6 +112,18 @@ func (d *tomlDoc) put(root []string, name string, entry map[string]any, _ []stri if err != nil { return err } + // The [root] table goes with its last server, the same as the container of a + // JSON file. Only the server is left in the table when it is found. + if entry == nil && found { + table, err := d.table(root) + if err != nil { + return err + } + if len(table) == 1 { + ranges = append(ranges, tableBlocks(lines, func(parts []string) bool { return slices.Equal(parts, root) })...) + slices.SortFunc(ranges, func(a, b lineRange) int { return a.start - b.start }) + } + } if found && len(ranges) == 0 { return fmt.Errorf("server %q is not defined by its own [%s] tables (inline table or dotted keys), so it cannot be edited; edit it by hand", name, strings.Join(append(root, name), ".")) @@ -361,6 +373,12 @@ type lineRange struct { // next table header. Blank and comment lines right before that header are not // part of the range, since they describe the header that follows. func serverBlocks(lines [][]byte, root []string, name string) []lineRange { + return tableBlocks(lines, func(parts []string) bool { return isServerTable(parts, root, name) }) +} + +// tableBlocks returns the line ranges of the tables whose key path match accepts, +// each ending before the next table header. See serverBlocks. +func tableBlocks(lines [][]byte, match func(parts []string) bool) []lineRange { var headers []int var keys [][]string for i, line := range lines { @@ -371,7 +389,7 @@ func serverBlocks(lines [][]byte, root []string, name string) []lineRange { } var ranges []lineRange for h, parts := range keys { - if !isServerTable(parts, root, name) { + if !match(parts) { continue } end := len(lines) diff --git a/libs/aitools/mcp/agentconfig/toml_test.go b/libs/aitools/mcp/agentconfig/toml_test.go index 96e720a244a..0212efb46a7 100644 --- a/libs/aitools/mcp/agentconfig/toml_test.go +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -352,3 +352,32 @@ func TestTOMLAddThenRemoveRestoresBytes(t *testing.T) { }) } } + +// TestTOMLRemoveLastDropsEmptyRootTable checks that removing the last server +// drops the [mcp_servers] table when no other key is left under it, and keeps it +// when a key is, in LF and CRLF files. +func TestTOMLRemoveLastDropsEmptyRootTable(t *testing.T) { + for _, tc := range []struct { + name string + before string + want string + }{ + {"header only", "model = \"x\"\n\n[mcp_servers]\n", "model = \"x\"\n"}, + {"header with key", "[mcp_servers]\nenabled = true\n", "[mcp_servers]\nenabled = true\n"}, + } { + for _, nl := range []string{"\n", "\r\n"} { + t.Run(tc.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(strings.ReplaceAll(tc.before, "\n", nl)), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, strings.ReplaceAll(tc.want, "\n", nl), string(readFile(t, path)), "line ending %q", nl) + }) + } + } +} From 5f1e1fe71dbd7ac618bdd48a49b5943d00d36582 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 12:55:51 +0000 Subject: [PATCH 08/21] Refuse to write agent MCP configs that the user made read-only The write replaces the file by rename, which needs write permission only on the directory. A config file with mode 0444 was therefore edited and then left at 0444, so the user's read-only setting was bypassed silently. Add and remove now refuse an existing config file that cannot be opened for writing, and name the file and the agent in the error. The check runs only when the write would change the file, so an add that matches the entry already there still succeeds. A missing file is not refused. Running as root writes read-only files, so the check passes there. A read-only directory still fails in the atomic write, and the error no longer repeats the config path the way it did before. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/writer.go | 28 ++++++++++++++--- libs/aitools/mcp/agentconfig/writer_test.go | 35 +++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index b1b7ad26bce..45277f7a298 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -152,6 +152,9 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err != nil { return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } + if err := checkWritable(a, path); err != nil { + return path, "", err + } if err := save(path, doc); err != nil { return path, "", err } @@ -191,6 +194,9 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { if err := doc.put(root, name, nil, nil); err != nil { return false, fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } + if err := checkWritable(a, path); err != nil { + return false, err + } if err := save(path, doc); err != nil { return false, err } @@ -260,6 +266,23 @@ func load(spec *mcpconfig.Spec, path string) (document, error) { return doc, nil } +// checkWritable refuses an existing config file that the user cannot write. The +// save replaces the file by rename, which needs write permission only on the +// directory, so a read-only file would be replaced anyway. A file the user made +// read-only is one they mean to keep as it is. A missing file is not refused. +func checkWritable(a *agents.Agent, path string) error { + f, err := os.OpenFile(path, os.O_WRONLY, 0) + switch { + case errors.Is(err, fs.ErrNotExist): + return nil + case errors.Is(err, fs.ErrPermission): + return fmt.Errorf("%s is read-only; make it writable or leave %s out of --agents", filepath.ToSlash(path), a.Name) + case err != nil: + return fmt.Errorf("%s: %w", filepath.ToSlash(path), err) + } + return f.Close() +} + // save writes doc to path atomically. The file keeps its mode; a new file gets // 0600 and a new directory 0700, because agent configs can hold auth state. func save(path string, doc document) error { @@ -286,10 +309,7 @@ func save(path string, doc document) error { default: return fmt.Errorf("resolve %s: %w", filepath.ToSlash(path), err) } - if err := atomicfile.Write(target, data, perm, atomicfile.MkDir(0o700)); err != nil { - return fmt.Errorf("write %s: %w", filepath.ToSlash(path), err) - } - return nil + return atomicfile.Write(target, data, perm, atomicfile.MkDir(0o700)) } // lineEnding returns CRLF when data uses it, and LF otherwise. diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index f0ab74fb6e2..cf586cf5474 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -784,3 +784,38 @@ func TestUpsertRefusesEnvThatIsNotATable(t *testing.T) { require.ErrorIs(t, err, ErrConfigInvalid) assert.Equal(t, data, string(readFile(t, path))) } + +// TestWriteRefusesReadOnlyFile checks that an add or a remove does not write a +// config file the user made read-only. The file keeps its bytes and mode. An add +// that needs no write still succeeds. +func TestWriteRefusesReadOnlyFile(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Windows does not honor unix file modes") + } + if os.Geteuid() == 0 { + t.Skip("root writes read-only files") + } + path := filepath.Join(t.TempDir(), "config.json") + writeFile(t, path, []byte(`{"mcpServers":{"other":{"command":"npx"}}}`), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + + require.NoError(t, os.Chmod(path, 0o400)) + before := readFile(t, path) + refused := filepath.ToSlash(path) + " is read-only; make it writable or leave test out of --agents" + + _, _, err = Upsert(t.Context(), a, testName, testCmd, testArgs("changed"), nil, false) + require.ErrorContains(t, err, refused) + _, err = Remove(t.Context(), a, testName) + require.ErrorContains(t, err, refused) + assert.Equal(t, before, readFile(t, path)) + info, err := os.Stat(path) + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o400), info.Mode().Perm()) + + _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) +} From 0869ad33841d54385be18b25ff8db353a6b42517 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 13:42:01 +0000 Subject: [PATCH 09/21] Keep env tables when a pin is removed and check env writes Removing one env variable from a server used to prune the env table when it was left empty, so Claude Code entries lost "env": {} and the next identical write reported updated. Only removing a whole server prunes emptied containers now; a member removal keeps its parent object. Duplicate keys were checked only on the server object, so a duplicate DATABRICKS_CONFIG_FILE inside env was patched in one place and read from another. Nested objects of an entry are checked now. Upsert accepted env variables other than the pinned one, which no later write could remove. It refuses them now. Pinning into an existing empty env table keeps its closing brace on its own line. TOML Remove checked for a server defined inline under [mcp_servers] after adding the root table's ranges, so it deleted the table and its comments. The check now runs first, as Upsert's does. The Cursor MCP path cites its documentation. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 1 + libs/aitools/mcp/agentconfig/json.go | 70 +++++++++++++------ libs/aitools/mcp/agentconfig/toml.go | 10 +-- libs/aitools/mcp/agentconfig/writer.go | 15 +++-- libs/aitools/mcp/agentconfig/writer_test.go | 75 +++++++++++++++++++++ 5 files changed, 143 insertions(+), 28 deletions(-) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index a75d07d2a63..08112c93f91 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -216,6 +216,7 @@ var Registry = []*Agent{ // Cursor has a databricks plugin, but it can't be installed headlessly, so // the CLI treats Cursor as a skills-only agent (Plugin nil) rather than // referencing a plugin it can't act on. + // Cursor's global MCP file. See https://cursor.com/docs/context/mcp. MCP: &mcpconfig.Spec{ Path: homeSubdir(".cursor", "mcp.json"), Format: mcpconfig.FormatJSON, diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index 68a33cfcaea..d6d54b2721c 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -124,6 +124,15 @@ func (d *jsonDoc) entry(root []string, name string) (map[string]any, bool, error if err := checkUnique(obj); err != nil { return nil, false, err } + // Nested objects, such as env, are checked too: a duplicate there would be + // patched in one place and read from another. + for _, m := range obj.Members { + if nested, ok := m.Value.Value.(*hujson.Object); ok { + if err := checkUnique(nested); err != nil { + return nil, false, err + } + } + } } entry, ok := decodeObject(&member.Value) if !ok { @@ -250,7 +259,9 @@ func (d *jsonDoc) update(root []string, name string, entry map[string]any, keys func (d *jsonDoc) sync(path []string, have any, inFile bool, want any, inWant bool, depth int) error { parent, key := path[:len(path)-1], path[len(path)-1] if !inWant { - return d.remove(parent, key) + // Only the member goes. An object it empties stays, as claude mcp add writes it. + _, err := d.drop(parent, key) + return err } haveObj, haveIsObj := have.(map[string]any) wantObj, wantIsObj := want.(map[string]any) @@ -279,6 +290,11 @@ func (d *jsonDoc) sync(path []string, have any, inFile bool, want any, inWant bo if err != nil { return err } + before, err := d.object(parent) + if err != nil { + return err + } + emptied := len(before.Members) == 0 if err := d.apply([]patch{{op: opAdd, path: jsonPointer(path...), value: value}}); err != nil { return err } @@ -286,6 +302,10 @@ func (d *jsonDoc) sync(path []string, have any, inFile bool, want any, inWant bo if err != nil { return err } + // An object that was empty keeps its closing brace on its own line, as set lays it out. + if emptied { + obj.AfterExtra = relayout(obj.AfterExtra, d.nl+indent(depth-1)) + } place(obj, key, depth, true, d.nl) return nil } @@ -363,37 +383,47 @@ func indent(depth int) string { return strings.Repeat(" ", depth) } -// remove deletes name under root. A missing name is a no-op. Removing the last -// member keeps the file's trailing-comma state: a strict file gets no comma after -// the member that becomes last. An object that the removal leaves empty is -// removed from its parent too, and so on up to the root object, which stays. A -// file that had no such object before an add gets back its bytes. +// remove deletes the server name under root and prunes the containers that the +// removal leaves empty: each object up to the root object is removed from its +// parent in turn, and the root object stays. A file that had no such object before +// an add gets back its bytes. A missing name is a no-op. func (d *jsonDoc) remove(root []string, name string) error { + obj, err := d.drop(root, name) + if err != nil || obj == nil || len(obj.Members) > 0 { + return err + } + if len(root) > 0 { + return d.remove(root[:len(root)-1], root[len(root)-1]) + } + // The root object is left empty. Its layout goes, so it reads {} as an empty + // file did, unless a comment sits inside it. + if len(bytes.TrimSpace(obj.AfterExtra)) == 0 { + obj.AfterExtra = nil + } + return nil +} + +// drop deletes name under root and returns the object at root after the removal. +// It returns nil when root is absent or name is not a member of it. Removing the +// last member keeps the file's trailing-comma state: a strict file gets no comma +// after the member that becomes last. Nothing else is pruned, so an object left +// empty stays. +func (d *jsonDoc) drop(root []string, name string) (*hujson.Object, error) { obj, err := d.object(root) if err != nil || obj == nil || findMember(obj, name) == nil { - return err + return nil, err } trailing := hasTrailingComma(obj) if err := d.apply([]patch{{op: opRemove, path: jsonPointer(append(root, name)...)}}); err != nil { - return err + return nil, err } obj, err = d.object(root) if err != nil { - return err + return nil, err } setTrailingComma(obj, trailing) useLineEnding(obj, d.nl) - if len(obj.Members) == 0 { - if len(root) > 0 { - return d.remove(root[:len(root)-1], root[len(root)-1]) - } - // The root object is left empty. Its layout goes, so it reads {} as an - // empty file did, unless a comment sits inside it. - if len(bytes.TrimSpace(obj.AfterExtra)) == 0 { - obj.AfterExtra = nil - } - } - return nil + return obj, nil } // hasTrailingComma reports whether the last member of obj is followed by a diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index a0d2872510e..9b036982900 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -112,6 +112,12 @@ func (d *tomlDoc) put(root []string, name string, entry map[string]any, _ []stri if err != nil { return err } + // Checked before the [root] table is added to the ranges below, so that its + // header cannot stand in for the server's own tables. + if found && len(ranges) == 0 { + return fmt.Errorf("server %q is not defined by its own [%s] tables (inline table or dotted keys), so it cannot be edited; edit it by hand", + name, strings.Join(append(root, name), ".")) + } // The [root] table goes with its last server, the same as the container of a // JSON file. Only the server is left in the table when it is found. if entry == nil && found { @@ -124,10 +130,6 @@ func (d *tomlDoc) put(root []string, name string, entry map[string]any, _ []stri slices.SortFunc(ranges, func(a, b lineRange) int { return a.start - b.start }) } } - if found && len(ranges) == 0 { - return fmt.Errorf("server %q is not defined by its own [%s] tables (inline table or dotted keys), so it cannot be edited; edit it by hand", - name, strings.Join(append(root, name), ".")) - } // BurntSushi accepts a table header added to an inline table, which TOML // does not, so the root table must have a header of its own before we add to it. if d.defined(root) && !hasHeaderUnder(lines, root) { diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 45277f7a298..368876671f3 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -86,14 +86,21 @@ func ownedKeys(spec *mcpconfig.Spec) []string { } // Upsert writes a stdio server name for agent a, which runs cmd with args and -// env. An entry this CLI wrote gets its launch keys updated, and its other keys -// are kept. A foreign entry is replaced only when force is set. The file is left -// alone when the entry already matches. cmd must be the databricks binary, or -// the entry could never be recognized as ours. It returns the config file path. +// env. The only variable env may set is pinnedEnv, since other variables are the +// user's and no later write could remove them. An entry this CLI wrote gets its +// launch keys updated, and its other keys are kept. A foreign entry is replaced +// only when force is set. The file is left alone when the entry already matches. +// cmd must be the databricks binary, or the entry could never be recognized as +// ours. It returns the config file path. func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []string, env map[string]string, force bool) (string, Status, error) { if err := checkName(name); err != nil { return "", "", err } + for k := range env { + if k != pinnedEnv { + return "", "", fmt.Errorf("server %q: env may only set %s, got %q", name, pinnedEnv, k) + } + } spec, err := specOf(a) if err != nil { return "", "", err diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index cf586cf5474..23aeceafb98 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -720,6 +720,81 @@ func TestUpsertKeepsUserEnvironmentOpenCode(t *testing.T) { assert.Equal(t, map[string]any{"HTTPS_PROXY": "http://proxy.test"}, entryOf(t, ctx, a, testName)["environment"]) } +// TestUpsertUnpinKeepsEnvClaude checks that unpinning the only variable leaves the +// env table of a Claude Code entry in place, since claude mcp add always writes +// it. The next identical write then reports unchanged. +func TestUpsertUnpinKeepsEnvClaude(t *testing.T) { + ctx := testHome(t) + a := agents.ByName(agents.NameClaudeCode) + _, _, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), map[string]string{pinnedEnv: "/cfg"}, false) + require.NoError(t, err) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, map[string]any{}, entryOf(t, ctx, a, testName)["env"]) + + _, status, err = Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) +} + +// TestUpsertPinIntoEmptyEnvLayout checks that pinning a variable into an existing +// empty env table keeps the closing brace on its own line, as a new env is laid out. +func TestUpsertPinIntoEmptyEnvLayout(t *testing.T) { + ctx := testHome(t) + a := agents.ByName(agents.NameClaudeCode) + path, err := a.MCP.Path(ctx) + require.NoError(t, err) + args, err := json.Marshal(testArgs("test")) + require.NoError(t, err) + writeFile(t, path, []byte(`{"mcpServers":{"`+testName+`":{"type":"stdio","command":"`+testCmd+`","args":`+string(args)+`,"env":{}}}}`), 0o600) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), map[string]string{pinnedEnv: "/cfg"}, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Equal(t, map[string]any{pinnedEnv: "/cfg"}, entryOf(t, ctx, a, testName)["env"]) + assert.Regexp(t, `"/cfg"\n\s+}`, string(readFile(t, path))) + requireStrictJSON(t, string(readFile(t, path))) +} + +// TestUpsertRejectsEnvOtherThanPin checks that Upsert refuses an env variable other +// than the pinned one, since such a variable could never be removed by a later write. +func TestUpsertRejectsEnvOtherThanPin(t *testing.T) { + ctx := testHome(t) + a := agents.ByName(agents.NameClaudeCode) + _, _, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), map[string]string{"HTTPS_PROXY": "http://proxy.test"}, false) + assert.ErrorContains(t, err, "env may only set "+pinnedEnv) +} + +// TestUpsertRejectsDuplicateEnvKey checks that a duplicate key in an env table is +// refused before any write, as a duplicate server key is. +func TestUpsertRejectsDuplicateEnvKey(t *testing.T) { + path := filepath.Join(t.TempDir(), "mcp_config.json") + args, err := json.Marshal(testArgs("test")) + require.NoError(t, err) + data := `{"mcpServers":{"` + testName + `":{"command":"` + testCmd + `","args":` + string(args) + `,"env":{"DATABRICKS_CONFIG_FILE":"/a","DATABRICKS_CONFIG_FILE":"/b"}}}}` + writeFile(t, path, []byte(data), 0o600) + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + _, _, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{pinnedEnv: "/new"}, false) + assert.ErrorContains(t, err, "duplicate key") + assert.Equal(t, data, string(readFile(t, path))) +} + +// TestRemoveInlineServerTOMLKeepsFile checks that Remove refuses a server defined +// inline under [mcp_servers], as Upsert does, and leaves the table and its comment. +func TestRemoveInlineServerTOMLKeepsFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + before := "[mcp_servers]\n# note\n" + testName + ` = { command = "` + testCmd + `", args = ["aitools", "mcp", "serve"] }` + "\n" + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, err := Remove(t.Context(), a, testName) + assert.ErrorContains(t, err, "edit it by hand") + assert.Equal(t, before, string(readFile(t, path))) +} + // TestUpsertKeepsTypeWhenBuilderDoesNotWriteIt checks that a "type" key is the // user's for agents whose builder never writes one, so an update keeps it. func TestUpsertKeepsTypeWhenBuilderDoesNotWriteIt(t *testing.T) { From 4e9655d8c36280b8fa3654ef8c968a33549520d7 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 13:58:20 +0000 Subject: [PATCH 10/21] Ignore array elements when finding TOML table headers A line inside a multi-line array was read as a table header when it looked like one, such as ["c"]. That ended a server's range early, so Remove left part of an array behind. A value such as ["mcp_servers"] inside an array also passed the inline-root guard, and Upsert then wrote a table that Codex rejects. The header scans now skip lines that start inside an array. A line's brackets outside quotes and comments are counted to find that. The IsUnmanaged comment no longer says Upsert calls it. Upsert refuses any entry this CLI did not write unless force is set. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/toml.go | 47 ++++++++++++++++++++- libs/aitools/mcp/agentconfig/unmanaged.go | 5 ++- libs/aitools/mcp/agentconfig/writer_test.go | 27 ++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index 9b036982900..d611522c196 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -308,7 +308,11 @@ func (d *tomlDoc) defined(root []string) bool { // hasHeaderUnder reports whether a table header in lines is root or below it. func hasHeaderUnder(lines [][]byte, root []string) bool { - for _, line := range lines { + inArray := arrayStarts(lines) + for i, line := range lines { + if inArray[i] { + continue + } parts, ok := tomlTableKey(string(line)) if !ok || len(parts) < len(root) { continue @@ -383,7 +387,11 @@ func serverBlocks(lines [][]byte, root []string, name string) []lineRange { func tableBlocks(lines [][]byte, match func(parts []string) bool) []lineRange { var headers []int var keys [][]string + inArray := arrayStarts(lines) for i, line := range lines { + if inArray[i] { + continue + } if parts, ok := tomlTableKey(string(line)); ok { headers = append(headers, i) keys = append(keys, parts) @@ -456,6 +464,43 @@ func tomlTableKey(line string) ([]string, bool) { return parts, true } +// arrayStarts reports, for each line, whether the line starts inside a multi-line +// array. Such a line is an array element, even when it looks like a table header, +// as ["c"] does. The text must have its multi-line strings protected already, so +// each line's quotes close on that line. +func arrayStarts(lines [][]byte) []bool { + starts := make([]bool, len(lines)) + depth := 0 + for i, line := range lines { + starts[i] = depth > 0 + depth = max(0, depth+bracketDelta(string(line))) + } + return starts +} + +// bracketDelta returns the number of "[" minus the number of "]" in line, outside +// of quoted strings and comments. +func bracketDelta(line string) int { + delta := 0 + for i := 0; i < len(line); i++ { + switch c := line[i]; c { + case '#': + return delta + case '"', '\'': + for i++; i < len(line) && line[i] != c; i++ { + if c == '"' && line[i] == '\\' { + i++ + } + } + case '[': + delta++ + case ']': + delta-- + } + } + return delta +} + // tomlKey reads one key at the start of s, a bare key or a basic ("...") or // literal ('...') quoted key, and returns it with the rest of s. func tomlKey(s string) (key, rest string, ok bool) { diff --git a/libs/aitools/mcp/agentconfig/unmanaged.go b/libs/aitools/mcp/agentconfig/unmanaged.go index a4aad7e6ffe..d6e4ba5f3ea 100644 --- a/libs/aitools/mcp/agentconfig/unmanaged.go +++ b/libs/aitools/mcp/agentconfig/unmanaged.go @@ -8,8 +8,9 @@ import ( // IsUnmanaged reports whether entry connects to Databricks managed MCP servers // without having been written by this CLI: the `ug mcp-proxy` bridge, or a -// direct /ai-gateway/mcp-services/ URL. Such entries are listed by `list`, and -// Upsert replaces one only with force. +// direct /ai-gateway/mcp-services/ URL. Such entries are listed by `list`. Upsert +// does not call IsUnmanaged; it replaces any entry this CLI did not write only with +// force, and an unmanaged entry is one of those. func IsUnmanaged(entry map[string]any) bool { if argv, ok := mcpconfig.Argv(entry); ok && len(argv) > 1 && mcpconfig.IsBinary(argv[0], "ug", "ug.exe") && argv[1] == "mcp-proxy" { diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 23aeceafb98..42694031c3f 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -795,6 +795,33 @@ func TestRemoveInlineServerTOMLKeepsFile(t *testing.T) { assert.Equal(t, before, string(readFile(t, path))) } +// TestUpsertArrayElementIsNotHeaderTOML checks that an array element that looks like +// a table header, here ["mcp_servers"], does not stand in for the root table. An +// inline root must still refuse a new server. +func TestUpsertArrayElementIsNotHeaderTOML(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + before := "mcp_servers = {}\nother = [\n [\"mcp_servers\"]\n]\n" + writeFile(t, path, []byte(before), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + assert.ErrorContains(t, err, "edit it by hand") + assert.Equal(t, before, string(readFile(t, path))) +} + +// TestRemoveArrayElementInsideServerTOML checks that an array inside a server's table, +// whose last element looks like a table header, is removed with the server. +func TestRemoveArrayElementInsideServerTOML(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + writeFile(t, path, []byte(testBlock+"extra = [\n [\"c\"]\n]\n"), 0o600) + a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Empty(t, readFile(t, path)) +} + // TestUpsertKeepsTypeWhenBuilderDoesNotWriteIt checks that a "type" key is the // user's for agents whose builder never writes one, so an update keeps it. func TestUpsertKeepsTypeWhenBuilderDoesNotWriteIt(t *testing.T) { From fe50ff08de4ad217295b8b69d7375042b1152160 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 14:16:55 +0000 Subject: [PATCH 11/21] Wrap the underlying errors in the agent MCP writer A dangling-symlink error did not wrap the stat error that caused it, and an env merge error named neither the file nor the server. Both now carry the underlying error and the context that the neighbouring errors have. The Cursor source link moves inside the MCP spec, out of the Plugin note. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 2 +- libs/aitools/mcp/agentconfig/writer.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 08112c93f91..c2552bceb6e 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -216,8 +216,8 @@ var Registry = []*Agent{ // Cursor has a databricks plugin, but it can't be installed headlessly, so // the CLI treats Cursor as a skills-only agent (Plugin nil) rather than // referencing a plugin it can't act on. - // Cursor's global MCP file. See https://cursor.com/docs/context/mcp. MCP: &mcpconfig.Spec{ + // Cursor's global MCP file. See https://cursor.com/docs/context/mcp. Path: homeSubdir(".cursor", "mcp.json"), Format: mcpconfig.FormatJSON, Root: mcpServersKey, diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 368876671f3..c30df24b7e0 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -144,7 +144,7 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin default: merged, mergeErr := keepUserKeys(current, ours, keys) if mergeErr != nil { - return path, "", mergeErr + return path, "", fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, mergeErr) } same, cmpErr := sameEntry(launchKeysOf(current, keys), launchKeysOf(merged, keys)) if cmpErr != nil { @@ -311,7 +311,7 @@ func save(path string, doc document) error { target = resolved case errors.Is(err, fs.ErrNotExist): if info, lerr := os.Lstat(path); lerr == nil && info.Mode()&os.ModeSymlink != 0 { - return fmt.Errorf("%s is a symlink to a file that does not exist", filepath.ToSlash(path)) + return fmt.Errorf("%s is a symlink to a file that does not exist: %w", filepath.ToSlash(path), err) } default: return fmt.Errorf("resolve %s: %w", filepath.ToSlash(path), err) From 920f030134dee48822821dc67cece8b7f5cc4f0e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 14:32:34 +0000 Subject: [PATCH 12/21] Report the first unexpected env key in a fixed order Upsert named whichever unexpected env key the map iteration reached first, so the same call could report a different key on each run. The keys are now checked in sorted order. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/writer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index c30df24b7e0..1b5a220703a 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -96,7 +96,7 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err := checkName(name); err != nil { return "", "", err } - for k := range env { + for _, k := range slices.Sorted(maps.Keys(env)) { if k != pinnedEnv { return "", "", fmt.Errorf("server %q: env may only set %s, got %q", name, pinnedEnv, k) } From d19a0d212334c75d3c8c502d4c8b5d9f07af4774 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 09:43:47 +0000 Subject: [PATCH 13/21] Define the MCP gateway path prefix in mcpconfig The path of the Databricks managed MCP servers was a literal in the unmanaged check. Name it once in mcpconfig, which both agentconfig and the MCP package can import, so the two cannot drift apart. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/unmanaged.go | 2 +- libs/aitools/mcpconfig/spec.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libs/aitools/mcp/agentconfig/unmanaged.go b/libs/aitools/mcp/agentconfig/unmanaged.go index d6e4ba5f3ea..1d6a5ae7a4c 100644 --- a/libs/aitools/mcp/agentconfig/unmanaged.go +++ b/libs/aitools/mcp/agentconfig/unmanaged.go @@ -19,7 +19,7 @@ func IsUnmanaged(entry map[string]any) bool { // Remote entries name their URL differently per agent: url (Cursor, Claude // Code), httpUrl (Gemini streamable HTTP), serverUrl (Antigravity). for _, key := range []string{"url", "httpUrl", "serverUrl"} { - if url, _ := entry[key].(string); strings.Contains(url, "/ai-gateway/mcp-services/") { + if url, _ := entry[key].(string); strings.Contains(url, mcpconfig.MCPServicesPathPrefix) { return true } } diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 9eb80bab97d..84ade55e146 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -9,6 +9,11 @@ import ( "strings" ) +// MCPServicesPathPrefix is the URL path prefix of the Databricks managed MCP +// servers on the AI gateway. An agent entry whose URL contains it connects to one +// of those servers. +const MCPServicesPathPrefix = "/ai-gateway/mcp-services/" + // Format is the on-disk format of an agent's MCP config file. type Format int From 3f8cf217239dd52b700023a9d5f89db7633d186d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 09:45:37 +0000 Subject: [PATCH 14/21] Check JSON agent config edits after writing them TOML edits are decoded again and checked before the file is written, but JSON edits were not. Share the check between the two formats: the edit must change only the target server, an add or update must write the intended entry, and a removal must remove it. The check compares the rest of the document with the containers a removal empties dropped, as the TOML check does, so the drop of the last server's containers passes. The JSON comparison is made on plain values, and TOML keeps its own comparison, since JSON has no inf or nan. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/json.go | 27 +++++++ libs/aitools/mcp/agentconfig/json_test.go | 94 +++++++++++++++++++++++ libs/aitools/mcp/agentconfig/toml.go | 59 +------------- libs/aitools/mcp/agentconfig/toml_test.go | 6 +- libs/aitools/mcp/agentconfig/writer.go | 73 ++++++++++++++++++ 5 files changed, 198 insertions(+), 61 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index d6d54b2721c..cf648ec0da6 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -221,7 +221,34 @@ func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { // put writes entry as server name under root. A nil entry removes it. With keys, // an existing server changes only those keys, and its other keys keep their bytes. +// The edited tree is checked against the tree before the edit, as the TOML edits are. +// The tree is changed in place, so after a failed check the document is not saved. func (d *jsonDoc) put(root []string, name string, entry map[string]any, keys []string) error { + before, err := d.decoded() + if err != nil { + return err + } + if err := d.edit(root, name, entry, keys); err != nil { + return err + } + after, err := d.decoded() + if err != nil { + return err + } + return checkEdit(before, after, root, name, entry, sameEntry) +} + +// decoded returns the document as plain values, without comments and trailing commas. +func (d *jsonDoc) decoded() (map[string]any, error) { + out, ok := decodeObject(&d.root) + if !ok { + return nil, errors.New("the document does not decode as JSON") + } + return out, nil +} + +// edit applies the edit that put describes. +func (d *jsonDoc) edit(root []string, name string, entry map[string]any, keys []string) error { switch { case entry == nil: return d.remove(root, name) diff --git a/libs/aitools/mcp/agentconfig/json_test.go b/libs/aitools/mcp/agentconfig/json_test.go index 30f241cc241..ced99fa49ee 100644 --- a/libs/aitools/mcp/agentconfig/json_test.go +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -263,3 +263,97 @@ func TestJSONRemoveLastDropsEmptyContainers(t *testing.T) { } } } + +// TestJSONCheckEditAcceptsOnlyTheEdit checks the check after a JSON edit. An edit +// that changes another setting, a sibling, or the server's own value is refused. +// A removal may drop the containers it leaves empty, up to the root, and nothing else. +func TestJSONCheckEditAcceptsOnlyTheEdit(t *testing.T) { + entry := testEntry(testCmd, testArgs("test"), nil) + for _, tc := range []struct { + name string + root []string + entry map[string]any + before map[string]any + after map[string]any + want string + }{ + { + name: "add with an other setting changed", + root: []string{"mcpServers"}, + entry: entry, + before: map[string]any{"theme": "dark", "mcpServers": map[string]any{}}, + after: map[string]any{"theme": "light", "mcpServers": map[string]any{testName: entry}}, + want: "changed other settings", + }, + { + name: "add that writes a different server", + root: []string{"mcpServers"}, + entry: entry, + before: map[string]any{"mcpServers": map[string]any{}}, + after: map[string]any{"mcpServers": map[string]any{testName: map[string]any{"command": "other"}}}, + want: "wrote a different server", + }, + { + name: "add that does not write the server", + root: []string{"mcpServers"}, + entry: entry, + before: map[string]any{"mcpServers": map[string]any{}}, + after: map[string]any{"mcpServers": map[string]any{}}, + want: "did not add the server", + }, + { + name: "removal that leaves the server", + root: []string{"mcpServers"}, + before: map[string]any{"mcpServers": map[string]any{testName: entry}}, + after: map[string]any{"mcpServers": map[string]any{testName: entry}}, + want: "did not remove the server", + }, + { + name: "removal that also drops a sibling server", + root: []string{"mcpServers"}, + before: map[string]any{"mcpServers": map[string]any{testName: entry, "other": map[string]any{"command": "x"}}}, + after: map[string]any{"mcpServers": map[string]any{}}, + want: "changed other settings", + }, + { + name: "removal that also drops an unrelated empty container", + root: []string{"mcpServers"}, + before: map[string]any{"mcpServers": map[string]any{testName: entry}, "extra": map[string]any{}}, + after: map[string]any{}, + want: "changed other settings", + }, + { + name: "removal of the last server drops the root table", + root: []string{"mcpServers"}, + before: map[string]any{"theme": "dark", "mcpServers": map[string]any{testName: entry}}, + after: map[string]any{"theme": "dark"}, + }, + { + name: "removal of the last server drops each container up to the root", + root: []string{"mcp", "servers"}, + before: map[string]any{"mcp": map[string]any{"servers": map[string]any{testName: entry}}}, + after: map[string]any{}, + }, + { + name: "removal keeps a sibling of an emptied container", + root: []string{"mcp", "servers"}, + before: map[string]any{"mcp": map[string]any{"servers": map[string]any{testName: entry}, "other": 1.0}}, + after: map[string]any{"mcp": map[string]any{"other": 1.0}}, + }, + { + name: "removal of an absent server changes nothing", + root: []string{"mcpServers"}, + before: map[string]any{"mcpServers": map[string]any{}}, + after: map[string]any{"mcpServers": map[string]any{}}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + err := checkEdit(tc.before, tc.after, tc.root, testName, tc.entry, sameEntry) + if tc.want == "" { + assert.NoError(t, err) + return + } + assert.ErrorContains(t, err, tc.want) + }) + } +} diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index d611522c196..2e051fa9d3c 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -2,9 +2,7 @@ package agentconfig import ( "bytes" - "errors" "fmt" - "maps" "slices" "strconv" "strings" @@ -152,7 +150,7 @@ func (d *tomlDoc) put(root []string, name string, entry map[string]any, _ []stri if _, err := toml.Decode(text, &parsed); err != nil { return fmt.Errorf("server %q: the edited file does not decode: %w", name, err) } - if err := checkEdit(d.root, parsed, root, name, entry); err != nil { + if err := checkEdit(d.root, parsed, root, name, entry, sameTOML); err != nil { return fmt.Errorf("server %q: %w", name, err) } d.text = []byte(text) @@ -222,39 +220,6 @@ func isBlankLine(line []byte) bool { return strings.TrimSpace(string(line)) == "" } -// checkEdit verifies an edit: the server is entry (or gone when entry is nil), -// and nothing else in the document changed. -func checkEdit(before, after map[string]any, root []string, name string, entry map[string]any) error { - same, err := sameTOML(withoutServer(before, root, name), withoutServer(after, root, name)) - if err != nil { - return err - } - if !same { - return errors.New("the edit changed other settings") - } - got, found, err := (&tomlDoc{root: after}).entry(root, name) - if err != nil { - return err - } - if entry == nil { - if found { - return errors.New("the edit did not remove the server") - } - return nil - } - if !found { - return errors.New("the edit did not add the server") - } - same, err = sameTOML(got, entry) - if err != nil { - return err - } - if !same { - return errors.New("the edit wrote a different server") - } - return nil -} - // sameTOML compares two tables by their TOML text. JSON has no inf or nan, which // TOML allows in any table, so the comparison is made in TOML. func sameTOML(a, b map[string]any) (bool, error) { @@ -277,28 +242,6 @@ func tomlText(table map[string]any) (string, error) { return buf.String(), nil } -// withoutServer returns doc without the server name under root. The tables on -// the way are copied, so doc is not changed. A table left empty is dropped, so -// a file without the table and one whose table only held the server compare equal. -func withoutServer(doc map[string]any, root []string, name string) map[string]any { - out := maps.Clone(doc) - if len(root) == 0 { - delete(out, name) - return out - } - child, ok := out[root[0]].(map[string]any) - if !ok { - return out - } - rest := withoutServer(child, root[1:], name) - if len(rest) == 0 { - delete(out, root[0]) - } else { - out[root[0]] = rest - } - return out -} - // defined reports whether the key path root exists in the document. A table on the // way that is not a table is taken as defined, since it blocks the path. func (d *tomlDoc) defined(root []string) bool { diff --git a/libs/aitools/mcp/agentconfig/toml_test.go b/libs/aitools/mcp/agentconfig/toml_test.go index 0212efb46a7..b2c7c6e4904 100644 --- a/libs/aitools/mcp/agentconfig/toml_test.go +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -106,12 +106,12 @@ func TestTOMLRefusesInlineRootTable(t *testing.T) { func TestCheckEditRejectsOtherChanges(t *testing.T) { before := map[string]any{"model": "a", "mcp_servers": map[string]any{}} after := map[string]any{"model": "b", "mcp_servers": map[string]any{testName: testEntry(testCmd, testArgs("test"), nil)}} - err := checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil)) + err := checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil), sameTOML) assert.ErrorContains(t, err, "changed other settings") after["model"] = "a" - assert.NoError(t, checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil))) - assert.ErrorContains(t, checkEdit(before, after, []string{"mcp_servers"}, testName, nil), "did not remove") + assert.NoError(t, checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil), sameTOML)) + assert.ErrorContains(t, checkEdit(before, after, []string{"mcp_servers"}, testName, nil, sameTOML), "did not remove") } func TestTOMLKeepsBOMAndCRLF(t *testing.T) { diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 1b5a220703a..6b8cc7d7704 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -403,6 +403,79 @@ func envTable(v any) (map[string]any, error) { return table, nil } +// checkEdit verifies an edit to a document, in memory and before it is saved: the +// server is entry (or gone when entry is nil), and nothing else in the document +// changed. same compares two documents in the terms of their format. A removal +// drops the containers it leaves empty, up to the root, as withoutServer does, and +// the check accepts that drop and nothing else. +func checkEdit(before, after map[string]any, root []string, name string, entry map[string]any, same func(a, b map[string]any) (bool, error)) error { + eq, err := same(withoutServer(before, root, name), withoutServer(after, root, name)) + if err != nil { + return err + } + if !eq { + return errors.New("the edit changed other settings") + } + got, found := serverIn(after, root, name) + if entry == nil { + if found { + return errors.New("the edit did not remove the server") + } + return nil + } + if !found { + return errors.New("the edit did not add the server") + } + eq, err = same(got, entry) + if err != nil { + return err + } + if !eq { + return errors.New("the edit wrote a different server") + } + return nil +} + +// serverIn returns the table of server name under root, and whether name is there +// at all. A name that holds something other than a table is found, with a nil table. +func serverIn(doc map[string]any, root []string, name string) (map[string]any, bool) { + for _, key := range root { + child, ok := doc[key].(map[string]any) + if !ok { + return nil, false + } + doc = child + } + v, ok := doc[name] + if !ok { + return nil, false + } + entry, _ := v.(map[string]any) + return entry, true +} + +// withoutServer returns doc without the server name under root. The tables on +// the way are copied, so doc is not changed. A table left empty is dropped, so +// a file without the table and one whose table only held the server compare equal. +func withoutServer(doc map[string]any, root []string, name string) map[string]any { + out := maps.Clone(doc) + if len(root) == 0 { + delete(out, name) + return out + } + child, ok := out[root[0]].(map[string]any) + if !ok { + return out + } + rest := withoutServer(child, root[1:], name) + if len(rest) == 0 { + delete(out, root[0]) + } else { + out[root[0]] = rest + } + return out +} + // sameEntry compares two entries as JSON values, so the in-memory Go types of a // freshly built entry and one decoded from a file compare equal. func sameEntry(a, b map[string]any) (bool, error) { From 7101f112fc719dfad39d83b143d571933e1f9876 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 11:26:18 +0000 Subject: [PATCH 15/21] Name the server in JSON edit refusals and wrap the read-only error A JSON edit that fails the check now names the server, as the TOML edit does. The read-only refusal keeps the cause for errors.Is. The read-only test also runs its refusal on Windows, where the mode is the read-only attribute. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/json.go | 5 ++++- libs/aitools/mcp/agentconfig/writer.go | 2 +- libs/aitools/mcp/agentconfig/writer_test.go | 14 ++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index cf648ec0da6..7ad6634475c 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -235,7 +235,10 @@ func (d *jsonDoc) put(root []string, name string, entry map[string]any, keys []s if err != nil { return err } - return checkEdit(before, after, root, name, entry, sameEntry) + if err := checkEdit(before, after, root, name, entry, sameEntry); err != nil { + return fmt.Errorf("server %q: %w", name, err) + } + return nil } // decoded returns the document as plain values, without comments and trailing commas. diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 6b8cc7d7704..0e731421c39 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -283,7 +283,7 @@ func checkWritable(a *agents.Agent, path string) error { case errors.Is(err, fs.ErrNotExist): return nil case errors.Is(err, fs.ErrPermission): - return fmt.Errorf("%s is read-only; make it writable or leave %s out of --agents", filepath.ToSlash(path), a.Name) + return fmt.Errorf("%s is read-only; make it writable or leave %s out of --agents: %w", filepath.ToSlash(path), a.Name, err) case err != nil: return fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 42694031c3f..ae3bc6a513b 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -891,13 +891,13 @@ func TestUpsertRefusesEnvThatIsNotATable(t *testing.T) { // config file the user made read-only. The file keeps its bytes and mode. An add // that needs no write still succeeds. func TestWriteRefusesReadOnlyFile(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("Windows does not honor unix file modes") - } if os.Geteuid() == 0 { t.Skip("root writes read-only files") } path := filepath.Join(t.TempDir(), "config.json") + // Windows maps the mode to the read-only attribute. Writable again before the + // temporary directory is removed. + t.Cleanup(func() { _ = os.Chmod(path, 0o600) }) writeFile(t, path, []byte(`{"mcpServers":{"other":{"command":"npx"}}}`), 0o600) a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) @@ -913,9 +913,11 @@ func TestWriteRefusesReadOnlyFile(t *testing.T) { _, err = Remove(t.Context(), a, testName) require.ErrorContains(t, err, refused) assert.Equal(t, before, readFile(t, path)) - info, err := os.Stat(path) - require.NoError(t, err) - assert.Equal(t, os.FileMode(0o400), info.Mode().Perm()) + if runtime.GOOS != "windows" { + info, err := os.Stat(path) + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o400), info.Mode().Perm()) + } _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) require.NoError(t, err) From 7c4d5c35b07e8e59ca2e487824b046eec20c775a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 12:10:51 +0000 Subject: [PATCH 16/21] Refuse a read-only agent config with the ErrReadOnly sentinel The refusal named --agents, which is a flag of the MCP commands and not of the writer. The writer now says only that the file is read-only and asks for it to be made writable. A ReadOnlyError carries the path and wraps the error that opening the file returned, and it matches the new ErrReadOnly sentinel, so a command can name its own way out of the refusal. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/writer.go | 26 +++++++++++++++++---- libs/aitools/mcp/agentconfig/writer_test.go | 6 ++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 0e731421c39..00baba00a52 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -39,6 +39,9 @@ var ( // ErrConfigInvalid means an agent config file exists but cannot be parsed. // Such a file is never written to. ErrConfigInvalid = errors.New("agent config file cannot be parsed") + // ErrReadOnly means an agent config file exists and the user cannot write it, + // so the write is refused. The error is a *ReadOnlyError. + ErrReadOnly = errors.New("agent config file is read-only") ) var utf8BOM = []byte{0xEF, 0xBB, 0xBF} @@ -159,7 +162,7 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err != nil { return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } - if err := checkWritable(a, path); err != nil { + if err := checkWritable(path); err != nil { return path, "", err } if err := save(path, doc); err != nil { @@ -201,7 +204,7 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { if err := doc.put(root, name, nil, nil); err != nil { return false, fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } - if err := checkWritable(a, path); err != nil { + if err := checkWritable(path); err != nil { return false, err } if err := save(path, doc); err != nil { @@ -273,17 +276,32 @@ func load(spec *mcpconfig.Spec, path string) (document, error) { return doc, nil } +// ReadOnlyError is a write refused because the config file at Path is read-only. +// It matches ErrReadOnly, and wraps the error that opening the file returned. +type ReadOnlyError struct { + Path string + Err error +} + +func (e *ReadOnlyError) Error() string { + return fmt.Sprintf("%s is read-only; make it writable: %v", e.Path, e.Err) +} + +func (e *ReadOnlyError) Unwrap() error { return e.Err } + +func (e *ReadOnlyError) Is(target error) bool { return target == ErrReadOnly } + // checkWritable refuses an existing config file that the user cannot write. The // save replaces the file by rename, which needs write permission only on the // directory, so a read-only file would be replaced anyway. A file the user made // read-only is one they mean to keep as it is. A missing file is not refused. -func checkWritable(a *agents.Agent, path string) error { +func checkWritable(path string) error { f, err := os.OpenFile(path, os.O_WRONLY, 0) switch { case errors.Is(err, fs.ErrNotExist): return nil case errors.Is(err, fs.ErrPermission): - return fmt.Errorf("%s is read-only; make it writable or leave %s out of --agents: %w", filepath.ToSlash(path), a.Name, err) + return &ReadOnlyError{Path: filepath.ToSlash(path), Err: err} case err != nil: return fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index ae3bc6a513b..8007a8e5f38 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -3,6 +3,7 @@ package agentconfig import ( "context" "encoding/json" + "io/fs" "os" "path/filepath" "runtime" @@ -906,11 +907,14 @@ func TestWriteRefusesReadOnlyFile(t *testing.T) { require.NoError(t, os.Chmod(path, 0o400)) before := readFile(t, path) - refused := filepath.ToSlash(path) + " is read-only; make it writable or leave test out of --agents" + refused := filepath.ToSlash(path) + " is read-only; make it writable" _, _, err = Upsert(t.Context(), a, testName, testCmd, testArgs("changed"), nil, false) + require.ErrorIs(t, err, ErrReadOnly) + require.ErrorIs(t, err, fs.ErrPermission) require.ErrorContains(t, err, refused) _, err = Remove(t.Context(), a, testName) + require.ErrorIs(t, err, ErrReadOnly) require.ErrorContains(t, err, refused) assert.Equal(t, before, readFile(t, path)) if runtime.GOOS != "windows" { From 1f2656dbb0a84da0df5db420c9fc490d51d42143 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 12:13:05 +0000 Subject: [PATCH 17/21] Check only the edited part of a JSON agent config edit The check that a JSON edit changed nothing but the target server decoded the whole file before and after each edit. On a large ~/.claude.json that cost about 1.8 s per add or remove. Decode only the containers on the root path and the server. Every other member is kept as its raw bytes, which hujson leaves as they were when a patch does not touch them. The layout after a member's value is left out of the comparison, since removing the last member moves the trailing comma after it. The guarantees are the same. Nothing outside the server changes, except the containers that a removal empties. An add or update writes exactly the entry, and a removal removes it. TOML keeps its own comparison. BenchmarkJSONAddBesideLargeMember adds a server beside a large member: 1 MB went from 19 ms to 1.4 ms, and 16 MB from 312 ms to 19 ms. The cost left is copying the other members' bytes, not decoding them. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/json.go | 68 +++++++++++++++++------ libs/aitools/mcp/agentconfig/json_test.go | 38 ++++++++++++- libs/aitools/mcp/agentconfig/toml.go | 2 +- libs/aitools/mcp/agentconfig/toml_test.go | 6 +- libs/aitools/mcp/agentconfig/writer.go | 13 +++-- 5 files changed, 99 insertions(+), 28 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/json.go b/libs/aitools/mcp/agentconfig/json.go index 7ad6634475c..8330d11fbda 100644 --- a/libs/aitools/mcp/agentconfig/json.go +++ b/libs/aitools/mcp/agentconfig/json.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "maps" + "reflect" "slices" "strings" @@ -221,33 +222,66 @@ func (d *jsonDoc) set(root []string, name string, entry map[string]any) error { // put writes entry as server name under root. A nil entry removes it. With keys, // an existing server changes only those keys, and its other keys keep their bytes. -// The edited tree is checked against the tree before the edit, as the TOML edits are. -// The tree is changed in place, so after a failed check the document is not saved. +// The edited tree is checked against the tree before the edit, as the TOML edits are, +// but only the root path and the server are decoded: see shape. The tree is changed +// in place, so after a failed check the document is not saved. func (d *jsonDoc) put(root []string, name string, entry map[string]any, keys []string) error { - before, err := d.decoded() - if err != nil { - return err - } + before := d.shape(root, name) if err := d.edit(root, name, entry, keys); err != nil { return err } - after, err := d.decoded() - if err != nil { - return err - } - if err := checkEdit(before, after, root, name, entry, sameEntry); err != nil { + after := d.shape(root, name) + if err := checkEdit(before, after, root, name, entry, sameShape, sameEntry); err != nil { return fmt.Errorf("server %q: %w", name, err) } return nil } -// decoded returns the document as plain values, without comments and trailing commas. -func (d *jsonDoc) decoded() (map[string]any, error) { - out, ok := decodeObject(&d.root) - if !ok { - return nil, errors.New("the document does not decode as JSON") +// rawJSON is a member of a document kept as its bytes, without decoding. +type rawJSON string + +// shape returns the document as checkEdit compares it. The containers on the path +// to name and the server are decoded, and every other member is kept as its raw +// bytes: they are copied, not decoded, so an unrelated member costs a copy of its +// bytes and no more. The edit leaves those members as they were, and the bytes show it. +func (d *jsonDoc) shape(root []string, name string) map[string]any { + return shapeOf(d.root.Value.(*hujson.Object), root, name) +} + +func shapeOf(obj *hujson.Object, root []string, name string) map[string]any { + out := make(map[string]any, len(obj.Members)) + for i := range obj.Members { + m := &obj.Members[i] + key := memberName(*m) + switch { + case len(root) > 0 && key == root[0]: + if child, ok := m.Value.Value.(*hujson.Object); ok { + out[key] = shapeOf(child, root[1:], name) + continue + } + case len(root) == 0 && key == name: + if entry, ok := decodeObject(&m.Value); ok { + out[key] = entry + continue + } + } + out[key] = rawValue(&m.Value) } - return out, nil + return out +} + +// rawValue returns the bytes of a member's value. The layout after the value is +// left out, since removing the last member moves the trailing comma that follows it. +func rawValue(v *hujson.Value) rawJSON { + bare := *v + bare.AfterExtra = nil + return rawJSON(bare.Pack()) +} + +// sameShape compares two documents as shape returns them. Raw members must match +// byte for byte. +func sameShape(a, b map[string]any) (bool, error) { + return reflect.DeepEqual(a, b), nil } // edit applies the edit that put describes. diff --git a/libs/aitools/mcp/agentconfig/json_test.go b/libs/aitools/mcp/agentconfig/json_test.go index ced99fa49ee..468c66bb17f 100644 --- a/libs/aitools/mcp/agentconfig/json_test.go +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -2,6 +2,7 @@ package agentconfig import ( "encoding/json" + "fmt" "strings" "testing" @@ -348,7 +349,7 @@ func TestJSONCheckEditAcceptsOnlyTheEdit(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - err := checkEdit(tc.before, tc.after, tc.root, testName, tc.entry, sameEntry) + err := checkEdit(tc.before, tc.after, tc.root, testName, tc.entry, sameShape, sameEntry) if tc.want == "" { assert.NoError(t, err) return @@ -357,3 +358,38 @@ func TestJSONCheckEditAcceptsOnlyTheEdit(t *testing.T) { }) } } + +// TestJSONCheckEditComparesOtherMembersAsBytes checks that a change to a member off +// the root path is refused even though the check does not decode that member. +func TestJSONCheckEditComparesOtherMembersAsBytes(t *testing.T) { + parsed, err := parseJSON([]byte(`{"other":{"a":1},"mcpServers":{}}`)) + require.NoError(t, err) + d := parsed.(*jsonDoc) + root := []string{"mcpServers"} + before := d.shape(root, testName) + require.NoError(t, d.apply([]patch{{op: opReplace, path: "/other/a", value: []byte("2")}})) + after := d.shape(root, testName) + assert.ErrorContains(t, checkEdit(before, after, root, testName, nil, sameShape, sameEntry), "changed other settings") +} + +// BenchmarkJSONAddBesideLargeMember times an add to a config whose other top-level +// member is large, as ~/.claude.json is, where the check must not decode that member. +// The file is parsed again for each run, outside the timer. +func BenchmarkJSONAddBesideLargeMember(b *testing.B) { + entry := testEntry(testCmd, testArgs("test"), nil) + for _, size := range []int{1 << 20, 16 << 20} { + b.Run(fmt.Sprintf("%dMB", size>>20), func(b *testing.B) { + data := []byte(`{"history":"` + strings.Repeat("x", size) + `","mcpServers":{}}`) + b.ReportAllocs() + for b.Loop() { + b.StopTimer() + doc, err := parseJSON(data) + require.NoError(b, err) + b.StartTimer() + if err := doc.put([]string{"mcpServers"}, testName, entry, nil); err != nil { + b.Fatal(err) + } + } + }) + } +} diff --git a/libs/aitools/mcp/agentconfig/toml.go b/libs/aitools/mcp/agentconfig/toml.go index 2e051fa9d3c..697a32c4923 100644 --- a/libs/aitools/mcp/agentconfig/toml.go +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -150,7 +150,7 @@ func (d *tomlDoc) put(root []string, name string, entry map[string]any, _ []stri if _, err := toml.Decode(text, &parsed); err != nil { return fmt.Errorf("server %q: the edited file does not decode: %w", name, err) } - if err := checkEdit(d.root, parsed, root, name, entry, sameTOML); err != nil { + if err := checkEdit(d.root, parsed, root, name, entry, sameTOML, sameTOML); err != nil { return fmt.Errorf("server %q: %w", name, err) } d.text = []byte(text) diff --git a/libs/aitools/mcp/agentconfig/toml_test.go b/libs/aitools/mcp/agentconfig/toml_test.go index b2c7c6e4904..1f65285932b 100644 --- a/libs/aitools/mcp/agentconfig/toml_test.go +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -106,12 +106,12 @@ func TestTOMLRefusesInlineRootTable(t *testing.T) { func TestCheckEditRejectsOtherChanges(t *testing.T) { before := map[string]any{"model": "a", "mcp_servers": map[string]any{}} after := map[string]any{"model": "b", "mcp_servers": map[string]any{testName: testEntry(testCmd, testArgs("test"), nil)}} - err := checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil), sameTOML) + err := checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil), sameTOML, sameTOML) assert.ErrorContains(t, err, "changed other settings") after["model"] = "a" - assert.NoError(t, checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil), sameTOML)) - assert.ErrorContains(t, checkEdit(before, after, []string{"mcp_servers"}, testName, nil, sameTOML), "did not remove") + assert.NoError(t, checkEdit(before, after, []string{"mcp_servers"}, testName, testEntry(testCmd, testArgs("test"), nil), sameTOML, sameTOML)) + assert.ErrorContains(t, checkEdit(before, after, []string{"mcp_servers"}, testName, nil, sameTOML, sameTOML), "did not remove") } func TestTOMLKeepsBOMAndCRLF(t *testing.T) { diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 00baba00a52..fe916035b1f 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -423,11 +423,12 @@ func envTable(v any) (map[string]any, error) { // checkEdit verifies an edit to a document, in memory and before it is saved: the // server is entry (or gone when entry is nil), and nothing else in the document -// changed. same compares two documents in the terms of their format. A removal -// drops the containers it leaves empty, up to the root, as withoutServer does, and -// the check accepts that drop and nothing else. -func checkEdit(before, after map[string]any, root []string, name string, entry map[string]any, same func(a, b map[string]any) (bool, error)) error { - eq, err := same(withoutServer(before, root, name), withoutServer(after, root, name)) +// changed. sameRest compares the document without the server, and sameEntry +// compares the server with entry; both compare in the terms of the format. A +// removal drops the containers it leaves empty, up to the root, as withoutServer +// does, and the check accepts that drop and nothing else. +func checkEdit(before, after map[string]any, root []string, name string, entry map[string]any, sameRest, sameEntry func(a, b map[string]any) (bool, error)) error { + eq, err := sameRest(withoutServer(before, root, name), withoutServer(after, root, name)) if err != nil { return err } @@ -444,7 +445,7 @@ func checkEdit(before, after map[string]any, root []string, name string, entry m if !found { return errors.New("the edit did not add the server") } - eq, err = same(got, entry) + eq, err = sameEntry(got, entry) if err != nil { return err } From 2cf24e608ce8fbd61dd3152f1ff1b3d2630579f7 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 19 Nov 2026 08:23:25 +0000 Subject: [PATCH 18/21] Hold Claude Code's config lock while editing its MCP entry Claude Code locks ~/.claude.json while it saves it, by mkdir on .lock. An edit that read the file, then renamed its own version over it without the lock could drop a Claude Code update, and Claude Code could drop the entry the edit had just written. Specs that set Lock now take the same lock across load, edit and save in Upsert and Remove. The lock is the directory named after the config's real path, and an existing lock older than 10 s is taken as abandoned, as Claude Code does. A live holder keeps the lock for milliseconds, so the lock is not refreshed. A held lock is waited for up to 15 s, then the edit fails naming the lock. Only Claude Code's spec sets Lock. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 1 + libs/aitools/mcp/agentconfig/writer.go | 89 +++++++ libs/aitools/mcp/agentconfig/writer_test.go | 251 ++++++++++++++++++++ libs/aitools/mcpconfig/spec.go | 4 + 4 files changed, 345 insertions(+) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index c2552bceb6e..bd796ec2bfd 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -204,6 +204,7 @@ var Registry = []*Agent{ Format: mcpconfig.FormatJSON, Root: mcpServersKey, Entry: claudeMCPEntry, + Lock: true, }, }, { diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index fe916035b1f..6500a8c143d 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -17,6 +17,7 @@ import ( "reflect" "slices" "strings" + "time" "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/mcpconfig" @@ -123,6 +124,13 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err != nil { return "", "", err } + if spec.Lock { + release, err := lockConfig(path) + if err != nil { + return path, "", err + } + defer release() + } doc, err := load(spec, path) if err != nil { return path, "", err @@ -186,6 +194,13 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { if err != nil { return false, err } + if spec.Lock { + release, err := lockConfig(path) + if err != nil { + return false, err + } + defer release() + } doc, err := load(spec, path) if err != nil { return false, err @@ -255,6 +270,80 @@ func rootPath(spec *mcpconfig.Spec) []string { return strings.Split(spec.Root, ".") } +// Claude Code takes a lock while it saves its config, so a write that reads and +// then renames without the lock can lose Claude's update, or be lost to it. The +// lock is the directory .lock: Claude Code makes it with mkdir and +// removes it with rmdir, and treats a lock whose mtime is over 10 s old as left +// behind by a process that died. +const ( + // lockStaleAfter is the age after which a lock is taken as abandoned. + lockStaleAfter = 10 * time.Second + // lockRetry is the pause between attempts to take a lock that is held. + lockRetry = time.Millisecond +) + +// lockTimeout bounds the wait for a lock held by a live process. It is longer +// than lockStaleAfter, so a lock left by a process that died is taken over on +// the first attempt rather than failing it. A live holder keeps the lock for a +// few milliseconds, since Claude Code only rereads and rewrites the file under it. +var lockTimeout = 15 * time.Second + +// lockConfig takes the lock on the config file at path and returns the function +// that releases it. The lock is held for a few milliseconds at most, so it is +// not refreshed while held; a holder that dies leaves it to go stale. +func lockConfig(path string) (func(), error) { + lock, err := configLock(path) + if err != nil { + return nil, err + } + // With no config directory, Claude Code has never saved this file, so there is no lock to share. The lock is not + // taken then, so that a remove does not create the directory. + if _, err := os.Stat(filepath.Dir(lock)); errors.Is(err, fs.ErrNotExist) { + return func() {}, nil + } + deadline := time.Now().Add(lockTimeout) + for { + err := os.Mkdir(lock, 0o700) + if err == nil { + return func() { _ = os.Remove(lock) }, nil + } + if !errors.Is(err, fs.ErrExist) { + return nil, fmt.Errorf("lock %s: %w", filepath.ToSlash(lock), err) + } + if info, statErr := os.Stat(lock); statErr == nil && time.Since(info.ModTime()) > lockStaleAfter { + if err := os.Remove(lock); err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, fmt.Errorf("remove stale lock %s: %w", filepath.ToSlash(lock), err) + } + continue + } + if time.Now().After(deadline) { + return nil, fmt.Errorf("%s is locked by another process, most likely Claude Code saving its config; try again", filepath.ToSlash(lock)) + } + time.Sleep(lockRetry) + } +} + +// configLock returns the lock path for the config file at path. Like +// proper-lockfile, it names the lock after the file's real path, so a symlinked +// config is locked where its target is. A file that does not exist yet has no +// real path, and its absolute path is used. +func configLock(path string) (string, error) { + abs, err := filepath.Abs(path) + if err != nil { + return "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) + } + resolved, err := filepath.EvalSymlinks(abs) + switch { + case err == nil: + abs = resolved + case errors.Is(err, fs.ErrNotExist): + // A missing file, or a link to one; save refuses the latter. + default: + return "", fmt.Errorf("resolve %s: %w", filepath.ToSlash(path), err) + } + return abs + ".lock", nil +} + // load reads and parses the config at path. A missing file is an empty document. func load(spec *mcpconfig.Spec, path string) (document, error) { data, err := os.ReadFile(path) diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 8007a8e5f38..3817c9f23ce 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -3,12 +3,17 @@ package agentconfig import ( "context" "encoding/json" + "fmt" "io/fs" "os" "path/filepath" "runtime" + "strconv" "strings" + "sync" + "sync/atomic" "testing" + "time" "github.com/BurntSushi/toml" "github.com/databricks/cli/libs/aitools/agents" @@ -927,3 +932,249 @@ func TestWriteRefusesReadOnlyFile(t *testing.T) { require.NoError(t, err) assert.Equal(t, StatusUnchanged, status) } + +// lockedAgent is an agent whose config is locked the way Claude Code locks its own. +func lockedAgent(path string) *agents.Agent { + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + a.MCP.Lock = true + return a +} + +func TestConfigLockPath(t *testing.T) { + dir := t.TempDir() + realDir, err := filepath.EvalSymlinks(dir) + require.NoError(t, err) + + existing := filepath.Join(dir, ".claude.json") + writeFile(t, existing, []byte(`{}`), 0o600) + got, err := configLock(existing) + require.NoError(t, err) + assert.Equal(t, filepath.Join(realDir, ".claude.json.lock"), got) + + missing := filepath.Join(dir, "missing", ".claude.json") + got, err = configLock(missing) + require.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "missing", ".claude.json.lock"), got) +} + +func TestConfigLockPathFollowsSymlink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("creating symlinks needs privileges on Windows") + } + dir := t.TempDir() + realDir, err := filepath.EvalSymlinks(dir) + require.NoError(t, err) + target := filepath.Join(dir, "dotfiles", ".claude.json") + writeFile(t, target, []byte(`{}`), 0o600) + link := filepath.Join(dir, "linked.json") + require.NoError(t, os.Symlink(target, link)) + + got, err := configLock(link) + require.NoError(t, err) + assert.Equal(t, filepath.Join(realDir, "dotfiles", ".claude.json.lock"), got) +} + +// TestLockedEditHoldsLockUntilReleased checks that the lock directory exists +// while it is held and is gone once it is released. Upsert has no point between +// the lock and the save where a test could look, so this checks the lock itself. +func TestLockedEditHoldsLockUntilReleased(t *testing.T) { + path := filepath.Join(t.TempDir(), ".claude.json") + lock, err := configLock(path) + require.NoError(t, err) + + release, err := lockConfig(path) + require.NoError(t, err) + info, err := os.Stat(lock) + require.NoError(t, err) + assert.True(t, info.IsDir()) + + release() + _, err = os.Stat(lock) + assert.ErrorIs(t, err, fs.ErrNotExist) +} + +func TestUpsertAndRemoveReleaseLock(t *testing.T) { + path := filepath.Join(t.TempDir(), ".claude.json") + lock, err := configLock(path) + require.NoError(t, err) + a := lockedAgent(path) + + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + _, err = os.Stat(lock) + assert.ErrorIs(t, err, fs.ErrNotExist) + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.True(t, removed) + _, err = os.Stat(lock) + assert.ErrorIs(t, err, fs.ErrNotExist) +} + +func TestRemoveWithoutConfigDirCreatesNothing(t *testing.T) { + dir := filepath.Join(t.TempDir(), "claude-home") + a := lockedAgent(filepath.Join(dir, ".claude.json")) + + removed, err := Remove(t.Context(), a, testName) + require.NoError(t, err) + assert.False(t, removed) + _, err = os.Stat(dir) + assert.ErrorIs(t, err, fs.ErrNotExist) +} + +func TestUnlockedAgentIgnoresLock(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + lock, err := configLock(path) + require.NoError(t, err) + require.NoError(t, os.Mkdir(lock, 0o700)) + t.Cleanup(func() { _ = os.Remove(lock) }) + + // A lock held by someone else does not stop an agent that does not take one. + _, status, err := Upsert(t.Context(), testAgent(path, mcpconfig.FormatJSON, "mcpServers"), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) +} + +// TestLockedEditTimesOutOnLiveLock checks that an edit waits for a lock that +// another process holds, and fails naming the lock once the wait is over. The +// lock is not ours, so it is left in place. +func TestLockedEditTimesOutOnLiveLock(t *testing.T) { + defer func(prev time.Duration) { lockTimeout = prev }(lockTimeout) + lockTimeout = 200 * time.Millisecond + + path := filepath.Join(t.TempDir(), ".claude.json") + writeFile(t, path, []byte(`{"mcpServers":{"other":{"command":"npx"}}}`), 0o600) + before := readFile(t, path) + lock, err := configLock(path) + require.NoError(t, err) + require.NoError(t, os.Mkdir(lock, 0o700)) + a := lockedAgent(path) + + start := time.Now() + _, _, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) + require.ErrorContains(t, err, filepath.ToSlash(lock)+" is locked by another process") + assert.GreaterOrEqual(t, time.Since(start), lockTimeout) + + _, err = Remove(t.Context(), a, testName) + require.ErrorContains(t, err, filepath.ToSlash(lock)+" is locked by another process") + + _, err = os.Stat(lock) + require.NoError(t, err, "the lock is not ours to remove") + assert.Equal(t, before, readFile(t, path)) +} + +// TestLockedEditWaitsForRelease checks that an edit goes ahead once the other +// process lets go of the lock while the edit waits. +func TestLockedEditWaitsForRelease(t *testing.T) { + path := filepath.Join(t.TempDir(), ".claude.json") + lock, err := configLock(path) + require.NoError(t, err) + require.NoError(t, os.Mkdir(lock, 0o700)) + go func() { + time.Sleep(100 * time.Millisecond) + _ = os.Remove(lock) + }() + + _, status, err := Upsert(t.Context(), lockedAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) +} + +// TestLockedEditTakesOverStaleLock checks that a lock left by a process that died +// is removed once its mtime is older than lockStaleAfter. +func TestLockedEditTakesOverStaleLock(t *testing.T) { + path := filepath.Join(t.TempDir(), ".claude.json") + lock, err := configLock(path) + require.NoError(t, err) + require.NoError(t, os.Mkdir(lock, 0o700)) + old := time.Now().Add(-(lockStaleAfter + time.Second)) + require.NoError(t, os.Chtimes(lock, old, old)) + + _, status, err := Upsert(t.Context(), lockedAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + _, err = os.Stat(lock) + assert.ErrorIs(t, err, fs.ErrNotExist) +} + +// claudeLikeWriter saves the config the way Claude Code does: it takes the lock, +// reads the file, adds one to numStartups, writes a temp file and renames it over +// the config, then releases the lock. saves counts the renames that landed. It +// runs until stop is closed. +func claudeLikeWriter(path, lock string, stop <-chan struct{}, saves *atomic.Int64) { + for { + select { + case <-stop: + return + default: + } + if os.Mkdir(lock, 0o700) != nil { + time.Sleep(100 * time.Microsecond) + continue + } + data, _ := os.ReadFile(path) + m := map[string]any{} + _ = json.Unmarshal(data, &m) + n, _ := m["numStartups"].(float64) + m["numStartups"] = n + 1 + time.Sleep(2 * time.Millisecond) + out, _ := json.MarshalIndent(m, "", " ") + if os.WriteFile(path+".tmp", out, 0o600) == nil && os.Rename(path+".tmp", path) == nil { + saves.Add(1) + } + _ = os.Remove(lock) + time.Sleep(time.Millisecond) + } +} + +// TestLockedEditLosesNoUpdates runs edits against a config that Claude Code +// writes at the same time. Neither side may lose the other's update: every save +// by Claude Code is counted in the file, and every upsert is in the file when it +// returns. +func TestLockedEditLosesNoUpdates(t *testing.T) { + const iterations = 100 + path := filepath.Join(t.TempDir(), ".claude.json") + // A large config makes each save slow enough for the race to show. + projects := map[string]any{} + for i := range 800 { + projects[fmt.Sprintf("/work/project-%03d", i)] = map[string]any{"history": strings.Repeat("x", 500)} + } + seed, err := json.MarshalIndent(map[string]any{"numStartups": 0, "projects": projects}, "", " ") + require.NoError(t, err) + writeFile(t, path, seed, 0o600) + + // Claude Code locks the real path of the config, as configLock does. The test + // resolves it on its own, so the lock under test is not checked against itself. + real, err := filepath.EvalSymlinks(path) + require.NoError(t, err) + + var saves atomic.Int64 + stop := make(chan struct{}) + var wg sync.WaitGroup + wg.Go(func() { + claudeLikeWriter(path, real+".lock", stop, &saves) + }) + stopWriter := sync.OnceFunc(func() { + close(stop) + wg.Wait() + }) + defer stopWriter() + + a := lockedAgent(path) + for i := range iterations { + agent := strconv.Itoa(i) + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs(agent), nil, false) + require.NoError(t, err) + entries, err := Read(t.Context(), a) + require.NoError(t, err) + require.Contains(t, entries, testName, "upsert %d", i) + assert.Equal(t, testArgs(agent), toStrings(t, entries[testName]["args"]), "upsert %d", i) + } + + stopWriter() + var final map[string]any + require.NoError(t, json.Unmarshal(readFile(t, path), &final)) + numStartups, _ := final["numStartups"].(float64) + assert.Equal(t, saves.Load(), int64(numStartups), "Claude Code saves that were lost") +} diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 84ade55e146..702ccf6186c 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -34,6 +34,10 @@ type Spec struct { // Entry builds the entry for a stdio server that runs cmd with args and env. // env is empty when there are no variables to pin. Entry func(cmd string, args []string, env map[string]string) map[string]any + // Lock is set when the agent takes a lock on its config file while it saves + // it, and writers must take the same lock. The lock is the directory named + // after the file's real path with a .lock suffix. + Lock bool } // Argv returns the command line of a stdio entry as argv[0] and its arguments. From 221de5135a20a4c0f3b79a14a95523a4548c8fc2 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 19 Nov 2026 09:02:48 +0000 Subject: [PATCH 19/21] Refuse an OpenCode config that still holds V1 servers OpenCode's V1 layout keeps servers directly under mcp, and V2 moves them under mcp.servers. A write to a V1 file would add a mcp.servers table beside the V1 servers, which a V1 OpenCode reads as a server named servers. Upsert and Remove now refuse such a file with an error that names the servers to move, and leave the file as it is. A member of mcp is taken as a V1 server when it is an object with a type or command key, which every documented server shape has. The V2 mcp.timeout object has neither, so a V2 file still works. The check is opt-in per spec through Legacy, and only the OpenCode spec sets it. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 3 ++ libs/aitools/mcp/agentconfig/writer.go | 36 +++++++++++++++++++ libs/aitools/mcp/agentconfig/writer_test.go | 39 +++++++++++++++++++++ libs/aitools/mcpconfig/spec.go | 4 +++ 4 files changed, 82 insertions(+) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index bd796ec2bfd..93a352089bb 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -251,6 +251,9 @@ var Registry = []*Agent{ Format: mcpconfig.FormatJSON, Root: "mcp.servers", Entry: openCodeMCPEntry, + // OpenCode's V1 layout kept servers directly under mcp. See + // https://opencode.ai/docs/migrate-v1/. + Legacy: true, }, }, { diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 6500a8c143d..c75efb37e5f 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -135,6 +135,9 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if err != nil { return path, "", err } + if err := checkLegacy(path, spec, doc); err != nil { + return path, "", err + } root := rootPath(spec) current, found, err := doc.entry(root, name) if err != nil { @@ -205,6 +208,9 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { if err != nil { return false, err } + if err := checkLegacy(path, spec, doc); err != nil { + return false, err + } root := rootPath(spec) current, found, err := doc.entry(root, name) if err != nil { @@ -270,6 +276,36 @@ func rootPath(spec *mcpconfig.Spec) []string { return strings.Split(spec.Root, ".") } +// checkLegacy refuses a config that still holds servers in an older layout, next +// to the table that Root names. A write would add that table beside them, which +// the older layout reads as a server. An object with a type or command key is +// taken as a server. Other objects in the table, such as a timeout, are not. +func checkLegacy(path string, spec *mcpconfig.Spec, doc document) error { + if !spec.Legacy { + return nil + } + root := rootPath(spec) + parent, own := root[:len(root)-1], root[len(root)-1] + members, err := doc.entries(parent) + if err != nil { + return fmt.Errorf("%s: %w", filepath.ToSlash(path), err) + } + var legacy []string + for name, member := range members { + _, typed := member["type"] + _, command := member["command"] + if name != own && (typed || command) { + legacy = append(legacy, name) + } + } + if len(legacy) == 0 { + return nil + } + slices.Sort(legacy) + return fmt.Errorf("%s: %q holds servers in an older layout (%s); move them under %q and try again", + filepath.ToSlash(path), strings.Join(parent, "."), strings.Join(legacy, ", "), spec.Root) +} + // Claude Code takes a lock while it saves its config, so a write that reads and // then renames without the lock can lose Claude's update, or be lost to it. The // lock is the directory .lock: Claude Code makes it with mkdir and diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 3817c9f23ce..4ef11773317 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -880,6 +880,45 @@ func TestUpsertRefusesDanglingOpenCodeLink(t *testing.T) { assert.NotZero(t, info.Mode()&os.ModeSymlink, "link is kept") } +// TestOpenCodeV1LayoutIsRefused checks that a write refuses an OpenCode config +// whose mcp table holds V1 servers. A V2 mcp.servers table added beside them +// would be read by a V1 OpenCode as a server named servers. The V2 mcp.timeout +// object is not a server, so it does not block a write. +func TestOpenCodeV1LayoutIsRefused(t *testing.T) { + a := agents.ByName(agents.NameOpenCode) + tests := []struct { + name string + body string + refused bool + }{ + {"v1 local server", `{"mcp":{"playwright":{"type":"local","command":["npx","@playwright/mcp"],"enabled":true}}}`, true}, + {"v1 server with command only", `{"mcp":{"playwright":{"command":["npx"]}}}`, true}, + {"v1 server beside v2 table", `{"mcp":{"servers":{},"playwright":{"type":"remote","url":"https://mcp.example.test/mcp"}}}`, true}, + {"v2 servers and timeout", `{"mcp":{"servers":{"other":{"type":"local","command":["npx"]}},"timeout":{"catalog":30000,"execution":30000}}}`, false}, + {"v2 servers only", `{"mcp":{"servers":{}}}`, false}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ctx := testHome(t) + path, err := a.MCP.Path(ctx) + require.NoError(t, err) + writeFile(t, path, []byte(tc.body), 0o600) + + _, _, upsertErr := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + _, removeErr := Remove(ctx, a, testName) + if !tc.refused { + require.NoError(t, upsertErr) + require.NoError(t, removeErr) + return + } + assert.ErrorContains(t, upsertErr, "older layout") + assert.ErrorContains(t, upsertErr, `"mcp.servers"`) + assert.ErrorContains(t, removeErr, "older layout") + assert.Equal(t, tc.body, string(readFile(t, path))) + }) + } +} + // TestUpsertRefusesEnvThatIsNotATable checks that an env value that is not a table // is refused and not overwritten. func TestUpsertRefusesEnvThatIsNotATable(t *testing.T) { diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 702ccf6186c..3a770f6e211 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -38,6 +38,10 @@ type Spec struct { // it, and writers must take the same lock. The lock is the directory named // after the file's real path with a .lock suffix. Lock bool + // Legacy is set when an older layout of the config kept servers directly + // under the table that Root sits in, beside the key Root names. Writers + // refuse such a config. + Legacy bool } // Argv returns the command line of a stdio entry as argv[0] and its arguments. From 6ef00825f583badc2d10e03b0b5c4db58cb5871f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 19 Nov 2026 09:04:52 +0000 Subject: [PATCH 20/21] Write Claude MCP servers to the legacy config Claude Code reads Claude Code reads /.config.json instead of the global .claude.json when the legacy file is non-empty. The entry was written to .claude.json in that case, into a file Claude Code does not read. claudeMCPPath now follows Claude Code's rule. A non-empty legacy file wins. An empty one wins only if it has a write bit set and the global file is missing. The lock from the previous commit is taken on whichever file is written, since it is named after the file's real path. Claude Code's placeholder check for an empty, read-only global file on Linux and WSL is not modelled. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 47 +++++++++++++++++++-- libs/aitools/agents/agents_test.go | 46 ++++++++++++++++++++ libs/aitools/mcp/agentconfig/writer_test.go | 29 +++++++++++++ 3 files changed, 119 insertions(+), 3 deletions(-) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 93a352089bb..0fff0d4c68b 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -438,17 +438,58 @@ func gooseConfigDir(ctx context.Context) (string, error) { return filepath.Join(xdg, "goose"), nil } -// claudeMCPPath returns Claude Code's global config file, which holds user-scope -// mcpServers next to auth state. It sits in the home directory, not in ~/.claude; -// CLAUDE_CONFIG_DIR moves it to /.claude.json. +// claudeMCPPath returns the file that Claude Code reads its user-scope mcpServers +// from. That is the global config, which holds auth state next to them: it sits +// in the home directory, not in ~/.claude, and CLAUDE_CONFIG_DIR moves it to +// /.claude.json. Claude Code reads the legacy /.config.json +// instead when claudeLegacyWins says so. // See https://code.claude.com/docs/en/env-vars. func claudeMCPPath(ctx context.Context) (string, error) { + global, err := claudeGlobalConfig(ctx) + if err != nil { + return "", err + } + dir, err := claudeConfigDir(ctx) + if err != nil { + return "", err + } + legacy := filepath.Join(dir, ".config.json") + if claudeLegacyWins(legacy, global) { + return legacy, nil + } + return global, nil +} + +// claudeGlobalConfig returns Claude Code's global config file: .claude.json in +// CLAUDE_CONFIG_DIR when that is set, and in the home directory otherwise. +func claudeGlobalConfig(ctx context.Context) (string, error) { if dir := env.Get(ctx, "CLAUDE_CONFIG_DIR"); dir != "" { return filepath.Join(dir, ".claude.json"), nil } return homeSubdir(".claude.json")(ctx) } +// claudeLegacyWins reports whether Claude Code reads the legacy config file +// instead of the global one, following its config path resolution. A non-empty +// legacy file always wins. An empty one wins only if some write bit is set and +// the global file is missing. A legacy file that cannot be inspected does not +// win. Claude Code also treats an empty, read-only global file as a placeholder +// on Linux and WSL, which this does not model. +func claudeLegacyWins(legacy, global string) bool { + info, err := os.Stat(legacy) + if err != nil || !info.Mode().IsRegular() { + return false + } + if info.Size() > 0 { + return true + } + if info.Mode().Perm()&0o222 == 0 { + return false + } + _, err = os.Stat(global) + return err != nil +} + // claudeMCPEntry matches `claude mcp add`, which always writes an env map, even an empty one. // See https://code.claude.com/docs/en/mcp. func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any { diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index 4fc4e43026c..276dc6dbbdb 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -2,6 +2,7 @@ package agents import ( "context" + "fmt" "os" "path/filepath" "runtime" @@ -105,6 +106,51 @@ func TestMCPPaths(t *testing.T) { } } +// TestClaudeMCPPathFollowsLegacyConfig checks that Claude's MCP path is the legacy +// .config.json when Claude Code reads that file, and the global file otherwise. +// The legacy file is read when it is non-empty. An empty one that can be written +// is read only when the global file is missing. +func TestClaudeMCPPathFollowsLegacyConfig(t *testing.T) { + tests := []struct { + name string + files map[string]string + want string + }{ + {"no files", nil, "global"}, + {"legacy only, non-empty", map[string]string{"legacy": "{}"}, "legacy"}, + {"legacy and global, non-empty", map[string]string{"legacy": "{}", "global": "{}"}, "legacy"}, + {"legacy empty, global non-empty", map[string]string{"legacy": "", "global": "{}"}, "global"}, + {"legacy empty, global empty", map[string]string{"legacy": "", "global": ""}, "global"}, + {"legacy empty, global missing", map[string]string{"legacy": ""}, "legacy"}, + {"global only", map[string]string{"global": "{}"}, "global"}, + } + for _, tc := range tests { + for _, override := range []bool{false, true} { + t.Run(fmt.Sprintf("%s override=%t", tc.name, override), func(t *testing.T) { + ctx, home := clearMCPOverrides(t) + configDir, globalDir := filepath.Join(home, ".claude"), home + if override { + custom := t.TempDir() + ctx = env.Set(ctx, "CLAUDE_CONFIG_DIR", custom) + configDir, globalDir = custom, custom + } + paths := map[string]string{ + "legacy": filepath.Join(configDir, ".config.json"), + "global": filepath.Join(globalDir, ".claude.json"), + } + for kind, body := range tc.files { + require.NoError(t, os.MkdirAll(filepath.Dir(paths[kind]), 0o755)) + require.NoError(t, os.WriteFile(paths[kind], []byte(body), 0o600)) + } + + got, err := ByName(NameClaudeCode).MCP.Path(ctx) + require.NoError(t, err) + assert.Equal(t, paths[tc.want], got) + }) + } + } +} + // TestOpenCodeMCPPathPrefersJSONC checks that OpenCode reads opencode.json or // opencode.jsonc, and that an existing .jsonc is the file to edit, so a second // file is not created beside it. diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 4ef11773317..9395aabf5f2 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -1075,6 +1075,35 @@ func TestUnlockedAgentIgnoresLock(t *testing.T) { assert.Equal(t, StatusAdded, status) } +// TestClaudeLegacyConfigIsLockedAndWritten checks that when Claude Code reads its +// legacy config, an edit writes that file under the lock on it, and leaves +// ~/.claude.json alone. +func TestClaudeLegacyConfigIsLockedAndWritten(t *testing.T) { + defer func(prev time.Duration) { lockTimeout = prev }(lockTimeout) + lockTimeout = 200 * time.Millisecond + + ctx := testHome(t) + home, err := env.UserHomeDir(ctx) + require.NoError(t, err) + legacy := filepath.Join(home, ".claude", ".config.json") + writeFile(t, legacy, []byte(`{"mcpServers":{"other":{"command":"npx"}}}`), 0o600) + a := agents.ByName(agents.NameClaudeCode) + + lock, err := configLock(legacy) + require.NoError(t, err) + require.NoError(t, os.Mkdir(lock, 0o700)) + _, _, err = Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.ErrorContains(t, err, filepath.ToSlash(lock)+" is locked by another process") + + require.NoError(t, os.Remove(lock)) + path, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, legacy, path) + assert.Equal(t, StatusAdded, status) + _, err = os.Stat(filepath.Join(home, ".claude.json")) + assert.ErrorIs(t, err, fs.ErrNotExist) +} + // TestLockedEditTimesOutOnLiveLock checks that an edit waits for a lock that // another process holds, and fails naming the lock once the wait is over. The // lock is not ours, so it is left in place. From 244c03f07e11c9c19a7769bff14a8bfd644adbb6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 19 Nov 2026 09:44:13 +0000 Subject: [PATCH 21/21] Name Claude Code's config lock after the path as given, and retry a lock Windows is still removing Claude Code locks .lock without resolving symlinks, so a symlinked config is locked next to the link. On Windows, creating a directory that another process is still removing fails with access denied; that is retried until the lock timeout. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/writer.go | 36 ++++++++++------- libs/aitools/mcp/agentconfig/writer_test.go | 44 +++++++++++++++------ libs/aitools/mcpconfig/spec.go | 2 +- 3 files changed, 54 insertions(+), 28 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index c75efb37e5f..198cba95c3e 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -15,6 +15,7 @@ import ( "os" "path/filepath" "reflect" + "runtime" "slices" "strings" "time" @@ -308,7 +309,7 @@ func checkLegacy(path string, spec *mcpconfig.Spec, doc document) error { // Claude Code takes a lock while it saves its config, so a write that reads and // then renames without the lock can lose Claude's update, or be lost to it. The -// lock is the directory .lock: Claude Code makes it with mkdir and +// lock is the directory .lock: Claude Code makes it with mkdir and // removes it with rmdir, and treats a lock whose mtime is over 10 s old as left // behind by a process that died. const ( @@ -343,6 +344,15 @@ func lockConfig(path string) (func(), error) { if err == nil { return func() { _ = os.Remove(lock) }, nil } + if lockBusy(err) { + // Windows refuses to create a directory that another process is still removing, with access denied. + // The lock is then being released, so it is tried again; a real permission problem fails at the deadline. + if time.Now().After(deadline) { + return nil, fmt.Errorf("lock %s: %w", filepath.ToSlash(lock), err) + } + time.Sleep(lockRetry) + continue + } if !errors.Is(err, fs.ErrExist) { return nil, fmt.Errorf("lock %s: %w", filepath.ToSlash(lock), err) } @@ -359,24 +369,22 @@ func lockConfig(path string) (func(), error) { } } -// configLock returns the lock path for the config file at path. Like -// proper-lockfile, it names the lock after the file's real path, so a symlinked -// config is locked where its target is. A file that does not exist yet has no -// real path, and its absolute path is used. +// lockBusy reports whether a failed mkdir of a lock means the lock is being released: on Windows, creating a directory +// that is pending deletion fails with access denied. +func lockBusy(err error) bool { + return runtime.GOOS == "windows" && errors.Is(err, fs.ErrPermission) +} + +// configLock returns the lock path for the config file at path: its absolute +// path with a .lock suffix. Claude Code names its lock after the config path as +// given, without resolving symlinks, so a symlinked config is locked next to the +// link, not next to its target. A symlink in a parent directory needs no care, +// since both names reach the same directory. func configLock(path string) (string, error) { abs, err := filepath.Abs(path) if err != nil { return "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) } - resolved, err := filepath.EvalSymlinks(abs) - switch { - case err == nil: - abs = resolved - case errors.Is(err, fs.ErrNotExist): - // A missing file, or a link to one; save refuses the latter. - default: - return "", fmt.Errorf("resolve %s: %w", filepath.ToSlash(path), err) - } return abs + ".lock", nil } diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 9395aabf5f2..7217f75b479 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -981,14 +981,12 @@ func lockedAgent(path string) *agents.Agent { func TestConfigLockPath(t *testing.T) { dir := t.TempDir() - realDir, err := filepath.EvalSymlinks(dir) - require.NoError(t, err) existing := filepath.Join(dir, ".claude.json") writeFile(t, existing, []byte(`{}`), 0o600) got, err := configLock(existing) require.NoError(t, err) - assert.Equal(t, filepath.Join(realDir, ".claude.json.lock"), got) + assert.Equal(t, filepath.Join(dir, ".claude.json.lock"), got) missing := filepath.Join(dir, "missing", ".claude.json") got, err = configLock(missing) @@ -996,21 +994,41 @@ func TestConfigLockPath(t *testing.T) { assert.Equal(t, filepath.Join(dir, "missing", ".claude.json.lock"), got) } -func TestConfigLockPathFollowsSymlink(t *testing.T) { +// TestConfigLockPathIsTheLinkPath checks that a symlinked config is locked next to the link, where Claude Code locks it, +// not next to its target. +func TestConfigLockPathIsTheLinkPath(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("creating symlinks needs privileges on Windows") } dir := t.TempDir() - realDir, err := filepath.EvalSymlinks(dir) - require.NoError(t, err) - target := filepath.Join(dir, "dotfiles", ".claude.json") + target := filepath.Join(dir, "dotfiles", "claude.json") writeFile(t, target, []byte(`{}`), 0o600) - link := filepath.Join(dir, "linked.json") + link := filepath.Join(dir, ".claude.json") require.NoError(t, os.Symlink(target, link)) got, err := configLock(link) require.NoError(t, err) - assert.Equal(t, filepath.Join(realDir, "dotfiles", ".claude.json.lock"), got) + assert.Equal(t, link+".lock", got) +} + +// TestSymlinkedConfigWaitsForClaudesLock checks that an edit of a symlinked config waits for the lock Claude Code holds +// next to the link. +func TestSymlinkedConfigWaitsForClaudesLock(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("creating symlinks needs privileges on Windows") + } + defer func(prev time.Duration) { lockTimeout = prev }(lockTimeout) + lockTimeout = 200 * time.Millisecond + dir := t.TempDir() + target := filepath.Join(dir, "dotfiles", "claude.json") + writeFile(t, target, []byte(`{}`), 0o600) + link := filepath.Join(dir, ".claude.json") + require.NoError(t, os.Symlink(target, link)) + require.NoError(t, os.Mkdir(link+".lock", 0o700)) // what a live Claude Code holds + t.Cleanup(func() { _ = os.Remove(link + ".lock") }) + + _, _, err := Upsert(t.Context(), lockedAgent(link), testName, testCmd, testArgs("test"), nil, false) + assert.ErrorContains(t, err, "is locked by another process") } // TestLockedEditHoldsLockUntilReleased checks that the lock directory exists @@ -1212,16 +1230,16 @@ func TestLockedEditLosesNoUpdates(t *testing.T) { require.NoError(t, err) writeFile(t, path, seed, 0o600) - // Claude Code locks the real path of the config, as configLock does. The test - // resolves it on its own, so the lock under test is not checked against itself. - real, err := filepath.EvalSymlinks(path) + // Claude Code locks the config's path as given, as configLock does. The test + // builds the name on its own, so the lock under test is not checked against itself. + abs, err := filepath.Abs(path) require.NoError(t, err) var saves atomic.Int64 stop := make(chan struct{}) var wg sync.WaitGroup wg.Go(func() { - claudeLikeWriter(path, real+".lock", stop, &saves) + claudeLikeWriter(path, abs+".lock", stop, &saves) }) stopWriter := sync.OnceFunc(func() { close(stop) diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 3a770f6e211..3cdacf995bb 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -36,7 +36,7 @@ type Spec struct { Entry func(cmd string, args []string, env map[string]string) map[string]any // Lock is set when the agent takes a lock on its config file while it saves // it, and writers must take the same lock. The lock is the directory named - // after the file's real path with a .lock suffix. + // after the file's path as given, symlinks not resolved, with a .lock suffix. Lock bool // Legacy is set when an older layout of the config kept servers directly // under the table that Root sits in, beside the key Root names. Writers