diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 92b11077d14..a75d07d2a63 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -2,11 +2,15 @@ package agents import ( "context" + "errors" + "fmt" + "io/fs" "os" "path/filepath" "runtime" "strings" + "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/env" ) @@ -56,6 +60,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, @@ -137,6 +144,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. // @@ -182,6 +199,12 @@ var Registry = []*Agent{ Binary: "claude", Plugin: claudePlugin(), pluginInstalls: claudePluginInstalls, + MCP: &mcpconfig.Spec{ + Path: claudeMCPPath, + Format: mcpconfig.FormatJSON, + Root: mcpServersKey, + Entry: claudeMCPEntry, + }, }, { Name: NameCursor, @@ -193,6 +216,12 @@ 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: mcpServersKey, + Entry: stdioEntry, + }, }, { Name: NameCodex, @@ -200,6 +229,12 @@ var Registry = []*Agent{ ConfigDir: homeSubdir(".codex"), Binary: "codex", Plugin: databricksPlugin(), + MCP: &mcpconfig.Spec{ + Path: codexMCPPath, + Format: mcpconfig.FormatTOML, + Root: "mcp_servers", + Entry: stdioEntry, + }, }, { Name: NameOpenCode, @@ -209,6 +244,12 @@ 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, + }, }, { Name: NameCopilot, @@ -216,6 +257,12 @@ var Registry = []*Agent{ ConfigDir: homeSubdir(".copilot"), Binary: "copilot", Plugin: databricksPlugin(), + MCP: &mcpconfig.Spec{ + Path: copilotMCPPath, + Format: mcpconfig.FormatJSON, + Root: mcpServersKey, + Entry: copilotMCPEntry, + }, }, { Name: NameAntigravity, @@ -223,6 +270,12 @@ 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: mcpServersKey, + Entry: stdioEntry, + }, }, { Name: NamePi, @@ -246,6 +299,12 @@ 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: mcpServersKey, + Entry: stdioEntry, + }, }, { Name: NameGoose, @@ -374,6 +433,157 @@ 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. +// 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 + } + return homeSubdir(".claude.json")(ctx) +} + +// 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{} + } + return map[string]any{"type": "stdio", "command": cmd, "args": args, "env": vars} +} + +// codexMCPPath returns Codex's config.toml, which honors CODEX_HOME. +// 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 + } + return homeSubdir(".codex", "config.toml")(ctx) +} + +// 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 + } + 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 true, nil + case errors.Is(err, fs.ErrNotExist): + return false, nil + default: + 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). +// 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 { + entry["environment"] = vars + } + return entry +} + +// copilotMCPPath returns Copilot CLI's user config. COPILOT_HOME replaces ~/.copilot. +// 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 + } + return homeSubdir(".copilot", "mcp-config.json")(ctx) +} + +// copilotMCPEntry is the stdio entry Copilot CLI documents, with all tools enabled. +// 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 + 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. +// See 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). +// See https://antigravity.google/docs/mcp. +func antigravityMCPPath(ctx context.Context) (string, error) { + return homeSubdir(".gemini", "config", "mcp_config.json")(ctx) +} + +// 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 { + 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..4fc4e43026c 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -1,7 +1,10 @@ package agents import ( + "context" + "os" "path/filepath" + "runtime" "testing" "github.com/databricks/cli/libs/env" @@ -43,6 +46,111 @@ 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) { + 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"} { + ctx = env.Set(ctx, key, "") //nolint:fatcontext // each key is set on the same context + } + return ctx, 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) { + tctx := ctx + for k, v := range tc.override { + tctx = env.Set(tctx, k, v) //nolint:fatcontext // each override is set on the same context + } + got, err := ByName(tc.agent).MCP.Path(tctx) + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } +} + +// TestOpenCodeMCPPathPrefersJSONC checks that OpenCode reads opencode.json or +// opencode.jsonc, and that an existing .jsonc is the file to edit, so a second +// file is not created beside it. +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"} + + 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'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, map[string]any{"command": "/bin/databricks", "args": args}, antigravity) +} + func TestSkillsOnlyNamesMatchesRegistry(t *testing.T) { names := SkillsOnlyNames() // Skills-only agents (Plugin nil) are listed; plugin agents are not. @@ -57,3 +165,52 @@ func TestSkillsOnlyNamesMatchesRegistry(t *testing.T) { } } } + +// 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("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)) + 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) + 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 new file mode 100644 index 00000000000..0e680b41f0d --- /dev/null +++ b/libs/aitools/mcp/agentconfig/json.go @@ -0,0 +1,482 @@ +package agentconfig + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "maps" + "slices" + "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. +type jsonDoc struct { + bom bool + // 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 := 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") + } + root, err := hujson.Parse(data) + if err != nil { + return nil, err + } + if _, ok := root.Value.(*hujson.Object); !ok { + 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. 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 { + return nil, nil + } + next, ok := member.Value.Value.(*hujson.Object) + 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 { + return &obj.Members[i] + } + } + return nil +} + +func memberName(m hujson.ObjectMember) string { + return m.Name.Value.(hujson.Literal).String() +} + +// 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 := decodeJSON(std, &out); err != nil || out == nil { + return nil, false + } + return out, true +} + +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 + } + 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) + } + 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 out, 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 + } + } + // 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 + } + entryNew := parent == nil || findMember(parent, name) == nil + value, err := d.encodeValue(entry, n+1) + if err != nil { + return err + } + var ops []patch + for i := first; i < n; i++ { + ops = append(ops, patch{op: opAdd, path: jsonPointer(root[:i+1]...), value: []byte("{" + d.nl + indent(i+1) + "}")}) + } + ops = append(ops, patch{op: opAdd, 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, d.nl) + } + obj, err := d.object(root) + if err != nil { + return err + } + place(obj, name, n+1, entryNew, d.nl) + return nil +} + +// 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 + } + server := slices.Concat(root, []string{name}) + for _, key := range keys { + have, inFile := current[key] + 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 + } + } + return nil + } + if inFile { + same, err := sameEntry(map[string]any{key: have}, map[string]any{key: want}) + if err != nil || same { + return err + } + value, err := d.encodeValue(want, depth) + if err != nil { + return err + } + return d.apply([]patch{{op: opReplace, path: jsonPointer(path...), value: value}}) + } + value, err := d.encodeValue(want, depth) + if err != nil { + return err + } + 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 +} + +// 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 + } + 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. +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. +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(" ") + } + if newMember { + m.Name.BeforeExtra = relayout(m.Name.BeforeExtra, nl+indent(depth)) + } +} + +// 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 { + 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 { + 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: opRemove, 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 { + data, err := marshalPatch(ops) + if err != nil { + return err + } + return d.root.Patch(data) +} + +func (d *jsonDoc) bytes() ([]byte, error) { + return withBOM(d.root.Pack(), d.bom), nil +} + +// 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 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('}') + } + buf.WriteByte(']') + return buf.Bytes(), nil +} + +// 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)) + } + return b.String() +} + +// encodeJSON encodes v as compact JSON without escaping <, > and &. +func encodeJSON(v any) ([]byte, error) { + return encodeWith(v, "", "") +} + +// 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, " ") +} + +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 + } + 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..d1254cc5568 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/json_test.go @@ -0,0 +1,233 @@ +package agentconfig + +import ( + "encoding/json" + "strings" + "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.put([]string{"mcpServers"}, testName, entry, nil)) + }) + 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.put([]string{"mcpServers"}, testName, nil, nil)) + }) + 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.put([]string{"mcp", "servers"}, "x", map[string]any{"type": "local"}, nil)) + }) + 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.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) +} + +// 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.put([]string{"mcpServers"}, "x", entry, nil)) + }) + assert.Equal(t, `{ + "mcpServers": { + "x": { + "args": [ + "x" + ], + "command": "/a&c" + } + } +} +`, got) +} + +func TestJSONRejectsNonObjectTopLevel(t *testing.T) { + _, err := parseJSON([]byte(`"text"`)) + require.ErrorContains(t, err, "top-level value is not a JSON object") +} + +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) +} + +// 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) +} + +// 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) { + 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) + } +} + +// 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.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) +} + +// 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 + 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.put([]string{"mcpServers"}, "x", map[string]any{"command": "x"}, nil)) + }) + assert.Equal(t, tc.want, got) + }) + } +} + +// 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.put([]string{"mcpServers"}, "x", map[string]any{"command": "x"}, nil)) + }) + assert.Equal(t, want, got, "input %q", data) + } +} + +// 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", + "{\"mcpServers\": {\"a\": {} , \"b\": {\"command\": \"x\"}}}", + "{\"mcpServers\": {\"a\": {} , \"b\": {\"command\": \"x\"}}}", + } { + got := jsonEdit(t, data, func(d document) { + 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))) + } +} + +// 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.put([]string{"mcpServers"}, "b", nil, nil)) + }) + assert.Equal(t, "{\"mcpServers\": {\"a\": {},}}", got) +} + +// 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.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.put([]string{"mcpServers"}, "x", nil, nil)) + }) + assert.Equal(t, data, got) +} + +// 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{ + "{}", + "{\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.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/testdata/antigravity/after.json b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json new file mode 100644 index 00000000000..0607b1f31e2 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/antigravity/after.json @@ -0,0 +1,23 @@ +{ + "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" + } + } +} 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..db4e5bff87f --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/codex/after.toml @@ -0,0 +1,13 @@ +# Codex settings. This comment is kept. +model = "gpt-5" + +[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 new file mode 100644 index 00000000000..dcc64749324 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/codex/before.toml @@ -0,0 +1,9 @@ +# Codex settings. This comment is kept. +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..2dca4240bd4 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/copilot/after.json @@ -0,0 +1,29 @@ +{ + "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..281a1c106a5 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/cursor/after.json @@ -0,0 +1,23 @@ +{ + "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..40184dd02ec --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/gemini/after.json @@ -0,0 +1,25 @@ +{ + "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..1a532dcad2f --- /dev/null +++ b/libs/aitools/mcp/agentconfig/testdata/opencode/after.json @@ -0,0 +1,27 @@ +{ + "$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..99da49be832 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/toml.go @@ -0,0 +1,505 @@ +package agentconfig + +import ( + "bytes" + "errors" + "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 +// 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 := bytes.CutPrefix(data, utf8BOM) + root := map[string]any{} + if _, err := toml.Decode(string(data), &root); err != nil { + return nil, err + } + return &tomlDoc{bom: bom, text: data, root: root}, nil +} + +// 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 { + t, ok := v.(map[string]any) + if !ok { + return v, true + } + next, ok := t[key] + if !ok { + return nil, false + } + 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 +} + +func (d *tomlDoc) 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[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) + 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 +} + +// 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 + } + lines := splitLines([]byte(protected)) + ranges := serverBlocks(lines, root, name) + _, found, err := d.entry(root, name) + if err != nil { + return err + } + 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) + } + + nl := lineEnding(d.text) + var block [][]byte + if entry != 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) + } + 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 := false + for _, r := range ranges { + out = append(out, lines[pos:r.start]...) + pos = r.end + if block != nil && !placed { + out = append(out, block...) + placed = true + continue + } + out, pos = dropTable(out, lines, pos, nl) + } + out = append(out, lines[pos:]...) + if block != nil && !placed { + out = appendBlock(out, block, nl) + } + 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. 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]) { + pos++ + } + return out, pos + } + blank := false + for len(out) > 0 && isBlankLine(out[len(out)-1]) { + out = out[:len(out)-1] + blank = true + } + end := pos + for end < len(lines) && isBlankLine(lines[end]) { + end++ + } + if end == len(lines) { + return out, pos + } + if blank || end > pos { + out = append(out, []byte(nl)) + } + return out, end +} + +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), +// and nothing else in the document changed. +func checkEdit(before, after map[string]any, root []string, name string, entry map[string]any) error { + same, err := sameTOML(withoutServer(before, root, name), withoutServer(after, root, name)) + if err != nil { + return err + } + if !same { + return errors.New("the edit changed other settings") + } + got, found, err := (&tomlDoc{root: after}).entry(root, name) + if err != nil { + return err + } + if entry == nil { + if found { + return errors.New("the edit did not remove the server") + } + return nil + } + if !found { + return errors.New("the edit did not add the server") + } + same, err = sameTOML(got, entry) + if err != nil { + return err + } + if !same { + return errors.New("the edit wrote a different server") + } + return nil +} + +// sameTOML compares two tables by their TOML text. JSON has no inf or nan, which +// TOML allows in any table, so the comparison is made in TOML. +func sameTOML(a, b map[string]any) (bool, error) { + 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. +func withoutServer(doc map[string]any, root []string, name string) map[string]any { + out := maps.Clone(doc) + if len(root) == 0 { + delete(out, name) + return out + } + child, ok := out[root[0]].(map[string]any) + if !ok { + return out + } + rest := withoutServer(child, root[1:], name) + if len(rest) == 0 { + delete(out, root[0]) + } else { + out[root[0]] = rest + } + return out +} + +// defined reports whether the key path root exists in the document. A table on the +// way that is not a table is taken as defined, since it blocks the path. +func (d *tomlDoc) defined(root []string) bool { + _, found := d.node(root) + return found +} + +// 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(tree); err != nil { + return nil, err + } + 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 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 { + 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...) + } + res = append(res, []byte(nl)) + } + res = append(res, block...) + return append(res, out[end:]...) +} + +// 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."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) + closing := "]" + switch { + case strings.HasPrefix(s, "[["): + s, closing = s[2:], "]]" + case strings.HasPrefix(s, "["): + s = s[1:] + default: + return nil, false + } + 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 + } + 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, "#") +} + +// 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..96e720a244a --- /dev/null +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -0,0 +1,354 @@ +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))) +} + +// 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 + 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, filepath.ToSlash(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))) + }) + } +} + +// 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" + 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))) +} + +// 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)}} + 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) +} + +// 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 or a blank first +// line. +func requireTidyTOML(t *testing.T, got string) { + t.Helper() + require.NotContains(t, got, "\n\n\n", "no double blank line") + require.False(t, strings.HasPrefix(got, "\n"), "no blank first line") +} + +// 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]\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{"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{"DATABRICKS_CONFIG_FILE": "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{"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, `DATABRICKS_CONFIG_FILE = "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, "DATABRICKS_CONFIG_FILE") + 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))) + }) +} + +// 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") + 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))) +} + +// 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") + 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))) +} + +// 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 { + 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) + }) + } +} + +// 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/unmanaged.go b/libs/aitools/mcp/agentconfig/unmanaged.go new file mode 100644 index 00000000000..a4aad7e6ffe --- /dev/null +++ b/libs/aitools/mcp/agentconfig/unmanaged.go @@ -0,0 +1,26 @@ +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 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" { + return true + } + // 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 new file mode 100644 index 00000000000..3c4f2f4590c --- /dev/null +++ b/libs/aitools/mcp/agentconfig/unmanaged_test.go @@ -0,0 +1,60 @@ +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: "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"}, + 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..b1b7ad26bce --- /dev/null +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -0,0 +1,411 @@ +// Package agentconfig reads and writes the user-scope MCP server entries of +// 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" + "fmt" + "io/fs" + "maps" + "os" + "path/filepath" + "reflect" + "slices" + "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") +) + +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) + // 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 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 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 +// 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 its other keys +// are kept. A foreign entry is replaced only when force is set. The file is left +// alone when the entry already matches. cmd must be the databricks binary, or +// the entry could never be recognized as ours. It returns the config file path. +func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []string, env map[string]string, force bool) (string, Status, error) { + if err := checkName(name); err != nil { + return "", "", err + } + 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.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) { + return "", "", fmt.Errorf("server %q: args must start with aitools mcp serve", name) + } + path, err := spec.Path(ctx) + if err != nil { + return "", "", err + } + doc, err := load(spec, path) + if err != nil { + return path, "", err + } + root := rootPath(spec) + current, found, err := doc.entry(root, name) + if err != nil { + return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) + } + + keys := ownedKeys(spec) + var status Status + switch { + case !found: + status = StatusAdded + 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.put(root, name, want, nil) + default: + 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 + } + if same { + return path, StatusUnchanged, nil + } + status = StatusUpdated + err = doc.put(root, name, merged, keys) + } + if err != nil { + return path, "", fmt.Errorf("%s: %w", filepath.ToSlash(path), err) + } + if err := save(path, doc); err != nil { + return path, "", err + } + 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 + } + 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", filepath.ToSlash(path), err) + } + if !found { + return false, nil + } + if !mcpconfig.IsDatabricksEntry(current) { + return false, fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) + } + 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 { + 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", filepath.ToSlash(path), err) + } + 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) + } + 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", filepath.ToSlash(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", filepath.ToSlash(path), ErrConfigInvalid, err) + } + return doc, nil +} + +// save writes doc to path atomically. The file keeps its mode; a new file gets +// 0600 and a new directory 0700, because agent configs can hold auth state. +func save(path string, doc document) error { + data, err := doc.bytes() + if err != nil { + return fmt.Errorf("serialize %s: %w", filepath.ToSlash(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. A link whose target does not + // exist is refused: there is no target to write. + target := path + 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", filepath.ToSlash(path)) + } + default: + return fmt.Errorf("resolve %s: %w", filepath.ToSlash(path), err) + } + if err := atomicfile.Write(target, data, perm, atomicfile.MkDir(0o700)); err != nil { + return fmt.Errorf("write %s: %w", filepath.ToSlash(path), err) + } + return nil +} + +// 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 { + return data + } + return append(append([]byte{}, utf8BOM...), data...) +} + +// 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 keys { + if v, ok := entry[key]; ok { + out[key] = v + } + } + return out +} + +// 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 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, 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 +// 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 := 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 new file mode 100644 index 00000000000..f0ab74fb6e2 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -0,0 +1,786 @@ +package agentconfig + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "runtime" + "strings" + "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/databricks/cli/libs/testdiff" + "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 set to empty so the test does not depend on the developer's shell. +func testHome(t *testing.T) context.Context { + 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"} { + 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 { + 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, + }, + } +} + +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 +} + +// 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) { + 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) + 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) + 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, mcpconfig.IsDatabricksEntry(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, filepath.ToSlash(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, mcpconfig.IsDatabricksEntry(entries[testName])) +} + +// 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 + 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 +} + +// 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) { + 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 the databricks binary") + _, 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) { + 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()) + 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) { + 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) +} + +// 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") + } + 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 { + 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])) + }) + } +} + +// 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) { + 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) + }) + } +} + +// 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) + require.ErrorContains(t, err, "must be the databricks binary") + + _, _, err = Upsert(t.Context(), a, testName, testCmd, []string{"aitools", "serve"}, nil, false) + require.ErrorContains(t, err, "args must start with aitools mcp serve") + assert.NotContains(t, err.Error(), "binary") +} + +// 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) + 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") +} + +// 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" + 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"]) +} + +// 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) + 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"]) +} + +// 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 := `{ + "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))) +} + +// 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{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{pinnedEnv: "/cfg"}, 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))) +} + +// 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" + 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"]) +} + +// 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) + 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))) + }) + } +} + +// 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) + 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()) +} + +// 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":[]}}}` + 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))) +} + +// 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))) +} diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go new file mode 100644 index 00000000000..9eb80bab97d --- /dev/null +++ b/libs/aitools/mcpconfig/spec.go @@ -0,0 +1,113 @@ +// 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 +} + +// 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. +func IsDatabricksEntry(entry map[string]any) bool { + argv, ok := Argv(entry) + if !ok { + return false + } + args := argv[1:] + 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. +func IsBinary(cmd string, names ...string) bool { + base := cmd[strings.LastIndexAny(cmd, `/\`)+1:] + return slices.ContainsFunc(names, func(name string) bool { + return strings.EqualFold(base, 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 { + 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 new file mode 100644 index 00000000000..c8483918cde --- /dev/null +++ b/libs/aitools/mcpconfig/spec_test.go @@ -0,0 +1,75 @@ +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}, + {"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}, + {"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)) + }) + } +} + +func TestValidServerName(t *testing.T) { + assert.True(t, ValidServerName("genie_one-mcp1")) + 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