From 667d01352e7bc6390d32267adb4372de2fb8cda0 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 11:45:13 +0000 Subject: [PATCH 1/6] 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 380b523755a01eea8f775f7f02bf8789f9ab00cc Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 12:51:04 +0000 Subject: [PATCH 2/6] 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 1f193242a494101226317b62435387addcc4f027 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 13:19:04 +0000 Subject: [PATCH 3/6] 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 cbb3491e2f954b08cfea8533547fbab02a07a2d3 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 13:45:53 +0000 Subject: [PATCH 4/6] 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 2e8d0f49ba7812fc740c356422d4dcf1374579d8 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:17:45 +0000 Subject: [PATCH 5/6] 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 07cc225f875a44db529f4f685e1e02e9a48938e1 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:18:29 +0000 Subject: [PATCH 6/6] 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