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..7977947a028 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood-$UNIQUE_NAME 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..4b340d665dc --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-agent-not-installed/output.txt @@ -0,0 +1,41 @@ + +=== 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" + } + ] + } + ] +} + +=== 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..8d80c7ca7da --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-default-config/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME 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-name-notices/.databrickscfg b/acceptance/cmd/aitools/mcp/add-name-notices/.databrickscfg new file mode 100644 index 00000000000..7977947a028 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-name-notices/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood-$UNIQUE_NAME diff --git a/acceptance/cmd/aitools/mcp/add-name-notices/home/.cursor/mcp.json b/acceptance/cmd/aitools/mcp/add-name-notices/home/.cursor/mcp.json new file mode 100644 index 00000000000..3e3f820bd7a --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-name-notices/home/.cursor/mcp.json @@ -0,0 +1 @@ +{"mcpServers": {"genie_one_mcp-dogfood": {"command": "users-own", "args": ["users-own"]}, "user-server": {"command": "true"}}} diff --git a/acceptance/cmd/aitools/mcp/add-name-notices/out.test.toml b/acceptance/cmd/aitools/mcp/add-name-notices/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-name-notices/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-name-notices/output.txt b/acceptance/cmd/aitools/mcp/add-name-notices/output.txt new file mode 100644 index 00000000000..6a18ace2b03 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-name-notices/output.txt @@ -0,0 +1,35 @@ + +=== A generated name that an entry this CLI did not write holds gets a suffix, and the output says who holds the base name + +>>> [CLI] aitools mcp add --profile dogfood --names genie_one_mcp --agents cursor +Profile dogfood ([DATABRICKS_URL]) +Agents (named with --agents): cursor + +genie_one_mcp-dogfood-2 system.ai.genie_one_mcp + genie_one_mcp-dogfood is taken in Cursor; using genie_one_mcp-dogfood-2 + cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json + +Cursor may ask you to approve genie_one_mcp-dogfood-2. Run: cursor-agent mcp enable genie_one_mcp-dogfood-2 (`agent` in the Cursor docs), or approve it in Cursor's MCP settings. + +=== --force replaces the user's own entry, and the output says so + +>>> [CLI] aitools mcp add --profile dogfood --names dbsql --name user-server --agents cursor --force +Profile dogfood ([DATABRICKS_URL]) +Agents (named with --agents): cursor + +user-server system.ai.dbsql + replaced user-server in Cursor (not written by this CLI) + cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json + Needs an account admin to turn on the Enhanced Unity Gateway preview. + +Cursor may ask you to approve user-server. Run: cursor-agent mcp enable user-server (`agent` in the Cursor docs), or approve it in Cursor's MCP settings. + +=== The user's entry that was not replaced is intact + +>>> jq .mcpServers["genie_one_mcp-dogfood"] home/.cursor/mcp.json +{ + "command": "users-own", + "args": [ + "users-own" + ] +} diff --git a/acceptance/cmd/aitools/mcp/add-name-notices/script b/acceptance/cmd/aitools/mcp/add-name-notices/script new file mode 100644 index 00000000000..be3f7b227cb --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-name-notices/script @@ -0,0 +1,19 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +# 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 dogfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-dogfood +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=dbsql" --profile dogfood --json '{"comment": "Query your SQL warehouses"}' &> LOG.create-dbsql-dogfood + +# home/.cursor/mcp.json holds the user's own Cursor servers. Their names are the ones the CLI would generate or use. + +title "A generated name that an entry this CLI did not write holds gets a suffix, and the output says who holds the base name" +trace $CLI aitools mcp add --profile dogfood --names genie_one_mcp --agents cursor + +title "--force replaces the user's own entry, and the output says so" +trace $CLI aitools mcp add --profile dogfood --names dbsql --name user-server --agents cursor --force + +title "The user's entry that was not replaced is intact" +trace jq '.mcpServers["genie_one_mcp-dogfood"]' home/.cursor/mcp.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..8d80c7ca7da --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME 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..a5f05b70a05 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -0,0 +1,76 @@ + +=== Text output: Cursor approval hint, and the sign-in step for github + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code,cursor +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code, cursor + +genie_one_mcp-logfood system.ai.genie_one_mcp + claude-code installed [TEST_TMP_DIR]/home/.claude.json + cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json + +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 + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). + +Cursor may ask you to approve genie_one_mcp-logfood, github-logfood. Run cursor-agent mcp enable for each (`agent` in the Cursor docs), or approve them 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 + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). + +=== 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..8d80c7ca7da --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME 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..c1eeb2bceb5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-agents/output.txt @@ -0,0 +1,157 @@ + +=== 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" + } + ] + } + ] +} + +=== 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..7977947a028 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-two-profiles/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood-$UNIQUE_NAME 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-unknown-profile/.databrickscfg b/acceptance/cmd/aitools/mcp/add-unknown-profile/.databrickscfg new file mode 100644 index 00000000000..7977947a028 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unknown-profile/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood-$UNIQUE_NAME diff --git a/acceptance/cmd/aitools/mcp/add-unknown-profile/out.test.toml b/acceptance/cmd/aitools/mcp/add-unknown-profile/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unknown-profile/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/add-unknown-profile/output.txt b/acceptance/cmd/aitools/mcp/add-unknown-profile/output.txt new file mode 100644 index 00000000000..3d010604a99 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unknown-profile/output.txt @@ -0,0 +1,11 @@ + +=== A profile that does not exist is refused before any request, and the error lists the profiles that exist + +>>> [CLI] aitools mcp add --profile logfod --names system.ai.genie_one_mcp --agents claude-code +Error: MCP server system.ai.genie_one_mcp needs Databricks profile "logfod", which does not exist in [TEST_TMP_DIR]/.databrickscfg. Set it up with: databricks auth login --profile logfod +Existing profiles: logfood, dogfood + +=== Nothing was written + +>>> ls -A home +.claude diff --git a/acceptance/cmd/aitools/mcp/add-unknown-profile/script b/acceptance/cmd/aitools/mcp/add-unknown-profile/script new file mode 100644 index 00000000000..7059187f7df --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unknown-profile/script @@ -0,0 +1,11 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +mkdir -p home/.claude +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +title "A profile that does not exist is refused before any request, and the error lists the profiles that exist" +musterr trace $CLI aitools mcp add --profile logfod --names system.ai.genie_one_mcp --agents claude-code + +title "Nothing was written" +trace ls -A home 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..8d80c7ca7da --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME 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..fe6a320f15b --- /dev/null +++ b/acceptance/cmd/aitools/mcp/add-unparsable-config/output.txt @@ -0,0 +1,47 @@ + +=== 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" + } + ] + } + ] +} + +=== 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..bcf1f4e5cda --- /dev/null +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -0,0 +1,62 @@ + +=== 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 --help +Databricks managed MCP servers give coding agents tools for Databricks services, such as GitHub or web search. +Use 'add' to install them into coding agents, bound to a Databricks profile. + +Usage: + databricks aitools mcp [command] + +Available Commands: + add Install Databricks managed MCP servers into coding agents + +Flags: + -h, --help[=true|false] help for mcp + +Global Flags: + --debug[=true|false] 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) + +Use "databricks aitools mcp [command] --help" for more information about a command. + +>>> [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. + +Services 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 when there are no leaf names. +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] + +Examples: + # Install GitHub and web search for Claude Code and Cursor, bound to the dogfood profile + databricks aitools mcp add --names github,web_search --agents claude-code,cursor --profile dogfood + +Flags: + --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) + --force[=true|false] Overwrite an existing server with the same name that this CLI did not write + -h, --help[=true|false] 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 when there are no leaf names) + --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[=true|false] 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..6e7aba4345b --- /dev/null +++ b/acceptance/cmd/aitools/mcp/hidden/script @@ -0,0 +1,7 @@ +title "The mcp group is hidden from aitools help" +trace $CLI aitools --help | tee LOG.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 --help +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 2805d891ef5..ad6faedd199 100644 --- a/cmd/aitools/aitools.go +++ b/cmd/aitools/aitools.go @@ -32,4 +32,5 @@ https://github.com/databricks/databricks-agent-skills` cmd.AddCommand(NewListCmd()) cmd.AddCommand(NewVersionCmd()) cmd.AddCommand(newAutoInstallCmd()) + cmd.AddCommand(NewMcpCmd()) } diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go new file mode 100644 index 00000000000..ee3d35a8caa --- /dev/null +++ b/cmd/aitools/mcp.go @@ -0,0 +1,121 @@ +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/auth" + "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", + Long: `Databricks managed MCP servers give coding agents tools for Databricks services, such as GitHub or web search. +Use 'add' to install them into coding agents, bound to a Databricks profile.`, + Hidden: true, + } + cmd.AddCommand(NewMcpAddCmd()) + return cmd +} + +// failure returns the message shown to the user, tagged with the category reported in JSON. +func failure(category protos.AitoolsErrorCategory, format string, args ...any) error { + return withCategory(fmt.Errorf(format, args...), category) +} + +// withAgentsHint names --agents as the way out of a read-only agent config file. The writer does not know the +// flag, so the command that takes --agents adds the hint. Other errors are returned as they are. +func withAgentsHint(err error, agent string) error { + ro, ok := errors.AsType[*agentconfig.ReadOnlyError](err) + if !ok { + return err + } + return fmt.Errorf("%s is read-only; make it writable or leave %s out of --agents: %w", ro.Path, agent, ro.Err) +} + +// 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[*categorizedError](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, mcpWorkspaceProfile) + if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { + return "", err + } + switch len(profiles) { + case 0: + return "", failure(categoryNoProfile, "no workspace profiles in %s. Run: databricks auth login --host --profile ", filepath.ToSlash(path)) + case 1: + return profiles[0].Name, nil + } + return "", failure(categoryProfileRequired, "several workspace profiles exist; pass --profile. Found: %s", strings.Join(profiles.Names(), ", ")) +} + +// mcpWorkspaceProfile reports whether auto-detect may pick p. It accepts the profiles that --profile accepts: a +// workspace profile, or a profile whose host names its workspace in ?w=, which is an account profile otherwise. +func mcpWorkspaceProfile(p profile.Profile) bool { + return profile.MatchWorkspaceProfiles(p) || auth.ExtractHostQueryParams(p.Host).WorkspaceID != "" +} + +// 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..d1a5f0b9a82 --- /dev/null +++ b/cmd/aitools/mcp_add.go @@ -0,0 +1,1181 @@ +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/databrickscfg/profile" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/shellquote" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/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" +) + +// serverNameRules is the rule that a server name must meet, in the messages that refuse a name. +const serverNameRules = "use lowercase letters, digits, '-' and '_', with no \"__\" and no leading or trailing '-' or '_'" + +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,omitempty"` + 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"` + + // notes explain the name and the entries that were replaced. The text output prints them; the JSON does not. + notes []string + // note is the service's prerequisite, printed after the agent results. The text output prints it; the JSON does not. + note string +} + +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. note explains the name when it got a suffix. replaced holds the agents whose entry +// --force replaces. +type addPlan struct { + service mcp.Service + name string + failed map[*agents.Agent]error + note string + replaced map[*agents.Agent]bool +} + +// 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. + +Services 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 when there are no leaf names. +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(), ", "), + Example: ` # Install GitHub and web search for Claude Code and Cursor, bound to the dogfood profile + databricks aitools mcp add --names github,web_search --agents claude-code,cursor --profile dogfood`, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + jsonMode := installOutputIsJSON(cmd) + + out := &addOutput{ + Scope: installer.ScopeGlobal, + Servers: []addServerOutput{}, + } + 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 when there are no leaf names)") + 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. Refusals that need no request come before the first one. +// Named agents' failures are returned as 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, "--name needs exactly one service in --names") + } + targets, explicit, detectErrs, err := targetAgents(ctx, agentNames) + if err != nil { + return err + } + binary, absolute := mcpBinaryPath() + if !mcpconfig.IsDatabricksBinary(binary) { + return failure(protos.AitoolsErrorCategoryUncategorized, "%s is not the databricks binary; coding agents start MCP servers with databricks or databricks.exe", filepath.ToSlash(binary)) + } + held, unreadable := readAgentEntries(ctx, targets) + var refused map[*agents.Agent]error + if explicit { + refused = refusedAgents(ctx, targets) + } + for a, err := range unreadable { + if refused == nil { + refused = map[*agents.Agent]error{} + } + refused[a] = err + } + for a, err := range detectErrs { + if refused == nil { + refused = map[*agents.Agent]error{} + } + refused[a] = err + } + // Nothing can be written when every agent is refused, so the refusals end the run before any request. Detected + // agents are refused only for an unreadable config, and that ends the run the same way. + if len(refused) == len(targets) { + var errs []error + for _, a := range targets { + errs = append(errs, refused[a]) + } + return withCategory(errors.Join(errs...), errorCategory(errs[0])) + } + + // 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 withExistingProfiles(ctx, state, profileName, err) + } + } + in := planInput{ + binding: addBinding{profile: profileName, configFile: configFile}, + nameFlag: f.name, + force: f.force, + explicit: explicit, + targets: targets, + held: held, + refused: refused, + } + plans, err := planAdds(ctx, in, state, fullNames) + if err != nil { + return err + } + if err := planningRefusals(plans, targets, refused); 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 { + // A value that names no agent, such as ",", must not fall back to every detected agent. + if f.agents != "" && len(agentNames) == 0 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, "--agents names no agent; pass one or more of: %s", strings.Join(mcpAgentNames(), ", ")) + } + 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, "--name %q is not a valid server name: "+serverNameRules, f.name) + } + // The name is ASCII once it comes out of Sanitize unchanged, so its length in bytes is its length in characters. + if len(f.name) > mcp.MaxServerNameLen { + return failure(protos.AitoolsErrorCategoryInvalidFlags, "--name must be at most %d characters, got %d; pick a shorter name", mcp.MaxServerNameLen, len(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, "--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, "interactive selection is not available yet; pass --names and --agents") + } + return nil + } + if len(names) == 0 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, "--names is required so the command runs without interactive prompts") + } + return nil +} + +// validateLocation checks that location is .. +func validateLocation(location string) error { + catalog, schema, ok := strings.Cut(location, ".") + if !ok || catalog == "" || schema == "" || strings.Contains(schema, ".") { + return failure(protos.AitoolsErrorCategoryInvalidFlags, "--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 +} + +// withExistingProfiles adds the workspace profiles that exist to a profile-not-found failure, when no server is +// installed for the profile. A mistyped --profile is the usual cause then. Other failures are returned unchanged, and so +// is the failure of an installed server, whose recreate hint is the one to follow. +func withExistingProfiles(ctx context.Context, state *mcp.State, profileName string, err error) error { + if be, ok := errors.AsType[*mcp.BindError](err); !ok || be.Category != mcp.BindProfileNotFound || !errors.Is(err, mcp.ErrProfileMissing) { + return err + } + if slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { return e.Profile == profileName }) { + return err + } + profiles, loadErr := profile.GetProfiler(ctx).LoadProfiles(ctx, mcpWorkspaceProfile) + if loadErr != nil && !errors.Is(loadErr, profile.ErrNoConfiguration) { + return err + } + if len(profiles) == 0 { + return err + } + return fmt.Errorf("%w\nExisting profiles: %s", err, strings.Join(profiles.Names(), ", ")) +} + +// 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 + } + // The workspace commands refuse an account-only profile after the SDK has resolved its workspace ID, so this + // checks the resolved config, which includes a workspace given in the host or found by discovery. It runs before + // the credential is sent. + if cfg.AccountID != "" && (cfg.WorkspaceID == "" || cfg.WorkspaceID == profile.WorkspaceIDNone) { + return nil, failure(protos.AitoolsErrorCategoryUncategorized, "profile %q is account-level, with no workspace_id in %s; MCP servers need a profile for one workspace", name, filepath.ToSlash(configFile)) + } + 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 withCategory(fmt.Errorf("profile %s: %w", profileName, listErr), category) +} + +// targetAgents returns the agents named by --agents, or else every detected MCP-capable agent. +// explicit reports whether the names were given. +// detectErrs holds the agents whose detection failed. They are not written, and each reports its failure. +func targetAgents(ctx context.Context, names []string) ([]*agents.Agent, bool, map[*agents.Agent]error, error) { + if len(names) > 0 { + list, err := resolveMCPAgents(names) + return list, true, nil, err + } + var detected []*agents.Agent + var detectErrs map[*agents.Agent]error + for _, a := range agents.MCPAgents() { + installed, err := a.InstalledForMCP(ctx) + if err != nil { + // The agent is kept, so that its failure is reported in its result rather than the agent being skipped. + if detectErrs == nil { + detectErrs = map[*agents.Agent]error{} + } + detectErrs[a] = err + detected = append(detected, a) + } else if installed { + detected = append(detected, a) + } + } + if len(detected) == 0 { + return nil, false, nil, failure(protos.AitoolsErrorCategoryUnknownAgent, "no MCP-capable coding agents detected; pass --agents with one of: %s", strings.Join(mcpAgentNames(), ", ")) + } + return detected, false, detectErrs, 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: + if suggestion := agentSuggestion(name); suggestion != "" { + return nil, failure(protos.AitoolsErrorCategoryUnknownAgent, "unknown agent %q; did you mean %s? Supported: %s", name, suggestion, supported) + } + return nil, failure(protos.AitoolsErrorCategoryUnknownAgent, "unknown agent %q. Supported: %s", name, supported) + case !a.SupportsMCP(): + return nil, failure(protos.AitoolsErrorCategoryUnknownAgent, "%s does not support MCP servers. Supported: %s", name, supported) + } + if !slices.Contains(result, a) { + result = append(result, a) + } + } + return result, nil +} + +// agentSuggestion returns the MCP-capable agent that an unknown name most likely means: one whose binary +// is the name, or whose name starts with it. It returns "" when no agent or several agents match. +func agentSuggestion(name string) string { + var match string + for _, a := range agents.MCPAgents() { + if a.Binary != name && !strings.HasPrefix(a.Name, name) { + continue + } + if match != "" { + return "" + } + match = a.Name + } + return match +} + +// mcpAgentNames returns the names of the agents that take MCP servers. +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, "--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, "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, "%s is not in %s; pass --location to match its schema", name, location) + } + fullName = name + default: + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, "%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, "%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, "%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 + +// readAgentEntries returns the entries of each target agent's config. An agent whose config cannot be read is +// returned in unreadable, and has no entries here. +func readAgentEntries(ctx context.Context, targets []*agents.Agent) (agentEntries, map[*agents.Agent]error) { + held := make(agentEntries, len(targets)) + var unreadable map[*agents.Agent]error + for _, a := range targets { + entries, err := agentconfig.Read(ctx, a) + if err != nil { + if unreadable == nil { + unreadable = map[*agents.Agent]error{} + } + unreadable[a] = err + continue + } + held[a] = entries + } + return held, unreadable +} + +// 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 + // refused are the agents that will not be written, so a conflict in them does not refuse the run. + refused map[*agents.Agent]error +} + +// 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)) + chosen := map[string]bool{} + 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, note, err := chooseName(ctx, in, state, fullName, entry, taken) + if err != nil { + return nil, err + } + // A name recovered from an agent entry can repeat across services only through a hand edit; two plans must not share it. + if chosen[name] { + return nil, failure(categoryNameConflict, "%s and another requested server both use %s; remove one of the entries first", fullName, name) + } + chosen[name] = true + taken[name] = true + plans = append(plans, addPlan{service: mcp.Service{FullName: fullName}, name: name, failed: failed, note: note, replaced: replacedByForce(in, name)}) + } + return plans, nil +} + +// chooseName returns the server name for fullName, the target agents that cannot take it, and a note when the name +// was generated with a suffix. 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, string, error) { + b := in.binding + owned, err := ownedNames(ctx, in.held, in.targets, fullName, b) + if err != nil { + return "", nil, "", err + } + stored := "" + if entry != nil { + stored = entry.Name + if stored == "" { + return "", nil, "", failure(protos.AitoolsErrorCategoryUncategorized, "%s is installed on profile %s without a server name in the state; remove that entry and add it again", fullName, b.profile) + } + // The agents must hold the state's name, or the run would add a second name for the same server beside theirs. + if slices.ContainsFunc(owned, func(name string) bool { return name != stored }) { + return "", nil, "", failure(categoryNameConflict, "%s is installed on profile %s as %s, but agents hold it as %s; remove the extra entries", fullName, b.profile, stored, strings.Join(owned, ", ")) + } + } else { + switch len(owned) { + case 0: + case 1: + stored = owned[0] + default: + return "", nil, "", failure(categoryNameConflict, "%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 != "" { + // A stored name gets the checks that --name gets, since a hand edit can leave a name that does not split as mcp____ does. + if mcp.Sanitize(stored) != stored { + return "", nil, "", failure(protos.AitoolsErrorCategoryUncategorized, "%s is installed as %q, which is not a valid server name: "+serverNameRules+". Run: databricks aitools mcp remove --profile %s and add it again", fullName, stored, shellquote.BashArg(b.profile)) + } + if len(stored) > mcp.MaxServerNameLen { + return "", nil, "", failure(protos.AitoolsErrorCategoryUncategorized, "%s is installed as %q, which is longer than the %d-character server name limit. Run: databricks aitools mcp remove --profile %s and add it again", fullName, stored, mcp.MaxServerNameLen, shellquote.BashArg(b.profile)) + } + if in.nameFlag != "" && in.nameFlag != stored { + return "", nil, "", failure(protos.AitoolsErrorCategoryInvalidFlags, "%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, "%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, "--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, err := mcp.ServerName(fullName, b.profile) + if err != nil { + return "", nil, "", withCategory(fmt.Errorf("%w; pass --name", err), protos.AitoolsErrorCategoryInvalidFlags) + } + // 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 + } + } + name := mcp.UniqueName(base, func(n string) bool { return taken[n] || reserved[n] }) + return name, nil, suffixNote(in, base, name), nil +} + +// suffixNote explains a generated name that got a suffix: the target agents that hold the base name, or else another +// installed server. It is "" when the base name is used as it is. +func suffixNote(in planInput, base, name string) string { + if name == base { + return "" + } + var holders []string + for _, a := range in.targets { + if _, ok := in.held[a][base]; ok { + holders = append(holders, a.DisplayName) + } + } + if len(holders) == 0 { + return fmt.Sprintf("%s is taken by another installed server; using %s", base, name) + } + return fmt.Sprintf("%s is taken in %s; using %s", base, strings.Join(holders, ", "), name) +} + +// replacedByForce returns the target agents that hold name as an entry this CLI did not write, which --force replaces. +func replacedByForce(in planInput, name string) map[*agents.Agent]bool { + replaced := map[*agents.Agent]bool{} + if !in.force { + return replaced + } + for _, a := range in.targets { + if entry, ok := in.held[a][name]; ok && !mcpconfig.IsDatabricksEntry(entry) { + replaced[a] = true + } + } + return replaced +} + +// 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 { + if in.refused[a] != nil { + continue + } + 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, "%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, "%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, "--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. An agent in refused cannot take any server. It returns nil when some agent can take some server. +func planningRefusals(plans []addPlan, targets []*agents.Agent, refused map[*agents.Agent]error) error { + var errs []error + for _, plan := range plans { + for _, a := range targets { + err := refused[a] + if err == nil { + err = plan.failed[a] + } + if err == nil { + return nil + } + errs = appendDistinct(errs, err) + } + } + 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)), + } + var replaced []string + 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 := append(slices.Clone(mcpconfig.ServeCommand), 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) + // A detected agent was not named, so --agents is no way out for it. + if req.explicit { + err = withAgentsHint(err, a.Name) + } + } + + 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 + if plan.replaced[a] { + replaced = append(replaced, a.DisplayName) + } + } + out.Agents = append(out.Agents, result) + } + // Known is nil for a service outside system.ai, so only a known service can have a note. + if plan.service.Known != nil { + out.note = plan.service.Known.Note + } + if plan.note != "" { + out.notes = append(out.notes, plan.note) + } + if len(replaced) > 0 { + out.notes = append(out.notes, fmt.Sprintf("replaced %s in %s (not written by this CLI)", plan.name, strings.Join(replaced, ", "))) + } + 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, "%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...)} +} + +// printAddHeader prints the profile and host that the servers bind to, unless the output is JSON. +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. The Cursor approval +// hint comes once, after the servers. +func printAddResults(ctx context.Context, req addRequest, servers []addServerOutput) { + if req.jsonMode { + return + } + var cursorServers []string + for _, server := range servers { + cmdio.LogString(ctx, "") + cmdio.LogString(ctx, server.Name+" "+server.Service) + for _, note := range server.notes { + cmdio.LogString(ctx, " "+note) + } + succeeded := 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 { + cursorServers = append(cursorServers, server.Name) + } + } + 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 server.note != "" && succeeded { + cmdio.LogString(ctx, " "+server.note) + } + } + if len(cursorServers) > 0 { + cmdio.LogString(ctx, "") + cmdio.LogString(ctx, cursorApprovalHint(cursorServers)) + } +} + +// agentResultLine returns the text line that reports one agent's result for a server. +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 new servers. 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. enable takes one identifier. +func cursorApprovalHint(names []string) string { + if len(names) == 1 { + return fmt.Sprintf("Cursor may ask you to approve %s. Run: cursor-agent mcp enable %s (`agent` in the Cursor docs), or approve it in Cursor's MCP settings.", names[0], names[0]) + } + return fmt.Sprintf("Cursor may ask you to approve %s. Run cursor-agent mcp enable for each (`agent` in the Cursor docs), or approve them in Cursor's MCP settings.", strings.Join(names, ", ")) +} diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go new file mode 100644 index 00000000000..6b2acd6abd4 --- /dev/null +++ b/cmd/aitools/mcp_add_test.go @@ -0,0 +1,1613 @@ +package aitools + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "io/fs" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "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 '_'`, + }, + { + name: "name at the length cap is valid", flags: mcpAddFlags{name: strings.Repeat("a", mcp.MaxServerNameLen)}, names: []string{"github"}, + }, + { + name: "name over the length cap is refused", flags: mcpAddFlags{name: strings.Repeat("a", mcp.MaxServerNameLen+8)}, names: []string{"github"}, + want: "--name must be at most 32 characters, got 40; pick a shorter name", + }, + } + 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", OAuthProvider: "github"}, + } +} + +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 TestResolveMCPAgentsSuggestsCloseName(t *testing.T) { + supported := strings.Join(mcpAgentNames(), ", ") + tests := []struct { + name string + suggestion string + }{ + {"claude", "claude-code"}, + {"cursor-agent", "cursor"}, + {"open", "opencode"}, + // "co" is a prefix of codex and copilot, so no single agent is suggested. + {"co", ""}, + } + for _, tc := range tests { + _, err := resolveMCPAgents([]string{tc.name}) + require.Error(t, err) + want := fmt.Sprintf(`unknown agent %q. Supported: %s`, tc.name, supported) + if tc.suggestion != "" { + want = fmt.Sprintf(`unknown agent %q; did you mean %s? Supported: %s`, tc.name, tc.suggestion, supported) + } + assert.Equal(t, want, err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(err)) + } +} + +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() + withArg0(t, "databricks") +} + +// withArg0 sets the name the CLI was started with, which is argv[0], for the rest of the test. +func withArg0(t *testing.T, arg0 string) { + t.Helper() + saved := os.Args[0] + os.Args[0] = arg0 + t.Cleanup(func() { os.Args[0] = saved }) +} + +// noRequestServer returns a server that fails the test on any request. A command that refuses before +// it contacts the profile's host never reaches it. +func noRequestServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request before the refusal: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(server.Close) + return server +} + +// noAPIServer returns a server that fails the test on any API call. The SDK still reads host metadata +// and OIDC discovery when it configures a profile, so those paths are allowed. +func noAPIServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + t.Errorf("unexpected API request: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(server.Close) + return server +} + +// 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() + held, _ := readAgentEntries(t.Context(), targets) + in := planInput{binding: b, nameFlag: nameFlag, targets: targets, held: held} + 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.ErrorIs(t, err, mcp.ErrNoServerName) + assert.Equal(t, `cannot derive a server name from the service name: "数据" has no characters that can be used in a server name; 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 (`agent` in the Cursor docs), or approve it in Cursor's MCP settings.", + cursorApprovalHint([]string{"github-logfood"})) + assert.Equal(t, + "Cursor may ask you to approve github-logfood, web_search-logfood. Run cursor-agent mcp enable for each (`agent` in the Cursor docs), or approve them in Cursor's MCP settings.", + cursorApprovalHint([]string{"github-logfood", "web_search-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) +} + +// TestInstallServerNamesAgentsForReadOnlyConfig checks that a config file the user made read-only is refused with +// --agents named as the way out. The file keeps its bytes. +func TestInstallServerNamesAgentsForReadOnlyConfig(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("root writes read-only files") + } + withHome(t) + a := agents.ByName("claude-code") + path, err := a.MCP.Path(t.Context()) + require.NoError(t, err) + require.NoError(t, os.WriteFile(path, []byte(`{}`), 0o600)) + // Windows maps the mode to the read-only attribute. Writable again before the home directory is removed. + t.Cleanup(func() { _ = os.Chmod(path, 0o600) }) + require.NoError(t, os.Chmod(path, 0o400)) + + req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{a}, binary: "databricks"} + plan := addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"} + out := installServer(t.Context(), req, plan) + + require.Len(t, out.Agents, 1) + assert.Equal(t, statusFailed, out.Agents[0].Status) + assert.Contains(t, out.Agents[0].Message, filepath.ToSlash(path)+" is read-only; make it writable or leave claude-code out of --agents: ") + assert.ErrorIs(t, out.Agents[0].err, fs.ErrPermission) +} + +// TestInstallServerDoesNotNameDetectedAgentForReadOnlyConfig checks that a detected agent whose config file is read-only +// is refused with the writer's own message. --agents is no way out for an agent that was not named. +func TestInstallServerDoesNotNameDetectedAgentForReadOnlyConfig(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("root writes read-only files") + } + withHome(t) + a := agents.ByName("claude-code") + path, err := a.MCP.Path(t.Context()) + require.NoError(t, err) + require.NoError(t, os.WriteFile(path, []byte(`{}`), 0o600)) + // Windows maps the mode to the read-only attribute. Writable again before the home directory is removed. + t.Cleanup(func() { _ = os.Chmod(path, 0o600) }) + require.NoError(t, os.Chmod(path, 0o400)) + + req := addRequest{profile: "logfood", explicit: false, agents: []*agents.Agent{a}, binary: "databricks"} + plan := addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"} + out := installServer(t.Context(), req, plan) + + require.Len(t, out.Agents, 1) + assert.Equal(t, statusFailed, out.Agents[0].Status) + assert.Contains(t, out.Agents[0].Message, filepath.ToSlash(path)+" is read-only; make it writable: ") + assert.NotContains(t, out.Agents[0].Message, "--agents") + assert.ErrorIs(t, out.Agents[0].err, fs.ErrPermission) +} + +// 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")) +} + +// TestMcpAddRefusesUnreadableDetectedAgentBeforeAnyRequest checks that when the only detected agent has an unreadable config +// and no --agents is passed, the run is refused before any request, as it is when --agents names that agent. +func TestMcpAddRefusesUnreadableDetectedAgentBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + // withHome creates ~/.claude, which would be a second detected agent. + require.NoError(t, os.RemoveAll(filepath.Join(home, ".claude"))) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".codex"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte("[[[\n"), 0o600)) + + err := runAdd(t, "--profile", "logfood", "--names", "github") + require.Error(t, err) + assert.Equal(t, categoryAgentConfigInvalid, errorCategory(err)) +} + +// TestValidateMCPAddFlagsRefusesAgentsWithNoName checks that --agents holding no agent name is refused, rather than read as +// the default of every detected agent. +func TestValidateMCPAddFlagsRefusesAgentsWithNoName(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + err := validateMCPAddFlags(ctx, mcpAddFlags{agents: ","}, []string{"github"}, nil, false) + assert.ErrorContains(t, err, "--agents names no agent") +} + +// TestMcpAddMissingProfileListsAccountProfileWithWorkspaceID checks that the existing profiles include an account-level profile +// whose host names a workspace in ?w=, since --profile accepts it. +func TestMcpAddMissingProfileListsAccountProfileWithWorkspaceID(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[other]\nhost = "+server.URL+"/?w=123\naccount_id = 456\nauth_type = pat\ntoken = dapi-other\n") + + err := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Contains(t, err.Error(), "Existing profiles: other") +} + +// 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") + + withArg0(t, "dbx-not-on-path") + + _, 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}) +} + +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, + }, + } + 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()) +} + +// TestPlanAddsRefusesStoredNameThatIsNotAServerName checks that a name recovered from the state file or an agent entry is +// checked as --name is, and refused with the fix, since a hand edit can leave a name that agents cannot split. +func TestPlanAddsRefusesStoredNameThatIsNotAServerName(t *testing.T) { + claude := agents.ByName("claude-code") + const host = "https://adb-123.test" + want := `system.ai.github is installed as "%s", which is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'. Run: databricks aitools mcp remove --profile logfood and add it again` + + t.Run("state entry", 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, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) + assert.Equal(t, fmt.Sprintf(want, "Github Logfood"), err.Error()) + }) + + t.Run("agent entry", func(t *testing.T) { + home := withHome(t) + entries := `{"mcpServers":{"github__logfood":` + claudeEntry("system.ai.github", "logfood", "") + `}}` + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(entries), 0o600)) + _, err := planNames(t, "", defaultBinding(t, "logfood"), &mcp.State{}, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) + assert.Equal(t, fmt.Sprintf(want, "github__logfood"), err.Error()) + }) +} + +// TestPlanAddsRefusesStoredNameLongerThanTheCap checks that a stored name over the server name cap is refused with the fix, +// as a --name over the cap is refused. +func TestPlanAddsRefusesStoredNameLongerThanTheCap(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + const host = "https://adb-123.test" + long := strings.Repeat("a", mcp.MaxServerNameLen+1) + state := &mcp.State{Servers: []mcp.Entry{{Name: long, Service: "system.ai.github", Profile: "logfood", Host: host}}} + _, err := planNames(t, "", defaultBinding(t, "logfood"), state, []string{"system.ai.github"}, []*agents.Agent{claude}) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, errorCategory(err)) + assert.Equal(t, fmt.Sprintf(`system.ai.github is installed as "%s", which is longer than the 32-character server name limit. Run: databricks aitools mcp remove --profile logfood and add it again`, long), 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")) +} + +// TestMcpAddAccountOnlyProfileRefusedBeforeAnyRequest checks that a profile with a host and an account_id but no workspace_id +// is refused before its credential is sent, as the workspace commands refuse it. The SDK's discovery reads are allowed. +func TestMcpAddAccountOnlyProfileRefusedBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noAPIServer(t) + writeHomeConfig(t, home, "[acct]\nhost = "+server.URL+"\naccount_id = 123\n") + + err := runAdd(t, "--profile", "acct", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.ErrorContains(t, err, "profile \"acct\" is account-level, with no workspace_id in ") + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// TestMcpAddHostlessProfileIsNotListedAsMissing checks that a profile that exists without a host is refused with its own +// error, and that the list of existing profiles is not added to it. +func TestMcpAddHostlessProfileIsNotListedAsMissing(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[acct]\naccount_id = 123\n[dev]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-dev\n") + + err := runAdd(t, "--profile", "acct", "--names", "github", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + assert.Contains(t, err.Error(), "which has no host in ") + assert.NotContains(t, err.Error(), "Existing profiles") +} + +// TestProfileClientRefusesAccountOnlyProfile checks which account profiles are refused, on the config the SDK resolves: a +// workspace given in the host query or found by discovery is accepted. +func TestProfileClientRefusesAccountOnlyProfile(t *testing.T) { + server := noAPIServer(t) + tests := []struct { + name string + config string + refused bool + }{ + {name: "account without workspace is refused", config: "host = " + server.URL + "\naccount_id = 123\n", refused: true}, + {name: "workspace_id none is refused", config: "host = " + server.URL + "\naccount_id = 123\nworkspace_id = none\n", refused: true}, + {name: "workspace in the host query is accepted", config: "host = " + server.URL + "/?w=123\naccount_id = 123\n"}, + {name: "workspace_id is accepted", config: "host = " + server.URL + "\naccount_id = 123\nworkspace_id = 456\n"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + home := withHome(t) + writeHomeConfig(t, home, "[acct]\n"+tt.config) + + _, err := profileClient(t.Context(), "acct", filepath.Join(home, ".databrickscfg")) + if tt.refused { + assert.ErrorContains(t, err, "profile \"acct\" is account-level") + return + } + if err != nil { + assert.NotContains(t, err.Error(), "account-level") + } + }) + } +} + +// TestMcpAddMissingProfileWithoutEntryListsProfiles checks that a profile that is missing when no server is installed keeps the +// shared check's message, and adds the profiles that exist, since a mistyped --profile is the usual cause. +func TestMcpAddMissingProfileWithoutEntryListsProfiles(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()+"\nExisting profiles: other", 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} + held, _ := readAgentEntries(ctx, targets) + in := planInput{binding: defaultBinding(t, "logfood"), targets: targets, held: held} + + 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, nil)) + + 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} + heldClaude, _ := readAgentEntries(ctx, onlyClaude) + in = planInput{binding: defaultBinding(t, "logfood"), targets: onlyClaude, held: heldClaude} + plans, err = planAdds(ctx, in, state, []string{"system.ai.github"}) + require.NoError(t, err) + refusal := planningRefusals(plans, onlyClaude, nil) + require.Error(t, refusal) + assert.Equal(t, categoryNameConflict, errorCategory(refusal)) +} + +// TestPlanAddsRefusedAgentConflictDoesNotRefuseRun checks that with --agents, a name conflict in an agent that is refused, and so +// not written, does not refuse the run. The agents that are written decide. +func TestPlanAddsRefusedAgentConflictDoesNotRefuseRun(t *testing.T) { + ctx := t.Context() + home := withHome(t) + claude := agents.ByName("claude-code") + codex := agents.ByName("codex") + 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} + held, _ := readAgentEntries(ctx, targets) + refused := map[*agents.Agent]error{claude: errors.New("claude-code is not installed")} + in := planInput{binding: defaultBinding(t, "logfood"), targets: targets, held: held, explicit: true, refused: refused} + + 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) + assert.NotContains(t, plans[0].failed, claude) + assert.NoError(t, planningRefusals(plans, targets, refused)) +} + +// 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: "claude config dir exists", agent: "claude-code", homeEnv: "CLAUDE_CONFIG_DIR", homeDir: "claude-config", dirs: []string{"claude-config"}, written: "claude-config/.claude.json"}, + {name: "claude config dir missing although the CLI is on PATH", agent: "claude-code", homeEnv: "CLAUDE_CONFIG_DIR", homeDir: "claude-config", onPath: true, absent: "claude-config"}, + {name: "gemini cli home with its projects.json", agent: "gemini", homeEnv: "GEMINI_CLI_HOME", homeDir: "gemini-home", dirs: []string{"gemini-home/.gemini"}, files: map[string]string{"gemini-home/.gemini/projects.json": "{}"}, written: "gemini-home/.gemini/settings.json"}, + {name: "gemini cli home with its .gemini directory alone is refused", agent: "gemini", homeEnv: "GEMINI_CLI_HOME", homeDir: "gemini-home", dirs: []string{"gemini-home/.gemini"}, absent: "gemini-home/.gemini/settings.json"}, + {name: "gemini cli home without its .gemini directory although the CLI is on PATH", agent: "gemini", homeEnv: "GEMINI_CLI_HOME", homeDir: "gemini-home", dirs: []string{"gemini-home"}, onPath: true, absent: "gemini-home/.gemini"}, + {name: "xdg config home with its opencode directory", agent: "opencode", homeEnv: "XDG_CONFIG_HOME", homeDir: "xdg", dirs: []string{"xdg/opencode"}, written: "xdg/opencode/opencode.json"}, + {name: "xdg config home without its opencode directory although the CLI is on PATH", agent: "opencode", homeEnv: "XDG_CONFIG_HOME", homeDir: "xdg", dirs: []string{"xdg"}, onPath: true, absent: "xdg/opencode"}, + {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 { + fakeBinsOnPath(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) + } + }) + } +} + +// TestPlanningRefusalsCountsRefusedAgents checks that an agent refused for its config counts as failing every server, so the +// run ends before any request when the other agents fail too. +func TestPlanningRefusalsCountsRefusedAgents(t *testing.T) { + claude := agents.ByName(agents.NameClaudeCode) + cursor := agents.ByName(agents.NameCursor) + targets := []*agents.Agent{claude, cursor} + plans := []addPlan{{failed: map[*agents.Agent]error{cursor: errors.New("cursor holds another server")}}} + + err := planningRefusals(plans, targets, map[*agents.Agent]error{claude: errors.New("claude config is unreadable")}) + require.Error(t, err) + assert.ErrorContains(t, err, "claude config is unreadable") + assert.ErrorContains(t, err, "cursor holds another server") + + assert.NoError(t, planningRefusals(plans, targets, nil)) +} diff --git a/cmd/aitools/mcp_binary.go b/cmd/aitools/mcp_binary.go new file mode 100644 index 00000000000..9844cbe0ac9 --- /dev/null +++ b/cmd/aitools/mcp_binary.go @@ -0,0 +1,57 @@ +package aitools + +import ( + "errors" + "os" + "os/exec" + "path/filepath" + "strings" + + "github.com/databricks/cli/libs/aitools/mcp" +) + +// 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. An empty argv has no invoked path, so the caller refuses +// the empty command as not being the databricks binary. +func mcpBinaryPath() (string, bool) { + if len(os.Args) == 0 { + return "", false + } + return binaryPathFor(os.Args[0], exec.LookPath, os.Executable) +} + +// 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. A binary found only in the working directory is the running executable instead, 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), executable func() (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. The separators after it are not a + // root, so they are dropped with it: ".//bin" stays relative. + rel := filepath.FromSlash(arg0) + if after, ok := strings.CutPrefix(rel, "."+string(filepath.Separator)); ok { + rel = strings.TrimLeft(after, string(filepath.Separator)) + } + abs, err := mcp.AbsoluteAsWritten(rel) + if err != nil { + return arg0, false + } + return abs, true + } + found, err := lookPath(arg0) + if err == nil && filepath.IsAbs(found) { + return found, true + } + // On Windows the lookup also searches the working directory, and LookPath returns exec.ErrDot for a binary found + // only there. The running executable is that binary, and its path is absolute. + if errors.Is(err, exec.ErrDot) { + if running, runErr := executable(); runErr == nil { + return running, 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..e162c5db76e --- /dev/null +++ b/cmd/aitools/mcp_binary_test.go @@ -0,0 +1,222 @@ +package aitools + +import ( + "errors" + "os" + "os/exec" + "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, + }, + { + // ".//bin" is still relative: the separators after "." do not make it a root. + name: "dot-relative arg0 with repeated separators stays relative", + arg0: "." + string(filepath.Separator) + string(filepath.Separator) + "bin" + string(filepath.Separator) + "databricks", + onPath: foundPath, + wantPath: wd + string(filepath.Separator) + "bin" + string(filepath.Separator) + "databricks", + wantAbs: true, + }, + { + // A path with no drive is on the working directory's drive. On Windows it is not absolute, and the drive is the + // volume of the working directory. On other systems it is absolute and kept as is. + name: "rooted arg0 with no drive is on the working directory's drive", + arg0: string(filepath.Separator) + filepath.Join("tools", "databricks.exe"), + onPath: foundPath, + wantPath: filepath.VolumeName(wd) + string(filepath.Separator) + filepath.Join("tools", "databricks.exe"), + 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, noExecutable) + assert.Equal(t, tt.wantPath, path) + assert.Equal(t, tt.wantAbs, abs) + assert.Equal(t, tt.wantLookup, lookup.asked) + }) + } +} + +// TestBinaryPathForUsesRunningExecutableOnDot checks the case where the PATH lookup finds the binary only in the working +// directory. exec.LookPath then returns a relative path with exec.ErrDot, as it does for a databricks.exe run from the +// folder it was unzipped to on Windows. The running executable is written instead, since it is absolute. The lookup is +// stubbed with the error that LookPath returns, so the case runs on every OS. +func TestBinaryPathForUsesRunningExecutableOnDot(t *testing.T) { + running := filepath.Join(t.TempDir(), "databricks.exe") + dot := "." + string(filepath.Separator) + "databricks" + dotErr := &exec.Error{Name: "databricks", Err: exec.ErrDot} + notFoundErr := &exec.Error{Name: "databricks", Err: exec.ErrNotFound} + + tests := []struct { + name string + lookPath string + lookErr error + exe string + exeErr error + wantPath string + wantAbs bool + wantExe bool + }{ + { + name: "dot lookup writes the running executable", + lookPath: dot, + lookErr: dotErr, + exe: running, + wantPath: running, + wantAbs: true, + wantExe: true, + }, + { + name: "dot lookup keeps the bare name when the running executable is unknown", + lookPath: dot, + lookErr: dotErr, + exeErr: errors.New("no executable"), + wantPath: "databricks", + wantExe: true, + }, + { + name: "not found keeps the bare name without asking for the running executable", + lookErr: notFoundErr, + exe: running, + wantPath: "databricks", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + lookPath := func(string) (string, error) { return tt.lookPath, tt.lookErr } + asked := false + executable := func() (string, error) { + asked = true + return tt.exe, tt.exeErr + } + path, abs := binaryPathFor("databricks", lookPath, executable) + assert.Equal(t, tt.wantPath, path) + assert.Equal(t, tt.wantAbs, abs) + assert.Equal(t, tt.wantExe, asked) + }) + } +} + +// noExecutable stands in for os.Executable in cases that must not reach it. +func noExecutable() (string, error) { + return "", errors.New("os.Executable must not be called") +} + +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 +} + +// TestMcpBinaryPathReadsArgsWhenRun checks that the invoked path is read when the command runs, not when the package +// loads. An empty argv has no invoked path and is refused, not a panic. +func TestMcpBinaryPathReadsArgsWhenRun(t *testing.T) { + saved := os.Args + t.Cleanup(func() { os.Args = saved }) + + absolute := filepath.Join(t.TempDir(), "databricks") + os.Args = []string{absolute} + path, abs := mcpBinaryPath() + assert.Equal(t, absolute, path) + assert.True(t, abs) + + os.Args = nil + path, abs = mcpBinaryPath() + assert.Empty(t, path) + assert.False(t, abs) +} diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go new file mode 100644 index 00000000000..a3fb6e2356f --- /dev/null +++ b/cmd/aitools/mcp_test.go @@ -0,0 +1,137 @@ +package aitools + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/cmd/root" + "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 root.NewCommand(t.Context(), Spec).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("only profile naming its workspace in the host query", func(t *testing.T) { + // --profile accepts this profile, so auto-detect must too. + writeDatabricksCfg(t, `[ws] +host = https://ws.test/?w=123 +account_id = 456 +`) + name, err := resolveProfile(ctx, "") + require.NoError(t, err) + assert.Equal(t, "ws", 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, "unknown agent %q", "pi") + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(typed)) + assert.Equal(t, "unknown agent \"pi\"", typed.Error()) + + wrapped := withCategory(fmt.Errorf("%s: %w", "boom", errors.New("cause")), categoryServiceNotFound) + 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 0fff0d4c68b..0870d5afa07 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -63,6 +63,12 @@ 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 + // mcpHomeEnvs names the variables that move the MCP config file to another directory: CODEX_HOME for Codex + // (https://learn.chatgpt.com/docs/config-file/config-advanced), COPILOT_HOME for Copilot CLI + // (https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference), + // CLAUDE_CONFIG_DIR for Claude Code, GEMINI_CLI_HOME for Gemini CLI, and XDG_CONFIG_HOME for OpenCode. Empty when + // the file lives at a fixed path. + mcpHomeEnvs []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 +100,69 @@ func (a *Agent) Detected(ctx context.Context) bool { return err == nil && HasDatabricksSkillsIn(skillsDir) } +// mcpHomeDefaults gives the directory that each variable in mcpHomeEnvs points to when it is unset, relative to the home +// directory, as the agent's own path code resolves it. GEMINI_CLI_HOME's default is the home directory itself, since +// geminiConfigDir appends .gemini to it. None of these path codes expands a leading "~", so the value is compared as it is. +var mcpHomeDefaults = map[string]string{ + "CLAUDE_CONFIG_DIR": ".claude", + codexHomeEnv: ".codex", + copilotHomeEnv: ".copilot", + "GEMINI_CLI_HOME": "", + "XDG_CONFIG_HOME": ".config", +} + +// mcpHomeMoved reports whether one of the variables in mcpHomeEnvs is set to a directory other than its default. A +// variable set to its default leaves the MCP config where it is when unset, so the agent is detected as it is then. +func (a *Agent) mcpHomeMoved(ctx context.Context) (bool, error) { + for _, name := range a.mcpHomeEnvs { + value := env.Get(ctx, name) + if value == "" { + continue + } + home, err := env.UserHomeDir(ctx) + if err != nil { + return false, err + } + if filepath.Clean(value) != filepath.Join(home, mcpHomeDefaults[name]) { + return true, nil + } + } + return false, nil +} + +// InstalledForMCP reports whether the agent is installed for MCP servers. When one of mcpHomeEnvs is set to a directory +// other than its default, the directory the MCP config file lives in is the only signal: the agent's own directory may +// exist while that directory does not, and writing the MCP config would create it. That signal is the MandatoryFile +// there, as Detected reads it, when the agent has one. Otherwise Detected, HasBinary, or the MCP config file counts. +func (a *Agent) InstalledForMCP(ctx context.Context) (bool, error) { + moved, err := a.mcpHomeMoved(ctx) + if err != nil { + return false, err + } + if moved { + path, err := a.MCP.Path(ctx) + if err != nil { + return false, err + } + target := filepath.Dir(path) + if a.MandatoryFile != "" { + target = filepath.Join(target, a.MandatoryFile) + } + return linkExists(target) + } + 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) @@ -152,6 +221,9 @@ const ( // localServerType is the "type" of a local stdio server in Copilot and // OpenCode. Claude Code writes "stdio" instead. localServerType = "local" + // codexHomeEnv and copilotHomeEnv move the MCP config files of Codex and Copilot CLI. + codexHomeEnv = "CODEX_HOME" + copilotHomeEnv = "COPILOT_HOME" ) // Databricks plugin identity, shared across the agents that ship a plugin. @@ -206,6 +278,7 @@ var Registry = []*Agent{ Entry: claudeMCPEntry, Lock: true, }, + mcpHomeEnvs: []string{"CLAUDE_CONFIG_DIR"}, }, { Name: NameCursor, @@ -237,6 +310,7 @@ var Registry = []*Agent{ Root: "mcp_servers", Entry: stdioEntry, }, + mcpHomeEnvs: []string{codexHomeEnv}, }, { Name: NameOpenCode, @@ -255,6 +329,7 @@ var Registry = []*Agent{ // https://opencode.ai/docs/migrate-v1/. Legacy: true, }, + mcpHomeEnvs: []string{"XDG_CONFIG_HOME"}, }, { Name: NameCopilot, @@ -268,6 +343,7 @@ var Registry = []*Agent{ Root: mcpServersKey, Entry: copilotMCPEntry, }, + mcpHomeEnvs: []string{copilotHomeEnv}, }, { Name: NameAntigravity, @@ -310,6 +386,7 @@ var Registry = []*Agent{ Root: mcpServersKey, Entry: stdioEntry, }, + mcpHomeEnvs: []string{"GEMINI_CLI_HOME"}, }, { Name: NameGoose, @@ -502,7 +579,7 @@ func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[strin // 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) @@ -569,7 +646,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 276dc6dbbdb..813e16d5f45 100644 --- a/libs/aitools/agents/agents_test.go +++ b/libs/aitools/agents/agents_test.go @@ -260,3 +260,153 @@ func TestOpenCodeMCPPathDanglingJSONCLink(t *testing.T) { require.NoError(t, err) assert.Equal(t, jsonc, path) } + +// mcpHomeSetup points HOME at a temporary directory with the agent home variables cleared, and hides the agent +// binaries, so that only the case's own files count. +func mcpHomeSetup(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 +} + +// installedForMCP reports whether the agent named name is installed for MCP servers. +func installedForMCP(t *testing.T, name string) bool { + t.Helper() + a := ByName(name) + require.NotNil(t, a, name) + got, err := a.InstalledForMCP(t.Context()) + require.NoError(t, err) + return got +} + +// 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) { + t.Run("nothing present is not installed", func(t *testing.T) { + mcpHomeSetup(t) + for _, a := range MCPAgents() { + assert.False(t, installedForMCP(t, a.Name), a.Name) + } + }) + + t.Run("config dir is installed", func(t *testing.T) { + home := mcpHomeSetup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".cursor"), 0o755)) + assert.True(t, installedForMCP(t, NameCursor)) + }) + + t.Run("CLI on PATH alone is installed", func(t *testing.T) { + mcpHomeSetup(t) + stubLookPath(t, "codex") + assert.True(t, installedForMCP(t, NameCodex)) + assert.False(t, installedForMCP(t, NameCopilot)) + }) + + t.Run("MCP config file alone is installed", func(t *testing.T) { + home := mcpHomeSetup(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte("{}"), 0o600)) + assert.True(t, installedForMCP(t, NameClaudeCode)) + }) + + t.Run("CODEX_HOME that exists counts for Codex", func(t *testing.T) { + mcpHomeSetup(t) + codexHome := t.TempDir() + t.Setenv("CODEX_HOME", codexHome) + assert.True(t, installedForMCP(t, NameCodex)) + + t.Setenv("CODEX_HOME", filepath.Join(t.TempDir(), "missing")) + assert.False(t, installedForMCP(t, NameCodex)) + }) + + t.Run("CODEX_HOME that is missing is not installed although ~/.codex exists", func(t *testing.T) { + home := mcpHomeSetup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".codex"), 0o755)) + t.Setenv("CODEX_HOME", filepath.Join(home, "codex-home")) + assert.False(t, installedForMCP(t, NameCodex)) + assert.NoDirExists(t, filepath.Join(home, "codex-home")) + }) + + t.Run("COPILOT_HOME that exists counts for Copilot", func(t *testing.T) { + mcpHomeSetup(t) + t.Setenv("COPILOT_HOME", t.TempDir()) + assert.True(t, installedForMCP(t, NameCopilot)) + }) + + t.Run("CLAUDE_CONFIG_DIR that is missing is not installed although the CLI is on PATH", func(t *testing.T) { + mcpHomeSetup(t) + stubLookPath(t, "claude") + t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(t.TempDir(), "missing")) + assert.False(t, installedForMCP(t, NameClaudeCode)) + + t.Setenv("CLAUDE_CONFIG_DIR", t.TempDir()) + assert.True(t, installedForMCP(t, NameClaudeCode)) + }) + + t.Run("GEMINI_CLI_HOME without its .gemini directory is not installed although the CLI is on PATH", func(t *testing.T) { + mcpHomeSetup(t) + stubLookPath(t, "gemini") + geminiHome := t.TempDir() + t.Setenv("GEMINI_CLI_HOME", geminiHome) + assert.False(t, installedForMCP(t, NameGemini)) + + // The directory alone is not enough: Gemini's mandatory file must be there, as Detected requires. + require.NoError(t, os.MkdirAll(filepath.Join(geminiHome, ".gemini", "antigravity"), 0o755)) + assert.False(t, installedForMCP(t, NameGemini)) + + require.NoError(t, os.WriteFile(filepath.Join(geminiHome, ".gemini", "projects.json"), []byte("{}"), 0o600)) + assert.True(t, installedForMCP(t, NameGemini)) + }) + + t.Run("XDG_CONFIG_HOME without its opencode directory is not installed although the CLI is on PATH", func(t *testing.T) { + mcpHomeSetup(t) + stubLookPath(t, "opencode") + xdg := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", xdg) + assert.False(t, installedForMCP(t, NameOpenCode)) + + require.NoError(t, os.MkdirAll(filepath.Join(xdg, "opencode"), 0o755)) + assert.True(t, installedForMCP(t, NameOpenCode)) + }) + + t.Run("MCP home variables set to their defaults count as unset", func(t *testing.T) { + for _, tc := range []struct { + agent, binary, variable, dir string + }{ + {NameClaudeCode, "claude", "CLAUDE_CONFIG_DIR", ".claude"}, + {NameCodex, "codex", "CODEX_HOME", ".codex"}, + {NameCopilot, "copilot", "COPILOT_HOME", ".copilot"}, + {NameGemini, "gemini", "GEMINI_CLI_HOME", ""}, + {NameOpenCode, "opencode", "XDG_CONFIG_HOME", ".config"}, + } { + t.Run(tc.variable, func(t *testing.T) { + home := mcpHomeSetup(t) + stubLookPath(t, tc.binary) + // The default directory does not exist, so only the CLI on PATH makes the agent installed. + t.Setenv(tc.variable, filepath.Join(home, tc.dir)) + assert.True(t, installedForMCP(t, tc.agent)) + }) + } + }) + + t.Run("Gemini is not installed because Antigravity made ~/.gemini", func(t *testing.T) { + home := mcpHomeSetup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) + assert.False(t, installedForMCP(t, NameGemini)) + assert.True(t, installedForMCP(t, NameAntigravity)) + + require.NoError(t, os.WriteFile(filepath.Join(home, ".gemini", "projects.json"), []byte("{}"), 0o644)) + assert.True(t, installedForMCP(t, NameGemini)) + }) + + t.Run("Antigravity is installed from its own directory, without ~/.gemini/config", func(t *testing.T) { + home := mcpHomeSetup(t) + require.NoError(t, os.MkdirAll(filepath.Join(home, ".gemini", "antigravity"), 0o755)) + assert.NoDirExists(t, filepath.Join(home, ".gemini", "config")) + assert.True(t, installedForMCP(t, NameAntigravity)) + }) +} diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 8bc7d126edd..c65d186c62c 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -67,10 +67,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. @@ -499,7 +495,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 2781f7292a7..2f2da45347d 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,61 +78,102 @@ 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) } - 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, err) + case errors.Is(err, errProfileNoHost): + return profile.Profile{}, profileGoneError(name, profileName, filepath.ToSlash(configFile), entry, "has no host", err) + 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. @@ -145,19 +188,22 @@ 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. -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 = mcpconfig.ConfigFileEnv + "=" + shellquote.BashArg(path) + " " + recreate - } +// 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, cause error) error { + 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, cause: cause} + } + recreate := authLoginCommand + " --host " + shellquote.BashArg(entry.Host) + " --profile " + shellquote.BashArg(profileName) + if entry.ConfigFile != "" { + recreate = mcpconfig.ConfigFileEnv + "=" + 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)) - return &BindError{Category: BindProfileNotFound, message: message} + return &BindError{Category: BindProfileNotFound, message: message, cause: cause} } // driftError is the PROFILE_HOST_CHANGED error for an entry whose profile now names another host, @@ -183,31 +229,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..61450a510a3 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()) } + +// 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) +} + +// TestCheckBindingMatchesBind checks that CheckBinding refuses the same drift as Bind, before any request. +func TestCheckBindingMatchesBind(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request to the profile's host: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(server.Close) + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, server.URL, bindingTestToken)) + state := bindingTestState(cfgPath, bindingTestHost) + + _, err := 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..613564f91f1 100644 --- a/libs/aitools/mcp/configfile.go +++ b/libs/aitools/mcp/configfile.go @@ -7,16 +7,17 @@ import ( "io/fs" "os" "path/filepath" + "strings" "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 +27,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 } @@ -41,6 +45,22 @@ func absoluteAsWritten(path string) (string, error) { if err != nil { return "", fmt.Errorf("failed to determine working directory: %w", err) } + if volume := filepath.VolumeName(path); volume != "" { + // A drive-relative path, such as C:cfg on Windows, is relative to the working directory when that is on its drive. + // Another drive's working directory is not known here, so that path is refused. + if !strings.EqualFold(volume, filepath.VolumeName(wd)) { + return "", fmt.Errorf("%s is relative to drive %s, but the working directory is on drive %s", filepath.ToSlash(path), volume, filepath.VolumeName(wd)) + } + path = path[len(volume):] + } + if strings.HasPrefix(path, string(filepath.Separator)) { + // A rooted path with no drive, such as \tools on Windows, is on the working directory's drive. + return filepath.VolumeName(wd) + path, nil + } + if strings.HasSuffix(wd, string(filepath.Separator)) { + // The working directory is a root, which already ends with a separator. + return wd + path, nil + } return wd + string(filepath.Separator) + path, nil } diff --git a/libs/aitools/mcp/configfile_test.go b/libs/aitools/mcp/configfile_test.go index b1f16412ba3..21f60ec2ba4 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) { @@ -73,3 +84,13 @@ func TestSamePathComparesExistingFilesByIdentity(t *testing.T) { assert.True(t, samePath(a, b)) assert.False(t, samePath(filepath.Join(dir, "x"), filepath.Join(dir, "X"))) } + +// TestAbsoluteAsWrittenInRootDoesNotDoubleSeparator checks that a path relative to a working directory that is a root +// gets one separator, not two. +func TestAbsoluteAsWrittenInRootDoesNotDoubleSeparator(t *testing.T) { + root := filepath.VolumeName(t.TempDir()) + string(filepath.Separator) + t.Chdir(root) + got, err := AbsoluteAsWritten("cfg") + require.NoError(t, err) + assert.Equal(t, root+"cfg", got) +} diff --git a/libs/aitools/mcp/entry.go b/libs/aitools/mcp/entry.go new file mode 100644 index 00000000000..78feb5cf443 --- /dev/null +++ b/libs/aitools/mcp/entry.go @@ -0,0 +1,58 @@ +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 +} + +// valueFlags are the serve flags that take a value. +var valueFlags = []string{mcpconfig.ProfileFlag, mcpconfig.ServiceFlag, mcpconfig.AgentFlag} + +// flagValue returns the value of flag in argv, or "" when flag is absent. As pflag reads them, a repeated flag takes its +// last value, and both "--flag value" and "--flag=value" name a value. The value of each serve flag is skipped when it +// is read, so a value that looks like a flag, such as a profile named --service, is not taken for one. +func flagValue(argv []string, flag string) string { + value := "" + for i := 0; i < len(argv); i++ { + if slices.Contains(valueFlags, argv[i]) { + next := "" + if i+1 < len(argv) { + next = argv[i+1] + } + if argv[i] == flag { + value = next + } + i++ + continue + } + if v, ok := strings.CutPrefix(argv[i], flag+"="); ok { + value = v + } + } + return value +} diff --git a/libs/aitools/mcp/entry_test.go b/libs/aitools/mcp/entry_test.go new file mode 100644 index 00000000000..43a299baf60 --- /dev/null +++ b/libs/aitools/mcp/entry_test.go @@ -0,0 +1,75 @@ +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: "profile named like a flag", entry: map[string]any{"command": "databricks", "args": anyList([]string{"aitools", "mcp", "serve", "--profile", "--service", "--service", bindingTestService, "--agent", "x"})}, service: bindingTestService, profile: "--service", configFile: defaultFile(t), wantServes: true}, + {name: "repeated profile takes its last value", entry: map[string]any{"command": "databricks", "args": anyList([]string{"aitools", "mcp", "serve", "--profile", "other", "--profile", bindingTestProfile, "--service", bindingTestService, "--agent", "x"})}, service: bindingTestService, profile: bindingTestProfile, configFile: defaultFile(t), wantServes: true}, + {name: "repeated profile does not match its earlier value", entry: map[string]any{"command": "databricks", "args": anyList([]string{"aitools", "mcp", "serve", "--profile", bindingTestProfile, "--profile", "other", "--service", bindingTestService, "--agent", "x"})}, service: bindingTestService, profile: bindingTestProfile, configFile: defaultFile(t), wantServes: false}, + {name: "profile in the equals form", entry: map[string]any{"command": "databricks", "args": anyList([]string{"aitools", "mcp", "serve", "--profile=" + bindingTestProfile, "--service", bindingTestService, "--agent", "x"})}, 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 7cb9b8b7c9e..27e241a3a9e 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 b4074c241fa..a93137daa41 100644 --- a/libs/aitools/mcp/naming.go +++ b/libs/aitools/mcp/naming.go @@ -10,9 +10,9 @@ import ( "sync" ) -// maxServerNameLen caps a server name. Agents expose tools as mcp____ and limit +// MaxServerNameLen caps a server name. Agents expose tools as mcp____ and limit // tool names to about 64 characters. -const maxServerNameLen = 32 +const MaxServerNameLen = 32 // serverNameHashLen is the number of hex characters that end a capped server name. const serverNameHashLen = 6 @@ -40,7 +40,7 @@ func Sanitize(s string) string { // ServerName returns the agent-facing name of the server for a service (catalog.schema.leaf) // and a profile: - in system.ai, --- elsewhere. // Each part is sanitized and parts that sanitize to nothing are left out, except the leaf, which -// makes an error wrapping ErrNoServerName. A name longer than maxServerNameLen is capped by +// makes an error wrapping ErrNoServerName. A name longer than MaxServerNameLen is capped by // capServerName. Tool names in agents are mcp____, so the name must stay stable for // the entry's life. func ServerName(fullName, profile string) (string, error) { @@ -63,18 +63,18 @@ func ServerName(fullName, profile string) (string, error) { return capServerName(strings.Join(name, "-")), nil } -// capServerName returns name unchanged when it fits in maxServerNameLen. Otherwise it cuts name +// capServerName returns name unchanged when it fits in MaxServerNameLen. Otherwise it cuts name // to fit beside "-" and serverNameHashLen hex characters of the sha256 of the uncut name, drops // any "-" or "_" the cut leaves at its end, and appends "-" and the hash. Long names that share a // prefix stay distinct, and the same name always gets the same result. name is sanitized, so it // is ASCII and cutting it by byte is safe. func capServerName(name string) string { - if len(name) <= maxServerNameLen { + if len(name) <= MaxServerNameLen { return name } sum := sha256.Sum256([]byte(name)) hash := hex.EncodeToString(sum[:])[:serverNameHashLen] - head := strings.TrimRight(name[:maxServerNameLen-len(hash)-1], "-_") + head := strings.TrimRight(name[:MaxServerNameLen-len(hash)-1], "-_") return head + "-" + hash } @@ -89,12 +89,12 @@ 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 // returns false. Each candidate is capped like a server name, so a suffix cannot push the name -// past maxServerNameLen. +// past MaxServerNameLen. func UniqueName(base string, taken func(string) bool) string { if !taken(base) { return base diff --git a/libs/aitools/mcp/naming_test.go b/libs/aitools/mcp/naming_test.go index 404c87d947f..45c81bec891 100644 --- a/libs/aitools/mcp/naming_test.go +++ b/libs/aitools/mcp/naming_test.go @@ -102,7 +102,7 @@ func TestServerNameCapFollowsSanitizeRules(t *testing.T) { serverName(t, "main.sales.abcdefghijklm-n", "production"), serverName(t, "main.sales.abcdefghijklmnopqrstuvwxyz", "production_"), } { - assert.LessOrEqual(t, len(name), maxServerNameLen, name) + assert.LessOrEqual(t, len(name), MaxServerNameLen, name) assert.NotContains(t, name, "__", name) assert.NotRegexp(t, `^[-_]|[-_]$`, name) } @@ -134,10 +134,10 @@ func TestUniqueName(t *testing.T) { func TestUniqueNameSuffixStaysWithinCap(t *testing.T) { base := "main-sales-abcdefghijklmnop-prod" - require.Len(t, base, maxServerNameLen) + require.Len(t, base, MaxServerNameLen) taken := func(name string) bool { return name == base } got := UniqueName(base, taken) - assert.LessOrEqual(t, len(got), maxServerNameLen, got) + assert.LessOrEqual(t, len(got), MaxServerNameLen, got) assert.NotEqual(t, base, got) assert.Equal(t, got, UniqueName(base, taken)) } diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go index ceeaf7ab69c..87627c7b1ce 100644 --- a/libs/aitools/mcp/state.go +++ b/libs/aitools/mcp/state.go @@ -20,8 +20,8 @@ const ( // Profile lookups that do not yield a usable profile. var ( - // errProfileMissing means the config file has no profile of the name, or does not exist. - errProfileMissing = errors.New("profile missing") + // ErrProfileMissing means the config file has no profile of the name, or does not exist. + ErrProfileMissing = errors.New("profile missing") // errProfileNoHost means the config file has a section of the name whose host is missing or empty. errProfileNoHost = errors.New("profile has no host") ) @@ -106,12 +106,12 @@ 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 } p, err := files.profile(path, entry.Profile) - if errors.Is(err, errProfileMissing) || errors.Is(err, errProfileNoHost) { + if errors.Is(err, ErrProfileMissing) || errors.Is(err, errProfileNoHost) { return &Drift{Kind: DriftProfileMissing, Entry: entry}, nil } if err != nil { @@ -124,7 +124,7 @@ func checkEntry(ctx context.Context, files configFiles, entry Entry) (*Drift, er // parsed once. A nil file is one that does not exist. type configFiles map[string]*config.File -// profile returns the profile named name from the config file at path. It returns errProfileMissing +// profile returns the profile named name from the config file at path. It returns ErrProfileMissing // when the file does not exist or has no section of that name, and errProfileNoHost when the section // has no host or an empty one. func (c configFiles) profile(path, name string) (profile.Profile, error) { @@ -141,11 +141,11 @@ func (c configFiles) profile(path, name string) (profile.Profile, error) { c[path] = file } if file == nil { - return profile.Profile{}, errProfileMissing + return profile.Profile{}, ErrProfileMissing } section, err := file.GetSection(name) if err != nil { - return profile.Profile{}, errProfileMissing + return profile.Profile{}, ErrProfileMissing } keys := section.KeysHash() if keys["host"] == "" { diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index b9890e74105..74ddf23eff6 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -84,6 +84,31 @@ func Argv(entry map[string]any) ([]string, bool) { } } +// ServeCommand is the argv that starts an MCP server, after the binary: `aitools mcp serve`. Entries carry it, and +// IsDatabricksEntry recognizes it, so both sides share this definition. +var ServeCommand = []string{"aitools", "mcp", "serve"} + +// 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 { @@ -93,7 +118,7 @@ func IsDatabricksEntry(entry map[string]any) bool { } args := argv[1:] return IsDatabricksBinary(argv[0]) && - len(args) >= 3 && args[0] == "aitools" && args[1] == "mcp" && args[2] == "serve" + len(args) >= len(ServeCommand) && slices.Equal(args[:len(ServeCommand)], ServeCommand) } // IsDatabricksBinary reports whether cmd names the databricks binary, as a bare 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)) + }) + } +}