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..b186d02e28c --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt @@ -0,0 +1,42 @@ + +=== 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,antigravity --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": "antigravity", + "status": "failed", + "error_category": "UNKNOWN_AGENT", + "message": "Antigravity is not installed; install it first or leave it out of --agents" + } + ] + } + ], + "error_category": "TYPE_UNSPECIFIED" +} + +=== Nothing was created for Antigravity + +>>> ls -A home +.claude +.claude.json +.databricks + +=== With no installed agent named, the command is refused before any request, and the text output says what to do + +>>> [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 new file mode 100644 index 00000000000..d671e1a0a88 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/script @@ -0,0 +1,17 @@ +# 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 + +# The services of system.ai, created in the fake workspace. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood + +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 Antigravity" +trace ls -A home + +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/.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..9d92f10571f --- /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 + +>>> gron.py home/.claude.json +json.mcpServers.genie_one_mcp-logfood.env = {}; + +=== 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..a0c4ff56b10 --- /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"}' &> 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 + +title "Claude Code config has no pinned variables for the server" +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-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..d35665a25fc --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -0,0 +1,74 @@ + +=== 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 + +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 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..f0e4aa8c937 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/script @@ -0,0 +1,18 @@ +# 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 + +# The services of system.ai, created in the fake workspace. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' &> LOG.create-github-logfood + +title "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/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/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..faa949bdf06 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt @@ -0,0 +1,158 @@ + +=== 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": { + "my-server": { + "type": "stdio", + "command": "my-server", + "args": [] + }, + "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.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 .mcp | del(.servers[].installed_at, .servers[].cli_version) home/.databricks/aitools/skills/.state.json +{ + "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..cb8c5829825 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/script @@ -0,0 +1,21 @@ +# 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 + +# The services of system.ai, created in the fake workspace. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' &> LOG.create-github-logfood + +title "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 '.mcp | del(.servers[].installed_at, .servers[].cli_version)' home/.databricks/aitools/skills/.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..08a250cd75e --- /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 .mcp | [.servers[] | {name, profile, agents: (.agents | keys)}] home/.databricks/aitools/skills/.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..928f11ae81f --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/script @@ -0,0 +1,20 @@ +# 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 + +# The services of system.ai, created in the fake workspace. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood +# 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"}' &> 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 +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 '.mcp | [.servers[] | {name, profile, agents: (.agents | keys)}]' home/.databricks/aitools/skills/.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..ceb8c486282 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/home/.codex/config.toml @@ -0,0 +1,2 @@ +[mcp_servers.other x] +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..52b28af6e45 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt @@ -0,0 +1,48 @@ + +=== 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 +{ + "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 1: expected '.' or ']' to end table name, but got 'x' instead" + }, + { + "name": "claude-code", + "status": "installed", + "path": "[TEST_TMP_DIR]/home/.claude.json" + } + ] + } + ], + "error_category": "TYPE_UNSPECIFIED" +} + +=== The broken file is left as it was + +>>> cat home/.codex/config.toml +[mcp_servers.other x] +command = "broken" + +=== State records only the agent that succeeded + +>>> jq .mcp | [.servers[] | {name, agents: (.agents | keys)}] home/.databricks/aitools/skills/.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..b0bef562d44 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/script @@ -0,0 +1,17 @@ +# 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 + +# The services of system.ai, created in the fake workspace. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood + +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 + +title "State records only the agent that succeeded" +trace jq '.mcp | [.servers[] | {name, agents: (.agents | keys)}]' home/.databricks/aitools/skills/.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..ed892dd4ef5 --- /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 +aitools help does not list mcp + +=== 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 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. + +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, 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 + +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..9a8f70f927b --- /dev/null +++ b/acceptance/cmd/aitools/mcp/hidden/script @@ -0,0 +1,6 @@ +title "The mcp group is hidden from aitools help" +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" +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..340a89ac46e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/test.toml @@ -0,0 +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"] +Env.CLAUDE_CONFIG_DIR = "" +Env.CODEX_HOME = "" +Env.COPILOT_HOME = "" +Env.GEMINI_CLI_HOME = "" +Env.XDG_CONFIG_HOME = "" +Env.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/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..20c296c7457 --- /dev/null +++ b/cmd/aitools/mcp.go @@ -0,0 +1,114 @@ +package aitools + +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" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/spf13/cobra" +) + +// 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" + 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 +// 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. 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 + } + // 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 { + case errors.Is(err, agentconfig.ErrConfigInvalid): + return categoryAgentConfigInvalid + case errors.Is(err, agentconfig.ErrNotOwned): + return categoryNameConflict + } + return classifyInstallError(err) +} + +// 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 "", 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 "", 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. +func profileFlag(cmd *cobra.Command) string { + return cmd.Flag("profile").Value.String() +} diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go new file mode 100644 index 00000000000..960c5b2d458 --- /dev/null +++ b/cmd/aitools/mcp_add.go @@ -0,0 +1,992 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "path/filepath" + "slices" + "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/installer" + "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/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/spf13/cobra" +) + +// 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" + statusFailed = "failed" +) + +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. failed holds the agents that cannot take the server, with the +// reason; they are not written. +type addPlan struct { + service mcp.Service + name string + failed map[*agents.Agent]error +} + +// 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 + agents []*agents.Agent + explicit bool + force bool + binary string + // 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 + // 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`. +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 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. + +Supported agents: ` + strings.Join(mcpAgentNames(), ", "), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + jsonMode := installOutputIsJSON(cmd) + + out := &addOutput{ + Scope: installer.ScopeGlobal, + 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) + } + out.slashPaths() + if err := renderJSON(cmd.OutOrStdout(), out); err != nil { + return errors.Join(runErr, 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, 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") + return cmd +} + +// 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 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) + if err := validateMCPAddFlags(ctx, f, names, agentNames, jsonMode); err != nil { + return err + } + 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 + } + 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", 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...) + } + } + + // 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 + } + out.Profile = profileName + + state, err := mcp.LoadState(ctx) + if err != nil { + return err + } + for _, name := range fullNames { + if _, err := mcp.CheckBinding(ctx, state, name, profileName, configFile); 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, 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 { + return err + } + out.Host = w.Config.Host + workspaceID, err := auth.ResolveWorkspaceID(ctx, w) + if err != nil { + 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 describeRequestError(profileName, err) + } + picked, err := pickServices(location, fullNames, services) + if err != nil { + return err + } + for i := range plans { + plans[i].service = picked[i] + } + + req := addRequest{ + jsonMode: jsonMode, + profile: profileName, + host: w.Config.Host, + workspaceID: workspaceID, + agents: targets, + explicit: explicit, + force: f.force, + binary: binary, + configFile: configFile, + pinned: pinned, + refused: refused, + } + 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.") + } + + printAddHeader(ctx, req) + servers := make([]addServerOutput, 0, len(plans)) + for _, plan := range plans { + servers = append(servers, installServer(ctx, req, plan)) + } + out.Servers = servers + + // 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) +} + +// 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 != "" { + // 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) + } + } + + 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 +} + +// 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 + } + return path, path, nil +} + +// 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, err := mcp.BoundConfig(name, configFile) + if err != nil { + return nil, err + } + w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) + if err != nil { + return nil, err + } + if err := mcp.AuthenticateBound(ctx, cfg, name); err != nil { + return nil, err + } + 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. +func describeListError(profileName string, err error) error { + listErr, ok := errors.AsType[*mcp.ListError](err) + if !ok { + return err + } + 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()) +} + +// 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() { + 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, failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "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 +} + +// 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 { + case len(parts) == 1: + leaves = true + case isFullName(parts): + 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, "."); { + case len(parts) == 1: + fullName = location + "." + name + 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) + } + 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 +} + +// 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) { + 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 { + 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) + } + return nil, failure(categoryServiceNotFound, nil, "%s is not an MCP service in %s. Available: %s", name, location, strings.Join(available, ", ")) + } + picked = append(picked, services[i]) + } + return picked, nil +} + +// 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 +} + +// 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, in planInput, state *mcp.State, fullNames []string) ([]addPlan, error) { + taken := map[string]bool{} + for i := range state.Servers { + taken[state.Servers[i].Name] = true + } + + plans := make([]addPlan, 0, len(fullNames)) + for _, fullName := range fullNames { + entry, err := mcp.FindEntry(ctx, state, fullName, in.binding.profile, in.binding.configFile) + if err != nil { + return nil, err + } + 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, failed: failed}) + } + return plans, nil +} + +// 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, in.held, in.targets, fullName, b) + if err != nil { + return "", nil, err + } + switch len(owned) { + case 0: + case 1: + stored = owned[0] + default: + 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 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) + } + 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) + } + failed, err := nameFailures(ctx, in, fullName, stored, true) + return stored, failed, err + } + + 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) + } + failed, err := nameFailures(ctx, in, fullName, in.nameFlag, false) + return in.nameFlag, failed, err + } + + base := mcp.ServerName(fullName, b.profile) + if base == "" { + 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 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, 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. +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) + } + } + } + slices.Sort(names) + return names, nil +} + +// 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 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 append(errs, err) +} + +// 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, 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, + Service: plan.service.FullName, + SignInRequired: plan.service.SignInRequired, + Agents: make([]addAgentOutput, 0, len(req.agents)), + } + for _, a := range req.agents { + var path string + var status agentconfig.Status + err := req.refused[a] + if err == nil { + 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) + } + + result := addAgentOutput{Name: a.Name} + switch { + case err != nil: + result.Status = statusFailed + result.ErrorCategory = errorCategory(err) + result.Message = err.Error() + result.err = err + case status == agentconfig.StatusUnchanged: + result.Status = statusReasserted + result.Path = path + default: + result.Status = statusInstalled + result.Path = path + } + out.Agents = append(out.Agents, result) + } + return out +} + +// 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 := a.InstalledForMCP(ctx) + if err != nil { + return err + } + if installed { + return nil + } + 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 +// 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{} + for _, a := range server.Agents { + if a.Status != statusFailed { + paths[a.Name] = a.Path + } + } + if len(paths) == 0 { + continue + } + if err := recordServer(ctx, state, req, plans[i], paths); err != nil { + errs = append(errs, err) + continue + } + recorded = true + } + if recorded { + if err := mcp.SaveState(ctx, state); err != nil { + errs = append(errs, err) + } + } + return errors.Join(errs...) +} + +// recordServer stores the install of plan's server for req.profile in configFile, with the agents that succeeded +// and their config paths. +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, + 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.pinned + 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: agentScopeUser} + } + return nil +} + +// 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. Every distinct failure is kept; one that repeats for each +// service is reported once. +func agentFailureError(req addRequest, servers []addServerOutput) error { + var errs []error + fail := req.explicit + for _, server := range servers { + reached := false + for _, a := range server.Agents { + if a.err == nil { + reached = true + continue + } + errs = appendDistinct(errs, a.err) + } + if !reached { + fail = true + } + } + if len(errs) == 0 || !fail { + 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 == agents.NameCursor && 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, shellquote.BashArg(server.Service), shellquote.BashArg(req.profile))) + } + if cursor { + cmdio.LogString(ctx, cursorApprovalHint(server.Name)) + } + } +} + +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, path) + default: + return fmt.Sprintf("%-12s installed %s", a.Name, 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..981c350d3c5 --- /dev/null +++ b/cmd/aitools/mcp_add_test.go @@ -0,0 +1,1362 @@ +package aitools + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "io/fs" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strconv" + "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/aitools/mcp/agentconfig" + "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 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 '_'`, + }, + { + 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 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 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 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: "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) { + 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 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.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) + 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 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() + + // 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.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) + 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, categoryServiceNotFound, errorCategory(err)) +} + +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, categoryPermissionDenied, 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)) + + 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, 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 +} + +// 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", codexHomeEnv, copilotHomeEnv, "GEMINI_CLI_HOME", "XDG_CONFIG_HOME", "APPDATA", "DATABRICKS_CONFIG_FILE"} + +// withDatabricksBinary makes the command name the databricks command, as the installed CLI has it. +func withDatabricksBinary(t *testing.T) { + t.Helper() + 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 +// 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 +} + +// 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) { + 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 +} + +// 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) + 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", + } + req.refused = refusedAgents(ctx, req.agents) + 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; 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) +} + +// 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() + 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 +// 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) { + 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, 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.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, 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) + }) + + 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, 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) + }) + + 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 := 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) + }) + + 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, 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) + }) + + 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, 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 := 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 := 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 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}}} + 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) { + 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"}}}, + } + 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) + }) + + 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) { + ctx := t.Context() + 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.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")) + path, pinned, err = addConfigFile(ctx) + require.NoError(t, err) + assert.True(t, filepath.IsAbs(path), path) + assert.Equal(t, path, pinned) +} + +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")) +} + +// 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(cmdio.MockDiscard(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) +} + +// 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) + 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(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")) +} + +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(mcp.BindProfileHostChanged), errorCategory(err)) + assert.Contains(t, err.Error(), "was set up for workspace 111, but profile \"logfood\" now points at workspace 222") +} + +// 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) + 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(mcp.BindProfileHostChanged), 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) +} + +// 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) + 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(mcp.BindAuthFailed), errorCategory(err)) + assert.Contains(t, err.Error(), "databricks auth login --profile logfood") +} + +// 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) + 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)) +} + +// 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") + 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") } + +// 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) + 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) +} + +// 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{ + {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 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{installed, mixed})) + assert.NoError(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{installed})) +} + +func TestErrorCategoryMapsTypedFailures(t *testing.T) { + 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(mcp.BindAuthFailed), errorCategory(mcp.AuthError("logfood", errors.New("no token")))) +} + +// 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) + 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 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) + 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 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) + 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 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) + 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 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) + 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 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") + + 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\", 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. +func TestMcpAddRejectedTokenIsAuthFailed(t *testing.T) { + home := withHome(t) + 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") + 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 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 := 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) + 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")) +} + +// 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.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 +// 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. +// 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) + }{ + { + name: "host changed", + setup: setupHostChanged, + }, + { + name: "workspace changed on the same host", + setup: setupWorkspaceChanged, + }, + { + name: "entry pinned to another config file", + setup: setupOtherConfigFile, + }, + { + name: "profile gone while an entry is bound to it", + setup: setupProfileGoneWithEntry, + }, + { + name: "profile gone with no entry", + setup: setupProfileGoneWithoutEntry, + }, + } + 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)) + 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}})) +} + +// 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()) + // 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")) +} + +// 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) + var 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.go b/cmd/aitools/mcp_binary.go new file mode 100644 index 00000000000..1b5f2659d6a --- /dev/null +++ b/cmd/aitools/mcp_binary.go @@ -0,0 +1,41 @@ +package aitools + +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(binaryArg0, exec.LookPath) +} + +// 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 { + // 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 + } + return abs, true + } + if found, err := lookPath(arg0); err == nil && filepath.IsAbs(found) { + return found, true + } + return arg0, false +} diff --git a/cmd/aitools/mcp_binary_test.go b/cmd/aitools/mcp_binary_test.go new file mode 100644 index 00000000000..0ae6b3f616b --- /dev/null +++ b/cmd/aitools/mcp_binary_test.go @@ -0,0 +1,121 @@ +package aitools + +import ( + "errors" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBinaryPathFor(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) + absolute := filepath.Join(t.TempDir(), "bin", "databricks") + foundPath := filepath.Join(t.TempDir(), "databricks") + relativeOnPath := filepath.Join("bin", "databricks") + + tests := []struct { + 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 + 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, + wantPath: absolute, + wantAbs: true, + }, + { + name: "relative arg0 with a directory is made absolute, not looked up", + arg0: filepath.Join("bin", "databricks"), + 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", + onPath: foundPath, + 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", + 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", + onPath: foundPath, + wantLookup: "databricks", + wantPath: foundPath, + wantAbs: true, + }, + { + name: "bare name not on PATH falls back to the bare name", + arg0: "databricks", + onPath: "", + 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", + onPath: "", + wantLookup: "dbx", + wantPath: "dbx", + }, + { + name: "relative PATH entry falls back to the bare name", + arg0: "databricks", + onPath: relativeOnPath, + wantLookup: "databricks", + wantPath: "databricks", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + 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, lookup.asked) + }) + } +} + +func mustAbs(t *testing.T, path string) string { + t.Helper() + abs, err := filepath.Abs(path) + 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 +} diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go new file mode 100644 index 00000000000..ed571a8032d --- /dev/null +++ b/cmd/aitools/mcp_test.go @@ -0,0 +1,124 @@ +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()) + assert.Equal(t, categoryProfileRequired, errorCategory(err)) + }) + + 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.Equal(t, categoryNoProfile, errorCategory(err)) + 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(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)) + + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(errors.New("some failure"))) +} diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index a75d07d2a63..48c45758ec7 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -63,6 +63,11 @@ type Agent struct { // MCP describes where the agent keeps user-scope MCP server entries, or nil // when this CLI does not write them (Pi, Goose, Kiro). MCP *mcpconfig.Spec + // mcpHomeEnv names the variable that moves the MCP config file out of ConfigDir: CODEX_HOME for Codex + // (https://learn.chatgpt.com/docs/config-file/config-advanced) and COPILOT_HOME for Copilot CLI + // (https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference). + // Empty when the file lives in ConfigDir, or when ConfigDir itself follows the variable. + mcpHomeEnv string // 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 +99,30 @@ func (a *Agent) Detected(ctx context.Context) bool { return err == nil && HasDatabricksSkillsIn(skillsDir) } +// InstalledForMCP reports whether the agent is installed for MCP servers. When mcpHomeEnv is set, the directory it names +// is the only signal: the agent's own directory may exist while the MCP home does not, and writing the MCP config would +// create that home. Otherwise Detected, HasBinary, or the MCP config file counts. +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 + } + if a.MCP == nil { + return false, nil + } + path, err := a.MCP.Path(ctx) + if err != nil { + return false, err + } + return linkExists(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 +264,7 @@ var Registry = []*Agent{ Root: "mcp_servers", Entry: stdioEntry, }, + mcpHomeEnv: codexHomeEnv, }, { Name: NameOpenCode, @@ -263,6 +293,7 @@ var Registry = []*Agent{ Root: mcpServersKey, Entry: copilotMCPEntry, }, + mcpHomeEnv: copilotHomeEnv, }, { Name: NameAntigravity, @@ -453,10 +484,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) @@ -523,7 +560,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/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/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/mcp/binding.go b/libs/aitools/mcp/binding.go index 17320c755e3..0b4a2741c73 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -7,9 +7,11 @@ import ( "io" "net/http" "path/filepath" + "strings" "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,62 +78,103 @@ 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) } 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 - } - 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) + 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 state entry for service and profileName bound to configFile, the config file the caller +// 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 { + return nil, err + } + if entry == nil { + _, err := profileFor(service, profileName, configFile, nil) + return nil, err + } + p, err := profileFor(entry.Name, profileName, configFile, entry) + if err != nil { + return nil, err + } + if drift := driftOf(*entry, p); drift != nil { + return nil, driftError(entry.Name, profileName, *entry, *drift) + } + return entry, 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): + 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: + 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. @@ -146,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)) @@ -184,31 +230,31 @@ 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} } -// 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. +// 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. // // 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 // 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 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) + 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..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 { @@ -780,3 +780,100 @@ 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. +// 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) + 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 := 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)) + 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) { + 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 := bindingTestCheck(t, state) + 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 := bindingTestCheck(t, bindingTestState(other, bindingTestHost)) + requireBindError(t, err, mcp.BindProfileHostChanged) +} + +// 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)) + 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) +} + +// 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 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 { + 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()) + }) + } +} diff --git a/libs/aitools/mcp/configfile.go b/libs/aitools/mcp/configfile.go index 7244fd9bab2..8e2b7346d5d 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 @@ -26,14 +26,17 @@ 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. 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 b1f16412ba3..7660a8a98cf 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,11 +54,22 @@ 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) } +// 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) { 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/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 } diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 07c9a910f33..cfd7e4d6d4e 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -71,6 +71,27 @@ 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" + AgentFlag = "--agent" +) + +// 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 EnvKeys { + 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 { 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)) + }) + } +}