From 8213903e3eda32ef003149cb57a7c9873c766d92 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 12:07:37 +0000 Subject: [PATCH 01/20] Add non-interactive aitools mcp add behind a hidden group `databricks aitools mcp add` installs Databricks managed MCP servers into coding agents, bound to one CLI profile. Each server is written as a stdio entry that runs `databricks aitools mcp serve`, which lands in a later change. The group stays hidden until the stack is complete. Co-authored-by: Isaac --- .../cmd/aitools/mcp/add-text/.databrickscfg | 3 + .../cmd/aitools/mcp/add-text/out.test.toml | 1 + .../cmd/aitools/mcp/add-text/output.txt | 76 ++ acceptance/cmd/aitools/mcp/add-text/script | 13 + .../aitools/mcp/add-two-agents/.databrickscfg | 3 + .../aitools/mcp/add-two-agents/out.test.toml | 1 + .../cmd/aitools/mcp/add-two-agents/output.txt | 154 ++++ .../cmd/aitools/mcp/add-two-agents/script | 16 + .../mcp/add-two-profiles/.databrickscfg | 7 + .../mcp/add-two-profiles/out.test.toml | 1 + .../aitools/mcp/add-two-profiles/output.txt | 53 ++ .../cmd/aitools/mcp/add-two-profiles/script | 14 + .../mcp/add-unparsable-config/.databrickscfg | 3 + .../home/.codex/config.toml | 2 + .../mcp/add-unparsable-config/out.test.toml | 1 + .../mcp/add-unparsable-config/output.txt | 50 ++ .../aitools/mcp/add-unparsable-config/script | 13 + .../cmd/aitools/mcp/hidden/out.test.toml | 1 + acceptance/cmd/aitools/mcp/hidden/output.txt | 37 + acceptance/cmd/aitools/mcp/hidden/script | 5 + acceptance/cmd/aitools/mcp/test.toml | 31 + cmd/aitools/aitools.go | 1 + cmd/aitools/mcp.go | 109 +++ cmd/aitools/mcp_add.go | 693 ++++++++++++++++++ cmd/aitools/mcp_add_test.go | 267 +++++++ cmd/aitools/mcp_binary.go | 26 + cmd/aitools/mcp_binary_test.go | 51 ++ cmd/aitools/mcp_test.go | 122 +++ 28 files changed, 1754 insertions(+) create mode 100644 acceptance/cmd/aitools/mcp/add-text/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/add-text/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/add-text/output.txt create mode 100644 acceptance/cmd/aitools/mcp/add-text/script create mode 100644 acceptance/cmd/aitools/mcp/add-two-agents/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/add-two-agents/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/add-two-agents/output.txt create mode 100644 acceptance/cmd/aitools/mcp/add-two-agents/script create mode 100644 acceptance/cmd/aitools/mcp/add-two-profiles/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/add-two-profiles/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/add-two-profiles/output.txt create mode 100644 acceptance/cmd/aitools/mcp/add-two-profiles/script create mode 100644 acceptance/cmd/aitools/mcp/add-unparsable-config/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml create mode 100644 acceptance/cmd/aitools/mcp/add-unparsable-config/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt create mode 100644 acceptance/cmd/aitools/mcp/add-unparsable-config/script create mode 100644 acceptance/cmd/aitools/mcp/hidden/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/hidden/output.txt create mode 100644 acceptance/cmd/aitools/mcp/hidden/script create mode 100644 acceptance/cmd/aitools/mcp/test.toml create mode 100644 cmd/aitools/mcp.go create mode 100644 cmd/aitools/mcp_add.go create mode 100644 cmd/aitools/mcp_add_test.go create mode 100644 cmd/aitools/mcp_binary.go create mode 100644 cmd/aitools/mcp_binary_test.go create mode 100644 cmd/aitools/mcp_test.go diff --git a/acceptance/cmd/aitools/mcp/add-text/.databrickscfg b/acceptance/cmd/aitools/mcp/add-text/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/add-text/out.test.toml b/acceptance/cmd/aitools/mcp/add-text/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt new file mode 100644 index 00000000000..4f1954ea338 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -0,0 +1,76 @@ + +=== Text output: Cursor approval hint, and the sign-in step for github + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,cursor +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code, cursor + +genie_one_mcp-logfood system.ai.genie_one_mcp + claude-code installed [TEST_TMP_DIR]/home/.claude.json + cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json +Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enable genie_one_mcp-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. + +github-logfood system.ai.github + claude-code installed [TEST_TMP_DIR]/home/.claude.json + cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json +system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood +Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. + +=== Running it again re-asserts the entries, and nothing is written twice + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,cursor +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code, cursor + +genie_one_mcp-logfood system.ai.genie_one_mcp + claude-code already installed, re-asserted [TEST_TMP_DIR]/home/.claude.json + cursor already installed, re-asserted [TEST_TMP_DIR]/home/.cursor/mcp.json +Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enable genie_one_mcp-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. + +github-logfood system.ai.github + claude-code already installed, re-asserted [TEST_TMP_DIR]/home/.claude.json + cursor already installed, re-asserted [TEST_TMP_DIR]/home/.cursor/mcp.json +system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood +Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. + +=== Cursor config + +>>> cat home/.cursor/mcp.json +{ + "mcpServers": { + "genie_one_mcp-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.genie_one_mcp", + "--agent", + "cursor" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + } + }, + "github-logfood": { + "args": [ + "aitools", + "mcp", + "serve", + "--profile", + "logfood", + "--service", + "system.ai.github", + "--agent", + "cursor" + ], + "command": "[CLI]", + "env": { + "DATABRICKS_CONFIG_FILE": "[TEST_TMP_DIR]/.databrickscfg" + } + } + } +} diff --git a/acceptance/cmd/aitools/mcp/add-text/script b/acceptance/cmd/aitools/mcp/add-text/script new file mode 100644 index 00000000000..06b05f7d9fd --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/script @@ -0,0 +1,13 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +title "Text output: Cursor approval hint, and the sign-in step for github" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,cursor + +title "Running it again re-asserts the entries, and nothing is written twice" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,cursor + +title "Cursor config" +trace cat home/.cursor/mcp.json diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/.databrickscfg b/acceptance/cmd/aitools/mcp/add-two-agents/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/out.test.toml b/acceptance/cmd/aitools/mcp/add-two-agents/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/output.txt b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt new file mode 100644 index 00000000000..2a91e7770e8 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt @@ -0,0 +1,154 @@ + +=== Two services for two agents, as JSON + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex --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": "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" + } + ] + } + ], + "error_category": "TYPE_UNSPECIFIED" +} + +=== Claude Code config keeps the entries it already had and gets ours + +>>> 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" + } + } +} + +=== Codex config + +>>> cat home/.codex/config.toml +[mcp_servers] + [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" + +=== State records the binding, without the time and CLI version + +>>> jq del(.servers[].installed_at, .servers[].cli_version) home/.databricks/aitools/mcp/state.json +{ + "schema_version": 1, + "servers": [ + { + "name": "genie_one_mcp-logfood", + "service": "system.ai.genie_one_mcp", + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "workspace_id": "[WORKSPACE_ID]", + "sign_in_required": false, + "command": "[CLI]", + "config_file": "[TEST_TMP_DIR]/.databrickscfg", + "agents": { + "claude-code": { + "path": "[TEST_TMP_DIR]/home/.claude.json", + "scope": "user" + }, + "codex": { + "path": "[TEST_TMP_DIR]/home/.codex/config.toml", + "scope": "user" + } + } + }, + { + "name": "github-logfood", + "service": "system.ai.github", + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "workspace_id": "[WORKSPACE_ID]", + "sign_in_required": true, + "command": "[CLI]", + "config_file": "[TEST_TMP_DIR]/.databrickscfg", + "agents": { + "claude-code": { + "path": "[TEST_TMP_DIR]/home/.claude.json", + "scope": "user" + }, + "codex": { + "path": "[TEST_TMP_DIR]/home/.codex/config.toml", + "scope": "user" + } + } + } + ] +} diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/script b/acceptance/cmd/aitools/mcp/add-two-agents/script new file mode 100644 index 00000000000..8f507b8cbb1 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -0,0 +1,16 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +title "Two services for two agents, as JSON" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex --output json + +title "Claude Code config keeps the entries it already had and gets ours" +trace cat home/.claude.json + +title "Codex config" +trace cat home/.codex/config.toml + +title "State records the binding, without the time and CLI version" +trace jq 'del(.servers[].installed_at, .servers[].cli_version)' home/.databricks/aitools/mcp/state.json diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/.databrickscfg b/acceptance/cmd/aitools/mcp/add-two-profiles/.databrickscfg new file mode 100644 index 00000000000..f685e4656ff --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/out.test.toml b/acceptance/cmd/aitools/mcp/add-two-profiles/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/output.txt b/acceptance/cmd/aitools/mcp/add-two-profiles/output.txt new file mode 100644 index 00000000000..0826da7ba77 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/output.txt @@ -0,0 +1,53 @@ + +=== The same service for a second profile adds an entry and renames nothing + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code + +genie_one_mcp-logfood system.ai.genie_one_mcp + claude-code installed [TEST_TMP_DIR]/home/.claude.json + +>>> [CLI] aitools mcp add --profile dogfood --names system.ai.genie_one_mcp --agents claude-code --output json +{ + "profile": "dogfood", + "servers": [ + { + "name": "genie_one_mcp-dogfood", + "agents": [ + { + "name": "claude-code", + "status": "installed" + } + ] + } + ] +} + +=== Both entries are in the Claude Code config + +>>> jq .mcpServers | keys home/.claude.json +[ + "genie_one_mcp-dogfood", + "genie_one_mcp-logfood" +] + +=== State holds one entry per profile + +>>> jq [.servers[] | {name, profile, agents: (.agents | keys)}] home/.databricks/aitools/mcp/state.json +[ + { + "name": "genie_one_mcp-logfood", + "profile": "logfood", + "agents": [ + "claude-code" + ] + }, + { + "name": "genie_one_mcp-dogfood", + "profile": "dogfood", + "agents": [ + "claude-code" + ] + } +] diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/script b/acceptance/cmd/aitools/mcp/add-two-profiles/script new file mode 100644 index 00000000000..971597474b5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/script @@ -0,0 +1,14 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +title "The same service for a second profile adds an entry and renames nothing" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code +trace $CLI aitools mcp add --profile dogfood --names system.ai.genie_one_mcp --agents claude-code --output json | jq '{profile, servers: [.servers[] | {name, agents: [.agents[] | {name, status}]}]}' + +title "Both entries are in the Claude Code config" +trace jq '.mcpServers | keys' home/.claude.json + +title "State holds one entry per profile" +trace jq '[.servers[] | {name, profile, agents: (.agents | keys)}]' home/.databricks/aitools/mcp/state.json diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/.databrickscfg b/acceptance/cmd/aitools/mcp/add-unparsable-config/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml b/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml new file mode 100644 index 00000000000..0beaf816e05 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml @@ -0,0 +1,2 @@ +[mcp_servers.other +command = "broken" diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/out.test.toml b/acceptance/cmd/aitools/mcp/add-unparsable-config/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt new file mode 100644 index 00000000000..8b319aeaf91 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt @@ -0,0 +1,50 @@ + +=== An unparsable config fails only its agent; the other agent still installs, and the command exits 1 + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex,claude-code --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": "codex", + "status": "failed", + "error_category": "AGENT_CONFIG_INVALID", + "message": "[TEST_TMP_DIR]/home/.codex/config.toml: agent config file cannot be parsed: toml: line 2: expected '.' or ']' to end table name, but got '\\n' instead" + }, + { + "name": "claude-code", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.claude.json" + } + ] + } + ], + "error_category": "TYPE_UNSPECIFIED" +} + +Exit code: 1 + +=== The broken file is left as it was + +>>> cat home/.codex/config.toml +[mcp_servers.other +command = "broken" + +=== State records only the agent that succeeded + +>>> jq [.servers[] | {name, agents: (.agents | keys)}] home/.databricks/aitools/mcp/state.json +[ + { + "name": "genie_one_mcp-logfood", + "agents": [ + "claude-code" + ] + } +] diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/script b/acceptance/cmd/aitools/mcp/add-unparsable-config/script new file mode 100644 index 00000000000..69de153f7d4 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/script @@ -0,0 +1,13 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +title "An unparsable config fails only its agent; the other agent still installs, and the command exits 1" +errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex,claude-code --output json + +title "The broken file is left as it was" +trace cat home/.codex/config.toml + +title "State records only the agent that succeeded" +trace jq '[.servers[] | {name, agents: (.agents | keys)}]' home/.databricks/aitools/mcp/state.json diff --git a/acceptance/cmd/aitools/mcp/hidden/out.test.toml b/acceptance/cmd/aitools/mcp/hidden/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/hidden/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt new file mode 100644 index 00000000000..b5dc617c68a --- /dev/null +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -0,0 +1,37 @@ + +=== The mcp group is hidden from aitools help + +>>> [CLI] aitools --help +0 + +=== The hidden group still answers help for its subcommands + +>>> [CLI] aitools mcp add --help +Install Databricks managed MCP servers into coding agents, bound to a Databricks profile. +Each server runs through 'databricks aitools mcp serve' with that profile's credentials. + +Servers come from --names: full names such as system.ai.github, or leaf names resolved in +--location (default system.ai). Agents come from --agents. Without it, every detected +MCP-capable agent is used. + +Interactive selection is not available yet: in a terminal, pass both --names and --agents. +With --output json, both are required. + +Supported agents: claude-code, cursor, codex, opencode, copilot, antigravity, gemini + +Usage: + databricks aitools mcp add [flags] + +Flags: + --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) + --force Overwrite an existing server with the same name that this CLI did not write + -h, --help help for add + --location string Schema to resolve leaf names in, as . (default system.ai) + --name string Server name to use instead of the generated one; only with exactly one service + --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated + +Global Flags: + --debug enable debug logging + -o, --output type output type: text or json (default text) + -p, --profile string ~/.databrickscfg profile + -t, --target string bundle target to use (if applicable) diff --git a/acceptance/cmd/aitools/mcp/hidden/script b/acceptance/cmd/aitools/mcp/hidden/script new file mode 100644 index 00000000000..f96bd778250 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/hidden/script @@ -0,0 +1,5 @@ +title "The mcp group is hidden from aitools help" +trace $CLI aitools --help | grep -c ' mcp ' || true + +title "The hidden group still answers help for its subcommands" +trace $CLI aitools mcp add --help diff --git a/acceptance/cmd/aitools/mcp/test.toml b/acceptance/cmd/aitools/mcp/test.toml new file mode 100644 index 00000000000..25de79431e7 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/test.toml @@ -0,0 +1,31 @@ +# The fake workspace lists two system.ai services: genie_one_mcp needs no sign-in and github +# is backed by an OAuth connection, so it needs sign-in. The list is the same for every +# query, which is enough for one schema. +Ignore = [ + "home", +] + +[[Server]] +Pattern = "GET /api/2.1/unity-catalog/mcp-services" +Response.Body = ''' +{ + "mcp_services": [ + { + "name": "mcp-services/system.ai.genie_one_mcp", + "comment": "Ask questions about your data" + }, + { + "name": "mcp-services/system.ai.github", + "comment": "Repositories, issues, pull requests", + "config": { + "source_connection": { + "name": "connections/github", + "options": { + "oauth_provider": "github" + } + } + } + } + ] +} +''' diff --git a/cmd/aitools/aitools.go b/cmd/aitools/aitools.go index 451e8cee523..41f6d813307 100644 --- a/cmd/aitools/aitools.go +++ b/cmd/aitools/aitools.go @@ -25,6 +25,7 @@ https://github.com/databricks/databricks-agent-skills`, cmd.AddCommand(NewUninstallCmd()) cmd.AddCommand(NewListCmd()) cmd.AddCommand(NewVersionCmd()) + cmd.AddCommand(NewMcpCmd()) return cmd } diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go new file mode 100644 index 00000000000..aff40464caa --- /dev/null +++ b/cmd/aitools/mcp.go @@ -0,0 +1,109 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "strings" + + "github.com/databricks/cli/libs/aitools/mcp/agentconfig" + "github.com/databricks/cli/libs/databrickscfg" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/spf13/cobra" +) + +// defaultMCPLocation is the schema that ships the Databricks managed MCP services. +const defaultMCPLocation = "system.ai" + +// Error categories that the telemetry enum does not define yet. +// The JSON output reports them by name. +const ( + categoryAgentConfigInvalid protos.AitoolsErrorCategory = "AGENT_CONFIG_INVALID" + categoryAuthFailed protos.AitoolsErrorCategory = "AUTH_FAILED" + categoryProfileNotFound protos.AitoolsErrorCategory = "PROFILE_NOT_FOUND" + categoryProfileHostChanged protos.AitoolsErrorCategory = "PROFILE_HOST_CHANGED" + categoryServiceNotFound protos.AitoolsErrorCategory = "MCP_SERVICE_NOT_FOUND" +) + +// NewMcpCmd returns the `aitools mcp` group. It stays hidden until the whole MCP +// stack has landed, so a release never ships `add` without `serve`. +func NewMcpCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "mcp", + Short: "Manage Databricks managed MCP servers for coding agents", + Hidden: true, + } + cmd.AddCommand(NewMcpAddCmd()) + return cmd +} + +// cliError is a failure with the message shown to the user, the category reported in JSON +// and telemetry, and the error it wraps when there is one. +type cliError struct { + category protos.AitoolsErrorCategory + message string + cause error +} + +func (e *cliError) Error() string { return e.message } + +func (e *cliError) Unwrap() error { return e.cause } + +// failure returns a cliError. cause is nil when the message is the whole failure. +func failure(category protos.AitoolsErrorCategory, cause error, format string, args ...any) error { + return &cliError{category: category, message: fmt.Sprintf(format, args...), cause: cause} +} + +// errorCategory classifies an error by its typed errors only, never by its text. +// Errors without a typed category are UNCATEGORIZED. +func errorCategory(err error) protos.AitoolsErrorCategory { + if ce, ok := errors.AsType[*cliError](err); ok { + return ce.category + } + if errors.Is(err, agentconfig.ErrConfigInvalid) { + return categoryAgentConfigInvalid + } + return protos.AitoolsErrorCategoryUncategorized +} + +// resolveProfile picks the profile a non-interactive command binds to: the +// --profile flag, then DATABRICKS_CONFIG_PROFILE, then the default profile, then the only +// workspace profile. It never prompts. +func resolveProfile(ctx context.Context, flagValue string) (string, error) { + if flagValue != "" { + return flagValue, nil + } + if name := env.Get(ctx, "DATABRICKS_CONFIG_PROFILE"); name != "" { + return name, nil + } + if name := databrickscfg.ResolveDefaultProfile(ctx); name != "" { + return name, nil + } + + profiler := profile.GetProfiler(ctx) + path, err := profiler.GetPath(ctx) + if err != nil { + return "", err + } + profiles, err := profiler.LoadProfiles(ctx, profile.MatchWorkspaceProfiles) + if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { + return "", err + } + switch len(profiles) { + case 0: + return "", fmt.Errorf("no workspace profiles in %s. Run: databricks auth login --host --profile ", path) + case 1: + return profiles[0].Name, nil + } + return "", fmt.Errorf("several workspace profiles exist; pass --profile. Found: %s", strings.Join(profiles.Names(), ", ")) +} + +// profileFlag returns the root --profile value, or "" when it is not set. +func profileFlag(cmd *cobra.Command) string { + if f := cmd.Flag("profile"); f != nil { + return f.Value.String() + } + return "" +} diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go new file mode 100644 index 00000000000..ca00243b3ff --- /dev/null +++ b/cmd/aitools/mcp_add.go @@ -0,0 +1,693 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "net/http" + "path/filepath" + "regexp" + "slices" + "strconv" + "strings" + "time" + + "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/internal/build" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/aitools/mcp/agentconfig" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/databrickscfg" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/config" + "github.com/spf13/cobra" +) + +// serverNamePattern is the strictest server-name rule among the agents. +var serverNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) + +// Per-agent results of `mcp add`. +const ( + statusInstalled = "installed" + statusReasserted = "reasserted" + statusFailed = "failed" +) + +// cursorAgent is the registry name of Cursor, which needs a server approved before use. +const cursorAgent = "cursor" + +type mcpAddFlags struct { + names string + location string + agents string + name string + force bool +} + +// addOutput is the `mcp add --output json` document. +type addOutput struct { + Profile string `json:"profile"` + Host string `json:"host"` + Scope string `json:"scope"` + Servers []addServerOutput `json:"servers"` + ErrorCategory protos.AitoolsErrorCategory `json:"error_category"` + Error string `json:"error,omitempty"` +} + +type addServerOutput struct { + Name string `json:"name"` + Service string `json:"service"` + SignInRequired bool `json:"sign_in_required"` + Agents []addAgentOutput `json:"agents"` +} + +type addAgentOutput struct { + Name string `json:"name"` + Status string `json:"status"` + Path string `json:"path,omitempty"` + ErrorCategory protos.AitoolsErrorCategory `json:"error_category,omitempty"` + Message string `json:"message,omitempty"` + + // err is the failure of a failed agent, for the exit status. It is not in the JSON. + err error +} + +// addPlan is one service to install for the profile. entry is the state record it updates, +// nil for a new install. +type addPlan struct { + service mcp.Service + name string + entry *mcp.Entry +} + +// addRequest is what one `mcp add` run applies to every server. +type addRequest struct { + // jsonMode suppresses the text output; the JSON document is the only output. + jsonMode bool + profile string + host string + workspaceID string + location string + agents []*agents.Agent + explicit bool + force bool + binary string + configFile string + // env is the entry env: DATABRICKS_CONFIG_FILE when it was set. + env map[string]string +} + +// NewMcpAddCmd returns `aitools mcp add`. +func NewMcpAddCmd() *cobra.Command { + var f mcpAddFlags + cmd := &cobra.Command{ + Use: "add", + Short: "Install Databricks managed MCP servers into coding agents", + Long: `Install Databricks managed MCP servers into coding agents, bound to a Databricks profile. +Each server runs through 'databricks aitools mcp serve' with that profile's credentials. + +Servers come from --names: full names such as system.ai.github, or leaf names resolved in +--location (default system.ai). Agents come from --agents. Without it, every detected +MCP-capable agent is used. + +Interactive selection is not available yet: in a terminal, pass both --names and --agents. +With --output json, both are required. + +Supported agents: ` + strings.Join(mcpAgentNames(), ", "), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + jsonMode := installOutputIsJSON(cmd) + if jsonMode { + // JSON mode writes only the result document to stdout. + ctx = cmdio.WithQuiet(ctx) + } + + out := &addOutput{ + Scope: "global", + Servers: []addServerOutput{}, + ErrorCategory: protos.AitoolsErrorCategoryUnspecified, + } + runErr := runMcpAdd(ctx, cmd, f, jsonMode, out) + if !jsonMode { + return runErr + } + + // The document carries a top-level failure; per-agent failures are already in its agents. + if top := topLevelFailure(runErr); top != nil { + out.Error = top.Error() + out.ErrorCategory = errorCategory(top) + } + if err := renderJSON(cmd.OutOrStdout(), out); err != nil { + return err + } + if runErr != nil { + return root.ErrAlreadyPrinted + } + return nil + }, + } + + cmd.Flags().StringVar(&f.names, "names", "", "Services to install: full names (system.ai.github) or leaf names (github), comma-separated") + cmd.Flags().StringVar(&f.location, "location", "", "Schema to resolve leaf names in, as . (default system.ai)") + cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to install for, comma-separated (default: every detected MCP-capable agent)") + cmd.Flags().StringVar(&f.name, "name", "", "Server name to use instead of the generated one; only with exactly one service") + cmd.Flags().BoolVar(&f.force, "force", false, "Overwrite an existing server with the same name that this CLI did not write") + return cmd +} + +// runMcpAdd installs the requested servers and fills out. A failure of an agent named with +// --agents is returned as an agentErrors; detected agents only report theirs. +func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode bool, out *addOutput) error { + names := splitAndTrim(f.names) + agentNames := splitAndTrim(f.agents) + if err := validateMCPAddFlags(ctx, f, names, agentNames, jsonMode); err != nil { + return err + } + location := f.location + if location == "" { + location = defaultMCPLocation + } + if err := validateLocation(location); err != nil { + return err + } + + profileName, err := resolveProfile(ctx, profileFlag(cmd)) + if err != nil { + return err + } + out.Profile = profileName + if err := requireProfile(ctx, profileName); err != nil { + return err + } + w, err := profileClient(profileName) + if err != nil { + return err + } + out.Host = w.Config.Host + workspaceID, err := workspaceIDOf(ctx, w) + if err != nil { + return err + } + + targets, explicit, err := targetAgents(ctx, agentNames) + if err != nil { + return err + } + + services, err := mcp.Discover(ctx, w, location) + if err != nil { + return describeListError(profileName, err) + } + picked, err := pickServices(location, names, services) + if err != nil { + return err + } + + state, err := mcp.LoadState(ctx) + if err != nil { + return err + } + plans, err := planAdds(ctx, f.name, location, profileName, w.Config.Host, state, picked, targets) + if err != nil { + return err + } + + configFile, err := pinnedConfigFile(ctx) + if err != nil { + return err + } + binary, absolute := mcpBinaryPath() + req := addRequest{ + jsonMode: jsonMode, + profile: profileName, + host: w.Config.Host, + workspaceID: workspaceID, + location: location, + agents: targets, + explicit: explicit, + force: f.force, + binary: binary, + configFile: configFile, + } + if configFile != "" { + req.env = map[string]string{"DATABRICKS_CONFIG_FILE": configFile} + } + if !absolute && !jsonMode { + cmdio.LogString(ctx, "Note: databricks is not an absolute path; coding agents must find it on PATH to start the MCP servers.") + } + + printAddHeader(ctx, req) + servers := make([]addServerOutput, 0, len(plans)) + for _, plan := range plans { + servers = append(servers, installServer(ctx, req, plan)) + } + out.Servers = servers + + if err := recordInstalls(ctx, req, plans, servers); err != nil { + return err + } + printAddResults(ctx, req, servers) + return agentFailureError(req, servers) +} + +// validateMCPAddFlags rejects flag combinations that cannot run, before anything is read or written. +func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames []string, jsonMode bool) error { + if f.name != "" { + if len(names) != 1 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service in --names") + } + if !serverNamePattern.MatchString(f.name) { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %q may only contain letters, digits, '_' and '-'", f.name) + } + } + + if jsonMode { + var missing []string + if len(names) == 0 { + missing = append(missing, "--names") + } + if len(agentNames) == 0 { + missing = append(missing, "--agents") + } + if len(missing) > 0 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--output json requires %s so the command runs without interactive prompts", strings.Join(missing, " and ")) + } + return nil + } + if cmdio.IsPromptSupported(ctx) { + if len(names) == 0 || len(agentNames) == 0 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "interactive selection is not available yet; pass --names and --agents") + } + return nil + } + if len(names) == 0 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--names is required so the command runs without interactive prompts") + } + return nil +} + +func validateLocation(location string) error { + catalog, schema, ok := strings.Cut(location, ".") + if !ok || catalog == "" || schema == "" || strings.Contains(schema, ".") { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--location must be ., got %q", location) + } + return nil +} + +// requireProfile fails when the named profile is not in the Databricks config file. +func requireProfile(ctx context.Context, name string) error { + profiler := profile.GetProfiler(ctx) + profiles, err := profiler.LoadProfiles(ctx, profile.WithName(name)) + if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { + return err + } + if len(profiles) > 0 { + return nil + } + path, err := profiler.GetPath(ctx) + if err != nil { + return err + } + return failure(categoryProfileNotFound, nil, "Profile %s not found in %s", name, path) +} + +// profileClient returns a workspace client bound to exactly the named profile. It never prompts +// and never falls back to another profile or to environment credentials. +func profileClient(name string) (*databricks.WorkspaceClient, error) { + cfg := &config.Config{Profile: name, Loaders: databrickscfg.ProfileAuthLoaders} + w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) + if err == nil { + err = w.Config.Authenticate(&http.Request{Header: make(http.Header)}) + } + if err != nil { + return nil, failure(categoryAuthFailed, err, "%s\nRun: databricks auth login --profile %s", err, name) + } + return w, nil +} + +// workspaceIDOf returns the workspace ID recorded for the profile: its workspace_id when set, +// otherwise the ID that the host reports. +func workspaceIDOf(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { + if id := w.Config.WorkspaceID; id != "" && id != profile.WorkspaceIDNone { + return id, nil + } + id, err := w.CurrentWorkspaceID(ctx) + if err != nil { + return "", fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err) + } + return strconv.FormatInt(id, 10), nil +} + +// describeListError names the profile that made a failed listing call. The +// ListError text already carries the server's message, so it is only prefixed. The cause +// keeps the *mcp.ListError for errors.As. +func describeListError(profileName string, err error) error { + listErr, ok := errors.AsType[*mcp.ListError](err) + if !ok { + return err + } + category := protos.AitoolsErrorCategoryUncategorized + if listErr.Kind == mcp.ListErrRateLimited { + category = protos.AitoolsErrorCategoryRateLimited + } + return failure(category, listErr, "profile %s: %s", profileName, listErr.Error()) +} + +// targetAgents returns the agents named by --agents, or else every detected MCP-capable agent. +// explicit reports whether the names were given. +func targetAgents(ctx context.Context, names []string) ([]*agents.Agent, bool, error) { + if len(names) > 0 { + list, err := resolveMCPAgents(names) + return list, true, err + } + var detected []*agents.Agent + for _, a := range agents.MCPAgents() { + if a.Detected(ctx) { + detected = append(detected, a) + } + } + if len(detected) == 0 { + return nil, false, fmt.Errorf("no MCP-capable coding agents detected; pass --agents with one of: %s", strings.Join(mcpAgentNames(), ", ")) + } + return detected, false, nil +} + +// resolveMCPAgents maps --agents names to MCP-capable agents. Unknown and non-MCP agents are errors. +func resolveMCPAgents(names []string) ([]*agents.Agent, error) { + supported := strings.Join(mcpAgentNames(), ", ") + var result []*agents.Agent + for _, name := range names { + a := agents.ByName(name) + switch { + case a == nil: + return nil, failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "unknown agent %q. Supported: %s", name, supported) + case !a.SupportsMCP(): + return nil, failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%s does not support MCP servers. Supported: %s", name, supported) + } + if !slices.Contains(result, a) { + result = append(result, a) + } + } + return result, nil +} + +func mcpAgentNames() []string { + var names []string + for _, a := range agents.MCPAgents() { + names = append(names, a.Name) + } + return names +} + +// pickServices maps --names to the discovered services of location. A full name must be inside +// location; a leaf name is looked up in it. +func pickServices(location string, names []string, services []mcp.Service) ([]mcp.Service, error) { + var picked []mcp.Service + for _, name := range names { + leaf, inLocation := leafName(name, location) + if !inLocation { + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not in %s; pass --location to match its schema", name, location) + } + i := slices.IndexFunc(services, func(s mcp.Service) bool { return strings.EqualFold(s.Leaf, leaf) }) + if i < 0 { + available := make([]string, 0, len(services)) + for _, s := range services { + available = append(available, s.FullName) + } + return nil, failure(categoryServiceNotFound, nil, "%s.%s is not an MCP service in %s. Available: %s", location, leaf, location, strings.Join(available, ", ")) + } + if !slices.ContainsFunc(picked, func(s mcp.Service) bool { return s.FullName == services[i].FullName }) { + picked = append(picked, services[i]) + } + } + return picked, nil +} + +// leafName returns the leaf of a name given as a leaf or as a full name, and whether a full +// name is inside location. Unity Catalog names are case-insensitive, so the comparison is too. +func leafName(name, location string) (string, bool) { + parts := strings.Split(name, ".") + if len(parts) != 3 { + return name, true + } + return parts[2], strings.EqualFold(parts[0]+"."+parts[1], location) +} + +// planAdds chooses the server name of each picked service before anything is written. A service +// already installed for this profile keeps its name. A new name avoids names of other state +// entries and of unmanaged entries in the target agents. +func planAdds(ctx context.Context, nameFlag, location, profileName, host string, state *mcp.State, picked []mcp.Service, targets []*agents.Agent) ([]addPlan, error) { + taken := map[string]bool{} + for i := range state.Servers { + taken[state.Servers[i].Name] = true + } + for _, a := range targets { + for name := range unmanagedNames(ctx, a) { + taken[name] = true + } + } + + plans := make([]addPlan, 0, len(picked)) + for _, svc := range picked { + entry := stateEntry(state, svc.FullName, profileName) + if entry != nil && mcp.NormalizeHost(entry.Host) != mcp.NormalizeHost(host) { + return nil, failure(categoryProfileHostChanged, nil, "MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: databricks aitools mcp remove --profile %s and add it again.", entry.Name, entry.Host, profileName, host, profileName) + } + + var name string + switch { + case entry != nil && nameFlag != "" && nameFlag != entry.Name: + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is already installed on profile %s as %s; remove it before using --name %s", svc.FullName, profileName, entry.Name, nameFlag) + case entry != nil: + name = entry.Name + case nameFlag != "": + if slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { return e.Name == nameFlag }) { + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another installed server; pick a different name", nameFlag) + } + name = nameFlag + default: + name = mcp.UniqueName(mcp.ServerName(svc.FullName, profileName), func(n string) bool { return taken[n] }) + } + taken[name] = true + plans = append(plans, addPlan{service: svc, name: name, entry: entry}) + } + return plans, nil +} + +// unmanagedNames returns the server names in the agent's config that this CLI did not write. A +// config that cannot be read has none here; Upsert reports that failure for the agent. +func unmanagedNames(ctx context.Context, a *agents.Agent) map[string]bool { + entries, err := agentconfig.Read(ctx, a) + if err != nil { + return nil + } + names := make(map[string]bool, len(entries)) + for name, entry := range entries { + if !a.MCP.IsOurs(entry) { + names[name] = true + } + } + return names +} + +func stateEntry(state *mcp.State, service, profileName string) *mcp.Entry { + i := slices.IndexFunc(state.Servers, func(e mcp.Entry) bool { + return e.Service == service && e.Profile == profileName + }) + if i < 0 { + return nil + } + return &state.Servers[i] +} + +// pinnedConfigFile returns the absolute DATABRICKS_CONFIG_FILE, or "" when it is unset. +func pinnedConfigFile(ctx context.Context) (string, error) { + value := env.Get(ctx, "DATABRICKS_CONFIG_FILE") + if value == "" { + return "", nil + } + return filepath.Abs(value) +} + +// installServer writes one server into every target agent. Each agent's result is reported; one +// agent's failure does not stop the others. +func installServer(ctx context.Context, req addRequest, plan addPlan) addServerOutput { + out := addServerOutput{ + Name: plan.name, + Service: plan.service.FullName, + SignInRequired: plan.service.SignInRequired, + Agents: make([]addAgentOutput, 0, len(req.agents)), + } + for _, a := range req.agents { + args := []string{"aitools", "mcp", "serve", "--profile", req.profile, "--service", plan.service.FullName, "--agent", a.Name} + path, _, err := agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) + + result := addAgentOutput{Name: a.Name} + switch { + case err != nil: + result.Status = statusFailed + result.ErrorCategory = errorCategory(err) + result.Message = err.Error() + result.err = err + case plan.installedFor(a.Name): + result.Status = statusReasserted + result.Path = path + default: + result.Status = statusInstalled + result.Path = path + } + out.Agents = append(out.Agents, result) + } + return out +} + +// installedFor reports whether state already records the server for the agent. +func (p addPlan) installedFor(agent string) bool { + if p.entry == nil { + return false + } + _, ok := p.entry.Agents[agent] + return ok +} + +// recordInstalls adds each server that reached at least one agent to the state file. The file is +// re-read first, so entries written since planning are kept. +func recordInstalls(ctx context.Context, req addRequest, plans []addPlan, servers []addServerOutput) error { + state, err := mcp.LoadState(ctx) + if err != nil { + return err + } + recorded := false + for i, server := range servers { + paths := map[string]string{} + for _, a := range server.Agents { + if a.Status != statusFailed { + paths[a.Name] = a.Path + } + } + if len(paths) == 0 { + continue + } + recordServer(state, req, plans[i], paths) + recorded = true + } + if !recorded { + return nil + } + return mcp.SaveState(ctx, state) +} + +// recordServer stores the install of plan's server for req.profile, with the agents that succeeded +// and their config paths. +func recordServer(state *mcp.State, req addRequest, plan addPlan, paths map[string]string) { + entry := stateEntry(state, plan.service.FullName, req.profile) + if entry == nil { + state.Servers = append(state.Servers, mcp.Entry{ + Name: plan.name, + Service: plan.service.FullName, + Profile: req.profile, + InstalledAt: time.Now().UTC(), + }) + entry = &state.Servers[len(state.Servers)-1] + } + entry.Host = req.host + entry.WorkspaceID = req.workspaceID + entry.SignInRequired = plan.service.SignInRequired + entry.Command = req.binary + entry.ConfigFile = req.configFile + entry.CLIVersion = build.GetInfo().Version + if entry.Agents == nil { + // A hand-edited state file can hold "agents": null. + entry.Agents = map[string]mcp.AgentRecord{} + } + for agent, path := range paths { + entry.Agents[agent] = mcp.AgentRecord{Path: path, Scope: "user"} + } +} + +// agentFailureError returns the failures of agents named with --agents. Agents picked by detection +// only warn, and the results already show them. +func agentFailureError(req addRequest, servers []addServerOutput) error { + if !req.explicit { + return nil + } + var errs []error + for _, server := range servers { + for _, a := range server.Agents { + if a.err != nil { + errs = append(errs, a.err) + } + } + } + if len(errs) == 0 { + return nil + } + return &agentErrors{err: errors.Join(errs...)} +} + +func printAddHeader(ctx context.Context, req addRequest) { + if req.jsonMode { + return + } + cmdio.LogString(ctx, fmt.Sprintf("Profile %s (%s)", req.profile, req.host)) + names := make([]string, 0, len(req.agents)) + for _, a := range req.agents { + names = append(names, a.Name) + } + source := "named with --agents" + if !req.explicit { + source = "detected" + } + cmdio.LogString(ctx, fmt.Sprintf("Agents (%s): %s", source, strings.Join(names, ", "))) +} + +// printAddResults prints each server, its agents, and the follow-up steps the server needs. +func printAddResults(ctx context.Context, req addRequest, servers []addServerOutput) { + if req.jsonMode { + return + } + for _, server := range servers { + cmdio.LogString(ctx, "") + cmdio.LogString(ctx, server.Name+" "+server.Service) + succeeded := false + cursor := false + for _, a := range server.Agents { + cmdio.LogString(ctx, " "+agentResultLine(ctx, a)) + if a.Status != statusFailed { + succeeded = true + } + if a.Name == cursorAgent && a.Status != statusFailed { + cursor = true + } + } + if server.SignInRequired && succeeded { + cmdio.LogString(ctx, fmt.Sprintf("%s needs sign-in. Run: databricks aitools mcp login %s --profile %s", server.Service, server.Service, req.profile)) + } + if cursor { + cmdio.LogString(ctx, cursorApprovalHint(server.Name)) + } + } +} + +func agentResultLine(ctx context.Context, a addAgentOutput) string { + switch a.Status { + case statusFailed: + return cmdio.Yellow(ctx, fmt.Sprintf("%-12s failed: %s", a.Name, a.Message)) + case statusReasserted: + return fmt.Sprintf("%-12s already installed, re-asserted %s", a.Name, a.Path) + default: + return fmt.Sprintf("%-12s installed %s", a.Name, a.Path) + } +} + +// cursorApprovalHint explains the approval step Cursor asks for on a new server. The Cursor docs +// (https://cursor.com/docs/cli/mcp) call the CLI "agent" and list "agent mcp enable "; +// the installed binary is cursor-agent, so the hint names that. +func cursorApprovalHint(name string) string { + return fmt.Sprintf("Cursor may ask you to approve %s. Run: cursor-agent mcp enable %s (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings.", name, name) +} diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go new file mode 100644 index 00000000000..def42bb799c --- /dev/null +++ b/cmd/aitools/mcp_add_test.go @@ -0,0 +1,267 @@ +package aitools + +import ( + "bytes" + "encoding/json" + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestValidateMCPAddFlags(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + cases := []struct { + name string + flags mcpAddFlags + names []string + agentNames []string + jsonMode bool + want string + }{ + {name: "text with names only is valid", names: []string{"github"}}, + {name: "text without names", want: "--names is required so the command runs without interactive prompts"}, + { + name: "json needs agents", names: []string{"github"}, jsonMode: true, + want: "--output json requires --agents so the command runs without interactive prompts", + }, + { + name: "json needs names and agents", jsonMode: true, + want: "--output json requires --names and --agents so the command runs without interactive prompts", + }, + {name: "json with both", names: []string{"github"}, agentNames: []string{"codex"}, jsonMode: true}, + { + name: "name needs one service", flags: mcpAddFlags{name: "x"}, names: []string{"a", "b"}, + want: "--name needs exactly one service in --names", + }, + { + name: "name must be a valid server name", flags: mcpAddFlags{name: "a.b"}, names: []string{"github"}, + want: `--name "a.b" may only contain letters, digits, '_' and '-'`, + }, + {name: "valid name", flags: mcpAddFlags{name: "my_github-1"}, names: []string{"github"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := validateMCPAddFlags(ctx, tc.flags, tc.names, tc.agentNames, tc.jsonMode) + if tc.want == "" { + assert.NoError(t, err) + return + } + require.Error(t, err) + assert.Equal(t, tc.want, err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + }) + } +} + +func TestValidateLocation(t *testing.T) { + assert.NoError(t, validateLocation("system.ai")) + assert.NoError(t, validateLocation("Main.Tools")) + for _, bad := range []string{"system", "system.", ".ai", "a.b.c"} { + err := validateLocation(bad) + require.Error(t, err, bad) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + } +} + +func testServices() []mcp.Service { + return []mcp.Service{ + {FullName: "system.ai.genie_one_mcp", Leaf: "genie_one_mcp"}, + {FullName: "system.ai.github", Leaf: "github", SignInRequired: true}, + } +} + +func TestPickServices(t *testing.T) { + services := testServices() + + picked, err := pickServices("system.ai", []string{"github", "system.ai.genie_one_mcp", "github"}, services) + require.NoError(t, err) + require.Len(t, picked, 2) + assert.Equal(t, "system.ai.github", picked[0].FullName) + assert.Equal(t, "system.ai.genie_one_mcp", picked[1].FullName) + + picked, err = pickServices("System.AI", []string{"GitHub"}, services) + require.NoError(t, err) + assert.Equal(t, "system.ai.github", picked[0].FullName) + + _, err = pickServices("system.ai", []string{"system.ai.foo"}, services) + require.Error(t, err) + assert.Equal(t, "system.ai.foo is not an MCP service in system.ai. Available: system.ai.genie_one_mcp, system.ai.github", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategory("MCP_SERVICE_NOT_FOUND"), errorCategory(err)) + + _, err = pickServices("system.ai", []string{"main.tools.crm"}, services) + require.Error(t, err) + assert.Equal(t, "main.tools.crm is not in system.ai; pass --location to match its schema", err.Error()) +} + +func TestResolveMCPAgents(t *testing.T) { + got, err := resolveMCPAgents([]string{"codex", "claude-code", "codex"}) + require.NoError(t, err) + require.Len(t, got, 2) + assert.Equal(t, "codex", got[0].Name) + assert.Equal(t, "claude-code", got[1].Name) + + _, err = resolveMCPAgents([]string{"nope"}) + require.Error(t, err) + assert.Equal(t, `unknown agent "nope". Supported: claude-code, cursor, codex, opencode, copilot, antigravity, gemini`, err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(err)) + + _, err = resolveMCPAgents([]string{"pi"}) + require.Error(t, err) + assert.True(t, strings.HasPrefix(err.Error(), "pi does not support MCP servers. Supported: "), err.Error()) +} + +func TestDescribeListErrorNamesProfileAndKeepsCause(t *testing.T) { + cause := errors.New("403 Forbidden") + listErr := &mcp.ListError{Kind: mcp.ListErrPermissionDenied, Location: "main.tools", Err: cause} + + err := describeListError("logfood", listErr) + assert.Equal(t, "profile logfood: "+listErr.Error(), err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) + + got, ok := errors.AsType[*mcp.ListError](err) + require.True(t, ok) + assert.Equal(t, mcp.ListErrPermissionDenied, got.Kind) + assert.ErrorIs(t, err, cause) + + rateLimited := describeListError("logfood", &mcp.ListError{Kind: mcp.ListErrRateLimited, Location: "system.ai", Err: cause}) + assert.Equal(t, protos.AitoolsErrorCategoryRateLimited, errorCategory(rateLimited)) + + plain := errors.New("network down") + assert.Equal(t, plain, describeListError("logfood", plain)) +} + +// withHome points the home directory at a temp dir, so agent config paths are inside it. +func withHome(t *testing.T) string { + t.Helper() + home := t.TempDir() + t.Setenv(env.HomeEnvVar(), home) + t.Setenv("CLAUDE_CONFIG_DIR", "") + t.Setenv("CODEX_HOME", "") + return home +} + +func TestPlanAddsNaming(t *testing.T) { + ctx := t.Context() + claude := agents.ByName("claude-code") + host := "https://adb-123.test" + + t.Run("new service gets the generated name", func(t *testing.T) { + withHome(t) + plans, err := planAdds(ctx, "", "system.ai", "logfood", host, &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) + require.NoError(t, err) + require.Len(t, plans, 1) + assert.Equal(t, "genie_one_mcp-logfood", plans[0].name) + assert.Nil(t, plans[0].entry) + }) + + t.Run("existing install keeps its name", func(t *testing.T) { + withHome(t) + state := &mcp.State{Servers: []mcp.Entry{{Name: "custom-name", Service: "system.ai.github", Profile: "logfood", Host: host}}} + plans, err := planAdds(ctx, "", "system.ai", "logfood", host, state, testServices()[1:], []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "custom-name", plans[0].name) + assert.NotNil(t, plans[0].entry) + }) + + t.Run("name used by another state entry gets a suffix", func(t *testing.T) { + withHome(t) + state := &mcp.State{Servers: []mcp.Entry{{Name: "genie_one_mcp-logfood", Service: "main.tools.other", Profile: "logfood", Host: host}}} + plans, err := planAdds(ctx, "", "system.ai", "logfood", host, state, testServices()[:1], []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) + }) + + t.Run("unmanaged entry in an agent file gets a suffix", func(t *testing.T) { + home := withHome(t) + unmanaged := `{"mcpServers":{"genie_one_mcp-logfood":{"type":"stdio","command":"ug","args":["mcp-proxy"]}}}` + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(unmanaged), 0o600)) + plans, err := planAdds(ctx, "", "system.ai", "logfood", host, &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) + }) + + t.Run("name flag for an installed service must match", func(t *testing.T) { + withHome(t) + state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host}}} + _, err := planAdds(ctx, "other", "system.ai", "logfood", host, state, testServices()[1:], []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, "system.ai.github is already installed on profile logfood as github-logfood; remove it before using --name other", err.Error()) + }) + + t.Run("name flag already used by another service is refused", func(t *testing.T) { + withHome(t) + state := &mcp.State{Servers: []mcp.Entry{{Name: "taken", Service: "main.tools.other", Profile: "logfood", Host: host}}} + _, err := planAdds(ctx, "taken", "system.ai", "logfood", host, state, testServices()[:1], []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, "--name taken is used by another installed server; pick a different name", err.Error()) + }) + + t.Run("host change is refused, never rebound", func(t *testing.T) { + withHome(t) + state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://old.test"}}} + _, err := planAdds(ctx, "", "system.ai", "logfood", host, state, testServices()[1:], []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) + assert.Contains(t, err.Error(), "was set up for https://old.test, but profile \"logfood\" now points at "+host) + }) +} + +func TestPinnedConfigFile(t *testing.T) { + ctx := t.Context() + t.Setenv("DATABRICKS_CONFIG_FILE", "") + got, err := pinnedConfigFile(ctx) + require.NoError(t, err) + assert.Empty(t, got) + + t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join("rel", ".databrickscfg")) + got, err = pinnedConfigFile(ctx) + require.NoError(t, err) + assert.True(t, filepath.IsAbs(got), got) +} + +func TestCursorApprovalHintNamesBinary(t *testing.T) { + assert.Equal(t, + "Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings.", + cursorApprovalHint("github-logfood")) +} + +// newAddTestCmd builds `aitools mcp add` under a root with the --output flag, as root does. +func newAddTestCmd(t *testing.T, args ...string) (*bytes.Buffer, error) { + t.Helper() + parent := &cobra.Command{Use: "databricks", SilenceErrors: true, SilenceUsage: true} + output := flags.OutputText + parent.PersistentFlags().VarP(&output, "output", "o", "output type: text or json") + parent.AddCommand(NewMcpCmd()) + + var stdout bytes.Buffer + parent.SetOut(&stdout) + parent.SetErr(&bytes.Buffer{}) + parent.SetArgs(append([]string{"mcp", "add"}, args...)) + return &stdout, parent.ExecuteContext(t.Context()) +} + +func TestMcpAddJSONReportsMissingFlagsAsDocument(t *testing.T) { + withHome(t) + stdout, err := newAddTestCmd(t, "--names", "github", "--output", "json") + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + + var out addOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &out), stdout.String()) + assert.Equal(t, "INVALID_FLAGS", string(out.ErrorCategory)) + assert.Equal(t, "--output json requires --agents so the command runs without interactive prompts", out.Error) + assert.Empty(t, out.Servers) + assert.Equal(t, "global", out.Scope) +} diff --git a/cmd/aitools/mcp_binary.go b/cmd/aitools/mcp_binary.go new file mode 100644 index 00000000000..4ba6fed9eee --- /dev/null +++ b/cmd/aitools/mcp_binary.go @@ -0,0 +1,26 @@ +package aitools + +import ( + "os" + "os/exec" + "path/filepath" +) + +// mcpBinaryPath returns the command that agents run for MCP servers, and whether it is absolute. +// The invoked path is used when it is absolute, so a symlink such as Homebrew's stays the +// stable path; it is never resolved. +func mcpBinaryPath() (string, bool) { + return binaryPathFor(os.Args[0], exec.LookPath) +} + +// binaryPathFor resolves arg0 without touching the file system beyond lookPath. When neither +// arg0 nor its PATH lookup is absolute, it returns the bare name, which the agent must find on PATH. +func binaryPathFor(arg0 string, lookPath func(string) (string, error)) (string, bool) { + if filepath.IsAbs(arg0) { + return arg0, true + } + if found, err := lookPath(filepath.Base(arg0)); err == nil && filepath.IsAbs(found) { + return found, true + } + return "databricks", false +} diff --git a/cmd/aitools/mcp_binary_test.go b/cmd/aitools/mcp_binary_test.go new file mode 100644 index 00000000000..d0fc20bf1aa --- /dev/null +++ b/cmd/aitools/mcp_binary_test.go @@ -0,0 +1,51 @@ +package aitools + +import ( + "errors" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBinaryPathForAbsoluteArg0KeepsSymlinkPath(t *testing.T) { + // The invoked path is kept as is, so a Homebrew symlink is not replaced by its target. + arg0 := filepath.Join(t.TempDir(), "bin", "databricks") + lookPath := func(string) (string, error) { + t.Fatal("PATH lookup must not run when arg0 is absolute") + return "", nil + } + + path, absolute := binaryPathFor(arg0, lookPath) + assert.True(t, absolute) + assert.Equal(t, arg0, path) +} + +func TestBinaryPathForRelativeArg0UsesPathLookup(t *testing.T) { + found := filepath.Join(t.TempDir(), "databricks") + var looked string + lookPath := func(name string) (string, error) { + looked = name + return found, nil + } + + path, absolute := binaryPathFor("./databricks", lookPath) + assert.True(t, absolute) + assert.Equal(t, found, path) + assert.Equal(t, "databricks", looked) +} + +func TestBinaryPathForFallsBackToBareName(t *testing.T) { + notFound := func(string) (string, error) { return "", errors.New("not found") } + path, absolute := binaryPathFor("databricks", notFound) + assert.False(t, absolute) + assert.Equal(t, "databricks", path) +} + +func TestBinaryPathForRelativeLookupResultFallsBackToBareName(t *testing.T) { + // A relative PATH entry does not give a path an agent can start from another directory. + relative := func(string) (string, error) { return filepath.Join("bin", "databricks"), nil } + path, absolute := binaryPathFor("databricks", relative) + assert.False(t, absolute) + assert.Equal(t, "databricks", path) +} diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go new file mode 100644 index 00000000000..b3bb4779eb8 --- /dev/null +++ b/cmd/aitools/mcp_test.go @@ -0,0 +1,122 @@ +package aitools + +import ( + "errors" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/libs/aitools/mcp/agentconfig" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// writeDatabricksCfg writes a Databricks config file and points DATABRICKS_CONFIG_FILE at it. +// Profile selection variables are cleared so each test sets only what it needs. +func writeDatabricksCfg(t *testing.T, contents string) { + t.Helper() + path := filepath.Join(t.TempDir(), ".databrickscfg") + require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) + t.Setenv("DATABRICKS_CONFIG_FILE", path) + t.Setenv("DATABRICKS_CONFIG_PROFILE", "") +} + +func TestMcpCommandIsHidden(t *testing.T) { + mcp := NewMcpCmd() + assert.True(t, mcp.Hidden) + assert.NotNil(t, mcp.Commands()) + + var names []string + for _, c := range mcp.Commands() { + names = append(names, c.Name()) + } + assert.Equal(t, []string{"add"}, names) + + for _, c := range NewAitoolsCmd().Commands() { + if c.Name() == "mcp" { + assert.True(t, c.Hidden, "aitools mcp must stay hidden") + return + } + } + t.Fatal("aitools has no mcp command") +} + +func TestResolveProfilePrecedence(t *testing.T) { + ctx := t.Context() + const twoProfiles = `[__settings__] +default_profile = dflt + +[dflt] +host = https://dflt.test + +[other] +host = https://other.test +` + + t.Run("flag wins over environment and default", func(t *testing.T) { + writeDatabricksCfg(t, twoProfiles) + t.Setenv("DATABRICKS_CONFIG_PROFILE", "env") + name, err := resolveProfile(ctx, "flag") + require.NoError(t, err) + assert.Equal(t, "flag", name) + }) + + t.Run("environment wins over default profile", func(t *testing.T) { + writeDatabricksCfg(t, twoProfiles) + t.Setenv("DATABRICKS_CONFIG_PROFILE", "other") + name, err := resolveProfile(ctx, "") + require.NoError(t, err) + assert.Equal(t, "other", name) + }) + + t.Run("default profile when nothing else is set", func(t *testing.T) { + writeDatabricksCfg(t, twoProfiles) + name, err := resolveProfile(ctx, "") + require.NoError(t, err) + assert.Equal(t, "dflt", name) + }) + + t.Run("only workspace profile", func(t *testing.T) { + writeDatabricksCfg(t, `[solo] +host = https://solo.test +`) + name, err := resolveProfile(ctx, "") + require.NoError(t, err) + assert.Equal(t, "solo", name) + }) + + t.Run("several profiles and no default is an error listing them", func(t *testing.T) { + writeDatabricksCfg(t, `[a] +host = https://a.test + +[b] +host = https://b.test +`) + _, err := resolveProfile(ctx, "") + require.Error(t, err) + assert.Equal(t, "several workspace profiles exist; pass --profile. Found: a, b", err.Error()) + }) + + t.Run("no workspace profiles is an error naming the file", func(t *testing.T) { + writeDatabricksCfg(t, "") + _, err := resolveProfile(ctx, "") + require.Error(t, err) + assert.Contains(t, err.Error(), "no workspace profiles in ") + assert.Contains(t, err.Error(), "databricks auth login --host --profile ") + }) +} + +func TestErrorCategoryUsesTypedErrorsOnly(t *testing.T) { + typed := failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "unknown agent %q", "pi") + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(typed)) + assert.Equal(t, "unknown agent \"pi\"", typed.Error()) + + wrapped := failure(categoryAuthFailed, errors.New("boom"), "%s", "boom") + assert.Equal(t, categoryAuthFailed, errorCategory(wrapped)) + + configInvalid := &os.PathError{Op: "parse", Path: "x", Err: agentconfig.ErrConfigInvalid} + assert.Equal(t, categoryAgentConfigInvalid, errorCategory(configInvalid)) + + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(errors.New("some failure"))) +} From e40dc2088c1f3a701953498dba1b2de16cb427be Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:27:40 +0000 Subject: [PATCH 02/20] Adapt aitools mcp add to the agentconfig and mcpconfig changes mcpconfig.Spec.IsOurs is gone, so the unmanaged check calls mcpconfig.IsDatabricksEntry directly. serverNamePattern is replaced by mcpconfig.ValidServerName so the --name rule cannot drift from the writer. The add-two-agents Codex golden changes: agentconfig now edits TOML as text, so the server tables are written flush-left without the [mcp_servers] parent header. The entries are unchanged. Co-authored-by: Isaac --- .../cmd/aitools/mcp/add-two-agents/output.txt | 22 +++++++++---------- cmd/aitools/mcp_add.go | 9 +++----- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/output.txt b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt index 2a91e7770e8..804059219ad 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/output.txt +++ b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt @@ -92,17 +92,17 @@ === Codex config >>> cat home/.codex/config.toml -[mcp_servers] - [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" +[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" === State records the binding, without the time and CLI version diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index ca00243b3ff..d63ac81bff1 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "path/filepath" - "regexp" "slices" "strconv" "strings" @@ -17,6 +16,7 @@ import ( "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/aitools/mcp/agentconfig" + "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" @@ -27,9 +27,6 @@ import ( "github.com/spf13/cobra" ) -// serverNamePattern is the strictest server-name rule among the agents. -var serverNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) - // Per-agent results of `mcp add`. const ( statusInstalled = "installed" @@ -261,7 +258,7 @@ func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames [ if len(names) != 1 { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service in --names") } - if !serverNamePattern.MatchString(f.name) { + if !mcpconfig.ValidServerName(f.name) { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %q may only contain letters, digits, '_' and '-'", f.name) } } @@ -488,7 +485,7 @@ func unmanagedNames(ctx context.Context, a *agents.Agent) map[string]bool { } names := make(map[string]bool, len(entries)) for name, entry := range entries { - if !a.MCP.IsOurs(entry) { + if !mcpconfig.IsDatabricksEntry(entry) { names[name] = true } } From 59f229a4909372b32f1e188a44aafe055275b836 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:31:16 +0000 Subject: [PATCH 03/20] Refuse agents named with --agents that are not installed An agent named explicitly with --agents whose config directory does not exist now fails for that agent alone: nothing is written and its directory is not created. The other agents still install, and the command exits 1. Agents picked by detection are installed by construction, so they are unchanged. The failure uses the UNKNOWN_AGENT category, since the SPEC table has no row for an agent that is not installed. The add cases whose agents must exist now create their config directories first, and add-agent-not-installed covers the refusal. Co-authored-by: Isaac --- .../add-agent-not-installed/.databrickscfg | 7 +++ .../mcp/add-agent-not-installed/out.test.toml | 1 + .../mcp/add-agent-not-installed/output.txt | 51 +++++++++++++++++++ .../mcp/add-agent-not-installed/script | 14 +++++ acceptance/cmd/aitools/mcp/add-text/script | 1 + .../cmd/aitools/mcp/add-two-agents/script | 1 + .../cmd/aitools/mcp/add-two-profiles/script | 1 + .../aitools/mcp/add-unparsable-config/script | 1 + cmd/aitools/mcp_add.go | 20 +++++++- cmd/aitools/mcp_add_test.go | 27 ++++++++++ 10 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/add-agent-not-installed/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/add-agent-not-installed/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt create mode 100644 acceptance/cmd/aitools/mcp/add-agent-not-installed/script diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/.databrickscfg b/acceptance/cmd/aitools/mcp/add-agent-not-installed/.databrickscfg new file mode 100644 index 00000000000..f685e4656ff --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/out.test.toml b/acceptance/cmd/aitools/mcp/add-agent-not-installed/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt b/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt new file mode 100644 index 00000000000..4f94cfe3360 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt @@ -0,0 +1,51 @@ + +=== An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits 1 + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,codex --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": "failed", + "error_category": "UNKNOWN_AGENT", + "message": "Codex CLI is not installed (no [TEST_TMP_DIR]/home/.codex); install it first or leave it out of --agents" + } + ] + } + ], + "error_category": "TYPE_UNSPECIFIED" +} + +Exit code: 1 + +=== Nothing was created for Codex + +>>> ls -A home +.claude +.claude.json +.databricks + +=== With no installed agent named, the text output says what to do, and the command exits 1 + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): codex + +genie_one_mcp-logfood system.ai.genie_one_mcp + codex failed: Codex CLI is not installed (no [TEST_TMP_DIR]/home/.codex); install it first or leave it out of --agents +Error: Codex CLI is not installed (no [TEST_TMP_DIR]/home/.codex); install it first or leave it out of --agents + +Exit code: 1 diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/script b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script new file mode 100644 index 00000000000..96cc5866926 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script @@ -0,0 +1,14 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +mkdir -p home/.claude +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +title "An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits 1" +errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,codex --output json + +title "Nothing was created for Codex" +trace ls -A home + +title "With no installed agent named, the text output says what to do, and the command exits 1" +errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex diff --git a/acceptance/cmd/aitools/mcp/add-text/script b/acceptance/cmd/aitools/mcp/add-text/script index 06b05f7d9fd..8c2195203de 100644 --- a/acceptance/cmd/aitools/mcp/add-text/script +++ b/acceptance/cmd/aitools/mcp/add-text/script @@ -1,5 +1,6 @@ # Isolate HOME so the agent config files and the state file land in this test directory. sethome home +mkdir -p home/.claude home/.cursor envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/script b/acceptance/cmd/aitools/mcp/add-two-agents/script index 8f507b8cbb1..194f5115514 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/script +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -1,5 +1,6 @@ # Isolate HOME so the agent config files and the state file land in this test directory. sethome home +mkdir -p home/.claude home/.codex envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/script b/acceptance/cmd/aitools/mcp/add-two-profiles/script index 971597474b5..3f7cfe45f22 100644 --- a/acceptance/cmd/aitools/mcp/add-two-profiles/script +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/script @@ -1,5 +1,6 @@ # Isolate HOME so the agent config files and the state file land in this test directory. sethome home +mkdir -p home/.claude envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/script b/acceptance/cmd/aitools/mcp/add-unparsable-config/script index 69de153f7d4..c49b9bd5371 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/script +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/script @@ -1,5 +1,6 @@ # Isolate HOME so the agent config files and the state file land in this test directory. sethome home +mkdir -p home/.claude envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index d63ac81bff1..dd4cbde03b3 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -521,8 +521,14 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO Agents: make([]addAgentOutput, 0, len(req.agents)), } for _, a := range req.agents { - args := []string{"aitools", "mcp", "serve", "--profile", req.profile, "--service", plan.service.FullName, "--agent", a.Name} - path, _, err := agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) + var path string + var err error + if req.explicit && !a.Detected(ctx) { + err = notInstalledError(ctx, a) + } else { + args := []string{"aitools", "mcp", "serve", "--profile", req.profile, "--service", plan.service.FullName, "--agent", a.Name} + path, _, err = agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) + } result := addAgentOutput{Name: a.Name} switch { @@ -543,6 +549,16 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO return out } +// notInstalledError is the failure of an agent named with --agents that is not installed. Writing its +// config would create its config directory, so the agent is refused instead. +func notInstalledError(ctx context.Context, a *agents.Agent) error { + dir, err := a.ConfigDir(ctx) + if err != nil { + return err + } + return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%s is not installed (no %s); install it first or leave it out of --agents", a.DisplayName, dir) +} + // installedFor reports whether state already records the server for the agent. func (p addPlan) installedFor(agent string) bool { if p.entry == nil { diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index def42bb799c..b766e2d7bc8 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -153,6 +153,33 @@ func withHome(t *testing.T) string { return home } +// An agent named with --agents that is not installed is refused: its config is not written and its +// directory is not created. The agent that is installed still gets the server. +func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { + ctx := t.Context() + home := withHome(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".claude"), 0o700)) + req := addRequest{ + profile: "logfood", + explicit: true, + agents: []*agents.Agent{agents.ByName("codex"), agents.ByName("claude-code")}, + binary: "databricks", + } + plan := addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"} + + out := installServer(ctx, req, plan) + require.Len(t, out.Agents, 2) + + codex := out.Agents[0] + assert.Equal(t, statusFailed, codex.Status) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, codex.ErrorCategory) + assert.Equal(t, "Codex CLI is not installed (no "+filepath.Join(home, ".codex")+"); install it first or leave it out of --agents", codex.Message) + assert.NoDirExists(t, filepath.Join(home, ".codex")) + + assert.Equal(t, statusInstalled, out.Agents[1].Status) + assert.NoError(t, out.Agents[1].err) +} + func TestPlanAddsNaming(t *testing.T) { ctx := t.Context() claude := agents.ByName("claude-code") From 35db3ab1606e9a7a89b86a6be00ced3b4932e547 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:03:05 +0000 Subject: [PATCH 04/20] Read the MCP state from the shared skills state file in acceptance Card 03 moved MCP records into the `mcp` section of ~/.databricks/aitools/skills/.state.json. The add acceptance scripts read the old mcp/state.json path and its top-level `servers` array. Point them at the new file and filter the `mcp` section. The golden loses the `schema_version: 1` line, which is no longer part of the MCP section. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/add-two-agents/output.txt | 3 +-- acceptance/cmd/aitools/mcp/add-two-agents/script | 2 +- acceptance/cmd/aitools/mcp/add-two-profiles/output.txt | 2 +- acceptance/cmd/aitools/mcp/add-two-profiles/script | 2 +- acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt | 2 +- acceptance/cmd/aitools/mcp/add-unparsable-config/script | 2 +- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/output.txt b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt index 804059219ad..333d70f3b03 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/output.txt +++ b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt @@ -106,9 +106,8 @@ DATABRICKS_CONFIG_FILE = "[TEST_TMP_DIR]/.databrickscfg" === State records the binding, without the time and CLI version ->>> jq del(.servers[].installed_at, .servers[].cli_version) home/.databricks/aitools/mcp/state.json +>>> jq .mcp | del(.servers[].installed_at, .servers[].cli_version) home/.databricks/aitools/skills/.state.json { - "schema_version": 1, "servers": [ { "name": "genie_one_mcp-logfood", diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/script b/acceptance/cmd/aitools/mcp/add-two-agents/script index 194f5115514..3cb691e4fcd 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/script +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -14,4 +14,4 @@ title "Codex config" trace cat home/.codex/config.toml title "State records the binding, without the time and CLI version" -trace jq 'del(.servers[].installed_at, .servers[].cli_version)' home/.databricks/aitools/mcp/state.json +trace jq '.mcp | del(.servers[].installed_at, .servers[].cli_version)' home/.databricks/aitools/skills/.state.json diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/output.txt b/acceptance/cmd/aitools/mcp/add-two-profiles/output.txt index 0826da7ba77..08a250cd75e 100644 --- a/acceptance/cmd/aitools/mcp/add-two-profiles/output.txt +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/output.txt @@ -34,7 +34,7 @@ genie_one_mcp-logfood system.ai.genie_one_mcp === State holds one entry per profile ->>> jq [.servers[] | {name, profile, agents: (.agents | keys)}] home/.databricks/aitools/mcp/state.json +>>> jq .mcp | [.servers[] | {name, profile, agents: (.agents | keys)}] home/.databricks/aitools/skills/.state.json [ { "name": "genie_one_mcp-logfood", diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/script b/acceptance/cmd/aitools/mcp/add-two-profiles/script index 3f7cfe45f22..860266a85c5 100644 --- a/acceptance/cmd/aitools/mcp/add-two-profiles/script +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/script @@ -12,4 +12,4 @@ title "Both entries are in the Claude Code config" trace jq '.mcpServers | keys' home/.claude.json title "State holds one entry per profile" -trace jq '[.servers[] | {name, profile, agents: (.agents | keys)}]' home/.databricks/aitools/mcp/state.json +trace jq '.mcp | [.servers[] | {name, profile, agents: (.agents | keys)}]' home/.databricks/aitools/skills/.state.json diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt index 8b319aeaf91..f99d1fde360 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt @@ -39,7 +39,7 @@ command = "broken" === State records only the agent that succeeded ->>> jq [.servers[] | {name, agents: (.agents | keys)}] home/.databricks/aitools/mcp/state.json +>>> jq .mcp | [.servers[] | {name, agents: (.agents | keys)}] home/.databricks/aitools/skills/.state.json [ { "name": "genie_one_mcp-logfood", diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/script b/acceptance/cmd/aitools/mcp/add-unparsable-config/script index c49b9bd5371..4b97eb147ed 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/script +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/script @@ -11,4 +11,4 @@ title "The broken file is left as it was" trace cat home/.codex/config.toml title "State records only the agent that succeeded" -trace jq '[.servers[] | {name, agents: (.agents | keys)}]' home/.databricks/aitools/mcp/state.json +trace jq '.mcp | [.servers[] | {name, agents: (.agents | keys)}]' home/.databricks/aitools/skills/.state.json From 8548e1004fd9325fbf74b640619dfa06d293f368 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:38:56 +0000 Subject: [PATCH 05/20] Export the binding helpers for aitools mcp add ResolveConfigFile and AuthError are exported, and CheckBinding runs the binding drift check without a request. Bind's profile and drift checks move into shared helpers with the same behavior. Co-authored-by: Isaac --- libs/aitools/mcp/binding.go | 61 +++++++++++++++++++++++------ libs/aitools/mcp/binding_test.go | 39 ++++++++++++++++++ libs/aitools/mcp/configfile.go | 4 +- libs/aitools/mcp/configfile_test.go | 8 ++-- libs/aitools/mcp/state.go | 2 +- 5 files changed, 94 insertions(+), 20 deletions(-) diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 17320c755e3..ca71ae6264e 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -10,6 +10,7 @@ import ( "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/databrickscfg" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/shellquote" "github.com/databricks/databricks-sdk-go/config" @@ -76,7 +77,7 @@ func (e *BindError) ExitCode() int { // token store reads DATABRICKS_CONFIG_FILE from the process environment instead, so a caller // that sets the variable in the context must also set it in the process environment. func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) { - configFile, err := resolveConfigFile(ctx, env.Get(ctx, mcpconfig.ConfigFileEnv)) + configFile, err := ResolveConfigFile(ctx, env.Get(ctx, mcpconfig.ConfigFileEnv)) if err != nil { return nil, nil, uncategorizedError(err) } @@ -90,14 +91,9 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) name = entry.Name } - p, err := configFiles{}.profile(configFile, opts.Profile) - switch { - case errors.Is(err, errProfileMissing): - return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(configFile), entry, "no longer exists") - case errors.Is(err, errProfileNoHost): - return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(configFile), entry, "has no host") - case err != nil: - return nil, nil, uncategorizedError(err) + p, err := profileFor(name, opts.Profile, configFile, entry) + if err != nil { + return nil, nil, err } if entry != nil { if drift := driftOf(*entry, p); drift != nil { @@ -129,11 +125,50 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) return nil, nil, uncategorizedError(err) } if err := cfg.Authenticate(req); err != nil { - return nil, nil, authError(opts.Profile, err) + return nil, nil, AuthError(opts.Profile, err) } return cfg, entry, nil } +// CheckBinding returns the error that Bind returns for the state entry of service and profileName +// when the profile no longer matches it: the entry is pinned to another config file, the profile is +// gone or has no host, or its host or workspace changed. It returns nil when there is no entry or the +// entry still matches. It reads the config files only and makes no request, so a caller can refuse +// before it sends a token to the profile's host. +func CheckBinding(ctx context.Context, state *State, service, profileName string) error { + configFile, err := ResolveConfigFile(ctx, env.Get(ctx, mcpconfig.ConfigFileEnv)) + if err != nil { + return uncategorizedError(err) + } + entry, err := findEntry(ctx, state, service, profileName, configFile) + if err != nil || entry == nil { + return err + } + p, err := profileFor(entry.Name, profileName, configFile, entry) + if err != nil { + return err + } + if drift := driftOf(*entry, p); drift != nil { + return driftError(entry.Name, profileName, *entry, *drift) + } + return nil +} + +// profileFor returns the profile profileName in configFile, or the PROFILE_NOT_FOUND error that says it +// is gone. name and entry describe the state entry the profile serves; entry is nil without one. +func profileFor(name, profileName, configFile string, entry *Entry) (profile.Profile, error) { + p, err := configFiles{}.profile(configFile, profileName) + switch { + case errors.Is(err, errProfileMissing): + return profile.Profile{}, profileGoneError(name, profileName, filepath.ToSlash(configFile), entry, "no longer exists") + case errors.Is(err, errProfileNoHost): + return profile.Profile{}, profileGoneError(name, profileName, filepath.ToSlash(configFile), entry, "has no host") + case err != nil: + return profile.Profile{}, uncategorizedError(err) + } + return p, nil +} + // uncategorizedError wraps a failure that has no category of its own. Its message is the cause's. func uncategorizedError(err error) error { return &BindError{Category: BindUncategorized, message: err.Error(), cause: err} @@ -184,9 +219,9 @@ func configFileError(name, profileName, recordedPath, currentPath string) error return &BindError{Category: BindProfileHostChanged, message: message} } -// authError is the AUTH_FAILED error for a credential that the profile's own settings could not +// AuthError is the AUTH_FAILED error for a credential that the profile's own settings could not // provide. The message points at the login command for the profile. -func authError(profileName string, cause error) error { +func AuthError(profileName string, cause error) error { message := fmt.Sprintf("%s\nRun: %s --profile %s", cause, authLoginCommand, shellquote.BashArg(profileName)) return &BindError{Category: BindAuthFailed, message: message, cause: cause} } @@ -208,7 +243,7 @@ func findEntry(ctx context.Context, state *State, service, profileName, configFi if entry.Service != service || entry.Profile != profileName { continue } - path, err := resolveConfigFile(ctx, entry.ConfigFile) + path, err := ResolveConfigFile(ctx, entry.ConfigFile) if err != nil { unresolved = errors.Join(unresolved, fmt.Errorf("MCP server %s: %w", entry.Name, err)) continue diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 7e0220c0165..e76fcb22f7f 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -780,3 +780,42 @@ func TestBindRecreateHintQuotesValues(t *testing.T) { filepath.ToSlash(bindingTestResolved(t, cfgPath)), filepath.ToSlash(bindingTestResolved(t, cfgPath))) assert.Equal(t, want, err.Error()) } + +// TestCheckBindingMatchesBind checks that CheckBinding refuses the same drift as Bind, before any request. +func TestCheckBindingMatchesBind(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request to the profile's host: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(server.Close) + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, server.URL, bindingTestToken)) + state := bindingTestState(cfgPath, bindingTestHost) + + err := mcp.CheckBinding(t.Context(), state, bindingTestService, bindingTestProfile) + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Equal(t, "MCP server github-logfood was set up for https://adb-123.test, but profile \"logfood\" now points at "+server.URL+". Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) +} + +func TestCheckBindingPassesWhenEntryMatchesOrIsAbsent(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + require.NoError(t, mcp.CheckBinding(t.Context(), bindingTestState(cfgPath, bindingTestHost), bindingTestService, bindingTestProfile)) + require.NoError(t, mcp.CheckBinding(t.Context(), &mcp.State{}, bindingTestService, bindingTestProfile)) +} + +func TestCheckBindingRefusesWorkspaceChangeOnSameHost(t *testing.T) { + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nworkspace_id = 222\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, bindingTestToken)) + state := bindingTestState(cfgPath, bindingTestHost) + state.Servers[0].WorkspaceID = "111" + + err := mcp.CheckBinding(t.Context(), state, bindingTestService, bindingTestProfile) + requireBindError(t, err, mcp.BindProfileHostChanged) +} + +func TestCheckBindingRefusesEntryPinnedToOtherConfigFile(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + other := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) + require.NotEqual(t, cfgPath, other) + + err := mcp.CheckBinding(t.Context(), bindingTestState(other, bindingTestHost), bindingTestService, bindingTestProfile) + requireBindError(t, err, mcp.BindProfileHostChanged) +} diff --git a/libs/aitools/mcp/configfile.go b/libs/aitools/mcp/configfile.go index 7244fd9bab2..499d722878f 100644 --- a/libs/aitools/mcp/configfile.go +++ b/libs/aitools/mcp/configfile.go @@ -11,12 +11,12 @@ import ( "github.com/databricks/cli/libs/databrickscfg" ) -// resolveConfigFile returns the absolute path of the config file that name refers to. An empty +// ResolveConfigFile returns the absolute path of the config file that name refers to. An empty // name is the default file, and a leading "~" is the home directory. Symlinks are resolved while // the path is still as the user wrote it: the OS reads "link/../x" through the link's target, so // the same file has one path however it is named. A path that does not exist is kept as written, // since there is no file to resolve and cleaning it could name another file. -func resolveConfigFile(ctx context.Context, name string) (string, error) { +func ResolveConfigFile(ctx context.Context, name string) (string, error) { path, err := databrickscfg.ResolveConfigFilePath(ctx, name) if err != nil { return "", err diff --git a/libs/aitools/mcp/configfile_test.go b/libs/aitools/mcp/configfile_test.go index b1f16412ba3..c47abcffdda 100644 --- a/libs/aitools/mcp/configfile_test.go +++ b/libs/aitools/mcp/configfile_test.go @@ -26,7 +26,7 @@ func TestResolveConfigFileResolvesSymlinkBeforeDotDot(t *testing.T) { // filepath.Join would clean the ".." away, so the path is joined by hand. sep := string(filepath.Separator) - got, err := resolveConfigFile(t.Context(), link+sep+".."+sep+".databrickscfg") + got, err := ResolveConfigFile(t.Context(), link+sep+".."+sep+".databrickscfg") require.NoError(t, err) assert.Equal(t, want, got) } @@ -37,11 +37,11 @@ func TestResolveConfigFileWithoutHomeAndAbsoluteName(t *testing.T) { ctx := env.WithUserHomeDir(t.Context(), "") name := filepath.Join(t.TempDir(), ".databrickscfg") - got, err := resolveConfigFile(ctx, name) + got, err := ResolveConfigFile(ctx, name) require.NoError(t, err) assert.True(t, filepath.IsAbs(got)) - _, err = resolveConfigFile(ctx, "~/.databrickscfg") + _, err = ResolveConfigFile(ctx, "~/.databrickscfg") require.ErrorIs(t, err, env.ErrNoHomeEnv) } @@ -54,7 +54,7 @@ func TestResolveConfigFileKeepsPathThroughMissingDir(t *testing.T) { sep := string(filepath.Separator) name := dir + sep + "missing" + sep + ".." + sep + "cfg" - got, err := resolveConfigFile(t.Context(), name) + got, err := ResolveConfigFile(t.Context(), name) require.NoError(t, err) assert.Equal(t, name, got) } diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go index ceeaf7ab69c..9516442eac5 100644 --- a/libs/aitools/mcp/state.go +++ b/libs/aitools/mcp/state.go @@ -106,7 +106,7 @@ func Reconcile(ctx context.Context, state *State) ([]Drift, error) { // checkEntry returns the drift of one entry, or nil when its profile still names the recorded host // and workspace. The entry's profile is read from the config file the entry is pinned to. func checkEntry(ctx context.Context, files configFiles, entry Entry) (*Drift, error) { - path, err := resolveConfigFile(ctx, entry.ConfigFile) + path, err := ResolveConfigFile(ctx, entry.ConfigFile) if err != nil { return nil, err } From d37af4344974f3461d6405926f0428bfc5254b7d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:47:55 +0000 Subject: [PATCH 06/20] Fix review findings in aitools mcp add Refuse a recorded entry whose profile changed (host, workspace or config file) before any request reaches the profile's host, and load state first. Read the config file with the same resolver as Bind, and create the client with Bind's loaders. Check --agents, the binary name and the --names with the other flags, before the profile is read. Installed agents are the ones whose MCP config file or its directory exists, which covers CODEX_HOME, COPILOT_HOME and Antigravity's ~/.gemini/config. A status comes from the agent write, so a re-add reports re-asserted without state. The command fails when no agent was installed. The state is recorded after the results are printed. Printed paths use forward slashes. Errors use the binding and install categories, and a name clash reports MCP_NAME_CONFLICT. --name follows mcp.Sanitize. A relative arg0 with a directory is made absolute. Co-authored-by: Isaac --- cmd/aitools/mcp.go | 31 +-- cmd/aitools/mcp_add.go | 299 ++++++++++++++++---------- cmd/aitools/mcp_add_test.go | 376 +++++++++++++++++++++++++++++---- cmd/aitools/mcp_binary.go | 15 +- cmd/aitools/mcp_binary_test.go | 117 +++++++--- cmd/aitools/mcp_test.go | 4 +- 6 files changed, 640 insertions(+), 202 deletions(-) diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index aff40464caa..3a186f14d98 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -4,8 +4,10 @@ import ( "context" "errors" "fmt" + "path/filepath" "strings" + "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/aitools/mcp/agentconfig" "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" @@ -17,13 +19,11 @@ import ( // defaultMCPLocation is the schema that ships the Databricks managed MCP services. const defaultMCPLocation = "system.ai" -// Error categories that the telemetry enum does not define yet. -// The JSON output reports them by name. +// Error categories that the telemetry enum does not define yet. The JSON output reports them by name. +// The binding categories come from mcp.BindCategory, which has the same names. const ( categoryAgentConfigInvalid protos.AitoolsErrorCategory = "AGENT_CONFIG_INVALID" - categoryAuthFailed protos.AitoolsErrorCategory = "AUTH_FAILED" - categoryProfileNotFound protos.AitoolsErrorCategory = "PROFILE_NOT_FOUND" - categoryProfileHostChanged protos.AitoolsErrorCategory = "PROFILE_HOST_CHANGED" + categoryNameConflict protos.AitoolsErrorCategory = "MCP_NAME_CONFLICT" categoryServiceNotFound protos.AitoolsErrorCategory = "MCP_SERVICE_NOT_FOUND" ) @@ -56,16 +56,22 @@ func failure(category protos.AitoolsErrorCategory, cause error, format string, a return &cliError{category: category, message: fmt.Sprintf(format, args...), cause: cause} } -// errorCategory classifies an error by its typed errors only, never by its text. -// Errors without a typed category are UNCATEGORIZED. +// errorCategory classifies an error by its typed errors only, never by its text. Failures without a +// category of their own fall through to the install classification, as in the other aitools commands. func errorCategory(err error) protos.AitoolsErrorCategory { if ce, ok := errors.AsType[*cliError](err); ok { return ce.category } - if errors.Is(err, agentconfig.ErrConfigInvalid) { + if be, ok := errors.AsType[*mcp.BindError](err); ok { + return protos.AitoolsErrorCategory(be.Category) + } + switch { + case errors.Is(err, agentconfig.ErrConfigInvalid): return categoryAgentConfigInvalid + case errors.Is(err, agentconfig.ErrNotOwned): + return categoryNameConflict } - return protos.AitoolsErrorCategoryUncategorized + return classifyInstallError(err) } // resolveProfile picks the profile a non-interactive command binds to: the @@ -93,7 +99,7 @@ func resolveProfile(ctx context.Context, flagValue string) (string, error) { } switch len(profiles) { case 0: - return "", fmt.Errorf("no workspace profiles in %s. Run: databricks auth login --host --profile ", path) + return "", fmt.Errorf("no workspace profiles in %s. Run: databricks auth login --host --profile ", filepath.ToSlash(path)) case 1: return profiles[0].Name, nil } @@ -102,8 +108,5 @@ func resolveProfile(ctx context.Context, flagValue string) (string, error) { // profileFlag returns the root --profile value, or "" when it is not set. func profileFlag(cmd *cobra.Command) string { - if f := cmd.Flag("profile"); f != nil { - return f.Value.String() - } - return "" + return cmd.Flag("profile").Value.String() } diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index dd4cbde03b3..f70068d6e99 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -4,10 +4,11 @@ import ( "context" "errors" "fmt" + "io/fs" "net/http" + "os" "path/filepath" "slices" - "strconv" "strings" "time" @@ -17,17 +18,20 @@ import ( "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/aitools/mcp/agentconfig" "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" ) -// Per-agent results of `mcp add`. +// Per-agent results of `mcp add`. installed means the agent's entry was added or changed; reasserted +// means the file already held the same entry. const ( statusInstalled = "installed" statusReasserted = "reasserted" @@ -93,7 +97,8 @@ type addRequest struct { explicit bool force bool binary string - configFile string + // configFile is the config file pinned into the entries, "" when DATABRICKS_CONFIG_FILE is unset. + configFile string // env is the entry env: DATABRICKS_CONFIG_FILE when it was set. env map[string]string } @@ -140,7 +145,7 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), out.ErrorCategory = errorCategory(top) } if err := renderJSON(cmd.OutOrStdout(), out); err != nil { - return err + return errors.Join(runErr, err) } if runErr != nil { return root.ErrAlreadyPrinted @@ -157,8 +162,10 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), return cmd } -// runMcpAdd installs the requested servers and fills out. A failure of an agent named with -// --agents is returned as an agentErrors; detected agents only report theirs. +// runMcpAdd installs the requested servers and fills out. Every refusal that does not need the +// workspace comes before the first request: the flags, the agents, the binary name, and the binding of +// each service that a recorded entry already holds. A failure of an agent named with --agents is +// returned as an agentErrors; detected agents only report theirs. func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode bool, out *addOutput) error { names := splitAndTrim(f.names) agentNames := splitAndTrim(f.agents) @@ -172,6 +179,18 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if err := validateLocation(location); err != nil { return err } + fullNames, err := serviceNames(location, names) + if err != nil { + return err + } + targets, explicit, err := targetAgents(ctx, agentNames) + if err != nil { + return err + } + binary, absolute := mcpBinaryPath() + if !mcpconfig.IsDatabricksBinary(binary) { + return failure(protos.AitoolsErrorCategoryUncategorized, nil, "%s is not the databricks binary; coding agents start MCP servers with databricks or databricks.exe", binary) + } profileName, err := resolveProfile(ctx, profileFlag(cmd)) if err != nil { @@ -181,44 +200,44 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if err := requireProfile(ctx, profileName); err != nil { return err } - w, err := profileClient(profileName) + + state, err := mcp.LoadState(ctx) if err != nil { return err } - out.Host = w.Config.Host - workspaceID, err := workspaceIDOf(ctx, w) - if err != nil { - return err + for _, name := range fullNames { + if err := mcp.CheckBinding(ctx, state, name, profileName); err != nil { + return err + } } - targets, explicit, err := targetAgents(ctx, agentNames) + configFile, pinned, err := addConfigFile(ctx) if err != nil { return err } - - services, err := mcp.Discover(ctx, w, location) + w, err := profileClient(ctx, profileName, configFile) if err != nil { - return describeListError(profileName, err) + return err } - picked, err := pickServices(location, names, services) + out.Host = w.Config.Host + workspaceID, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return err + return fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err) } - state, err := mcp.LoadState(ctx) + services, err := mcp.Discover(ctx, w, location) if err != nil { - return err + return describeListError(profileName, err) } - plans, err := planAdds(ctx, f.name, location, profileName, w.Config.Host, state, picked, targets) + picked, err := pickServices(location, fullNames, services) if err != nil { return err } - - configFile, err := pinnedConfigFile(ctx) + plans, err := planAdds(ctx, f.name, profileName, state, picked, targets) if err != nil { return err } - binary, absolute := mcpBinaryPath() + req := addRequest{ jsonMode: jsonMode, profile: profileName, @@ -229,10 +248,10 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode explicit: explicit, force: f.force, binary: binary, - configFile: configFile, + configFile: pinned, } - if configFile != "" { - req.env = map[string]string{"DATABRICKS_CONFIG_FILE": configFile} + if pinned != "" { + req.env = map[string]string{mcpconfig.ConfigFileEnv: pinned} } if !absolute && !jsonMode { cmdio.LogString(ctx, "Note: databricks is not an absolute path; coding agents must find it on PATH to start the MCP servers.") @@ -245,10 +264,12 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode } out.Servers = servers - if err := recordInstalls(ctx, req, plans, servers); err != nil { - return err - } + // The results are printed even when the state cannot be saved: the agent files were written. + recordErr := recordInstalls(ctx, req, plans, servers) printAddResults(ctx, req, servers) + if recordErr != nil { + return recordErr + } return agentFailureError(req, servers) } @@ -258,8 +279,10 @@ func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames [ if len(names) != 1 { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service in --names") } - if !mcpconfig.ValidServerName(f.name) { - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %q may only contain letters, digits, '_' and '-'", f.name) + // A name must come out of mcp.Sanitize unchanged apart from case, so that mcp____ + // splits the same way as a generated name. + if mcp.Sanitize(f.name) != strings.ToLower(f.name) { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %q is not a valid server name: use letters, digits, '-' and '_', with no \"__\" and no leading or trailing '-' or '_'", f.name) } } @@ -310,34 +333,43 @@ func requireProfile(ctx context.Context, name string) error { if err != nil { return err } - return failure(categoryProfileNotFound, nil, "Profile %s not found in %s", name, path) + return failure(protos.AitoolsErrorCategory(mcp.BindProfileNotFound), nil, "profile %s not found in %s", name, filepath.ToSlash(path)) } -// profileClient returns a workspace client bound to exactly the named profile. It never prompts -// and never falls back to another profile or to environment credentials. -func profileClient(name string) (*databricks.WorkspaceClient, error) { - cfg := &config.Config{Profile: name, Loaders: databrickscfg.ProfileAuthLoaders} - w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) - if err == nil { - err = w.Config.Authenticate(&http.Request{Header: make(http.Header)}) +// addConfigFile returns the config file that the server binds to, resolved as Bind resolves it, and the +// file to pin into the agent entries: the same file when DATABRICKS_CONFIG_FILE is set, "" otherwise. +func addConfigFile(ctx context.Context) (path, pinned string, err error) { + value := env.Get(ctx, mcpconfig.ConfigFileEnv) + path, err = mcp.ResolveConfigFile(ctx, value) + if err != nil || value == "" { + return path, "", err } - if err != nil { - return nil, failure(categoryAuthFailed, err, "%s\nRun: databricks auth login --profile %s", err, name) - } - return w, nil + return path, path, nil } -// workspaceIDOf returns the workspace ID recorded for the profile: its workspace_id when set, -// otherwise the ID that the host reports. -func workspaceIDOf(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { - if id := w.Config.WorkspaceID; id != "" && id != profile.WorkspaceIDNone { - return id, nil +// profileClient returns a workspace client bound to exactly the named profile in configFile. Its loaders +// are the ones Bind uses: the profile and the non-auth environment. The environment's credentials are +// never sent to the profile's host. +func profileClient(ctx context.Context, name, configFile string) (*databricks.WorkspaceClient, error) { + cfg := &config.Config{ + Profile: name, + ConfigFile: configFile, + Loaders: []config.Loader{databrickscfg.ResolveNonAuthFromEnv, config.ConfigFile}, } - id, err := w.CurrentWorkspaceID(ctx) + w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) + if err != nil { + // Configuration that the SDK rejects is not a credential failure, so a login would not fix it. + return nil, err + } + // Authenticate only writes headers onto req, which is never sent. + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil) if err != nil { - return "", fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err) + return nil, err } - return strconv.FormatInt(id, 10), nil + if err := w.Config.Authenticate(req); err != nil { + return nil, mcp.AuthError(name, err) + } + return w, nil } // describeListError names the profile that made a failed listing call. The @@ -364,7 +396,9 @@ func targetAgents(ctx context.Context, names []string) ([]*agents.Agent, bool, e } var detected []*agents.Agent for _, a := range agents.MCPAgents() { - if a.Detected(ctx) { + installed, err := mcpInstalled(ctx, a) + // An error is kept, so the agent reports it when its config is written. + if installed || err != nil { detected = append(detected, a) } } @@ -401,22 +435,39 @@ func mcpAgentNames() []string { return names } -// pickServices maps --names to the discovered services of location. A full name must be inside -// location; a leaf name is looked up in it. +// serviceNames returns the full name of each service in --names. A leaf name is resolved in location. A +// full name must be in location, so its catalog and schema must match it. Any other dotted name is refused, +// since it cannot be either. +func serviceNames(location string, names []string) ([]string, error) { + full := make([]string, 0, len(names)) + for _, name := range names { + switch parts := strings.Split(name, "."); len(parts) { + case 1: + full = append(full, location+"."+name) + case 3: + if !strings.EqualFold(parts[0]+"."+parts[1], location) { + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not in %s; pass --location to match its schema", name, location) + } + full = append(full, name) + default: + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not a service name: use a leaf such as github or a full name such as system.ai.github", name) + } + } + return full, nil +} + +// pickServices maps the full names of --names to the discovered services of location. Unity Catalog names +// are case-insensitive, so the comparison is too. func pickServices(location string, names []string, services []mcp.Service) ([]mcp.Service, error) { var picked []mcp.Service for _, name := range names { - leaf, inLocation := leafName(name, location) - if !inLocation { - return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not in %s; pass --location to match its schema", name, location) - } - i := slices.IndexFunc(services, func(s mcp.Service) bool { return strings.EqualFold(s.Leaf, leaf) }) + i := slices.IndexFunc(services, func(s mcp.Service) bool { return strings.EqualFold(s.FullName, name) }) if i < 0 { available := make([]string, 0, len(services)) for _, s := range services { available = append(available, s.FullName) } - return nil, failure(categoryServiceNotFound, nil, "%s.%s is not an MCP service in %s. Available: %s", location, leaf, location, strings.Join(available, ", ")) + return nil, failure(categoryServiceNotFound, nil, "%s is not an MCP service in %s. Available: %s", name, location, strings.Join(available, ", ")) } if !slices.ContainsFunc(picked, func(s mcp.Service) bool { return s.FullName == services[i].FullName }) { picked = append(picked, services[i]) @@ -425,20 +476,11 @@ func pickServices(location string, names []string, services []mcp.Service) ([]mc return picked, nil } -// leafName returns the leaf of a name given as a leaf or as a full name, and whether a full -// name is inside location. Unity Catalog names are case-insensitive, so the comparison is too. -func leafName(name, location string) (string, bool) { - parts := strings.Split(name, ".") - if len(parts) != 3 { - return name, true - } - return parts[2], strings.EqualFold(parts[0]+"."+parts[1], location) -} - // planAdds chooses the server name of each picked service before anything is written. A service // already installed for this profile keeps its name. A new name avoids names of other state -// entries and of unmanaged entries in the target agents. -func planAdds(ctx context.Context, nameFlag, location, profileName, host string, state *mcp.State, picked []mcp.Service, targets []*agents.Agent) ([]addPlan, error) { +// entries and of unmanaged entries in the target agents. The entries have been checked against the +// profile by mcp.CheckBinding. +func planAdds(ctx context.Context, nameFlag, profileName string, state *mcp.State, picked []mcp.Service, targets []*agents.Agent) ([]addPlan, error) { taken := map[string]bool{} for i := range state.Servers { taken[state.Servers[i].Name] = true @@ -452,10 +494,6 @@ func planAdds(ctx context.Context, nameFlag, location, profileName, host string, plans := make([]addPlan, 0, len(picked)) for _, svc := range picked { entry := stateEntry(state, svc.FullName, profileName) - if entry != nil && mcp.NormalizeHost(entry.Host) != mcp.NormalizeHost(host) { - return nil, failure(categoryProfileHostChanged, nil, "MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: databricks aitools mcp remove --profile %s and add it again.", entry.Name, entry.Host, profileName, host, profileName) - } - var name string switch { case entry != nil && nameFlag != "" && nameFlag != entry.Name: @@ -502,15 +540,6 @@ func stateEntry(state *mcp.State, service, profileName string) *mcp.Entry { return &state.Servers[i] } -// pinnedConfigFile returns the absolute DATABRICKS_CONFIG_FILE, or "" when it is unset. -func pinnedConfigFile(ctx context.Context) (string, error) { - value := env.Get(ctx, "DATABRICKS_CONFIG_FILE") - if value == "" { - return "", nil - } - return filepath.Abs(value) -} - // installServer writes one server into every target agent. Each agent's result is reported; one // agent's failure does not stop the others. func installServer(ctx context.Context, req addRequest, plan addPlan) addServerOutput { @@ -522,12 +551,14 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO } for _, a := range req.agents { var path string + var status agentconfig.Status var err error - if req.explicit && !a.Detected(ctx) { - err = notInstalledError(ctx, a) - } else { + if req.explicit { + err = requireInstalled(ctx, a) + } + if err == nil { args := []string{"aitools", "mcp", "serve", "--profile", req.profile, "--service", plan.service.FullName, "--agent", a.Name} - path, _, err = agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) + path, status, err = agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) } result := addAgentOutput{Name: a.Name} @@ -537,35 +568,80 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO result.ErrorCategory = errorCategory(err) result.Message = err.Error() result.err = err - case plan.installedFor(a.Name): + case status == agentconfig.StatusUnchanged: result.Status = statusReasserted - result.Path = path + result.Path = filepath.ToSlash(path) default: result.Status = statusInstalled - result.Path = path + result.Path = filepath.ToSlash(path) } out.Agents = append(out.Agents, result) } return out } -// notInstalledError is the failure of an agent named with --agents that is not installed. Writing its -// config would create its config directory, so the agent is refused instead. -func notInstalledError(ctx context.Context, a *agents.Agent) error { - dir, err := a.ConfigDir(ctx) +// requireInstalled fails when an agent named with --agents has neither its MCP config file nor the +// directory that holds it. Writing the config would create that directory, so the agent is refused. +func requireInstalled(ctx context.Context, a *agents.Agent) error { + installed, err := mcpInstalled(ctx, a) + if err != nil { + return err + } + if installed { + return nil + } + dir, err := mcpDir(ctx, a) if err != nil { return err } - return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%s is not installed (no %s); install it first or leave it out of --agents", a.DisplayName, dir) + return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%s is not installed (no %s); install it first or leave it out of --agents", a.DisplayName, filepath.ToSlash(dir)) +} + +// mcpInstalled reports whether the agent's MCP config file or the directory that holds it exists. +func mcpInstalled(ctx context.Context, a *agents.Agent) (bool, error) { + path, err := a.MCP.Path(ctx) + if err != nil { + return false, err + } + exists, err := pathExists(path) + if err != nil || exists { + return exists, err + } + dir, err := mcpDir(ctx, a) + if err != nil { + return false, err + } + return pathExists(dir) +} + +// mcpDir returns the directory that holds the agent's MCP config file. Claude Code keeps that file in the +// home directory, which always exists, so its config directory stands for it instead. +func mcpDir(ctx context.Context, a *agents.Agent) (string, error) { + path, err := a.MCP.Path(ctx) + if err != nil { + return "", err + } + home, err := env.UserHomeDir(ctx) + if err != nil { + return "", err + } + dir := filepath.Dir(path) + if filepath.Clean(dir) == filepath.Clean(home) { + return a.ConfigDir(ctx) + } + return dir, nil } -// installedFor reports whether state already records the server for the agent. -func (p addPlan) installedFor(agent string) bool { - if p.entry == nil { - return false +func pathExists(path string) (bool, error) { + _, err := os.Stat(path) + switch { + case err == nil: + return true, nil + case errors.Is(err, fs.ErrNotExist): + return false, nil + default: + return false, err } - _, ok := p.entry.Agents[agent] - return ok } // recordInstalls adds each server that reached at least one agent to the state file. The file is @@ -623,21 +699,22 @@ func recordServer(state *mcp.State, req addRequest, plan addPlan, paths map[stri } } -// agentFailureError returns the failures of agents named with --agents. Agents picked by detection -// only warn, and the results already show them. +// agentFailureError returns the agents' failures as an agentErrors. They fail the command when an agent was +// named with --agents, or when no agent was installed at all. Otherwise detected agents only warn, as in +// install, and the results already show them. func agentFailureError(req addRequest, servers []addServerOutput) error { - if !req.explicit { - return nil - } var errs []error + installed := false for _, server := range servers { for _, a := range server.Agents { if a.err != nil { errs = append(errs, a.err) + } else { + installed = true } } } - if len(errs) == 0 { + if len(errs) == 0 || (!req.explicit && installed) { return nil } return &agentErrors{err: errors.Join(errs...)} @@ -674,12 +751,12 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut if a.Status != statusFailed { succeeded = true } - if a.Name == cursorAgent && a.Status != statusFailed { + if a.Name == cursorAgent && a.Status == statusInstalled { cursor = true } } if server.SignInRequired && succeeded { - cmdio.LogString(ctx, fmt.Sprintf("%s needs sign-in. Run: databricks aitools mcp login %s --profile %s", server.Service, server.Service, req.profile)) + cmdio.LogString(ctx, fmt.Sprintf("%s needs sign-in. Run: databricks aitools mcp login %s --profile %s", server.Service, shellquote.BashArg(server.Service), shellquote.BashArg(req.profile))) } if cursor { cmdio.LogString(ctx, cursorApprovalHint(server.Name)) diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index b766e2d7bc8..c0139dfb658 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -2,8 +2,13 @@ package aitools import ( "bytes" + "context" "encoding/json" "errors" + "fmt" + "io/fs" + "net/http" + "net/http/httptest" "os" "path/filepath" "strings" @@ -12,6 +17,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/aitools/mcp/agentconfig" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/flags" @@ -48,9 +54,22 @@ func TestValidateMCPAddFlags(t *testing.T) { }, { name: "name must be a valid server name", flags: mcpAddFlags{name: "a.b"}, names: []string{"github"}, - want: `--name "a.b" may only contain letters, digits, '_' and '-'`, + want: `--name "a.b" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + }, + { + name: "name must not contain a double underscore", flags: mcpAddFlags{name: "a__b"}, names: []string{"github"}, + want: `--name "a__b" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + }, + { + name: "name must not start with an underscore", flags: mcpAddFlags{name: "_a"}, names: []string{"github"}, + want: `--name "_a" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + }, + { + name: "name must not end with a dash", flags: mcpAddFlags{name: "a-"}, names: []string{"github"}, + want: `--name "a-" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, }, {name: "valid name", flags: mcpAddFlags{name: "my_github-1"}, names: []string{"github"}}, + {name: "valid name in mixed case", flags: mcpAddFlags{name: "GitHub"}, names: []string{"github"}}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -83,16 +102,33 @@ func testServices() []mcp.Service { } } +func TestServiceNames(t *testing.T) { + got, err := serviceNames("system.ai", []string{"github", "System.AI.GitHub", "system.ai.genie_one_mcp"}) + require.NoError(t, err) + assert.Equal(t, []string{"system.ai.github", "System.AI.GitHub", "system.ai.genie_one_mcp"}, got) + + _, err = serviceNames("system.ai", []string{"main.tools.crm"}) + require.Error(t, err) + assert.Equal(t, "main.tools.crm is not in system.ai; pass --location to match its schema", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + + // A two-part name is neither a leaf nor a full name, so it is not read as a leaf in the location. + _, err = serviceNames("system.ai", []string{"ai.github"}) + require.Error(t, err) + assert.Equal(t, "ai.github is not a service name: use a leaf such as github or a full name such as system.ai.github", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) +} + func TestPickServices(t *testing.T) { services := testServices() - picked, err := pickServices("system.ai", []string{"github", "system.ai.genie_one_mcp", "github"}, services) + picked, err := pickServices("system.ai", []string{"system.ai.github", "system.ai.genie_one_mcp", "system.ai.github"}, services) require.NoError(t, err) require.Len(t, picked, 2) assert.Equal(t, "system.ai.github", picked[0].FullName) assert.Equal(t, "system.ai.genie_one_mcp", picked[1].FullName) - picked, err = pickServices("System.AI", []string{"GitHub"}, services) + picked, err = pickServices("System.AI", []string{"System.AI.GitHub"}, services) require.NoError(t, err) assert.Equal(t, "system.ai.github", picked[0].FullName) @@ -100,10 +136,6 @@ func TestPickServices(t *testing.T) { require.Error(t, err) assert.Equal(t, "system.ai.foo is not an MCP service in system.ai. Available: system.ai.genie_one_mcp, system.ai.github", err.Error()) assert.Equal(t, protos.AitoolsErrorCategory("MCP_SERVICE_NOT_FOUND"), errorCategory(err)) - - _, err = pickServices("system.ai", []string{"main.tools.crm"}, services) - require.Error(t, err) - assert.Equal(t, "main.tools.crm is not in system.ai; pass --location to match its schema", err.Error()) } func TestResolveMCPAgents(t *testing.T) { @@ -143,16 +175,81 @@ func TestDescribeListErrorNamesProfileAndKeepsCause(t *testing.T) { assert.Equal(t, plain, describeListError("logfood", plain)) } -// withHome points the home directory at a temp dir, so agent config paths are inside it. +// withHome points the home directory at a temp dir, so agent config paths are inside it. The +// agent home overrides are cleared, so a developer's own settings do not reach the test. func withHome(t *testing.T) string { t.Helper() home := t.TempDir() t.Setenv(env.HomeEnvVar(), home) - t.Setenv("CLAUDE_CONFIG_DIR", "") - t.Setenv("CODEX_HOME", "") + for _, name := range agentHomeEnv { + t.Setenv(name, "") + } return home } +// agentHomeEnv are the variables that move an agent's config directory. +var agentHomeEnv = []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA", "DATABRICKS_CONFIG_FILE"} + +// withDatabricksBinary makes os.Args[0] the databricks command, as the installed CLI has it. +func withDatabricksBinary(t *testing.T) { + t.Helper() + saved := os.Args[0] + os.Args[0] = "databricks" + t.Cleanup(func() { os.Args[0] = saved }) +} + +// noRequestServer returns a server that fails the test on any request. A command that refuses before +// it contacts the profile's host never reaches it. +func noRequestServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request before the refusal: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(server.Close) + return server +} + +// noAPIServer returns a server that fails the test on any API call. The SDK still reads host metadata +// and OIDC discovery when it configures a profile, so those paths are allowed. +func noAPIServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + t.Errorf("unexpected API request: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(server.Close) + return server +} + +// writeHomeConfig writes the default Databricks config file, ~/.databrickscfg, under home. Entries that are +// not pinned to a file use that file. +func writeHomeConfig(t *testing.T, home, contents string) { + t.Helper() + require.NoError(t, os.WriteFile(filepath.Join(home, ".databrickscfg"), []byte(contents), 0o600)) +} + +// writeAddConfig writes a Databricks config file elsewhere and points DATABRICKS_CONFIG_FILE at it. +func writeAddConfig(t *testing.T, contents string) string { + t.Helper() + path := filepath.Join(t.TempDir(), ".databrickscfg") + require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) + t.Setenv("DATABRICKS_CONFIG_FILE", path) + return path +} + +// runAdd runs `aitools mcp add` with args and returns its error. +func runAdd(t *testing.T, args ...string) error { + t.Helper() + withDatabricksBinary(t) + _, err := newAddTestCmd(t, args...) + return err +} + // An agent named with --agents that is not installed is refused: its config is not written and its // directory is not created. The agent that is installed still gets the server. func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { @@ -173,7 +270,7 @@ func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { codex := out.Agents[0] assert.Equal(t, statusFailed, codex.Status) assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, codex.ErrorCategory) - assert.Equal(t, "Codex CLI is not installed (no "+filepath.Join(home, ".codex")+"); install it first or leave it out of --agents", codex.Message) + assert.Equal(t, "Codex CLI is not installed (no "+filepath.ToSlash(filepath.Join(home, ".codex"))+"); install it first or leave it out of --agents", codex.Message) assert.NoDirExists(t, filepath.Join(home, ".codex")) assert.Equal(t, statusInstalled, out.Agents[1].Status) @@ -183,11 +280,11 @@ func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { func TestPlanAddsNaming(t *testing.T) { ctx := t.Context() claude := agents.ByName("claude-code") - host := "https://adb-123.test" + const host = "https://adb-123.test" t.Run("new service gets the generated name", func(t *testing.T) { withHome(t) - plans, err := planAdds(ctx, "", "system.ai", "logfood", host, &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) + plans, err := planAdds(ctx, "", "logfood", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) require.NoError(t, err) require.Len(t, plans, 1) assert.Equal(t, "genie_one_mcp-logfood", plans[0].name) @@ -197,7 +294,7 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("existing install keeps its name", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "custom-name", Service: "system.ai.github", Profile: "logfood", Host: host}}} - plans, err := planAdds(ctx, "", "system.ai", "logfood", host, state, testServices()[1:], []*agents.Agent{claude}) + plans, err := planAdds(ctx, "", "logfood", state, testServices()[1:], []*agents.Agent{claude}) require.NoError(t, err) assert.Equal(t, "custom-name", plans[0].name) assert.NotNil(t, plans[0].entry) @@ -206,7 +303,7 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("name used by another state entry gets a suffix", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "genie_one_mcp-logfood", Service: "main.tools.other", Profile: "logfood", Host: host}}} - plans, err := planAdds(ctx, "", "system.ai", "logfood", host, state, testServices()[:1], []*agents.Agent{claude}) + plans, err := planAdds(ctx, "", "logfood", state, testServices()[:1], []*agents.Agent{claude}) require.NoError(t, err) assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) }) @@ -215,7 +312,7 @@ func TestPlanAddsNaming(t *testing.T) { home := withHome(t) unmanaged := `{"mcpServers":{"genie_one_mcp-logfood":{"type":"stdio","command":"ug","args":["mcp-proxy"]}}}` require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(unmanaged), 0o600)) - plans, err := planAdds(ctx, "", "system.ai", "logfood", host, &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) + plans, err := planAdds(ctx, "", "logfood", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) require.NoError(t, err) assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) }) @@ -223,7 +320,7 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("name flag for an installed service must match", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host}}} - _, err := planAdds(ctx, "other", "system.ai", "logfood", host, state, testServices()[1:], []*agents.Agent{claude}) + _, err := planAdds(ctx, "other", "logfood", state, testServices()[1:], []*agents.Agent{claude}) require.Error(t, err) assert.Equal(t, "system.ai.github is already installed on profile logfood as github-logfood; remove it before using --name other", err.Error()) }) @@ -231,32 +328,34 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("name flag already used by another service is refused", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "taken", Service: "main.tools.other", Profile: "logfood", Host: host}}} - _, err := planAdds(ctx, "taken", "system.ai", "logfood", host, state, testServices()[:1], []*agents.Agent{claude}) + _, err := planAdds(ctx, "taken", "logfood", state, testServices()[:1], []*agents.Agent{claude}) require.Error(t, err) assert.Equal(t, "--name taken is used by another installed server; pick a different name", err.Error()) }) - - t.Run("host change is refused, never rebound", func(t *testing.T) { - withHome(t) - state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://old.test"}}} - _, err := planAdds(ctx, "", "system.ai", "logfood", host, state, testServices()[1:], []*agents.Agent{claude}) - require.Error(t, err) - assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) - assert.Contains(t, err.Error(), "was set up for https://old.test, but profile \"logfood\" now points at "+host) - }) } -func TestPinnedConfigFile(t *testing.T) { +func TestAddConfigFileResolvesAsBindDoes(t *testing.T) { ctx := t.Context() - t.Setenv("DATABRICKS_CONFIG_FILE", "") - got, err := pinnedConfigFile(ctx) + home := withHome(t) + + // Unset: the default file is resolved, but nothing is pinned into the agent entries. + path, pinned, err := addConfigFile(ctx) require.NoError(t, err) - assert.Empty(t, got) + assert.Equal(t, filepath.Join(home, ".databrickscfg"), path) + assert.Empty(t, pinned) + + // A leading "~" is the home directory, not a directory named "~" under the working directory. + t.Setenv("DATABRICKS_CONFIG_FILE", "~/cfg/.databrickscfg") + path, pinned, err = addConfigFile(ctx) + require.NoError(t, err) + assert.Equal(t, filepath.Join(home, "cfg", ".databrickscfg"), path) + assert.Equal(t, path, pinned) t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join("rel", ".databrickscfg")) - got, err = pinnedConfigFile(ctx) + path, pinned, err = addConfigFile(ctx) require.NoError(t, err) - assert.True(t, filepath.IsAbs(got), got) + assert.True(t, filepath.IsAbs(path), path) + assert.Equal(t, path, pinned) } func TestCursorApprovalHintNamesBinary(t *testing.T) { @@ -265,19 +364,26 @@ func TestCursorApprovalHintNamesBinary(t *testing.T) { cursorApprovalHint("github-logfood")) } -// newAddTestCmd builds `aitools mcp add` under a root with the --output flag, as root does. -func newAddTestCmd(t *testing.T, args ...string) (*bytes.Buffer, error) { - t.Helper() +// newAddTestRoot returns a root command with the persistent --profile and --output flags that root +// defines, and the mcp group. +func newAddTestRoot() *cobra.Command { parent := &cobra.Command{Use: "databricks", SilenceErrors: true, SilenceUsage: true} output := flags.OutputText parent.PersistentFlags().VarP(&output, "output", "o", "output type: text or json") + parent.PersistentFlags().StringP("profile", "p", "", "profile to use") parent.AddCommand(NewMcpCmd()) + return parent +} +// newAddTestCmd runs `aitools mcp add` under a root, as root does. +func newAddTestCmd(t *testing.T, args ...string) (*bytes.Buffer, error) { + t.Helper() + parent := newAddTestRoot() var stdout bytes.Buffer parent.SetOut(&stdout) parent.SetErr(&bytes.Buffer{}) parent.SetArgs(append([]string{"mcp", "add"}, args...)) - return &stdout, parent.ExecuteContext(t.Context()) + return &stdout, parent.ExecuteContext(cmdio.MockDiscard(t.Context())) } func TestMcpAddJSONReportsMissingFlagsAsDocument(t *testing.T) { @@ -292,3 +398,199 @@ func TestMcpAddJSONReportsMissingFlagsAsDocument(t *testing.T) { assert.Empty(t, out.Servers) assert.Equal(t, "global", out.Scope) } + +// A profile re-pointed at another host is refused before any request reaches that host. +func TestMcpAddRefusesRepointedHostBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test", + }}})) + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) + assert.Contains(t, err.Error(), "was set up for https://adb-123.test, but profile \"logfood\" now points at "+server.URL) + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +func TestMcpAddRefusesWorkspaceChangeOnSameHostBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: server.URL, WorkspaceID: "111", + }}})) + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) + assert.Contains(t, err.Error(), "was set up for workspace 111, but profile \"logfood\" now points at workspace 222") +} + +// A second service on a profile whose first entry is pinned to another file is refused as a whole: +// no agent file is written and the state keeps its one entry. +func TestMcpAddRefusesPartialReaddWithOtherConfigFile(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: server.URL, + // Unpinned: bound to the default file, which is not the one DATABRICKS_CONFIG_FILE names below. + }}})) + writeAddConfig(t, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + err := runAdd(t, "--profile", "logfood", "--names", "genie_one_mcp,github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) + assert.Contains(t, err.Error(), "was set up with config file") + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Equal(t, "github-logfood", state.Servers[0].Name) +} + +// The profile's own credentials are used. The environment's token is not sent to a profile without one. +func TestMcpAddNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { + home := withHome(t) + server := noAPIServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\n") + t.Setenv("DATABRICKS_TOKEN", "dapi-env-token") + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory("AUTH_FAILED"), errorCategory(err)) + assert.Contains(t, err.Error(), "databricks auth login --profile logfood") +} + +// A configuration the SDK rejects is not an authentication failure, so a login would not fix it. +func TestMcpAddInvalidConfigIsNotAuthFailed(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + t.Setenv("DATABRICKS_RATE_LIMIT", "soon") + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) +} + +// --agents is checked with the other flags, before the profile is read or the network is used. +func TestMcpAddRejectsUnknownAgentBeforeProfile(t *testing.T) { + withHome(t) + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "pi") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(err)) +} + +func TestMcpAddJSONWriteFailureKeepsRunError(t *testing.T) { + withHome(t) + withDatabricksBinary(t) + parent := newAddTestRoot() + parent.SetOut(failingWriter{}) + parent.SetErr(&bytes.Buffer{}) + parent.SetArgs([]string{"mcp", "add", "--names", "github", "--output", "json"}) + + err := parent.ExecuteContext(cmdio.MockDiscard(t.Context())) + require.Error(t, err) + assert.ErrorContains(t, err, "--output json requires --agents") + assert.ErrorContains(t, err, "disk full") +} + +type failingWriter struct{} + +func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("disk full") } + +// An agent with a config directory is installed for MCP even when Detected would say otherwise. +func TestInstallServerCodexHomeCountsAsInstalled(t *testing.T) { + home := withHome(t) + codexHome := filepath.Join(t.TempDir(), "codex") + require.NoError(t, os.MkdirAll(codexHome, 0o700)) + t.Setenv("CODEX_HOME", codexHome) + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("codex")}, binary: "databricks"} + out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) + require.Len(t, out.Agents, 1) + assert.Equal(t, statusInstalled, out.Agents[0].Status) + assert.NoError(t, out.Agents[0].err) + assert.FileExists(t, filepath.Join(codexHome, "config.toml")) + assert.NoDirExists(t, filepath.Join(home, ".codex")) +} + +// Gemini CLI is installed once its settings directory exists, whether or not projects.json does. +func TestInstallServerGeminiWithoutProjectsJSON(t *testing.T) { + home := withHome(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini"), 0o700)) + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("gemini")}, binary: "databricks"} + out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) + require.Len(t, out.Agents, 1) + assert.Equal(t, statusInstalled, out.Agents[0].Status) + assert.FileExists(t, filepath.Join(home, ".gemini", "settings.json")) +} + +// Antigravity's MCP file is in ~/.gemini/config, so that directory is what says it is installed. +func TestInstallServerAntigravityUsesItsMCPDirectory(t *testing.T) { + home := withHome(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "config"), 0o700)) + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("antigravity")}, binary: "databricks"} + out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) + require.Len(t, out.Agents, 1) + assert.Equal(t, statusInstalled, out.Agents[0].Status) + assert.FileExists(t, filepath.Join(home, ".gemini", "config", "mcp_config.json")) +} + +// A second install of the same entry reports "re-asserted" from the agent file, even when the state +// that recorded it is gone. +func TestInstallServerReassertsFromAgentFile(t *testing.T) { + home := withHome(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".claude"), 0o700)) + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("claude-code")}, binary: "databricks"} + plan := addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github-logfood"} + + first := installServer(t.Context(), req, plan) + require.Len(t, first.Agents, 1) + assert.Equal(t, statusInstalled, first.Agents[0].Status) + + second := installServer(t.Context(), req, plan) + require.Len(t, second.Agents, 1) + assert.Equal(t, statusReasserted, second.Agents[0].Status) +} + +func TestAgentFailureErrorWhenNothingInstalled(t *testing.T) { + ok := addServerOutput{Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled}}} + failed := addServerOutput{Agents: []addAgentOutput{{Name: "codex", Status: statusFailed, err: errors.New("bad config")}}} + + // Detected agents only warn, unless none of them was installed. + assert.NoError(t, agentFailureError(addRequest{}, []addServerOutput{ok, failed})) + err := agentFailureError(addRequest{}, []addServerOutput{failed}) + require.Error(t, err) + assert.ErrorContains(t, err, "bad config") + + // Agents named with --agents fail the command on any failure. + assert.Error(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{ok, failed})) + assert.NoError(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{ok})) +} + +func TestRequireProfileNamesProfileAndPath(t *testing.T) { + ctx := t.Context() + path := writeAddConfig(t, "[other]\nhost = https://other.test\n") + + err := requireProfile(ctx, "logfood") + require.Error(t, err) + assert.Equal(t, "profile logfood not found in "+filepath.ToSlash(path), err.Error()) + assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_NOT_FOUND"), errorCategory(err)) +} + +func TestErrorCategoryMapsTypedFailures(t *testing.T) { + assert.Equal(t, protos.AitoolsErrorCategory("MCP_NAME_CONFLICT"), + errorCategory(fmt.Errorf("claude-code: server %q: %w", "github", agentconfig.ErrNotOwned))) + assert.Equal(t, protos.AitoolsErrorCategoryTimeout, errorCategory(fmt.Errorf("list: %w", context.DeadlineExceeded))) + assert.Equal(t, protos.AitoolsErrorCategoryPermissionDenied, errorCategory(&fs.PathError{Op: "open", Path: "x", Err: fs.ErrPermission})) + assert.Equal(t, protos.AitoolsErrorCategory("AUTH_FAILED"), errorCategory(mcp.AuthError("logfood", errors.New("no token")))) +} diff --git a/cmd/aitools/mcp_binary.go b/cmd/aitools/mcp_binary.go index 4ba6fed9eee..eb1f1ec0a25 100644 --- a/cmd/aitools/mcp_binary.go +++ b/cmd/aitools/mcp_binary.go @@ -13,13 +13,22 @@ func mcpBinaryPath() (string, bool) { return binaryPathFor(os.Args[0], exec.LookPath) } -// binaryPathFor resolves arg0 without touching the file system beyond lookPath. When neither -// arg0 nor its PATH lookup is absolute, it returns the bare name, which the agent must find on PATH. +// binaryPathFor resolves arg0 without touching the file system beyond lookPath. An absolute arg0 is kept. +// A relative arg0 with a directory, such as ./databricks, is made absolute, and its symlinks are not +// resolved. A bare name is looked up on PATH. When none of these gives an absolute path, it returns the +// bare name, which the agent must find on PATH. func binaryPathFor(arg0 string, lookPath func(string) (string, error)) (string, bool) { if filepath.IsAbs(arg0) { return arg0, true } - if found, err := lookPath(filepath.Base(arg0)); err == nil && filepath.IsAbs(found) { + if filepath.Base(arg0) != arg0 { + abs, err := filepath.Abs(arg0) + if err != nil { + return "databricks", false + } + return abs, true + } + if found, err := lookPath(arg0); err == nil && filepath.IsAbs(found) { return found, true } return "databricks", false diff --git a/cmd/aitools/mcp_binary_test.go b/cmd/aitools/mcp_binary_test.go index d0fc20bf1aa..e20314c8ffb 100644 --- a/cmd/aitools/mcp_binary_test.go +++ b/cmd/aitools/mcp_binary_test.go @@ -6,46 +6,93 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestBinaryPathForAbsoluteArg0KeepsSymlinkPath(t *testing.T) { - // The invoked path is kept as is, so a Homebrew symlink is not replaced by its target. - arg0 := filepath.Join(t.TempDir(), "bin", "databricks") - lookPath := func(string) (string, error) { - t.Fatal("PATH lookup must not run when arg0 is absolute") - return "", nil - } - - path, absolute := binaryPathFor(arg0, lookPath) - assert.True(t, absolute) - assert.Equal(t, arg0, path) -} +func TestBinaryPathFor(t *testing.T) { + absolute := filepath.Join(t.TempDir(), "bin", "databricks") + onPath := filepath.Join(t.TempDir(), "databricks") + relativeOnPath := filepath.Join("bin", "databricks") + notFound := func(string) (string, error) { return "", errors.New("not found") } + found := func(string) (string, error) { return onPath, nil } + relative := func(string) (string, error) { return relativeOnPath, nil } -func TestBinaryPathForRelativeArg0UsesPathLookup(t *testing.T) { - found := filepath.Join(t.TempDir(), "databricks") - var looked string - lookPath := func(name string) (string, error) { - looked = name - return found, nil + tests := []struct { + name string + arg0 string + lookPath func(string) (string, error) + // wantLookup is the name the PATH lookup receives, or "" when the lookup must not run. + wantLookup string + wantPath string + wantAbs bool + }{ + { + // The invoked path is kept as is, so a Homebrew symlink is not replaced by its target. + name: "absolute arg0 is kept", + arg0: absolute, + lookPath: nil, + wantPath: absolute, + wantAbs: true, + }, + { + name: "relative arg0 with a directory is made absolute, not looked up", + arg0: filepath.Join("bin", "databricks"), + lookPath: found, + wantPath: mustAbs(t, filepath.Join("bin", "databricks")), + wantAbs: true, + }, + { + name: "dot-relative arg0 with a directory is made absolute", + arg0: "." + string(filepath.Separator) + "databricks", + lookPath: found, + wantPath: mustAbs(t, "."+string(filepath.Separator)+"databricks"), + wantAbs: true, + }, + { + name: "bare name is looked up on PATH", + arg0: "databricks", + lookPath: found, + wantLookup: "databricks", + wantPath: onPath, + wantAbs: true, + }, + { + name: "bare name not on PATH falls back to the bare name", + arg0: "databricks", + lookPath: notFound, + wantLookup: "databricks", + wantPath: "databricks", + }, + { + name: "relative PATH entry falls back to the bare name", + arg0: "databricks", + lookPath: relative, + wantLookup: "databricks", + wantPath: "databricks", + }, } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var looked string + lookPath := func(name string) (string, error) { + looked = name + if tt.lookPath == nil { + t.Fatal("PATH lookup must not run for this arg0") + } + return tt.lookPath(name) + } - path, absolute := binaryPathFor("./databricks", lookPath) - assert.True(t, absolute) - assert.Equal(t, found, path) - assert.Equal(t, "databricks", looked) -} - -func TestBinaryPathForFallsBackToBareName(t *testing.T) { - notFound := func(string) (string, error) { return "", errors.New("not found") } - path, absolute := binaryPathFor("databricks", notFound) - assert.False(t, absolute) - assert.Equal(t, "databricks", path) + path, abs := binaryPathFor(tt.arg0, lookPath) + assert.Equal(t, tt.wantPath, path) + assert.Equal(t, tt.wantAbs, abs) + assert.Equal(t, tt.wantLookup, looked) + }) + } } -func TestBinaryPathForRelativeLookupResultFallsBackToBareName(t *testing.T) { - // A relative PATH entry does not give a path an agent can start from another directory. - relative := func(string) (string, error) { return filepath.Join("bin", "databricks"), nil } - path, absolute := binaryPathFor("databricks", relative) - assert.False(t, absolute) - assert.Equal(t, "databricks", path) +func mustAbs(t *testing.T, path string) string { + t.Helper() + abs, err := filepath.Abs(path) + require.NoError(t, err) + return abs } diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go index b3bb4779eb8..6feeff931e0 100644 --- a/cmd/aitools/mcp_test.go +++ b/cmd/aitools/mcp_test.go @@ -112,8 +112,8 @@ func TestErrorCategoryUsesTypedErrorsOnly(t *testing.T) { assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(typed)) assert.Equal(t, "unknown agent \"pi\"", typed.Error()) - wrapped := failure(categoryAuthFailed, errors.New("boom"), "%s", "boom") - assert.Equal(t, categoryAuthFailed, errorCategory(wrapped)) + wrapped := failure(categoryServiceNotFound, errors.New("boom"), "%s", "boom") + assert.Equal(t, categoryServiceNotFound, errorCategory(wrapped)) configInvalid := &os.PathError{Op: "parse", Path: "x", Err: agentconfig.ErrConfigInvalid} assert.Equal(t, categoryAgentConfigInvalid, errorCategory(configInvalid)) From 3317af4de4181f38f7bd44cd22cf789f8f5085d8 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:48:02 +0000 Subject: [PATCH 07/20] Seed the mcp acceptance services through the fake, and clear agent homes The shared test.toml no longer stubs the listing with a fixed body. Each script creates the services it lists through the fake's POST handler, so parent filtering and the FULL view are exercised. Agent home overrides are cleared in test.toml. The hidden check uses contains.py. The new add-default-config script covers an unpinned entry. Output changes: add-text no longer repeats the Cursor approval hint for re-asserted entries; hidden checks the help text with contains.py. Co-authored-by: Isaac --- .../mcp/add-agent-not-installed/script | 3 ++ .../mcp/add-default-config/.databrickscfg | 3 ++ .../mcp/add-default-config/out.test.toml | 1 + .../aitools/mcp/add-default-config/output.txt | 23 ++++++++++++ .../cmd/aitools/mcp/add-default-config/script | 17 +++++++++ .../cmd/aitools/mcp/add-text/output.txt | 2 - acceptance/cmd/aitools/mcp/add-text/script | 4 ++ .../cmd/aitools/mcp/add-two-agents/script | 4 ++ .../cmd/aitools/mcp/add-two-profiles/script | 5 +++ .../aitools/mcp/add-unparsable-config/script | 3 ++ acceptance/cmd/aitools/mcp/hidden/output.txt | 4 +- acceptance/cmd/aitools/mcp/hidden/script | 3 +- acceptance/cmd/aitools/mcp/test.toml | 37 +++++-------------- 13 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/add-default-config/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/add-default-config/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/add-default-config/output.txt create mode 100644 acceptance/cmd/aitools/mcp/add-default-config/script diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/script b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script index 96cc5866926..b06084c14e8 100644 --- a/acceptance/cmd/aitools/mcp/add-agent-not-installed/script +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script @@ -4,6 +4,9 @@ mkdir -p home/.claude 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"}' > /dev/null + title "An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits 1" errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,codex --output json diff --git a/acceptance/cmd/aitools/mcp/add-default-config/.databrickscfg b/acceptance/cmd/aitools/mcp/add-default-config/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-default-config/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/add-default-config/out.test.toml b/acceptance/cmd/aitools/mcp/add-default-config/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-default-config/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-default-config/output.txt b/acceptance/cmd/aitools/mcp/add-default-config/output.txt new file mode 100644 index 00000000000..df99b30913a --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-default-config/output.txt @@ -0,0 +1,23 @@ + +=== Without DATABRICKS_CONFIG_FILE the server is added for the default config file + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code + +genie_one_mcp-logfood system.ai.genie_one_mcp + claude-code installed [TEST_TMP_DIR]/home/.claude.json + +=== Claude Code config has no pinned variables for the server + +>>> jq .mcpServers["genie_one_mcp-logfood"].env home/.claude.json +{} + +=== State holds the binding with no config file + +>>> jq .mcp.servers[] | {name, profile, config_file} home/.databricks/aitools/skills/.state.json +{ + "name": "genie_one_mcp-logfood", + "profile": "logfood", + "config_file": "" +} diff --git a/acceptance/cmd/aitools/mcp/add-default-config/script b/acceptance/cmd/aitools/mcp/add-default-config/script new file mode 100644 index 00000000000..74ab0765340 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-default-config/script @@ -0,0 +1,17 @@ +# Isolate HOME. The profile is in the default config file, so DATABRICKS_CONFIG_FILE is unset and no config file is pinned. +sethome home +mkdir -p home/.claude +envsubst < .databrickscfg > home/.databrickscfg +unset DATABRICKS_CONFIG_FILE + +# 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"}' > /dev/null + +title "Without DATABRICKS_CONFIG_FILE the server is added for the default config file" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code + +title "Claude Code config has no pinned variables for the server" +trace jq '.mcpServers["genie_one_mcp-logfood"].env' home/.claude.json + +title "State holds the binding with no config file" +trace jq '.mcp.servers[] | {name, profile, config_file}' home/.databricks/aitools/skills/.state.json diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt index 4f1954ea338..d35665a25fc 100644 --- a/acceptance/cmd/aitools/mcp/add-text/output.txt +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -25,13 +25,11 @@ Agents (named with --agents): claude-code, cursor genie_one_mcp-logfood system.ai.genie_one_mcp claude-code already installed, re-asserted [TEST_TMP_DIR]/home/.claude.json cursor already installed, re-asserted [TEST_TMP_DIR]/home/.cursor/mcp.json -Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enable genie_one_mcp-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. github-logfood system.ai.github claude-code already installed, re-asserted [TEST_TMP_DIR]/home/.claude.json cursor already installed, re-asserted [TEST_TMP_DIR]/home/.cursor/mcp.json system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood -Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. === Cursor config diff --git a/acceptance/cmd/aitools/mcp/add-text/script b/acceptance/cmd/aitools/mcp/add-text/script index 8c2195203de..8c8b4f39d95 100644 --- a/acceptance/cmd/aitools/mcp/add-text/script +++ b/acceptance/cmd/aitools/mcp/add-text/script @@ -4,6 +4,10 @@ mkdir -p home/.claude home/.cursor 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"}' > /dev/null +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"}}}}' > /dev/null + title "Text output: Cursor approval hint, and the sign-in step for github" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,cursor diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/script b/acceptance/cmd/aitools/mcp/add-two-agents/script index 3cb691e4fcd..592500e4672 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/script +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -4,6 +4,10 @@ mkdir -p home/.claude home/.codex 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"}' > /dev/null +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"}}}}' > /dev/null + title "Two services for two agents, as JSON" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex --output json diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/script b/acceptance/cmd/aitools/mcp/add-two-profiles/script index 860266a85c5..4ff7847ab2c 100644 --- a/acceptance/cmd/aitools/mcp/add-two-profiles/script +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/script @@ -4,6 +4,11 @@ mkdir -p home/.claude 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"}' > /dev/null +# The fake keeps one workspace per token, so the second profile needs its own copy. +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 dogfood --json '{"comment": "Ask questions about your data"}' > /dev/null + title "The same service for a second profile adds an entry and renames nothing" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code trace $CLI aitools mcp add --profile dogfood --names system.ai.genie_one_mcp --agents claude-code --output json | jq '{profile, servers: [.servers[] | {name, agents: [.agents[] | {name, status}]}]}' diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/script b/acceptance/cmd/aitools/mcp/add-unparsable-config/script index 4b97eb147ed..73a4d7bfef8 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/script +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/script @@ -4,6 +4,9 @@ mkdir -p home/.claude 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"}' > /dev/null + title "An unparsable config fails only its agent; the other agent still installs, and the command exits 1" errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex,claude-code --output json diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index b5dc617c68a..94e05a08381 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -1,8 +1,6 @@ === The mcp group is hidden from aitools help - ->>> [CLI] aitools --help -0 +aitools help does not list mcp === The hidden group still answers help for its subcommands diff --git a/acceptance/cmd/aitools/mcp/hidden/script b/acceptance/cmd/aitools/mcp/hidden/script index f96bd778250..bec6b90955b 100644 --- a/acceptance/cmd/aitools/mcp/hidden/script +++ b/acceptance/cmd/aitools/mcp/hidden/script @@ -1,5 +1,6 @@ title "The mcp group is hidden from aitools help" -trace $CLI aitools --help | grep -c ' mcp ' || true +$CLI aitools --help | contains.py '!mcp' > /dev/null +echo "aitools help does not list mcp" title "The hidden group still answers help for its subcommands" trace $CLI aitools mcp add --help diff --git a/acceptance/cmd/aitools/mcp/test.toml b/acceptance/cmd/aitools/mcp/test.toml index 25de79431e7..7ca93a44ec7 100644 --- a/acceptance/cmd/aitools/mcp/test.toml +++ b/acceptance/cmd/aitools/mcp/test.toml @@ -1,31 +1,14 @@ -# The fake workspace lists two system.ai services: genie_one_mcp needs no sign-in and github -# is backed by an OAuth connection, so it needs sign-in. The list is the same for every -# query, which is enough for one schema. +# The fake workspace serves the services that each script creates through the POST handler, so the +# listing is the one the API would return for the parent schema. Agent home overrides are cleared so +# a developer's own settings never reach the agent config files that the tests write. Ignore = [ "home", ] -[[Server]] -Pattern = "GET /api/2.1/unity-catalog/mcp-services" -Response.Body = ''' -{ - "mcp_services": [ - { - "name": "mcp-services/system.ai.genie_one_mcp", - "comment": "Ask questions about your data" - }, - { - "name": "mcp-services/system.ai.github", - "comment": "Repositories, issues, pull requests", - "config": { - "source_connection": { - "name": "connections/github", - "options": { - "oauth_provider": "github" - } - } - } - } - ] -} -''' +[Env] +CLAUDE_CONFIG_DIR = "" +CODEX_HOME = "" +COPILOT_HOME = "" +GEMINI_CLI_HOME = "" +XDG_CONFIG_HOME = "" +APPDATA = "" From e57b959c7bd8ecaea07261065c3e5eb498e6d2d6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 18:16:26 +0000 Subject: [PATCH 08/20] Fix second-round review findings in aitools mcp add Refusals that need no request now run before the first request: --name conflicts with the state or with another profile's agent entry, an --agents agent that is not installed, and a profile that is gone or has no host. When every --agents agent is refused, the command stops there. Service and location names are lowercased before the binding check, and service names match case-insensitively. Names that agent files hold for another binding are reserved, so a new install never overwrites them. Agent presence is one rule for explicit and detected agents: the agent's own detection, its CLI on PATH, its MCP config file, or the directory that CODEX_HOME or COPILOT_HOME names. A service that reached no agent fails the command. A rejected credential is AUTH_FAILED with the login command. --name must be lowercase. Listing and profile errors use the categories from the spec. Printed paths use forward slashes; the state keeps the native paths. Co-authored-by: Isaac --- .../mcp/add-agent-not-installed/output.txt | 25 +- .../mcp/add-agent-not-installed/script | 10 +- .../aitools/mcp/add-default-config/output.txt | 4 +- .../cmd/aitools/mcp/add-default-config/script | 2 +- .../cmd/aitools/mcp/add-two-agents/output.txt | 5 + .../cmd/aitools/mcp/add-two-agents/script | 13 + .../home/.codex/config.toml | 2 +- .../mcp/add-unparsable-config/output.txt | 8 +- .../aitools/mcp/add-unparsable-config/script | 4 +- acceptance/cmd/aitools/mcp/test.toml | 21 +- cmd/aitools/mcp.go | 12 +- cmd/aitools/mcp_add.go | 325 +++++++++++------- cmd/aitools/mcp_add_test.go | 285 ++++++++++++--- cmd/aitools/mcp_binary.go | 8 +- cmd/aitools/mcp_binary_test.go | 8 + libs/aitools/agents/agents.go | 28 ++ libs/aitools/agents/detect_test.go | 83 +++++ libs/aitools/mcp/binding.go | 7 +- 18 files changed, 616 insertions(+), 234 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt b/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt index 4f94cfe3360..b186d02e28c 100644 --- a/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt @@ -1,7 +1,7 @@ -=== An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits 1 +=== An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits non-zero ->>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,codex --output json +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,antigravity --output json { "profile": "logfood", "host": "[DATABRICKS_URL]", @@ -18,10 +18,10 @@ "path": "[TEST_TMP_DIR]/home/.claude.json" }, { - "name": "codex", + "name": "antigravity", "status": "failed", "error_category": "UNKNOWN_AGENT", - "message": "Codex CLI is not installed (no [TEST_TMP_DIR]/home/.codex); install it first or leave it out of --agents" + "message": "Antigravity is not installed; install it first or leave it out of --agents" } ] } @@ -29,23 +29,14 @@ "error_category": "TYPE_UNSPECIFIED" } -Exit code: 1 - -=== Nothing was created for Codex +=== Nothing was created for Antigravity >>> ls -A home .claude .claude.json .databricks -=== With no installed agent named, the text output says what to do, and the command exits 1 - ->>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex -Profile logfood ([DATABRICKS_URL]) -Agents (named with --agents): codex - -genie_one_mcp-logfood system.ai.genie_one_mcp - codex failed: Codex CLI is not installed (no [TEST_TMP_DIR]/home/.codex); install it first or leave it out of --agents -Error: Codex CLI is not installed (no [TEST_TMP_DIR]/home/.codex); install it first or leave it out of --agents +=== With no installed agent named, the command is refused before any request, and the text output says what to do -Exit code: 1 +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents antigravity +Error: Antigravity is not installed; install it first or leave it out of --agents diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/script b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script index b06084c14e8..07cca049f6e 100644 --- a/acceptance/cmd/aitools/mcp/add-agent-not-installed/script +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script @@ -7,11 +7,11 @@ 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"}' > /dev/null -title "An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits 1" -errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,codex --output json +title "An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits non-zero" +musterr trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,antigravity --output json -title "Nothing was created for Codex" +title "Nothing was created for Antigravity" trace ls -A home -title "With no installed agent named, the text output says what to do, and the command exits 1" -errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex +title "With no installed agent named, the command is refused before any request, and the text output says what to do" +musterr trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents antigravity diff --git a/acceptance/cmd/aitools/mcp/add-default-config/output.txt b/acceptance/cmd/aitools/mcp/add-default-config/output.txt index df99b30913a..9d92f10571f 100644 --- a/acceptance/cmd/aitools/mcp/add-default-config/output.txt +++ b/acceptance/cmd/aitools/mcp/add-default-config/output.txt @@ -10,8 +10,8 @@ genie_one_mcp-logfood system.ai.genie_one_mcp === Claude Code config has no pinned variables for the server ->>> jq .mcpServers["genie_one_mcp-logfood"].env home/.claude.json -{} +>>> gron.py home/.claude.json +json.mcpServers.genie_one_mcp-logfood.env = {}; === State holds the binding with no config file diff --git a/acceptance/cmd/aitools/mcp/add-default-config/script b/acceptance/cmd/aitools/mcp/add-default-config/script index 74ab0765340..4d5c0f7b695 100644 --- a/acceptance/cmd/aitools/mcp/add-default-config/script +++ b/acceptance/cmd/aitools/mcp/add-default-config/script @@ -11,7 +11,7 @@ title "Without DATABRICKS_CONFIG_FILE the server is added for the default config trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code title "Claude Code config has no pinned variables for the server" -trace jq '.mcpServers["genie_one_mcp-logfood"].env' home/.claude.json +trace gron.py home/.claude.json | grep -F 'mcpServers.genie_one_mcp-logfood.env' title "State holds the binding with no config file" trace jq '.mcp.servers[] | {name, profile, config_file}' home/.databricks/aitools/skills/.state.json diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/output.txt b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt index 333d70f3b03..faa949bdf06 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/output.txt +++ b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt @@ -50,6 +50,11 @@ >>> cat home/.claude.json { "mcpServers": { + "my-server": { + "type": "stdio", + "command": "my-server", + "args": [] + }, "genie_one_mcp-logfood": { "args": [ "aitools", diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/script b/acceptance/cmd/aitools/mcp/add-two-agents/script index 592500e4672..c71d9bec0f2 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/script +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -8,6 +8,19 @@ export DATABRICKS_CONFIG_FILE=.databrickscfg 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"}' > /dev/null 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"}}}}' > /dev/null +# Claude Code's config already holds an entry of its own, which the add keeps. +cat > home/.claude.json <<'EOF' +{ + "mcpServers": { + "my-server": { + "type": "stdio", + "command": "my-server", + "args": [] + } + } +} +EOF + title "Two services for two agents, as JSON" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex --output json diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml b/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml index 0beaf816e05..ceb8c486282 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml @@ -1,2 +1,2 @@ -[mcp_servers.other +[mcp_servers.other x] command = "broken" diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt index f99d1fde360..52b28af6e45 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt @@ -1,5 +1,5 @@ -=== An unparsable config fails only its agent; the other agent still installs, and the command exits 1 +=== An unparsable config fails only its agent; the other agent still installs, and the command exits non-zero >>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex,claude-code --output json { @@ -16,7 +16,7 @@ "name": "codex", "status": "failed", "error_category": "AGENT_CONFIG_INVALID", - "message": "[TEST_TMP_DIR]/home/.codex/config.toml: agent config file cannot be parsed: toml: line 2: expected '.' or ']' to end table name, but got '\\n' instead" + "message": "[TEST_TMP_DIR]/home/.codex/config.toml: agent config file cannot be parsed: toml: line 1: expected '.' or ']' to end table name, but got 'x' instead" }, { "name": "claude-code", @@ -29,12 +29,10 @@ "error_category": "TYPE_UNSPECIFIED" } -Exit code: 1 - === The broken file is left as it was >>> cat home/.codex/config.toml -[mcp_servers.other +[mcp_servers.other x] command = "broken" === State records only the agent that succeeded diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/script b/acceptance/cmd/aitools/mcp/add-unparsable-config/script index 73a4d7bfef8..336785db1b2 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/script +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/script @@ -7,8 +7,8 @@ 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"}' > /dev/null -title "An unparsable config fails only its agent; the other agent still installs, and the command exits 1" -errcode trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex,claude-code --output json +title "An unparsable config fails only its agent; the other agent still installs, and the command exits non-zero" +musterr trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex,claude-code --output json title "The broken file is left as it was" trace cat home/.codex/config.toml diff --git a/acceptance/cmd/aitools/mcp/test.toml b/acceptance/cmd/aitools/mcp/test.toml index 7ca93a44ec7..340a89ac46e 100644 --- a/acceptance/cmd/aitools/mcp/test.toml +++ b/acceptance/cmd/aitools/mcp/test.toml @@ -1,14 +1,15 @@ # The fake workspace serves the services that each script creates through the POST handler, so the # listing is the one the API would return for the parent schema. Agent home overrides are cleared so # a developer's own settings never reach the agent config files that the tests write. -Ignore = [ - "home", -] +Ignore = ["home"] +Env.CLAUDE_CONFIG_DIR = "" +Env.CODEX_HOME = "" +Env.COPILOT_HOME = "" +Env.GEMINI_CLI_HOME = "" +Env.XDG_CONFIG_HOME = "" +Env.APPDATA = "" -[Env] -CLAUDE_CONFIG_DIR = "" -CODEX_HOME = "" -COPILOT_HOME = "" -GEMINI_CLI_HOME = "" -XDG_CONFIG_HOME = "" -APPDATA = "" +# Paths in the agent config files and the state file are JSON-escaped on Windows, with backslashes. +[[Repls]] +Old = '\\\\' +New = '/' diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index 3a186f14d98..1c3ca5ef2c3 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -19,12 +19,16 @@ import ( // defaultMCPLocation is the schema that ships the Databricks managed MCP services. const defaultMCPLocation = "system.ai" -// Error categories that the telemetry enum does not define yet. The JSON output reports them by name. -// The binding categories come from mcp.BindCategory, which has the same names. +// Error categories that the telemetry enum does not define yet. The JSON output reports them by name, as +// SPEC section 13 names them. The binding categories come from mcp.BindCategory, which has the same names. const ( categoryAgentConfigInvalid protos.AitoolsErrorCategory = "AGENT_CONFIG_INVALID" categoryNameConflict protos.AitoolsErrorCategory = "MCP_NAME_CONFLICT" categoryServiceNotFound protos.AitoolsErrorCategory = "MCP_SERVICE_NOT_FOUND" + categoryNoProfile protos.AitoolsErrorCategory = "NO_PROFILE" + categoryProfileRequired protos.AitoolsErrorCategory = "PROFILE_REQUIRED" + categoryPermissionDenied protos.AitoolsErrorCategory = "MCP_PERMISSION_DENIED" + categoryLocationNotFound protos.AitoolsErrorCategory = "MCP_LOCATION_NOT_FOUND" ) // NewMcpCmd returns the `aitools mcp` group. It stays hidden until the whole MCP @@ -99,11 +103,11 @@ func resolveProfile(ctx context.Context, flagValue string) (string, error) { } switch len(profiles) { case 0: - return "", fmt.Errorf("no workspace profiles in %s. Run: databricks auth login --host --profile ", filepath.ToSlash(path)) + return "", failure(categoryNoProfile, nil, "no workspace profiles in %s. Run: databricks auth login --host --profile ", filepath.ToSlash(path)) case 1: return profiles[0].Name, nil } - return "", fmt.Errorf("several workspace profiles exist; pass --profile. Found: %s", strings.Join(profiles.Names(), ", ")) + return "", failure(categoryProfileRequired, nil, "several workspace profiles exist; pass --profile. Found: %s", strings.Join(profiles.Names(), ", ")) } // profileFlag returns the root --profile value, or "" when it is not set. diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index f70068d6e99..f02d43303e0 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -4,9 +4,7 @@ import ( "context" "errors" "fmt" - "io/fs" "net/http" - "os" "path/filepath" "slices" "strings" @@ -15,6 +13,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/installer" "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/aitools/mcp/agentconfig" "github.com/databricks/cli/libs/aitools/mcpconfig" @@ -26,6 +25,7 @@ import ( "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" ) @@ -38,9 +38,6 @@ const ( statusFailed = "failed" ) -// cursorAgent is the registry name of Cursor, which needs a server approved before use. -const cursorAgent = "cursor" - type mcpAddFlags struct { names string location string @@ -101,6 +98,20 @@ type addRequest struct { configFile string // env is the entry env: DATABRICKS_CONFIG_FILE when it was set. env map[string]string + // refused holds the agents named with --agents that are not installed, with the reason. Their + // config is not written. + refused map[*agents.Agent]error +} + +// slashPaths rewrites the agent config paths of the document to forward slashes, as the text output +// shows them. The state keeps the native paths. +func (o *addOutput) slashPaths() { + for i := range o.Servers { + for j := range o.Servers[i].Agents { + agent := &o.Servers[i].Agents[j] + agent.Path = filepath.ToSlash(agent.Path) + } + } } // NewMcpAddCmd returns `aitools mcp add`. @@ -130,7 +141,7 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), } out := &addOutput{ - Scope: "global", + Scope: installer.ScopeGlobal, Servers: []addServerOutput{}, ErrorCategory: protos.AitoolsErrorCategoryUnspecified, } @@ -144,6 +155,7 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), out.Error = top.Error() out.ErrorCategory = errorCategory(top) } + out.slashPaths() if err := renderJSON(cmd.OutOrStdout(), out); err != nil { return errors.Join(runErr, err) } @@ -162,17 +174,19 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), return cmd } -// runMcpAdd installs the requested servers and fills out. Every refusal that does not need the -// workspace comes before the first request: the flags, the agents, the binary name, and the binding of -// each service that a recorded entry already holds. A failure of an agent named with --agents is -// returned as an agentErrors; detected agents only report theirs. +// runMcpAdd installs the requested servers and fills out. Every refusal that needs no request comes before +// the first one: the flags, the agents, the binary name, the binding of each service that a recorded entry +// holds, and the --name conflicts. A failure of an agent named with --agents is returned as an agentErrors; +// detected agents only report theirs. func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode bool, out *addOutput) error { names := splitAndTrim(f.names) agentNames := splitAndTrim(f.agents) if err := validateMCPAddFlags(ctx, f, names, agentNames, jsonMode); err != nil { return err } - location := f.location + // Unity Catalog names are case-insensitive and the API returns them in lowercase, so the lowercase + // forms are the ones compared with the state and the agent entries. + location := strings.ToLower(f.location) if location == "" { location = defaultMCPLocation } @@ -189,7 +203,19 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode } binary, absolute := mcpBinaryPath() if !mcpconfig.IsDatabricksBinary(binary) { - return failure(protos.AitoolsErrorCategoryUncategorized, nil, "%s is not the databricks binary; coding agents start MCP servers with databricks or databricks.exe", binary) + return failure(protos.AitoolsErrorCategoryUncategorized, nil, "%s is not the databricks binary; coding agents start MCP servers with databricks or databricks.exe", filepath.ToSlash(binary)) + } + var refused map[*agents.Agent]error + if explicit { + refused = refusedAgents(ctx, targets) + // Nothing can be written when every agent is refused, so the refusals end the run before any request. + if len(refused) == len(targets) { + var errs []error + for _, a := range targets { + errs = append(errs, refused[a]) + } + return errors.Join(errs...) + } } profileName, err := resolveProfile(ctx, profileFlag(cmd)) @@ -197,24 +223,29 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode return err } out.Profile = profileName - if err := requireProfile(ctx, profileName); err != nil { - return err - } state, err := mcp.LoadState(ctx) if err != nil { return err } + configFile, pinned, err := addConfigFile(ctx) + if err != nil { + return err + } for _, name := range fullNames { if err := mcp.CheckBinding(ctx, state, name, profileName); err != nil { return err } } - - configFile, pinned, err := addConfigFile(ctx) - if err != nil { + if err := requireProfile(ctx, profileName); err != nil { return err } + if f.name != "" { + if err := checkNameFlag(ctx, f.name, fullNames[0], profileName, pinned, state, targets); err != nil { + return err + } + } + w, err := profileClient(ctx, profileName, configFile) if err != nil { return err @@ -222,21 +253,18 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode out.Host = w.Config.Host workspaceID, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err) + return describeRequestError(profileName, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } services, err := mcp.Discover(ctx, w, location) if err != nil { - return describeListError(profileName, err) + return describeRequestError(profileName, err) } picked, err := pickServices(location, fullNames, services) if err != nil { return err } - plans, err := planAdds(ctx, f.name, profileName, state, picked, targets) - if err != nil { - return err - } + plans := planAdds(ctx, f.name, profileName, pinned, state, picked, targets) req := addRequest{ jsonMode: jsonMode, @@ -249,6 +277,7 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode force: f.force, binary: binary, configFile: pinned, + refused: refused, } if pinned != "" { req.env = map[string]string{mcpconfig.ConfigFileEnv: pinned} @@ -279,10 +308,10 @@ func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames [ if len(names) != 1 { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service in --names") } - // A name must come out of mcp.Sanitize unchanged apart from case, so that mcp____ - // splits the same way as a generated name. - if mcp.Sanitize(f.name) != strings.ToLower(f.name) { - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %q is not a valid server name: use letters, digits, '-' and '_', with no \"__\" and no leading or trailing '-' or '_'", f.name) + // A name must come out of mcp.Sanitize unchanged, so that mcp____ splits the same way as + // a generated name. + if mcp.Sanitize(f.name) != f.name { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %q is not a valid server name: use lowercase letters, digits, '-' and '_', with no \"__\" and no leading or trailing '-' or '_'", f.name) } } @@ -319,14 +348,15 @@ func validateLocation(location string) error { return nil } -// requireProfile fails when the named profile is not in the Databricks config file. +// requireProfile fails when the named profile is not in the Databricks config file, or has no host. func requireProfile(ctx context.Context, name string) error { profiler := profile.GetProfiler(ctx) profiles, err := profiler.LoadProfiles(ctx, profile.WithName(name)) if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { return err } - if len(profiles) > 0 { + // A profile with no host names no workspace, so it is missing for a server as well. + if len(profiles) > 0 && profiles[0].Host != "" { return nil } path, err := profiler.GetPath(ctx) @@ -372,6 +402,16 @@ func profileClient(ctx context.Context, name, configFile string) (*databricks.Wo return w, nil } +// describeRequestError classifies a failed request to the profile's workspace. A credential the workspace +// rejects is AUTH_FAILED, with the login command for the profile. Other failures are described by +// describeListError. +func describeRequestError(profileName string, err error) error { + if errors.Is(err, apierr.ErrUnauthenticated) { + return mcp.AuthError(profileName, err) + } + return describeListError(profileName, err) +} + // describeListError names the profile that made a failed listing call. The // ListError text already carries the server's message, so it is only prefixed. The cause // keeps the *mcp.ListError for errors.As. @@ -380,9 +420,16 @@ func describeListError(profileName string, err error) error { if !ok { return err } - category := protos.AitoolsErrorCategoryUncategorized - if listErr.Kind == mcp.ListErrRateLimited { + var category protos.AitoolsErrorCategory + switch listErr.Kind { + case mcp.ListErrLocationNotFound: + category = categoryLocationNotFound + case mcp.ListErrPermissionDenied: + category = categoryPermissionDenied + case mcp.ListErrRateLimited: category = protos.AitoolsErrorCategoryRateLimited + default: + category = protos.AitoolsErrorCategoryUncategorized } return failure(category, listErr, "profile %s: %s", profileName, listErr.Error()) } @@ -396,14 +443,14 @@ func targetAgents(ctx context.Context, names []string) ([]*agents.Agent, bool, e } var detected []*agents.Agent for _, a := range agents.MCPAgents() { - installed, err := mcpInstalled(ctx, a) + installed, err := a.InstalledForMCP(ctx) // An error is kept, so the agent reports it when its config is written. if installed || err != nil { detected = append(detected, a) } } if len(detected) == 0 { - return nil, false, fmt.Errorf("no MCP-capable coding agents detected; pass --agents with one of: %s", strings.Join(mcpAgentNames(), ", ")) + return nil, false, failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "no MCP-capable coding agents detected; pass --agents with one of: %s", strings.Join(mcpAgentNames(), ", ")) } return detected, false, nil } @@ -437,15 +484,16 @@ func mcpAgentNames() []string { // serviceNames returns the full name of each service in --names. A leaf name is resolved in location. A // full name must be in location, so its catalog and schema must match it. Any other dotted name is refused, -// since it cannot be either. +// since it cannot be either. The names are returned in lowercase, as the API returns them. func serviceNames(location string, names []string) ([]string, error) { full := make([]string, 0, len(names)) for _, name := range names { + name = strings.ToLower(name) switch parts := strings.Split(name, "."); len(parts) { case 1: full = append(full, location+"."+name) case 3: - if !strings.EqualFold(parts[0]+"."+parts[1], location) { + if parts[0]+"."+parts[1] != location { return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not in %s; pass --location to match its schema", name, location) } full = append(full, name) @@ -477,62 +525,109 @@ func pickServices(location string, names []string, services []mcp.Service) ([]mc } // planAdds chooses the server name of each picked service before anything is written. A service -// already installed for this profile keeps its name. A new name avoids names of other state -// entries and of unmanaged entries in the target agents. The entries have been checked against the -// profile by mcp.CheckBinding. -func planAdds(ctx context.Context, nameFlag, profileName string, state *mcp.State, picked []mcp.Service, targets []*agents.Agent) ([]addPlan, error) { +// already installed for this profile keeps its name. A new name avoids the names of other state +// entries and of every agent entry that is not this binding's, so that a name this CLI wrote for another +// profile is never overwritten. The --name conflicts are checked by checkNameFlag. +func planAdds(ctx context.Context, nameFlag, profileName, pinned string, state *mcp.State, picked []mcp.Service, targets []*agents.Agent) []addPlan { taken := map[string]bool{} for i := range state.Servers { taken[state.Servers[i].Name] = true } - for _, a := range targets { - for name := range unmanagedNames(ctx, a) { - taken[name] = true - } - } plans := make([]addPlan, 0, len(picked)) for _, svc := range picked { entry := stateEntry(state, svc.FullName, profileName) var name string switch { - case entry != nil && nameFlag != "" && nameFlag != entry.Name: - return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is already installed on profile %s as %s; remove it before using --name %s", svc.FullName, profileName, entry.Name, nameFlag) case entry != nil: name = entry.Name case nameFlag != "": - if slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { return e.Name == nameFlag }) { - return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another installed server; pick a different name", nameFlag) - } name = nameFlag default: - name = mcp.UniqueName(mcp.ServerName(svc.FullName, profileName), func(n string) bool { return taken[n] }) + reserved := map[string]bool{} + for _, a := range targets { + for n := range reservedNames(ctx, a, svc.FullName, profileName, pinned) { + reserved[n] = true + } + } + name = mcp.UniqueName(mcp.ServerName(svc.FullName, profileName), func(n string) bool { return taken[n] || reserved[n] }) } taken[name] = true plans = append(plans, addPlan{service: svc, name: name, entry: entry}) } - return plans, nil + return plans } -// unmanagedNames returns the server names in the agent's config that this CLI did not write. A -// config that cannot be read has none here; Upsert reports that failure for the agent. -func unmanagedNames(ctx context.Context, a *agents.Agent) map[string]bool { +// checkNameFlag refuses a --name that another server holds: the state gives the name to another service, +// or a target agent holds it for another binding this CLI wrote. The service's own entry keeps its name, so +// --name must match it. An unmanaged entry is left to the agent's write, which refuses it unless --force is +// set. It reads files only, so it runs before any request. +func checkNameFlag(ctx context.Context, nameFlag, fullName, profileName, pinned string, state *mcp.State, targets []*agents.Agent) error { + entry := stateEntry(state, fullName, profileName) + switch { + case entry != nil && nameFlag != entry.Name: + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is already installed on profile %s as %s; remove it before using --name %s", fullName, profileName, entry.Name, nameFlag) + case entry == nil && slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { return e.Name == nameFlag }): + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another installed server; pick a different name", nameFlag) + } + for _, a := range targets { + // An unreadable config reserves nothing here. Its write reports the failure for the agent. + entries, err := agentconfig.Read(ctx, a) + if err != nil { + continue + } + if other, ok := entries[nameFlag]; ok && mcpconfig.IsDatabricksEntry(other) && !ownedBy(other, fullName, profileName, pinned) { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another server in %s; pick a different name", nameFlag, a.Name) + } + } + return nil +} + +// reservedNames returns the server names in the agent's config that a new install of service for profileName +// must not take: every entry except this binding's own. An unreadable config reserves nothing here, and its +// write reports the failure for the agent. +func reservedNames(ctx context.Context, a *agents.Agent, service, profileName, pinned string) map[string]bool { entries, err := agentconfig.Read(ctx, a) if err != nil { return nil } names := make(map[string]bool, len(entries)) for name, entry := range entries { - if !mcpconfig.IsDatabricksEntry(entry) { + if !ownedBy(entry, service, profileName, pinned) { names[name] = true } } return names } +// ownedBy reports whether entry is a Databricks entry that serves service for profileName through the config +// file pinned ("" for the default file). Only such an entry may be reused by an install. +func ownedBy(entry map[string]any, service, profileName, pinned string) bool { + argv, ok := mcpconfig.Argv(entry) + if !ok || !mcpconfig.IsDatabricksEntry(entry) { + return false + } + if !strings.EqualFold(argValue(argv, "--service"), service) || argValue(argv, "--profile") != profileName { + return false + } + vars, _ := entry["env"].(map[string]any) + file, _ := vars[mcpconfig.ConfigFileEnv].(string) + return file == pinned +} + +// argValue returns the value that follows flag in argv, or "" when flag is absent. +func argValue(argv []string, flag string) string { + for i := 0; i+1 < len(argv); i++ { + if argv[i] == flag { + return argv[i+1] + } + } + return "" +} + func stateEntry(state *mcp.State, service, profileName string) *mcp.Entry { i := slices.IndexFunc(state.Servers, func(e mcp.Entry) bool { - return e.Service == service && e.Profile == profileName + return strings.EqualFold(e.Service, service) && e.Profile == profileName }) if i < 0 { return nil @@ -541,7 +636,7 @@ func stateEntry(state *mcp.State, service, profileName string) *mcp.Entry { } // installServer writes one server into every target agent. Each agent's result is reported; one -// agent's failure does not stop the others. +// agent's failure does not stop the others. An agent in req.refused is not written. func installServer(ctx context.Context, req addRequest, plan addPlan) addServerOutput { out := addServerOutput{ Name: plan.name, @@ -552,10 +647,7 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO for _, a := range req.agents { var path string var status agentconfig.Status - var err error - if req.explicit { - err = requireInstalled(ctx, a) - } + err := req.refused[a] if err == nil { args := []string{"aitools", "mcp", "serve", "--profile", req.profile, "--service", plan.service.FullName, "--agent", a.Name} path, status, err = agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) @@ -570,78 +662,39 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO result.err = err case status == agentconfig.StatusUnchanged: result.Status = statusReasserted - result.Path = filepath.ToSlash(path) + result.Path = path default: result.Status = statusInstalled - result.Path = filepath.ToSlash(path) + result.Path = path } out.Agents = append(out.Agents, result) } return out } -// requireInstalled fails when an agent named with --agents has neither its MCP config file nor the -// directory that holds it. Writing the config would create that directory, so the agent is refused. +// refusedAgents returns the agents named with --agents that are not installed, with the reason for each. +// It runs once, before any request, so each service reports the same refusal without another check. +func refusedAgents(ctx context.Context, targets []*agents.Agent) map[*agents.Agent]error { + refused := map[*agents.Agent]error{} + for _, a := range targets { + if err := requireInstalled(ctx, a); err != nil { + refused[a] = err + } + } + return refused +} + +// requireInstalled fails when an agent named with --agents is not installed. Writing its config would create +// the agent's directory, so the agent is refused instead. func requireInstalled(ctx context.Context, a *agents.Agent) error { - installed, err := mcpInstalled(ctx, a) + installed, err := a.InstalledForMCP(ctx) if err != nil { return err } if installed { return nil } - dir, err := mcpDir(ctx, a) - if err != nil { - return err - } - return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%s is not installed (no %s); install it first or leave it out of --agents", a.DisplayName, filepath.ToSlash(dir)) -} - -// mcpInstalled reports whether the agent's MCP config file or the directory that holds it exists. -func mcpInstalled(ctx context.Context, a *agents.Agent) (bool, error) { - path, err := a.MCP.Path(ctx) - if err != nil { - return false, err - } - exists, err := pathExists(path) - if err != nil || exists { - return exists, err - } - dir, err := mcpDir(ctx, a) - if err != nil { - return false, err - } - return pathExists(dir) -} - -// mcpDir returns the directory that holds the agent's MCP config file. Claude Code keeps that file in the -// home directory, which always exists, so its config directory stands for it instead. -func mcpDir(ctx context.Context, a *agents.Agent) (string, error) { - path, err := a.MCP.Path(ctx) - if err != nil { - return "", err - } - home, err := env.UserHomeDir(ctx) - if err != nil { - return "", err - } - dir := filepath.Dir(path) - if filepath.Clean(dir) == filepath.Clean(home) { - return a.ConfigDir(ctx) - } - return dir, nil -} - -func pathExists(path string) (bool, error) { - _, err := os.Stat(path) - switch { - case err == nil: - return true, nil - case errors.Is(err, fs.ErrNotExist): - return false, nil - default: - return false, err - } + return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%s is not installed; install it first or leave it out of --agents", a.DisplayName) } // recordInstalls adds each server that reached at least one agent to the state file. The file is @@ -695,26 +748,35 @@ func recordServer(state *mcp.State, req addRequest, plan addPlan, paths map[stri entry.Agents = map[string]mcp.AgentRecord{} } for agent, path := range paths { - entry.Agents[agent] = mcp.AgentRecord{Path: path, Scope: "user"} + entry.Agents[agent] = mcp.AgentRecord{Path: path, Scope: agentScopeUser} } } -// agentFailureError returns the agents' failures as an agentErrors. They fail the command when an agent was -// named with --agents, or when no agent was installed at all. Otherwise detected agents only warn, as in -// install, and the results already show them. +// agentFailureError returns the agents' failures as an agentErrors. The command fails when an agent was named +// with --agents, or when a requested service reached no agent at all. Otherwise detected agents only warn, as +// in install, and the results already show them. An agent's failure is reported once, although it repeats for +// each service. func agentFailureError(req addRequest, servers []addServerOutput) error { var errs []error - installed := false + reported := map[string]bool{} + fail := req.explicit for _, server := range servers { + reached := false for _, a := range server.Agents { - if a.err != nil { + if a.err == nil { + reached = true + continue + } + if !reported[a.Name] { + reported[a.Name] = true errs = append(errs, a.err) - } else { - installed = true } } + if !reached { + fail = true + } } - if len(errs) == 0 || (!req.explicit && installed) { + if len(errs) == 0 || !fail { return nil } return &agentErrors{err: errors.Join(errs...)} @@ -751,7 +813,7 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut if a.Status != statusFailed { succeeded = true } - if a.Name == cursorAgent && a.Status == statusInstalled { + if a.Name == agents.NameCursor && a.Status == statusInstalled { cursor = true } } @@ -765,13 +827,14 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut } func agentResultLine(ctx context.Context, a addAgentOutput) string { + path := filepath.ToSlash(a.Path) switch a.Status { case statusFailed: return cmdio.Yellow(ctx, fmt.Sprintf("%-12s failed: %s", a.Name, a.Message)) case statusReasserted: - return fmt.Sprintf("%-12s already installed, re-asserted %s", a.Name, a.Path) + return fmt.Sprintf("%-12s already installed, re-asserted %s", a.Name, path) default: - return fmt.Sprintf("%-12s installed %s", a.Name, a.Path) + return fmt.Sprintf("%-12s installed %s", a.Name, path) } } diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index c0139dfb658..8a50e4a7fd7 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -11,6 +11,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "runtime" "strings" "testing" @@ -54,22 +55,25 @@ func TestValidateMCPAddFlags(t *testing.T) { }, { name: "name must be a valid server name", flags: mcpAddFlags{name: "a.b"}, names: []string{"github"}, - want: `--name "a.b" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + want: `--name "a.b" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, }, { name: "name must not contain a double underscore", flags: mcpAddFlags{name: "a__b"}, names: []string{"github"}, - want: `--name "a__b" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + want: `--name "a__b" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, }, { name: "name must not start with an underscore", flags: mcpAddFlags{name: "_a"}, names: []string{"github"}, - want: `--name "_a" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + want: `--name "_a" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, }, { name: "name must not end with a dash", flags: mcpAddFlags{name: "a-"}, names: []string{"github"}, - want: `--name "a-" is not a valid server name: use letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + want: `--name "a-" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, }, {name: "valid name", flags: mcpAddFlags{name: "my_github-1"}, names: []string{"github"}}, - {name: "valid name in mixed case", flags: mcpAddFlags{name: "GitHub"}, names: []string{"github"}}, + { + name: "name must be lowercase", flags: mcpAddFlags{name: "GitHub"}, names: []string{"github"}, + want: `--name "GitHub" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -105,7 +109,7 @@ func testServices() []mcp.Service { func TestServiceNames(t *testing.T) { got, err := serviceNames("system.ai", []string{"github", "System.AI.GitHub", "system.ai.genie_one_mcp"}) require.NoError(t, err) - assert.Equal(t, []string{"system.ai.github", "System.AI.GitHub", "system.ai.genie_one_mcp"}, got) + assert.Equal(t, []string{"system.ai.github", "system.ai.github", "system.ai.genie_one_mcp"}, got) _, err = serviceNames("system.ai", []string{"main.tools.crm"}) require.Error(t, err) @@ -135,7 +139,7 @@ func TestPickServices(t *testing.T) { _, err = pickServices("system.ai", []string{"system.ai.foo"}, services) require.Error(t, err) assert.Equal(t, "system.ai.foo is not an MCP service in system.ai. Available: system.ai.genie_one_mcp, system.ai.github", err.Error()) - assert.Equal(t, protos.AitoolsErrorCategory("MCP_SERVICE_NOT_FOUND"), errorCategory(err)) + assert.Equal(t, categoryServiceNotFound, errorCategory(err)) } func TestResolveMCPAgents(t *testing.T) { @@ -161,7 +165,7 @@ func TestDescribeListErrorNamesProfileAndKeepsCause(t *testing.T) { err := describeListError("logfood", listErr) assert.Equal(t, "profile logfood: "+listErr.Error(), err.Error()) - assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) + assert.Equal(t, categoryPermissionDenied, errorCategory(err)) got, ok := errors.AsType[*mcp.ListError](err) require.True(t, ok) @@ -171,19 +175,25 @@ func TestDescribeListErrorNamesProfileAndKeepsCause(t *testing.T) { rateLimited := describeListError("logfood", &mcp.ListError{Kind: mcp.ListErrRateLimited, Location: "system.ai", Err: cause}) assert.Equal(t, protos.AitoolsErrorCategoryRateLimited, errorCategory(rateLimited)) + notFound := describeListError("logfood", &mcp.ListError{Kind: mcp.ListErrLocationNotFound, Location: "main.tools", Err: cause}) + assert.Equal(t, categoryLocationNotFound, errorCategory(notFound)) + plain := errors.New("network down") assert.Equal(t, plain, describeListError("logfood", plain)) } // withHome points the home directory at a temp dir, so agent config paths are inside it. The -// agent home overrides are cleared, so a developer's own settings do not reach the test. +// agent home overrides are cleared, and PATH is empty, so a developer's own agents do not reach the test. +// Claude Code is installed in the home, so a run that names it with --agents is not refused. func withHome(t *testing.T) string { t.Helper() home := t.TempDir() t.Setenv(env.HomeEnvVar(), home) + t.Setenv("PATH", t.TempDir()) for _, name := range agentHomeEnv { t.Setenv(name, "") } + require.NoError(t, os.MkdirAll(filepath.Join(home, ".claude"), 0o700)) return home } @@ -250,8 +260,9 @@ func runAdd(t *testing.T, args ...string) error { return err } -// An agent named with --agents that is not installed is refused: its config is not written and its -// directory is not created. The agent that is installed still gets the server. +// TestInstallServerRefusesUndetectedExplicitAgent checks that an agent named with --agents that is not +// installed is refused: its config is not written and its directory is not created. The agent that is +// installed still gets the server. func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { ctx := t.Context() home := withHome(t) @@ -262,6 +273,7 @@ func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { agents: []*agents.Agent{agents.ByName("codex"), agents.ByName("claude-code")}, binary: "databricks", } + req.refused = refusedAgents(ctx, req.agents) plan := addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"} out := installServer(ctx, req, plan) @@ -270,7 +282,7 @@ func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { codex := out.Agents[0] assert.Equal(t, statusFailed, codex.Status) assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, codex.ErrorCategory) - assert.Equal(t, "Codex CLI is not installed (no "+filepath.ToSlash(filepath.Join(home, ".codex"))+"); install it first or leave it out of --agents", codex.Message) + assert.Equal(t, "Codex CLI is not installed; install it first or leave it out of --agents", codex.Message) assert.NoDirExists(t, filepath.Join(home, ".codex")) assert.Equal(t, statusInstalled, out.Agents[1].Status) @@ -284,8 +296,7 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("new service gets the generated name", func(t *testing.T) { withHome(t) - plans, err := planAdds(ctx, "", "logfood", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) - require.NoError(t, err) + plans := planAdds(ctx, "", "logfood", "", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) require.Len(t, plans, 1) assert.Equal(t, "genie_one_mcp-logfood", plans[0].name) assert.Nil(t, plans[0].entry) @@ -294,8 +305,7 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("existing install keeps its name", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "custom-name", Service: "system.ai.github", Profile: "logfood", Host: host}}} - plans, err := planAdds(ctx, "", "logfood", state, testServices()[1:], []*agents.Agent{claude}) - require.NoError(t, err) + plans := planAdds(ctx, "", "logfood", "", state, testServices()[1:], []*agents.Agent{claude}) assert.Equal(t, "custom-name", plans[0].name) assert.NotNil(t, plans[0].entry) }) @@ -303,8 +313,7 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("name used by another state entry gets a suffix", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "genie_one_mcp-logfood", Service: "main.tools.other", Profile: "logfood", Host: host}}} - plans, err := planAdds(ctx, "", "logfood", state, testServices()[:1], []*agents.Agent{claude}) - require.NoError(t, err) + plans := planAdds(ctx, "", "logfood", "", state, testServices()[:1], []*agents.Agent{claude}) assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) }) @@ -312,15 +321,31 @@ func TestPlanAddsNaming(t *testing.T) { home := withHome(t) unmanaged := `{"mcpServers":{"genie_one_mcp-logfood":{"type":"stdio","command":"ug","args":["mcp-proxy"]}}}` require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(unmanaged), 0o600)) - plans, err := planAdds(ctx, "", "logfood", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) - require.NoError(t, err) + plans := planAdds(ctx, "", "logfood", "", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) }) + t.Run("managed entry of another profile keeps its name when state is missing", func(t *testing.T) { + home := withHome(t) + // dev.foo and dev-foo sanitize to the same server name, so the entry for dev.foo is not dev-foo's. + other := `{"mcpServers":{"github-dev-foo":{"type":"stdio","command":"databricks","args":["aitools","mcp","serve","--profile","dev.foo","--service","system.ai.github","--agent","claude-code"]}}}` + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(other), 0o600)) + plans := planAdds(ctx, "", "dev-foo", "", &mcp.State{}, testServices()[1:], []*agents.Agent{claude}) + assert.Equal(t, "github-dev-foo-2", plans[0].name) + }) + + t.Run("managed entry of the same binding is reused when state is missing", func(t *testing.T) { + home := withHome(t) + same := `{"mcpServers":{"github-dev-foo":{"type":"stdio","command":"databricks","args":["aitools","mcp","serve","--profile","dev.foo","--service","system.ai.github","--agent","claude-code"]}}}` + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(same), 0o600)) + plans := planAdds(ctx, "", "dev.foo", "", &mcp.State{}, testServices()[1:], []*agents.Agent{claude}) + assert.Equal(t, "github-dev-foo", plans[0].name) + }) + t.Run("name flag for an installed service must match", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host}}} - _, err := planAdds(ctx, "other", "logfood", state, testServices()[1:], []*agents.Agent{claude}) + err := checkNameFlag(ctx, "other", "system.ai.github", "logfood", "", state, []*agents.Agent{claude}) require.Error(t, err) assert.Equal(t, "system.ai.github is already installed on profile logfood as github-logfood; remove it before using --name other", err.Error()) }) @@ -328,7 +353,7 @@ func TestPlanAddsNaming(t *testing.T) { t.Run("name flag already used by another service is refused", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "taken", Service: "main.tools.other", Profile: "logfood", Host: host}}} - _, err := planAdds(ctx, "taken", "logfood", state, testServices()[:1], []*agents.Agent{claude}) + err := checkNameFlag(ctx, "taken", "system.ai.genie_one_mcp", "logfood", "", state, []*agents.Agent{claude}) require.Error(t, err) assert.Equal(t, "--name taken is used by another installed server; pick a different name", err.Error()) }) @@ -399,7 +424,7 @@ func TestMcpAddJSONReportsMissingFlagsAsDocument(t *testing.T) { assert.Equal(t, "global", out.Scope) } -// A profile re-pointed at another host is refused before any request reaches that host. +// TestMcpAddRefusesRepointedHostBeforeAnyRequest a profile re-pointed at another host is refused before any request reaches that host. func TestMcpAddRefusesRepointedHostBeforeAnyRequest(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -410,7 +435,7 @@ func TestMcpAddRefusesRepointedHostBeforeAnyRequest(t *testing.T) { err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") require.Error(t, err) - assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err)) assert.Contains(t, err.Error(), "was set up for https://adb-123.test, but profile \"logfood\" now points at "+server.URL) assert.NoFileExists(t, filepath.Join(home, ".claude.json")) } @@ -425,11 +450,11 @@ func TestMcpAddRefusesWorkspaceChangeOnSameHostBeforeAnyRequest(t *testing.T) { err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") require.Error(t, err) - assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err)) assert.Contains(t, err.Error(), "was set up for workspace 111, but profile \"logfood\" now points at workspace 222") } -// A second service on a profile whose first entry is pinned to another file is refused as a whole: +// TestMcpAddRefusesPartialReaddWithOtherConfigFile a second service on a profile whose first entry is pinned to another file is refused as a whole: // no agent file is written and the state keeps its one entry. func TestMcpAddRefusesPartialReaddWithOtherConfigFile(t *testing.T) { home := withHome(t) @@ -443,7 +468,7 @@ func TestMcpAddRefusesPartialReaddWithOtherConfigFile(t *testing.T) { err := runAdd(t, "--profile", "logfood", "--names", "genie_one_mcp,github", "--agents", "claude-code") require.Error(t, err) - assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_HOST_CHANGED"), errorCategory(err)) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err)) assert.Contains(t, err.Error(), "was set up with config file") assert.NoFileExists(t, filepath.Join(home, ".claude.json")) @@ -453,7 +478,7 @@ func TestMcpAddRefusesPartialReaddWithOtherConfigFile(t *testing.T) { assert.Equal(t, "github-logfood", state.Servers[0].Name) } -// The profile's own credentials are used. The environment's token is not sent to a profile without one. +// TestMcpAddNeverUsesEnvTokenForProfileWithoutToken the profile's own credentials are used. The environment's token is not sent to a profile without one. func TestMcpAddNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { home := withHome(t) server := noAPIServer(t) @@ -462,11 +487,11 @@ func TestMcpAddNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") require.Error(t, err) - assert.Equal(t, protos.AitoolsErrorCategory("AUTH_FAILED"), errorCategory(err)) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(err)) assert.Contains(t, err.Error(), "databricks auth login --profile logfood") } -// A configuration the SDK rejects is not an authentication failure, so a login would not fix it. +// TestMcpAddInvalidConfigIsNotAuthFailed a configuration the SDK rejects is not an authentication failure, so a login would not fix it. func TestMcpAddInvalidConfigIsNotAuthFailed(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -478,7 +503,7 @@ func TestMcpAddInvalidConfigIsNotAuthFailed(t *testing.T) { assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) } -// --agents is checked with the other flags, before the profile is read or the network is used. +// TestMcpAddRejectsUnknownAgentBeforeProfile --agents is checked with the other flags, before the profile is read or the network is used. func TestMcpAddRejectsUnknownAgentBeforeProfile(t *testing.T) { withHome(t) err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "pi") @@ -504,7 +529,7 @@ type failingWriter struct{} func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("disk full") } -// An agent with a config directory is installed for MCP even when Detected would say otherwise. +// TestInstallServerCodexHomeCountsAsInstalled an agent with a config directory is installed for MCP even when Detected would say otherwise. func TestInstallServerCodexHomeCountsAsInstalled(t *testing.T) { home := withHome(t) codexHome := filepath.Join(t.TempDir(), "codex") @@ -520,10 +545,25 @@ func TestInstallServerCodexHomeCountsAsInstalled(t *testing.T) { assert.NoDirExists(t, filepath.Join(home, ".codex")) } -// Gemini CLI is installed once its settings directory exists, whether or not projects.json does. -func TestInstallServerGeminiWithoutProjectsJSON(t *testing.T) { +// TestInstallServerGeminiDirAloneIsRefused gemini CLI's directory is shared with Antigravity, so ~/.gemini alone does not install Gemini CLI. +func TestInstallServerGeminiDirAloneIsRefused(t *testing.T) { + home := withHome(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o700)) + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("gemini")}, binary: "databricks"} + req.refused = refusedAgents(t.Context(), req.agents) + out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) + require.Len(t, out.Agents, 1) + assert.Equal(t, statusFailed, out.Agents[0].Status) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, out.Agents[0].ErrorCategory) + assert.NoFileExists(t, filepath.Join(home, ".gemini", "settings.json")) +} + +// TestInstallServerGeminiWithProjectsJSON gemini CLI is installed once it has written projects.json, as the registry detects it. +func TestInstallServerGeminiWithProjectsJSON(t *testing.T) { home := withHome(t) require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(home, ".gemini", "projects.json"), []byte("{}"), 0o600)) req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("gemini")}, binary: "databricks"} out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) @@ -532,10 +572,10 @@ func TestInstallServerGeminiWithoutProjectsJSON(t *testing.T) { assert.FileExists(t, filepath.Join(home, ".gemini", "settings.json")) } -// Antigravity's MCP file is in ~/.gemini/config, so that directory is what says it is installed. -func TestInstallServerAntigravityUsesItsMCPDirectory(t *testing.T) { +// TestInstallServerAntigravityFromItsOwnDir antigravity is installed from its own directory, and its MCP file is written under ~/.gemini/config. +func TestInstallServerAntigravityFromItsOwnDir(t *testing.T) { home := withHome(t) - require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "config"), 0o700)) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o700)) req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("antigravity")}, binary: "databricks"} out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) @@ -544,7 +584,25 @@ func TestInstallServerAntigravityUsesItsMCPDirectory(t *testing.T) { assert.FileExists(t, filepath.Join(home, ".gemini", "config", "mcp_config.json")) } -// A second install of the same entry reports "re-asserted" from the agent file, even when the state +// TestInstallServerCLIOnPathCountsAsInstalled a CLI found only on PATH counts as installed, so its MCP config is written. +func TestInstallServerCLIOnPathCountsAsInstalled(t *testing.T) { + home := withHome(t) + dir := t.TempDir() + name := "codex" + if runtime.GOOS == "windows" { + name += ".exe" + } + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"), 0o755)) + t.Setenv("PATH", dir) + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("codex")}, binary: "databricks"} + out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) + require.Len(t, out.Agents, 1) + assert.Equal(t, statusInstalled, out.Agents[0].Status) + assert.FileExists(t, filepath.Join(home, ".codex", "config.toml")) +} + +// TestInstallServerReassertsFromAgentFile a second install of the same entry reports "re-asserted" from the agent file, even when the state // that recorded it is gone. func TestInstallServerReassertsFromAgentFile(t *testing.T) { home := withHome(t) @@ -562,19 +620,25 @@ func TestInstallServerReassertsFromAgentFile(t *testing.T) { assert.Equal(t, statusReasserted, second.Agents[0].Status) } -func TestAgentFailureErrorWhenNothingInstalled(t *testing.T) { - ok := addServerOutput{Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled}}} - failed := addServerOutput{Agents: []addAgentOutput{{Name: "codex", Status: statusFailed, err: errors.New("bad config")}}} +// TestAgentFailureErrorWhenAServiceReachedNoAgent a service that reached no agent fails the command, even when another service was installed. +func TestAgentFailureErrorWhenAServiceReachedNoAgent(t *testing.T) { + installed := addServerOutput{Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled}}} + mixed := addServerOutput{Agents: []addAgentOutput{ + {Name: "claude-code", Status: statusInstalled}, + {Name: "codex", Status: statusFailed, err: errors.New("bad config")}, + }} + failedAll := addServerOutput{Agents: []addAgentOutput{{Name: "codex", Status: statusFailed, err: errors.New("bad config")}}} - // Detected agents only warn, unless none of them was installed. - assert.NoError(t, agentFailureError(addRequest{}, []addServerOutput{ok, failed})) - err := agentFailureError(addRequest{}, []addServerOutput{failed}) + // Detected agents only warn while every service reached one of them. + assert.NoError(t, agentFailureError(addRequest{}, []addServerOutput{installed, mixed})) + + err := agentFailureError(addRequest{}, []addServerOutput{installed, failedAll}) require.Error(t, err) assert.ErrorContains(t, err, "bad config") // Agents named with --agents fail the command on any failure. - assert.Error(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{ok, failed})) - assert.NoError(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{ok})) + assert.Error(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{installed, mixed})) + assert.NoError(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{installed})) } func TestRequireProfileNamesProfileAndPath(t *testing.T) { @@ -584,13 +648,136 @@ func TestRequireProfileNamesProfileAndPath(t *testing.T) { err := requireProfile(ctx, "logfood") require.Error(t, err) assert.Equal(t, "profile logfood not found in "+filepath.ToSlash(path), err.Error()) - assert.Equal(t, protos.AitoolsErrorCategory("PROFILE_NOT_FOUND"), errorCategory(err)) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) } func TestErrorCategoryMapsTypedFailures(t *testing.T) { - assert.Equal(t, protos.AitoolsErrorCategory("MCP_NAME_CONFLICT"), + assert.Equal(t, categoryNameConflict, errorCategory(fmt.Errorf("claude-code: server %q: %w", "github", agentconfig.ErrNotOwned))) assert.Equal(t, protos.AitoolsErrorCategoryTimeout, errorCategory(fmt.Errorf("list: %w", context.DeadlineExceeded))) assert.Equal(t, protos.AitoolsErrorCategoryPermissionDenied, errorCategory(&fs.PathError{Op: "open", Path: "x", Err: fs.ErrPermission})) - assert.Equal(t, protos.AitoolsErrorCategory("AUTH_FAILED"), errorCategory(mcp.AuthError("logfood", errors.New("no token")))) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(mcp.AuthError("logfood", errors.New("no token")))) +} + +// TestMcpAddRefusesMixedCaseNamesBeforeAnyRequest mixed-case service and location names bind to the same entry as their lowercase forms, so a profile +// re-pointed at another host is refused before any request, whatever the case typed. +func TestMcpAddRefusesMixedCaseNamesBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test", + }}})) + + err := runAdd(t, "--profile", "logfood", "--location", "System.AI", "--names", "GitHub", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err)) + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// TestMcpAddRefusesNameFlagConflictBeforeAnyRequest a --name that the state gives to another server is refused before any request. +func TestMcpAddRefusesNameFlagConflictBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "taken", Service: "main.tools.other", Profile: "logfood", Host: server.URL, + }}})) + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--name", "taken", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, "--name taken is used by another installed server; pick a different name", err.Error()) +} + +// TestMcpAddRefusesNameFlagHeldByOtherBindingBeforeAnyRequest a --name that an agent file gives to another profile's managed server is refused before any request. +func TestMcpAddRefusesNameFlagHeldByOtherBindingBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[dev-foo]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-dev-foo\n") + other := `{"mcpServers":{"mine":{"type":"stdio","command":"databricks","args":["aitools","mcp","serve","--profile","dev.foo","--service","system.ai.github","--agent","claude-code"]}}}` + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(other), 0o600)) + + err := runAdd(t, "--profile", "dev-foo", "--names", "github", "--name", "mine", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + assert.Contains(t, err.Error(), "--name mine is used by another server") +} + +// TestMcpAddRefusesUninstalledAgentBeforeAnyRequest an agent named with --agents that is not installed is refused before any request, once, not per service. +func TestMcpAddRefusesUninstalledAgentBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + err := runAdd(t, "--profile", "logfood", "--names", "github,genie_one_mcp", "--agents", "codex") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(err)) + assert.Equal(t, "Codex CLI is not installed; install it first or leave it out of --agents", err.Error()) + assert.NoDirExists(t, filepath.Join(home, ".codex")) +} + +// TestMcpAddMissingProfileWithEntryGetsRecreateHint a profile that is gone while a server is still bound to it gets the recreate hint, not the generic message. +func TestMcpAddMissingProfileWithEntryGetsRecreateHint(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[other]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-other\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test", + }}})) + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + assert.Contains(t, err.Error(), "MCP server github-logfood is bound to Databricks profile \"logfood\", which no longer exists in ") + assert.Contains(t, err.Error(), "databricks auth login --host https://adb-123.test --profile logfood") +} + +// TestMcpAddEmptyHostIsProfileNotFound a profile whose host is empty has no workspace, so it is reported as missing before any request. +func TestMcpAddEmptyHostIsProfileNotFound(t *testing.T) { + home := withHome(t) + writeHomeConfig(t, home, "[logfood]\nhost =\nauth_type = pat\ntoken = dapi-logfood\n") + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + assert.Contains(t, err.Error(), "profile logfood not found in ") +} + +// TestMcpAddRejectedTokenIsAuthFailed a credential the workspace rejects on the first real request is AUTH_FAILED, with the login command. +func TestMcpAddRejectedTokenIsAuthFailed(t *testing.T) { + home := withHome(t) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`)) + })) + t.Cleanup(server.Close) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-expired\n") + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(err)) + assert.Contains(t, err.Error(), "Run: databricks auth login --profile logfood") + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// TestMcpAddRefusesBinaryNotNamedDatabricks the binary must be databricks: an argv[0] with another name, not on PATH, is refused, not replaced. +func TestMcpAddRefusesBinaryNotNamedDatabricks(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + saved := os.Args[0] + os.Args[0] = "dbx-not-on-path" + t.Cleanup(func() { os.Args[0] = saved }) + + _, err := newAddTestCmd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) + assert.Contains(t, err.Error(), "dbx-not-on-path is not the databricks binary") + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) } diff --git a/cmd/aitools/mcp_binary.go b/cmd/aitools/mcp_binary.go index eb1f1ec0a25..b1d8d926373 100644 --- a/cmd/aitools/mcp_binary.go +++ b/cmd/aitools/mcp_binary.go @@ -15,8 +15,8 @@ func mcpBinaryPath() (string, bool) { // binaryPathFor resolves arg0 without touching the file system beyond lookPath. An absolute arg0 is kept. // A relative arg0 with a directory, such as ./databricks, is made absolute, and its symlinks are not -// resolved. A bare name is looked up on PATH. When none of these gives an absolute path, it returns the -// bare name, which the agent must find on PATH. +// resolved. A bare name is looked up on PATH. When none of these gives an absolute path, it returns arg0 +// as the bare name, which the agent must find on PATH. The caller checks that the name is databricks. func binaryPathFor(arg0 string, lookPath func(string) (string, error)) (string, bool) { if filepath.IsAbs(arg0) { return arg0, true @@ -24,12 +24,12 @@ func binaryPathFor(arg0 string, lookPath func(string) (string, error)) (string, if filepath.Base(arg0) != arg0 { abs, err := filepath.Abs(arg0) if err != nil { - return "databricks", false + return arg0, false } return abs, true } if found, err := lookPath(arg0); err == nil && filepath.IsAbs(found) { return found, true } - return "databricks", false + return arg0, false } diff --git a/cmd/aitools/mcp_binary_test.go b/cmd/aitools/mcp_binary_test.go index e20314c8ffb..ca7238af64d 100644 --- a/cmd/aitools/mcp_binary_test.go +++ b/cmd/aitools/mcp_binary_test.go @@ -63,6 +63,14 @@ func TestBinaryPathFor(t *testing.T) { wantLookup: "databricks", wantPath: "databricks", }, + { + // Only a databricks argv[0] may fall back to the bare name; another name is kept, so the caller refuses it. + name: "other bare name not on PATH is kept", + arg0: "dbx", + lookPath: notFound, + wantLookup: "dbx", + wantPath: "dbx", + }, { name: "relative PATH entry falls back to the bare name", arg0: "databricks", diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index a75d07d2a63..1c38b6b3c59 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -63,6 +63,9 @@ 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, such as CODEX_HOME. + // Empty when the file lives in ConfigDir, or when ConfigDir itself follows the variable. + mcpHomeEnv string // 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, @@ -94,6 +97,29 @@ func (a *Agent) Detected(ctx context.Context) bool { return err == nil && HasDatabricksSkillsIn(skillsDir) } +// InstalledForMCP reports whether the agent is installed for MCP servers. It is installed when Detected +// reports it, when its CLI binary is on PATH, when its MCP config file exists, or when mcpHomeEnv names a +// directory that holds the MCP config file and that directory exists. +func (a *Agent) InstalledForMCP(ctx context.Context) (bool, error) { + if a.Detected(ctx) || a.HasBinary(ctx) { + return true, nil + } + if a.MCP == nil { + return false, nil + } + path, err := a.MCP.Path(ctx) + if err != nil { + return false, err + } + if exists, err := linkExists(path); err != nil || exists { + return exists, err + } + if a.mcpHomeEnv == "" || env.Get(ctx, a.mcpHomeEnv) == "" { + return false, nil + } + return linkExists(filepath.Dir(path)) +} + // SkillsDir returns the full path to the agent's skills directory. func (a *Agent) SkillsDir(ctx context.Context) (string, error) { configDir, err := a.ConfigDir(ctx) @@ -235,6 +261,7 @@ var Registry = []*Agent{ Root: "mcp_servers", Entry: stdioEntry, }, + mcpHomeEnv: "CODEX_HOME", }, { Name: NameOpenCode, @@ -263,6 +290,7 @@ var Registry = []*Agent{ Root: mcpServersKey, Entry: copilotMCPEntry, }, + mcpHomeEnv: "COPILOT_HOME", }, { Name: NameAntigravity, diff --git a/libs/aitools/agents/detect_test.go b/libs/aitools/agents/detect_test.go index dfb7a8c6ec1..a9071549e93 100644 --- a/libs/aitools/agents/detect_test.go +++ b/libs/aitools/agents/detect_test.go @@ -399,3 +399,86 @@ func TestOpenCodeConfigDir(t *testing.T) { assert.Equal(t, filepath.Join(home, ".config", "opencode"), dir) }) } + +// TestInstalledForMCP covers the signals that make an agent installed for MCP servers, with the +// agent's own paths under a temporary home and no agent binary on PATH unless a case says so. +func TestInstalledForMCP(t *testing.T) { + ctx := t.Context() + setup := func(t *testing.T) string { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + for _, name := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA"} { + t.Setenv(name, "") + } + stubLookPath(t) + return home + } + installed := func(t *testing.T, name string) bool { + t.Helper() + a := ByName(name) + require.NotNil(t, a, name) + got, err := a.InstalledForMCP(ctx) + require.NoError(t, err) + return got + } + + t.Run("nothing present is not installed", func(t *testing.T) { + setup(t) + for _, a := range MCPAgents() { + assert.False(t, installed(t, a.Name), a.Name) + } + }) + + t.Run("config dir is installed", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".cursor"), 0o755)) + assert.True(t, installed(t, NameCursor)) + }) + + t.Run("CLI on PATH alone is installed", func(t *testing.T) { + setup(t) + stubLookPath(t, "codex") + assert.True(t, installed(t, NameCodex)) + assert.False(t, installed(t, NameCopilot)) + }) + + t.Run("MCP config file alone is installed", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte("{}"), 0o600)) + assert.True(t, installed(t, NameClaudeCode)) + }) + + t.Run("CODEX_HOME that exists counts for Codex", func(t *testing.T) { + setup(t) + codexHome := t.TempDir() + t.Setenv("CODEX_HOME", codexHome) + assert.True(t, installed(t, NameCodex)) + + t.Setenv("CODEX_HOME", filepath.Join(t.TempDir(), "missing")) + assert.False(t, installed(t, NameCodex)) + }) + + t.Run("COPILOT_HOME that exists counts for Copilot", func(t *testing.T) { + setup(t) + t.Setenv("COPILOT_HOME", t.TempDir()) + assert.True(t, installed(t, NameCopilot)) + }) + + t.Run("Gemini is not installed because Antigravity made ~/.gemini", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) + assert.False(t, installed(t, NameGemini)) + assert.True(t, installed(t, NameAntigravity)) + + require.NoError(t, os.WriteFile(filepath.Join(home, ".gemini", "projects.json"), []byte("{}"), 0o644)) + assert.True(t, installed(t, NameGemini)) + }) + + t.Run("Antigravity is installed from its own directory, without ~/.gemini/config", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) + assert.NoDirExists(t, filepath.Join(home, ".gemini", "config")) + assert.True(t, installed(t, NameAntigravity)) + }) +} diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index ca71ae6264e..55ca4ba7356 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "path/filepath" + "strings" "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/databrickscfg" @@ -227,8 +228,8 @@ func AuthError(profileName string, cause error) error { } // findEntry returns the entry for the service and profile whose config file is configFile, or nil when -// the service and profile have no entry at all. Both sides are compared as resolved paths, so an entry -// with no pinned file matches the default file. +// the service and profile have no entry at all. Service names are compared without case, as Unity Catalog +// names are. Both sides are compared as resolved paths, so an entry with no pinned file matches the default file. // // An entry for the service and profile in another file is an error, not a missing entry: the server was // set up against that file, and binding through this one would switch it without a record. An entry @@ -240,7 +241,7 @@ func findEntry(ctx context.Context, state *State, service, profileName, configFi var otherPath string for i := range state.Servers { entry := &state.Servers[i] - if entry.Service != service || entry.Profile != profileName { + if !strings.EqualFold(entry.Service, service) || entry.Profile != profileName { continue } path, err := ResolveConfigFile(ctx, entry.ConfigFile) From 2ab04c3dc5e4b13b9f5de939cc5b6e7cf64b1157 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 18:18:30 +0000 Subject: [PATCH 09/20] Assert the profile error categories in aitools mcp add Co-authored-by: Isaac --- cmd/aitools/mcp_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go index 6feeff931e0..ed571a8032d 100644 --- a/cmd/aitools/mcp_test.go +++ b/cmd/aitools/mcp_test.go @@ -96,6 +96,7 @@ host = https://b.test _, err := resolveProfile(ctx, "") require.Error(t, err) assert.Equal(t, "several workspace profiles exist; pass --profile. Found: a, b", err.Error()) + assert.Equal(t, categoryProfileRequired, errorCategory(err)) }) t.Run("no workspace profiles is an error naming the file", func(t *testing.T) { @@ -103,6 +104,7 @@ host = https://b.test _, err := resolveProfile(ctx, "") require.Error(t, err) assert.Contains(t, err.Error(), "no workspace profiles in ") + assert.Equal(t, categoryNoProfile, errorCategory(err)) assert.Contains(t, err.Error(), "databricks auth login --host --profile ") }) } From 683cdc3657807ec428a5328007709075322ba236 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 18:44:46 +0000 Subject: [PATCH 10/20] Share the binding check and bound config with aitools mcp add Export the pieces that Bind and mcp add both need, so that the two agree: CheckBinding takes the resolved config file, FindEntry (was findEntry) finds the entry for a service, profile and file, BoundConfig and AuthenticateBound build and authenticate the profile's config, and EntryServes tells whether an agent entry is a Databricks entry for a binding in any layout (env or environment, args or command array). AbsoluteAsWritten keeps a path as written, and SystemAILocation is shared with the command. Bind's behavior is unchanged. Co-authored-by: Isaac --- libs/aitools/mcp/binding.go | 79 +++++++++++++++++--------------- libs/aitools/mcp/binding_test.go | 41 +++++++++++++++-- libs/aitools/mcp/configfile.go | 7 +-- libs/aitools/mcp/entry.go | 39 ++++++++++++++++ libs/aitools/mcp/entry_test.go | 71 ++++++++++++++++++++++++++++ libs/aitools/mcp/known.go | 4 +- libs/aitools/mcp/naming.go | 2 +- libs/aitools/mcpconfig/spec.go | 17 +++++++ 8 files changed, 211 insertions(+), 49 deletions(-) create mode 100644 libs/aitools/mcp/entry.go create mode 100644 libs/aitools/mcp/entry_test.go diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 55ca4ba7356..ff8e9bde181 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -83,76 +83,79 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) return nil, nil, uncategorizedError(err) } ctx = env.Set(ctx, mcpconfig.ConfigFileEnv, configFile) - entry, err := findEntry(ctx, opts.State, opts.Service, opts.Profile, configFile) + entry, err := CheckBinding(ctx, opts.State, opts.Service, opts.Profile, configFile) if err != nil { return nil, nil, err } - name := opts.Service - if entry != nil { - name = entry.Name + if entry == nil { + // Without an entry the profile is still needed, under the service's name. + if _, err := profileFor(opts.Service, opts.Profile, configFile, nil); err != nil { + return nil, nil, err + } } - p, err := profileFor(name, opts.Profile, configFile, entry) + cfg, err := BoundConfig(opts.Profile, configFile) if err != nil { return nil, nil, err } - if entry != nil { - if drift := driftOf(*entry, p); drift != nil { - return nil, nil, driftError(name, opts.Profile, *entry, *drift) - } + if entry == nil { + fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s\n", opts.Service, opts.Profile, cfg.Host) } + if err := AuthenticateBound(ctx, cfg, opts.Profile); err != nil { + return nil, nil, err + } + return cfg, entry, nil +} - // The loaders take the profile's own settings and non-auth environment only. The - // profile loader chain in databrickscfg also fills missing credentials from the - // environment, but the proxy must not send a token meant for another workspace. +// BoundConfig returns the config of profileName in configFile, which is resolved, with the loaders that Bind uses: the +// profile's own settings and the non-auth environment. The profile's loader chain would also fill missing credentials +// from the environment, and a token meant for another workspace must not reach this one. It does not authenticate; see +// AuthenticateBound. +func BoundConfig(profileName, configFile string) (*config.Config, error) { cfg := &config.Config{ - Profile: opts.Profile, + Profile: profileName, ConfigFile: configFile, Loaders: []config.Loader{databrickscfg.ResolveNonAuthFromEnv, config.ConfigFile}, } if err := cfg.EnsureResolved(); err != nil { // Configuration that the SDK rejects is not a credential failure, so a login would not fix it. - return nil, nil, uncategorizedError(err) - } - - if entry == nil { - fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s\n", opts.Service, opts.Profile, cfg.Host) + return nil, uncategorizedError(err) } + return cfg, nil +} +// AuthenticateBound checks that cfg, from BoundConfig for profileName, has a credential. A credential the profile cannot +// provide is AUTH_FAILED, with the login command for the profile. +func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName string) error { // Authenticate only writes headers onto req, which is never sent. The request takes // the command's context, as in cmd/root/auth.go. req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil) if err != nil { - return nil, nil, uncategorizedError(err) + return uncategorizedError(err) } if err := cfg.Authenticate(req); err != nil { - return nil, nil, AuthError(opts.Profile, err) + return AuthError(profileName, err) } - return cfg, entry, nil + return nil } -// CheckBinding returns the error that Bind returns for the state entry of service and profileName -// when the profile no longer matches it: the entry is pinned to another config file, the profile is -// gone or has no host, or its host or workspace changed. It returns nil when there is no entry or the -// entry still matches. It reads the config files only and makes no request, so a caller can refuse -// before it sends a token to the profile's host. -func CheckBinding(ctx context.Context, state *State, service, profileName string) error { - configFile, err := ResolveConfigFile(ctx, env.Get(ctx, mcpconfig.ConfigFileEnv)) - if err != nil { - return uncategorizedError(err) - } - entry, err := findEntry(ctx, state, service, profileName, configFile) +// CheckBinding returns the state entry for service and profileName bound to configFile, the config file the caller +// resolved with ResolveConfigFile, or nil when there is none. With an entry, it returns the error Bind returns when the +// profile no longer matches the entry: the profile is gone or has no host, or its host or workspace changed. It reads the +// config files only and makes no request, so a caller can refuse before it sends a token to the profile's host. +func CheckBinding(ctx context.Context, state *State, service, profileName, configFile string) (*Entry, error) { + entry, err := FindEntry(ctx, state, service, profileName, configFile) if err != nil || entry == nil { - return err + return nil, err } p, err := profileFor(entry.Name, profileName, configFile, entry) if err != nil { - return err + return nil, err } if drift := driftOf(*entry, p); drift != nil { - return driftError(entry.Name, profileName, *entry, *drift) + return nil, driftError(entry.Name, profileName, *entry, *drift) } - return nil + return entry, nil } // profileFor returns the profile profileName in configFile, or the PROFILE_NOT_FOUND error that says it @@ -227,7 +230,7 @@ func AuthError(profileName string, cause error) error { return &BindError{Category: BindAuthFailed, message: message, cause: cause} } -// findEntry returns the entry for the service and profile whose config file is configFile, or nil when +// FindEntry returns the entry for the service and profile whose config file is configFile, or nil when // the service and profile have no entry at all. Service names are compared without case, as Unity Catalog // names are. Both sides are compared as resolved paths, so an entry with no pinned file matches the default file. // @@ -235,7 +238,7 @@ func AuthError(profileName string, cause error) error { // set up against that file, and binding through this one would switch it without a record. An entry // whose file cannot be resolved, such as a "~" path with no home directory, cannot be compared. The // bind fails on it, since it might be the match. -func findEntry(ctx context.Context, state *State, service, profileName, configFile string) (*Entry, error) { +func FindEntry(ctx context.Context, state *State, service, profileName, configFile string) (*Entry, error) { var unresolved error var other *Entry var otherPath string diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index e76fcb22f7f..992ba1a7e65 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -782,6 +782,14 @@ func TestBindRecreateHintQuotesValues(t *testing.T) { } // TestCheckBindingMatchesBind checks that CheckBinding refuses the same drift as Bind, before any request. +// bindingTestCheck runs CheckBinding for the test service and profile, with the config file resolved as the caller resolves it. +func bindingTestCheck(t *testing.T, state *mcp.State) (*mcp.Entry, error) { + t.Helper() + configFile, err := mcp.ResolveConfigFile(t.Context(), env.Get(t.Context(), "DATABRICKS_CONFIG_FILE")) + require.NoError(t, err) + return mcp.CheckBinding(t.Context(), state, bindingTestService, bindingTestProfile, configFile) +} + func TestCheckBindingMatchesBind(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Errorf("unexpected request to the profile's host: %s %s", r.Method, r.URL) @@ -791,15 +799,22 @@ func TestCheckBindingMatchesBind(t *testing.T) { cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, server.URL, bindingTestToken)) state := bindingTestState(cfgPath, bindingTestHost) - err := mcp.CheckBinding(t.Context(), state, bindingTestService, bindingTestProfile) + _, err := bindingTestCheck(t, state) requireBindError(t, err, mcp.BindProfileHostChanged) assert.Equal(t, "MCP server github-logfood was set up for https://adb-123.test, but profile \"logfood\" now points at "+server.URL+". Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) + } func TestCheckBindingPassesWhenEntryMatchesOrIsAbsent(t *testing.T) { cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) - require.NoError(t, mcp.CheckBinding(t.Context(), bindingTestState(cfgPath, bindingTestHost), bindingTestService, bindingTestProfile)) - require.NoError(t, mcp.CheckBinding(t.Context(), &mcp.State{}, bindingTestService, bindingTestProfile)) + entry, err := bindingTestCheck(t, bindingTestState(cfgPath, bindingTestHost)) + require.NoError(t, err) + require.NotNil(t, entry, "a matching entry is returned") + assert.Equal(t, "github-logfood", entry.Name) + + entry, err = bindingTestCheck(t, &mcp.State{}) + require.NoError(t, err) + assert.Nil(t, entry) } func TestCheckBindingRefusesWorkspaceChangeOnSameHost(t *testing.T) { @@ -807,7 +822,7 @@ func TestCheckBindingRefusesWorkspaceChangeOnSameHost(t *testing.T) { state := bindingTestState(cfgPath, bindingTestHost) state.Servers[0].WorkspaceID = "111" - err := mcp.CheckBinding(t.Context(), state, bindingTestService, bindingTestProfile) + _, err := bindingTestCheck(t, state) requireBindError(t, err, mcp.BindProfileHostChanged) } @@ -816,6 +831,22 @@ func TestCheckBindingRefusesEntryPinnedToOtherConfigFile(t *testing.T) { other := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) require.NotEqual(t, cfgPath, other) - err := mcp.CheckBinding(t.Context(), bindingTestState(other, bindingTestHost), bindingTestService, bindingTestProfile) + _, err := bindingTestCheck(t, bindingTestState(other, bindingTestHost)) requireBindError(t, err, mcp.BindProfileHostChanged) } + +// TestCheckBindingMatchesEntryByConfigFileWhenOtherEntryComesFirst returns the entry of the config file in use, even when an +// entry for the same service and profile in another file comes first in the state. +func TestCheckBindingMatchesEntryByConfigFileWhenOtherEntryComesFirst(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + other := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "github-other", Service: bindingTestService, Profile: bindingTestProfile, Host: bindingTestHost, ConfigFile: other}, + {Name: "github-default", Service: bindingTestService, Profile: bindingTestProfile, Host: bindingTestHost, ConfigFile: cfgPath}, + }} + + entry, err := bindingTestCheck(t, state) + require.NoError(t, err) + require.NotNil(t, entry) + assert.Equal(t, "github-default", entry.Name) +} diff --git a/libs/aitools/mcp/configfile.go b/libs/aitools/mcp/configfile.go index 499d722878f..a6c2707f675 100644 --- a/libs/aitools/mcp/configfile.go +++ b/libs/aitools/mcp/configfile.go @@ -26,14 +26,15 @@ func ResolveConfigFile(ctx context.Context, name string) (string, error) { case err == nil: return filepath.Abs(resolved) case errors.Is(err, fs.ErrNotExist): - return absoluteAsWritten(path) + return AbsoluteAsWritten(path) default: return "", err } } -// absoluteAsWritten makes path absolute by joining it to the working directory, without cleaning it. -func absoluteAsWritten(path string) (string, error) { +// AbsoluteAsWritten makes path absolute by joining it to the working directory, without cleaning it, so that a +// ".." after a symlink still resolves through the link's target. +func AbsoluteAsWritten(path string) (string, error) { if filepath.IsAbs(path) { return path, nil } diff --git a/libs/aitools/mcp/entry.go b/libs/aitools/mcp/entry.go new file mode 100644 index 00000000000..5b1161089e7 --- /dev/null +++ b/libs/aitools/mcp/entry.go @@ -0,0 +1,39 @@ +package mcp + +import ( + "context" + "fmt" + "slices" + "strings" + + "github.com/databricks/cli/libs/aitools/mcpconfig" +) + +// EntryServes reports whether entry is a Databricks entry that serves service for profileName through configFile, which +// the caller resolved with ResolveConfigFile. Service names are compared without case. The entry's config file is the one +// it pins in its environment, or the default file when it pins none, resolved as Bind resolves it, so that a pin named +// "~/..." or a path through a symlink matches the file it names. An entry whose pin cannot be resolved is an error, since +// it might be the one that serves the binding. +func EntryServes(ctx context.Context, entry map[string]any, service, profileName, configFile string) (bool, error) { + if !mcpconfig.IsDatabricksEntry(entry) { + return false, nil + } + argv, _ := mcpconfig.Argv(entry) + if !strings.EqualFold(flagValue(argv, mcpconfig.ServiceFlag), service) || flagValue(argv, mcpconfig.ProfileFlag) != profileName { + return false, nil + } + pinned, _ := mcpconfig.EntryEnv(entry)[mcpconfig.ConfigFileEnv].(string) + path, err := ResolveConfigFile(ctx, pinned) + if err != nil { + return false, fmt.Errorf("MCP server entry for %s on profile %s: %w", service, profileName, err) + } + return samePath(path, configFile), nil +} + +// flagValue returns the value that follows flag in argv, or "" when flag is absent. +func flagValue(argv []string, flag string) string { + if i := slices.Index(argv, flag); i >= 0 && i+1 < len(argv) { + return argv[i+1] + } + return "" +} diff --git a/libs/aitools/mcp/entry_test.go b/libs/aitools/mcp/entry_test.go new file mode 100644 index 00000000000..ee855ac9992 --- /dev/null +++ b/libs/aitools/mcp/entry_test.go @@ -0,0 +1,71 @@ +package mcp_test + +import ( + "testing" + + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestEntryServes reports whether an agent entry is a Databricks entry bound to a service, profile and config file, in +// each layout the agents write: env or environment, and an args list or a command array. +func TestEntryServes(t *testing.T) { + ctx := t.Context() + cfgPath := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) + resolved := bindingTestResolved(t, cfgPath) + serve := []string{"aitools", "mcp", "serve", "--profile", bindingTestProfile, "--service", bindingTestService, "--agent", "x"} + + claude := map[string]any{"type": "stdio", "command": "databricks", "args": anyList(serve), "env": map[string]any{"DATABRICKS_CONFIG_FILE": cfgPath}} + codex := map[string]any{"command": "databricks", "args": anyList(serve)} + opencode := map[string]any{"type": "local", "command": anyList(append([]string{"databricks"}, serve...)), "environment": map[string]any{"DATABRICKS_CONFIG_FILE": cfgPath}} + openCodeUnpinned := map[string]any{"type": "local", "command": anyList(append([]string{"databricks"}, serve...))} + + tests := []struct { + name string + entry map[string]any + service string + profile string + configFile string + wantServes bool + }{ + {name: "claude env pinned to the file", entry: claude, service: bindingTestService, profile: bindingTestProfile, configFile: resolved, wantServes: true}, + {name: "opencode environment pinned to the file", entry: opencode, service: bindingTestService, profile: bindingTestProfile, configFile: resolved, wantServes: true}, + {name: "service is case-insensitive", entry: claude, service: "System.AI.GitHub", profile: bindingTestProfile, configFile: resolved, wantServes: true}, + {name: "unpinned matches the default file", entry: codex, service: bindingTestService, profile: bindingTestProfile, configFile: defaultFile(t), wantServes: true}, + {name: "unpinned opencode matches the default file", entry: openCodeUnpinned, service: bindingTestService, profile: bindingTestProfile, configFile: defaultFile(t), wantServes: true}, + {name: "pinned file differs", entry: claude, service: bindingTestService, profile: bindingTestProfile, configFile: defaultFile(t), wantServes: false}, + {name: "unpinned differs from another file", entry: codex, service: bindingTestService, profile: bindingTestProfile, configFile: resolved, wantServes: false}, + {name: "other profile", entry: claude, service: bindingTestService, profile: "other", configFile: resolved, wantServes: false}, + {name: "other service", entry: claude, service: "system.ai.genie_one_mcp", profile: bindingTestProfile, configFile: resolved, wantServes: false}, + { + name: "not a Databricks entry", + entry: map[string]any{"command": "ug", "args": []any{"mcp-proxy"}}, + service: bindingTestService, profile: bindingTestProfile, configFile: resolved, wantServes: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := mcp.EntryServes(ctx, tt.entry, tt.service, tt.profile, tt.configFile) + require.NoError(t, err) + assert.Equal(t, tt.wantServes, got) + }) + } +} + +// anyList converts a string list to the []any that a decoded JSON or TOML entry holds. +func anyList(values []string) []any { + out := make([]any, 0, len(values)) + for _, v := range values { + out = append(out, v) + } + return out +} + +// defaultFile returns the default config file, as the command resolves it. +func defaultFile(t *testing.T) string { + t.Helper() + path, err := mcp.ResolveConfigFile(t.Context(), "") + require.NoError(t, err) + return path +} diff --git a/libs/aitools/mcp/known.go b/libs/aitools/mcp/known.go index 9716b6d3c9b..30ea4d9d524 100644 --- a/libs/aitools/mcp/known.go +++ b/libs/aitools/mcp/known.go @@ -1,7 +1,7 @@ package mcp -// systemAILocation is the schema that ships the Databricks managed MCP services. -const systemAILocation = "system.ai" +// SystemAILocation is the schema that ships the Databricks managed MCP services. +const SystemAILocation = "system.ai" // KnownService is the CLI's own description of a system.ai service, keyed by leaf name. type KnownService struct { diff --git a/libs/aitools/mcp/naming.go b/libs/aitools/mcp/naming.go index 36ae7a1ef0a..4fa910c7daa 100644 --- a/libs/aitools/mcp/naming.go +++ b/libs/aitools/mcp/naming.go @@ -53,7 +53,7 @@ func leafOf(fullName string) string { // inSystemAI reports whether a service full name is in system.ai, in any casing. func inSystemAI(fullName string) bool { - return strings.HasPrefix(strings.ToLower(fullName), systemAILocation+".") + return strings.HasPrefix(strings.ToLower(fullName), SystemAILocation+".") } // UniqueName returns base, or base-2, base-3, ... for the first candidate for which taken diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 07c9a910f33..0ee3eb63dd3 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -71,6 +71,23 @@ func Argv(entry map[string]any) ([]string, bool) { } } +// The flags that `aitools mcp serve` takes, which an entry carries to name its binding. +const ( + ServiceFlag = "--service" + ProfileFlag = "--profile" +) + +// EntryEnv returns the environment variables of an entry: env for most agents, and environment for OpenCode. It returns +// nil when the entry has none. +func EntryEnv(entry map[string]any) map[string]any { + for _, key := range []string{"env", "environment"} { + if vars, ok := entry[key].(map[string]any); ok { + return vars + } + } + return nil +} + // IsDatabricksEntry reports whether entry runs `databricks aitools mcp serve`, // which is how we recognize entries we wrote. func IsDatabricksEntry(entry map[string]any) bool { From de52f5db32abb7b3fc4655f8339ab51f240bb591 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 18:51:39 +0000 Subject: [PATCH 11/20] Fix third-round review findings in aitools mcp add Ownership of agent entries: a server name stored for the profile is checked in each agent before any write, so an entry of another binding is refused instead of overwritten. Entries that this binding already holds are reused under whatever name they have, whether the state knows them or not, and their config file is compared as resolved paths, in the env and environment layouts. Planning reads each agent's entries once and finds the state entry by service, profile and config file. Shared binding check: mcp add uses mcp.CheckBinding, mcp.FindEntry, mcp.BoundConfig and mcp.AuthenticateBound, the same functions Bind uses, with the config file resolved once and the profile read from it. Names and flags: a generated server name that sanitizes to nothing is refused before any request, with a hint to pass --name. --name counts distinct services, so a leaf with its full name is one service. Without --location, full names in one schema give that schema. Errors: an uncategorized binding failure keeps its cause for classification. Cleanup: the --location help and the long help name the new default, the binary path is joined to the working directory without cleaning, the hidden-help check is traced, the mcp add fixture is committed, and the agent home variables and serve flags are constants. Co-authored-by: Isaac --- .../mcp/add-two-agents/home/.claude.json | 9 + .../cmd/aitools/mcp/add-two-agents/script | 13 - acceptance/cmd/aitools/mcp/hidden/output.txt | 8 +- acceptance/cmd/aitools/mcp/hidden/script | 2 +- cmd/aitools/mcp.go | 10 +- cmd/aitools/mcp_add.go | 368 ++++++++----- cmd/aitools/mcp_add_test.go | 501 ++++++++++++++++-- cmd/aitools/mcp_binary.go | 18 +- cmd/aitools/mcp_binary_test.go | 13 +- libs/aitools/agents/agents.go | 23 +- libs/aitools/mcp/binding_test.go | 3 +- 11 files changed, 726 insertions(+), 242 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/add-two-agents/home/.claude.json diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/home/.claude.json b/acceptance/cmd/aitools/mcp/add-two-agents/home/.claude.json new file mode 100644 index 00000000000..4c3ccc383dd --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/home/.claude.json @@ -0,0 +1,9 @@ +{ + "mcpServers": { + "my-server": { + "type": "stdio", + "command": "my-server", + "args": [] + } + } +} diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/script b/acceptance/cmd/aitools/mcp/add-two-agents/script index c71d9bec0f2..592500e4672 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/script +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -8,19 +8,6 @@ export DATABRICKS_CONFIG_FILE=.databrickscfg 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"}' > /dev/null 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"}}}}' > /dev/null -# Claude Code's config already holds an entry of its own, which the add keeps. -cat > home/.claude.json <<'EOF' -{ - "mcpServers": { - "my-server": { - "type": "stdio", - "command": "my-server", - "args": [] - } - } -} -EOF - title "Two services for two agents, as JSON" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex --output json diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index 94e05a08381..f85300f2b4f 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -1,5 +1,7 @@ === The mcp group is hidden from aitools help + +>>> [CLI] aitools --help aitools help does not list mcp === The hidden group still answers help for its subcommands @@ -9,8 +11,8 @@ Install Databricks managed MCP servers into coding agents, bound to a Databricks Each server runs through 'databricks aitools mcp serve' with that profile's credentials. Servers come from --names: full names such as system.ai.github, or leaf names resolved in ---location (default system.ai). Agents come from --agents. Without it, every detected -MCP-capable agent is used. +--location (default: the schema of the full names, else system.ai). Agents come from --agents. +Without it, every detected MCP-capable agent is used. Interactive selection is not available yet: in a terminal, pass both --names and --agents. With --output json, both are required. @@ -24,7 +26,7 @@ Flags: --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) --force Overwrite an existing server with the same name that this CLI did not write -h, --help help for add - --location string Schema to resolve leaf names in, as . (default system.ai) + --location string Schema to resolve leaf names in, as . (default: the schema of the full names in --names, else system.ai) --name string Server name to use instead of the generated one; only with exactly one service --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated diff --git a/acceptance/cmd/aitools/mcp/hidden/script b/acceptance/cmd/aitools/mcp/hidden/script index bec6b90955b..9a8f70f927b 100644 --- a/acceptance/cmd/aitools/mcp/hidden/script +++ b/acceptance/cmd/aitools/mcp/hidden/script @@ -1,5 +1,5 @@ title "The mcp group is hidden from aitools help" -$CLI aitools --help | contains.py '!mcp' > /dev/null +trace $CLI aitools --help | contains.py '!mcp' > /dev/null echo "aitools help does not list mcp" title "The hidden group still answers help for its subcommands" diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index 1c3ca5ef2c3..20c296c7457 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -16,11 +16,8 @@ import ( "github.com/spf13/cobra" ) -// defaultMCPLocation is the schema that ships the Databricks managed MCP services. -const defaultMCPLocation = "system.ai" - -// Error categories that the telemetry enum does not define yet. The JSON output reports them by name, as -// SPEC section 13 names them. The binding categories come from mcp.BindCategory, which has the same names. +// Error categories that the telemetry enum does not define yet. The JSON output reports them by name. The +// binding categories come from mcp.BindCategory, which has the same names. const ( categoryAgentConfigInvalid protos.AitoolsErrorCategory = "AGENT_CONFIG_INVALID" categoryNameConflict protos.AitoolsErrorCategory = "MCP_NAME_CONFLICT" @@ -66,7 +63,8 @@ func errorCategory(err error) protos.AitoolsErrorCategory { if ce, ok := errors.AsType[*cliError](err); ok { return ce.category } - if be, ok := errors.AsType[*mcp.BindError](err); ok { + // An uncategorized binding failure falls through, so that its cause (a filesystem or network error) is classified. + if be, ok := errors.AsType[*mcp.BindError](err); ok && be.Category != mcp.BindUncategorized { return protos.AitoolsErrorCategory(be.Category) } switch { diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index f02d43303e0..f7883f83993 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "net/http" "path/filepath" "slices" "strings" @@ -19,14 +18,12 @@ import ( "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdio" - "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" - "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" ) @@ -94,8 +91,10 @@ type addRequest struct { explicit bool force bool binary string - // configFile is the config file pinned into the entries, "" when DATABRICKS_CONFIG_FILE is unset. + // configFile is the resolved config file that the servers bind to. configFile string + // pinned is the config file pinned into the entries and the state, "" when DATABRICKS_CONFIG_FILE is unset. + pinned string // env is the entry env: DATABRICKS_CONFIG_FILE when it was set. env map[string]string // refused holds the agents named with --agents that are not installed, with the reason. Their @@ -124,8 +123,8 @@ func NewMcpAddCmd() *cobra.Command { Each server runs through 'databricks aitools mcp serve' with that profile's credentials. Servers come from --names: full names such as system.ai.github, or leaf names resolved in ---location (default system.ai). Agents come from --agents. Without it, every detected -MCP-capable agent is used. +--location (default: the schema of the full names, else system.ai). Agents come from --agents. +Without it, every detected MCP-capable agent is used. Interactive selection is not available yet: in a terminal, pass both --names and --agents. With --output json, both are required. @@ -135,10 +134,6 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() jsonMode := installOutputIsJSON(cmd) - if jsonMode { - // JSON mode writes only the result document to stdout. - ctx = cmdio.WithQuiet(ctx) - } out := &addOutput{ Scope: installer.ScopeGlobal, @@ -167,7 +162,7 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), } cmd.Flags().StringVar(&f.names, "names", "", "Services to install: full names (system.ai.github) or leaf names (github), comma-separated") - cmd.Flags().StringVar(&f.location, "location", "", "Schema to resolve leaf names in, as . (default system.ai)") + cmd.Flags().StringVar(&f.location, "location", "", "Schema to resolve leaf names in, as . (default: the schema of the full names in --names, else system.ai)") cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to install for, comma-separated (default: every detected MCP-capable agent)") cmd.Flags().StringVar(&f.name, "name", "", "Server name to use instead of the generated one; only with exactly one service") cmd.Flags().BoolVar(&f.force, "force", false, "Overwrite an existing server with the same name that this CLI did not write") @@ -176,7 +171,7 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), // runMcpAdd installs the requested servers and fills out. Every refusal that needs no request comes before // the first one: the flags, the agents, the binary name, the binding of each service that a recorded entry -// holds, and the --name conflicts. A failure of an agent named with --agents is returned as an agentErrors; +// holds, and the server names. A failure of an agent named with --agents is returned as an agentErrors; // detected agents only report theirs. func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode bool, out *addOutput) error { names := splitAndTrim(f.names) @@ -184,19 +179,13 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if err := validateMCPAddFlags(ctx, f, names, agentNames, jsonMode); err != nil { return err } - // Unity Catalog names are case-insensitive and the API returns them in lowercase, so the lowercase - // forms are the ones compared with the state and the agent entries. - location := strings.ToLower(f.location) - if location == "" { - location = defaultMCPLocation - } - if err := validateLocation(location); err != nil { - return err - } - fullNames, err := serviceNames(location, names) + location, fullNames, err := resolveServices(f.location, names) if err != nil { return err } + if f.name != "" && len(fullNames) != 1 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service in --names") + } targets, explicit, err := targetAgents(ctx, agentNames) if err != nil { return err @@ -218,6 +207,13 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode } } + // The config file is resolved once, as Bind resolves it. The profile is read from that same file, so the + // profile lookups and the bindings below all name one file. + configFile, pinned, err := addConfigFile(ctx) + if err != nil { + return err + } + ctx = env.Set(ctx, mcpconfig.ConfigFileEnv, configFile) profileName, err := resolveProfile(ctx, profileFlag(cmd)) if err != nil { return err @@ -228,22 +224,17 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if err != nil { return err } - configFile, pinned, err := addConfigFile(ctx) - if err != nil { - return err - } for _, name := range fullNames { - if err := mcp.CheckBinding(ctx, state, name, profileName); err != nil { + if _, err := mcp.CheckBinding(ctx, state, name, profileName, configFile); err != nil { return err } } if err := requireProfile(ctx, profileName); err != nil { return err } - if f.name != "" { - if err := checkNameFlag(ctx, f.name, fullNames[0], profileName, pinned, state, targets); err != nil { - return err - } + plans, err := planAdds(ctx, f.name, addBinding{profile: profileName, configFile: configFile}, state, fullNames, targets, readAgentEntries(ctx, targets)) + if err != nil { + return err } w, err := profileClient(ctx, profileName, configFile) @@ -264,7 +255,9 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if err != nil { return err } - plans := planAdds(ctx, f.name, profileName, pinned, state, picked, targets) + for i := range plans { + plans[i].service = picked[i] + } req := addRequest{ jsonMode: jsonMode, @@ -276,7 +269,8 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode explicit: explicit, force: f.force, binary: binary, - configFile: pinned, + configFile: configFile, + pinned: pinned, refused: refused, } if pinned != "" { @@ -305,9 +299,6 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode // validateMCPAddFlags rejects flag combinations that cannot run, before anything is read or written. func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames []string, jsonMode bool) error { if f.name != "" { - if len(names) != 1 { - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service in --names") - } // A name must come out of mcp.Sanitize unchanged, so that mcp____ splits the same way as // a generated name. if mcp.Sanitize(f.name) != f.name { @@ -377,27 +368,19 @@ func addConfigFile(ctx context.Context) (path, pinned string, err error) { return path, path, nil } -// profileClient returns a workspace client bound to exactly the named profile in configFile. Its loaders -// are the ones Bind uses: the profile and the non-auth environment. The environment's credentials are -// never sent to the profile's host. +// profileClient returns a workspace client bound to exactly the named profile in configFile. Its config is the one +// Bind builds, so the environment's credentials are never sent to the profile's host. func profileClient(ctx context.Context, name, configFile string) (*databricks.WorkspaceClient, error) { - cfg := &config.Config{ - Profile: name, - ConfigFile: configFile, - Loaders: []config.Loader{databrickscfg.ResolveNonAuthFromEnv, config.ConfigFile}, - } - w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) + cfg, err := mcp.BoundConfig(name, configFile) if err != nil { - // Configuration that the SDK rejects is not a credential failure, so a login would not fix it. return nil, err } - // Authenticate only writes headers onto req, which is never sent. - req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil) + w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) if err != nil { return nil, err } - if err := w.Config.Authenticate(req); err != nil { - return nil, mcp.AuthError(name, err) + if err := mcp.AuthenticateBound(ctx, cfg, name); err != nil { + return nil, err } return w, nil } @@ -482,32 +465,85 @@ func mcpAgentNames() []string { return names } -// serviceNames returns the full name of each service in --names. A leaf name is resolved in location. A +// resolveServices returns the schema that leaf names resolve in and the full name of each distinct service +// that names pick. The names are lowercase, as the API returns them. +func resolveServices(location string, names []string) (string, []string, error) { + location, err := locationOf(location, names) + if err != nil { + return "", nil, err + } + if err := validateLocation(location); err != nil { + return "", nil, err + } + full, err := serviceNames(location, names) + if err != nil { + return "", nil, err + } + return location, full, nil +} + +// locationOf returns the schema that leaf names resolve in, and that full names must be in. An explicit --location +// is used in lowercase. Without one, full names that all name one schema give that schema, and otherwise the default +// is system.ai. Leaf names resolve in that schema too, so a leaf with full names outside system.ai is refused rather +// than resolved in a schema the user did not name. +func locationOf(explicit string, names []string) (string, error) { + if explicit != "" { + return strings.ToLower(explicit), nil + } + var schemas []string + leaves := false + for _, name := range names { + parts := strings.Split(strings.ToLower(name), ".") + switch len(parts) { + case 1: + leaves = true + case 3: + if schema := parts[0] + "." + parts[1]; !slices.Contains(schemas, schema) { + schemas = append(schemas, schema) + } + } + } + switch { + case len(schemas) == 0: + return mcp.SystemAILocation, nil + case len(schemas) > 1: + return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--names are in different schemas (%s); run add once for each schema", strings.Join(schemas, ", ")) + case leaves && schemas[0] != mcp.SystemAILocation: + return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "leaf names resolve in %s, but the full names are in %s; pass --location %s to resolve the leaves there, or use full names only", mcp.SystemAILocation, schemas[0], schemas[0]) + } + return schemas[0], nil +} + +// serviceNames returns the full name of each distinct service in --names. A leaf name is resolved in location. A // full name must be in location, so its catalog and schema must match it. Any other dotted name is refused, // since it cannot be either. The names are returned in lowercase, as the API returns them. func serviceNames(location string, names []string) ([]string, error) { full := make([]string, 0, len(names)) for _, name := range names { name = strings.ToLower(name) + var fullName string switch parts := strings.Split(name, "."); len(parts) { case 1: - full = append(full, location+"."+name) + fullName = location + "." + name case 3: if parts[0]+"."+parts[1] != location { return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not in %s; pass --location to match its schema", name, location) } - full = append(full, name) + fullName = name default: return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not a service name: use a leaf such as github or a full name such as system.ai.github", name) } + if !slices.Contains(full, fullName) { + full = append(full, fullName) + } } return full, nil } -// pickServices maps the full names of --names to the discovered services of location. Unity Catalog names -// are case-insensitive, so the comparison is too. +// pickServices returns the discovered service of each full name, in the same order. Unity Catalog names are +// case-insensitive, so the comparison is too. func pickServices(location string, names []string, services []mcp.Service) ([]mcp.Service, error) { - var picked []mcp.Service + picked := make([]mcp.Service, 0, len(names)) for _, name := range names { i := slices.IndexFunc(services, func(s mcp.Service) bool { return strings.EqualFold(s.FullName, name) }) if i < 0 { @@ -517,122 +553,156 @@ func pickServices(location string, names []string, services []mcp.Service) ([]mc } return nil, failure(categoryServiceNotFound, nil, "%s is not an MCP service in %s. Available: %s", name, location, strings.Join(available, ", ")) } - if !slices.ContainsFunc(picked, func(s mcp.Service) bool { return s.FullName == services[i].FullName }) { - picked = append(picked, services[i]) - } + picked = append(picked, services[i]) } return picked, nil } -// planAdds chooses the server name of each picked service before anything is written. A service -// already installed for this profile keeps its name. A new name avoids the names of other state -// entries and of every agent entry that is not this binding's, so that a name this CLI wrote for another -// profile is never overwritten. The --name conflicts are checked by checkNameFlag. -func planAdds(ctx context.Context, nameFlag, profileName, pinned string, state *mcp.State, picked []mcp.Service, targets []*agents.Agent) []addPlan { +// addBinding is what the servers of one run bind to: the profile, and the config file it is read from. +type addBinding struct { + profile string + configFile string +} + +// agentEntries holds the server entries of each target agent, as they were read once before planning. An agent whose +// config cannot be read has none here: its write reports the failure. +type agentEntries map[*agents.Agent]map[string]map[string]any + +func readAgentEntries(ctx context.Context, targets []*agents.Agent) agentEntries { + held := make(agentEntries, len(targets)) + for _, a := range targets { + entries, err := agentconfig.Read(ctx, a) + if err != nil { + continue + } + held[a] = entries + } + return held +} + +// planAdds chooses the server name of each service before anything is written, and refuses what would overwrite +// another server. A service already installed for the binding keeps its name, and so does a name that an agent entry +// holds for the same binding when the state has none. A new name avoids the names of other state entries and of every +// agent entry, so that a name this CLI wrote for another profile is never overwritten. +func planAdds(ctx context.Context, nameFlag string, b addBinding, state *mcp.State, fullNames []string, targets []*agents.Agent, held agentEntries) ([]addPlan, error) { taken := map[string]bool{} for i := range state.Servers { taken[state.Servers[i].Name] = true } - plans := make([]addPlan, 0, len(picked)) - for _, svc := range picked { - entry := stateEntry(state, svc.FullName, profileName) - var name string - switch { - case entry != nil: - name = entry.Name - case nameFlag != "": - name = nameFlag - default: - reserved := map[string]bool{} - for _, a := range targets { - for n := range reservedNames(ctx, a, svc.FullName, profileName, pinned) { - reserved[n] = true - } - } - name = mcp.UniqueName(mcp.ServerName(svc.FullName, profileName), func(n string) bool { return taken[n] || reserved[n] }) + plans := make([]addPlan, 0, len(fullNames)) + for _, fullName := range fullNames { + entry, err := mcp.FindEntry(ctx, state, fullName, b.profile, b.configFile) + if err != nil { + return nil, err + } + name, err := chooseName(ctx, nameFlag, b, state, fullName, entry, taken, targets, held) + if err != nil { + return nil, err } taken[name] = true - plans = append(plans, addPlan{service: svc, name: name, entry: entry}) + plans = append(plans, addPlan{service: mcp.Service{FullName: fullName}, name: name, entry: entry}) } - return plans + return plans, nil } -// checkNameFlag refuses a --name that another server holds: the state gives the name to another service, -// or a target agent holds it for another binding this CLI wrote. The service's own entry keeps its name, so -// --name must match it. An unmanaged entry is left to the agent's write, which refuses it unless --force is -// set. It reads files only, so it runs before any request. -func checkNameFlag(ctx context.Context, nameFlag, fullName, profileName, pinned string, state *mcp.State, targets []*agents.Agent) error { - entry := stateEntry(state, fullName, profileName) - switch { - case entry != nil && nameFlag != entry.Name: - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is already installed on profile %s as %s; remove it before using --name %s", fullName, profileName, entry.Name, nameFlag) - case entry == nil && slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { return e.Name == nameFlag }): - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another installed server; pick a different name", nameFlag) - } - for _, a := range targets { - // An unreadable config reserves nothing here. Its write reports the failure for the agent. - entries, err := agentconfig.Read(ctx, a) +// chooseName returns the server name for fullName. entry is its state entry, nil for a new install. The name it holds is +// kept, and then --name is used only when no other server holds it. Otherwise the name is generated. +func chooseName(ctx context.Context, nameFlag string, b addBinding, state *mcp.State, fullName string, entry *mcp.Entry, taken map[string]bool, targets []*agents.Agent, held agentEntries) (string, error) { + stored := "" + if entry != nil { + stored = entry.Name + } else { + owned, err := ownedNames(ctx, held, targets, fullName, b) if err != nil { - continue + return "", err } - if other, ok := entries[nameFlag]; ok && mcpconfig.IsDatabricksEntry(other) && !ownedBy(other, fullName, profileName, pinned) { - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another server in %s; pick a different name", nameFlag, a.Name) + switch len(owned) { + case 0: + case 1: + stored = owned[0] + default: + return "", failure(categoryNameConflict, nil, "%s is installed on profile %s under more than one name in the agents (%s); remove the extra entries", fullName, b.profile, strings.Join(owned, ", ")) } } - return nil -} -// reservedNames returns the server names in the agent's config that a new install of service for profileName -// must not take: every entry except this binding's own. An unreadable config reserves nothing here, and its -// write reports the failure for the agent. -func reservedNames(ctx context.Context, a *agents.Agent, service, profileName, pinned string) map[string]bool { - entries, err := agentconfig.Read(ctx, a) - if err != nil { - return nil + if stored != "" { + if nameFlag != "" && nameFlag != stored { + return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is already installed on profile %s as %s; remove it before using --name %s", fullName, b.profile, stored, nameFlag) + } + other, err := foreignHolder(ctx, held, targets, stored, fullName, b) + if err != nil { + return "", err + } + if other != nil { + return "", failure(categoryNameConflict, nil, "%s is installed as %s, but %s holds %s for another server; remove that entry first", fullName, stored, other.Name, stored) + } + return stored, nil } - names := make(map[string]bool, len(entries)) - for name, entry := range entries { - if !ownedBy(entry, service, profileName, pinned) { - names[name] = true + + if nameFlag != "" { + if slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { return e.Name == nameFlag }) { + return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another installed server; pick a different name", nameFlag) + } + other, err := foreignHolder(ctx, held, targets, nameFlag, fullName, b) + if err != nil { + return "", err } + if other != nil { + return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another server in %s; pick a different name", nameFlag, other.Name) + } + return nameFlag, nil } - return names -} -// ownedBy reports whether entry is a Databricks entry that serves service for profileName through the config -// file pinned ("" for the default file). Only such an entry may be reused by an install. -func ownedBy(entry map[string]any, service, profileName, pinned string) bool { - argv, ok := mcpconfig.Argv(entry) - if !ok || !mcpconfig.IsDatabricksEntry(entry) { - return false + base := mcp.ServerName(fullName, b.profile) + if base == "" { + return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "cannot make a server name from %s for profile %s; pass --name", fullName, b.profile) } - if !strings.EqualFold(argValue(argv, "--service"), service) || argValue(argv, "--profile") != profileName { - return false + // No entry of this binding exists, so every name that an agent holds belongs to another server. + reserved := map[string]bool{} + for _, a := range targets { + for name := range held[a] { + reserved[name] = true + } } - vars, _ := entry["env"].(map[string]any) - file, _ := vars[mcpconfig.ConfigFileEnv].(string) - return file == pinned + return mcp.UniqueName(base, func(n string) bool { return taken[n] || reserved[n] }), nil } -// argValue returns the value that follows flag in argv, or "" when flag is absent. -func argValue(argv []string, flag string) string { - for i := 0; i+1 < len(argv); i++ { - if argv[i] == flag { - return argv[i+1] +// ownedNames returns the distinct names that the target agents hold for the binding of fullName, sorted. +func ownedNames(ctx context.Context, held agentEntries, targets []*agents.Agent, fullName string, b addBinding) ([]string, error) { + var names []string + for _, a := range targets { + for name, entry := range held[a] { + serves, err := mcp.EntryServes(ctx, entry, fullName, b.profile, b.configFile) + if err != nil { + return nil, err + } + if serves && !slices.Contains(names, name) { + names = append(names, name) + } } } - return "" + slices.Sort(names) + return names, nil } -func stateEntry(state *mcp.State, service, profileName string) *mcp.Entry { - i := slices.IndexFunc(state.Servers, func(e mcp.Entry) bool { - return strings.EqualFold(e.Service, service) && e.Profile == profileName - }) - if i < 0 { - return nil +// foreignHolder returns a target agent that holds name as a Databricks entry of another binding than fullName's, or nil. +// An unmanaged entry is not returned: the agent's write refuses it unless --force is set. +func foreignHolder(ctx context.Context, held agentEntries, targets []*agents.Agent, name, fullName string, b addBinding) (*agents.Agent, error) { + for _, a := range targets { + entry, ok := held[a][name] + if !ok || !mcpconfig.IsDatabricksEntry(entry) { + continue + } + serves, err := mcp.EntryServes(ctx, entry, fullName, b.profile, b.configFile) + if err != nil { + return nil, err + } + if !serves { + return a, nil + } } - return &state.Servers[i] + return nil, nil } // installServer writes one server into every target agent. Each agent's result is reported; one @@ -649,7 +719,7 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO var status agentconfig.Status err := req.refused[a] if err == nil { - args := []string{"aitools", "mcp", "serve", "--profile", req.profile, "--service", plan.service.FullName, "--agent", a.Name} + args := []string{"aitools", "mcp", "serve", mcpconfig.ProfileFlag, req.profile, mcpconfig.ServiceFlag, plan.service.FullName, "--agent", a.Name} path, status, err = agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) } @@ -715,7 +785,9 @@ func recordInstalls(ctx context.Context, req addRequest, plans []addPlan, server if len(paths) == 0 { continue } - recordServer(state, req, plans[i], paths) + if err := recordServer(ctx, state, req, plans[i], paths); err != nil { + return err + } recorded = true } if !recorded { @@ -724,10 +796,13 @@ func recordInstalls(ctx context.Context, req addRequest, plans []addPlan, server return mcp.SaveState(ctx, state) } -// recordServer stores the install of plan's server for req.profile, with the agents that succeeded +// recordServer stores the install of plan's server for req.profile in configFile, with the agents that succeeded // and their config paths. -func recordServer(state *mcp.State, req addRequest, plan addPlan, paths map[string]string) { - entry := stateEntry(state, plan.service.FullName, req.profile) +func recordServer(ctx context.Context, state *mcp.State, req addRequest, plan addPlan, paths map[string]string) error { + entry, err := mcp.FindEntry(ctx, state, plan.service.FullName, req.profile, req.configFile) + if err != nil { + return err + } if entry == nil { state.Servers = append(state.Servers, mcp.Entry{ Name: plan.name, @@ -741,7 +816,7 @@ func recordServer(state *mcp.State, req addRequest, plan addPlan, paths map[stri entry.WorkspaceID = req.workspaceID entry.SignInRequired = plan.service.SignInRequired entry.Command = req.binary - entry.ConfigFile = req.configFile + entry.ConfigFile = req.pinned entry.CLIVersion = build.GetInfo().Version if entry.Agents == nil { // A hand-edited state file can hold "agents": null. @@ -750,6 +825,7 @@ func recordServer(state *mcp.State, req addRequest, plan addPlan, paths map[stri for agent, path := range paths { entry.Agents[agent] = mcp.AgentRecord{Path: path, Scope: agentScopeUser} } + return nil } // agentFailureError returns the agents' failures as an agentErrors. The command fails when an agent was named diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 8a50e4a7fd7..85c274f12cf 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -12,6 +12,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "strings" "testing" @@ -49,10 +50,6 @@ func TestValidateMCPAddFlags(t *testing.T) { want: "--output json requires --names and --agents so the command runs without interactive prompts", }, {name: "json with both", names: []string{"github"}, agentNames: []string{"codex"}, jsonMode: true}, - { - name: "name needs one service", flags: mcpAddFlags{name: "x"}, names: []string{"a", "b"}, - want: "--name needs exactly one service in --names", - }, { name: "name must be a valid server name", flags: mcpAddFlags{name: "a.b"}, names: []string{"github"}, want: `--name "a.b" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, @@ -109,7 +106,7 @@ func testServices() []mcp.Service { func TestServiceNames(t *testing.T) { got, err := serviceNames("system.ai", []string{"github", "System.AI.GitHub", "system.ai.genie_one_mcp"}) require.NoError(t, err) - assert.Equal(t, []string{"system.ai.github", "system.ai.github", "system.ai.genie_one_mcp"}, got) + assert.Equal(t, []string{"system.ai.github", "system.ai.genie_one_mcp"}, got, "a leaf and its full name are one service") _, err = serviceNames("system.ai", []string{"main.tools.crm"}) require.Error(t, err) @@ -123,14 +120,68 @@ func TestServiceNames(t *testing.T) { assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) } +func TestResolveServices(t *testing.T) { + cases := []struct { + name string + location string + names []string + wantLoc string + wantFull []string + wantErr string + }{ + {name: "leaves resolve in the default schema", names: []string{"github"}, wantLoc: "system.ai", wantFull: []string{"system.ai.github"}}, + { + name: "full names in one schema give that schema", names: []string{"main.tools.crm", "main.tools.github"}, + wantLoc: "main.tools", wantFull: []string{"main.tools.crm", "main.tools.github"}, + }, + {name: "full names in system.ai give system.ai", names: []string{"system.ai.github", "github"}, wantLoc: "system.ai", wantFull: []string{"system.ai.github"}}, + { + name: "a leaf with full names outside system.ai is refused, since it would resolve in system.ai", names: []string{"main.tools.crm", "github"}, + wantErr: "leaf names resolve in system.ai, but the full names are in main.tools; pass --location main.tools to resolve the leaves there, or use full names only", + }, + { + name: "full names in different schemas need --location", names: []string{"main.tools.crm", "system.ai.github"}, + wantErr: "--names are in different schemas (main.tools, system.ai); run add once for each schema", + }, + { + name: "an explicit --location decides, and full names must agree with it", location: "main.tools", names: []string{"system.ai.github"}, + wantErr: "system.ai.github is not in main.tools; pass --location to match its schema", + }, + { + name: "an explicit --location is lowercased", location: "Main.Tools", names: []string{"crm"}, + wantLoc: "main.tools", wantFull: []string{"main.tools.crm"}, + }, + { + name: "full names are case-insensitive and distinct from their leaves", names: []string{"github", "System.AI.GitHub"}, + wantLoc: "system.ai", wantFull: []string{"system.ai.github"}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + loc, full, err := resolveServices(tc.location, tc.names) + if tc.wantErr != "" { + require.Error(t, err) + assert.Equal(t, tc.wantErr, err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + return + } + require.NoError(t, err) + assert.Equal(t, tc.wantLoc, loc) + assert.Equal(t, tc.wantFull, full) + }) + } +} + func TestPickServices(t *testing.T) { services := testServices() - picked, err := pickServices("system.ai", []string{"system.ai.github", "system.ai.genie_one_mcp", "system.ai.github"}, services) + // The result is one service per name, in the order of the names, so it lines up with the planned servers. + picked, err := pickServices("system.ai", []string{"system.ai.genie_one_mcp", "system.ai.github"}, services) require.NoError(t, err) require.Len(t, picked, 2) - assert.Equal(t, "system.ai.github", picked[0].FullName) - assert.Equal(t, "system.ai.genie_one_mcp", picked[1].FullName) + assert.Equal(t, "system.ai.genie_one_mcp", picked[0].FullName) + assert.Equal(t, "system.ai.github", picked[1].FullName) + assert.True(t, picked[1].SignInRequired) picked, err = pickServices("System.AI", []string{"System.AI.GitHub"}, services) require.NoError(t, err) @@ -197,15 +248,21 @@ func withHome(t *testing.T) string { return home } +// The variables that move an agent's config directory. Codex and Copilot CLI keep their MCP config there. +const ( + codexHomeEnv = "CODEX_HOME" + copilotHomeEnv = "COPILOT_HOME" +) + // agentHomeEnv are the variables that move an agent's config directory. -var agentHomeEnv = []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA", "DATABRICKS_CONFIG_FILE"} +var agentHomeEnv = []string{"CLAUDE_CONFIG_DIR", codexHomeEnv, copilotHomeEnv, "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA", "DATABRICKS_CONFIG_FILE"} -// withDatabricksBinary makes os.Args[0] the databricks command, as the installed CLI has it. +// withDatabricksBinary makes the command name the databricks command, as the installed CLI has it. func withDatabricksBinary(t *testing.T) { t.Helper() - saved := os.Args[0] - os.Args[0] = "databricks" - t.Cleanup(func() { os.Args[0] = saved }) + saved := binaryArg0 + binaryArg0 = "databricks" + t.Cleanup(func() { binaryArg0 = saved }) } // noRequestServer returns a server that fails the test on any request. A command that refuses before @@ -236,6 +293,23 @@ func noAPIServer(t *testing.T) *httptest.Server { return server } +// rejectingServer returns a server that rejects every API call with 401, as a workspace does for an expired token. +// A command that reaches its first API call fails with AUTH_FAILED. +func rejectingServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`)) + })) + t.Cleanup(server.Close) + return server +} + // writeHomeConfig writes the default Databricks config file, ~/.databrickscfg, under home. Entries that are // not pinned to a file use that file. func writeHomeConfig(t *testing.T, home, contents string) { @@ -289,31 +363,71 @@ func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { assert.NoError(t, out.Agents[1].err) } +// defaultBinding returns the binding to profile in the default config file, resolved as the command resolves it. +func defaultBinding(t *testing.T, profile string) addBinding { + t.Helper() + path, err := mcp.ResolveConfigFile(t.Context(), "") + require.NoError(t, err) + return addBinding{profile: profile, configFile: path} +} + +// planNames runs planAdds for the services with the agent entries read as the command reads them. +func planNames(t *testing.T, nameFlag string, b addBinding, state *mcp.State, fullNames []string, targets []*agents.Agent) ([]addPlan, error) { + t.Helper() + return planAdds(t.Context(), nameFlag, b, state, fullNames, targets, readAgentEntries(t.Context(), targets)) +} + +// claudeEntry is a Databricks entry for claude-code that runs the serve command for service and profile, with the +// config file pinned in env when pin is not "". +func claudeEntry(service, profile, pin string) string { + env := "" + if pin != "" { + env = `,"env":{"DATABRICKS_CONFIG_FILE":` + strconv.Quote(pin) + `}` + } + return `{"type":"stdio","command":"databricks","args":["aitools","mcp","serve","--profile","` + profile + `","--service","` + service + `","--agent","claude-code"]` + env + `}` +} + func TestPlanAddsNaming(t *testing.T) { - ctx := t.Context() claude := agents.ByName("claude-code") const host = "https://adb-123.test" t.Run("new service gets the generated name", func(t *testing.T) { withHome(t) - plans := planAdds(ctx, "", "logfood", "", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.genie_one_mcp"}, []*agents.Agent{claude}) + require.NoError(t, err) require.Len(t, plans, 1) assert.Equal(t, "genie_one_mcp-logfood", plans[0].name) assert.Nil(t, plans[0].entry) + assert.Equal(t, "system.ai.genie_one_mcp", plans[0].service.FullName) }) t.Run("existing install keeps its name", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "custom-name", Service: "system.ai.github", Profile: "logfood", Host: host}}} - plans := planAdds(ctx, "", "logfood", "", state, testServices()[1:], []*agents.Agent{claude}) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) assert.Equal(t, "custom-name", plans[0].name) assert.NotNil(t, plans[0].entry) }) + t.Run("stored entry for the same binding is found in its own config file only", func(t *testing.T) { + withHome(t) + other := writeAddConfig(t, "[logfood]\nhost = "+host+"\n") + // The first entry is for the same service and profile, in another config file. The second is in the default file. + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "github-other-file", Service: "system.ai.github", Profile: "logfood", Host: host, ConfigFile: other}, + {Name: "github-default-file", Service: "system.ai.github", Profile: "logfood", Host: host}, + }} + plans, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "github-default-file", plans[0].name) + }) + t.Run("name used by another state entry gets a suffix", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "genie_one_mcp-logfood", Service: "main.tools.other", Profile: "logfood", Host: host}}} - plans := planAdds(ctx, "", "logfood", "", state, testServices()[:1], []*agents.Agent{claude}) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.genie_one_mcp"}, []*agents.Agent{claude}) + require.NoError(t, err) assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) }) @@ -321,7 +435,8 @@ func TestPlanAddsNaming(t *testing.T) { home := withHome(t) unmanaged := `{"mcpServers":{"genie_one_mcp-logfood":{"type":"stdio","command":"ug","args":["mcp-proxy"]}}}` require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(unmanaged), 0o600)) - plans := planAdds(ctx, "", "logfood", "", &mcp.State{}, testServices()[:1], []*agents.Agent{claude}) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.genie_one_mcp"}, []*agents.Agent{claude}) + require.NoError(t, err) assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) }) @@ -330,7 +445,8 @@ func TestPlanAddsNaming(t *testing.T) { // dev.foo and dev-foo sanitize to the same server name, so the entry for dev.foo is not dev-foo's. other := `{"mcpServers":{"github-dev-foo":{"type":"stdio","command":"databricks","args":["aitools","mcp","serve","--profile","dev.foo","--service","system.ai.github","--agent","claude-code"]}}}` require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(other), 0o600)) - plans := planAdds(ctx, "", "dev-foo", "", &mcp.State{}, testServices()[1:], []*agents.Agent{claude}) + plans, err := planNames(t, "", defaultBinding(t, "dev-foo"), &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) assert.Equal(t, "github-dev-foo-2", plans[0].name) }) @@ -338,25 +454,114 @@ func TestPlanAddsNaming(t *testing.T) { home := withHome(t) same := `{"mcpServers":{"github-dev-foo":{"type":"stdio","command":"databricks","args":["aitools","mcp","serve","--profile","dev.foo","--service","system.ai.github","--agent","claude-code"]}}}` require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(same), 0o600)) - plans := planAdds(ctx, "", "dev.foo", "", &mcp.State{}, testServices()[1:], []*agents.Agent{claude}) + plans, err := planNames(t, "", defaultBinding(t, "dev.foo"), &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) assert.Equal(t, "github-dev-foo", plans[0].name) }) + t.Run("entry this binding wrote under a --name is reused when state is missing", func(t *testing.T) { + home := withHome(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"mine":`+claudeEntry("system.ai.github", "logfood", "")+`}}`), 0o600)) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "mine", plans[0].name) + }) + + t.Run("entry of this binding pinned to the default file by its absolute path is reused", func(t *testing.T) { + home := withHome(t) + b := defaultBinding(t, "logfood") + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"mine":`+claudeEntry("system.ai.github", "logfood", b.configFile)+`}}`), 0o600)) + plans, err := planNames(t, "", b, &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "mine", plans[0].name) + }) + + t.Run("entry pinned to the default file by a relative or ~ path is the default file", func(t *testing.T) { + home := withHome(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"mine":`+claudeEntry("system.ai.github", "logfood", "~/.databrickscfg")+`}}`), 0o600)) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "mine", plans[0].name) + }) + + t.Run("entry for this binding in another config file is not reused", func(t *testing.T) { + home := withHome(t) + other := writeAddConfig(t, "[logfood]\nhost = "+host+"\n") + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"mine":`+claudeEntry("system.ai.github", "logfood", other)+`}}`), 0o600)) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) + assert.Equal(t, "github-logfood", plans[0].name) + }) + + t.Run("entries of this binding named differently in two agents are refused", func(t *testing.T) { + home := withHome(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"one":`+claudeEntry("system.ai.github", "logfood", "")+`}}`), 0o600)) + openCode := filepath.Join(home, ".config", "opencode") + require.NoError(t, os.MkdirAll(openCode, 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(openCode, "opencode.json"), []byte(`{"mcp":{"servers":{"two":{"type":"local","command":["databricks","aitools","mcp","serve","--profile","logfood","--service","system.ai.github","--agent","opencode"]}}}}`), 0o600)) + _, err := planNames(t, "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude, agents.ByName("opencode")}) + require.Error(t, err) + assert.Equal(t, categoryNameConflict, errorCategory(err)) + }) + + t.Run("OpenCode entry with its variables in environment is reused", func(t *testing.T) { + home := withHome(t) + pinned := writeAddConfig(t, "[logfood]\nhost = https://adb-123.test\n") + openCode := filepath.Join(home, ".config", "opencode") + require.NoError(t, os.MkdirAll(openCode, 0o700)) + // OpenCode keeps the variables under environment and the command as one array. + entry := `{"type":"local","command":["databricks","aitools","mcp","serve","--profile","logfood","--service","system.ai.github","--agent","opencode"],"environment":{"DATABRICKS_CONFIG_FILE":` + strconv.Quote(pinned) + `}}` + require.NoError(t, os.WriteFile(filepath.Join(openCode, "opencode.json"), []byte(`{"mcp":{"servers":{"mine":`+entry+`}}}`), 0o600)) + b := addBinding{profile: "logfood", configFile: pinned} + plans, err := planNames(t, "", b, &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{agents.ByName("opencode")}) + require.NoError(t, err) + assert.Equal(t, "mine", plans[0].name) + }) + t.Run("name flag for an installed service must match", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host}}} - err := checkNameFlag(ctx, "other", "system.ai.github", "logfood", "", state, []*agents.Agent{claude}) + _, err := planNames(t, "other", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) require.Error(t, err) assert.Equal(t, "system.ai.github is already installed on profile logfood as github-logfood; remove it before using --name other", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) }) t.Run("name flag already used by another service is refused", func(t *testing.T) { withHome(t) state := &mcp.State{Servers: []mcp.Entry{{Name: "taken", Service: "main.tools.other", Profile: "logfood", Host: host}}} - err := checkNameFlag(ctx, "taken", "system.ai.genie_one_mcp", "logfood", "", state, []*agents.Agent{claude}) + _, err := planNames(t, "taken", defaultBinding(t, "logfood"), state, []string{"system.ai.genie_one_mcp"}, []*agents.Agent{claude}) require.Error(t, err) assert.Equal(t, "--name taken is used by another installed server; pick a different name", err.Error()) }) + + t.Run("stored name held by another binding in an agent is refused", func(t *testing.T) { + home := withHome(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"github-logfood":`+claudeEntry("system.ai.github", "dev-foo", "")+`}}`), 0o600)) + state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host}}} + _, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, categoryNameConflict, errorCategory(err)) + }) + + t.Run("planning reads the agent entries it is given, not the files", func(t *testing.T) { + withHome(t) + // Nothing is on disk. The entries that the command read once decide the names. + held := map[*agents.Agent]map[string]map[string]any{ + claude: {"genie_one_mcp-logfood": map[string]any{"type": "stdio", "command": "ug", "args": []any{"mcp-proxy"}}}, + } + plans, err := planAdds(t.Context(), "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.genie_one_mcp"}, []*agents.Agent{claude}, held) + require.NoError(t, err) + assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) + }) + + t.Run("a service whose generated name is empty is refused", func(t *testing.T) { + withHome(t) + _, err := planNames(t, "", defaultBinding(t, "数据"), &mcp.State{}, []string{"system.ai.数据"}, []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + assert.Equal(t, "cannot make a server name from system.ai.数据 for profile 数据; pass --name", err.Error()) + }) } func TestAddConfigFileResolvesAsBindDoes(t *testing.T) { @@ -424,7 +629,7 @@ func TestMcpAddJSONReportsMissingFlagsAsDocument(t *testing.T) { assert.Equal(t, "global", out.Scope) } -// TestMcpAddRefusesRepointedHostBeforeAnyRequest a profile re-pointed at another host is refused before any request reaches that host. +// TestMcpAddRefusesRepointedHostBeforeAnyRequest checks that a profile re-pointed at another host is refused before any request reaches that host. func TestMcpAddRefusesRepointedHostBeforeAnyRequest(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -454,7 +659,7 @@ func TestMcpAddRefusesWorkspaceChangeOnSameHostBeforeAnyRequest(t *testing.T) { assert.Contains(t, err.Error(), "was set up for workspace 111, but profile \"logfood\" now points at workspace 222") } -// TestMcpAddRefusesPartialReaddWithOtherConfigFile a second service on a profile whose first entry is pinned to another file is refused as a whole: +// TestMcpAddRefusesPartialReaddWithOtherConfigFile checks that a second service on a profile whose first entry is pinned to another file is refused as a whole: // no agent file is written and the state keeps its one entry. func TestMcpAddRefusesPartialReaddWithOtherConfigFile(t *testing.T) { home := withHome(t) @@ -478,7 +683,7 @@ func TestMcpAddRefusesPartialReaddWithOtherConfigFile(t *testing.T) { assert.Equal(t, "github-logfood", state.Servers[0].Name) } -// TestMcpAddNeverUsesEnvTokenForProfileWithoutToken the profile's own credentials are used. The environment's token is not sent to a profile without one. +// TestMcpAddNeverUsesEnvTokenForProfileWithoutToken checks that the profile's own credentials are used. The environment's token is not sent to a profile without one. func TestMcpAddNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { home := withHome(t) server := noAPIServer(t) @@ -491,7 +696,7 @@ func TestMcpAddNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { assert.Contains(t, err.Error(), "databricks auth login --profile logfood") } -// TestMcpAddInvalidConfigIsNotAuthFailed a configuration the SDK rejects is not an authentication failure, so a login would not fix it. +// TestMcpAddInvalidConfigIsNotAuthFailed checks that a configuration the SDK rejects is not an authentication failure, so a login would not fix it. func TestMcpAddInvalidConfigIsNotAuthFailed(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -503,7 +708,7 @@ func TestMcpAddInvalidConfigIsNotAuthFailed(t *testing.T) { assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) } -// TestMcpAddRejectsUnknownAgentBeforeProfile --agents is checked with the other flags, before the profile is read or the network is used. +// TestMcpAddRejectsUnknownAgentBeforeProfile checks that --agents is checked with the other flags, before the profile is read or the network is used. func TestMcpAddRejectsUnknownAgentBeforeProfile(t *testing.T) { withHome(t) err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "pi") @@ -529,14 +734,16 @@ type failingWriter struct{} func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("disk full") } -// TestInstallServerCodexHomeCountsAsInstalled an agent with a config directory is installed for MCP even when Detected would say otherwise. +// TestInstallServerCodexHomeCountsAsInstalled checks that an agent with a config directory is installed for MCP even when Detected would say otherwise. func TestInstallServerCodexHomeCountsAsInstalled(t *testing.T) { home := withHome(t) codexHome := filepath.Join(t.TempDir(), "codex") require.NoError(t, os.MkdirAll(codexHome, 0o700)) - t.Setenv("CODEX_HOME", codexHome) + t.Setenv(codexHomeEnv, codexHome) req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("codex")}, binary: "databricks"} + req.refused = refusedAgents(t.Context(), req.agents) + require.Empty(t, req.refused, "the agent must count as installed, so the refusal path must not refuse it") out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) require.Len(t, out.Agents, 1) assert.Equal(t, statusInstalled, out.Agents[0].Status) @@ -545,7 +752,7 @@ func TestInstallServerCodexHomeCountsAsInstalled(t *testing.T) { assert.NoDirExists(t, filepath.Join(home, ".codex")) } -// TestInstallServerGeminiDirAloneIsRefused gemini CLI's directory is shared with Antigravity, so ~/.gemini alone does not install Gemini CLI. +// TestInstallServerGeminiDirAloneIsRefused checks that gemini CLI's directory is shared with Antigravity, so ~/.gemini alone does not install Gemini CLI. func TestInstallServerGeminiDirAloneIsRefused(t *testing.T) { home := withHome(t) require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o700)) @@ -559,32 +766,36 @@ func TestInstallServerGeminiDirAloneIsRefused(t *testing.T) { assert.NoFileExists(t, filepath.Join(home, ".gemini", "settings.json")) } -// TestInstallServerGeminiWithProjectsJSON gemini CLI is installed once it has written projects.json, as the registry detects it. +// TestInstallServerGeminiWithProjectsJSON checks that gemini CLI is installed once it has written projects.json, as the registry detects it. func TestInstallServerGeminiWithProjectsJSON(t *testing.T) { home := withHome(t) require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini"), 0o700)) require.NoError(t, os.WriteFile(filepath.Join(home, ".gemini", "projects.json"), []byte("{}"), 0o600)) req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("gemini")}, binary: "databricks"} + req.refused = refusedAgents(t.Context(), req.agents) + require.Empty(t, req.refused, "projects.json must count as installed, so the refusal path must not refuse it") out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) require.Len(t, out.Agents, 1) assert.Equal(t, statusInstalled, out.Agents[0].Status) assert.FileExists(t, filepath.Join(home, ".gemini", "settings.json")) } -// TestInstallServerAntigravityFromItsOwnDir antigravity is installed from its own directory, and its MCP file is written under ~/.gemini/config. +// TestInstallServerAntigravityFromItsOwnDir checks that antigravity is installed from its own directory, and its MCP file is written under ~/.gemini/config. func TestInstallServerAntigravityFromItsOwnDir(t *testing.T) { home := withHome(t) require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o700)) req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("antigravity")}, binary: "databricks"} + req.refused = refusedAgents(t.Context(), req.agents) + require.Empty(t, req.refused, "its own directory must count as installed, so the refusal path must not refuse it") out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) require.Len(t, out.Agents, 1) assert.Equal(t, statusInstalled, out.Agents[0].Status) assert.FileExists(t, filepath.Join(home, ".gemini", "config", "mcp_config.json")) } -// TestInstallServerCLIOnPathCountsAsInstalled a CLI found only on PATH counts as installed, so its MCP config is written. +// TestInstallServerCLIOnPathCountsAsInstalled checks that a CLI found only on PATH counts as installed, so its MCP config is written. func TestInstallServerCLIOnPathCountsAsInstalled(t *testing.T) { home := withHome(t) dir := t.TempDir() @@ -596,13 +807,15 @@ func TestInstallServerCLIOnPathCountsAsInstalled(t *testing.T) { t.Setenv("PATH", dir) req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("codex")}, binary: "databricks"} + req.refused = refusedAgents(t.Context(), req.agents) + require.Empty(t, req.refused, "a CLI on PATH must count as installed, so the refusal path must not refuse it") out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) require.Len(t, out.Agents, 1) assert.Equal(t, statusInstalled, out.Agents[0].Status) assert.FileExists(t, filepath.Join(home, ".codex", "config.toml")) } -// TestInstallServerReassertsFromAgentFile a second install of the same entry reports "re-asserted" from the agent file, even when the state +// TestInstallServerReassertsFromAgentFile checks that a second install of the same entry reports "re-asserted" from the agent file, even when the state // that recorded it is gone. func TestInstallServerReassertsFromAgentFile(t *testing.T) { home := withHome(t) @@ -620,7 +833,7 @@ func TestInstallServerReassertsFromAgentFile(t *testing.T) { assert.Equal(t, statusReasserted, second.Agents[0].Status) } -// TestAgentFailureErrorWhenAServiceReachedNoAgent a service that reached no agent fails the command, even when another service was installed. +// TestAgentFailureErrorWhenAServiceReachedNoAgent checks that a service that reached no agent fails the command, even when another service was installed. func TestAgentFailureErrorWhenAServiceReachedNoAgent(t *testing.T) { installed := addServerOutput{Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled}}} mixed := addServerOutput{Agents: []addAgentOutput{ @@ -659,7 +872,7 @@ func TestErrorCategoryMapsTypedFailures(t *testing.T) { assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(mcp.AuthError("logfood", errors.New("no token")))) } -// TestMcpAddRefusesMixedCaseNamesBeforeAnyRequest mixed-case service and location names bind to the same entry as their lowercase forms, so a profile +// TestMcpAddRefusesMixedCaseNamesBeforeAnyRequest checks that mixed-case service and location names bind to the same entry as their lowercase forms, so a profile // re-pointed at another host is refused before any request, whatever the case typed. func TestMcpAddRefusesMixedCaseNamesBeforeAnyRequest(t *testing.T) { home := withHome(t) @@ -675,7 +888,7 @@ func TestMcpAddRefusesMixedCaseNamesBeforeAnyRequest(t *testing.T) { assert.NoFileExists(t, filepath.Join(home, ".claude.json")) } -// TestMcpAddRefusesNameFlagConflictBeforeAnyRequest a --name that the state gives to another server is refused before any request. +// TestMcpAddRefusesNameFlagConflictBeforeAnyRequest checks that a --name that the state gives to another server is refused before any request. func TestMcpAddRefusesNameFlagConflictBeforeAnyRequest(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -689,7 +902,7 @@ func TestMcpAddRefusesNameFlagConflictBeforeAnyRequest(t *testing.T) { assert.Equal(t, "--name taken is used by another installed server; pick a different name", err.Error()) } -// TestMcpAddRefusesNameFlagHeldByOtherBindingBeforeAnyRequest a --name that an agent file gives to another profile's managed server is refused before any request. +// TestMcpAddRefusesNameFlagHeldByOtherBindingBeforeAnyRequest checks that a --name that an agent file gives to another profile's managed server is refused before any request. func TestMcpAddRefusesNameFlagHeldByOtherBindingBeforeAnyRequest(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -703,7 +916,7 @@ func TestMcpAddRefusesNameFlagHeldByOtherBindingBeforeAnyRequest(t *testing.T) { assert.Contains(t, err.Error(), "--name mine is used by another server") } -// TestMcpAddRefusesUninstalledAgentBeforeAnyRequest an agent named with --agents that is not installed is refused before any request, once, not per service. +// TestMcpAddRefusesUninstalledAgentBeforeAnyRequest checks that an agent named with --agents that is not installed is refused before any request, once, not per service. func TestMcpAddRefusesUninstalledAgentBeforeAnyRequest(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -716,7 +929,7 @@ func TestMcpAddRefusesUninstalledAgentBeforeAnyRequest(t *testing.T) { assert.NoDirExists(t, filepath.Join(home, ".codex")) } -// TestMcpAddMissingProfileWithEntryGetsRecreateHint a profile that is gone while a server is still bound to it gets the recreate hint, not the generic message. +// TestMcpAddMissingProfileWithEntryGetsRecreateHint checks that a profile that is gone while a server is still bound to it gets the recreate hint, not the generic message. func TestMcpAddMissingProfileWithEntryGetsRecreateHint(t *testing.T) { home := withHome(t) server := noRequestServer(t) @@ -732,7 +945,7 @@ func TestMcpAddMissingProfileWithEntryGetsRecreateHint(t *testing.T) { assert.Contains(t, err.Error(), "databricks auth login --host https://adb-123.test --profile logfood") } -// TestMcpAddEmptyHostIsProfileNotFound a profile whose host is empty has no workspace, so it is reported as missing before any request. +// TestMcpAddEmptyHostIsProfileNotFound checks that a profile whose host is empty has no workspace, so it is reported as missing before any request. func TestMcpAddEmptyHostIsProfileNotFound(t *testing.T) { home := withHome(t) writeHomeConfig(t, home, "[logfood]\nhost =\nauth_type = pat\ntoken = dapi-logfood\n") @@ -743,19 +956,10 @@ func TestMcpAddEmptyHostIsProfileNotFound(t *testing.T) { assert.Contains(t, err.Error(), "profile logfood not found in ") } -// TestMcpAddRejectedTokenIsAuthFailed a credential the workspace rejects on the first real request is AUTH_FAILED, with the login command. +// TestMcpAddRejectedTokenIsAuthFailed checks that a credential the workspace rejects on the first real request is AUTH_FAILED, with the login command. func TestMcpAddRejectedTokenIsAuthFailed(t *testing.T) { home := withHome(t) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !strings.HasPrefix(r.URL.Path, "/api/") { - w.WriteHeader(http.StatusNotFound) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusUnauthorized) - _, _ = w.Write([]byte(`{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`)) - })) - t.Cleanup(server.Close) + server := rejectingServer(t) writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-expired\n") err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") @@ -765,15 +969,15 @@ func TestMcpAddRejectedTokenIsAuthFailed(t *testing.T) { assert.NoFileExists(t, filepath.Join(home, ".claude.json")) } -// TestMcpAddRefusesBinaryNotNamedDatabricks the binary must be databricks: an argv[0] with another name, not on PATH, is refused, not replaced. +// TestMcpAddRefusesBinaryNotNamedDatabricks checks that the binary must be databricks: an argv[0] with another name, not on PATH, is refused, not replaced. func TestMcpAddRefusesBinaryNotNamedDatabricks(t *testing.T) { home := withHome(t) server := noRequestServer(t) writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") - saved := os.Args[0] - os.Args[0] = "dbx-not-on-path" - t.Cleanup(func() { os.Args[0] = saved }) + saved := binaryArg0 + binaryArg0 = "dbx-not-on-path" + t.Cleanup(func() { binaryArg0 = saved }) _, err := newAddTestCmd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") require.Error(t, err) @@ -781,3 +985,188 @@ func TestMcpAddRefusesBinaryNotNamedDatabricks(t *testing.T) { assert.Contains(t, err.Error(), "dbx-not-on-path is not the databricks binary") assert.NoFileExists(t, filepath.Join(home, ".claude.json")) } + +// TestMcpAddRefusesStoredNameHeldByOtherBinding checks that a server name stored for this profile is refused, before any request, when an agent +// holds that name for another profile's managed server. +func TestMcpAddRefusesStoredNameHeldByOtherBinding(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n[dev-foo]\nhost = https://adb-456.test\nauth_type = pat\ntoken = dapi-dev\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: server.URL, + }}})) + other := `{"mcpServers":{"github-logfood":{"type":"stdio","command":"databricks","args":["aitools","mcp","serve","--profile","dev-foo","--service","system.ai.github","--agent","claude-code"]}}}` + claudeFile := filepath.Join(home, ".claude.json") + require.NoError(t, os.WriteFile(claudeFile, []byte(other), 0o600)) + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, categoryNameConflict, errorCategory(err)) + assert.ErrorContains(t, err, "github-logfood") + got, err := os.ReadFile(claudeFile) + require.NoError(t, err) + assert.Equal(t, other, string(got)) +} + +// TestErrorCategoryKeepsCauseOfUncategorizedBindingError checks that a binding failure with no category of its own does not hide the +// cause it wraps, so a filesystem or permission error is still classified. The cause is built here: no binding path +// wraps an OS error with %w that the SDK keeps. +func TestErrorCategoryKeepsCauseOfUncategorizedBindingError(t *testing.T) { + cause := &fs.PathError{Op: "open", Path: "cfg", Err: fs.ErrPermission} + err := fmt.Errorf("MCP server github-logfood: %w", errors.Join(&mcp.BindError{Category: mcp.BindUncategorized}, cause)) + assert.Equal(t, protos.AitoolsErrorCategoryPermissionDenied, errorCategory(err)) + + // Without a cause the failure stays uncategorized. + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(&mcp.BindError{Category: mcp.BindUncategorized})) +} + +// TestMcpAddNamesResolvedConfigFileWhenDotDotFollowsSymlink checks that the config file is named as it resolves, so a path that goes through a symlink +// and then ".." is read from the file that the OS reads. +func TestMcpAddNamesResolvedConfigFileWhenDotDotFollowsSymlink(t *testing.T) { + base := t.TempDir() + withHome(t) + real := filepath.Join(base, "real") + require.NoError(t, os.MkdirAll(filepath.Join(real, "sub"), 0o700)) + cfg := filepath.Join(real, "cfg") + require.NoError(t, os.WriteFile(cfg, []byte("[other]\nhost = https://other.test\n"), 0o600)) + if err := os.Symlink(filepath.Join(real, "sub"), filepath.Join(base, "link")); err != nil { + t.Skipf("cannot create symlink: %v", err) + } + // The variable is written as typed: filepath.Join would clean the ".." away. + t.Setenv("DATABRICKS_CONFIG_FILE", base+string(filepath.Separator)+"link"+string(filepath.Separator)+".."+string(filepath.Separator)+"cfg") + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + resolved, evalErr := filepath.EvalSymlinks(cfg) + require.NoError(t, evalErr) + assert.Equal(t, "profile logfood not found in "+filepath.ToSlash(resolved), err.Error()) +} + +// TestMcpAddRefusesNameThatSanitizesToNothing checks that a service whose generated name is empty is refused before any request, and the message +// says to pass --name. +func TestMcpAddRefusesNameThatSanitizesToNothing(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[数据]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + err := runAdd(t, "--profile", "数据", "--names", "数据", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + assert.ErrorContains(t, err, "pass --name") + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// TestMcpAddNameNeedsOneService checks that --name is refused with two services, and is checked before any request. +func TestMcpAddNameNeedsOneService(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + err := runAdd(t, "--profile", "logfood", "--names", "github,genie_one_mcp", "--name", "custom", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, "--name needs exactly one service in --names", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) +} + +// TestMcpAddCountsDistinctServicesForName checks that a leaf and the full name of the same service are one service, so --name is accepted with them. +func TestMcpAddCountsDistinctServicesForName(t *testing.T) { + home := withHome(t) + server := rejectingServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + err := runAdd(t, "--profile", "logfood", "--names", "github,system.ai.github", "--name", "custom", "--agents", "claude-code") + require.Error(t, err) + // The request is the first thing that can fail; the flags were accepted. + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(err)) +} + +// TestMcpAddTakesLocationFromFullNames checks that full names that all name one schema are resolved there when --location is not set. +func TestMcpAddTakesLocationFromFullNames(t *testing.T) { + home := withHome(t) + server := rejectingServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + err := runAdd(t, "--profile", "logfood", "--names", "main.tools.github", "--agents", "claude-code") + require.Error(t, err) + // The schema was accepted, so the first request was made. + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(err)) +} + +// TestMcpAddAgreesWithBind checks that add refuses the same bindings that Bind refuses, with the same message where the failure is about the server. +func TestMcpAddAgreesWithBind(t *testing.T) { + const service = "system.ai.github" + cases := []struct { + name string + setup func(t *testing.T, home, host string) + // sameMessage is false when add reports the profile as missing with its own wording. + sameMessage bool + }{ + { + name: "host changed", + setup: func(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: "https://adb-123.test"}) + }, + sameMessage: true, + }, + { + name: "workspace changed on the same host", + setup: func(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: host, WorkspaceID: "111"}) + }, + sameMessage: true, + }, + { + name: "entry pinned to another config file", + setup: func(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: host, ConfigFile: filepath.Join(t.TempDir(), "other")}) + }, + sameMessage: true, + }, + { + name: "profile gone while an entry is bound to it", + setup: func(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[other]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-other\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: host}) + }, + sameMessage: true, + }, + { + name: "profile gone with no entry", + setup: func(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[other]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-other\n") + }, + sameMessage: false, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + tc.setup(t, home, server.URL) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + var stderr bytes.Buffer + _, _, bindErr := mcp.Bind(t.Context(), mcp.BindOptions{Profile: "logfood", Service: service, State: state, Stderr: &stderr}) + require.Error(t, bindErr) + + addErr := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, addErr) + assert.Equal(t, errorCategory(bindErr), errorCategory(addErr)) + if tc.sameMessage { + assert.Equal(t, bindErr.Error(), addErr.Error()) + } + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) + }) + } +} + +// saveEntry saves the state with one entry. +func saveEntry(t *testing.T, entry mcp.Entry) { + t.Helper() + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{entry}})) +} diff --git a/cmd/aitools/mcp_binary.go b/cmd/aitools/mcp_binary.go index b1d8d926373..1b5f2659d6a 100644 --- a/cmd/aitools/mcp_binary.go +++ b/cmd/aitools/mcp_binary.go @@ -4,25 +4,31 @@ import ( "os" "os/exec" "path/filepath" + "strings" + + "github.com/databricks/cli/libs/aitools/mcp" ) +// binaryArg0 is the name the CLI was started with. It is a variable so that tests can set it. +var binaryArg0 = os.Args[0] + // mcpBinaryPath returns the command that agents run for MCP servers, and whether it is absolute. // The invoked path is used when it is absolute, so a symlink such as Homebrew's stays the // stable path; it is never resolved. func mcpBinaryPath() (string, bool) { - return binaryPathFor(os.Args[0], exec.LookPath) + return binaryPathFor(binaryArg0, exec.LookPath) } -// binaryPathFor resolves arg0 without touching the file system beyond lookPath. An absolute arg0 is kept. -// A relative arg0 with a directory, such as ./databricks, is made absolute, and its symlinks are not -// resolved. A bare name is looked up on PATH. When none of these gives an absolute path, it returns arg0 -// as the bare name, which the agent must find on PATH. The caller checks that the name is databricks. +// binaryPathFor returns the command for arg0, and whether it is absolute. A relative arg0 with a directory is joined +// to the working directory as written, so a symlink followed by ".." resolves as the OS resolves it. A bare name is +// looked up on PATH, and a name that is not found is returned as is, for the agent to find on PATH. func binaryPathFor(arg0 string, lookPath func(string) (string, error)) (string, bool) { if filepath.IsAbs(arg0) { return arg0, true } if filepath.Base(arg0) != arg0 { - abs, err := filepath.Abs(arg0) + // A leading "./" names the working directory, which the join already gives. + abs, err := mcp.AbsoluteAsWritten(strings.TrimPrefix(arg0, "."+string(filepath.Separator))) if err != nil { return arg0, false } diff --git a/cmd/aitools/mcp_binary_test.go b/cmd/aitools/mcp_binary_test.go index ca7238af64d..c9451e70b0d 100644 --- a/cmd/aitools/mcp_binary_test.go +++ b/cmd/aitools/mcp_binary_test.go @@ -2,6 +2,7 @@ package aitools import ( "errors" + "os" "path/filepath" "testing" @@ -10,6 +11,8 @@ import ( ) func TestBinaryPathFor(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) absolute := filepath.Join(t.TempDir(), "bin", "databricks") onPath := filepath.Join(t.TempDir(), "databricks") relativeOnPath := filepath.Join("bin", "databricks") @@ -45,7 +48,15 @@ func TestBinaryPathFor(t *testing.T) { name: "dot-relative arg0 with a directory is made absolute", arg0: "." + string(filepath.Separator) + "databricks", lookPath: found, - wantPath: mustAbs(t, "."+string(filepath.Separator)+"databricks"), + wantPath: wd + string(filepath.Separator) + "databricks", + wantAbs: true, + }, + { + // The OS reads "link/.." through the link's target, so the path must not be cleaned before the symlink is followed. + name: "relative arg0 through a symlink keeps its dot-dot", + arg0: "link" + string(filepath.Separator) + ".." + string(filepath.Separator) + "databricks", + lookPath: found, + wantPath: wd + string(filepath.Separator) + "link" + string(filepath.Separator) + ".." + string(filepath.Separator) + "databricks", wantAbs: true, }, { diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 1c38b6b3c59..4fa093826fc 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -63,7 +63,9 @@ 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, such as CODEX_HOME. + // 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 // pluginInstalls reads the databricks plugin's install records from the @@ -97,9 +99,8 @@ func (a *Agent) Detected(ctx context.Context) bool { return err == nil && HasDatabricksSkillsIn(skillsDir) } -// InstalledForMCP reports whether the agent is installed for MCP servers. It is installed when Detected -// reports it, when its CLI binary is on PATH, when its MCP config file exists, or when mcpHomeEnv names a -// directory that holds the MCP config file and that directory exists. +// InstalledForMCP reports whether the agent is installed for MCP servers: Detected or HasBinary, or the MCP +// config file or, with mcpHomeEnv, its directory exists. func (a *Agent) InstalledForMCP(ctx context.Context) (bool, error) { if a.Detected(ctx) || a.HasBinary(ctx) { return true, nil @@ -261,7 +262,7 @@ var Registry = []*Agent{ Root: "mcp_servers", Entry: stdioEntry, }, - mcpHomeEnv: "CODEX_HOME", + mcpHomeEnv: codexHomeEnv, }, { Name: NameOpenCode, @@ -290,7 +291,7 @@ var Registry = []*Agent{ Root: mcpServersKey, Entry: copilotMCPEntry, }, - mcpHomeEnv: "COPILOT_HOME", + mcpHomeEnv: copilotHomeEnv, }, { Name: NameAntigravity, @@ -481,10 +482,16 @@ 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. +const ( + codexHomeEnv = "CODEX_HOME" + copilotHomeEnv = "COPILOT_HOME" +) + // codexMCPPath returns Codex's config.toml, which honors CODEX_HOME. // See https://learn.chatgpt.com/docs/config-file/config-advanced. func codexMCPPath(ctx context.Context) (string, error) { - if home := env.Get(ctx, "CODEX_HOME"); home != "" { + if home := env.Get(ctx, codexHomeEnv); home != "" { return filepath.Join(home, "config.toml"), nil } return homeSubdir(".codex", "config.toml")(ctx) @@ -551,7 +558,7 @@ func openCodeMCPEntry(cmd string, args []string, vars map[string]string) map[str // copilotMCPPath returns Copilot CLI's user config. COPILOT_HOME replaces ~/.copilot. // See https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference. func copilotMCPPath(ctx context.Context) (string, error) { - if home := env.Get(ctx, "COPILOT_HOME"); home != "" { + if home := env.Get(ctx, copilotHomeEnv); home != "" { return filepath.Join(home, "mcp-config.json"), nil } return homeSubdir(".copilot", "mcp-config.json")(ctx) diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 992ba1a7e65..21890e119a8 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -802,7 +802,6 @@ func TestCheckBindingMatchesBind(t *testing.T) { _, err := bindingTestCheck(t, state) requireBindError(t, err, mcp.BindProfileHostChanged) assert.Equal(t, "MCP server github-logfood was set up for https://adb-123.test, but profile \"logfood\" now points at "+server.URL+". Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) - } func TestCheckBindingPassesWhenEntryMatchesOrIsAbsent(t *testing.T) { @@ -835,7 +834,7 @@ func TestCheckBindingRefusesEntryPinnedToOtherConfigFile(t *testing.T) { requireBindError(t, err, mcp.BindProfileHostChanged) } -// TestCheckBindingMatchesEntryByConfigFileWhenOtherEntryComesFirst returns the entry of the config file in use, even when an +// TestCheckBindingMatchesEntryByConfigFileWhenOtherEntryComesFirst checks that CheckBinding returns the entry of the config file in use, even when an // entry for the same service and profile in another file comes first in the state. func TestCheckBindingMatchesEntryByConfigFileWhenOtherEntryComesFirst(t *testing.T) { cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) From 6aa99af0fc050a7bc3f1870fcf42ecfef4b52dd1 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:17:57 +0000 Subject: [PATCH 12/20] Check the profile of a service without an entry in CheckBinding CheckBinding returned no error for a missing profile or one with no host when no state entry existed. Bind checked that case itself, and aitools mcp add had its own check with other wording. The check moves into CheckBinding, so Bind and add refuse the same profiles with the same error, and an account-level profile with no host is refused before any request. Co-authored-by: Isaac --- libs/aitools/mcp/binding.go | 19 +++++++++---------- libs/aitools/mcp/binding_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index ff8e9bde181..7978fc33fdc 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -87,12 +87,6 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) if err != nil { return nil, nil, err } - if entry == nil { - // Without an entry the profile is still needed, under the service's name. - if _, err := profileFor(opts.Service, opts.Profile, configFile, nil); err != nil { - return nil, nil, err - } - } cfg, err := BoundConfig(opts.Profile, configFile) if err != nil { @@ -140,12 +134,17 @@ func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName stri } // CheckBinding returns the state entry for service and profileName bound to configFile, the config file the caller -// resolved with ResolveConfigFile, or nil when there is none. With an entry, it returns the error Bind returns when the -// profile no longer matches the entry: the profile is gone or has no host, or its host or workspace changed. It reads the -// config files only and makes no request, so a caller can refuse before it sends a token to the profile's host. +// resolved with ResolveConfigFile, or nil when there is none. It returns the error Bind returns when the profile does not +// match: the profile is gone or has no host, or with an entry, its host or workspace changed. The profile is checked +// without an entry too, under the service's name. It reads the config files only and makes no request, so a caller can +// refuse before it sends a token to the profile's host. func CheckBinding(ctx context.Context, state *State, service, profileName, configFile string) (*Entry, error) { entry, err := FindEntry(ctx, state, service, profileName, configFile) - if err != nil || entry == nil { + if err != nil { + return nil, err + } + if entry == nil { + _, err := profileFor(service, profileName, configFile, nil) return nil, err } p, err := profileFor(entry.Name, profileName, configFile, entry) diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 21890e119a8..e6406f607e2 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -849,3 +849,31 @@ func TestCheckBindingMatchesEntryByConfigFileWhenOtherEntryComesFirst(t *testing require.NotNil(t, entry) assert.Equal(t, "github-default", entry.Name) } + +// TestCheckBindingChecksProfileWithoutEntry checks that a profile that is missing, or has no host, is refused when no entry +// exists either, with the error Bind returns for it. An account-level profile has no workspace, so it has no host. +func TestCheckBindingChecksProfileWithoutEntry(t *testing.T) { + tests := []struct { + name string + contents string + problem string + }{ + {name: "missing", contents: "[other]\nhost = https://other.test\n", problem: "which no longer exists in "}, + {name: "account profile without host", contents: "[" + bindingTestProfile + "]\naccount_id = 123\n", problem: "which has no host in "}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + bindingTestConfig(t, tt.contents) + + _, err := bindingTestCheck(t, &mcp.State{}) + requireBindError(t, err, mcp.BindProfileNotFound) + assert.Contains(t, err.Error(), tt.problem) + + _, _, bindErr := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, Service: bindingTestService, State: &mcp.State{}, Stderr: io.Discard, + }) + require.Error(t, bindErr) + assert.Equal(t, bindErr.Error(), err.Error()) + }) + } +} From 3f53557bf803804868fa99b922aa0a3b6b702f84 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:18:08 +0000 Subject: [PATCH 13/20] Define the entry env keys once in mcpconfig EntryEnv and the agent writers each listed env and environment. The list is now mcpconfig.EnvKeys, and EntryEnv has a direct test. Co-authored-by: Isaac --- libs/aitools/mcp/agentconfig/writer.go | 6 +----- libs/aitools/mcpconfig/spec.go | 7 +++++-- libs/aitools/mcpconfig/spec_test.go | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 6c0b27823c7..74f632a99ea 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -62,10 +62,6 @@ type document interface { // builder writes only some of them (see ownedKeys). var launchKeys = []string{"command", "args", "env", "environment", "type"} -// envKeys are the entry keys that hold environment variables: env, and -// environment in OpenCode. -var envKeys = []string{"env", "environment"} - // ownedKeys returns the launch keys that the agent's builder writes. Upsert // updates these keys and keeps every other key of the entry. The probe passes a // variable, since a builder writes its env key only when it has variables. @@ -321,7 +317,7 @@ func keepUserKeys(current, want map[string]any, keys []string) (map[string]any, out := maps.Clone(current) for _, key := range keys { value, ok := want[key] - if slices.Contains(envKeys, key) { + if slices.Contains(mcpconfig.EnvKeys, key) { var err error value, ok, err = mergeEnv(current[key], value, ok) if err != nil { diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 0ee3eb63dd3..f9387bb8799 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -77,10 +77,13 @@ const ( ProfileFlag = "--profile" ) -// EntryEnv returns the environment variables of an entry: env for most agents, and environment for OpenCode. It returns +// EnvKeys are the entry keys that hold environment variables: env for most agents, and environment for OpenCode. +var EnvKeys = []string{"env", "environment"} + +// EntryEnv returns the environment variables of an entry, under the first of EnvKeys that holds an object. It returns // nil when the entry has none. func EntryEnv(entry map[string]any) map[string]any { - for _, key := range []string{"env", "environment"} { + for _, key := range EnvKeys { if vars, ok := entry[key].(map[string]any); ok { return vars } diff --git a/libs/aitools/mcpconfig/spec_test.go b/libs/aitools/mcpconfig/spec_test.go index c8483918cde..47cca0577df 100644 --- a/libs/aitools/mcpconfig/spec_test.go +++ b/libs/aitools/mcpconfig/spec_test.go @@ -73,3 +73,24 @@ func TestValidServerName(t *testing.T) { assert.False(t, ValidServerName(name), name) } } + +// TestEntryEnv checks which object holds the environment of an entry: env, or environment for OpenCode. +func TestEntryEnv(t *testing.T) { + vars := map[string]any{"DATABRICKS_CONFIG_FILE": "/cfg"} + tests := []struct { + name string + entry map[string]any + want map[string]any + }{ + {name: "env", entry: map[string]any{"env": vars}, want: vars}, + {name: "environment", entry: map[string]any{"environment": vars}, want: vars}, + {name: "env is read before environment", entry: map[string]any{"env": vars, "environment": map[string]any{"other": "x"}}, want: vars}, + {name: "not an object is absent", entry: map[string]any{"env": "DATABRICKS_CONFIG_FILE=/cfg"}, want: nil}, + {name: "no environment", entry: map[string]any{"command": "databricks"}, want: nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, EntryEnv(tt.entry)) + }) + } +} From 7f3a1f7f422e5fd3f1590c7abe297b98cfb804f3 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:18:23 +0000 Subject: [PATCH 14/20] Count an agent with an MCP home variable by that directory only InstalledForMCP let a leftover agent directory (~/.codex) count as installed when CODEX_HOME pointed at a directory that did not exist, so the MCP write created it. With the variable set, the directory it names is the only signal. The InstalledForMCP test moves from detect_test.go to agents_test.go. Co-authored-by: Isaac --- libs/aitools/agents/agents.go | 20 ++++--- libs/aitools/agents/agents_test.go | 91 ++++++++++++++++++++++++++++++ libs/aitools/agents/detect_test.go | 83 --------------------------- 3 files changed, 102 insertions(+), 92 deletions(-) diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 4fa093826fc..48c45758ec7 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -99,9 +99,17 @@ func (a *Agent) Detected(ctx context.Context) bool { return err == nil && HasDatabricksSkillsIn(skillsDir) } -// InstalledForMCP reports whether the agent is installed for MCP servers: Detected or HasBinary, or the MCP -// config file or, with mcpHomeEnv, its directory exists. +// 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. func (a *Agent) InstalledForMCP(ctx context.Context) (bool, error) { + if a.mcpHomeEnv != "" && env.Get(ctx, a.mcpHomeEnv) != "" { + path, err := a.MCP.Path(ctx) + if err != nil { + return false, err + } + return linkExists(filepath.Dir(path)) + } if a.Detected(ctx) || a.HasBinary(ctx) { return true, nil } @@ -112,13 +120,7 @@ func (a *Agent) InstalledForMCP(ctx context.Context) (bool, error) { if err != nil { return false, err } - if exists, err := linkExists(path); err != nil || exists { - return exists, err - } - if a.mcpHomeEnv == "" || env.Get(ctx, a.mcpHomeEnv) == "" { - return false, nil - } - return linkExists(filepath.Dir(path)) + return linkExists(path) } // SkillsDir returns the full path to the agent's skills directory. diff --git a/libs/aitools/agents/agents_test.go b/libs/aitools/agents/agents_test.go index 4fc4e43026c..7ab21aea40f 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -214,3 +214,94 @@ func TestOpenCodeMCPPathDanglingJSONCLink(t *testing.T) { require.NoError(t, err) assert.Equal(t, jsonc, path) } + +// TestInstalledForMCP covers the signals that make an agent installed for MCP servers, with the +// agent's own paths under a temporary home and no agent binary on PATH unless a case says so. +func TestInstalledForMCP(t *testing.T) { + ctx := t.Context() + setup := func(t *testing.T) string { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + for _, name := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA"} { + t.Setenv(name, "") + } + stubLookPath(t) + return home + } + installed := func(t *testing.T, name string) bool { + t.Helper() + a := ByName(name) + require.NotNil(t, a, name) + got, err := a.InstalledForMCP(ctx) + require.NoError(t, err) + return got + } + + t.Run("nothing present is not installed", func(t *testing.T) { + setup(t) + for _, a := range MCPAgents() { + assert.False(t, installed(t, a.Name), a.Name) + } + }) + + t.Run("config dir is installed", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".cursor"), 0o755)) + assert.True(t, installed(t, NameCursor)) + }) + + t.Run("CLI on PATH alone is installed", func(t *testing.T) { + setup(t) + stubLookPath(t, "codex") + assert.True(t, installed(t, NameCodex)) + assert.False(t, installed(t, NameCopilot)) + }) + + t.Run("MCP config file alone is installed", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte("{}"), 0o600)) + assert.True(t, installed(t, NameClaudeCode)) + }) + + t.Run("CODEX_HOME that exists counts for Codex", func(t *testing.T) { + setup(t) + codexHome := t.TempDir() + t.Setenv("CODEX_HOME", codexHome) + assert.True(t, installed(t, NameCodex)) + + t.Setenv("CODEX_HOME", filepath.Join(t.TempDir(), "missing")) + assert.False(t, installed(t, NameCodex)) + }) + + t.Run("CODEX_HOME that is missing is not installed although ~/.codex exists", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".codex"), 0o755)) + t.Setenv("CODEX_HOME", filepath.Join(home, "codex-home")) + assert.False(t, installed(t, NameCodex)) + assert.NoDirExists(t, filepath.Join(home, "codex-home")) + }) + + t.Run("COPILOT_HOME that exists counts for Copilot", func(t *testing.T) { + setup(t) + t.Setenv("COPILOT_HOME", t.TempDir()) + assert.True(t, installed(t, NameCopilot)) + }) + + t.Run("Gemini is not installed because Antigravity made ~/.gemini", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) + assert.False(t, installed(t, NameGemini)) + assert.True(t, installed(t, NameAntigravity)) + + require.NoError(t, os.WriteFile(filepath.Join(home, ".gemini", "projects.json"), []byte("{}"), 0o644)) + assert.True(t, installed(t, NameGemini)) + }) + + t.Run("Antigravity is installed from its own directory, without ~/.gemini/config", func(t *testing.T) { + home := setup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) + assert.NoDirExists(t, filepath.Join(home, ".gemini", "config")) + assert.True(t, installed(t, NameAntigravity)) + }) +} diff --git a/libs/aitools/agents/detect_test.go b/libs/aitools/agents/detect_test.go index a9071549e93..dfb7a8c6ec1 100644 --- a/libs/aitools/agents/detect_test.go +++ b/libs/aitools/agents/detect_test.go @@ -399,86 +399,3 @@ func TestOpenCodeConfigDir(t *testing.T) { assert.Equal(t, filepath.Join(home, ".config", "opencode"), dir) }) } - -// TestInstalledForMCP covers the signals that make an agent installed for MCP servers, with the -// agent's own paths under a temporary home and no agent binary on PATH unless a case says so. -func TestInstalledForMCP(t *testing.T) { - ctx := t.Context() - setup := func(t *testing.T) string { - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - for _, name := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME", "COPILOT_HOME", "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA"} { - t.Setenv(name, "") - } - stubLookPath(t) - return home - } - installed := func(t *testing.T, name string) bool { - t.Helper() - a := ByName(name) - require.NotNil(t, a, name) - got, err := a.InstalledForMCP(ctx) - require.NoError(t, err) - return got - } - - t.Run("nothing present is not installed", func(t *testing.T) { - setup(t) - for _, a := range MCPAgents() { - assert.False(t, installed(t, a.Name), a.Name) - } - }) - - t.Run("config dir is installed", func(t *testing.T) { - home := setup(t) - require.NoError(t, os.MkdirAll(filepath.Join(home, ".cursor"), 0o755)) - assert.True(t, installed(t, NameCursor)) - }) - - t.Run("CLI on PATH alone is installed", func(t *testing.T) { - setup(t) - stubLookPath(t, "codex") - assert.True(t, installed(t, NameCodex)) - assert.False(t, installed(t, NameCopilot)) - }) - - t.Run("MCP config file alone is installed", func(t *testing.T) { - home := setup(t) - require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte("{}"), 0o600)) - assert.True(t, installed(t, NameClaudeCode)) - }) - - t.Run("CODEX_HOME that exists counts for Codex", func(t *testing.T) { - setup(t) - codexHome := t.TempDir() - t.Setenv("CODEX_HOME", codexHome) - assert.True(t, installed(t, NameCodex)) - - t.Setenv("CODEX_HOME", filepath.Join(t.TempDir(), "missing")) - assert.False(t, installed(t, NameCodex)) - }) - - t.Run("COPILOT_HOME that exists counts for Copilot", func(t *testing.T) { - setup(t) - t.Setenv("COPILOT_HOME", t.TempDir()) - assert.True(t, installed(t, NameCopilot)) - }) - - t.Run("Gemini is not installed because Antigravity made ~/.gemini", func(t *testing.T) { - home := setup(t) - require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) - assert.False(t, installed(t, NameGemini)) - assert.True(t, installed(t, NameAntigravity)) - - require.NoError(t, os.WriteFile(filepath.Join(home, ".gemini", "projects.json"), []byte("{}"), 0o644)) - assert.True(t, installed(t, NameGemini)) - }) - - t.Run("Antigravity is installed from its own directory, without ~/.gemini/config", func(t *testing.T) { - home := setup(t) - require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) - assert.NoDirExists(t, filepath.Join(home, ".gemini", "config")) - assert.True(t, installed(t, NameAntigravity)) - }) -} From 7e002632ad50dc6bc896e80b78b8bff21a72c86f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:23:25 +0000 Subject: [PATCH 15/20] Name the --agent flag next to the other entry flags The serve command line that aitools mcp add writes repeated the --agent literal. Co-authored-by: Isaac --- libs/aitools/mcpconfig/spec.go | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index f9387bb8799..cfd7e4d6d4e 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -75,6 +75,7 @@ func Argv(entry map[string]any) ([]string, bool) { const ( ServiceFlag = "--service" ProfileFlag = "--profile" + AgentFlag = "--agent" ) // EnvKeys are the entry keys that hold environment variables: env for most agents, and environment for OpenCode. From a90fa2f2275d83093cb1993cc0e25f62c7bea90f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:23:26 +0000 Subject: [PATCH 16/20] Refuse name conflicts before any request and keep every failure Planning now reads each named agent's entries. A stored or --name server name that another state entry holds, or that an agent holds as an entry this CLI did not write, is refused before any request (unless --force). A conflict in an agent that was only detected fails that agent for that server, and the run continues with the other agents. A run that no agent can take refuses before any request. Recording writes each server on its own and saves the state for the ones that recorded, returning the joined errors. Agent failures keep every distinct failure, not the first per agent. Service names with an empty part are reported as malformed, not as a location error. The profile check is the shared one in CheckBinding, so requireProfile is gone. The help text says leaf names resolve in --location. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 266 ++++++++++++------- cmd/aitools/mcp_add_test.go | 458 +++++++++++++++++++++++---------- cmd/aitools/mcp_binary_test.go | 58 +++-- 3 files changed, 522 insertions(+), 260 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index f7883f83993..caccd231998 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -18,7 +18,6 @@ import ( "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdio" - "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" @@ -71,12 +70,12 @@ type addAgentOutput struct { err error } -// addPlan is one service to install for the profile. entry is the state record it updates, -// nil for a new install. +// addPlan is one service to install for the profile. failed holds the agents that cannot take the server, with the +// reason; they are not written. type addPlan struct { service mcp.Service name string - entry *mcp.Entry + failed map[*agents.Agent]error } // addRequest is what one `mcp add` run applies to every server. @@ -86,7 +85,6 @@ type addRequest struct { profile string host string workspaceID string - location string agents []*agents.Agent explicit bool force bool @@ -122,9 +120,9 @@ func NewMcpAddCmd() *cobra.Command { Long: `Install Databricks managed MCP servers into coding agents, bound to a Databricks profile. Each server runs through 'databricks aitools mcp serve' with that profile's credentials. -Servers come from --names: full names such as system.ai.github, or leaf names resolved in ---location (default: the schema of the full names, else system.ai). Agents come from --agents. -Without it, every detected MCP-capable agent is used. +Servers come from --names: full names such as system.ai.github, or leaf names such as github. Leaf names +resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share. +Agents come from --agents. Without it, every detected MCP-capable agent is used. Interactive selection is not available yet: in a terminal, pass both --names and --agents. With --output json, both are required. @@ -162,7 +160,7 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), } cmd.Flags().StringVar(&f.names, "names", "", "Services to install: full names (system.ai.github) or leaf names (github), comma-separated") - cmd.Flags().StringVar(&f.location, "location", "", "Schema to resolve leaf names in, as . (default: the schema of the full names in --names, else system.ai)") + cmd.Flags().StringVar(&f.location, "location", "", "Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share)") cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to install for, comma-separated (default: every detected MCP-capable agent)") cmd.Flags().StringVar(&f.name, "name", "", "Server name to use instead of the generated one; only with exactly one service") cmd.Flags().BoolVar(&f.force, "force", false, "Overwrite an existing server with the same name that this CLI did not write") @@ -171,8 +169,9 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), // runMcpAdd installs the requested servers and fills out. Every refusal that needs no request comes before // the first one: the flags, the agents, the binary name, the binding of each service that a recorded entry -// holds, and the server names. A failure of an agent named with --agents is returned as an agentErrors; -// detected agents only report theirs. +// holds, and the server names. A server that no agent can take is refused the same way, unless a detected agent +// can take it. A failure of an agent named with --agents is returned as an agentErrors; detected agents only report +// theirs. A server that reaches no agent fails the run. func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode bool, out *addOutput) error { names := splitAndTrim(f.names) agentNames := splitAndTrim(f.agents) @@ -229,13 +228,21 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode return err } } - if err := requireProfile(ctx, profileName); err != nil { - return err + in := planInput{ + binding: addBinding{profile: profileName, configFile: configFile}, + nameFlag: f.name, + force: f.force, + explicit: explicit, + targets: targets, + held: readAgentEntries(ctx, targets), } - plans, err := planAdds(ctx, f.name, addBinding{profile: profileName, configFile: configFile}, state, fullNames, targets, readAgentEntries(ctx, targets)) + plans, err := planAdds(ctx, in, state, fullNames) if err != nil { return err } + if err := planningRefusals(plans, targets); err != nil { + return err + } w, err := profileClient(ctx, profileName, configFile) if err != nil { @@ -264,7 +271,6 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode profile: profileName, host: w.Config.Host, workspaceID: workspaceID, - location: location, agents: targets, explicit: explicit, force: f.force, @@ -339,24 +345,6 @@ func validateLocation(location string) error { return nil } -// requireProfile fails when the named profile is not in the Databricks config file, or has no host. -func requireProfile(ctx context.Context, name string) error { - profiler := profile.GetProfiler(ctx) - profiles, err := profiler.LoadProfiles(ctx, profile.WithName(name)) - if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { - return err - } - // A profile with no host names no workspace, so it is missing for a server as well. - if len(profiles) > 0 && profiles[0].Host != "" { - return nil - } - path, err := profiler.GetPath(ctx) - if err != nil { - return err - } - return failure(protos.AitoolsErrorCategory(mcp.BindProfileNotFound), nil, "profile %s not found in %s", name, filepath.ToSlash(path)) -} - // addConfigFile returns the config file that the server binds to, resolved as Bind resolves it, and the // file to pin into the agent entries: the same file when DATABRICKS_CONFIG_FILE is set, "" otherwise. func addConfigFile(ctx context.Context) (path, pinned string, err error) { @@ -494,10 +482,10 @@ func locationOf(explicit string, names []string) (string, error) { leaves := false for _, name := range names { parts := strings.Split(strings.ToLower(name), ".") - switch len(parts) { - case 1: + switch { + case len(parts) == 1: leaves = true - case 3: + case isFullName(parts): if schema := parts[0] + "." + parts[1]; !slices.Contains(schemas, schema) { schemas = append(schemas, schema) } @@ -522,10 +510,10 @@ func serviceNames(location string, names []string) ([]string, error) { for _, name := range names { name = strings.ToLower(name) var fullName string - switch parts := strings.Split(name, "."); len(parts) { - case 1: + switch parts := strings.Split(name, "."); { + case len(parts) == 1: fullName = location + "." + name - case 3: + case isFullName(parts): if parts[0]+"."+parts[1] != location { return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is not in %s; pass --location to match its schema", name, location) } @@ -540,6 +528,11 @@ func serviceNames(location string, names []string) ([]string, error) { return full, nil } +// isFullName reports whether parts are the catalog, schema and name of a full service name, each one non-empty. +func isFullName(parts []string) bool { + return len(parts) == 3 && parts[0] != "" && parts[1] != "" && parts[2] != "" +} + // pickServices returns the discovered service of each full name, in the same order. Unity Catalog names are // case-insensitive, so the comparison is too. func pickServices(location string, names []string, services []mcp.Service) ([]mcp.Service, error) { @@ -547,6 +540,9 @@ func pickServices(location string, names []string, services []mcp.Service) ([]mc for _, name := range names { i := slices.IndexFunc(services, func(s mcp.Service) bool { return strings.EqualFold(s.FullName, name) }) if i < 0 { + if len(services) == 0 { + return nil, failure(categoryServiceNotFound, nil, "%s is not an MCP service in %s, which has none", name, location) + } available := make([]string, 0, len(services)) for _, s := range services { available = append(available, s.FullName) @@ -580,11 +576,23 @@ func readAgentEntries(ctx context.Context, targets []*agents.Agent) agentEntries return held } +// planInput is what planAdds plans the servers of one run against. +type planInput struct { + binding addBinding + nameFlag string + force bool + // explicit is set when the agents were named with --agents. A name conflict in a named agent refuses the run. A + // conflict in a detected agent fails only that agent for that server. + explicit bool + targets []*agents.Agent + held agentEntries +} + // planAdds chooses the server name of each service before anything is written, and refuses what would overwrite // another server. A service already installed for the binding keeps its name, and so does a name that an agent entry // holds for the same binding when the state has none. A new name avoids the names of other state entries and of every // agent entry, so that a name this CLI wrote for another profile is never overwritten. -func planAdds(ctx context.Context, nameFlag string, b addBinding, state *mcp.State, fullNames []string, targets []*agents.Agent, held agentEntries) ([]addPlan, error) { +func planAdds(ctx context.Context, in planInput, state *mcp.State, fullNames []string) ([]addPlan, error) { taken := map[string]bool{} for i := range state.Servers { taken[state.Servers[i].Name] = true @@ -592,80 +600,83 @@ func planAdds(ctx context.Context, nameFlag string, b addBinding, state *mcp.Sta plans := make([]addPlan, 0, len(fullNames)) for _, fullName := range fullNames { - entry, err := mcp.FindEntry(ctx, state, fullName, b.profile, b.configFile) + entry, err := mcp.FindEntry(ctx, state, fullName, in.binding.profile, in.binding.configFile) if err != nil { return nil, err } - name, err := chooseName(ctx, nameFlag, b, state, fullName, entry, taken, targets, held) + name, failed, err := chooseName(ctx, in, state, fullName, entry, taken) if err != nil { return nil, err } taken[name] = true - plans = append(plans, addPlan{service: mcp.Service{FullName: fullName}, name: name, entry: entry}) + plans = append(plans, addPlan{service: mcp.Service{FullName: fullName}, name: name, failed: failed}) } return plans, nil } -// chooseName returns the server name for fullName. entry is its state entry, nil for a new install. The name it holds is -// kept, and then --name is used only when no other server holds it. Otherwise the name is generated. -func chooseName(ctx context.Context, nameFlag string, b addBinding, state *mcp.State, fullName string, entry *mcp.Entry, taken map[string]bool, targets []*agents.Agent, held agentEntries) (string, error) { +// chooseName returns the server name for fullName, and the target agents that cannot take it. entry is the server's +// state entry, nil for a new install. The name it holds is kept. Otherwise --name is used when no other server holds +// it, and the name is generated. +func chooseName(ctx context.Context, in planInput, state *mcp.State, fullName string, entry *mcp.Entry, taken map[string]bool) (string, map[*agents.Agent]error, error) { + b := in.binding stored := "" if entry != nil { stored = entry.Name } else { - owned, err := ownedNames(ctx, held, targets, fullName, b) + owned, err := ownedNames(ctx, in.held, in.targets, fullName, b) if err != nil { - return "", err + return "", nil, err } switch len(owned) { case 0: case 1: stored = owned[0] default: - return "", failure(categoryNameConflict, nil, "%s is installed on profile %s under more than one name in the agents (%s); remove the extra entries", fullName, b.profile, strings.Join(owned, ", ")) + return "", nil, failure(categoryNameConflict, nil, "%s is installed on profile %s under more than one name in the agents (%s); remove the extra entries", fullName, b.profile, strings.Join(owned, ", ")) } } if stored != "" { - if nameFlag != "" && nameFlag != stored { - return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is already installed on profile %s as %s; remove it before using --name %s", fullName, b.profile, stored, nameFlag) + if in.nameFlag != "" && in.nameFlag != stored { + return "", nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s is already installed on profile %s as %s; remove it before using --name %s", fullName, b.profile, stored, in.nameFlag) } - other, err := foreignHolder(ctx, held, targets, stored, fullName, b) - if err != nil { - return "", err + if stateHolds(state, stored, entry) { + return "", nil, failure(categoryNameConflict, nil, "%s is installed as %s, but another server in the state holds %s; remove that entry first", fullName, stored, stored) } - if other != nil { - return "", failure(categoryNameConflict, nil, "%s is installed as %s, but %s holds %s for another server; remove that entry first", fullName, stored, other.Name, stored) - } - return stored, nil + failed, err := nameFailures(ctx, in, fullName, stored, true) + return stored, failed, err } - if nameFlag != "" { - if slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { return e.Name == nameFlag }) { - return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another installed server; pick a different name", nameFlag) - } - other, err := foreignHolder(ctx, held, targets, nameFlag, fullName, b) - if err != nil { - return "", err - } - if other != nil { - return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another server in %s; pick a different name", nameFlag, other.Name) + if in.nameFlag != "" { + if stateHolds(state, in.nameFlag, nil) { + return "", nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another installed server; pick a different name", in.nameFlag) } - return nameFlag, nil + failed, err := nameFailures(ctx, in, fullName, in.nameFlag, false) + return in.nameFlag, failed, err } base := mcp.ServerName(fullName, b.profile) if base == "" { - return "", failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "cannot make a server name from %s for profile %s; pass --name", fullName, b.profile) + return "", nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "cannot make a server name from %s for profile %s; pass --name", fullName, b.profile) } // No entry of this binding exists, so every name that an agent holds belongs to another server. reserved := map[string]bool{} - for _, a := range targets { - for name := range held[a] { + for _, a := range in.targets { + for name := range in.held[a] { reserved[name] = true } } - return mcp.UniqueName(base, func(n string) bool { return taken[n] || reserved[n] }), nil + return mcp.UniqueName(base, func(n string) bool { return taken[n] || reserved[n] }), nil, nil +} + +// stateHolds reports whether a state entry other than self holds name. self is nil when no entry is excluded. +func stateHolds(state *mcp.State, name string, self *mcp.Entry) bool { + for i := range state.Servers { + if state.Servers[i].Name == name && &state.Servers[i] != self { + return true + } + } + return false } // ownedNames returns the distinct names that the target agents hold for the binding of fullName, sorted. @@ -686,23 +697,78 @@ func ownedNames(ctx context.Context, held agentEntries, targets []*agents.Agent, return names, nil } -// foreignHolder returns a target agent that holds name as a Databricks entry of another binding than fullName's, or nil. -// An unmanaged entry is not returned: the agent's write refuses it unless --force is set. -func foreignHolder(ctx context.Context, held agentEntries, targets []*agents.Agent, name, fullName string, b addBinding) (*agents.Agent, error) { - for _, a := range targets { - entry, ok := held[a][name] - if !ok || !mcpconfig.IsDatabricksEntry(entry) { - continue - } - serves, err := mcp.EntryServes(ctx, entry, fullName, b.profile, b.configFile) +// nameFailures returns the target agents that cannot take name for fullName, with the reason for each. stored says +// whether name is the server's stored name. In a named agent a conflict refuses the run, so it is returned as the +// error. In a detected agent it fails that agent for this server only. +func nameFailures(ctx context.Context, in planInput, fullName, name string, stored bool) (map[*agents.Agent]error, error) { + failed := map[*agents.Agent]error{} + for _, a := range in.targets { + conflict, err := holderConflict(ctx, in, a, fullName, name, stored) if err != nil { return nil, err } - if !serves { - return a, nil + if conflict == nil { + continue + } + if in.explicit { + return nil, conflict + } + failed[a] = conflict + } + return failed, nil +} + +// holderConflict returns why agent a cannot take name for fullName, or nil when it can. An entry of another binding +// is a conflict. So is an entry this CLI did not write, unless force is set: the agent's write would refuse it. +func holderConflict(ctx context.Context, in planInput, a *agents.Agent, fullName, name string, stored bool) (conflict, err error) { + entry, ok := in.held[a][name] + if !ok { + return nil, nil + } + if !mcpconfig.IsDatabricksEntry(entry) { + if in.force { + return nil, nil + } + return failure(categoryNameConflict, agentconfig.ErrNotOwned, "%s already has a server named %s that this CLI did not write; pass --force to replace it", a.DisplayName, name), nil + } + serves, err := mcp.EntryServes(ctx, entry, fullName, in.binding.profile, in.binding.configFile) + if err != nil { + return nil, err + } + switch { + case serves: + return nil, nil + case stored: + return failure(categoryNameConflict, nil, "%s is installed as %s, but %s holds %s for another server; remove that entry first", fullName, name, a.Name, name), nil + default: + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name %s is used by another server in %s; pick a different name", name, a.Name), nil + } +} + +// planningRefusals returns the failures of the plans when no agent can take any of the servers, which ends the run +// before any request. It returns nil when some agent can take some server. +func planningRefusals(plans []addPlan, targets []*agents.Agent) error { + var errs []error + for _, plan := range plans { + for _, a := range targets { + if plan.failed[a] == nil { + return nil + } + errs = appendDistinct(errs, plan.failed[a]) + } + } + return errors.Join(errs...) +} + +// appendDistinct appends err unless errs already holds that same error value. A failure that repeats for each server, +// such as a refusal, is reported once. +func appendDistinct(errs []error, err error) []error { + for _, e := range errs { + if errors.Is(err, e) { + return errs } } - return nil, nil + return append(errs, err) } // installServer writes one server into every target agent. Each agent's result is reported; one @@ -719,7 +785,10 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO var status agentconfig.Status err := req.refused[a] if err == nil { - args := []string{"aitools", "mcp", "serve", mcpconfig.ProfileFlag, req.profile, mcpconfig.ServiceFlag, plan.service.FullName, "--agent", a.Name} + err = plan.failed[a] + } + if err == nil { + args := []string{"aitools", "mcp", "serve", mcpconfig.ProfileFlag, req.profile, mcpconfig.ServiceFlag, plan.service.FullName, mcpconfig.AgentFlag, a.Name} path, status, err = agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) } @@ -768,12 +837,14 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { } // recordInstalls adds each server that reached at least one agent to the state file. The file is -// re-read first, so entries written since planning are kept. +// re-read first, so entries written since planning are kept. Each server is recorded on its own, so that one +// failure does not keep the others from being saved: their agent files are already written. func recordInstalls(ctx context.Context, req addRequest, plans []addPlan, servers []addServerOutput) error { state, err := mcp.LoadState(ctx) if err != nil { return err } + var errs []error recorded := false for i, server := range servers { paths := map[string]string{} @@ -786,14 +857,17 @@ func recordInstalls(ctx context.Context, req addRequest, plans []addPlan, server continue } if err := recordServer(ctx, state, req, plans[i], paths); err != nil { - return err + errs = append(errs, err) + continue } recorded = true } - if !recorded { - return nil + if recorded { + if err := mcp.SaveState(ctx, state); err != nil { + errs = append(errs, err) + } } - return mcp.SaveState(ctx, state) + return errors.Join(errs...) } // recordServer stores the install of plan's server for req.profile in configFile, with the agents that succeeded @@ -830,11 +904,10 @@ func recordServer(ctx context.Context, state *mcp.State, req addRequest, plan ad // agentFailureError returns the agents' failures as an agentErrors. The command fails when an agent was named // with --agents, or when a requested service reached no agent at all. Otherwise detected agents only warn, as -// in install, and the results already show them. An agent's failure is reported once, although it repeats for -// each service. +// in install, and the results already show them. Every distinct failure is kept; one that repeats for each +// service is reported once. func agentFailureError(req addRequest, servers []addServerOutput) error { var errs []error - reported := map[string]bool{} fail := req.explicit for _, server := range servers { reached := false @@ -843,10 +916,7 @@ func agentFailureError(req addRequest, servers []addServerOutput) error { reached = true continue } - if !reported[a.Name] { - reported[a.Name] = true - errs = append(errs, a.err) - } + errs = appendDistinct(errs, a.err) } if !reached { fail = true diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 85c274f12cf..9cf0657ce72 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/fs" "net/http" "net/http/httptest" @@ -374,7 +375,8 @@ func defaultBinding(t *testing.T, profile string) addBinding { // planNames runs planAdds for the services with the agent entries read as the command reads them. func planNames(t *testing.T, nameFlag string, b addBinding, state *mcp.State, fullNames []string, targets []*agents.Agent) ([]addPlan, error) { t.Helper() - return planAdds(t.Context(), nameFlag, b, state, fullNames, targets, readAgentEntries(t.Context(), targets)) + in := planInput{binding: b, nameFlag: nameFlag, targets: targets, held: readAgentEntries(t.Context(), targets)} + return planAdds(t.Context(), in, state, fullNames) } // claudeEntry is a Databricks entry for claude-code that runs the serve command for service and profile, with the @@ -397,7 +399,6 @@ func TestPlanAddsNaming(t *testing.T) { require.NoError(t, err) require.Len(t, plans, 1) assert.Equal(t, "genie_one_mcp-logfood", plans[0].name) - assert.Nil(t, plans[0].entry) assert.Equal(t, "system.ai.genie_one_mcp", plans[0].service.FullName) }) @@ -407,7 +408,6 @@ func TestPlanAddsNaming(t *testing.T) { plans, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) require.NoError(t, err) assert.Equal(t, "custom-name", plans[0].name) - assert.NotNil(t, plans[0].entry) }) t.Run("stored entry for the same binding is found in its own config file only", func(t *testing.T) { @@ -535,13 +535,16 @@ func TestPlanAddsNaming(t *testing.T) { assert.Equal(t, "--name taken is used by another installed server; pick a different name", err.Error()) }) - t.Run("stored name held by another binding in an agent is refused", func(t *testing.T) { + t.Run("stored name held by another binding in a detected agent fails only that agent", func(t *testing.T) { home := withHome(t) require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"github-logfood":`+claudeEntry("system.ai.github", "dev-foo", "")+`}}`), 0o600)) state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host}}} - _, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) - require.Error(t, err) - assert.Equal(t, categoryNameConflict, errorCategory(err)) + plans, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.NoError(t, err) + require.Len(t, plans, 1) + assert.Equal(t, "github-logfood", plans[0].name) + require.Contains(t, plans[0].failed, claude) + assert.Equal(t, categoryNameConflict, errorCategory(plans[0].failed[claude])) }) t.Run("planning reads the agent entries it is given, not the files", func(t *testing.T) { @@ -550,7 +553,8 @@ func TestPlanAddsNaming(t *testing.T) { held := map[*agents.Agent]map[string]map[string]any{ claude: {"genie_one_mcp-logfood": map[string]any{"type": "stdio", "command": "ug", "args": []any{"mcp-proxy"}}}, } - plans, err := planAdds(t.Context(), "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.genie_one_mcp"}, []*agents.Agent{claude}, held) + in := planInput{binding: defaultBinding(t, "logfood"), targets: []*agents.Agent{claude}, held: held} + plans, err := planAdds(t.Context(), in, &mcp.State{}, []string{"system.ai.genie_one_mcp"}) require.NoError(t, err) assert.Equal(t, "genie_one_mcp-logfood-2", plans[0].name) }) @@ -734,87 +738,6 @@ type failingWriter struct{} func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("disk full") } -// TestInstallServerCodexHomeCountsAsInstalled checks that an agent with a config directory is installed for MCP even when Detected would say otherwise. -func TestInstallServerCodexHomeCountsAsInstalled(t *testing.T) { - home := withHome(t) - codexHome := filepath.Join(t.TempDir(), "codex") - require.NoError(t, os.MkdirAll(codexHome, 0o700)) - t.Setenv(codexHomeEnv, codexHome) - - req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("codex")}, binary: "databricks"} - req.refused = refusedAgents(t.Context(), req.agents) - require.Empty(t, req.refused, "the agent must count as installed, so the refusal path must not refuse it") - out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) - require.Len(t, out.Agents, 1) - assert.Equal(t, statusInstalled, out.Agents[0].Status) - assert.NoError(t, out.Agents[0].err) - assert.FileExists(t, filepath.Join(codexHome, "config.toml")) - assert.NoDirExists(t, filepath.Join(home, ".codex")) -} - -// TestInstallServerGeminiDirAloneIsRefused checks that gemini CLI's directory is shared with Antigravity, so ~/.gemini alone does not install Gemini CLI. -func TestInstallServerGeminiDirAloneIsRefused(t *testing.T) { - home := withHome(t) - require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o700)) - - req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("gemini")}, binary: "databricks"} - req.refused = refusedAgents(t.Context(), req.agents) - out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) - require.Len(t, out.Agents, 1) - assert.Equal(t, statusFailed, out.Agents[0].Status) - assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, out.Agents[0].ErrorCategory) - assert.NoFileExists(t, filepath.Join(home, ".gemini", "settings.json")) -} - -// TestInstallServerGeminiWithProjectsJSON checks that gemini CLI is installed once it has written projects.json, as the registry detects it. -func TestInstallServerGeminiWithProjectsJSON(t *testing.T) { - home := withHome(t) - require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini"), 0o700)) - require.NoError(t, os.WriteFile(filepath.Join(home, ".gemini", "projects.json"), []byte("{}"), 0o600)) - - req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("gemini")}, binary: "databricks"} - req.refused = refusedAgents(t.Context(), req.agents) - require.Empty(t, req.refused, "projects.json must count as installed, so the refusal path must not refuse it") - out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) - require.Len(t, out.Agents, 1) - assert.Equal(t, statusInstalled, out.Agents[0].Status) - assert.FileExists(t, filepath.Join(home, ".gemini", "settings.json")) -} - -// TestInstallServerAntigravityFromItsOwnDir checks that antigravity is installed from its own directory, and its MCP file is written under ~/.gemini/config. -func TestInstallServerAntigravityFromItsOwnDir(t *testing.T) { - home := withHome(t) - require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o700)) - - req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("antigravity")}, binary: "databricks"} - req.refused = refusedAgents(t.Context(), req.agents) - require.Empty(t, req.refused, "its own directory must count as installed, so the refusal path must not refuse it") - out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) - require.Len(t, out.Agents, 1) - assert.Equal(t, statusInstalled, out.Agents[0].Status) - assert.FileExists(t, filepath.Join(home, ".gemini", "config", "mcp_config.json")) -} - -// TestInstallServerCLIOnPathCountsAsInstalled checks that a CLI found only on PATH counts as installed, so its MCP config is written. -func TestInstallServerCLIOnPathCountsAsInstalled(t *testing.T) { - home := withHome(t) - dir := t.TempDir() - name := "codex" - if runtime.GOOS == "windows" { - name += ".exe" - } - require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"), 0o755)) - t.Setenv("PATH", dir) - - req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("codex")}, binary: "databricks"} - req.refused = refusedAgents(t.Context(), req.agents) - require.Empty(t, req.refused, "a CLI on PATH must count as installed, so the refusal path must not refuse it") - out := installServer(t.Context(), req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) - require.Len(t, out.Agents, 1) - assert.Equal(t, statusInstalled, out.Agents[0].Status) - assert.FileExists(t, filepath.Join(home, ".codex", "config.toml")) -} - // TestInstallServerReassertsFromAgentFile checks that a second install of the same entry reports "re-asserted" from the agent file, even when the state // that recorded it is gone. func TestInstallServerReassertsFromAgentFile(t *testing.T) { @@ -854,16 +777,6 @@ func TestAgentFailureErrorWhenAServiceReachedNoAgent(t *testing.T) { assert.NoError(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{installed})) } -func TestRequireProfileNamesProfileAndPath(t *testing.T) { - ctx := t.Context() - path := writeAddConfig(t, "[other]\nhost = https://other.test\n") - - err := requireProfile(ctx, "logfood") - require.Error(t, err) - assert.Equal(t, "profile logfood not found in "+filepath.ToSlash(path), err.Error()) - assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) -} - func TestErrorCategoryMapsTypedFailures(t *testing.T) { assert.Equal(t, categoryNameConflict, errorCategory(fmt.Errorf("claude-code: server %q: %w", "github", agentconfig.ErrNotOwned))) @@ -953,7 +866,7 @@ func TestMcpAddEmptyHostIsProfileNotFound(t *testing.T) { err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") require.Error(t, err) assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) - assert.Contains(t, err.Error(), "profile logfood not found in ") + assert.Contains(t, err.Error(), "profile \"logfood\", which has no host in ") } // TestMcpAddRejectedTokenIsAuthFailed checks that a credential the workspace rejects on the first real request is AUTH_FAILED, with the login command. @@ -1040,7 +953,7 @@ func TestMcpAddNamesResolvedConfigFileWhenDotDotFollowsSymlink(t *testing.T) { assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) resolved, evalErr := filepath.EvalSymlinks(cfg) require.NoError(t, evalErr) - assert.Equal(t, "profile logfood not found in "+filepath.ToSlash(resolved), err.Error()) + assert.Contains(t, err.Error(), "which no longer exists in "+filepath.ToSlash(resolved)+". Recreate") } // TestMcpAddRefusesNameThatSanitizesToNothing checks that a service whose generated name is empty is refused before any request, and the message @@ -1094,52 +1007,60 @@ func TestMcpAddTakesLocationFromFullNames(t *testing.T) { } // TestMcpAddAgreesWithBind checks that add refuses the same bindings that Bind refuses, with the same message where the failure is about the server. +// setupHostChanged prepares the case "host changed" for a profile in the default config file. +func setupHostChanged(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test"}) +} + +// setupWorkspaceChanged prepares the case "workspace changed on the same host" for a profile in the default config file. +func setupWorkspaceChanged(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host, WorkspaceID: "111"}) +} + +// setupOtherConfigFile prepares the case "entry pinned to another config file" for a profile in the default config file. +func setupOtherConfigFile(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host, ConfigFile: filepath.Join(t.TempDir(), "other")}) +} + +// setupProfileGoneWithEntry prepares the case "profile gone while an entry is bound to it" for a profile in the default config file. +func setupProfileGoneWithEntry(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[other]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-other\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host}) +} + +// setupProfileGoneWithoutEntry prepares the case "profile gone with no entry" for a profile in the default config file. +func setupProfileGoneWithoutEntry(t *testing.T, home, host string) { + writeHomeConfig(t, home, "[other]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-other\n") +} + func TestMcpAddAgreesWithBind(t *testing.T) { const service = "system.ai.github" cases := []struct { name string setup func(t *testing.T, home, host string) - // sameMessage is false when add reports the profile as missing with its own wording. - sameMessage bool }{ { - name: "host changed", - setup: func(t *testing.T, home, host string) { - writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-logfood\n") - saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: "https://adb-123.test"}) - }, - sameMessage: true, + name: "host changed", + setup: setupHostChanged, }, { - name: "workspace changed on the same host", - setup: func(t *testing.T, home, host string) { - writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") - saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: host, WorkspaceID: "111"}) - }, - sameMessage: true, + name: "workspace changed on the same host", + setup: setupWorkspaceChanged, }, { - name: "entry pinned to another config file", - setup: func(t *testing.T, home, host string) { - writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-logfood\n") - saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: host, ConfigFile: filepath.Join(t.TempDir(), "other")}) - }, - sameMessage: true, + name: "entry pinned to another config file", + setup: setupOtherConfigFile, }, { - name: "profile gone while an entry is bound to it", - setup: func(t *testing.T, home, host string) { - writeHomeConfig(t, home, "[other]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-other\n") - saveEntry(t, mcp.Entry{Name: "github-logfood", Service: service, Profile: "logfood", Host: host}) - }, - sameMessage: true, + name: "profile gone while an entry is bound to it", + setup: setupProfileGoneWithEntry, }, { - name: "profile gone with no entry", - setup: func(t *testing.T, home, host string) { - writeHomeConfig(t, home, "[other]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-other\n") - }, - sameMessage: false, + name: "profile gone with no entry", + setup: setupProfileGoneWithoutEntry, }, } for _, tc := range cases { @@ -1157,9 +1078,7 @@ func TestMcpAddAgreesWithBind(t *testing.T) { addErr := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") require.Error(t, addErr) assert.Equal(t, errorCategory(bindErr), errorCategory(addErr)) - if tc.sameMessage { - assert.Equal(t, bindErr.Error(), addErr.Error()) - } + assert.Equal(t, bindErr.Error(), addErr.Error()) assert.NoFileExists(t, filepath.Join(home, ".claude.json")) }) } @@ -1170,3 +1089,272 @@ func saveEntry(t *testing.T, entry mcp.Entry) { t.Helper() require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{entry}})) } + +// TestPlanAddsRefusesRecoveredNameHeldByAnotherServer checks that a name recovered from an agent entry, which has no state record, is refused +// when another state entry holds it. Two servers with one name would overwrite each other's state. +func TestPlanAddsRefusesRecoveredNameHeldByAnotherServer(t *testing.T) { + home := withHome(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"github-logfood":`+claudeEntry("system.ai.github", "logfood", "")+`}}`), 0o600)) + state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.genie_one_mcp", Profile: "logfood", Host: "https://adb-123.test"}}} + + _, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{agents.ByName("claude-code")}) + require.Error(t, err) + assert.Equal(t, categoryNameConflict, errorCategory(err)) + assert.Equal(t, "system.ai.github is installed as github-logfood, but another server in the state holds github-logfood; remove that entry first", err.Error()) +} + +// TestMcpAddRefusesUnmanagedEntryBeforeAnyRequest checks that the stored name of a server is refused before any request when an agent holds that +// name as an entry this CLI did not write. The agent's write would refuse it only after the workspace was contacted. +func TestMcpAddRefusesUnmanagedEntryBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: server.URL, + }}})) + claudeFile := filepath.Join(home, ".claude.json") + unmanaged := `{"mcpServers":{"github-logfood":{"type":"stdio","command":"npx","args":["-y","other-server"]}}}` + require.NoError(t, os.WriteFile(claudeFile, []byte(unmanaged), 0o600)) + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, categoryNameConflict, errorCategory(err)) + assert.ErrorContains(t, err, "Claude Code already has a server named github-logfood that this CLI did not write; pass --force to replace it") + got, err := os.ReadFile(claudeFile) + require.NoError(t, err) + assert.Equal(t, unmanaged, string(got)) +} + +// TestMcpAddAccountProfileRefusedBeforeAnyRequest checks that a profile with no host, such as an account-level profile, is refused before any +// request, with the same error as a missing profile in bind. +func TestMcpAddAccountProfileRefusedBeforeAnyRequest(t *testing.T) { + home := withHome(t) + noRequestServer(t) + writeHomeConfig(t, home, "[account]\naccount_id = 123\n") + + err := runAdd(t, "--profile", "account", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + assert.Contains(t, err.Error(), "profile \"account\", which has no host in ") + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// TestMcpAddMissingProfileWithoutEntryMatchesBind checks that a profile that is missing when no server is installed is reported by the shared +// check, so add and bind word the failure the same way. +func TestMcpAddMissingProfileWithoutEntryMatchesBind(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[other]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-other\n") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + _, _, bindErr := mcp.Bind(t.Context(), mcp.BindOptions{Profile: "logfood", Service: "system.ai.github", State: state, Stderr: io.Discard}) + require.Error(t, bindErr) + + err = runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + assert.Equal(t, bindErr.Error(), err.Error()) + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// TestRecordInstallsSavesServersThatCanBeSaved checks that a server whose state record fails does not stop the other servers from being +// recorded. The agent files of every server were written, so their state must be saved, and the failure is still returned. +func TestRecordInstallsSavesServersThatCanBeSaved(t *testing.T) { + ctx := t.Context() + withHome(t) + // A config file name with a NUL byte cannot be resolved, so the record of the first service fails. + require.NoError(t, mcp.SaveState(ctx, &mcp.State{Servers: []mcp.Entry{{ + Name: "bad-logfood", Service: "system.ai.bad", Profile: "logfood", Host: "https://adb-123.test", ConfigFile: "bad\x00name", + }}})) + configFile, err := mcp.ResolveConfigFile(ctx, "") + require.NoError(t, err) + req := addRequest{profile: "logfood", host: "https://adb-456.test", configFile: configFile} + plans := []addPlan{ + {service: mcp.Service{FullName: "system.ai.bad"}, name: "bad-logfood"}, + {service: mcp.Service{FullName: "system.ai.github"}, name: "github-logfood"}, + } + servers := []addServerOutput{ + {Name: "bad-logfood", Service: "system.ai.bad", Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled, Path: "/x/.claude.json"}}}, + {Name: "github-logfood", Service: "system.ai.github", Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled, Path: "/x/.claude.json"}}}, + } + + err = recordInstalls(ctx, req, plans, servers) + require.Error(t, err) + assert.ErrorContains(t, err, "MCP server bad-logfood") + + state, loadErr := mcp.LoadState(ctx) + require.NoError(t, loadErr) + names := []string{} + for _, entry := range state.Servers { + names = append(names, entry.Name) + } + assert.Equal(t, []string{"bad-logfood", "github-logfood"}, names) +} + +// TestAgentFailureErrorKeepsEveryDistinctFailure checks that an agent's failures are all reported when they differ, and that one failure that repeats for +// each service, such as a refusal, is reported once. +func TestAgentFailureErrorKeepsEveryDistinctFailure(t *testing.T) { + refusal := errors.New("Claude Code is not installed") + denied := fmt.Errorf("write ~/.claude.json: %w", fs.ErrPermission) + servers := []addServerOutput{ + {Agents: []addAgentOutput{{Name: "claude-code", Status: statusFailed, err: refusal}}}, + {Agents: []addAgentOutput{{Name: "claude-code", Status: statusFailed, err: denied}}}, + {Agents: []addAgentOutput{{Name: "claude-code", Status: statusFailed, err: refusal}}}, + } + + err := agentFailureError(addRequest{explicit: true}, servers) + require.Error(t, err) + assert.ErrorContains(t, err, "write ~/.claude.json") + assert.Equal(t, 1, strings.Count(err.Error(), "Claude Code is not installed"), err.Error()) +} + +// TestResolveServicesMalformedFullNameIsNotALocationError checks that a name with an empty part is reported as a malformed service name, not as a +// --location error, since no --location was passed. +func TestResolveServicesMalformedFullNameIsNotALocationError(t *testing.T) { + for _, name := range []string{"system..github", "system.ai."} { + t.Run(name, func(t *testing.T) { + _, _, err := resolveServices("", []string{name}) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + assert.Equal(t, name+" is not a service name: use a leaf such as github or a full name such as system.ai.github", err.Error()) + }) + } +} + +// TestPlanAddsDetectedAgentConflictFailsOnlyThatAgent checks that a name conflict in an agent that was only detected fails that agent for the server, and +// the other agents still take it. The same conflict in an agent named with --agents refuses the run. When every agent fails, the run is refused. +func TestPlanAddsDetectedAgentConflictFailsOnlyThatAgent(t *testing.T) { + ctx := t.Context() + home := withHome(t) + claude := agents.ByName("claude-code") + codex := agents.ByName("codex") + // claude-code holds the stored name of this server for another server, so the name conflicts in claude-code only. + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"github-logfood":`+claudeEntry("system.ai.other", "logfood", "")+`}}`), 0o600)) + state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test"}}} + targets := []*agents.Agent{claude, codex} + in := planInput{binding: defaultBinding(t, "logfood"), targets: targets, held: readAgentEntries(ctx, targets)} + + plans, err := planAdds(ctx, in, state, []string{"system.ai.github"}) + require.NoError(t, err) + require.Len(t, plans, 1) + assert.Equal(t, "github-logfood", plans[0].name) + require.Contains(t, plans[0].failed, claude) + assert.Equal(t, categoryNameConflict, errorCategory(plans[0].failed[claude])) + assert.NotContains(t, plans[0].failed, codex) + assert.NoError(t, planningRefusals(plans, targets)) + + in.explicit = true + _, err = planAdds(ctx, in, state, []string{"system.ai.github"}) + require.Error(t, err) + assert.Equal(t, categoryNameConflict, errorCategory(err)) + + onlyClaude := []*agents.Agent{claude} + in = planInput{binding: defaultBinding(t, "logfood"), targets: onlyClaude, held: readAgentEntries(ctx, onlyClaude)} + plans, err = planAdds(ctx, in, state, []string{"system.ai.github"}) + require.NoError(t, err) + refusal := planningRefusals(plans, onlyClaude) + require.Error(t, refusal) + assert.Equal(t, categoryNameConflict, errorCategory(refusal)) +} + +// TestPlanAddsUnmanagedEntryIsReplacedOnlyWithForce checks that a stored name held by an entry this CLI did not write is refused at planning, +// and that --force lets the write replace it. +func TestPlanAddsUnmanagedEntryIsReplacedOnlyWithForce(t *testing.T) { + ctx := t.Context() + claude := agents.ByName("claude-code") + state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test"}}} + held := agentEntries{claude: {"github-logfood": map[string]any{"type": "stdio", "command": "npx", "args": []any{"-y", "other"}}}} + targets := []*agents.Agent{claude} + + in := planInput{binding: defaultBinding(t, "logfood"), targets: targets, held: held, explicit: true} + _, err := planAdds(ctx, in, state, []string{"system.ai.github"}) + require.Error(t, err) + assert.Equal(t, categoryNameConflict, errorCategory(err)) + + in.force = true + plans, err := planAdds(ctx, in, state, []string{"system.ai.github"}) + require.NoError(t, err) + assert.Empty(t, plans[0].failed) +} + +// TestPickServicesNamesSchemaWithNoServices checks the message for a schema that has no MCP services, which has no list of available services. +func TestPickServicesNamesSchemaWithNoServices(t *testing.T) { + _, err := pickServices("system.ai", []string{"system.ai.github"}, nil) + require.Error(t, err) + assert.Equal(t, "system.ai.github is not an MCP service in system.ai, which has none", err.Error()) + assert.Equal(t, categoryServiceNotFound, errorCategory(err)) +} + +// TestInstallServerWritesOnlyAgentsInstalledForMCP checks that an agent named with --agents is written when it counts as installed for MCP, and that +// the write goes to the file its config lives in. An agent that does not count is refused, and nothing is created for it. +func TestInstallServerWritesOnlyAgentsInstalledForMCP(t *testing.T) { + tests := []struct { + name string + agent string + homeEnv string // moves the MCP config out of the agent's directory + homeDir string // the directory under home that homeEnv names + dirs []string + files map[string]string + onPath bool + // written is the config file, under home, that the write creates; empty when the agent is refused. + written string + // absent is the path, under home, that must not exist after the run. + absent string + }{ + {name: "codex home directory exists", agent: "codex", homeEnv: codexHomeEnv, homeDir: "codex-home", dirs: []string{"codex-home"}, written: "codex-home/config.toml"}, + {name: "codex home directory missing although ~/.codex exists", agent: "codex", homeEnv: codexHomeEnv, homeDir: "codex-home", dirs: []string{".codex"}, absent: "codex-home"}, + {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"}, + {name: "cli on PATH", agent: "codex", onPath: true, written: ".codex/config.toml"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := t.Context() + home := withHome(t) + if tt.homeEnv != "" { + t.Setenv(tt.homeEnv, filepath.Join(home, tt.homeDir)) + } + for _, dir := range tt.dirs { + require.NoError(t, os.MkdirAll(filepath.Join(home, dir), 0o700)) + } + for name, contents := range tt.files { + require.NoError(t, os.WriteFile(filepath.Join(home, name), []byte(contents), 0o600)) + } + if tt.onPath { + writeFakeCLI(t, agents.ByName(tt.agent).Binary) + } + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName(tt.agent)}, binary: "databricks"} + req.refused = refusedAgents(ctx, req.agents) + out := installServer(ctx, req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) + require.Len(t, out.Agents, 1) + + if tt.written == "" { + assert.Equal(t, statusFailed, out.Agents[0].Status) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, out.Agents[0].ErrorCategory) + } else { + assert.Equal(t, statusInstalled, out.Agents[0].Status) + assert.NoError(t, out.Agents[0].err) + assert.FileExists(t, filepath.Join(home, filepath.FromSlash(tt.written))) + } + if tt.absent != "" { + _, err := os.Stat(filepath.Join(home, filepath.FromSlash(tt.absent))) + assert.ErrorIs(t, err, fs.ErrNotExist) + } + }) + } +} + +// writeFakeCLI puts an executable named binary in a directory that is the only one on PATH, so the agent counts as installed. +func writeFakeCLI(t *testing.T, binary string) { + t.Helper() + dir := t.TempDir() + name := binary + if runtime.GOOS == "windows" { + name += ".exe" + } + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"), 0o755)) + t.Setenv("PATH", dir) +} diff --git a/cmd/aitools/mcp_binary_test.go b/cmd/aitools/mcp_binary_test.go index c9451e70b0d..0ae6b3f616b 100644 --- a/cmd/aitools/mcp_binary_test.go +++ b/cmd/aitools/mcp_binary_test.go @@ -14,16 +14,14 @@ func TestBinaryPathFor(t *testing.T) { wd, err := os.Getwd() require.NoError(t, err) absolute := filepath.Join(t.TempDir(), "bin", "databricks") - onPath := filepath.Join(t.TempDir(), "databricks") + foundPath := filepath.Join(t.TempDir(), "databricks") relativeOnPath := filepath.Join("bin", "databricks") - notFound := func(string) (string, error) { return "", errors.New("not found") } - found := func(string) (string, error) { return onPath, nil } - relative := func(string) (string, error) { return relativeOnPath, nil } tests := []struct { - name string - arg0 string - lookPath func(string) (string, error) + name string + arg0 string + // onPath is what the PATH lookup finds, or "" when the name is not on PATH. + onPath string // wantLookup is the name the PATH lookup receives, or "" when the lookup must not run. wantLookup string wantPath string @@ -33,21 +31,20 @@ func TestBinaryPathFor(t *testing.T) { // The invoked path is kept as is, so a Homebrew symlink is not replaced by its target. name: "absolute arg0 is kept", arg0: absolute, - lookPath: nil, wantPath: absolute, wantAbs: true, }, { name: "relative arg0 with a directory is made absolute, not looked up", arg0: filepath.Join("bin", "databricks"), - lookPath: found, + onPath: foundPath, wantPath: mustAbs(t, filepath.Join("bin", "databricks")), wantAbs: true, }, { name: "dot-relative arg0 with a directory is made absolute", arg0: "." + string(filepath.Separator) + "databricks", - lookPath: found, + onPath: foundPath, wantPath: wd + string(filepath.Separator) + "databricks", wantAbs: true, }, @@ -55,22 +52,22 @@ func TestBinaryPathFor(t *testing.T) { // The OS reads "link/.." through the link's target, so the path must not be cleaned before the symlink is followed. name: "relative arg0 through a symlink keeps its dot-dot", arg0: "link" + string(filepath.Separator) + ".." + string(filepath.Separator) + "databricks", - lookPath: found, + onPath: foundPath, wantPath: wd + string(filepath.Separator) + "link" + string(filepath.Separator) + ".." + string(filepath.Separator) + "databricks", wantAbs: true, }, { name: "bare name is looked up on PATH", arg0: "databricks", - lookPath: found, + onPath: foundPath, wantLookup: "databricks", - wantPath: onPath, + wantPath: foundPath, wantAbs: true, }, { name: "bare name not on PATH falls back to the bare name", arg0: "databricks", - lookPath: notFound, + onPath: "", wantLookup: "databricks", wantPath: "databricks", }, @@ -78,33 +75,25 @@ func TestBinaryPathFor(t *testing.T) { // Only a databricks argv[0] may fall back to the bare name; another name is kept, so the caller refuses it. name: "other bare name not on PATH is kept", arg0: "dbx", - lookPath: notFound, + onPath: "", wantLookup: "dbx", wantPath: "dbx", }, { name: "relative PATH entry falls back to the bare name", arg0: "databricks", - lookPath: relative, + onPath: relativeOnPath, wantLookup: "databricks", wantPath: "databricks", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var looked string - lookPath := func(name string) (string, error) { - looked = name - if tt.lookPath == nil { - t.Fatal("PATH lookup must not run for this arg0") - } - return tt.lookPath(name) - } - - path, abs := binaryPathFor(tt.arg0, lookPath) + lookup := &pathLookup{path: tt.onPath} + path, abs := binaryPathFor(tt.arg0, lookup.lookPath) assert.Equal(t, tt.wantPath, path) assert.Equal(t, tt.wantAbs, abs) - assert.Equal(t, tt.wantLookup, looked) + assert.Equal(t, tt.wantLookup, lookup.asked) }) } } @@ -115,3 +104,18 @@ func mustAbs(t *testing.T, path string) string { require.NoError(t, err) return abs } + +// pathLookup stands in for the PATH lookup. It finds the name at path, or reports it not found when path is "". It +// records the name it was asked for, so a case can check whether the lookup ran at all. +type pathLookup struct { + path string + asked string +} + +func (l *pathLookup) lookPath(name string) (string, error) { + l.asked = name + if l.path == "" { + return "", errors.New("not found") + } + return l.path, nil +} From c990f3eac16d9a9672f94533ef057dfe4ee920eb Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:24:10 +0000 Subject: [PATCH 17/20] Route the mcp acceptance setup output to LOG files The setup posts that create the fake services sent their output to /dev/null, so a failed setup showed only a later, unrelated failure. They write to LOG.create-- now. The hidden golden shows the new help text for --location and the leaf-name rule. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/add-agent-not-installed/script | 2 +- acceptance/cmd/aitools/mcp/add-default-config/script | 2 +- acceptance/cmd/aitools/mcp/add-text/script | 4 ++-- acceptance/cmd/aitools/mcp/add-two-agents/script | 4 ++-- acceptance/cmd/aitools/mcp/add-two-profiles/script | 4 ++-- acceptance/cmd/aitools/mcp/add-unparsable-config/script | 2 +- acceptance/cmd/aitools/mcp/hidden/output.txt | 8 ++++---- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-agent-not-installed/script b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script index 07cca049f6e..d671e1a0a88 100644 --- a/acceptance/cmd/aitools/mcp/add-agent-not-installed/script +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script @@ -5,7 +5,7 @@ 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"}' > /dev/null +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 title "An agent named with --agents that is not installed is refused; the installed agent still gets the server, and the command exits non-zero" musterr trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,antigravity --output json diff --git a/acceptance/cmd/aitools/mcp/add-default-config/script b/acceptance/cmd/aitools/mcp/add-default-config/script index 4d5c0f7b695..a0c4ff56b10 100644 --- a/acceptance/cmd/aitools/mcp/add-default-config/script +++ b/acceptance/cmd/aitools/mcp/add-default-config/script @@ -5,7 +5,7 @@ envsubst < .databrickscfg > home/.databrickscfg unset DATABRICKS_CONFIG_FILE # 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"}' > /dev/null +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 title "Without DATABRICKS_CONFIG_FILE the server is added for the default config file" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code diff --git a/acceptance/cmd/aitools/mcp/add-text/script b/acceptance/cmd/aitools/mcp/add-text/script index 8c8b4f39d95..f0e4aa8c937 100644 --- a/acceptance/cmd/aitools/mcp/add-text/script +++ b/acceptance/cmd/aitools/mcp/add-text/script @@ -5,8 +5,8 @@ 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"}' > /dev/null -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"}}}}' > /dev/null +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 "Text output: Cursor approval hint, and the sign-in step for github" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,cursor diff --git a/acceptance/cmd/aitools/mcp/add-two-agents/script b/acceptance/cmd/aitools/mcp/add-two-agents/script index 592500e4672..cb8c5829825 100644 --- a/acceptance/cmd/aitools/mcp/add-two-agents/script +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -5,8 +5,8 @@ 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"}' > /dev/null -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"}}}}' > /dev/null +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 "Two services for two agents, as JSON" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,codex --output json diff --git a/acceptance/cmd/aitools/mcp/add-two-profiles/script b/acceptance/cmd/aitools/mcp/add-two-profiles/script index 4ff7847ab2c..928f11ae81f 100644 --- a/acceptance/cmd/aitools/mcp/add-two-profiles/script +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/script @@ -5,9 +5,9 @@ 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"}' > /dev/null +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 # The fake keeps one workspace per token, so the second profile needs its own copy. -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 dogfood --json '{"comment": "Ask questions about your data"}' > /dev/null +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 dogfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-dogfood title "The same service for a second profile adds an entry and renames nothing" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code diff --git a/acceptance/cmd/aitools/mcp/add-unparsable-config/script b/acceptance/cmd/aitools/mcp/add-unparsable-config/script index 336785db1b2..b0bef562d44 100644 --- a/acceptance/cmd/aitools/mcp/add-unparsable-config/script +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/script @@ -5,7 +5,7 @@ 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"}' > /dev/null +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 title "An unparsable config fails only its agent; the other agent still installs, and the command exits non-zero" musterr trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents codex,claude-code --output json diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index f85300f2b4f..ed892dd4ef5 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -10,9 +10,9 @@ aitools help does not list mcp Install Databricks managed MCP servers into coding agents, bound to a Databricks profile. Each server runs through 'databricks aitools mcp serve' with that profile's credentials. -Servers come from --names: full names such as system.ai.github, or leaf names resolved in ---location (default: the schema of the full names, else system.ai). Agents come from --agents. -Without it, every detected MCP-capable agent is used. +Servers come from --names: full names such as system.ai.github, or leaf names such as github. Leaf names +resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share. +Agents come from --agents. Without it, every detected MCP-capable agent is used. Interactive selection is not available yet: in a terminal, pass both --names and --agents. With --output json, both are required. @@ -26,7 +26,7 @@ Flags: --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) --force Overwrite an existing server with the same name that this CLI did not write -h, --help help for add - --location string Schema to resolve leaf names in, as . (default: the schema of the full names in --names, else system.ai) + --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share) --name string Server name to use instead of the generated one; only with exactly one service --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated From 99a56971e5cb4616758efdea612fcb25c77e7567 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:25:45 +0000 Subject: [PATCH 18/20] Document the per-agent refusals of installServer Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 2 +- cmd/aitools/mcp_add_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index caccd231998..960c5b2d458 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -772,7 +772,7 @@ func appendDistinct(errs []error, err error) []error { } // installServer writes one server into every target agent. Each agent's result is reported; one -// agent's failure does not stop the others. An agent in req.refused is not written. +// agent's failure does not stop the others. An agent in req.refused, or that failed planning for the server, is not written. func installServer(ctx context.Context, req addRequest, plan addPlan) addServerOutput { out := addServerOutput{ Name: plan.name, diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 9cf0657ce72..43879b1b47f 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1185,7 +1185,7 @@ func TestRecordInstallsSavesServersThatCanBeSaved(t *testing.T) { state, loadErr := mcp.LoadState(ctx) require.NoError(t, loadErr) - names := []string{} + var names []string for _, entry := range state.Servers { names = append(names, entry.Name) } From f423c92c79b3c4db9ca32e2fca3c7c3386f7d64f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:33:12 +0000 Subject: [PATCH 19/20] Word a missing profile without an install record as not set up A profile that is missing when no state entry exists is not bound to any server yet, so the message no longer says the server is bound to it or suggests removing it. It says the service needs the profile and how to set it up. Co-authored-by: Isaac --- cmd/aitools/mcp_add_test.go | 4 +++- libs/aitools/mcp/binding.go | 26 +++++++++++++++++--------- libs/aitools/mcp/binding_test.go | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 43879b1b47f..981c350d3c5 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -953,7 +953,7 @@ func TestMcpAddNamesResolvedConfigFileWhenDotDotFollowsSymlink(t *testing.T) { assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) resolved, evalErr := filepath.EvalSymlinks(cfg) require.NoError(t, evalErr) - assert.Contains(t, err.Error(), "which no longer exists in "+filepath.ToSlash(resolved)+". Recreate") + assert.Contains(t, err.Error(), "which does not exist in "+filepath.ToSlash(resolved)+". Set it up with: ") } // TestMcpAddRefusesNameThatSanitizesToNothing checks that a service whose generated name is empty is refused before any request, and the message @@ -1155,6 +1155,8 @@ func TestMcpAddMissingProfileWithoutEntryMatchesBind(t *testing.T) { require.Error(t, err) assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) assert.Equal(t, bindErr.Error(), err.Error()) + // Nothing is installed for the profile, so the message does not say the server is bound to it. + assert.NotContains(t, err.Error(), "remove the server") assert.NoFileExists(t, filepath.Join(home, ".claude.json")) } diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 7978fc33fdc..0b4a2741c73 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -163,7 +163,12 @@ func profileFor(name, profileName, configFile string, entry *Entry) (profile.Pro p, err := configFiles{}.profile(configFile, profileName) switch { case errors.Is(err, errProfileMissing): - return profile.Profile{}, profileGoneError(name, profileName, filepath.ToSlash(configFile), entry, "no longer exists") + problem := "no longer exists" + if entry == nil { + // Nothing records that the profile existed: the server is not installed yet, or its entry was written by hand. + problem = "does not exist" + } + return profile.Profile{}, profileGoneError(name, profileName, filepath.ToSlash(configFile), entry, problem) case errors.Is(err, errProfileNoHost): return profile.Profile{}, profileGoneError(name, profileName, filepath.ToSlash(configFile), entry, "has no host") case err != nil: @@ -184,15 +189,18 @@ func removeCommand(profileName string) string { // profileGoneError builds the PROFILE_NOT_FOUND error for a profile that the config file does not // name with a host, either because it is absent or because its section has no host. problem completes -// the sentence "which ... in ". Without a state entry the recorded host is unknown, so the -// recreate hint omits --host. An entry pinned to a config file puts that file in the recreate hint. +// the sentence "which ... in ". Without a state entry nothing was set up for the profile, so the +// message only says how to set the profile up, without a host. An entry pinned to a config file puts that +// file in the recreate hint. func profileGoneError(name, profileName, path string, entry *Entry, problem string) error { - recreate := authLoginCommand + " --profile " + shellquote.BashArg(profileName) - if entry != nil { - recreate = authLoginCommand + " --host " + shellquote.BashArg(entry.Host) + " --profile " + shellquote.BashArg(profileName) - if entry.ConfigFile != "" { - recreate = "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(path) + " " + recreate - } + if entry == nil { + login := authLoginCommand + " --profile " + shellquote.BashArg(profileName) + message := fmt.Sprintf("MCP server %s needs Databricks profile \"%s\", which %s in %s. Set it up with: %s", name, profileName, problem, path, login) + return &BindError{Category: BindProfileNotFound, message: message} + } + recreate := authLoginCommand + " --host " + shellquote.BashArg(entry.Host) + " --profile " + shellquote.BashArg(profileName) + if entry.ConfigFile != "" { + recreate = "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(path) + " " + recreate } message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which %s in %s. Recreate it with: %s or remove the server with: %s", name, profileName, problem, path, recreate, removeCommand(profileName)) diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index e6406f607e2..1ab27abf006 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -132,7 +132,7 @@ func TestBindProfileAbsent(t *testing.T) { { name: "without state entry", state: &mcp.State{}, - want: fmt.Sprintf(`MCP server system.ai.github is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), + want: fmt.Sprintf(`MCP server system.ai.github needs Databricks profile "gone", which does not exist in %s. Set it up with: databricks auth login --profile gone`, filepath.ToSlash(path)), }, } for _, tt := range tests { @@ -858,7 +858,7 @@ func TestCheckBindingChecksProfileWithoutEntry(t *testing.T) { contents string problem string }{ - {name: "missing", contents: "[other]\nhost = https://other.test\n", problem: "which no longer exists in "}, + {name: "missing", contents: "[other]\nhost = https://other.test\n", problem: "which does not exist in "}, {name: "account profile without host", contents: "[" + bindingTestProfile + "]\naccount_id = 123\n", problem: "which has no host in "}, } for _, tt := range tests { From aa53c0ea7dc41728f9f7ac943aee4e58b8c67eef Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 19:49:24 +0000 Subject: [PATCH 20/20] Resolve a missing config file with OS separators A "~" name expands to the home directory joined with "/", and a file that does not exist is kept as written, so on Windows the resolved path mixed separators and did not equal the one other callers build. Convert slashes to the OS separator without cleaning, which keeps ".." after a symlink. Co-authored-by: Isaac --- libs/aitools/mcp/configfile.go | 4 +++- libs/aitools/mcp/configfile_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libs/aitools/mcp/configfile.go b/libs/aitools/mcp/configfile.go index a6c2707f675..8e2b7346d5d 100644 --- a/libs/aitools/mcp/configfile.go +++ b/libs/aitools/mcp/configfile.go @@ -33,8 +33,10 @@ func ResolveConfigFile(ctx context.Context, name string) (string, error) { } // AbsoluteAsWritten makes path absolute by joining it to the working directory, without cleaning it, so that a -// ".." after a symlink still resolves through the link's target. +// ".." after a symlink still resolves through the link's target. Slashes become the OS separator, as cleaning would +// make them: "~/.databrickscfg" expands to the home directory joined with "/" on Windows too. func AbsoluteAsWritten(path string) (string, error) { + path = filepath.FromSlash(path) if filepath.IsAbs(path) { return path, nil } diff --git a/libs/aitools/mcp/configfile_test.go b/libs/aitools/mcp/configfile_test.go index c47abcffdda..7660a8a98cf 100644 --- a/libs/aitools/mcp/configfile_test.go +++ b/libs/aitools/mcp/configfile_test.go @@ -59,6 +59,17 @@ func TestResolveConfigFileKeepsPathThroughMissingDir(t *testing.T) { assert.Equal(t, name, got) } +// TestResolveConfigFileMissingFileUsesOSSeparators checks that a "~" name of a file that does not exist resolves with +// the OS separator throughout, as the name of an existing file does. +func TestResolveConfigFileMissingFileUsesOSSeparators(t *testing.T) { + home := t.TempDir() + ctx := env.WithUserHomeDir(t.Context(), home) + + got, err := ResolveConfigFile(ctx, "~/missing/.databrickscfg") + require.NoError(t, err) + assert.Equal(t, filepath.Join(home, "missing", ".databrickscfg"), got) +} + // TestSamePathComparesExistingFilesByIdentity checks that two names of one existing file compare equal, // as a hard link does, while names of files that do not exist are compared as text. func TestSamePathComparesExistingFilesByIdentity(t *testing.T) {