From 8cca61be2f3d047c15fd3df972d5089d4218635e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 14:56:16 +0000 Subject: [PATCH] Add and remove agent MCP entries through the agent's own CLI aitools mcp add and remove now change the Claude Code, Codex CLI, Gemini CLI and Copilot CLI entries through each agent's own mcp commands, with a read-back of the agent's config file. The agent CLI runner, resolver and stderr helper live in agents, and the installer's plugin probe reuses agents.ResolveBinary. The installer keeps its timeout-aware runner and stderr helper. Co-authored-by: Isaac --- acceptance/acceptance_test.go | 64 +- .../mcp/add-remove-agent-clis/.databrickscfg | 3 + .../mcp/add-remove-agent-clis/out.test.toml | 1 + .../mcp/add-remove-agent-clis/output.txt | 389 ++++++++++ .../aitools/mcp/add-remove-agent-clis/script | 27 + .../mcp/add-remove-agent-clis/test.toml | 4 + acceptance/cmd/aitools/script.prepare | 16 + acceptance/cmd/aitools/test.toml | 2 + acceptance/internal/fakeagent/main.go | 189 +++++ cmd/aitools/mcp_add_test.go | 1 + cmd/aitools/mcp_remove.go | 2 +- libs/aitools/agents/agents.go | 162 ++++- libs/aitools/agents/agents_test.go | 100 +++ libs/aitools/agents/cli.go | 25 + libs/aitools/agents/cli_test.go | 24 + libs/aitools/agents/detect.go | 18 +- libs/aitools/agents/detect_test.go | 14 +- libs/aitools/installer/plugin.go | 25 +- libs/aitools/installer/plugin_test.go | 45 +- libs/aitools/installer/plugin_windows_test.go | 6 +- libs/aitools/mcp/agentconfig/cli.go | 286 ++++++++ libs/aitools/mcp/agentconfig/cli_test.go | 674 ++++++++++++++++++ libs/aitools/mcp/agentconfig/cli_unix_test.go | 41 ++ libs/aitools/mcp/agentconfig/writer.go | 67 +- libs/aitools/mcp/agentconfig/writer_test.go | 4 +- libs/aitools/mcpconfig/spec.go | 12 + libs/process/opts.go | 38 + libs/process/opts_test.go | 27 + 28 files changed, 2162 insertions(+), 104 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/add-remove-agent-clis/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/add-remove-agent-clis/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/add-remove-agent-clis/output.txt create mode 100644 acceptance/cmd/aitools/mcp/add-remove-agent-clis/script create mode 100644 acceptance/cmd/aitools/mcp/add-remove-agent-clis/test.toml create mode 100644 acceptance/cmd/aitools/script.prepare create mode 100644 acceptance/cmd/aitools/test.toml create mode 100644 acceptance/internal/fakeagent/main.go create mode 100644 libs/aitools/agents/cli.go create mode 100644 libs/aitools/agents/cli_test.go create mode 100644 libs/aitools/mcp/agentconfig/cli.go create mode 100644 libs/aitools/mcp/agentconfig/cli_test.go create mode 100644 libs/aitools/mcp/agentconfig/cli_unix_test.go diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index 74ab59d4419..2f36ba4b059 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -236,8 +236,9 @@ func requirePrerequisites(t *testing.T) bool { // selectedTests, when non-empty, limits the run to those test directories. // skipToolBuilds skips building the tools that the selected tests do not use -// (the databricks-bundles wheel, yamlfmt); it must stay false for a -// full run. +// (the databricks-bundles wheel, yamlfmt) and the agent CLI stand-ins. Tests +// under acceptance/cmd/aitools then stop at script.prepare, so it must stay +// false for a full run. func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipToolBuilds bool) int { if testdiff.OverwriteMode && !hasRunFilter() { Subset = true @@ -297,24 +298,32 @@ func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipTo buildDir := getBuildDir(t, cwd, runtime.GOOS, runtime.GOARCH) - // Download old CLI versions in parallel, overlapping with the builds below. + // Download old CLI versions and build the agent CLI stand-ins in parallel, overlapping with the builds below. var cli293Path, cli18Path, cli1161Path string - var oldCLIs errgroup.Group + var setup errgroup.Group if !inprocessMode { - oldCLIs.Go(func() (err error) { + setup.Go(func() (err error) { cli293Path, err = downloadCLI(buildDir, "0.293.0") return err }) - oldCLIs.Go(func() (err error) { + setup.Go(func() (err error) { cli18Path, err = downloadCLI(buildDir, "1.8.0") return err }) // CLI version that predates the hashed_fields feature. Used by acceptance/bundle/hashing - oldCLIs.Go(func() (err error) { + setup.Go(func() (err error) { cli1161Path, err = downloadCLI(buildDir, "1.16.1") return err }) } + // The stand-ins for the agent CLIs go first on PATH in every test under acceptance/cmd/aitools (see script.prepare there). + var fakeAgentDir string + if !skipToolBuilds { + setup.Go(func() (err error) { + fakeAgentDir, err = buildFakeAgentCLIs(buildDir) + return err + }) + } vendoredPyPackages, err := filepath.Abs("../libs/vendored_py_packages") require.NoError(t, err) @@ -370,7 +379,7 @@ func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipTo version = resolveLatestVersion(t, buildDir) } // Wait for the old CLI downloads so the same version isn't downloaded concurrently. - require.NoError(t, oldCLIs.Wait()) + require.NoError(t, setup.Wait()) execPath = DownloadCLI(t, buildDir, version) // For a downloaded release the version string is already known. cliVersion = version @@ -402,9 +411,12 @@ func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipTo t.Setenv("SELECTION", selectionPath) repls.SetPath(selectionPath, "[SELECTION]") - if !inprocessMode { - require.NoError(t, oldCLIs.Wait()) + require.NoError(t, setup.Wait()) + if !skipToolBuilds { + t.Setenv("FAKE_AGENT_DIR", fakeAgentDir) + } + if !inprocessMode { t.Setenv("CLI_293", cli293Path) repls.SetPath(cli293Path, "[CLI_293]") @@ -1498,6 +1510,38 @@ func buildSelectionCmd(t *testing.T, buildDir string) string { return execPath } +// buildFakeAgentCLIs builds the stand-in for the claude, codex, gemini and copilot CLIs once, and copies it under each +// name, since a PATH lookup finds a binary by name. It returns the directory that holds them. It takes no *testing.T, +// so that it can run in the errgroup of the builds. +func buildFakeAgentCLIs(buildDir string) (string, error) { + dir := filepath.Join(buildDir, "agent-clis") + if err := os.MkdirAll(dir, 0o755); err != nil { + return "", err + } + built := filepath.Join(dir, "fakeagent"+exeSuffix) + args := []string{"go", "build", "-o", built} + if runtime.GOOS == "windows" { + // See BuildCLI: VCS stamping fails on Windows. + args = append(args, "-buildvcs=false") + } + // The package path goes last: go build reads anything after it as another package. + args = append(args, "./internal/fakeagent") + if out, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil { + return "", fmt.Errorf("building the agent CLI stand-in failed: %w\n%s", err, out) + } + + data, err := os.ReadFile(built) + if err != nil { + return "", err + } + for _, name := range []string{"claude", "codex", "gemini", "copilot"} { + if err := os.WriteFile(filepath.Join(dir, name+exeSuffix), data, 0o755); err != nil { + return "", err + } + } + return dir, nil +} + // readGOFIPS140 returns the GOFIPS140 version the Taskfile pins for `task` // builds; the release pipeline pins the same value independently in // .goreleaser.yaml. diff --git a/acceptance/cmd/aitools/mcp/add-remove-agent-clis/.databrickscfg b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/add-remove-agent-clis/out.test.toml b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-remove-agent-clis/output.txt b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/output.txt new file mode 100644 index 00000000000..f7697cc72dc --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/output.txt @@ -0,0 +1,389 @@ + +=== add goes through each agent's own CLI: the stand-ins are the only agent CLIs on PATH + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex,gemini,copilot --output json +{ + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "scope": "global", + "servers": [ + { + "name": "genie_one_mcp-logfood", + "service": "system.ai.genie_one_mcp", + "sign_in_required": false, + "agents": [ + { + "name": "claude-code", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.claude.json" + }, + { + "name": "codex", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.codex/config.toml" + }, + { + "name": "gemini", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.gemini/settings.json" + }, + { + "name": "copilot", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.copilot/mcp-config.json" + } + ] + }, + { + "name": "github-logfood", + "service": "system.ai.github", + "sign_in_required": true, + "agents": [ + { + "name": "claude-code", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.claude.json" + }, + { + "name": "codex", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.codex/config.toml" + }, + { + "name": "gemini", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.gemini/settings.json" + }, + { + "name": "copilot", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.copilot/mcp-config.json" + } + ] + } + ], + "error_category": "TYPE_UNSPECIFIED" +} + +=== the argv each agent CLI ran for add + +>>> cat agent-calls.log +claude + mcp + add-json + --scope + user + genie_one_mcp-logfood + {"args":["aitools","mcp","serve","--profile","logfood","--service","system.ai.genie_one_mcp","--agent","claude-code"],"command":"[CLI]","env":{"DATABRICKS_CONFIG_FILE":"[TEST_TMP_DIR]/.databrickscfg"},"type":"stdio"} +codex + mcp + add + genie_one_mcp-logfood + --env + DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg + -- + [CLI] + aitools + mcp + serve + --profile + logfood + --service + system.ai.genie_one_mcp + --agent + codex +gemini + mcp + add + --scope + user + -e + DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg + genie_one_mcp-logfood + [CLI] + -- + aitools + mcp + serve + --profile + logfood + --service + system.ai.genie_one_mcp + --agent + gemini +copilot + mcp + add + genie_one_mcp-logfood + --tools + * + --env + DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg + -- + [CLI] + aitools + mcp + serve + --profile + logfood + --service + system.ai.genie_one_mcp + --agent + copilot +claude + mcp + add-json + --scope + user + github-logfood + {"args":["aitools","mcp","serve","--profile","logfood","--service","system.ai.github","--agent","claude-code"],"command":"[CLI]","env":{"DATABRICKS_CONFIG_FILE":"[TEST_TMP_DIR]/.databrickscfg"},"type":"stdio"} +codex + mcp + add + github-logfood + --env + DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg + -- + [CLI] + aitools + mcp + serve + --profile + logfood + --service + system.ai.github + --agent + codex +gemini + mcp + add + --scope + user + -e + DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg + github-logfood + [CLI] + -- + aitools + mcp + serve + --profile + logfood + --service + system.ai.github + --agent + gemini +copilot + mcp + add + github-logfood + --tools + * + --env + DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg + -- + [CLI] + aitools + mcp + serve + --profile + logfood + --service + system.ai.github + --agent + copilot + +=== the config each agent CLI wrote + +>>> cat home/.claude.json +{ + "mcpServers": { + "genie_one_mcp-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "claude-code" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + }, + "type": "stdio" + }, + "github-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.github", + "--agent", + "claude-code" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + }, + "type": "stdio" + } + } +} + +>>> cat home/.codex/config.toml +[mcp_servers.genie_one_mcp-logfood] +args = ["aitools", "mcp", "serve", "--profile", "logfood", "--service", "system.ai.genie_one_mcp", "--agent", "codex"] +command = "[CLI]" +[mcp_servers.genie_one_mcp-logfood.env] +DATABRICKS_CONFIG_FILE = "[TEST_TMP_DIR]/.databrickscfg" + +[mcp_servers.github-logfood] +args = ["aitools", "mcp", "serve", "--profile", "logfood", "--service", "system.ai.github", "--agent", "codex"] +command = "[CLI]" +[mcp_servers.github-logfood.env] +DATABRICKS_CONFIG_FILE = "[TEST_TMP_DIR]/.databrickscfg" + +>>> cat home/.gemini/settings.json +{ + "mcpServers": { + "genie_one_mcp-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "gemini" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + } + }, + "github-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.github", + "--agent", + "gemini" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + } + } + } +} + +>>> cat home/.copilot/mcp-config.json +{ + "mcpServers": { + "genie_one_mcp-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "copilot" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + }, + "tools": [ + "*" + ], + "type": "local" + }, + "github-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.github", + "--agent", + "copilot" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + }, + "tools": [ + "*" + ], + "type": "local" + } + } +} + +=== remove goes through each agent's CLI too + +>>> [CLI] aitools mcp remove --names github-logfood,genie_one_mcp-logfood +Removed genie_one_mcp-logfood system.ai.genie_one_mcp profile logfood (Claude Code, Codex CLI, GitHub Copilot, Gemini CLI) +Removed github-logfood system.ai.github profile logfood (Claude Code, Codex CLI, GitHub Copilot, Gemini CLI) + +=== the argv each agent CLI ran for remove + +>>> cat agent-calls.log +claude + mcp + remove + --scope + user + genie_one_mcp-logfood +codex + mcp + remove + genie_one_mcp-logfood +copilot + mcp + remove + genie_one_mcp-logfood +gemini + mcp + remove + --scope + user + genie_one_mcp-logfood +claude + mcp + remove + --scope + user + github-logfood +codex + mcp + remove + github-logfood +copilot + mcp + remove + github-logfood +gemini + mcp + remove + --scope + user + github-logfood diff --git a/acceptance/cmd/aitools/mcp/add-remove-agent-clis/script b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/script new file mode 100644 index 00000000000..911e6ce5f55 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/script @@ -0,0 +1,27 @@ +# Isolate HOME so the agent config files land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +# The services of system.ai, created in the fake workspace. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' &> LOG.create-github-logfood + +title "add goes through each agent's own CLI: the stand-ins are the only agent CLIs on PATH" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex,gemini,copilot --output json + +title "the argv each agent CLI ran for add" +trace cat agent-calls.log +rm agent-calls.log + +title "the config each agent CLI wrote" +trace cat home/.claude.json +trace cat home/.codex/config.toml +trace cat home/.gemini/settings.json +trace cat home/.copilot/mcp-config.json + +title "remove goes through each agent's CLI too" +trace $CLI aitools mcp remove --names github-logfood,genie_one_mcp-logfood + +title "the argv each agent CLI ran for remove" +trace cat agent-calls.log diff --git a/acceptance/cmd/aitools/mcp/add-remove-agent-clis/test.toml b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/test.toml new file mode 100644 index 00000000000..7e5e378519e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-remove-agent-clis/test.toml @@ -0,0 +1,4 @@ +# The argv log is not JSON, so on Windows a path in it keeps one native separator after the test directory. +[[Repls]] +Old = '(\[TEST_TMP_DIR\])\\' +New = '$1/' diff --git a/acceptance/cmd/aitools/script.prepare b/acceptance/cmd/aitools/script.prepare new file mode 100644 index 00000000000..de6418dc61b --- /dev/null +++ b/acceptance/cmd/aitools/script.prepare @@ -0,0 +1,16 @@ +# The host's claude, codex, gemini and copilot must not run from any test under acceptance/cmd/aitools. The harness built +# stand-ins for them (see acceptance/internal/fakeagent), and they go first on PATH, so `add` and `remove` reach the +# stand-ins and nothing else. An empty FAKE_AGENT_DIR would put the host's CLIs first on PATH, so the test stops instead. +if [[ -z "${FAKE_AGENT_DIR:-}" ]]; then + echo "FAKE_AGENT_DIR is empty: the agent CLI stand-ins were not built" >&2 + exit 1 +fi +# Each stand-in appends its argv to agent-calls.log, which a test can show with trace. The agent CLIs run in an empty +# directory, not in the test's, so the log path is absolute. Go opens it, so on msys it is in the mixed form. +export FAKE_AGENT_LOG="$PWD/agent-calls.log" +if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "win32" ]]; then + export PATH="$(cygpath -u "$FAKE_AGENT_DIR"):$PATH" + export FAKE_AGENT_LOG="$(cygpath -m "$FAKE_AGENT_LOG")" +else + export PATH="$FAKE_AGENT_DIR:$PATH" +fi diff --git a/acceptance/cmd/aitools/test.toml b/acceptance/cmd/aitools/test.toml new file mode 100644 index 00000000000..a10d6bc59f1 --- /dev/null +++ b/acceptance/cmd/aitools/test.toml @@ -0,0 +1,2 @@ +# The argv log of the agent CLI stand-ins (script.prepare) is shown by the tests that need it, not compared as a new file. +Ignore = ["agent-calls.log"] diff --git a/acceptance/internal/fakeagent/main.go b/acceptance/internal/fakeagent/main.go new file mode 100644 index 00000000000..c3c6b497314 --- /dev/null +++ b/acceptance/internal/fakeagent/main.go @@ -0,0 +1,189 @@ +// Command fakeagent stands in for the claude, codex, gemini and copilot CLIs in the aitools mcp acceptance tests. The +// harness builds it once per run and copies it under each agent's name, and a test puts those on PATH, so no test runs +// a CLI from the host. +// +// Run as an agent, it appends its argv to the file named by $FAKE_AGENT_LOG, then makes the change through agentconfig's +// direct writer, which edits the agent's user-scope config file as the agent's CLI would. The stand-in clears its PATH +// first, so the writer finds no agent CLI there and writes the file itself, in the writer's layout. Only the add and +// remove commands that the agents' CLI wrappers use are accepted; any other command fails, so a test that runs an +// unexpected command fails too. +package main + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp/agentconfig" + "github.com/databricks/cli/libs/env" +) + +// agentOf maps the name a stand-in runs under to the agent whose config it edits. +var agentOf = map[string]string{ + "claude": agents.NameClaudeCode, + "codex": agents.NameCodex, + "gemini": agents.NameGemini, + "copilot": agents.NameCopilot, +} + +func main() { + ctx := context.Background() + program := strings.TrimSuffix(strings.ToLower(filepath.Base(os.Args[0])), ".exe") + args := os.Args[1:] + if err := logArgv(ctx, append([]string{program}, args...)); err != nil { + fail(err) + } + if err := run(ctx, program, args); err != nil { + fail(err) + } +} + +// fail reports err on stderr and exits 1, as a CLI does when a command fails. +func fail(err error) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +} + +// logArgv appends one invocation to the log: the program on a line of its own, then each argument on a line of its own, +// indented. The arguments are not quoted, so a path or JSON in an argument reads as it was written. +func logArgv(ctx context.Context, argv []string) error { + f, err := os.OpenFile(env.Get(ctx, "FAKE_AGENT_LOG"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) + if err != nil { + return err + } + var b strings.Builder + b.WriteString(argv[0] + "\n") + for _, arg := range argv[1:] { + b.WriteString("\t" + arg + "\n") + } + _, err = f.WriteString(b.String()) + return errors.Join(err, f.Close()) +} + +// run dispatches one invocation to the handler of the agent that program stands for. It empties PATH first, so that the +// writer does not run a CLI. +func run(ctx context.Context, program string, args []string) error { + name, ok := agentOf[program] + if !ok { + return fmt.Errorf("fakeagent: no agent CLI named %q", program) + } + // The writer runs an agent CLI that it finds on PATH. The stand-ins are on PATH, so it must find none. + if err := os.Setenv("PATH", ""); err != nil { + return err + } + agent := agents.ByName(name) + switch program { + case "claude": + return claude(ctx, agent, args) + case "gemini": + return gemini(ctx, agent, args) + default: + return codex(ctx, agent, program, args) + } +} + +// claude handles `mcp add-json --scope user ` and `mcp remove --scope user `. +func claude(ctx context.Context, agent *agents.Agent, args []string) error { + switch { + case len(args) == 6 && args[0] == "mcp" && args[1] == "add-json" && args[2] == "--scope" && args[3] == "user": + var entry struct { + Command string `json:"command"` + Args []string `json:"args"` + Env map[string]string `json:"env"` + } + if err := json.Unmarshal([]byte(args[5]), &entry); err != nil { + return err + } + return upsert(ctx, agent, args[4], entry.Command, entry.Args, entry.Env) + case len(args) == 5 && args[0] == "mcp" && args[1] == "remove" && args[2] == "--scope" && args[3] == "user": + return remove(ctx, agent, args[4]) + } + return unexpected("claude", args) +} + +// codex handles `mcp add [--env K=V]... -- ...` and `mcp remove `. copilot takes the same +// forms, and `--tools *` after the name. +func codex(ctx context.Context, agent *agents.Agent, program string, args []string) error { + switch { + case len(args) >= 3 && args[0] == "mcp" && args[1] == "add": + name, rest := args[2], args[3:] + if program == "copilot" && len(rest) >= 2 && rest[0] == "--tools" && rest[1] == "*" { + rest = rest[2:] + } + env := map[string]string{} + for len(rest) >= 2 && rest[0] == "--env" { + key, value, err := splitVar(rest[1]) + if err != nil { + return err + } + env[key] = value + rest = rest[2:] + } + if len(rest) < 2 || rest[0] != "--" { + return unexpected(program, args) + } + return upsert(ctx, agent, name, rest[1], rest[2:], env) + case len(args) == 3 && args[0] == "mcp" && args[1] == "remove": + return remove(ctx, agent, args[2]) + } + return unexpected(program, args) +} + +// gemini handles `mcp add --scope user [-e K=V]... -- ...` and `mcp remove --scope user `. +func gemini(ctx context.Context, agent *agents.Agent, args []string) error { + switch { + case len(args) >= 5 && args[0] == "mcp" && args[1] == "add" && args[2] == "--scope" && args[3] == "user": + rest := args[4:] + env := map[string]string{} + for len(rest) >= 2 && rest[0] == "-e" { + key, value, err := splitVar(rest[1]) + if err != nil { + return err + } + env[key] = value + rest = rest[2:] + } + if len(rest) < 3 || rest[2] != "--" { + return unexpected("gemini", args) + } + return upsert(ctx, agent, rest[0], rest[1], rest[3:], env) + case len(args) == 5 && args[0] == "mcp" && args[1] == "remove" && args[2] == "--scope" && args[3] == "user": + return remove(ctx, agent, args[4]) + } + return unexpected("gemini", args) +} + +// upsert adds the server through the direct writer, which writes the entry as the agent's CLI would. +func upsert(ctx context.Context, agent *agents.Agent, name, cmd string, args []string, env map[string]string) error { + _, _, err := agentconfig.Upsert(ctx, agent, name, cmd, args, env, false) + return err +} + +// remove deletes the server from the config file that the agent reads now. +func remove(ctx context.Context, agent *agents.Agent, name string) error { + path, err := agentconfig.Path(ctx, agent) + if err != nil { + return err + } + _, err = agentconfig.RemoveAt(ctx, agent, path, name) + return err +} + +// splitVar splits a KEY=VALUE argument at its first "=". +func splitVar(s string) (string, string, error) { + key, value, ok := strings.Cut(s, "=") + if !ok { + return "", "", fmt.Errorf("fakeagent: %q is not KEY=VALUE", s) + } + return key, value, nil +} + +// unexpected is the error for a command that the stand-in does not accept, so that a test which runs one fails. +func unexpected(program string, args []string) error { + return fmt.Errorf("fakeagent: %s does not take %q", program, args) +} diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 841feaf986b..0d364d9d64a 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1336,6 +1336,7 @@ func TestInstallServerWritesOnlyAgentsInstalledForMCP(t *testing.T) { {name: "gemini with projects.json", agent: "gemini", dirs: []string{".gemini"}, files: map[string]string{".gemini/projects.json": "{}"}, written: ".gemini/settings.json"}, {name: "gemini directory alone is refused", agent: "gemini", dirs: []string{".gemini/antigravity"}, absent: ".gemini/settings.json"}, {name: "antigravity from its own directory", agent: "antigravity", dirs: []string{".gemini/antigravity"}, written: ".gemini/config/mcp_config.json"}, + // The CLI on PATH is a stand-in that exits 0 and writes nothing, so the direct writer writes the config. {name: "cli on PATH", agent: "codex", onPath: true, written: ".codex/config.toml"}, } for _, tt := range tests { diff --git a/cmd/aitools/mcp_remove.go b/cmd/aitools/mcp_remove.go index eba147d5ffe..6c62b7d8eae 100644 --- a/cmd/aitools/mcp_remove.go +++ b/cmd/aitools/mcp_remove.go @@ -364,7 +364,7 @@ func removeFromAgent(ctx context.Context, files *agentFiles, e mcp.Entry, a *age if !owned { return removeKept, keptReason(e, a, loc.path, loc.entry), nil } - if _, err := agentconfig.RemoveAt(a, loc.path, e.Name); err != nil { + if _, err := agentconfig.RemoveAt(ctx, a, loc.path, e.Name); err != nil { return 0, "", err } return removeDeleted, "", nil diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 48c45758ec7..8d8cd25ae4b 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -2,18 +2,32 @@ package agents import ( "context" + "encoding/json" "errors" "fmt" "io/fs" + "maps" "os" "path/filepath" "runtime" + "slices" "strings" "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/env" ) +// The words that the agent CLIs' MCP commands share. The user-scope commands name the user scope, since the default +// scope of each of them is another one. +const ( + cliMCP = "mcp" + cliAdd = "add" + cliRemove = "remove" + cliScopeFlag = "--scope" + cliUserScope = "user" + cliEndOfFlags = "--" +) + // PluginSpec describes the databricks plugin for an agent. A non-nil // Agent.Plugin means the agent has a databricks plugin the CLI can install // headlessly (Claude Code, Codex, Copilot); a nil Plugin means raw skill files @@ -63,11 +77,10 @@ type Agent struct { // 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 - // mcpHomeEnv names the variable that moves the MCP config file out of ConfigDir: CODEX_HOME for Codex - // (https://learn.chatgpt.com/docs/config-file/config-advanced) and COPILOT_HOME for Copilot CLI - // (https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference). - // Empty when the file lives in ConfigDir, or when ConfigDir itself follows the variable. - mcpHomeEnv string + // mcpHomeOnly is set when the MCP config file lives in the directory that MCP.HomeEnv names, outside ConfigDir: Codex + // (CODEX_HOME) and Copilot CLI (COPILOT_HOME). It is unset when the file lives in ConfigDir, or when ConfigDir itself + // follows the variable. + mcpHomeOnly bool // 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, @@ -99,11 +112,11 @@ func (a *Agent) Detected(ctx context.Context) bool { return err == nil && HasDatabricksSkillsIn(skillsDir) } -// InstalledForMCP reports whether the agent is installed for MCP servers. When mcpHomeEnv is set, the directory it names -// is the only signal: the agent's own directory may exist while the MCP home does not, and writing the MCP config would -// create that home. Otherwise Detected, HasBinary, or the MCP config file counts. +// InstalledForMCP reports whether the agent is installed for MCP servers. When mcpHomeOnly is set and MCP.HomeEnv is set, +// the directory it names is the only signal: the agent's own directory may exist while the MCP home does not, and writing +// the MCP config would create that home. Otherwise Detected, HasBinary, or the MCP config file counts. func (a *Agent) InstalledForMCP(ctx context.Context) (bool, error) { - if a.mcpHomeEnv != "" && env.Get(ctx, a.mcpHomeEnv) != "" { + if a.mcpHomeOnly && env.Get(ctx, a.MCP.HomeEnv) != "" { path, err := a.MCP.Path(ctx) if err != nil { return false, err @@ -229,10 +242,13 @@ var Registry = []*Agent{ Plugin: claudePlugin(), pluginInstalls: claudePluginInstalls, MCP: &mcpconfig.Spec{ - Path: claudeMCPPath, - Format: mcpconfig.FormatJSON, - Root: mcpServersKey, - Entry: claudeMCPEntry, + Path: claudeMCPPath, + Format: mcpconfig.FormatJSON, + Root: mcpServersKey, + Entry: claudeMCPEntry, + AddArgs: claudeMCPAddArgs, + RemoveArgs: claudeMCPRemoveArgs, + HomeEnv: claudeHomeEnv, }, }, { @@ -259,12 +275,15 @@ var Registry = []*Agent{ Binary: "codex", Plugin: databricksPlugin(), MCP: &mcpconfig.Spec{ - Path: codexMCPPath, - Format: mcpconfig.FormatTOML, - Root: "mcp_servers", - Entry: stdioEntry, + Path: codexMCPPath, + Format: mcpconfig.FormatTOML, + Root: "mcp_servers", + Entry: stdioEntry, + AddArgs: codexMCPAddArgs, + RemoveArgs: mcpRemoveArgs, + HomeEnv: codexHomeEnv, }, - mcpHomeEnv: codexHomeEnv, + mcpHomeOnly: true, }, { Name: NameOpenCode, @@ -288,12 +307,15 @@ var Registry = []*Agent{ Binary: "copilot", Plugin: databricksPlugin(), MCP: &mcpconfig.Spec{ - Path: copilotMCPPath, - Format: mcpconfig.FormatJSON, - Root: mcpServersKey, - Entry: copilotMCPEntry, + Path: copilotMCPPath, + Format: mcpconfig.FormatJSON, + Root: mcpServersKey, + Entry: copilotMCPEntry, + AddArgs: copilotMCPAddArgs, + RemoveArgs: mcpRemoveArgs, + HomeEnv: copilotHomeEnv, }, - mcpHomeEnv: copilotHomeEnv, + mcpHomeOnly: true, }, { Name: NameAntigravity, @@ -331,10 +353,13 @@ var Registry = []*Agent{ // 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, + Path: geminiMCPPath, + Format: mcpconfig.FormatJSON, + Root: mcpServersKey, + Entry: stdioEntry, + AddArgs: geminiMCPAddArgs, + RemoveArgs: geminiMCPRemoveArgs, + HomeEnv: geminiHomeEnv, }, }, { @@ -365,7 +390,7 @@ var Registry = []*Agent{ // claudeConfigDir follows Claude Code's configuration directory override. // See https://code.claude.com/docs/en/env-vars for CLAUDE_CONFIG_DIR. func claudeConfigDir(ctx context.Context) (string, error) { - if dir := env.Get(ctx, "CLAUDE_CONFIG_DIR"); dir != "" { + if dir := env.Get(ctx, claudeHomeEnv); dir != "" { return dir, nil } return homeSubdir(".claude")(ctx) @@ -401,7 +426,7 @@ func piConfigDir(ctx context.Context) (string, error) { // reads them under a relocated home (e.g. ucode). // https://github.com/google-gemini/gemini-cli/blob/main/docs/reference/configuration.md func geminiConfigDir(ctx context.Context) (string, error) { - root := env.Get(ctx, "GEMINI_CLI_HOME") + root := env.Get(ctx, geminiHomeEnv) if root == "" { home, err := env.UserHomeDir(ctx) if err != nil { @@ -469,7 +494,7 @@ func gooseConfigDir(ctx context.Context) (string, error) { // 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 != "" { + if dir := env.Get(ctx, claudeHomeEnv); dir != "" { return filepath.Join(dir, ".claude.json"), nil } return homeSubdir(".claude.json")(ctx) @@ -484,10 +509,85 @@ func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[strin return map[string]any{"type": "stdio", "command": cmd, "args": args, "env": vars} } -// codexHomeEnv and copilotHomeEnv move the MCP config files of Codex and Copilot CLI. +// claudeMCPAddArgs passes the entry as JSON to `claude mcp add-json`, so the entry Claude writes is the one +// claudeMCPEntry builds. The command is described at https://code.claude.com/docs/en/mcp. +func claudeMCPAddArgs(name, cmd string, args []string, vars map[string]string) []string { + // Cannot fail: the entry holds only strings and maps of strings. + raw, _ := json.Marshal(claudeMCPEntry(cmd, args, vars)) + return []string{cliMCP, "add-json", cliScopeFlag, cliUserScope, name, string(raw)} +} + +// claudeMCPRemoveArgs removes the server from the user scope, which the remove command does not default to. +func claudeMCPRemoveArgs(name string) []string { + return []string{cliMCP, cliRemove, cliScopeFlag, cliUserScope, name} +} + +// codexMCPAddArgs follows `codex mcp add --env K=V -- `, the form in the Codex MCP documentation +// at https://learn.chatgpt.com/docs/extend/mcp?surface=cli. The -- ends codex's own flags, so an argument that starts +// with a dash reaches the server unchanged. +func codexMCPAddArgs(name, cmd string, args []string, vars map[string]string) []string { + return envFlagAddArgs(name, cmd, args, vars) +} + +// copilotMCPAddArgs follows `copilot mcp add --tools * --env K=V -- `, the form in Copilot CLI's +// documentation at https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/add-mcp-servers. The docs +// name no scope flag. The config directory is COPILOT_HOME when it is set, as +// https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference describes; copilotMCPPath +// resolves the same, and the read-back after the add checks the file. The docs also make * the default; the flag names it, +// so the tools of the entry do not depend on that default. +func copilotMCPAddArgs(name, cmd string, args []string, vars map[string]string) []string { + return envFlagAddArgs(name, cmd, args, vars, "--tools", "*") +} + +// mcpRemoveArgs removes server name from the user config, which is where Codex and Copilot CLI keep the servers that +// the remove command reads by default. +func mcpRemoveArgs(name string) []string { + return []string{cliMCP, cliRemove, name} +} + +// envFlagAddArgs is the `mcp add` argv shared by Codex and Copilot CLI: the name, then flags, one --env flag per +// variable, then -- and the command line. +func envFlagAddArgs(name, cmd string, args []string, vars map[string]string, flags ...string) []string { + argv := append([]string{cliMCP, cliAdd, name}, flags...) + for _, key := range slices.Sorted(maps.Keys(vars)) { + argv = append(argv, "--env", key+"="+vars[key]) + } + argv = append(argv, cliEndOfFlags, cmd) + return append(argv, args...) +} + +// geminiMCPAddArgs follows `gemini mcp add --scope user -e K=V -- `, as the gemini-cli source +// at https://github.com/google-gemini/gemini-cli/blob/main/packages/cli/src/commands/mcp/add.ts parses it, and the +// command is described at https://geminicli.com/docs/tools/mcp-server/. Gemini adds to the project scope unless told +// otherwise. The -- ends its option parsing, so an argument of the server that Gemini also knows, such as --help, +// reaches the server. Its -e splits a value at every '=' and keeps two parts, so a value with '=' is refused: the argv +// is nil, and the direct writer makes the change. +func geminiMCPAddArgs(name, cmd string, args []string, vars map[string]string) []string { + argv := []string{cliMCP, cliAdd, cliScopeFlag, cliUserScope} + for _, key := range slices.Sorted(maps.Keys(vars)) { + if strings.Contains(vars[key], "=") { + return nil + } + argv = append(argv, "-e", key+"="+vars[key]) + } + argv = append(argv, name, cmd, cliEndOfFlags) + return append(argv, args...) +} + +// geminiMCPRemoveArgs removes the server from the user scope, because removing from the project scope is the default. +func geminiMCPRemoveArgs(name string) []string { + return []string{cliMCP, cliRemove, cliScopeFlag, cliUserScope, name} +} + +// The variables that move the agents' config directories. Codex and Copilot CLI keep their MCP config files under the +// directory that their variable names, outside ConfigDir: see +// https://learn.chatgpt.com/docs/config-file/config-advanced and +// https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference. const ( + claudeHomeEnv = "CLAUDE_CONFIG_DIR" codexHomeEnv = "CODEX_HOME" copilotHomeEnv = "COPILOT_HOME" + geminiHomeEnv = "GEMINI_CLI_HOME" ) // codexMCPPath returns Codex's config.toml, which honors CODEX_HOME. diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index 7ab21aea40f..de2ed6d122c 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -2,6 +2,7 @@ package agents import ( "context" + "encoding/json" "os" "path/filepath" "runtime" @@ -305,3 +306,102 @@ func TestInstalledForMCP(t *testing.T) { assert.True(t, installed(t, NameAntigravity)) }) } + +// Each command gets the Windows path of the databricks binary as one argument, with spaces, and the server arguments +// with a space inside one of them. The argv must pass each value through as one element. +var ( + testMCPCmd = `C:\Program Files\Databricks\databricks.exe` + testMCPArgs = []string{"aitools", "mcp", "serve", "--profile", "my profile", "--service", "system.ai.github", "--agent", "codex"} +) + +func TestMCPAddArgsPerAgent(t *testing.T) { + pin := map[string]string{"DATABRICKS_CONFIG_FILE": `C:\Users\me\.databrickscfg`} + tests := []struct { + name string + agent string + env map[string]string + want []string + }{ + { + name: "codex without env", + agent: NameCodex, + want: append([]string{"mcp", "add", "github", "--", testMCPCmd}, testMCPArgs...), + }, + { + name: "codex pins the config file as --env", + agent: NameCodex, + env: pin, + want: append([]string{"mcp", "add", "github", "--env", `DATABRICKS_CONFIG_FILE=C:\Users\me\.databrickscfg`, "--", testMCPCmd}, testMCPArgs...), + }, + { + name: "copilot without env", + agent: NameCopilot, + want: append([]string{"mcp", "add", "github", "--tools", "*", "--", testMCPCmd}, testMCPArgs...), + }, + { + name: "copilot pins the config file as --env", + agent: NameCopilot, + env: pin, + want: append([]string{"mcp", "add", "github", "--tools", "*", "--env", `DATABRICKS_CONFIG_FILE=C:\Users\me\.databrickscfg`, "--", testMCPCmd}, testMCPArgs...), + }, + { + name: "gemini without env", + agent: NameGemini, + want: append([]string{"mcp", "add", "--scope", "user", "github", testMCPCmd, "--"}, testMCPArgs...), + }, + { + name: "gemini pins the config file as -e", + agent: NameGemini, + env: pin, + want: append([]string{"mcp", "add", "--scope", "user", "-e", `DATABRICKS_CONFIG_FILE=C:\Users\me\.databrickscfg`, "github", testMCPCmd, "--"}, testMCPArgs...), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ByName(tt.agent).MCP.AddArgs("github", testMCPCmd, testMCPArgs, tt.env) + assert.Equal(t, tt.want, got) + }) + } +} + +func TestGeminiMCPAddArgsRefusesEnvValueWithEquals(t *testing.T) { + // Gemini's -e keeps only the first two parts of KEY=VALUE, so the direct writer makes this change. + got := ByName(NameGemini).MCP.AddArgs("github", testMCPCmd, testMCPArgs, map[string]string{"DATABRICKS_CONFIG_FILE": "C:\\a=b"}) + assert.Nil(t, got) +} + +func TestClaudeMCPAddArgsPassesTheEntryAsJSON(t *testing.T) { + pin := map[string]string{"DATABRICKS_CONFIG_FILE": `C:\Users\me\.databrickscfg`} + got := ByName(NameClaudeCode).MCP.AddArgs("github", testMCPCmd, testMCPArgs, pin) + require.Len(t, got, 6) + assert.Equal(t, []string{"mcp", "add-json", "--scope", "user", "github"}, got[:5]) + + want, err := json.Marshal(claudeMCPEntry(testMCPCmd, testMCPArgs, pin)) + require.NoError(t, err) + assert.JSONEq(t, string(want), got[5]) +} + +func TestMCPRemoveArgsPerAgent(t *testing.T) { + tests := []struct { + agent string + want []string + }{ + {NameClaudeCode, []string{"mcp", "remove", "--scope", "user", "github"}}, + {NameCodex, []string{"mcp", "remove", "github"}}, + {NameGemini, []string{"mcp", "remove", "--scope", "user", "github"}}, + {NameCopilot, []string{"mcp", "remove", "github"}}, + } + for _, tt := range tests { + t.Run(tt.agent, func(t *testing.T) { + assert.Equal(t, tt.want, ByName(tt.agent).MCP.RemoveArgs("github")) + }) + } +} + +func TestOtherAgentsHaveNoCLIArgs(t *testing.T) { + for _, name := range []string{NameCursor, NameOpenCode, NameAntigravity} { + spec := ByName(name).MCP + assert.Nil(t, spec.AddArgs, name) + assert.Nil(t, spec.RemoveArgs, name) + } +} diff --git a/libs/aitools/agents/cli.go b/libs/aitools/agents/cli.go new file mode 100644 index 00000000000..56d85a3de76 --- /dev/null +++ b/libs/aitools/agents/cli.go @@ -0,0 +1,25 @@ +package agents + +import ( + "fmt" + "os/exec" +) + +// LookPath resolves a binary on PATH. It is a package-level var so tests can +// inject a fake resolver without depending on the host PATH. It mirrors the +// exec.LookPath precedent in libs/python/detect.go. +var LookPath = exec.LookPath + +// ResolveBinary resolves the agent's CLI binary to an absolute path. It refuses +// a binary that resolves only relative to the current directory (exec.ErrDot), +// so a malicious ./claude is never executed. +func ResolveBinary(agent *Agent) (string, error) { + if agent.Binary == "" { + return "", fmt.Errorf("%s has no CLI binary", agent.DisplayName) + } + path, err := LookPath(agent.Binary) + if err != nil { + return "", fmt.Errorf("could not resolve %s on PATH: %w", agent.Binary, err) + } + return path, nil +} diff --git a/libs/aitools/agents/cli_test.go b/libs/aitools/agents/cli_test.go new file mode 100644 index 00000000000..badf0b4a60d --- /dev/null +++ b/libs/aitools/agents/cli_test.go @@ -0,0 +1,24 @@ +package agents + +import ( + "os/exec" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestResolveBinaryNotOnPath(t *testing.T) { + stubLookPath(t) + _, err := ResolveBinary(&Agent{Binary: "claude", DisplayName: "Claude Code"}) + assert.ErrorIs(t, err, exec.ErrNotFound) +} + +func TestResolveBinaryRefusesDotRelativeBinary(t *testing.T) { + orig := LookPath + LookPath = func(name string) (string, error) { return filepath.Join(".", name), exec.ErrDot } + t.Cleanup(func() { LookPath = orig }) + + _, err := ResolveBinary(&Agent{Binary: "claude", DisplayName: "Claude Code"}) + assert.ErrorIs(t, err, exec.ErrDot) +} diff --git a/libs/aitools/agents/detect.go b/libs/aitools/agents/detect.go index 53ae8d085f8..30b274ffab8 100644 --- a/libs/aitools/agents/detect.go +++ b/libs/aitools/agents/detect.go @@ -4,17 +4,11 @@ import ( "context" "errors" "fmt" - "os/exec" "time" "github.com/databricks/cli/libs/process" ) -// lookPath resolves a binary on PATH. It is a package-level var so tests can -// inject a fake resolver without depending on the host PATH. It mirrors the -// exec.LookPath precedent in libs/python/detect.go. -var lookPath = exec.LookPath - // pluginProbeTimeout bounds the ` plugin --help` capability probe so a // hung agent CLI cannot stall install. It matches the probe timeout in // libs/aitools/installer, which leaves headroom for a slow agent CLI cold start. @@ -47,7 +41,7 @@ func (a *Agent) HasBinary(_ context.Context) bool { if a.Binary == "" { return false } - _, err := lookPath(a.Binary) + _, err := LookPath(a.Binary) return err == nil } @@ -100,13 +94,11 @@ func (a *Agent) ProbePlugin(ctx context.Context) error { return ErrPluginUnsupported } - // exec.LookPath returns exec.ErrDot (along with the path) when the binary - // resolves only relative to the current directory. Returning on any error, - // including ErrDot, means we never execute a binary resolved from cwd (a - // malicious ./claude dropped there is refused, not run). - path, err := lookPath(a.Binary) + // ResolveBinary refuses a binary that resolves only relative to the current directory (exec.ErrDot), so a + // malicious ./claude dropped there is never executed. + path, err := ResolveBinary(a) if err != nil { - return fmt.Errorf("could not resolve %s on PATH: %w", a.Binary, err) + return err } cctx, cancel := context.WithTimeout(ctx, pluginProbeTimeout) diff --git a/libs/aitools/agents/detect_test.go b/libs/aitools/agents/detect_test.go index dfb7a8c6ec1..0b77b8e6532 100644 --- a/libs/aitools/agents/detect_test.go +++ b/libs/aitools/agents/detect_test.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/require" ) -// stubLookPath replaces the package lookPath with one that resolves only the +// stubLookPath replaces the package LookPath with one that resolves only the // named binaries, restoring the original when the test finishes. func stubLookPath(t *testing.T, onPath ...string) { t.Helper() @@ -22,14 +22,14 @@ func stubLookPath(t *testing.T, onPath ...string) { for _, name := range onPath { set[name] = true } - orig := lookPath - lookPath = func(name string) (string, error) { + orig := LookPath + LookPath = func(name string) (string, error) { if set[name] { return filepath.Join("/usr/bin", name), nil } return "", exec.ErrNotFound } - t.Cleanup(func() { lookPath = orig }) + t.Cleanup(func() { LookPath = orig }) } // configDir returns a closure usable as Agent.ConfigDir. When create is true a @@ -358,9 +358,9 @@ func TestProbePluginBinaryNotOnPath(t *testing.T) { func TestProbePluginRefusesDotRelativeBinary(t *testing.T) { // LookPath returns ErrDot (with a path) when the binary resolves only // relative to cwd. ProbePlugin must refuse to run it. - orig := lookPath - lookPath = func(name string) (string, error) { return filepath.Join(".", name), exec.ErrDot } - t.Cleanup(func() { lookPath = orig }) + orig := LookPath + LookPath = func(name string) (string, error) { return filepath.Join(".", name), exec.ErrDot } + t.Cleanup(func() { LookPath = orig }) ctx, stub := process.WithStub(t.Context()) a := &Agent{Binary: "claude", Plugin: &PluginSpec{}} diff --git a/libs/aitools/installer/plugin.go b/libs/aitools/installer/plugin.go index 51cb0b2fce2..a4244f9c909 100644 --- a/libs/aitools/installer/plugin.go +++ b/libs/aitools/installer/plugin.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "maps" - "os/exec" "path/filepath" "regexp" "strings" @@ -17,10 +16,6 @@ import ( "github.com/databricks/cli/libs/process" ) -// lookPath resolves a binary on PATH. It is a package-level var so tests can -// inject a fake resolver without depending on the host PATH. -var lookPath = exec.LookPath - const ( // pluginProbeTimeout bounds the ` plugin --help` capability check. // Agent CLIs are large Node/Rust binaries whose cold start (first run, @@ -127,20 +122,6 @@ func stderrOf(err error) string { return err.Error() } -// resolveAgentBinary resolves the agent's CLI binary to an absolute path. -// It refuses a binary that resolves only relative to the current directory -// (exec.ErrDot), so a malicious ./claude is never executed. -func resolveAgentBinary(agent *agents.Agent) (string, error) { - if agent.Binary == "" { - return "", fmt.Errorf("%s has no CLI binary", agent.DisplayName) - } - path, err := lookPath(agent.Binary) - if err != nil { - return "", fmt.Errorf("could not resolve %s on PATH: %w", agent.Binary, err) - } - return path, nil -} - // installTarget is the `@` argument the agent CLIs accept, // e.g. "databricks@databricks-agent-skills". func installTarget(spec *agents.PluginSpec) string { @@ -311,7 +292,7 @@ func appendScopeArg(args []string, scope string) []string { // plugin subcommand, so we don't register a marketplace on a CLI that can't // install plugins. Returns the resolved absolute path. func probePluginCLI(ctx context.Context, agent *agents.Agent) (string, error) { - bin, err := resolveAgentBinary(agent) + bin, err := agents.ResolveBinary(agent) if err != nil { return "", &BlockedError{Agent: agent.Name, Reason: ReasonCLINotOnPath, Detail: err.Error()} } @@ -443,7 +424,7 @@ func UpdatePluginForAgent(ctx context.Context, agent *agents.Agent, rec PluginRe if agent.Plugin == nil { return nil, &BlockedError{Agent: agent.Name, Reason: ReasonNoPlugin} } - bin, err := resolveAgentBinary(agent) + bin, err := agents.ResolveBinary(agent) if err != nil { return nil, &BlockedError{Agent: agent.Name, Reason: ReasonCLINotOnPath, Detail: err.Error()} } @@ -467,7 +448,7 @@ func UninstallPluginForAgent(ctx context.Context, agent *agents.Agent, rec Plugi if agent.Plugin == nil { return &BlockedError{Agent: agent.Name, Reason: ReasonNoPlugin} } - bin, err := resolveAgentBinary(agent) + bin, err := agents.ResolveBinary(agent) if err != nil { return &BlockedError{Agent: agent.Name, Reason: ReasonCLINotOnPath, Detail: err.Error()} } diff --git a/libs/aitools/installer/plugin_test.go b/libs/aitools/installer/plugin_test.go index 09120d406fe..fd0552cc99f 100644 --- a/libs/aitools/installer/plugin_test.go +++ b/libs/aitools/installer/plugin_test.go @@ -4,7 +4,9 @@ import ( "bytes" "context" "errors" + "fmt" "log/slog" + "os" "os/exec" "path/filepath" "strings" @@ -13,6 +15,7 @@ import ( "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/process" "github.com/stretchr/testify/assert" @@ -30,14 +33,14 @@ const localCopyWarning = "plugin came from its local copy and may be out of date func stubAgentLookPath(t *testing.T, found bool) { t.Helper() - orig := lookPath - lookPath = func(name string) (string, error) { + orig := agents.LookPath + agents.LookPath = func(name string) (string, error) { if found { return filepath.Join("/usr/bin", name), nil } return "", exec.ErrNotFound } - t.Cleanup(func() { lookPath = orig }) + t.Cleanup(func() { agents.LookPath = orig }) } func pluginAgent(name, display, binary string) *agents.Agent { @@ -950,9 +953,9 @@ func TestProbeWindowsShimPrintedHelp(t *testing.T) { } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - origLook := lookPath - t.Cleanup(func() { lookPath = origLook }) - lookPath = func(string) (string, error) { return tc.bin, nil } + origLook := agents.LookPath + t.Cleanup(func() { agents.LookPath = origLook }) + agents.LookPath = func(string) (string, error) { return tc.bin, nil } ctx, stub := process.WithStub(t.Context()) stub.WithCallback(func(*exec.Cmd) error { return nil }) @@ -991,3 +994,33 @@ func TestWindowsShimPrintedHelpRejectsTimeout(t *testing.T) { _, ok = windowsShimPrintedHelp(bin, &agentCmdTimeoutError{timeout: time.Second, err: perr}) assert.False(t, ok) } + +// stderrCLIEnv tells TestStderrThenSleep to print and then block, so that the timeout test can run it as a child process. +const stderrCLIEnv = "AGENTS_TEST_STDERR_SLEEP" + +// TestStderrOfKeepsStderrOnTimeout checks that a CLI killed by the timeout still reports what it printed: stderrOf +// returns the captured stderr, which is what the installer shows. +func TestStderrOfKeepsStderrOnTimeout(t *testing.T) { + ctx, cancel := context.WithTimeout(env.Set(t.Context(), stderrCLIEnv, "1"), 2*time.Second) + defer cancel() + argv := []string{os.Args[0], "-test.run=^TestStderrThenSleep$"} + + _, err := process.Background(ctx, argv) + require.Error(t, err) + assert.Contains(t, stderrOf(err), "partial output") +} + +// TestStderrThenSleep is the child process of TestStderrOfKeepsStderrOnTimeout. It prints, then blocks. +func TestStderrThenSleep(t *testing.T) { + if os.Getenv(stderrCLIEnv) == "" { + t.Skip("runs only as the child process of TestStderrOfKeepsStderrOnTimeout") + } + fmt.Fprint(os.Stderr, "partial output") + time.Sleep(time.Minute) +} + +func TestStderrOf(t *testing.T) { + failed := &process.ProcessError{Command: "claude", Err: exec.ErrNotFound, Stderr: " boom \n"} + assert.Equal(t, "boom", stderrOf(failed)) + assert.Equal(t, exec.ErrNotFound.Error(), stderrOf(exec.ErrNotFound)) +} diff --git a/libs/aitools/installer/plugin_windows_test.go b/libs/aitools/installer/plugin_windows_test.go index b9172285cec..ef156b4c5fd 100644 --- a/libs/aitools/installer/plugin_windows_test.go +++ b/libs/aitools/installer/plugin_windows_test.go @@ -22,9 +22,9 @@ func TestProbeRealCmdShimPrintedHelp(t *testing.T) { shim := filepath.Join(dir, "copilot.cmd") require.NoError(t, os.WriteFile(shim, []byte("@type \"%~dp0help.txt\"\r\n@exit /b 1\r\n"), 0o755)) - origLook := lookPath - t.Cleanup(func() { lookPath = origLook }) - lookPath = func(string) (string, error) { return shim, nil } + origLook := agents.LookPath + t.Cleanup(func() { agents.LookPath = origLook }) + agents.LookPath = func(string) (string, error) { return shim, nil } _, err := InstallPluginForAgent(t.Context(), pluginAgent(agents.NameCopilot, "GitHub Copilot", "copilot"), "", "v0.2.6") var be *BlockedError diff --git a/libs/aitools/mcp/agentconfig/cli.go b/libs/aitools/mcp/agentconfig/cli.go new file mode 100644 index 00000000000..b0735de36b4 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/cli.go @@ -0,0 +1,286 @@ +package agentconfig + +import ( + "context" + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "slices" + "strings" + "time" + + "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/log" + "github.com/databricks/cli/libs/process" +) + +// cliCommand is one run of an agent CLI: its command line, and the options of the run, which set the variable that the +// CLI must see (see homeOpts). +type cliCommand struct { + argv []string + opts []process.ExecOption +} + +// runCLI runs one agent CLI command. It is a variable so tests can run a stand-in for the CLI. +var runCLI = func(ctx context.Context, c cliCommand) error { + return runWithTimeout(ctx, cliTimeout, c) +} + +// cliTimeout bounds one add or remove. Each one edits a single config file and reads no input. A Node-based CLI can take +// seconds to start, so the bound is generous; it keeps a CLI that hangs from holding up add or remove. +const cliTimeout = 30 * time.Second + +// cliWaitDelay bounds how long a run waits for the output of the CLI once the CLI has exited or been killed. A launcher +// can leave a child that holds the output open, as an npm shim or Gemini relaunching itself does, and without this +// bound the wait would outlast the timeout. The CLI is not put in a process group, since that would take it out of the +// terminal's foreground group: Ctrl-C would then stop databricks and leave the CLI running. So a child of the CLI can +// outlive the timeout. The read-back after the run reads the file whatever the CLI did. +const cliWaitDelay = 5 * time.Second + +// runWithTimeout runs c with the environment of ctx. A run that exceeds timeout fails with context.DeadlineExceeded. It +// starts in an empty directory, so that a project config in the working directory does not change what the CLI does. +// The error of a timeout does not carry the process error, whose text is the whole argv. +func runWithTimeout(ctx context.Context, timeout time.Duration, c cliCommand) error { + dir, err := os.MkdirTemp("", "databricks-agent-cli-") + if err != nil { + return err + } + defer func() { _ = os.RemoveAll(dir) }() + cctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + opts := append([]process.ExecOption{process.WithWaitDelay(cliWaitDelay), process.WithDir(dir)}, c.opts...) + _, err = process.Background(cctx, c.argv, opts...) + // Only the timeout of this run is labelled: a deadline of the caller's ctx is returned as the process error. + if err != nil && ctx.Err() == nil && errors.Is(cctx.Err(), context.DeadlineExceeded) { + return fmt.Errorf("%w after %s", context.DeadlineExceeded, timeout) + } + return err +} + +// addViaCLI adds the server through the agent's own CLI, when the agent has an add command and the CLI can run here. It +// reports whether it finished the add. When it reports false with a nil error, the caller writes the entry itself. +// +// Once the CLI has run, the file decides what happens, whatever the CLI's exit status, and afterwards path holds the +// entry asked for: +// - the entry is there and is the one asked for: done. A non-zero exit is logged. +// - otherwise the direct writer writes the entry to path. An entry there that differs (another launch key, or another +// type) was written by this run's CLI, since it was absent before, and is replaced. When the CLI wrote nothing to +// path, it failed, or it wrote another file, the entry is added there. +// +// entry is the entry to be written, normalized to JSON values as a file holds them, so that it compares with what the +// CLI wrote whatever Go types the spec used for it. +func addViaCLI(ctx context.Context, a *agents.Agent, spec *mcpconfig.Spec, path, name, cmd string, args []string, env map[string]string, entry map[string]any) (bool, error) { + if spec.AddArgs == nil || flagLike(name) || isSymlink(path) { + return false, nil + } + sub := spec.AddArgs(name, cmd, args, env) + if sub == nil { + return false, nil + } + call, ok, err := cliCall(ctx, a, spec, sub) + if !ok { + return false, err + } + if runErr := runCLI(ctx, call); runErr != nil { + log.Debugf(ctx, "%v", cliFailure(a, sub, runErr)) + } + got, found, err := entryAt(spec, path, name) + if err != nil { + return true, err + } + if !found { + log.Debugf(ctx, "%s did not write %s; the direct writer adds the entry", commandName(a, sub), filepath.ToSlash(path)) + return true, putDirect(spec, path, name, entry) + } + asked, err := isAsked(spec, got, entry) + if err != nil { + return true, err + } + if asked { + return true, nil + } + return true, putDirect(spec, path, name, entry) +} + +// removeViaCLI removes the server through the agent's own CLI, when the agent has a remove command, the CLI is on PATH, +// and path is the file the agent reads now. The caller has already checked that the entry is ours. It reports whether +// the entry is gone afterwards. When the CLI fails, or exits 0 and leaves the entry in path (it reads another file, say), +// it reports false, and the caller removes the entry with the direct writer: the entry is ours, and the user asked for +// it to go. The CLI's failure is logged. +func removeViaCLI(ctx context.Context, a *agents.Agent, spec *mcpconfig.Spec, path, name string) (bool, error) { + if spec.RemoveArgs == nil || flagLike(name) || isSymlink(path) { + return false, nil + } + current, err := configPath(ctx, spec) + if err != nil { + // Without the current file, the entry is not in the file the CLI reads. + log.Debugf(ctx, "%v; the direct writer removes the entry", err) + return false, nil + } + if path != current { + // The entry is in a file the agent no longer reads, so the CLI would not find it. + return false, nil + } + sub := spec.RemoveArgs(name) + call, ok, err := cliCall(ctx, a, spec, sub) + if !ok { + return false, err + } + if err := runCLI(ctx, call); err != nil { + log.Debugf(ctx, "%v; the direct writer removes the entry", cliFailure(a, sub, err)) + } + _, found, err := entryAt(spec, path, name) + if err != nil { + return false, err + } + return !found, nil +} + +// isSymlink reports whether path is a symbolic link. A CLI that renames a temp file over its config replaces the link with +// a regular file, so the direct writer makes the change instead: it writes through the link. +func isSymlink(path string) bool { + info, err := os.Lstat(path) + return err == nil && info.Mode()&os.ModeSymlink != 0 +} + +// flagLike reports whether name starts with a dash. The CLIs would read such a name as one of their flags, so the direct +// writer makes the change. +func flagLike(name string) bool { + return strings.HasPrefix(name, "-") +} + +// isAsked reports whether got, the entry that the file holds after a CLI ran, is the one asked for: ours, with the +// launch keys of want. An agent may write "env": {} for a server with no variables, which counts as no env. +func isAsked(spec *mcpconfig.Spec, got, want map[string]any) (bool, error) { + if !mcpconfig.IsDatabricksEntry(got) { + return false, nil + } + keys := ownedKeys(spec) + return sameEntry(launchOf(got, keys), launchOf(want, keys)) +} + +// launchOf returns the launch keys of entry, without an empty env table. An agent's CLI may write an empty env table +// for a server with no variables, and that counts as none. +func launchOf(entry map[string]any, keys []string) map[string]any { + out := launchKeysOf(entry, keys) + for _, key := range mcpconfig.EnvKeys { + if vars, ok := out[key].(map[string]any); ok && len(vars) == 0 { + delete(out, key) + } + } + return out +} + +// entryAt returns the entry named name in the config file at path, and whether the file holds one. +func entryAt(spec *mcpconfig.Spec, path, name string) (map[string]any, bool, error) { + doc, err := load(spec, path) + if err != nil { + return nil, false, err + } + entry, found, err := doc.entry(rootPath(spec), name) + if err != nil { + return nil, false, fmt.Errorf("%s: %w", filepath.ToSlash(path), err) + } + return entry, found, nil +} + +// cliCall returns the run of agent a's CLI with the arguments sub. It reports false when the direct writer must make the +// change: the CLI is not on PATH, or it is a batch file that cannot take these arguments. +func cliCall(ctx context.Context, a *agents.Agent, spec *mcpconfig.Spec, sub []string) (cliCommand, bool, error) { + bin, err := cliPath(ctx, a) + if err != nil || bin == "" { + return cliCommand{}, false, err + } + argv := append([]string{bin}, sub...) + if !batchSafe(argv) { + return cliCommand{}, false, nil + } + opts, err := homeOpts(ctx, spec) + if err != nil { + return cliCommand{}, false, err + } + return cliCommand{argv: argv, opts: opts}, true, nil +} + +// homeOpts returns the option that gives the CLI the agent's config directory variable, or nil when the agent has none. +// The value is made absolute, since the CLI runs in an empty directory, and configPath resolved it against the directory +// of the caller. It is cleaned, as the config path that the read-back uses is, so that the CLI and the read-back name +// the same directory. An empty value is unset rather than passed on: a CLI that reads an empty variable as set may then write +// relative to its working directory. +func homeOpts(ctx context.Context, spec *mcpconfig.Spec) ([]process.ExecOption, error) { + if spec.HomeEnv == "" { + return nil, nil + } + home := env.Get(ctx, spec.HomeEnv) + if home == "" { + return []process.ExecOption{process.WithoutEnv(spec.HomeEnv)}, nil + } + abs, err := mcpconfig.AbsoluteAsWritten(home) + if err != nil { + return nil, err + } + return []process.ExecOption{process.WithEnv(spec.HomeEnv, filepath.Clean(abs))}, nil +} + +// cliPath returns the path of agent a's CLI, or "" when no CLI of that name is on PATH. A CLI that resolves only +// through the current directory is treated the same: it is not run, and the direct writer makes the change. +func cliPath(ctx context.Context, a *agents.Agent) (string, error) { + path, err := agents.ResolveBinary(a) + if errors.Is(err, exec.ErrNotFound) || errors.Is(err, exec.ErrDot) { + log.Debugf(ctx, "%s CLI is not usable on PATH: %v", a.DisplayName, err) + return "", nil + } + return path, err +} + +// batchSafe reports whether argv reaches its program intact. npm installs many agent CLIs as .cmd shims, and Go hands +// the arguments of a batch file to cmd.exe unquoted (see os/exec). A double quote, as in the JSON that claude add-json +// takes, or a character that cmd.exe expands, splits or ends a line on, does not survive that. Nor does a space: Go +// quotes an argument that has one, and cmd.exe then strips the first and last quote, which may be the ones around +// argv[0] when the path of the shim has a space. +func batchSafe(argv []string) bool { + ext := strings.ToLower(filepath.Ext(argv[0])) + if ext != ".cmd" && ext != ".bat" { + return true + } + return !slices.ContainsFunc(argv, func(arg string) bool { + return strings.ContainsAny(arg, " \"%!^&|<>\r\n") + }) +} + +// cliError is a failed agent CLI command. Its text names the command and the stderr the CLI printed. It does not print +// the argv, which a process error would: for claude add-json that is the whole server entry as JSON. So it is not +// the installer's stderrOf, whose fallback is the process error's own text. +type cliError struct { + command string + err error +} + +func (e *cliError) Error() string { + perr, ok := errors.AsType[*process.ProcessError](e.err) + if !ok { + return e.command + " failed: " + e.err.Error() + } + if s := strings.TrimSpace(perr.Stderr); s != "" { + return e.command + " failed: " + s + } + return e.command + " failed: " + perr.Err.Error() +} + +func (e *cliError) Unwrap() error { return e.err } + +// cliFailure reports a CLI command that failed. err stays in the chain, so that a timeout or a process error can be +// matched with errors.Is or errors.As. +func cliFailure(a *agents.Agent, sub []string, err error) error { + return &cliError{command: commandName(a, sub), err: err} +} + +// commandName names an agent CLI command for an error: the binary and its subcommand words, such as "claude mcp +// add-json". The first two arguments of sub are those words (see mcpconfig.Spec). +func commandName(a *agents.Agent, sub []string) string { + return a.Binary + " " + strings.Join(sub[:2], " ") +} diff --git a/libs/aitools/mcp/agentconfig/cli_test.go b/libs/aitools/mcp/agentconfig/cli_test.go new file mode 100644 index 00000000000..d478a2eef87 --- /dev/null +++ b/libs/aitools/mcp/agentconfig/cli_test.go @@ -0,0 +1,674 @@ +package agentconfig + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "maps" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "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/process" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// sleepingCLIEnv tells TestSleepingCLI to block, so that the timeout test can run it as a child process. +const sleepingCLIEnv = "AGENTCONFIG_TEST_SLEEP" + +func TestMain(m *testing.M) { + // No agent CLI is on PATH unless a test puts one there, so no test runs a host CLI. + agents.LookPath = func(string) (string, error) { return "", exec.ErrNotFound } + m.Run() +} + +// cliAgent is an agent whose config is the JSON file at path, and whose CLI has the add and remove commands of a +// stand-in: `mcp add -- ` and `mcp remove `. +func cliAgent(path string) *agents.Agent { + return &agents.Agent{ + Name: "fakeagent", + DisplayName: "Fake agent", + Binary: "fakeagent", + MCP: &mcpconfig.Spec{ + Path: func(context.Context) (string, error) { return path, nil }, + Format: mcpconfig.FormatJSON, + Root: "mcpServers", + Entry: testEntry, + AddArgs: func(name, cmd string, args []string, _ map[string]string) []string { + return append([]string{"mcp", "add", name, "--", cmd}, args...) + }, + RemoveArgs: func(name string) []string { return []string{"mcp", "remove", name} }, + }, + } +} + +// stubCLI puts a CLI named bin on PATH, and replaces the run of that CLI with run. It returns the argv of each run. +func stubCLI(t *testing.T, bin string, run func(argv []string) error) *[][]string { + t.Helper() + var calls [][]string + origLookPath, origRunCLI := agents.LookPath, runCLI + t.Cleanup(func() { agents.LookPath, runCLI = origLookPath, origRunCLI }) + agents.LookPath = func(string) (string, error) { return bin, nil } + runCLI = func(_ context.Context, c cliCommand) error { + calls = append(calls, c.argv) + return run(c.argv) + } + return &calls +} + +// writeServers writes a JSON config with the given server entries under mcpServers, as an agent's CLI would. +func writeServers(t *testing.T, path string, servers map[string]any) { + t.Helper() + data, err := json.MarshalIndent(map[string]any{"mcpServers": servers}, "", " ") + require.NoError(t, err) + require.NoError(t, os.WriteFile(path, data, 0o600)) +} + +func TestUpsertAddsThroughCLI(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + bin := filepath.Join(t.TempDir(), "fakeagent") + entry := testEntry(testCmd, testArgs("test"), nil) + writeServers(t, path, map[string]any{"other": map[string]any{"command": "/usr/bin/other"}}) + before, err := os.ReadFile(path) + require.NoError(t, err) + + var written []byte + calls := stubCLI(t, bin, func([]string) error { + // The stand-in writes its own bytes, so a direct write after the CLI would be visible. + data, err := json.MarshalIndent(map[string]any{"mcpServers": map[string]any{"other": map[string]any{"command": "/usr/bin/other"}, testName: entry}}, "", " ") + if err != nil { + return err + } + written = append(data, '\n') + return os.WriteFile(path, written, 0o600) + }) + + gotPath, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, path, gotPath) + assert.Equal(t, StatusAdded, status) + assert.Equal(t, [][]string{append([]string{bin, "mcp", "add", testName, "--", testCmd}, testArgs("test")...)}, *calls) + after, err := os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, written, after) + assert.NotEqual(t, before, after) +} + +func TestUpsertWritesDirectlyWhenCLIIsNotOnPath(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + + _, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + entries, err := ReadAt(cliAgent(path), path) + require.NoError(t, err) + assert.Contains(t, entries, testName) +} + +func TestUpsertWritesDirectlyWhenCLIResolvesOnlyFromCurrentDirectory(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + origLookPath, origRunCLI := agents.LookPath, runCLI + t.Cleanup(func() { agents.LookPath, runCLI = origLookPath, origRunCLI }) + agents.LookPath = func(string) (string, error) { return filepath.Join(".", "fakeagent"), exec.ErrDot } + runCLI = func(context.Context, cliCommand) error { + t.Fatal("a ./fakeagent must never run") + return nil + } + + _, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + entries, err := ReadAt(cliAgent(path), path) + require.NoError(t, err) + assert.Contains(t, entries, testName) +} + +// TestUpsertWritesDirectlyWhenCLIWritesNothing covers a CLI that does not write the entry to the file: it fails, for +// example because it is too old for the command or a different program has the same name, or it hangs, or it exits 0 +// and writes another file or nothing. The direct writer adds the entry. +func TestUpsertWritesDirectlyWhenCLIWritesNothing(t *testing.T) { + failures := map[string]error{ + "exit 0": nil, + "exit 1": &process.ProcessError{Command: "fakeagent", Err: errors.New("exit status 1"), Stderr: "unknown command\n"}, + "timeout": fmt.Errorf("%w after 30s", context.DeadlineExceeded), + } + for name, failure := range failures { + t.Run(name, func(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return failure }) + + _, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Len(t, *calls, 1) + entries, err := ReadAt(cliAgent(path), path) + require.NoError(t, err) + assert.Contains(t, entries, testName) + }) + } +} + +func TestUpsertKeepsEntryWhenCLIFailsAfterWriting(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + failure := &process.ProcessError{Command: "fakeagent", Err: errors.New("exit status 1"), Stderr: "warning\n"} + stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func(argv []string) error { + writeServers(t, path, map[string]any{testName: testEntry(testCmd, testArgs("test"), nil)}) + return failure + }) + + _, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err, "the entry the CLI wrote is the one asked for, so its exit status does not matter") + assert.Equal(t, StatusAdded, status) +} + +func TestUpsertDoesNotRunCLIForEntryWeOwn(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + _, _, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("other"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Empty(t, *calls, "an update of our entry keeps the direct writer") + entries, err := ReadAt(a, path) + require.NoError(t, err) + assert.Contains(t, entries[testName]["args"], "other") +} + +func TestUpsertDoesNotRunCLIForForeignEntry(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + writeServers(t, path, map[string]any{testName: map[string]any{"command": "/usr/bin/other", "args": []string{"x"}}}) + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + + _, _, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + assert.ErrorIs(t, err, ErrNotOwned) + assert.Empty(t, *calls, "a refused entry is not touched by the CLI") + + _, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, true) + require.NoError(t, err) + assert.Equal(t, StatusUpdated, status) + assert.Empty(t, *calls, "--force replaces the entry directly") +} + +func TestUpsertAddsEntryWhenCLIWritesOtherFile(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + other := filepath.Join(t.TempDir(), "elsewhere.json") + a := cliAgent(path) + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { + writeServers(t, other, map[string]any{testName: testEntry(testCmd, testArgs("test"), nil)}) + return nil + }) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Len(t, *calls, 1, "the CLI is not run again to undo its add") + entries, err := ReadAt(a, path) + require.NoError(t, err) + assert.Contains(t, entries, testName, "the agent's file holds the entry asked for") +} + +// TestUpsertReplacesEntryWhenCLIWritesDifferentEntry covers a CLI that writes the name with other launch keys, such as +// a different type. This run's CLI wrote the entry, so the direct writer replaces it with the one asked for, and the +// add is reported as added. +func TestUpsertReplacesEntryWhenCLIWritesDifferentEntry(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + a.MCP.Entry = func(cmd string, args []string, env map[string]string) map[string]any { + entry := testEntry(cmd, args, env) + entry["type"] = "local" + return entry + } + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { + entry := testEntry(testCmd, testArgs("test"), nil) + entry["type"] = "stdio" + writeServers(t, path, map[string]any{testName: entry}) + return nil + }) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Len(t, *calls, 1, "the CLI is not run again") + entries, err := ReadAt(a, path) + require.NoError(t, err) + assert.Equal(t, "local", entries[testName]["type"]) + assert.Equal(t, testCmd, entries[testName]["command"]) +} + +func TestUpsertAcceptsCLIEntryWhenSpecWritesEmptyEnvAsTyped(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + // Like claudeMCPEntry: env is a typed map, empty when there are no variables. + a.MCP.Entry = func(cmd string, args []string, env map[string]string) map[string]any { + vars := map[string]string{} + maps.Copy(vars, env) + return map[string]any{"command": cmd, "args": args, "env": vars} + } + stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { + entry := testEntry(testCmd, testArgs("test"), nil) + entry["env"] = map[string]any{} + writeServers(t, path, map[string]any{testName: entry}) + return nil + }) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) +} + +func TestUpsertWritesDirectlyWhenBatchFileCannotTakeArguments(t *testing.T) { + ctx := testHome(t) + args := append(testArgs("test"), `say "hi"`) + dir := t.TempDir() + + path := filepath.Join(dir, "batch.json") + calls := stubCLI(t, filepath.Join(dir, "fakeagent.cmd"), func([]string) error { return nil }) + _, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, args, nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Empty(t, *calls, "a batch file would garble the quotes, so the direct writer does the change") + + path = filepath.Join(dir, "binary.json") + calls = stubCLI(t, filepath.Join(dir, "fakeagent.exe"), func([]string) error { return nil }) + _, status, err = Upsert(ctx, cliAgent(path), testName, testCmd, args, nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Len(t, *calls, 1, "an executable runs the CLI, which here writes nothing, and the direct writer adds the entry") +} + +func TestRemoveThroughCLI(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + _, _, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + + bin := filepath.Join(t.TempDir(), "fakeagent") + calls := stubCLI(t, bin, func([]string) error { + writeServers(t, path, map[string]any{}) + return nil + }) + removed, err := RemoveAt(ctx, a, path, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Equal(t, [][]string{{bin, "mcp", "remove", testName}}, *calls) + entries, err := ReadAt(a, path) + require.NoError(t, err) + assert.Empty(t, entries) +} + +func TestRemoveWritesDirectlyWhenFileMoved(t *testing.T) { + ctx := testHome(t) + current := filepath.Join(t.TempDir(), "current.json") + recorded := filepath.Join(t.TempDir(), "recorded.json") + a := cliAgent(current) + writeServers(t, recorded, map[string]any{testName: testEntry(testCmd, testArgs("test"), nil)}) + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + + removed, err := RemoveAt(ctx, a, recorded, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Empty(t, *calls, "the CLI reads the current file, not the recorded one") + entries, err := ReadAt(a, recorded) + require.NoError(t, err) + assert.Empty(t, entries) +} + +func TestRemoveRefusesForeignEntryBeforeCLI(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + writeServers(t, path, map[string]any{testName: map[string]any{"command": "/usr/bin/other", "args": []string{"x"}}}) + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + + _, err := RemoveAt(ctx, cliAgent(path), path, testName) + assert.ErrorIs(t, err, ErrNotOwned) + assert.Empty(t, *calls) +} + +func TestRemoveDoesNotRunCLIWhenEntryIsAbsent(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + + removed, err := RemoveAt(ctx, cliAgent(path), path, testName) + require.NoError(t, err) + assert.False(t, removed) + assert.Empty(t, *calls) +} + +func TestRemoveDirectWhenCLILeavesEntry(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + _, _, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + + removed, err := RemoveAt(ctx, a, path, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Len(t, *calls, 1, "the CLI ran once, and the direct writer removed what it left") + entries, err := ReadAt(a, path) + require.NoError(t, err) + assert.Empty(t, entries) +} + +func TestRemoveDirectWhenCLIFails(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + _, _, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + failure := &process.ProcessError{Command: "fakeagent", Err: errors.New("exit status 2"), Stderr: "no such server\n"} + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return failure }) + + removed, err := RemoveAt(ctx, a, path, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Len(t, *calls, 1) + entries, err := ReadAt(a, path) + require.NoError(t, err) + assert.Empty(t, entries) +} + +// launcherEnv tells TestLauncherCLI to start the sleeping CLI as a child and wait for it, as npm shims and Gemini do. +const launcherEnv = "AGENTCONFIG_TEST_LAUNCHER" + +// TestLauncherCLI is a CLI that starts another process and waits for it. The child inherits the output of the launcher, +// so the output stays open after the launcher is killed. +func TestLauncherCLI(t *testing.T) { + if os.Getenv(launcherEnv) == "" { + t.Skip("runs only as the launcher of TestRunWithTimeoutStopsLauncherThatHoldsOutput") + } + child := exec.Command(os.Args[0], "-test.run=^TestSleepingCLI$") + child.Stdout = os.Stdout + child.Stderr = os.Stderr + _ = child.Run() +} + +func TestRunWithTimeoutStopsLauncherThatHoldsOutput(t *testing.T) { + ctx := env.Set(env.Set(t.Context(), launcherEnv, "1"), sleepingCLIEnv, "1") + argv := []string{os.Args[0], "-test.run=^TestLauncherCLI$"} + + start := time.Now() + err := runWithTimeout(ctx, 500*time.Millisecond, cliCommand{argv: argv}) + assert.ErrorIs(t, err, context.DeadlineExceeded) + assert.Less(t, time.Since(start), 30*time.Second, "the child kept the run open past its timeout") +} + +func TestRunWithTimeoutStopsCLIThatHangs(t *testing.T) { + ctx := env.Set(t.Context(), sleepingCLIEnv, "1") + argv := []string{os.Args[0], "-test.run=^TestSleepingCLI$"} + + err := runWithTimeout(ctx, 200*time.Millisecond, cliCommand{argv: argv}) + assert.ErrorIs(t, err, context.DeadlineExceeded) + assert.NotContains(t, err.Error(), os.Args[0], "the timeout error does not carry the argv") +} + +func TestRunWithTimeoutDoesNotLabelCallerDeadline(t *testing.T) { + ctx, cancel := context.WithTimeout(env.Set(t.Context(), sleepingCLIEnv, "1"), 200*time.Millisecond) + defer cancel() + argv := []string{os.Args[0], "-test.run=^TestSleepingCLI$"} + + err := runWithTimeout(ctx, time.Minute, cliCommand{argv: argv}) + assert.Error(t, err) + assert.NotErrorIs(t, err, context.DeadlineExceeded, "the caller's deadline is not the timeout of this run") +} + +// TestSleepingCLI is the child process of the timeout tests. It blocks when run as that child, for just over the wait +// delay, so that a launcher's wait for it ends at the delay and the test does not leave it running long. +func TestSleepingCLI(t *testing.T) { + if os.Getenv(sleepingCLIEnv) == "" { + t.Skip("runs only as the child process of the timeout tests") + } + time.Sleep(cliWaitDelay + time.Second) +} + +func TestBatchSafe(t *testing.T) { + tests := []struct { + name string + argv []string + want bool + }{ + {"executable takes JSON", []string{`C:\bin\claude.exe`, `{"type":"stdio"}`}, true}, + {"batch file takes plain arguments", []string{`C:\npm\codex.cmd`, "mcp", "remove", "github"}, true}, + {"executable with a path that has spaces", []string{`C:\Program Files\claude\claude.exe`, "mcp", "remove", "github"}, true}, + {"batch file with a path that has spaces", []string{`C:\Users\John Doe\npm\codex.CMD`, "mcp", "remove", "github"}, false}, + {"batch file takes an argument with a space", []string{`C:\npm\codex.cmd`, "mcp", "add", "my server"}, false}, + {"batch file takes a double quote", []string{`C:\npm\claude.cmd`, `{"type":"stdio"}`}, false}, + {"batch file takes a percent sign", []string{`C:\npm\codex.bat`, "%PATH%"}, false}, + {"batch file takes an ampersand", []string{`C:\npm\codex.bat`, "a&b"}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, batchSafe(tt.argv)) + }) + } +} + +func TestCLIFailureNamesCommandNotArgv(t *testing.T) { + argv := []string{"/bin/fakeagent", "mcp", "add-json", testName, `{"command":"/bin/databricks"}`} + failed := &process.ProcessError{Command: strings.Join(argv, " "), Err: errors.New("exit status 1")} + + err := cliFailure(cliAgent(""), argv[1:], failed) + assert.EqualError(t, err, "fakeagent mcp add-json failed: exit status 1") + assert.ErrorIs(t, err, failed) + + failed.Stderr = " no such server \n" + assert.EqualError(t, cliFailure(cliAgent(""), argv[1:], failed), "fakeagent mcp add-json failed: no such server") +} + +func TestUpsertWritesDirectlyForNameWithDash(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + + _, status, err := Upsert(ctx, a, "-h", testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + _, err = RemoveAt(ctx, a, path, "-h") + require.NoError(t, err) + assert.Empty(t, *calls, "a name with a dash would reach the CLI as one of its flags") +} + +func TestUpsertTreatsEmptyEnvFromCLIAsUnchanged(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { + entry := testEntry(testCmd, testArgs("test"), nil) + entry["env"] = map[string]any{} + writeServers(t, path, map[string]any{testName: entry}) + return nil + }) + + _, status, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + entries, err := ReadAt(cliAgent(path), path) + require.NoError(t, err) + assert.Equal(t, map[string]any{}, entries[testName]["env"], "the entry the CLI wrote is kept as it wrote it") + before, err := os.ReadFile(path) + require.NoError(t, err) + + _, status, err = Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusUnchanged, status) + after, err := os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, before, after) +} + +// homeOutEnv names the file that TestWriteHomeVariable writes the agent home variable to. +const homeOutEnv = "AGENTCONFIG_TEST_HOME_OUT" + +// testHomeVar is the agent home variable of the tests. Its value is what TestRunCLISeesHomeVariable sets. +const testHomeVar = "FAKEAGENT_HOME" + +// TestWriteHomeVariable is the child process of TestRunCLISeesHomeVariable. It writes the agent home variable it sees, or +// "unset" when it does not see the variable. +func TestWriteHomeVariable(t *testing.T) { + out := os.Getenv(homeOutEnv) + if out == "" { + t.Skip("runs only as the child process of TestRunCLISeesHomeVariable") + } + value, ok := os.LookupEnv(testHomeVar) + if !ok { + value = "unset" + } + require.NoError(t, os.WriteFile(out, []byte(value), 0o600)) +} + +// runHomeChild runs the child TestWriteHomeVariable as an agent CLI would be run, with the agent home variable set to +// value, and returns what the child saw. +func runHomeChild(t *testing.T, value string) string { + out := filepath.Join(t.TempDir(), "home") + ctx := env.Set(env.Set(t.Context(), testHomeVar, value), homeOutEnv, out) + spec := &mcpconfig.Spec{HomeEnv: testHomeVar} + opts, err := homeOpts(ctx, spec) + require.NoError(t, err) + argv := []string{os.Args[0], "-test.run=^TestWriteHomeVariable$"} + require.NoError(t, runWithTimeout(ctx, time.Minute, cliCommand{argv: argv, opts: opts})) + data, err := os.ReadFile(out) + require.NoError(t, err) + return string(data) +} + +func TestRunCLISeesHomeVariable(t *testing.T) { + t.Run("relative value is made absolute", func(t *testing.T) { + want, err := mcpconfig.AbsoluteAsWritten("relative-home") + require.NoError(t, err) + assert.Equal(t, want, runHomeChild(t, "relative-home"), "the CLI runs in an empty directory") + }) + t.Run("empty value is unset", func(t *testing.T) { + assert.Equal(t, "unset", runHomeChild(t, ""), "a CLI that reads an empty variable as set writes relative to its directory") + }) +} + +// TestHomeOptsPassesCleanedHome checks that the CLI gets the directory the read-back reads: a ".." is cleaned, as the +// config path is, so the CLI and the read-back do not name different directories. +func TestHomeOptsPassesCleanedHome(t *testing.T) { + sep := string(filepath.Separator) + home := t.TempDir() + sep + "link" + sep + ".." + sep + "codex" + ctx := env.Set(t.Context(), testHomeVar, home) + + opts, err := homeOpts(ctx, &mcpconfig.Spec{HomeEnv: testHomeVar}) + require.NoError(t, err) + cmd := &exec.Cmd{} + for _, opt := range opts { + require.NoError(t, opt(ctx, cmd)) + } + assert.Equal(t, []string{testHomeVar + "=" + filepath.Clean(home)}, cmd.Env) +} + +func TestSymlinkedConfigIsWrittenThroughWithoutCLI(t *testing.T) { + ctx := testHome(t) + dir := t.TempDir() + target := filepath.Join(dir, "dotfiles", "agent.json") + require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o700)) + writeServers(t, target, map[string]any{}) + path := filepath.Join(dir, "agent.json") + if err := os.Symlink(target, path); err != nil { + t.Skipf("cannot create a symlink here: %v", err) + } + a := cliAgent(path) + calls := stubCLI(t, filepath.Join(dir, "fakeagent"), func([]string) error { return nil }) + + _, status, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + assert.Equal(t, StatusAdded, status) + assert.Empty(t, *calls, "a CLI that renames over the config would replace the link") + + _, err = RemoveAt(ctx, a, path, testName) + require.NoError(t, err) + assert.Empty(t, *calls) + + info, err := os.Lstat(path) + require.NoError(t, err) + assert.NotZero(t, info.Mode()&os.ModeSymlink, "the link is kept") + entries, err := ReadAt(a, target) + require.NoError(t, err) + assert.Empty(t, entries) +} + +func TestRemoveWritesDirectlyWhenCurrentPathCannotBeResolved(t *testing.T) { + ctx := testHome(t) + path := filepath.Join(t.TempDir(), "agent.json") + a := cliAgent(path) + _, _, err := Upsert(ctx, a, testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + a.MCP.Path = func(context.Context) (string, error) { return "", errors.New("no home directory") } + calls := stubCLI(t, filepath.Join(t.TempDir(), "fakeagent"), func([]string) error { return nil }) + + removed, err := RemoveAt(ctx, a, path, testName) + require.NoError(t, err) + assert.True(t, removed) + assert.Empty(t, *calls, "the recorded file is not the current one, so the CLI is not run") + entries, err := ReadAt(a, path) + require.NoError(t, err) + assert.Empty(t, entries) +} + +func TestUpsertRefusesDanglingSymlinkBeforeCLI(t *testing.T) { + ctx := testHome(t) + dir := t.TempDir() + path := filepath.Join(dir, "agent.json") + if err := os.Symlink(filepath.Join(dir, "missing.json"), path); err != nil { + t.Skipf("cannot create a symlink here: %v", err) + } + calls := stubCLI(t, filepath.Join(dir, "fakeagent"), func([]string) error { return nil }) + + _, _, err := Upsert(ctx, cliAgent(path), testName, testCmd, testArgs("test"), nil, false) + assert.ErrorContains(t, err, "symlink to a file that does not exist") + assert.Empty(t, *calls, "the CLI would write through a link with no target") +} + +// listingEnv names the file that TestWriteCLIDirListing writes the names in its working directory to. +const listingEnv = "AGENTCONFIG_TEST_LISTING" + +// TestWriteCLIDirListing is the child process of TestRunWithTimeoutStartsInEmptyDirectory. It lists its working directory. +func TestWriteCLIDirListing(t *testing.T) { + out := os.Getenv(listingEnv) + if out == "" { + t.Skip("runs only as the child process of TestRunWithTimeoutStartsInEmptyDirectory") + } + entries, err := os.ReadDir(".") + require.NoError(t, err) + var names []string + for _, e := range entries { + names = append(names, e.Name()) + } + require.NoError(t, os.WriteFile(out, []byte(strings.Join(names, "\n")), 0o600)) +} + +func TestRunWithTimeoutStartsInEmptyDirectory(t *testing.T) { + out := filepath.Join(t.TempDir(), "listing") + ctx := env.Set(t.Context(), listingEnv, out) + argv := []string{os.Args[0], "-test.run=^TestWriteCLIDirListing$"} + + require.NoError(t, runWithTimeout(ctx, 30*time.Second, cliCommand{argv: argv})) + data, err := os.ReadFile(out) + require.NoError(t, err) + assert.Empty(t, data, "a project config in the working directory would change what the CLI does") +} diff --git a/libs/aitools/mcp/agentconfig/cli_unix_test.go b/libs/aitools/mcp/agentconfig/cli_unix_test.go new file mode 100644 index 00000000000..9296224a57e --- /dev/null +++ b/libs/aitools/mcp/agentconfig/cli_unix_test.go @@ -0,0 +1,41 @@ +//go:build unix + +package agentconfig + +import ( + "os" + "path/filepath" + "strconv" + "syscall" + "testing" + "time" + + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// groupEnv names the file that TestWriteProcessGroup writes its process group to. +const groupEnv = "AGENTCONFIG_TEST_GROUP" + +// TestWriteProcessGroup is the child process of TestRunWithTimeoutKeepsCLIInForegroundGroup. It writes its process group. +func TestWriteProcessGroup(t *testing.T) { + out := os.Getenv(groupEnv) + if out == "" { + t.Skip("runs only as the child process of TestRunWithTimeoutKeepsCLIInForegroundGroup") + } + require.NoError(t, os.WriteFile(out, []byte(strconv.Itoa(syscall.Getpgrp())), 0o600)) +} + +// TestRunWithTimeoutKeepsCLIInForegroundGroup checks that the CLI stays in the process group of databricks, so that a +// Ctrl-C at the terminal reaches it too. +func TestRunWithTimeoutKeepsCLIInForegroundGroup(t *testing.T) { + out := filepath.Join(t.TempDir(), "group") + ctx := env.Set(t.Context(), groupEnv, out) + argv := []string{os.Args[0], "-test.run=^TestWriteProcessGroup$"} + + require.NoError(t, runWithTimeout(ctx, 30*time.Second, cliCommand{argv: argv})) + data, err := os.ReadFile(out) + require.NoError(t, err) + assert.Equal(t, strconv.Itoa(syscall.Getpgrp()), string(data)) +} diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index c4129f75abf..6d51207a89c 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -1,7 +1,8 @@ // 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. +// coding agents. The direct writer 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. An add or remove that the agent's own CLI makes (see +// cli.go) leaves the file in the layout that the CLI writes. package agentconfig import ( @@ -79,8 +80,10 @@ func ownedKeys(spec *mcpconfig.Spec) []string { // Upsert writes a stdio server name for agent a, which runs cmd with args and // env. An entry this CLI wrote gets its launch keys updated, and its other keys // are kept. A foreign entry is replaced only when force is set. The file is left -// alone when the entry already matches. cmd must be the databricks binary, or -// the entry could never be recognized as ours. It returns the config file path. +// alone when the entry already matches. A new entry goes through the agent's own +// CLI when that CLI is on PATH (see addViaCLI), and the file then has the CLI's +// layout. 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 @@ -119,6 +122,13 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin switch { case !found: status = StatusAdded + done, cliErr := addViaCLI(ctx, a, spec, path, name, cmd, args, env, ours) + if cliErr != nil { + return path, "", cliErr + } + if done { + return path, status, nil + } 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) @@ -130,7 +140,7 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if mergeErr != nil { return path, "", mergeErr } - same, cmpErr := sameEntry(launchKeysOf(current, keys), launchKeysOf(merged, keys)) + same, cmpErr := sameEntry(launchOf(current, keys), launchOf(merged, keys)) if cmpErr != nil { return path, "", cmpErr } @@ -151,8 +161,10 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin // RemoveAt deletes server name from the config file at path, and refuses an entry this CLI did not write. It reports // false, without writing, when there is no such entry. The file need not be the one the spec names now: a record names -// the file an entry was written to, and the agent's config may have moved since. A missing file has no entry. -func RemoveAt(a *agents.Agent, path, name string) (bool, error) { +// the file an entry was written to, and the agent's config may have moved since. A missing file has no entry. The +// removal runs through the agent's own CLI when that CLI is on PATH and path is the file it reads now (see removeViaCLI); +// otherwise, or when the CLI leaves the entry behind, the direct writer removes it. +func RemoveAt(ctx context.Context, a *agents.Agent, path, name string) (bool, error) { if err := checkName(name); err != nil { return false, err } @@ -160,6 +172,43 @@ func RemoveAt(a *agents.Agent, path, name string) (bool, error) { if err != nil { return false, err } + if spec.RemoveArgs == nil { + return removeDirect(spec, path, name) + } + current, found, err := entryAt(spec, path, name) + if err != nil { + return false, err + } + if !found { + return false, nil + } + if !mcpconfig.IsDatabricksEntry(current) { + return false, fmt.Errorf("%s: server %q: %w", filepath.ToSlash(path), name, ErrNotOwned) + } + removed, err := removeViaCLI(ctx, a, spec, path, name) + if err != nil || removed { + return removed, err + } + return removeDirect(spec, path, name) +} + +// putDirect writes entry as server name in the config file at path with the direct writer. It reads the file again, +// since the agent's CLI may have changed it. Unlike removeDirect, it does not check who wrote the entry: it is used for +// an entry that this run's CLI wrote. +func putDirect(spec *mcpconfig.Spec, path, name string, entry map[string]any) error { + doc, err := load(spec, path) + if err != nil { + return err + } + if err := doc.put(rootPath(spec), name, entry, nil); err != nil { + return fmt.Errorf("%s: %w", filepath.ToSlash(path), err) + } + return save(path, doc) +} + +// removeDirect deletes server name from the config file at path, with the direct writer. It reads the file again, since +// the agent's CLI may have changed it, and it refuses an entry this CLI did not write. +func removeDirect(spec *mcpconfig.Spec, path, name string) (bool, error) { doc, err := load(spec, path) if err != nil { return false, err @@ -284,7 +333,7 @@ func save(path string, doc document) error { case err == nil: target = resolved case errors.Is(err, fs.ErrNotExist): - if info, lerr := os.Lstat(path); lerr == nil && info.Mode()&os.ModeSymlink != 0 { + if isSymlink(path) { return fmt.Errorf("%s is a symlink to a file that does not exist", filepath.ToSlash(path)) } default: diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index b9cf3520b88..544a6b51605 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -819,7 +819,7 @@ func TestReadAtAndRemoveAtUseTheGivenFile(t *testing.T) { require.NoError(t, err) assert.Empty(t, entries, "the spec's file does not have the entry") - removed, err := RemoveAt(a, recorded, testName) + removed, err := RemoveAt(t.Context(), a, recorded, testName) require.NoError(t, err) assert.True(t, removed) entries, err = ReadAt(a, recorded) @@ -852,5 +852,5 @@ func removeAgent(ctx context.Context, a *agents.Agent, name string) (bool, error if err != nil { return false, err } - return RemoveAt(a, path, name) + return RemoveAt(ctx, a, path, name) } diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index cfd7e4d6d4e..a6adf60f935 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -34,6 +34,18 @@ type Spec struct { // Entry builds the entry for a stdio server that runs cmd with args and env. // env is empty when there are no variables to pin. Entry func(cmd string, args []string, env map[string]string) map[string]any + // AddArgs returns the arguments, without the program, of the agent CLI command that adds server name at user + // scope as the entry Entry(cmd, args, env) builds. The first two arguments are the subcommand words, such as "mcp" + // and "add-json". Nil when the agent has no non-interactive add. It returns nil too when the CLI cannot take these + // arguments, and the direct writer makes the change instead. + AddArgs func(name, cmd string, args []string, env map[string]string) []string + // RemoveArgs returns the arguments, without the program, of the agent CLI command that removes server name from + // user scope. Nil when the agent has no non-interactive remove. The first two arguments are the subcommand words, + // as for AddArgs. + RemoveArgs func(name string) []string + // HomeEnv is the variable that moves the agent's config directory, such as CODEX_HOME, or "" when none does. The + // CLI runs with it set to the cleaned absolute form of its value, or with it unset when the value is empty. + HomeEnv string } // Argv returns the command line of a stdio entry as argv[0] and its arguments. diff --git a/libs/process/opts.go b/libs/process/opts.go index a9848cebde3..94807687562 100644 --- a/libs/process/opts.go +++ b/libs/process/opts.go @@ -6,11 +6,18 @@ import ( "fmt" "io" "os/exec" + "runtime" + "slices" + "strings" "sync" + "time" ) type execOption func(context.Context, *exec.Cmd) error +// ExecOption is an option of Background and Forwarded. Code that passes options on to them names the type with it. +type ExecOption = execOption + func WithEnv(key, value string) execOption { return func(ctx context.Context, c *exec.Cmd) error { v := fmt.Sprintf("%s=%s", key, value) @@ -31,6 +38,37 @@ func WithEnvs(envs map[string]string) execOption { } } +// WithoutEnv removes key from the environment that the process inherits, so the process does not see the variable at all. +// It is not the same as setting the variable to an empty value, which a process may read as set. +func WithoutEnv(key string) execOption { + return func(_ context.Context, c *exec.Cmd) error { + c.Env = slices.DeleteFunc(c.Env, func(kv string) bool { + return envEntryHasKey(runtime.GOOS, kv, key) + }) + return nil + } +} + +// envEntryHasKey reports whether the environment entry kv, of the form key=value, sets key. Windows variable names are +// case-insensitive, so there the case of key does not matter. goos is a parameter so that tests can check both. +func envEntryHasKey(goos, kv, key string) bool { + name, _, _ := strings.Cut(kv, "=") + if goos == "windows" { + return strings.EqualFold(name, key) + } + return name == key +} + +// WithWaitDelay bounds how long Background waits for the output of the process once the process has exited or been +// killed. Without it, a child that a launcher started and that holds the output open would block Wait past a timeout. +// Unlike WithProcessGroup, it does not change the process group, so the process stays in the terminal's foreground group. +func WithWaitDelay(delay time.Duration) execOption { + return func(_ context.Context, c *exec.Cmd) error { + c.WaitDelay = delay + return nil + } +} + func WithDir(dir string) execOption { return func(_ context.Context, c *exec.Cmd) error { c.Dir = dir diff --git a/libs/process/opts_test.go b/libs/process/opts_test.go index 94838f4a7e1..6b67dc6c4e1 100644 --- a/libs/process/opts_test.go +++ b/libs/process/opts_test.go @@ -5,10 +5,12 @@ import ( "runtime" "slices" "testing" + "time" "github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestWithEnvs(t *testing.T) { @@ -26,6 +28,31 @@ func TestWithEnvs(t *testing.T) { assert.Equal(t, "foo delirium\n", res) } +func TestWithoutEnvRemovesVariable(t *testing.T) { + cmd := &exec.Cmd{Env: []string{"FOO=", "FOOBAR=bar", "BAZ=baz"}} + require.NoError(t, WithoutEnv("FOO")(t.Context(), cmd)) + assert.Equal(t, []string{"FOOBAR=bar", "BAZ=baz"}, cmd.Env) +} + +func TestEnvEntryHasKey(t *testing.T) { + assert.True(t, envEntryHasKey("linux", "CODEX_HOME=/a", "CODEX_HOME")) + assert.False(t, envEntryHasKey("linux", "Codex_Home=/a", "CODEX_HOME"), "variable names are case-sensitive off Windows") + assert.False(t, envEntryHasKey("linux", "CODEX_HOMEX=/a", "CODEX_HOME")) + assert.True(t, envEntryHasKey("windows", "Codex_Home=/a", "CODEX_HOME")) + assert.False(t, envEntryHasKey("windows", "CODEX_HOMEX=/a", "CODEX_HOME")) +} + +func TestWithWaitDelayReturnsWhileChildHoldsOutput(t *testing.T) { + if runtime.GOOS == "windows" { + // Skipping test on windows for now, as TestWithEnvs does: /bin/sh is not there. + t.Skip() + } + start := time.Now() + _, err := Background(t.Context(), []string{"/bin/sh", "-c", "sleep 3 & echo started"}, WithWaitDelay(100*time.Millisecond)) + assert.ErrorIs(t, err, exec.ErrWaitDelay, "the child keeps the output open, so the wait ends at the delay") + assert.Less(t, time.Since(start), 2*time.Second) +} + func TestWorksWithLibsEnv(t *testing.T) { testutil.CleanupEnvironment(t) ctx := t.Context()