diff --git a/acceptance/cmd/aitools/mcp/drift-profile-missing/.databrickscfg b/acceptance/cmd/aitools/mcp/drift-profile-missing/.databrickscfg new file mode 100644 index 00000000000..f685e4656ff --- /dev/null +++ b/acceptance/cmd/aitools/mcp/drift-profile-missing/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood diff --git a/acceptance/cmd/aitools/mcp/drift-profile-missing/dogfood-moved.databrickscfg b/acceptance/cmd/aitools/mcp/drift-profile-missing/dogfood-moved.databrickscfg new file mode 100644 index 00000000000..da10ba7e01a --- /dev/null +++ b/acceptance/cmd/aitools/mcp/drift-profile-missing/dogfood-moved.databrickscfg @@ -0,0 +1,3 @@ +[dogfood] +host = https://adb-2.test +token = test-token-dogfood diff --git a/acceptance/cmd/aitools/mcp/drift-profile-missing/dogfood-only.databrickscfg b/acceptance/cmd/aitools/mcp/drift-profile-missing/dogfood-only.databrickscfg new file mode 100644 index 00000000000..91c0e3709ed --- /dev/null +++ b/acceptance/cmd/aitools/mcp/drift-profile-missing/dogfood-only.databrickscfg @@ -0,0 +1,3 @@ +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood diff --git a/acceptance/cmd/aitools/mcp/drift-profile-missing/out.test.toml b/acceptance/cmd/aitools/mcp/drift-profile-missing/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/drift-profile-missing/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/drift-profile-missing/output.txt b/acceptance/cmd/aitools/mcp/drift-profile-missing/output.txt new file mode 100644 index 00000000000..7746d060f10 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/drift-profile-missing/output.txt @@ -0,0 +1,91 @@ + +=== Both profiles get a server + +>>> [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 +Profile dogfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code + +genie_one_mcp-dogfood system.ai.genie_one_mcp + claude-code installed [TEST_TMP_DIR]/home/.claude.json + +=== logfood is deleted from the Databricks config file: list reports it and exits 0 + +>>> [CLI] aitools mcp list +Profile dogfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-dogfood system.ai.genie_one_mcp Claude Code + +Profile logfood (profile not found in [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + broken: run `databricks aitools mcp remove --names genie_one_mcp-logfood` or `DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks auth login --host [DATABRICKS_URL] --profile logfood` + +=== dogfood now points at another host: list reports the host change + +>>> [CLI] aitools mcp list --output json +{ + "profiles": [ + { + "profile": "dogfood", + "host": "https://adb-2.test", + "config_file": "[TEST_TMP_DIR]/.databrickscfg", + "status": "host_changed", + "servers": [ + { + "name": "genie_one_mcp-dogfood", + "service": "system.ai.genie_one_mcp", + "host": "[DATABRICKS_URL]", + "sign_in_required": false, + "status": "host_changed", + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ] + } + ] + }, + { + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "config_file": "[TEST_TMP_DIR]/.databrickscfg", + "status": "profile_missing", + "servers": [ + { + "name": "genie_one_mcp-logfood", + "service": "system.ai.genie_one_mcp", + "host": "[DATABRICKS_URL]", + "sign_in_required": false, + "status": "profile_missing", + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ] + } + ] + } + ], + "unmanaged": [], + "warnings": [] +} + +=== The same change in text + +>>> [CLI] aitools mcp list +Profile dogfood (https://adb-2.test, config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-dogfood system.ai.genie_one_mcp Claude Code + broken: it was set up for [DATABRICKS_URL], but profile "dogfood" now points at https://adb-2.test; run `databricks aitools mcp remove --names genie_one_mcp-dogfood` and add it again + +Profile logfood (profile not found in [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + broken: run `databricks aitools mcp remove --names genie_one_mcp-logfood` or `DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks auth login --host [DATABRICKS_URL] --profile logfood` diff --git a/acceptance/cmd/aitools/mcp/drift-profile-missing/script b/acceptance/cmd/aitools/mcp/drift-profile-missing/script new file mode 100644 index 00000000000..9e384e52081 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/drift-profile-missing/script @@ -0,0 +1,24 @@ +# 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 service, created in the fake workspace. The fake keeps one workspace per token, so each profile gets 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 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=genie_one_mcp" --profile dogfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-dogfood + +title "Both profiles get a server" +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 + +title "logfood is deleted from the Databricks config file: list reports it and exits 0" +envsubst < dogfood-only.databrickscfg > out && mv out .databrickscfg +trace $CLI aitools mcp list + +title "dogfood now points at another host: list reports the host change" +envsubst < dogfood-moved.databrickscfg > out && mv out .databrickscfg +trace $CLI aitools mcp list --output json + +title "The same change in text" +trace $CLI aitools mcp list diff --git a/acceptance/cmd/aitools/mcp/list-and-remove/.databrickscfg b/acceptance/cmd/aitools/mcp/list-and-remove/.databrickscfg new file mode 100644 index 00000000000..f685e4656ff --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-and-remove/.databrickscfg @@ -0,0 +1,7 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood + +[dogfood] +host = $DATABRICKS_HOST +token = test-token-dogfood diff --git a/acceptance/cmd/aitools/mcp/list-and-remove/out.test.toml b/acceptance/cmd/aitools/mcp/list-and-remove/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-and-remove/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt new file mode 100644 index 00000000000..145ee12df94 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt @@ -0,0 +1,178 @@ + +=== Two profiles, add twice: logfood gets two services, dogfood gets one + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --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 + +github-logfood system.ai.github + claude-code installed [TEST_TMP_DIR]/home/.claude.json +system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood + +>>> [CLI] aitools mcp add --profile dogfood --names system.ai.genie_one_mcp --agents claude-code +Profile dogfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code + +genie_one_mcp-dogfood system.ai.genie_one_mcp + claude-code installed [TEST_TMP_DIR]/home/.claude.json + +=== list groups the servers by profile + +>>> [CLI] aitools mcp list +Profile dogfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-dogfood system.ai.genie_one_mcp Claude Code + +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + github-logfood system.ai.github Claude Code + +=== list as JSON + +>>> [CLI] aitools mcp list --output json +{ + "profiles": [ + { + "profile": "dogfood", + "host": "[DATABRICKS_URL]", + "config_file": "[TEST_TMP_DIR]/.databrickscfg", + "status": "ok", + "servers": [ + { + "name": "genie_one_mcp-dogfood", + "service": "system.ai.genie_one_mcp", + "host": "[DATABRICKS_URL]", + "sign_in_required": false, + "status": "ok", + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ] + } + ] + }, + { + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "config_file": "[TEST_TMP_DIR]/.databrickscfg", + "status": "ok", + "servers": [ + { + "name": "genie_one_mcp-logfood", + "service": "system.ai.genie_one_mcp", + "host": "[DATABRICKS_URL]", + "sign_in_required": false, + "status": "ok", + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ] + }, + { + "name": "github-logfood", + "service": "system.ai.github", + "host": "[DATABRICKS_URL]", + "sign_in_required": true, + "status": "ok", + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ] + } + ] + } + ], + "unmanaged": [], + "warnings": [] +} + +=== remove --profile removes that profile only + +>>> [CLI] aitools mcp remove --profile dogfood +Removed genie_one_mcp-dogfood system.ai.genie_one_mcp profile dogfood (Claude Code) + +>>> [CLI] aitools mcp list +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + github-logfood system.ai.github Claude Code + +=== A hand-edited entry is listed as modified; an unmanaged ug entry is listed and never changed + +>>> [CLI] aitools mcp list +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + github-logfood system.ai.github Claude Code (modified) + changed outside this CLI in Claude Code: delete the entry from [TEST_TMP_DIR]/home/.claude.json by hand and run `databricks aitools mcp remove --names github-logfood` again, or add the server again with --force + +Unmanaged Databricks MCP entries (not changed by this command): + Claude Code web_search ug mcp-proxy + +=== --profile lists that profile only, without the unmanaged entries + +>>> [CLI] aitools mcp list --profile logfood +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + github-logfood system.ai.github Claude Code (modified) + changed outside this CLI in Claude Code: delete the entry from [TEST_TMP_DIR]/home/.claude.json by hand and run `databricks aitools mcp remove --names github-logfood` again, or add the server again with --force + +=== remove leaves the modified entry in place and says why + +>>> [CLI] aitools mcp remove --names github-logfood,genie_one_mcp-logfood +Removed genie_one_mcp-logfood system.ai.genie_one_mcp profile logfood (Claude Code) +Skipped github-logfood: Claude Code has an entry named github-logfood that was changed outside this CLI; left in place. To remove it, delete the entry from [TEST_TMP_DIR]/home/.claude.json by hand and run `databricks aitools mcp remove --names github-logfood` again, or add the server again with --force + +>>> jq .mcpServers | keys home/.claude.json +[ + "github-logfood", + "web_search" +] + +>>> jq .mcp | [.servers[] | {name, profile, agents: (.agents | keys)}] home/.databricks/aitools/skills/.state.json +[ + { + "name": "github-logfood", + "profile": "logfood", + "agents": [ + "claude-code" + ] + } +] + +=== remove --all removes what this CLI wrote and nothing else + +>>> [CLI] aitools mcp remove --all --output json +{ + "removed": [], + "skipped": [ + { + "name": "github-logfood", + "agent": "claude-code", + "reason": "Claude Code has an entry named github-logfood that was changed outside this CLI; left in place. To remove it, delete the entry from [TEST_TMP_DIR]/home/.claude.json by hand and run `databricks aitools mcp remove --names github-logfood` again, or add the server again with --force" + } + ] +} + +>>> jq .mcpServers | keys home/.claude.json +[ + "github-logfood", + "web_search" +] + +>>> [CLI] aitools mcp list +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + github-logfood system.ai.github Claude Code (modified) + changed outside this CLI in Claude Code: delete the entry from [TEST_TMP_DIR]/home/.claude.json by hand and run `databricks aitools mcp remove --names github-logfood` again, or add the server again with --force + +Unmanaged Databricks MCP entries (not changed by this command): + Claude Code web_search ug mcp-proxy diff --git a/acceptance/cmd/aitools/mcp/list-and-remove/script b/acceptance/cmd/aitools/mcp/list-and-remove/script new file mode 100644 index 00000000000..2b6481b7e36 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-and-remove/script @@ -0,0 +1,42 @@ +# 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. The fake keeps one workspace per token, so each profile +# gets 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 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 +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 "Two profiles, add twice: logfood gets two services, dogfood gets one" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code +trace $CLI aitools mcp add --profile dogfood --names system.ai.genie_one_mcp --agents claude-code + +title "list groups the servers by profile" +trace $CLI aitools mcp list + +title "list as JSON" +trace $CLI aitools mcp list --output json + +title "remove --profile removes that profile only" +trace $CLI aitools mcp remove --profile dogfood +trace $CLI aitools mcp list + +title "A hand-edited entry is listed as modified; an unmanaged ug entry is listed and never changed" +jq '.mcpServers["github-logfood"].command = "/usr/bin/other" | .mcpServers.web_search = {"command": "ug", "args": ["mcp-proxy"]}' home/.claude.json > home/claude.tmp && mv home/claude.tmp home/.claude.json +trace $CLI aitools mcp list + +title "--profile lists that profile only, without the unmanaged entries" +trace $CLI aitools mcp list --profile logfood + +title "remove leaves the modified entry in place and says why" +trace $CLI aitools mcp remove --names github-logfood,genie_one_mcp-logfood +trace jq '.mcpServers | keys' home/.claude.json +trace jq '.mcp | [.servers[] | {name, profile, agents: (.agents | keys)}]' home/.databricks/aitools/skills/.state.json + +title "remove --all removes what this CLI wrote and nothing else" +trace $CLI aitools mcp remove --all --output json +trace jq '.mcpServers | keys' home/.claude.json +trace $CLI aitools mcp list diff --git a/acceptance/cmd/aitools/mcp/repair-and-warnings/.databrickscfg b/acceptance/cmd/aitools/mcp/repair-and-warnings/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/repair-and-warnings/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/repair-and-warnings/broken-codex-config.toml b/acceptance/cmd/aitools/mcp/repair-and-warnings/broken-codex-config.toml new file mode 100644 index 00000000000..88e1fe9610b --- /dev/null +++ b/acceptance/cmd/aitools/mcp/repair-and-warnings/broken-codex-config.toml @@ -0,0 +1 @@ +[mcp_servers.broken]] diff --git a/acceptance/cmd/aitools/mcp/repair-and-warnings/out.test.toml b/acceptance/cmd/aitools/mcp/repair-and-warnings/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/repair-and-warnings/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/repair-and-warnings/output.txt b/acceptance/cmd/aitools/mcp/repair-and-warnings/output.txt new file mode 100644 index 00000000000..b57da8bcbf0 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/repair-and-warnings/output.txt @@ -0,0 +1,57 @@ + +=== Claude Code and Codex get the server + +>>> [CLI] aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,codex +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code, codex + +genie_one_mcp-logfood system.ai.genie_one_mcp + claude-code installed [TEST_TMP_DIR]/home/.claude.json + codex installed [TEST_TMP_DIR]/home/.codex/config.toml + +=== Claude Code reads its config from another directory: list says path changed, and remove takes the entry out of the file it was written to + +>>> [CLI] aitools mcp list +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code (path changed), Codex CLI + +>>> [CLI] aitools mcp remove --names genie_one_mcp-logfood --agents claude-code +Removed genie_one_mcp-logfood system.ai.genie_one_mcp profile logfood (Claude Code) + +>>> jq .mcpServers | keys home/.claude.json +[] + +=== A Codex config that cannot be parsed is a warning: list exits 0 and shows Codex CLI as unknown + +>>> [CLI] aitools mcp list +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Codex CLI (unknown) +Warning: could not read the Codex CLI config: [TEST_TMP_DIR]/home/.codex/config.toml: agent config file cannot be parsed: toml: line 1 (last key "mcp_servers.broken"): expected a top-level item to end with a newline, comment, or EOF, but got ']' instead + +=== An entry that runs aitools mcp serve without an install record is unmanaged, and remove does not touch it + +>>> jq .mcpServers.orphan = {"command": "databricks", "args": ["aitools", "mcp", "serve", "--profile", "logfood", "--service", "system.ai.genie_one_mcp", "--agent", "claude-code"]} home/.claude.json + +>>> [CLI] aitools mcp list +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) + genie_one_mcp-logfood system.ai.genie_one_mcp Codex CLI (unknown) + +Unmanaged Databricks MCP entries (not changed by this command): + Claude Code orphan databricks aitools mcp serve, no install record +Warning: could not read the Codex CLI config: [TEST_TMP_DIR]/home/.codex/config.toml: agent config file cannot be parsed: toml: line 1 (last key "mcp_servers.broken"): expected a top-level item to end with a newline, comment, or EOF, but got ']' instead + +>>> musterr [CLI] aitools mcp remove --names orphan +Error: no installed MCP server named orphan + +>>> jq .mcpServers | keys home/.claude.json +[ + "orphan" +] + +=== A profile or an agent with nothing installed is refused + +>>> musterr [CLI] aitools mcp remove --profile typo +Error: no installed MCP servers on profile typo + +>>> musterr [CLI] aitools mcp remove --names genie_one_mcp-logfood --agents cursor +Error: no selected MCP server is installed for Cursor diff --git a/acceptance/cmd/aitools/mcp/repair-and-warnings/script b/acceptance/cmd/aitools/mcp/repair-and-warnings/script new file mode 100644 index 00000000000..9880e55b4fd --- /dev/null +++ b/acceptance/cmd/aitools/mcp/repair-and-warnings/script @@ -0,0 +1,31 @@ +# 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 + +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 "Claude Code and Codex get the server" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp --agents claude-code,codex + +title "Claude Code reads its config from another directory: list says path changed, and remove takes the entry out of the file it was written to" +export CLAUDE_CONFIG_DIR=$PWD/home/moved-claude +trace $CLI aitools mcp list +trace $CLI aitools mcp remove --names genie_one_mcp-logfood --agents claude-code +unset CLAUDE_CONFIG_DIR +trace jq '.mcpServers | keys' home/.claude.json + +title "A Codex config that cannot be parsed is a warning: list exits 0 and shows Codex CLI as unknown" +cp broken-codex-config.toml home/.codex/config.toml +trace $CLI aitools mcp list + +title "An entry that runs aitools mcp serve without an install record is unmanaged, and remove does not touch it" +trace jq '.mcpServers.orphan = {"command": "databricks", "args": ["aitools", "mcp", "serve", "--profile", "logfood", "--service", "system.ai.genie_one_mcp", "--agent", "claude-code"]}' home/.claude.json > home/claude.tmp && mv home/claude.tmp home/.claude.json +trace $CLI aitools mcp list +trace musterr $CLI aitools mcp remove --names orphan +trace jq '.mcpServers | keys' home/.claude.json + +title "A profile or an agent with nothing installed is refused" +trace musterr $CLI aitools mcp remove --profile typo +trace musterr $CLI aitools mcp remove --names genie_one_mcp-logfood --agents cursor diff --git a/acceptance/cmd/aitools/mcp/test.toml b/acceptance/cmd/aitools/mcp/test.toml index 340a89ac46e..b1d08986b6a 100644 --- a/acceptance/cmd/aitools/mcp/test.toml +++ b/acceptance/cmd/aitools/mcp/test.toml @@ -13,3 +13,8 @@ Env.APPDATA = "" [[Repls]] Old = '\\\\' New = '/' + +# The login hint quotes a config path that a shell would read differently, such as the colon of a Windows drive. +[[Repls]] +Old = "'(\\[TEST_TMP_DIR\\][^']*)'" +New = "$1" diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index 20c296c7457..69a7f872166 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -37,6 +37,8 @@ func NewMcpCmd() *cobra.Command { Hidden: true, } cmd.AddCommand(NewMcpAddCmd()) + cmd.AddCommand(NewMcpListCmd()) + cmd.AddCommand(NewMcpRemoveCmd()) return cmd } diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 960c5b2d458..288e884a0b9 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -566,12 +566,11 @@ type agentEntries map[*agents.Agent]map[string]map[string]any func readAgentEntries(ctx context.Context, targets []*agents.Agent) agentEntries { held := make(agentEntries, len(targets)) + files := newAgentFiles() for _, a := range targets { - entries, err := agentconfig.Read(ctx, a) - if err != nil { - continue + if cfg := files.current(ctx, a); cfg.err == nil { + held[a] = cfg.entries } - held[a] = entries } return held } diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 981c350d3c5..6169d390538 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -598,9 +598,9 @@ func TestCursorApprovalHintNamesBinary(t *testing.T) { cursorApprovalHint("github-logfood")) } -// newAddTestRoot returns a root command with the persistent --profile and --output flags that root +// newMCPTestRoot returns a root command with the persistent --profile and --output flags that root // defines, and the mcp group. -func newAddTestRoot() *cobra.Command { +func newMCPTestRoot() *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") @@ -612,7 +612,7 @@ func newAddTestRoot() *cobra.Command { // 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() + parent := newMCPTestRoot() var stdout bytes.Buffer parent.SetOut(&stdout) parent.SetErr(&bytes.Buffer{}) @@ -723,7 +723,7 @@ func TestMcpAddRejectsUnknownAgentBeforeProfile(t *testing.T) { func TestMcpAddJSONWriteFailureKeepsRunError(t *testing.T) { withHome(t) withDatabricksBinary(t) - parent := newAddTestRoot() + parent := newMCPTestRoot() parent.SetOut(failingWriter{}) parent.SetErr(&bytes.Buffer{}) parent.SetArgs([]string{"mcp", "add", "--names", "github", "--output", "json"}) diff --git a/cmd/aitools/mcp_binary.go b/cmd/aitools/mcp_binary.go index 1b5f2659d6a..a92497c60ac 100644 --- a/cmd/aitools/mcp_binary.go +++ b/cmd/aitools/mcp_binary.go @@ -6,7 +6,7 @@ import ( "path/filepath" "strings" - "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/aitools/mcpconfig" ) // binaryArg0 is the name the CLI was started with. It is a variable so that tests can set it. @@ -28,7 +28,7 @@ func binaryPathFor(arg0 string, lookPath func(string) (string, error)) (string, } if filepath.Base(arg0) != arg0 { // A leading "./" names the working directory, which the join already gives. - abs, err := mcp.AbsoluteAsWritten(strings.TrimPrefix(arg0, "."+string(filepath.Separator))) + abs, err := mcpconfig.AbsoluteAsWritten(strings.TrimPrefix(arg0, "."+string(filepath.Separator))) if err != nil { return arg0, false } diff --git a/cmd/aitools/mcp_entries.go b/cmd/aitools/mcp_entries.go new file mode 100644 index 00000000000..285e76c5e3d --- /dev/null +++ b/cmd/aitools/mcp_entries.go @@ -0,0 +1,153 @@ +package aitools + +import ( + "context" + "fmt" + "path/filepath" + + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/aitools/mcp/agentconfig" + "github.com/databricks/cli/libs/aitools/mcpconfig" +) + +// agentFile is an agent config file as a command reads it: the server entries it holds, or why it cannot be read. A +// file that does not exist holds no entries. +type agentFile struct { + entries map[string]map[string]any + err error +} + +// agentFiles reads the agent config files of one command. An agent's current config path is found once, and each file +// is read once, so the file a record names and the file the agent reads now are each read at most once. +type agentFiles struct { + paths map[string]agentPath + files map[agentFileKey]agentFile +} + +// agentPath is the path of an agent's config file as the agent reads it now. err is set when the path cannot be found. +type agentPath struct { + path string + err error +} + +// agentFileKey names a file read: the agent it is read for, and its path. +type agentFileKey struct { + agent string + path string +} + +func newAgentFiles() *agentFiles { + return &agentFiles{paths: map[string]agentPath{}, files: map[agentFileKey]agentFile{}} +} + +// currentPath returns the path of agent a's config file as the agent reads it now. +func (f *agentFiles) currentPath(ctx context.Context, a *agents.Agent) (string, error) { + p, ok := f.paths[a.Name] + if !ok { + p.path, p.err = agentconfig.Path(ctx, a) + f.paths[a.Name] = p + } + return p.path, p.err +} + +// current returns agent a's config file as the agent reads it now. +func (f *agentFiles) current(ctx context.Context, a *agents.Agent) agentFile { + path, err := f.currentPath(ctx, a) + if err != nil { + return agentFile{err: err} + } + return f.file(a, path) +} + +// file returns agent a's config file at path. A record names the file an entry was written to, which need not be the +// file the agent reads now. The file is read on first use. +func (f *agentFiles) file(a *agents.Agent, path string) agentFile { + key := agentFileKey{agent: a.Name, path: path} + file, ok := f.files[key] + if !ok { + entries, err := agentconfig.ReadAt(a, path) + file = agentFile{entries: entries, err: err} + f.files[key] = file + } + return file +} + +// entryLocation is where remove finds a server's entry in one agent config, and what the entry holds. +type entryLocation struct { + // path is the file the entry was found in, or the record's file when no file holds it. + path string + // entry is the entry, when found is set. + entry map[string]any + found bool + // recorded is set when the entry is in the file the record names. + recorded bool +} + +// locateEntry finds the server's entry for agent a as remove does: in the file its record names, when that file holds +// it. Otherwise it is in the file the agent reads now, since the user may have moved the agent's config directory. A +// file that cannot be read is an error, since it may be the one that holds the entry. +func (f *agentFiles) locateEntry(ctx context.Context, e mcp.Entry, a *agents.Agent) (entryLocation, error) { + recorded := e.Agents[a.Name].Path + file := f.file(a, recorded) + if file.err != nil { + return entryLocation{}, file.err + } + if entry, found := file.entries[e.Name]; found { + return entryLocation{path: recorded, entry: entry, found: true, recorded: true}, nil + } + current, err := f.currentPath(ctx, a) + if err != nil { + return entryLocation{}, err + } + if mcp.SamePath(current, recorded) { + return entryLocation{path: recorded}, nil + } + file = f.file(a, current) + if file.err != nil { + return entryLocation{}, file.err + } + entry, found := file.entries[e.Name] + return entryLocation{path: current, entry: entry, found: found}, nil +} + +// bindingFile is the config file that a server is bound to, resolved once for the server. err is set when the file +// cannot be resolved, and then none of the server's entries can be checked for ownership. +type bindingFile struct { + path string + err error +} + +// resolveBinding resolves the config file that e is pinned to, or the default file when it pins none. +func resolveBinding(ctx context.Context, e mcp.Entry) bindingFile { + path, err := mcp.ResolveConfigFile(ctx, e.ConfigFile) + return bindingFile{path: path, err: err} +} + +// checkedBinding returns the binding file of a checked server: the file the check resolved, or the error that stopped it. +func checkedBinding(check mcp.EntryCheck) bindingFile { + if check.Path == "" { + return bindingFile{err: check.Err} + } + return bindingFile{path: check.Path} +} + +// owns reports whether current, the entry under e's name in an agent config, is the one this CLI wrote for e: a +// `serve` entry for its service and profile, through the binding's config file. See mcp.EntryServes. +func (b bindingFile) owns(ctx context.Context, e mcp.Entry, current map[string]any) (bool, error) { + if b.err != nil { + return false, b.err + } + return mcp.EntryServes(ctx, current, e.Service, e.Profile, b.path) +} + +// changedEntryFix says how to clear an entry that was changed outside this CLI, so that remove can take the server out +// of the agent. add replaces an entry this CLI did not write only with --force. It refuses a Databricks entry that serves +// another binding whatever --force says, so that entry is deleted by hand. +func changedEntryFix(name, path string, entry map[string]any) string { + fix := fmt.Sprintf("delete the entry from %s by hand and run `%s` again", filepath.ToSlash(path), mcp.RemoveNamesCommand(name)) + if mcpconfig.IsDatabricksEntry(entry) { + return fix + } + return fix + ", or add the server again with --force" +} diff --git a/cmd/aitools/mcp_entries_test.go b/cmd/aitools/mcp_entries_test.go new file mode 100644 index 00000000000..0ceeeb8bfaf --- /dev/null +++ b/cmd/aitools/mcp_entries_test.go @@ -0,0 +1,61 @@ +package aitools + +import ( + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestAgentFilesReadsEachFileOnce checks that a file is read on first use and then served from the cache: a second read +// in the same command returns what the first read saw, even when the file has changed since. +func TestAgentFilesReadsEachFileOnce(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + path := filepath.Join(home, ".claude.json") + require.NoError(t, os.WriteFile(path, []byte(`{"mcpServers":{"first":{"command":"ug","args":["mcp-proxy"]}}}`), 0o600)) + + files := newAgentFiles() + before := files.file(claude, path) + require.NoError(t, before.err) + require.Contains(t, before.entries, "first") + + require.NoError(t, os.WriteFile(path, []byte(`{"mcpServers":{"second":{"command":"ug","args":["mcp-proxy"]}}}`), 0o600)) + assert.Equal(t, before, files.file(claude, path), "the same command reads the file once") + + fresh := newAgentFiles().file(claude, path) + require.NoError(t, fresh.err) + assert.Contains(t, fresh.entries, "second", "a new command reads the file again") +} + +// TestAgentFilesFindsCurrentPathOnce checks that an agent's current config path is found once: the agent's +// directory, changed after the first lookup, does not move the path for the rest of the command. +func TestAgentFilesFindsCurrentPathOnce(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + first := t.TempDir() + t.Setenv("CLAUDE_CONFIG_DIR", first) + + files := newAgentFiles() + path, err := files.currentPath(t.Context(), claude) + require.NoError(t, err) + assert.Equal(t, filepath.Join(first, ".claude.json"), path) + + t.Setenv("CLAUDE_CONFIG_DIR", t.TempDir()) + again, err := files.currentPath(t.Context(), claude) + require.NoError(t, err) + assert.Equal(t, path, again) +} + +// TestCheckedBindingKeepsTheCheckError checks that a server whose config file could not be resolved has no binding +// file, and that its ownership check reports the check's error. +func TestCheckedBindingKeepsTheCheckError(t *testing.T) { + checkErr := assert.AnError + binding := checkedBinding(mcp.EntryCheck{Err: checkErr}) + _, err := binding.owns(t.Context(), mcp.Entry{}, nil) + assert.ErrorIs(t, err, checkErr) +} diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go new file mode 100644 index 00000000000..d80972d75cc --- /dev/null +++ b/cmd/aitools/mcp_list.go @@ -0,0 +1,479 @@ +package aitools + +import ( + "cmp" + "context" + "errors" + "fmt" + "maps" + "path/filepath" + "slices" + "strings" + + "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/aitools/mcpconfig" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/spf13/cobra" +) + +// Agent entry statuses of `mcp list`. Server statuses are the drift kinds of mcp.CheckEntries, plus statusOK and +// statusUnknown, which is an entry or config that could not be checked. +const ( + agentStatusInstalled = "installed" + agentStatusMissingOnDisk = "missing_on_disk" + agentStatusModified = "modified" + agentStatusPathChanged = "path_changed" + statusUnknown = "unknown" + statusOK = "ok" +) + +// Unmanaged entry kinds of `mcp list`. +const ( + unmanagedKindUGProxy = "ug-proxy" + unmanagedKindAIGateway = "ai-gateway-url" + unmanagedKindNoRecord = "no-record" +) + +// mcpListOutput is the `mcp list` document. +type mcpListOutput struct { + Profiles []mcpListProfile `json:"profiles"` + Unmanaged []mcpListUnmanaged `json:"unmanaged"` + // Warnings name the entries and agent configs that could not be checked. The rest is still listed. + Warnings []string `json:"warnings"` + // Error and ErrorCategory are set when the state cannot be read. The listing is then empty. + Error string `json:"error,omitempty"` + ErrorCategory protos.AitoolsErrorCategory `json:"error_category,omitempty"` +} + +// mcpListProfile groups the servers that bind to one profile in one config file. +type mcpListProfile struct { + Profile string `json:"profile"` + Host string `json:"host"` + // ConfigFile is the config file the servers are pinned to. It is set only when a server is pinned. + ConfigFile string `json:"config_file,omitempty"` + Status string `json:"status"` + Servers []mcpListServer `json:"servers"` + + // configPath is the config file the profile is read from, resolved. The text names it when the profile is missing. + configPath string + // noHost is set when Status is profile_missing because the profile's section has no host. + noHost bool +} + +type mcpListServer struct { + Name string `json:"name"` + Service string `json:"service"` + // Host is the host the server was set up for. The profile's host can differ after a change. + Host string `json:"host"` + SignInRequired bool `json:"sign_in_required"` + Status string `json:"status"` + Agents []mcpListAgent `json:"agents"` + + // entry is the state record, and drift the drift of the server from its profile, when it has one. + entry mcp.Entry + drift *mcp.Drift +} + +type mcpListAgent struct { + Name string `json:"name"` + Path string `json:"path"` + Status string `json:"status"` + // fix says how to clear a modified entry, for the text listing. + fix string +} + +type mcpListUnmanaged struct { + Agent string `json:"agent"` + Name string `json:"name"` + Kind string `json:"kind"` +} + +// profileKey groups the servers of one profile name in one resolved config file. +type profileKey struct { + profile string + path string +} + +// NewMcpListCmd returns `aitools mcp list`. It reads the state file and the agent configs only, so it +// needs no network. Drift and unreadable entries are reported, not failed: the command exits 0. +func NewMcpListCmd() *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "List Databricks managed MCP servers installed by this CLI", + Long: `List Databricks managed MCP servers installed by this CLI, grouped by profile, with the +coding agents each one is in. Entries that drifted from their profile or from the agent config are +reported. Entries in agent configs that this CLI did not write are listed as unmanaged and never changed. +Entries that run 'aitools mcp serve' but have no install record are listed as unmanaged too. + +--profile lists the servers of that profile only. Unmanaged entries have no profile, so they are not +listed then. + +This command makes no network calls. It always exits 0 when entries have drifted or cannot be checked; +an entry or agent config that cannot be checked is a warning.`, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + profileName := profileFlag(cmd) + out, err := buildMcpList(ctx, profileName) + if !installOutputIsJSON(cmd) { + if err != nil { + return err + } + renderMcpList(ctx, out, profileName) + return nil + } + // The document carries the failure, as add and remove write theirs. + if err != nil { + out.Error = err.Error() + out.ErrorCategory = errorCategory(err) + } + if renderErr := renderJSON(cmd.OutOrStdout(), out); renderErr != nil { + return errors.Join(err, renderErr) + } + if err != nil { + return root.ErrAlreadyPrinted + } + return nil + }, + } +} + +// buildMcpList reads the state and the agent configs and returns the listing. profileName, when set, lists the +// servers of that profile only. +func buildMcpList(ctx context.Context, profileName string) (mcpListOutput, error) { + out := mcpListOutput{Profiles: []mcpListProfile{}, Unmanaged: []mcpListUnmanaged{}, Warnings: []string{}} + + state, err := mcp.LoadState(ctx) + if err != nil { + return out, err + } + var entries []mcp.Entry + for _, e := range state.Servers { + if profileName == "" || e.Profile == profileName { + entries = append(entries, e) + } + } + // Without --profile, the configs of every registry agent are read. The agents that the listed servers are recorded + // for are read too, so that a record for an agent outside the registry is reported as unknown, not as a moved config. + var names []string + if profileName == "" { + names = mcpAgentNames() + } + for _, e := range entries { + for _, name := range slices.Sorted(maps.Keys(e.Agents)) { + if !slices.Contains(names, name) { + names = append(names, name) + } + } + } + files := newAgentFiles() + for _, name := range names { + a := agents.ByName(name) + if a == nil { + out.Warnings = append(out.Warnings, fmt.Sprintf("could not read the %s config: %s is not a known agent", name, name)) + continue + } + if !a.SupportsMCP() { + // Its records are warned about with their servers, since the agent has no config to read. + continue + } + if cfg := files.current(ctx, a); cfg.err != nil { + out.Warnings = append(out.Warnings, fmt.Sprintf("could not read the %s config: %s", agentDisplayName(name), cfg.err)) + } + } + + checks := mcp.CheckEntries(ctx, &mcp.State{Servers: entries}) + groups := map[profileKey]*mcpListProfile{} + // A config file that cannot be read is warned about once, for every server that reads it. + warnedFiles := map[string]bool{} + for i, e := range entries { + check := checks[i] + path := check.Path + if path == "" { + // The file cannot be resolved, so the entry is grouped under its file as written. + path = e.ConfigFile + } + key := profileKey{profile: e.Profile, path: path} + p, ok := groups[key] + if !ok { + p = &mcpListProfile{Profile: e.Profile, Host: e.Host, Status: statusOK, Servers: []mcpListServer{}, configPath: path} + groups[key] = p + } + if e.ConfigFile != "" { + p.ConfigFile = filepath.ToSlash(path) + } + s := mcpListServer{Name: e.Name, Service: e.Service, Host: e.Host, SignInRequired: e.SignInRequired, Status: statusOK, Agents: []mcpListAgent{}, entry: e} + switch { + case check.Err != nil: + s.Status = statusUnknown + p.Status = worseStatus(p.Status, statusUnknown) + if check.Path == "" { + out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", e.Name, check.Err)) + } else if !warnedFiles[check.Path] { + warnedFiles[check.Path] = true + out.Warnings = append(out.Warnings, fmt.Sprintf("%s: %s", filepath.ToSlash(check.Path), check.Err)) + } + case check.Drift != nil: + s.Status = string(check.Drift.Kind) + s.drift = check.Drift + p.Status = worseStatus(p.Status, s.Status) + p.noHost = p.noHost || check.Drift.NoHost + if check.Drift.Kind == mcp.DriftHostChanged { + p.Host = check.Drift.CurrentHost + } + } + for _, agentName := range slices.Sorted(maps.Keys(e.Agents)) { + // An agent whose config cannot be read is warned about once, for the agent; here its entries are unknown. + a := agents.ByName(agentName) + status, path, fix := statusUnknown, e.Agents[agentName].Path, "" + switch { + case a == nil: + case !a.SupportsMCP(): + out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s does not take MCP servers; remove drops this record", e.Name, agentDisplayName(agentName))) + default: + var err error + status, path, fix, err = agentStatus(ctx, files, e, a, checkedBinding(check)) + // An error the entry check or the agent's config already reported is not warned about again. + if err != nil && !errors.Is(err, check.Err) && !errors.Is(err, files.current(ctx, a).err) { + out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: could not check %s: %s", e.Name, agentDisplayName(agentName), err)) + } + } + s.Agents = append(s.Agents, mcpListAgent{ + Name: agentName, + Path: filepath.ToSlash(path), + Status: status, + fix: fix, + }) + } + p.Servers = append(p.Servers, s) + } + + keys := slices.Collect(maps.Keys(groups)) + slices.SortFunc(keys, func(a, b profileKey) int { + return cmp.Or(strings.Compare(a.profile, b.profile), strings.Compare(a.path, b.path)) + }) + for _, key := range keys { + out.Profiles = append(out.Profiles, *groups[key]) + } + + // Unmanaged entries have no profile, so a --profile listing has none. + if profileName != "" { + return out, nil + } + for _, a := range agents.MCPAgents() { + cfg := files.current(ctx, a) + if cfg.err != nil { + continue + } + for _, entryName := range slices.Sorted(maps.Keys(cfg.entries)) { + kind := unmanagedKind(cfg.entries[entryName]) + if kind == "" || recordedFor(state, a.Name, entryName) { + continue + } + out.Unmanaged = append(out.Unmanaged, mcpListUnmanaged{Agent: a.Name, Name: entryName, Kind: kind}) + } + } + return out, nil +} + +// worseRank orders the statuses of a profile group. A server that could not be checked makes its profile unknown, +// which is worse than ok and less serious than a drift. +var worseRank = map[string]int{statusOK: 0, statusUnknown: 1, string(mcp.DriftHostChanged): 2, string(mcp.DriftProfileMissing): 3} + +// worseStatus returns the more serious of two server statuses, for the status of a profile. +func worseStatus(a, b string) string { + if worseRank[b] > worseRank[a] { + return b + } + return a +} + +// agentStatus compares the server's entry for agent a with the entry that remove acts on (see locateEntry). It returns +// the status, the path of the file the entry is in, and how to clear a modified entry. A recorded entry is path_changed +// when the agent now reads another file: the record names the file the entry was written to, which the agent no longer +// reads. pin decides ownership. err is set when the entry cannot be located or checked. +func agentStatus(ctx context.Context, files *agentFiles, rec mcp.Entry, a *agents.Agent, pin bindingFile) (status, path, fix string, err error) { + loc, err := files.locateEntry(ctx, rec, a) + if err != nil { + return statusUnknown, rec.Agents[a.Name].Path, "", err + } + if !loc.found { + return agentStatusMissingOnDisk, loc.path, "", nil + } + if loc.recorded { + current, err := files.currentPath(ctx, a) + if err != nil { + return statusUnknown, loc.path, "", err + } + if !mcp.SamePath(loc.path, current) { + return agentStatusPathChanged, loc.path, "", nil + } + } + owned, err := pin.owns(ctx, rec, loc.entry) + if err != nil { + return statusUnknown, loc.path, "", err + } + if !owned { + return agentStatusModified, loc.path, changedEntryFix(rec.Name, loc.path, loc.entry), nil + } + return agentStatusInstalled, loc.path, "", nil +} + +// unmanagedKind names how an entry reaches managed MCP servers without being recorded for its agent, or returns "" +// for any other entry. An entry that runs `aitools mcp serve` and is not recorded has no install record. +func unmanagedKind(entry map[string]any) string { + switch { + case mcpconfig.IsDatabricksEntry(entry): + return unmanagedKindNoRecord + case agentconfig.IsUGProxy(entry): + return unmanagedKindUGProxy + case agentconfig.IsUnmanaged(entry): + return unmanagedKindAIGateway + } + return "" +} + +// recordedFor reports whether state has a server with this name installed for the agent. +func recordedFor(state *mcp.State, agentName, serverName string) bool { + return slices.ContainsFunc(state.Servers, func(e mcp.Entry) bool { + _, ok := e.Agents[agentName] + return ok && e.Name == serverName + }) +} + +// mcpAgentList joins the display names of agents, in the order given. +func mcpAgentList(names []string) string { + display := make([]string, 0, len(names)) + for _, name := range names { + display = append(display, agentDisplayName(name)) + } + return strings.Join(display, ", ") +} + +// renderMcpList writes the text listing: the profiles, the unmanaged entries, then the warnings. profileName is the +// --profile value, which only changes the message for an empty listing. +func renderMcpList(ctx context.Context, out mcpListOutput, profileName string) { + if len(out.Profiles) == 0 && len(out.Unmanaged) == 0 { + if profileName != "" { + cmdio.LogString(ctx, fmt.Sprintf("No Databricks MCP servers installed by this CLI on profile %s.", profileName)) + } else { + cmdio.LogString(ctx, "No Databricks MCP servers installed by this CLI.") + } + } + for i, p := range out.Profiles { + if i > 0 { + cmdio.LogString(ctx, "") + } + cmdio.LogString(ctx, profileHeader(p)) + for _, s := range p.Servers { + cmdio.LogString(ctx, fmt.Sprintf(" %-24s %-30s %s", s.Name, s.Service, listAgentsText(s.Agents))) + if hint := serverHint(ctx, p, s); hint != "" { + cmdio.LogString(ctx, " "+hint) + } + for _, a := range s.Agents { + if a.Status == agentStatusModified { + cmdio.LogString(ctx, fmt.Sprintf(" changed outside this CLI in %s: %s", agentDisplayName(a.Name), a.fix)) + } + } + } + } + if len(out.Unmanaged) > 0 { + if len(out.Profiles) > 0 { + cmdio.LogString(ctx, "") + } + cmdio.LogString(ctx, "Unmanaged Databricks MCP entries (not changed by this command):") + for _, u := range out.Unmanaged { + cmdio.LogString(ctx, fmt.Sprintf(" %-14s %-20s %s", agentDisplayName(u.Agent), u.Name, unmanagedKindText(u.Kind))) + } + } + for _, w := range out.Warnings { + cmdio.LogString(ctx, "Warning: "+w) + } +} + +// profileHeader names a profile group: its host, or the config file it was not found in when the profile is missing. +// A pinned group also names its config file, so that groups of one profile and host are told apart. +func profileHeader(p mcpListProfile) string { + if p.Status == string(mcp.DriftProfileMissing) { + problem := "profile not found in" + if p.noHost { + problem = "profile has no host in" + } + return fmt.Sprintf("Profile %s (%s %s)", p.Profile, problem, filepath.ToSlash(p.configPath)) + } + if p.ConfigFile != "" { + return fmt.Sprintf("Profile %s (%s, config file %s)", p.Profile, p.Host, p.ConfigFile) + } + return fmt.Sprintf("Profile %s (%s)", p.Profile, p.Host) +} + +// serverHint tells the user how to repair a server whose profile no longer serves it. The remove command names the +// server, so that the other servers of the profile are kept. +func serverHint(ctx context.Context, p mcpListProfile, s mcpListServer) string { + if s.Status == statusUnknown { + return "unchecked: see the warning below" + } + if s.drift == nil { + return "" + } + remove := mcp.RemoveNamesCommand(s.Name) + if s.drift.Kind == mcp.DriftProfileMissing { + return fmt.Sprintf("broken: run `%s` or `%s`", remove, mcp.RecreateCommand(s.entry.Host, p.Profile, loginFile(ctx, p, s.entry))) + } + return fmt.Sprintf("broken: %s; run `%s` and add it again", mcp.DriftMessage("it", s.entry, p.Profile, *s.drift), remove) +} + +// loginFile returns the config file that a login hint for a server with a missing profile must name, or "" when the +// login's default file is that file. A pinned server logs in to the file it is pinned to. An unpinned server is checked +// against ~/.databrickscfg, and login writes to the file DATABRICKS_CONFIG_FILE names, so the hint names the checked +// file when the environment names another one, or one that cannot be resolved. +func loginFile(ctx context.Context, p mcpListProfile, entry mcp.Entry) string { + if entry.ConfigFile != "" { + return filepath.ToSlash(p.configPath) + } + environment := env.Get(ctx, mcpconfig.ConfigFileEnv) + if environment == "" { + return "" + } + path, err := mcp.ResolveConfigFile(ctx, environment) + if err == nil && mcp.SamePath(path, p.configPath) { + return "" + } + return filepath.ToSlash(p.configPath) +} + +// listAgentsText lists the agents of a server, with the status of each entry that needs a word. +func listAgentsText(agentList []mcpListAgent) string { + parts := make([]string, 0, len(agentList)) + for _, a := range agentList { + text := agentDisplayName(a.Name) + switch a.Status { + case agentStatusMissingOnDisk: + text += " (missing on disk)" + case agentStatusModified: + text += " (modified)" + case agentStatusPathChanged: + text += " (path changed)" + case statusUnknown: + text += " (unknown)" + } + parts = append(parts, text) + } + return strings.Join(parts, ", ") +} + +// unmanagedKindText describes an unmanaged entry kind for the text listing. +func unmanagedKindText(kind string) string { + switch kind { + case unmanagedKindUGProxy: + return "ug mcp-proxy" + case unmanagedKindNoRecord: + return "databricks aitools mcp serve, no install record" + } + return "/ai-gateway/mcp-services/ URL" +} diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go new file mode 100644 index 00000000000..c09a9b36202 --- /dev/null +++ b/cmd/aitools/mcp_list_test.go @@ -0,0 +1,869 @@ +package aitools + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/databricks/cli/cmd/root" + "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/cmdio" + "github.com/databricks/cli/libs/shellquote" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const testDatabricksCommand = "/opt/databricks/bin/databricks" + +// seed describes one server as `add` writes it: its binding, the command it runs and the config file it is pinned to. +type seed struct { + name, service, profile, host, command, configFile string +} + +// seedInstallEntry writes the server for an agent with Upsert and returns the state record. Like `add`, the entry +// and the record are pinned to configFile when it is set. +func seedInstallEntry(t *testing.T, a *agents.Agent, s seed) mcp.Entry { + t.Helper() + args := []string{"aitools", "mcp", "serve", "--profile", s.profile, "--service", s.service, "--agent", a.Name} + var env map[string]string + if s.configFile != "" { + env = map[string]string{mcpconfig.ConfigFileEnv: s.configFile} + } + path, _, err := agentconfig.Upsert(t.Context(), a, s.name, s.command, args, env, false) + require.NoError(t, err) + return mcp.Entry{ + Name: s.name, + Service: s.service, + Profile: s.profile, + Host: s.host, + Command: s.command, + ConfigFile: s.configFile, + Agents: map[string]mcp.AgentRecord{a.Name: {Path: path, Scope: "user"}}, + } +} + +// seedInstall writes the server for an agent with the test command, pinned to DATABRICKS_CONFIG_FILE when it is set. +func seedInstall(t *testing.T, a *agents.Agent, name, service, profileName, host string) mcp.Entry { + t.Helper() + return seedInstallEntry(t, a, seed{name, service, profileName, host, testDatabricksCommand, os.Getenv("DATABRICKS_CONFIG_FILE")}) +} + +// editEntry changes the server name in the agent config at path, as a user editing the file by hand would. +func editEntry(t *testing.T, path, name string, edit func(server map[string]any)) { + t.Helper() + doc := readJSONFile(t, path) + edit(doc["mcpServers"].(map[string]any)[name].(map[string]any)) + data, err := json.MarshalIndent(doc, "", " ") + require.NoError(t, err) + require.NoError(t, os.WriteFile(path, data, 0o600)) +} + +// repointEntry sets the command of a Claude Code server, as a user editing the file by hand would. Upsert refuses a +// command that is not the databricks binary, so it cannot write this. +func repointEntry(t *testing.T, path, name, command string) { + t.Helper() + editEntry(t, path, name, func(server map[string]any) { server["command"] = command }) +} + +// setServeFlag changes the value of a `serve` flag in the argument list of a hand-edited entry. +func setServeFlag(t *testing.T, server map[string]any, flag, value string) { + t.Helper() + args, ok := server["args"].([]any) + require.True(t, ok) + for i := range args { + if args[i] == flag { + args[i+1] = value + return + } + } + require.Failf(t, "flag not found", "%s", flag) +} + +func saveState(t *testing.T, entries ...mcp.Entry) { + t.Helper() + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: entries})) +} + +// runMcpCmd runs `mcp list` or `mcp remove` with args and returns its stdout and stderr. +func runMcpCmd(t *testing.T, args ...string) (stdout, stderr *bytes.Buffer, err error) { + t.Helper() + parent := newMCPTestRoot() + ctx, stderrBuf := cmdio.NewTestContextWithStderr(t.Context()) + var stdoutBuf bytes.Buffer + parent.SetOut(&stdoutBuf) + parent.SetErr(&bytes.Buffer{}) + parent.SetArgs(append([]string{"mcp"}, args...)) + return &stdoutBuf, stderrBuf, parent.ExecuteContext(ctx) +} + +func TestMcpHandEditedEntryIsModified(t *testing.T) { + cases := []struct { + name string + edit func(t *testing.T, server map[string]any) + }{ + {"profile", func(t *testing.T, server map[string]any) { + setServeFlag(t, server, "--profile", "other") + }}, + {"service", func(t *testing.T, server map[string]any) { + setServeFlag(t, server, "--service", "system.ai.github") + }}, + {"pinned config file", func(t *testing.T, server map[string]any) { + server["env"] = map[string]any{mcpconfig.ConfigFileEnv: filepath.Join(t.TempDir(), "other.cfg")} + }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + editEntry(t, claudePath, entry.Name, func(server map[string]any) { tc.edit(t, server) }) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, 1) + require.Len(t, out.Profiles[0].Servers[0].Agents, 1) + assert.Equal(t, agentStatusModified, out.Profiles[0].Servers[0].Agents[0].Status, "an entry that serves another binding is not ours") + + stdout, _, err := runMcpCmd(t, "remove", "--names", entry.Name, "--output", "json") + require.NoError(t, err) + var removed removeOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &removed), stdout.String()) + assert.Empty(t, removed.Removed) + require.Len(t, removed.Skipped, 1) + assert.Equal(t, claude.Name, removed.Skipped[0].Agent) + assert.Contains(t, readJSONFile(t, claudePath)["mcpServers"], entry.Name, "the hand-edited entry is kept") + }) + } +} + +func TestMcpReAddFromAnotherBinaryKeepsOtherAgentsRemovable(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + codex := agents.ByName("codex") + require.NoError(t, os.MkdirAll(filepath.Join(home, ".codex"), 0o755)) + + const name = "genie_one_mcp-logfood" + const service = "system.ai.genie_one_mcp" + first := seedInstall(t, claude, name, service, "logfood", "https://adb-1.test") + // `add` for codex ran from another binary, and the record keeps the command of the last add. + second := seedInstallEntry(t, codex, seed{name, service, "logfood", "https://adb-1.test", "/opt/other/databricks", first.ConfigFile}) + first.Agents[codex.Name] = second.Agents[codex.Name] + first.Command = "/opt/other/databricks" + saveState(t, first) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Profiles, 1) + for _, a := range out.Profiles[0].Servers[0].Agents { + assert.Equal(t, agentStatusInstalled, a.Status, a.Name) + } + + stdout, _, err := runMcpCmd(t, "remove", "--names", name, "--output", "json") + require.NoError(t, err) + var removed removeOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &removed), stdout.String()) + require.Len(t, removed.Removed, 1) + assert.ElementsMatch(t, []string{"claude-code", "codex"}, removed.Removed[0].Agents) + assert.Empty(t, removed.Skipped) +} + +func TestBuildMcpListGroupsProfilesByConfigFile(t *testing.T) { + withHome(t) + dir := t.TempDir() + first := filepath.Join(dir, "first.cfg") + second := filepath.Join(dir, "second.cfg") + // The profile of the first file now names another host. The second file is healthy. + require.NoError(t, os.WriteFile(first, []byte("[logfood]\nhost = https://adb-9.test\ntoken = t\n"), 0o600)) + require.NoError(t, os.WriteFile(second, []byte("[logfood]\nhost = https://adb-2.test\ntoken = t\n"), 0o600)) + t.Setenv("DATABRICKS_CONFIG_FILE", first) + + claude := agents.ByName("claude-code") + // One server recorded against the old host, one added later against the new host, and one in the second file. + drifted := seedInstallEntry(t, claude, seed{"genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test", testDatabricksCommand, first}) + current := seedInstallEntry(t, claude, seed{"github-logfood", "system.ai.github", "logfood", "https://adb-9.test", testDatabricksCommand, first}) + healthy := seedInstallEntry(t, claude, seed{"genie_one_mcp-other", "system.ai.genie_one_mcp", "logfood", "https://adb-2.test", testDatabricksCommand, second}) + saveState(t, drifted, current, healthy) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Profiles, 2, "one profile name in two config files is two groups") + + firstPath, err := mcp.ResolveConfigFile(t.Context(), first) + require.NoError(t, err) + secondPath, err := mcp.ResolveConfigFile(t.Context(), second) + require.NoError(t, err) + + pFirst := out.Profiles[0] + assert.Equal(t, filepath.ToSlash(firstPath), pFirst.ConfigFile) + assert.Equal(t, "host_changed", pFirst.Status, "the group status is the worst of its servers") + assert.Equal(t, "https://adb-9.test", pFirst.Host) + require.Len(t, pFirst.Servers, 2) + assert.Equal(t, "host_changed", pFirst.Servers[0].Status) + assert.Equal(t, "ok", pFirst.Servers[1].Status, "a server added against the new host is not broken") + + pSecond := out.Profiles[1] + assert.Equal(t, filepath.ToSlash(secondPath), pSecond.ConfigFile) + assert.Equal(t, "ok", pSecond.Status) + require.Len(t, pSecond.Servers, 1) + assert.Equal(t, "ok", pSecond.Servers[0].Status) +} + +func TestBuildMcpListReportsAgentPathChanged(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + recordedPath := filepath.Join(home, ".claude.json") + + // The agent config now lives elsewhere, so the entry is in a file this command does not read. + t.Setenv("CLAUDE_CONFIG_DIR", t.TempDir()) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, 1) + assert.Equal(t, []mcpListAgent{{Name: "claude-code", Path: filepath.ToSlash(recordedPath), Status: agentStatusPathChanged}}, out.Profiles[0].Servers[0].Agents) +} + +// TestBuildMcpListRecordForUnknownAgentIsUnknown checks that a record for an agent outside the registry is unknown, +// with a warning, rather than a config that moved. +func TestBuildMcpListRecordForUnknownAgentIsUnknown(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + saveState(t, mcp.Entry{ + Name: "genie_one_mcp-logfood", Service: "system.ai.genie_one_mcp", Profile: "logfood", Host: "https://adb-1.test", + Command: testDatabricksCommand, Agents: map[string]mcp.AgentRecord{"retired-agent": {Path: "/x/config.json", Scope: "user"}}, + }) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, 1) + require.Len(t, out.Profiles[0].Servers[0].Agents, 1) + assert.Equal(t, statusUnknown, out.Profiles[0].Servers[0].Agents[0].Status) + assert.Contains(t, out.Warnings, "could not read the retired-agent config: retired-agent is not a known agent") +} + +func TestBuildMcpListUnknownServerIsWarnedNotOK(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + broken := filepath.Join(t.TempDir(), "broken.cfg") + require.NoError(t, os.WriteFile(broken, []byte("[logfood\n"), 0o600)) + + claude := agents.ByName("claude-code") + gone := seedInstall(t, claude, "dbsql-old", "system.ai.dbsql", "old", "https://adb-3.test") + unreadable := mcp.Entry{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-1.test", + Command: testDatabricksCommand, ConfigFile: broken, + } + saveState(t, gone, unreadable) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.NotEmpty(t, out.Warnings) + // The warning names the resolved file, which on Windows expands the short name of the temp directory. + resolved, err := filepath.EvalSymlinks(broken) + require.NoError(t, err) + assert.Contains(t, out.Warnings[0], filepath.ToSlash(resolved)+": failed to read Databricks profiles", "the file is warned about once, by name") + + require.Len(t, out.Profiles, 2) + logfood := out.Profiles[0] + assert.Equal(t, "logfood", logfood.Profile) + assert.Equal(t, statusUnknown, logfood.Status, "a profile with an entry that could not be checked is not ok") + require.Len(t, logfood.Servers, 1) + assert.Equal(t, statusUnknown, logfood.Servers[0].Status, "an entry that could not be checked is not reported as ok") + assert.Equal(t, "old", out.Profiles[1].Profile) + assert.Equal(t, "profile_missing", out.Profiles[1].Status) +} + +func TestMcpListExitsZeroWhenAnEntryCannotBeChecked(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + broken := filepath.Join(t.TempDir(), "broken.cfg") + require.NoError(t, os.WriteFile(broken, []byte("[logfood\n"), 0o600)) + saveState(t, mcp.Entry{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-1.test", + Command: testDatabricksCommand, ConfigFile: broken, + }) + + stdout, _, err := runMcpCmd(t, "list", "--output", "json") + require.NoError(t, err, "an entry that cannot be checked is a warning, not a failure") + var out mcpListOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &out), stdout.String()) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, 1) + assert.Equal(t, statusUnknown, out.Profiles[0].Servers[0].Status) + assert.NotEmpty(t, out.Warnings) +} + +func TestBuildMcpListWarnsForUnreadableAgentConfig(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + codex := agents.ByName("codex") + require.NoError(t, os.MkdirAll(filepath.Join(home, ".codex"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte("[mcp_servers.broken\n"), 0o600)) + claudePath := filepath.Join(home, ".claude.json") + require.NoError(t, os.WriteFile(claudePath, []byte(`{"mcpServers":{"web_search":{"command":"ug","args":["mcp-proxy"]}}}`), 0o600)) + + genie := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + // Recorded for codex, whose config cannot be read. + github := mcp.Entry{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-1.test", + Command: testDatabricksCommand, ConfigFile: os.Getenv("DATABRICKS_CONFIG_FILE"), + Agents: map[string]mcp.AgentRecord{codex.Name: {Path: filepath.Join(home, ".codex", "config.toml"), Scope: "user"}}, + } + saveState(t, genie, github) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err, "one unreadable agent does not fail the listing") + require.Len(t, out.Warnings, 1) + assert.Contains(t, out.Warnings[0], "Codex CLI") + + require.Len(t, out.Profiles, 1) + servers := out.Profiles[0].Servers + require.Len(t, servers, 2) + require.Len(t, servers[0].Agents, 1) + require.Len(t, servers[1].Agents, 1) + assert.Equal(t, agentStatusInstalled, servers[0].Agents[0].Status, "the readable agent is listed") + assert.Equal(t, statusUnknown, servers[1].Agents[0].Status, "the recorded server in the unreadable agent is unknown") + assert.Equal(t, []mcpListUnmanaged{{Agent: "claude-code", Name: "web_search", Kind: unmanagedKindUGProxy}}, out.Unmanaged) +} + +func TestBuildMcpListReportsOrphanDatabricksEntry(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + claude := agents.ByName("claude-code") + // Written by this CLI, but its state record is gone. + seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + assert.Empty(t, out.Profiles) + assert.Equal(t, []mcpListUnmanaged{{Agent: "claude-code", Name: "genie_one_mcp-logfood", Kind: unmanagedKindNoRecord}}, out.Unmanaged) +} + +func TestBuildMcpListDriftAndUnmanaged(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, `[logfood] +host = https://adb-1.test +token = t + +[dogfood] +host = https://adb-2.test +token = t +`) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + // A server this CLI did not write, reached through ug, next to ours. + require.NoError(t, os.WriteFile(claudePath, []byte(`{"mcpServers":{"web_search":{"command":"ug","args":["mcp-proxy"]}}}`), 0o600)) + + genie := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + github := seedInstall(t, claude, "github-logfood", "system.ai.github", "logfood", "https://adb-old.test") + dogfood := seedInstall(t, claude, "genie_one_mcp-dogfood", "system.ai.genie_one_mcp", "dogfood", "https://adb-2.test") + // Recorded for claude-code but never written to its config file. + absent := mcp.Entry{ + Name: "dbsql-dogfood", Service: "system.ai.dbsql", Profile: "dogfood", Host: "https://adb-2.test", Command: testDatabricksCommand, + ConfigFile: os.Getenv("DATABRICKS_CONFIG_FILE"), + Agents: map[string]mcp.AgentRecord{"claude-code": {Path: claudePath, Scope: "user"}}, + } + // Written for a profile that is then removed from the config file, and changed by hand afterwards. + gone := seedInstall(t, claude, "dbsql-old", "system.ai.dbsql", "old", "https://adb-3.test") + repointEntry(t, claudePath, "dbsql-old", "/usr/bin/other") + + saveState(t, genie, github, dogfood, absent, gone) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Profiles, 3) + + dogfoodProfile := out.Profiles[0] + assert.Equal(t, "dogfood", dogfoodProfile.Profile) + assert.Equal(t, statusOK, dogfoodProfile.Status) + assert.Equal(t, "https://adb-2.test", dogfoodProfile.Host) + require.Len(t, dogfoodProfile.Servers, 2) + assert.Equal(t, []mcpListAgent{{Name: "claude-code", Path: filepath.ToSlash(claudePath), Status: agentStatusInstalled}}, dogfoodProfile.Servers[0].Agents) + assert.Equal(t, []mcpListAgent{{Name: "claude-code", Path: filepath.ToSlash(claudePath), Status: agentStatusMissingOnDisk}}, dogfoodProfile.Servers[1].Agents) + + logfood := out.Profiles[1] + assert.Equal(t, "logfood", logfood.Profile) + assert.Equal(t, "host_changed", logfood.Status) + assert.Equal(t, "https://adb-1.test", logfood.Host, "a changed host shows the current host of the profile") + require.Len(t, logfood.Servers, 2) + assert.Equal(t, "ok", logfood.Servers[0].Status) + require.Len(t, logfood.Servers[0].Agents, 1) + assert.Equal(t, agentStatusInstalled, logfood.Servers[0].Agents[0].Status) + assert.Equal(t, "https://adb-1.test", logfood.Servers[0].Host, "a server shows the host it was set up for") + assert.Equal(t, "host_changed", logfood.Servers[1].Status, "github-logfood was recorded against the old host") + assert.Equal(t, "https://adb-old.test", logfood.Servers[1].Host, "the recorded host, not the profile's new one") + require.Len(t, logfood.Servers[1].Agents, 1) + assert.Equal(t, agentStatusInstalled, logfood.Servers[1].Agents[0].Status) + + old := out.Profiles[2] + assert.Equal(t, "old", old.Profile) + assert.Equal(t, "profile_missing", old.Status) + assert.Equal(t, "https://adb-3.test", old.Host, "a missing profile shows the recorded host") + require.Len(t, old.Servers, 1) + require.Len(t, old.Servers[0].Agents, 1) + assert.Equal(t, agentStatusModified, old.Servers[0].Agents[0].Status) + + assert.Equal(t, []mcpListUnmanaged{{Agent: "claude-code", Name: "web_search", Kind: unmanagedKindUGProxy}}, out.Unmanaged) + pinned, err := mcp.ResolveConfigFile(t.Context(), os.Getenv("DATABRICKS_CONFIG_FILE")) + require.NoError(t, err) + assert.Equal(t, filepath.ToSlash(pinned), out.Profiles[0].ConfigFile, "a pinned profile names its config file") +} + +func TestBuildMcpListEmpty(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + assert.NotNil(t, out.Profiles, "an empty listing is [] in JSON, not null") + assert.NotNil(t, out.Unmanaged) + assert.NotNil(t, out.Warnings) + assert.Empty(t, out.Profiles) + assert.Empty(t, out.Unmanaged) +} + +func TestUnmanagedKind(t *testing.T) { + cases := []struct { + name string + entry map[string]any + want string + }{ + {"ug mcp-proxy", map[string]any{"command": "ug", "args": []any{"mcp-proxy", "web"}}, unmanagedKindUGProxy}, + {"ug mcp-proxy as a command array", map[string]any{"command": []any{"ug", "mcp-proxy"}}, unmanagedKindUGProxy}, + {"ai gateway URL", map[string]any{"url": "https://adb-1.test/ai-gateway/mcp-services/github"}, unmanagedKindAIGateway}, + {"other command", map[string]any{"command": "npx", "args": []any{"some-server"}}, ""}, + {"mcp-proxy under another command with an ai gateway URL", map[string]any{"command": "npx", "args": []any{"mcp-proxy"}, "url": "https://adb-1.test/ai-gateway/mcp-services/github"}, unmanagedKindAIGateway}, + {"databricks serve without a record", map[string]any{"command": testDatabricksCommand, "args": []any{"aitools", "mcp", "serve"}}, unmanagedKindNoRecord}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, unmanagedKind(tc.entry)) + }) + } +} + +// TestRenderMcpListNamesTheFileAndTheRepair checks the text of the drifts: the file that was checked, the pinned +// variable in the repair hints, and the shell quoting of the profile. +func TestRenderMcpListNamesTheFileAndTheRepair(t *testing.T) { + withHome(t) + dir := t.TempDir() + pinned := filepath.Join(dir, "pinned.cfg") + require.NoError(t, os.WriteFile(pinned, []byte("[logfood]\nhost = https://adb-1.test\nworkspace_id = 222\ntoken = t\n\n[nohost]\ntoken = t\n"), 0o600)) + pinnedPath, err := mcp.ResolveConfigFile(t.Context(), pinned) + require.NoError(t, err) + claude := agents.ByName("claude-code") + + workspaceMoved := seedInstallEntry(t, claude, seed{"genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test", testDatabricksCommand, pinned}) + workspaceMoved.WorkspaceID = "111" + // The workspace is named in the host's query, not in workspace_id. + workspaceFromQuery := seedInstallEntry(t, claude, seed{"genie_one_mcp-query", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test/?o=111", testDatabricksCommand, pinned}) + hostMoved := seedInstallEntry(t, claude, seed{"github-moved", "system.ai.github", "logfood", "https://adb-old.test", testDatabricksCommand, pinned}) + // The profile is not in the file at all, and the profile name needs quoting in the hint. + missing := seedInstallEntry(t, claude, seed{"github-gone", "system.ai.github", "gone profile", "https://adb-old.test", testDatabricksCommand, pinned}) + noHost := seedInstallEntry(t, claude, seed{"dbsql-nohost", "system.ai.dbsql", "nohost", "https://adb-4.test", testDatabricksCommand, pinned}) + saveState(t, workspaceMoved, workspaceFromQuery, hostMoved, missing, noHost) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + text := stderr.String() + + shownPath := filepath.ToSlash(pinnedPath) + assert.Contains(t, text, "Profile logfood (https://adb-1.test, config file "+shownPath+")") + assert.Contains(t, text, "broken: it was set up for workspace 111, but profile \"logfood\" now points at workspace 222; run `databricks aitools mcp remove --names genie_one_mcp-logfood` and add it again") + assert.Contains(t, text, "broken: it was set up for workspace 111, but profile \"logfood\" now points at workspace 222; run `databricks aitools mcp remove --names genie_one_mcp-query` and add it again") + assert.Contains(t, text, "broken: it was set up for https://adb-old.test, but profile \"logfood\" now points at https://adb-1.test; run `databricks aitools mcp remove --names github-moved` and add it again") + assert.Contains(t, text, "Profile gone profile (profile not found in "+shownPath+")") + // The config path is quoted when it has characters a shell would read, such as the colon of a Windows drive. + assert.Contains(t, text, "broken: run `databricks aitools mcp remove --names github-gone` or `DATABRICKS_CONFIG_FILE="+shellquote.BashArg(shownPath)+" databricks auth login --host https://adb-old.test --profile 'gone profile'`") + assert.Contains(t, text, "Profile nohost (profile has no host in "+shownPath+")") + assert.NotContains(t, text, "Warning", "every entry in this test can be checked") +} + +func TestRenderMcpListWarnsAndNamesUnmanagedRecords(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + claude := agents.ByName("claude-code") + seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.Contains(t, stderr.String(), " Claude Code genie_one_mcp-logfood databricks aitools mcp serve, no install record") +} + +// TestMcpListFiltersToProfile checks that --profile lists the servers of that profile only, with no unmanaged entries, +// since those have no profile, and says so when the profile has no server. +func TestMcpListFiltersToProfile(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n\n[dogfood]\nhost = https://adb-2.test\ntoken = t\n") + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + genie := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + other := seedInstall(t, claude, "github-dogfood", "system.ai.github", "dogfood", "https://adb-2.test") + saveState(t, genie, other) + doc := readJSONFile(t, claudePath) + doc["mcpServers"].(map[string]any)["web_search"] = map[string]any{"command": "ug", "args": []any{"mcp-proxy"}} + data, err := json.Marshal(doc) + require.NoError(t, err) + require.NoError(t, os.WriteFile(claudePath, data, 0o600)) + + stdout, _, err := runMcpCmd(t, "list", "--profile", "logfood", "--output", "json") + require.NoError(t, err) + var out mcpListOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &out), stdout.String()) + require.Len(t, out.Profiles, 1) + assert.Equal(t, "logfood", out.Profiles[0].Profile) + assert.Empty(t, out.Unmanaged) + + _, stderr, err := runMcpCmd(t, "list", "--profile", "typo") + require.NoError(t, err) + assert.Contains(t, stderr.String(), "No Databricks MCP servers installed by this CLI on profile typo.") +} + +// TestMcpListJSONReportsStateError checks that a state file that cannot be read is a JSON document with the error, +// as remove writes it. +func TestMcpListJSONReportsStateError(t *testing.T) { + withHome(t) + dir, err := installer.GlobalSkillsDir(t.Context()) + require.NoError(t, err) + require.NoError(t, os.MkdirAll(dir, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, ".state.json"), []byte("{not json"), 0o600)) + + stdout, _, err := runMcpCmd(t, "list", "--output", "json") + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + var out mcpListOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &out), stdout.String()) + assert.NotEmpty(t, out.Error) + assert.Equal(t, protos.AitoolsErrorCategoryStateInvalid, out.ErrorCategory) + assert.Empty(t, out.Profiles) +} + +// TestBuildMcpListWarnsOnceForUnresolvableConfigFile checks that an entry whose config file cannot be resolved gets one +// warning, for the server, not another for each agent it is installed in. The name has a NUL byte, which no file system +// accepts, on every platform. +func TestBuildMcpListWarnsOnceForUnresolvableConfigFile(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + entry.ConfigFile = "unresolvable\x00.cfg" + saveState(t, entry) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + var warnings []string + for _, w := range out.Warnings { + if strings.Contains(w, "genie_one_mcp-logfood") { + warnings = append(warnings, w) + } + } + require.Len(t, warnings, 1, "one cause, one warning: %v", out.Warnings) + assert.Contains(t, warnings[0], "MCP server genie_one_mcp-logfood: ") + require.Len(t, out.Profiles, 1) + assert.Equal(t, statusUnknown, out.Profiles[0].Servers[0].Status) + assert.Equal(t, statusUnknown, out.Profiles[0].Servers[0].Agents[0].Status) +} + +// TestRenderMcpListLoginHintNamesCheckedFile checks the login hint of an unpinned server whose profile is missing from +// ~/.databrickscfg. The entry is checked against that file, so the hint names it when DATABRICKS_CONFIG_FILE names +// another file: login would otherwise write the profile to the environment's file. +func TestRenderMcpListLoginHintNamesCheckedFile(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + claude := agents.ByName("claude-code") + checked, err := mcp.ResolveConfigFile(t.Context(), "") + require.NoError(t, err) + shownChecked := filepath.ToSlash(checked) + saveState(t, seedInstallEntry(t, claude, seed{"github-logfood", "system.ai.github", "logfood", "https://adb-1.test", testDatabricksCommand, ""})) + + cases := []struct { + name, environment, login string + }{ + { + name: "environment names another file", + environment: filepath.Join(t.TempDir(), "other.cfg"), + login: "`DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(shownChecked) + " databricks auth login --host https://adb-1.test --profile logfood`", + }, + { + name: "environment names no file", + environment: "", + login: "`databricks auth login --host https://adb-1.test --profile logfood`", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("DATABRICKS_CONFIG_FILE", tc.environment) + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.Contains(t, stderr.String(), "or "+tc.login) + }) + } +} + +// TestRenderMcpListPrintsWarningsWhenEmpty checks that a listing with no server and no unmanaged entry still prints its +// warnings in text, as JSON does, for a profile with no server and for no profile. +func TestRenderMcpListPrintsWarningsWhenEmpty(t *testing.T) { + warning := "could not read the Codex CLI config: broken" + for _, profileName := range []string{"typo", ""} { + out := mcpListOutput{Profiles: []mcpListProfile{}, Unmanaged: []mcpListUnmanaged{}, Warnings: []string{warning}} + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, profileName) + assert.Contains(t, stderr.String(), "Warning: "+warning, "profile %q", profileName) + } +} + +// TestBuildMcpListWithProfileReadsOnlyItsAgents checks that --profile reads the configs of the agents the profile's +// servers are recorded for, so a broken config of an agent that the profile does not use is not warned about. +func TestBuildMcpListWithProfileReadsOnlyItsAgents(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + require.NoError(t, os.MkdirAll(filepath.Join(home, ".codex"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte("[mcp_servers.broken\n"), 0o600)) + saveState(t, seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test")) + + out, err := buildMcpList(t.Context(), "logfood") + require.NoError(t, err) + assert.Empty(t, out.Warnings, "codex is not used by logfood, so its config is not read") + require.Len(t, out.Profiles, 1) + + all, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, all.Warnings, 1) + assert.Contains(t, all.Warnings[0], "Codex CLI") +} + +// TestBuildMcpListWarnsInStableOrderForUnknownAgents checks that the warnings for two records of agents outside the +// registry come in name order, not in the order a map yields them. The map is iterated many times, so an unstable +// order would show. +func TestBuildMcpListWarnsInStableOrderForUnknownAgents(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "") + for range 20 { + saveState(t, mcp.Entry{ + Name: "genie_one_mcp-logfood", Service: "system.ai.genie_one_mcp", Profile: "logfood", Host: "https://adb-1.test", + Command: testDatabricksCommand, Agents: map[string]mcp.AgentRecord{ + "zeta-agent": {Path: "/x/zeta.json", Scope: "user"}, + "alpha-agent": {Path: "/x/alpha.json", Scope: "user"}, + }, + }) + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Warnings, 2) + assert.Equal(t, "could not read the alpha-agent config: alpha-agent is not a known agent", out.Warnings[0]) + assert.Equal(t, "could not read the zeta-agent config: zeta-agent is not a known agent", out.Warnings[1]) + } +} + +// TestRenderMcpListSaysHowToClearChangedEntry checks that a server with an entry changed outside this CLI says in list how +// to get it out, the same way remove says it. +func TestRenderMcpListSaysHowToClearChangedEntry(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + claudePath := filepath.Join(home, ".claude.json") + repointEntry(t, claudePath, entry.Name, "/usr/bin/other") + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.Contains(t, stderr.String(), " changed outside this CLI in Claude Code: delete the entry from "+filepath.ToSlash(claudePath)+ + " by hand and run `databricks aitools mcp remove --names genie_one_mcp-logfood` again, or add the server again with --force\n") +} + +// TestProfileHeaderNamesPinnedConfigFile checks that two groups of one profile and host, in different config files, have +// different headers: a pinned group names its file. +func TestProfileHeaderNamesPinnedConfigFile(t *testing.T) { + first := mcpListProfile{Profile: "logfood", Host: "https://adb-1.test", ConfigFile: "/cfg/first.cfg", Status: statusOK} + second := mcpListProfile{Profile: "logfood", Host: "https://adb-1.test", ConfigFile: "/cfg/second.cfg", Status: statusOK} + unpinned := mcpListProfile{Profile: "logfood", Host: "https://adb-1.test", Status: statusOK} + + assert.Equal(t, "Profile logfood (https://adb-1.test, config file /cfg/first.cfg)", profileHeader(first)) + assert.NotEqual(t, profileHeader(first), profileHeader(second)) + assert.Equal(t, "Profile logfood (https://adb-1.test)", profileHeader(unpinned)) +} + +// TestRenderMcpListHintForDatabricksEntryOmitsForce checks the hint for an entry this CLI wrote that serves another +// binding now: add refuses it whatever --force says, so the hint says to delete it by hand and does not offer --force. +func TestRenderMcpListHintForDatabricksEntryOmitsForce(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + claudePath := filepath.Join(home, ".claude.json") + editEntry(t, claudePath, entry.Name, func(server map[string]any) { setServeFlag(t, server, "--profile", "other") }) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.Contains(t, stderr.String(), " changed outside this CLI in Claude Code: delete the entry from "+filepath.ToSlash(claudePath)+ + " by hand and run `databricks aitools mcp remove --names genie_one_mcp-logfood` again\n") + assert.NotContains(t, stderr.String(), "--force") +} + +// TestBuildMcpListWarnsOncePerUnreadableProfileFile checks that a config file which cannot be read is one warning, however +// many servers read it. Both servers here read the default file. +func TestBuildMcpListWarnsOncePerUnreadableProfileFile(t *testing.T) { + home := withHome(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".databrickscfg"), []byte("[broken\n"), 0o600)) + saveState(t, + mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand}, + mcp.Entry{Name: "github-dogfood", Service: "system.ai.github", Profile: "dogfood", Host: "https://adb-2.test", Command: testDatabricksCommand}, + ) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + var warnings []string + for _, w := range out.Warnings { + if strings.Contains(w, "failed to read Databricks profiles") { + warnings = append(warnings, w) + } + } + require.Len(t, warnings, 1, "one file, one warning: %v", out.Warnings) + assert.NotContains(t, warnings[0], "genie", "the warning is for the file, not for a server") + for _, p := range out.Profiles { + assert.Equal(t, statusUnknown, p.Status, "every profile of the file is unknown") + } +} + +// TestBuildMcpListWarnsForOwnershipErrorWithAnotherCause checks that an ownership error is warned about when the entry +// check failed for another reason. Here the profile file cannot be read, and the agent's entry pins a file whose name +// cannot be resolved: two causes, so two warnings. +func TestBuildMcpListWarnsForOwnershipErrorWithAnotherCause(t *testing.T) { + home := withHome(t) + require.NoError(t, os.WriteFile(filepath.Join(home, ".databrickscfg"), []byte("[broken\n"), 0o600)) + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "github-logfood", "system.ai.github", "logfood", "https://adb-1.test") + saveState(t, entry) + claudePath := filepath.Join(home, ".claude.json") + editEntry(t, claudePath, entry.Name, func(server map[string]any) { + server["env"] = map[string]any{mcpconfig.ConfigFileEnv: "unresolvable\x00.cfg"} + }) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + var ownership []string + for _, w := range out.Warnings { + if strings.Contains(w, "MCP server github-logfood: could not check Claude Code") { + ownership = append(ownership, w) + } + } + require.Len(t, ownership, 1, "the agent's check has its own cause: %v", out.Warnings) + assert.Equal(t, statusUnknown, out.Profiles[0].Servers[0].Agents[0].Status) +} + +// TestBuildMcpListRecordForAgentWithoutMCPIsUnknown checks a record for a registry agent that takes no MCP servers. It +// is not a config that failed to read: the warning says the agent takes no servers, and remove drops the record. +func TestBuildMcpListRecordForAgentWithoutMCPIsUnknown(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + pi := agents.ByName("pi") + require.NotNil(t, pi) + require.False(t, pi.SupportsMCP()) + saveState(t, mcp.Entry{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand, + Agents: map[string]mcp.AgentRecord{pi.Name: {Path: "/x/pi.json", Scope: "user"}}, + }) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + assert.Equal(t, []string{"MCP server github-logfood: Pi does not take MCP servers; remove drops this record"}, out.Warnings) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, 1) + assert.Equal(t, statusUnknown, out.Profiles[0].Servers[0].Agents[0].Status) +} + +// TestBuildMcpListAndRemoveAgreeAfterConfigMoved checks that list reports the entry where remove acts on it, when the +// agent's config directory moved. The entry is either in the file the record names, which the agent no longer reads, or +// in the file the agent reads now, with the record naming the old file. +func TestBuildMcpListAndRemoveAgreeAfterConfigMoved(t *testing.T) { + cases := []struct { + name string + // entryIn is the file that holds the entry: "recorded" or "current". + entryIn string + status string + // holds is the file that keeps the entry after remove: "" when no file keeps it. + removedFrom string + }{ + {name: "entry in the recorded file", entryIn: "recorded", status: agentStatusPathChanged, removedFrom: "recorded"}, + {name: "entry in the file the agent reads now", entryIn: "current", status: agentStatusInstalled, removedFrom: "current"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + oldDir := t.TempDir() + t.Setenv("CLAUDE_CONFIG_DIR", oldDir) + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + oldPath := entry.Agents[claude.Name].Path + saveState(t, entry) + + newDir := t.TempDir() + newPath := filepath.Join(newDir, ".claude.json") + t.Setenv("CLAUDE_CONFIG_DIR", newDir) + if tc.entryIn == "current" { + // The user moved the config with the directory: the entry is in the new file and the old one is gone. + data, err := os.ReadFile(oldPath) + require.NoError(t, err) + require.NoError(t, os.WriteFile(newPath, data, 0o600)) + require.NoError(t, os.Remove(oldPath)) + } else { + require.NoError(t, os.WriteFile(newPath, []byte("{}"), 0o600)) + } + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + require.Len(t, out.Profiles, 1) + listed := out.Profiles[0].Servers[0].Agents[0] + assert.Equal(t, tc.status, listed.Status) + wantPath := filepath.ToSlash(oldPath) + if tc.removedFrom == "current" { + wantPath = filepath.ToSlash(newPath) + } + assert.Equal(t, wantPath, listed.Path, "list names the file remove acts on") + + stdout, _, err := runMcpCmd(t, "remove", "--names", entry.Name, "--output", "json") + require.NoError(t, err) + removed := readRemoveOutput(t, stdout.String()) + require.Len(t, removed.Removed, 1, "remove takes the entry from the file list named") + assert.Empty(t, removed.Skipped) + + acted := oldPath + if tc.removedFrom == "current" { + acted = newPath + } + if data, err := os.ReadFile(acted); err == nil { + assert.NotContains(t, string(data), entry.Name, "the entry is gone from %s", acted) + } + }) + } +} diff --git a/cmd/aitools/mcp_remove.go b/cmd/aitools/mcp_remove.go new file mode 100644 index 00000000000..eba147d5ffe --- /dev/null +++ b/cmd/aitools/mcp_remove.go @@ -0,0 +1,421 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "maps" + "slices" + "strings" + + "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/telemetry/protos" + "github.com/spf13/cobra" +) + +// removeFlagsHint is the error for a run that names no servers. Tests check it too. +const removeFlagsHint = "pass --names, --profile or --all; interactive selection is not available yet" + +type mcpRemoveFlags struct { + names string + agents string + all bool +} + +// removeOutput is the `mcp remove --output json` document. +type removeOutput struct { + Removed []removeRemoved `json:"removed"` + Skipped []removeSkipped `json:"skipped"` + Error string `json:"error,omitempty"` + ErrorCategory protos.AitoolsErrorCategory `json:"error_category,omitempty"` +} + +type removeRemoved struct { + Name string `json:"name"` + Service string `json:"service"` + Profile string `json:"profile"` + Agents []string `json:"agents"` + // RecordOnly names the agents whose record was dropped without an edit, because this CLI cannot edit their config. + RecordOnly []string `json:"record_only,omitempty"` +} + +// removeSkipped is one server that was left in one agent: the agent is named, since a server can be removed from +// some agents and skipped in others. +type removeSkipped struct { + Name string `json:"name"` + Agent string `json:"agent"` + Reason string `json:"reason"` +} + +// removeOutcome is what happened to one server in one agent config. +type removeOutcome int + +const ( + // removeDeleted means the entry was removed from the agent config. + removeDeleted removeOutcome = iota + // removePruned means the entry was already gone from the agent config, so only the state record goes. + removePruned + // removeKept means the entry is left in place, because it is not the one this CLI wrote. + removeKept +) + +// saveMCPState writes the MCP state. It is a variable so that a test can make the save fail. +var saveMCPState = mcp.SaveState + +// recordDrop names one agent record to delete from state: the server's name and the agent. +type recordDrop struct { + name string + agent string +} + +// NewMcpRemoveCmd returns `aitools mcp remove`. It needs no network. +func NewMcpRemoveCmd() *cobra.Command { + var f mcpRemoveFlags + cmd := &cobra.Command{ + Use: "remove", + Short: "Remove Databricks managed MCP servers from coding agents", + Long: `Remove Databricks managed MCP servers that this CLI installed. + +Choose servers with --names (server names as listed by 'aitools mcp list', or service names; the +last part of a service name, such as github, is enough when only one schema has it), --profile, or +--all. --profile narrows --all or --names to one profile, and --agents narrows them to some agents. +Interactive selection is not available yet, so one of --names, --profile or --all is required. + +Only entries this CLI wrote are removed. An entry that was changed outside this CLI is left in place +and reported. When an agent's config moved, the entry is removed from the file it was written to. +Entries that run 'aitools mcp serve' without an install record are not chosen by any flag; +'aitools mcp list' shows them as unmanaged. Provider credentials are not deleted.`, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + jsonMode := installOutputIsJSON(cmd) + + out := &removeOutput{Removed: []removeRemoved{}, Skipped: []removeSkipped{}} + runErr := runMcpRemove(ctx, cmd, f, jsonMode, out) + if !jsonMode { + return runErr + } + + // The document carries a top-level failure; per-agent failures are already in skipped. + if top := topLevelFailure(runErr); top != nil { + out.Error = top.Error() + out.ErrorCategory = errorCategory(top) + } + 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", "", "Servers to remove: server names or service names, comma-separated") + cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to remove from, comma-separated (default: every agent the server is installed for)") + cmd.Flags().BoolVar(&f.all, "all", false, "Remove every server this CLI installed") + return cmd +} + +// runMcpRemove removes the chosen servers from the agent configs and records the result. Results are printed +// before a state failure is returned. An agent failure fails the command when --agents named the agent, or when a +// selected server was removed from no agent; otherwise it is reported in skipped. +func runMcpRemove(ctx context.Context, cmd *cobra.Command, f mcpRemoveFlags, jsonMode bool, out *removeOutput) error { + names := splitAndTrim(f.names) + agentNames := splitAndTrim(f.agents) + profileName := profileFlag(cmd) + if err := validateMCPRemoveFlags(f.all, names, profileName); err != nil { + return err + } + + // A nil targets means every agent a server is installed for. + var targets []*agents.Agent + if len(agentNames) > 0 { + var err error + targets, err = resolveMCPAgents(agentNames) + if err != nil { + return err + } + } + + state, err := mcp.LoadState(ctx) + if err != nil { + return err + } + selected, err := selectInstalls(state, names, profileName) + if err != nil { + return err + } + if err := checkTargetsInstalled(selected, targets); err != nil { + return err + } + if targets != nil { + // --agents chooses the servers installed for those agents, so a server with no agent left is not chosen. + selected = slices.DeleteFunc(selected, func(e mcp.Entry) bool { return len(e.Agents) == 0 }) + } + + // Each agent config file is read once per command, and each server's config file is resolved once. + files := newAgentFiles() + var drops []recordDrop + var agentErrs []error + for _, e := range selected { + removed := removeRemoved{Name: e.Name, Service: e.Service, Profile: e.Profile, Agents: []string{}} + pin := resolveBinding(ctx, e) + var errs []error + for _, agentName := range slices.Sorted(maps.Keys(e.Agents)) { + if targets != nil && !slices.ContainsFunc(targets, func(a *agents.Agent) bool { return a.Name == agentName }) { + continue + } + a := agents.ByName(agentName) + if a == nil || !a.SupportsMCP() { + // There is no config this CLI can edit for the agent, so only the record can go. + removed.Agents = append(removed.Agents, agentName) + removed.RecordOnly = append(removed.RecordOnly, agentName) + drops = append(drops, recordDrop{name: e.Name, agent: agentName}) + continue + } + outcome, reason, err := removeFromAgent(ctx, files, e, a, pin) + if err != nil { + errs = append(errs, err) + out.Skipped = append(out.Skipped, removeSkipped{Name: e.Name, Agent: agentName, Reason: fmt.Sprintf("could not update %s: %s", agentDisplayName(agentName), err)}) + continue + } + if outcome == removeKept { + out.Skipped = append(out.Skipped, removeSkipped{Name: e.Name, Agent: agentName, Reason: reason}) + continue + } + removed.Agents = append(removed.Agents, agentName) + drops = append(drops, recordDrop{name: e.Name, agent: agentName}) + } + // A server with no agent left in state is removed from state too, so it is reported as removed. + if len(removed.Agents) > 0 || len(e.Agents) == 0 { + out.Removed = append(out.Removed, removed) + } + // A server that no agent gave up on is a failure. So is any agent failure when --agents named the agent. + if len(errs) > 0 && (len(removed.Agents) == 0 || targets != nil) { + for _, err := range errs { + agentErrs = appendDistinct(agentErrs, err) + } + } + } + + var saveErr error + if len(drops) > 0 || slices.ContainsFunc(selected, func(e mcp.Entry) bool { return len(e.Agents) == 0 }) { + saveErr = dropRecords(ctx, drops, selected) + } + if !jsonMode { + printRemoveResults(ctx, out) + } + if saveErr != nil { + return saveErr + } + if len(agentErrs) > 0 { + return &agentErrors{err: errors.Join(agentErrs...)} + } + return nil +} + +// validateMCPRemoveFlags rejects a choice of servers that cannot run, before anything is read or written. +func validateMCPRemoveFlags(all bool, names []string, profileName string) error { + if all && len(names) > 0 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--all cannot be combined with --names") + } + if !all && len(names) == 0 && profileName == "" { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, removeFlagsHint) + } + return nil +} + +// selectInstalls returns the state entries that names pick, in state order, or every entry in scope when names is +// empty. profileName, when set, narrows the scope to that profile. A name that matches nothing, and a profile with no +// installed server, are errors, so a typo does not look like a successful removal. +func selectInstalls(state *mcp.State, names []string, profileName string) ([]mcp.Entry, error) { + var scope []mcp.Entry + for _, e := range state.Servers { + if profileName == "" || e.Profile == profileName { + scope = append(scope, e) + } + } + where := "" + if profileName != "" { + where = " on profile " + profileName + } + chosen := make([]bool, len(scope)) + for _, name := range names { + matched, err := matchInstalls(scope, name) + if err != nil { + return nil, err + } + if len(matched) == 0 { + return nil, failure(categoryServiceNotFound, nil, "no installed MCP server named %s%s", name, where) + } + for _, i := range matched { + chosen[i] = true + } + } + if profileName != "" && len(scope) == 0 { + return nil, failure(categoryServiceNotFound, nil, "no installed MCP servers on profile %s", profileName) + } + if len(names) == 0 { + return scope, nil + } + var selected []mcp.Entry + for i, e := range scope { + if chosen[i] { + selected = append(selected, e) + } + } + return selected, nil +} + +// matchInstalls returns the indexes of the entries of scope that name means. A server name selects that server: an +// exact match first, and otherwise a match without case, which is refused when several servers match that way, since +// state can hold names that differ only in case. Only when no server has the name, it means the service: the full +// service name, or the leaf of a service name (its last part, when the name has no dot). A leaf is refused when the +// entries it matches are of more than one service, since then it does not say which one is meant. +func matchInstalls(scope []mcp.Entry, name string) ([]int, error) { + if i := slices.IndexFunc(scope, func(e mcp.Entry) bool { return e.Name == name }); i >= 0 { + return []int{i}, nil + } + var byName []int + var serverNames []string + for i, e := range scope { + if strings.EqualFold(e.Name, name) { + byName = append(byName, i) + serverNames = append(serverNames, e.Name) + } + } + if len(byName) > 1 { + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s matches more than one MCP server (%s); pass the exact server name", + name, strings.Join(serverNames, ", ")) + } + if len(byName) == 1 { + return byName, nil + } + var matched []int + var services []string + for i, e := range scope { + if !matchesService(e, name) { + continue + } + matched = append(matched, i) + if !slices.ContainsFunc(services, func(s string) bool { return strings.EqualFold(s, e.Service) }) { + services = append(services, e.Service) + } + } + if len(services) > 1 && !strings.Contains(name, ".") { + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "%s matches more than one MCP service (%s); pass the full service name or a server name", + name, strings.Join(services, ", ")) + } + return matched, nil +} + +// matchesService reports whether a --names value means the entry's service: its full name, or the leaf of its service +// name when the value has no dot. Names compare without case. +func matchesService(e mcp.Entry, name string) bool { + if strings.EqualFold(e.Service, name) { + return true + } + return !strings.Contains(name, ".") && strings.EqualFold(mcp.LeafOf(e.Service), name) +} + +// checkTargetsInstalled refuses an --agents agent that none of the selected servers is installed for. With no +// selected server there is nothing to check, so removing everything from an empty state stays a no-op. +func checkTargetsInstalled(selected []mcp.Entry, targets []*agents.Agent) error { + if len(selected) == 0 { + return nil + } + for _, a := range targets { + if !slices.ContainsFunc(selected, func(e mcp.Entry) bool { + _, ok := e.Agents[a.Name] + return ok + }) { + return failure(categoryServiceNotFound, nil, "no selected MCP server is installed for %s", agentDisplayName(a.Name)) + } + } + return nil +} + +// keptReason says why an entry is left in place, and how to clear it. +func keptReason(e mcp.Entry, a *agents.Agent, path string, entry map[string]any) string { + return fmt.Sprintf("%s has an entry named %s that was changed outside this CLI; left in place. To remove it, %s", + agentDisplayName(a.Name), e.Name, changedEntryFix(e.Name, path, entry)) +} + +// removeFromAgent removes the server's entry for agent a, but only when the entry there is the one this CLI wrote. The +// entry is looked for where locateEntry finds it. An agent config that holds no entry is pruned: the record goes without +// an edit. +func removeFromAgent(ctx context.Context, files *agentFiles, e mcp.Entry, a *agents.Agent, pin bindingFile) (removeOutcome, string, error) { + loc, err := files.locateEntry(ctx, e, a) + if err != nil { + return 0, "", err + } + if !loc.found { + return removePruned, "", nil + } + owned, err := pin.owns(ctx, e, loc.entry) + if err != nil { + return 0, "", err + } + if !owned { + return removeKept, keptReason(e, a, loc.path, loc.entry), nil + } + if _, err := agentconfig.RemoveAt(a, loc.path, e.Name); err != nil { + return 0, "", err + } + return removeDeleted, "", nil +} + +// dropRecords deletes the agent records in drops from state, then the entries that were selected or dropped and have +// no agent left. Each record is matched by the name of its server, which is unique in state. The state file is re-read +// first, so entries written since the selection are kept, and an entry that was not selected is never removed here. +func dropRecords(ctx context.Context, drops []recordDrop, selected []mcp.Entry) error { + state, err := mcp.LoadState(ctx) + if err != nil { + return err + } + for _, d := range drops { + i := slices.IndexFunc(state.Servers, func(e mcp.Entry) bool { + return e.Name == d.name + }) + if i >= 0 { + delete(state.Servers[i].Agents, d.agent) + } + } + touched := map[string]bool{} + for _, d := range drops { + touched[d.name] = true + } + for _, e := range selected { + touched[e.Name] = true + } + state.Servers = slices.DeleteFunc(state.Servers, func(e mcp.Entry) bool { + return touched[e.Name] && len(e.Agents) == 0 + }) + return saveMCPState(ctx, state) +} + +// printRemoveResults writes the text result: each removed server, then each skipped entry with its reason. +func printRemoveResults(ctx context.Context, out *removeOutput) { + if len(out.Removed) == 0 && len(out.Skipped) == 0 { + cmdio.LogString(ctx, "No Databricks MCP servers to remove.") + return + } + for _, r := range out.Removed { + line := fmt.Sprintf("Removed %s %s profile %s", r.Name, r.Service, r.Profile) + if len(r.Agents) > 0 { + line += fmt.Sprintf(" (%s)", mcpAgentList(r.Agents)) + } + cmdio.LogString(ctx, line) + for _, name := range r.RecordOnly { + cmdio.LogString(ctx, fmt.Sprintf(" only the record of %s was dropped: this CLI has no MCP config to edit for it", agentDisplayName(name))) + } + } + for _, s := range out.Skipped { + cmdio.LogString(ctx, fmt.Sprintf("Skipped %s: %s", s.Name, s.Reason)) + } +} diff --git a/cmd/aitools/mcp_remove_test.go b/cmd/aitools/mcp_remove_test.go new file mode 100644 index 00000000000..4e537b0ecf3 --- /dev/null +++ b/cmd/aitools/mcp_remove_test.go @@ -0,0 +1,649 @@ +package aitools + +import ( + "context" + "encoding/json" + "errors" + "maps" + "os" + "path/filepath" + "slices" + "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/telemetry/protos" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestValidateMCPRemoveFlags(t *testing.T) { + names := []string{"github-logfood"} + cases := []struct { + name string + all bool + names []string + profile string + want string + }{ + {"names", false, names, "", ""}, + {"profile", false, nil, "logfood", ""}, + {"all", true, nil, "", ""}, + {"all with profile", true, nil, "logfood", ""}, + {"nothing", false, nil, "", removeFlagsHint}, + {"all with names", true, names, "", "--all cannot be combined with --names"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := validateMCPRemoveFlags(tc.all, tc.names, tc.profile) + if tc.want == "" { + assert.NoError(t, err) + return + } + require.Error(t, err) + assert.Equal(t, tc.want, err.Error()) + }) + } + + err := validateMCPRemoveFlags(false, nil, "") + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) +} + +func TestSelectInstalls(t *testing.T) { + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "github-logfood", Service: "system.ai.github", Profile: "logfood"}, + {Name: "github-dogfood", Service: "system.ai.github", Profile: "dogfood"}, + {Name: "genie_one_mcp-logfood", Service: "system.ai.genie_one_mcp", Profile: "logfood"}, + }} + + all, err := selectInstalls(state, nil, "") + require.NoError(t, err) + assert.Len(t, all, 3) + + byProfile, err := selectInstalls(state, nil, "logfood") + require.NoError(t, err) + require.Len(t, byProfile, 2) + assert.Equal(t, "github-logfood", byProfile[0].Name) + + // A service name matches every profile of that service, unless narrowed. + byService, err := selectInstalls(state, []string{"SYSTEM.AI.GITHUB"}, "") + require.NoError(t, err) + assert.Len(t, byService, 2) + + byName, err := selectInstalls(state, []string{"github-dogfood"}, "") + require.NoError(t, err) + require.Len(t, byName, 1) + assert.Equal(t, "dogfood", byName[0].Profile) + + _, err = selectInstalls(state, []string{"github-dogfood"}, "logfood") + require.Error(t, err) + assert.Equal(t, "no installed MCP server named github-dogfood on profile logfood", err.Error()) + assert.Equal(t, categoryServiceNotFound, errorCategory(err)) + + // A profile with no installed server is a typo or an empty profile, not an empty removal. + _, err = selectInstalls(state, nil, "typo") + require.Error(t, err) + assert.Equal(t, "no installed MCP servers on profile typo", err.Error()) + assert.Equal(t, categoryServiceNotFound, errorCategory(err)) + + none, err := selectInstalls(&mcp.State{}, nil, "") + require.NoError(t, err, "--all on an empty state is a no-op") + assert.Empty(t, none) +} + +func TestSelectInstallsLeafAndCaseRules(t *testing.T) { + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "github-logfood", Service: "system.ai.github", Profile: "logfood"}, + {Name: "github-dogfood", Service: "system.ai.github", Profile: "dogfood"}, + {Name: "github-team", Service: "team.ai.github", Profile: "logfood"}, + }} + + // Server names compare without case. + byName, err := selectInstalls(state, []string{"GITHUB-LOGFOOD"}, "") + require.NoError(t, err) + require.Len(t, byName, 1) + + // A leaf is the last part of the service name. Here it names services in two schemas, so it is refused. + _, err = selectInstalls(state, []string{"github"}, "") + require.Error(t, err) + assert.Equal(t, "github matches more than one MCP service (system.ai.github, team.ai.github); pass the full service name or a server name", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + + // Narrowed to a profile where only one schema is installed, the leaf is unambiguous. + byLeaf, err := selectInstalls(state, []string{"github"}, "dogfood") + require.NoError(t, err) + require.Len(t, byLeaf, 1) + assert.Equal(t, "dogfood", byLeaf[0].Profile) + + // A name with a dot is a full service name, not a leaf. + full, err := selectInstalls(state, []string{"team.ai.github"}, "") + require.NoError(t, err) + require.Len(t, full, 1) +} + +func readJSONFile(t *testing.T, path string) map[string]any { + t.Helper() + data, err := os.ReadFile(path) + require.NoError(t, err) + var doc map[string]any + require.NoError(t, json.Unmarshal(data, &doc)) + return doc +} + +// readRemoveOutput decodes the JSON document of `mcp remove`. +func readRemoveOutput(t *testing.T, stdout string) removeOutput { + t.Helper() + var out removeOutput + require.NoError(t, json.Unmarshal([]byte(stdout), &out), stdout) + return out +} + +func TestMcpRemoveDeletesOwnedEntriesAndPrunesMissing(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + require.NoError(t, os.WriteFile(claudePath, []byte(`{"numStartups": 3, "mcpServers": {"web_search": {"command": "ug", "args": ["mcp-proxy"]}}}`), 0o600)) + + genie := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + // Recorded but never written: remove prunes it without touching the file. + absent := mcp.Entry{ + Name: "dbsql-logfood", Service: "system.ai.dbsql", Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand, + Agents: map[string]mcp.AgentRecord{"claude-code": {Path: claudePath, Scope: "user"}}, + } + other := seedInstall(t, claude, "github-dogfood", "system.ai.github", "dogfood", "https://adb-2.test") + saveState(t, genie, absent, other) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood,dbsql-logfood", "--output", "json") + require.NoError(t, err) + + out := readRemoveOutput(t, stdout.String()) + assert.Equal(t, []removeRemoved{ + {Name: "genie_one_mcp-logfood", Service: "system.ai.genie_one_mcp", Profile: "logfood", Agents: []string{"claude-code"}}, + {Name: "dbsql-logfood", Service: "system.ai.dbsql", Profile: "logfood", Agents: []string{"claude-code"}}, + }, out.Removed) + assert.Empty(t, out.Skipped) + + doc := readJSONFile(t, claudePath) + assert.EqualValues(t, 3, doc["numStartups"], "keys outside mcpServers are kept") + servers := doc["mcpServers"].(map[string]any) + assert.Contains(t, servers, "web_search", "entries we did not write are kept") + assert.Contains(t, servers, "github-dogfood", "other servers are kept") + assert.NotContains(t, servers, "genie_one_mcp-logfood") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Equal(t, "github-dogfood", state.Servers[0].Name) +} + +func TestMcpRemoveSkipsEntryChangedOutsideCLI(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + // Someone points the same name at another command. It is no longer ours. + repointEntry(t, claudePath, "genie_one_mcp-logfood", "/usr/bin/other") + before, err := os.ReadFile(claudePath) + require.NoError(t, err) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--output", "json") + require.NoError(t, err) + + out := readRemoveOutput(t, stdout.String()) + assert.Empty(t, out.Removed) + require.Len(t, out.Skipped, 1) + assert.Equal(t, "genie_one_mcp-logfood", out.Skipped[0].Name) + assert.Equal(t, "claude-code", out.Skipped[0].Agent) + assert.Equal(t, "Claude Code has an entry named genie_one_mcp-logfood that was changed outside this CLI; left in place. "+ + "To remove it, delete the entry from "+filepath.ToSlash(claudePath)+" by hand and run `databricks aitools mcp remove --names genie_one_mcp-logfood` again, "+ + "or add the server again with --force", out.Skipped[0].Reason) + + after, err := os.ReadFile(claudePath) + require.NoError(t, err) + assert.Equal(t, before, after, "the agent config is not written") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Contains(t, state.Servers[0].Agents, "claude-code", "the record stays, so list keeps showing the entry") +} + +// TestMcpRemoveRemovesEntryFromRecordedFileWhenAgentConfigMoved checks that an agent whose config moved loses its entry +// from the file the record names, the file this CLI wrote, and that the record goes with it. +func TestMcpRemoveRemovesEntryFromRecordedFileWhenAgentConfigMoved(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + + // The agent now reads its config from another directory. + moved := t.TempDir() + t.Setenv("CLAUDE_CONFIG_DIR", moved) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--output", "json") + require.NoError(t, err) + + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Equal(t, []string{"claude-code"}, out.Removed[0].Agents) + assert.Empty(t, out.Skipped) + assert.NotContains(t, readJSONFile(t, claudePath)["mcpServers"], "genie_one_mcp-logfood") + _, err = os.Stat(filepath.Join(moved, ".claude.json")) + assert.ErrorIs(t, err, os.ErrNotExist, "the agent's current file is not written") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Empty(t, state.Servers, "the record went with the entry") +} + +// TestMcpRemovePrunesWhenRecordedFileIsGone checks that a record whose file no longer exists is pruned, even when the +// agent's config moved to a directory that is not the recorded one. +func TestMcpRemovePrunesWhenRecordedFileIsGone(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + require.NoError(t, os.Remove(claudePath)) + t.Setenv("CLAUDE_CONFIG_DIR", t.TempDir()) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--output", "json") + require.NoError(t, err) + + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Equal(t, []string{"claude-code"}, out.Removed[0].Agents) + assert.Empty(t, out.Skipped) + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Empty(t, state.Servers) +} + +// TestMcpRemoveDropsRecordOfAgentThatCannotBeEdited checks that a record for an agent this CLI cannot edit, one +// outside the registry or one without MCP support, is dropped and reported, not left to fail every remove --all. +func TestMcpRemoveDropsRecordOfAgentThatCannotBeEdited(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + genie := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + retired := mcp.Entry{ + Name: "dbsql-logfood", Service: "system.ai.dbsql", Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand, + Agents: map[string]mcp.AgentRecord{"retired-agent": {Path: "/x/config.json", Scope: "user"}, "pi": {Path: "/x/pi.json", Scope: "user"}}, + } + saveState(t, genie, retired) + + stdout, _, err := runMcpCmd(t, "remove", "--all", "--output", "json") + require.NoError(t, err, "a record that cannot be edited is not a failure") + + out := readRemoveOutput(t, stdout.String()) + assert.Empty(t, out.Error) + assert.Empty(t, out.Skipped) + require.Len(t, out.Removed, 2) + assert.Equal(t, []string{"pi", "retired-agent"}, out.Removed[1].Agents) + assert.Equal(t, []string{"pi", "retired-agent"}, out.Removed[1].RecordOnly, "only the record of these agents was dropped") + assert.Equal(t, []string{"claude-code"}, out.Removed[0].Agents) + assert.Empty(t, out.Removed[0].RecordOnly) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Empty(t, state.Servers) +} + +// TestMcpRemoveDropsNamedEntryWithoutAgents checks that a named entry left with no agents is removed from state. +func TestMcpRemoveDropsNamedEntryWithoutAgents(t *testing.T) { + withHome(t) + saveState(t, mcp.Entry{Name: "dangling", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand}) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "dangling", "--output", "json") + require.NoError(t, err) + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Equal(t, "dangling", out.Removed[0].Name) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Empty(t, state.Servers) +} + +// TestMcpRemoveKeepsUnselectedEntryWithoutAgents checks that removing one server does not drop another entry that +// has no agents: only the entries this run selected or emptied are dropped. +func TestMcpRemoveKeepsUnselectedEntryWithoutAgents(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + genie := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, genie, mcp.Entry{Name: "keep-me", Service: "system.ai.dbsql", Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand}) + + _, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood") + require.NoError(t, err) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Equal(t, "keep-me", state.Servers[0].Name) +} + +// TestMcpRemoveNamesAcceptsLeafService checks that --names takes the leaf of a service name, as add does. +func TestMcpRemoveNamesAcceptsLeafService(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + github := seedInstall(t, claude, "github-logfood", "system.ai.github", "logfood", "https://adb-1.test") + saveState(t, github) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "github", "--output", "json") + require.NoError(t, err) + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Equal(t, "github-logfood", out.Removed[0].Name) +} + +func TestMcpRemoveExplicitAgentFailureIsAnError(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + codex := agents.ByName("codex") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + codexPath := filepath.Join(home, ".codex", "config.toml") + require.NoError(t, os.MkdirAll(filepath.Dir(codexPath), 0o755)) + require.NoError(t, os.WriteFile(codexPath, []byte("[mcp_servers.broken\n"), 0o600)) + entry.Agents[codex.Name] = mcp.AgentRecord{Path: codexPath, Scope: "user"} + saveState(t, entry) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--agents", "claude-code,codex", "--output", "json") + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Equal(t, []string{"claude-code"}, out.Removed[0].Agents) + require.Len(t, out.Skipped, 1) + assert.Equal(t, "codex", out.Skipped[0].Agent) + assert.Contains(t, out.Skipped[0].Reason, "could not update Codex CLI") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Equal(t, []string{"codex"}, slices.Sorted(maps.Keys(state.Servers[0].Agents))) +} + +func TestMcpRemoveFailsWhenNoAgentWasRemoved(t *testing.T) { + home := withHome(t) + codex := agents.ByName("codex") + codexPath := filepath.Join(home, ".codex", "config.toml") + require.NoError(t, os.MkdirAll(filepath.Dir(codexPath), 0o755)) + require.NoError(t, os.WriteFile(codexPath, []byte("[mcp_servers.broken\n"), 0o600)) + saveState(t, mcp.Entry{ + Name: "genie_one_mcp-logfood", Service: "system.ai.genie_one_mcp", Profile: "logfood", Host: "https://adb-1.test", + Command: testDatabricksCommand, Agents: map[string]mcp.AgentRecord{codex.Name: {Path: codexPath, Scope: "user"}}, + }) + + // No --agents: the failure is only a warning when another agent was removed. Here none was. + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--output", "json") + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + + out := readRemoveOutput(t, stdout.String()) + assert.Empty(t, out.Removed) + require.Len(t, out.Skipped, 1) + assert.Equal(t, "codex", out.Skipped[0].Agent) +} + +func TestMcpRemoveRefusesProfileAndAgentWithNothingInstalled(t *testing.T) { + withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + + stdout, _, err := runMcpCmd(t, "remove", "--profile", "typo", "--output", "json") + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + out := readRemoveOutput(t, stdout.String()) + assert.Equal(t, "no installed MCP servers on profile typo", out.Error) + assert.Equal(t, categoryServiceNotFound, out.ErrorCategory) + + stdout, _, err = runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--agents", "cursor", "--output", "json") + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + out = readRemoveOutput(t, stdout.String()) + assert.Equal(t, "no selected MCP server is installed for Cursor", out.Error) + assert.Equal(t, categoryServiceNotFound, out.ErrorCategory) + + // Nothing was removed by either refusal. + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Contains(t, state.Servers[0].Agents, "claude-code") +} + +func TestMcpRemoveReportsResultsWhenStateCannotBeSaved(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + + // The agent entry is deleted first; then the state cannot be written. + saveMCPState = func(context.Context, *mcp.State) error { return errors.New("state file is read-only") } + t.Cleanup(func() { saveMCPState = mcp.SaveState }) + + stdout, stderr, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood") + require.ErrorContains(t, err, "state file is read-only") + assert.Empty(t, stdout.String()) + assert.Contains(t, stderr.String(), "Removed genie_one_mcp-logfood", "the results are printed before the error") + assert.NotContains(t, readJSONFile(t, claudePath)["mcpServers"], "genie_one_mcp-logfood") +} + +func TestMcpRemoveDropsRecordOfNamedEntry(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + dir := t.TempDir() + first := filepath.Join(dir, "first.cfg") + second := filepath.Join(dir, "second.cfg") + for _, path := range []string{first, second} { + require.NoError(t, os.WriteFile(path, []byte("[logfood]\nhost = https://adb-1.test\ntoken = t\n"), 0o600)) + } + + // Two servers of one service and profile, each pinned to its own config file. + kept := seedInstallEntry(t, claude, seed{"genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test", testDatabricksCommand, first}) + removed := seedInstallEntry(t, claude, seed{"genie_one_mcp-logfood-2", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test", testDatabricksCommand, second}) + saveState(t, kept, removed) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood-2", "--output", "json") + require.NoError(t, err) + assert.Len(t, readRemoveOutput(t, stdout.String()).Removed, 1) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Equal(t, "genie_one_mcp-logfood", state.Servers[0].Name) + assert.Contains(t, state.Servers[0].Agents, "claude-code", "the record of the other server is kept") +} + +func TestMcpRemoveLeavesEntriesWithoutRecord(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + // Written by this CLI, but the state that recorded it is gone. Remove does not know it, so it stays. + seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + + stdout, _, err := runMcpCmd(t, "remove", "--all", "--output", "json") + require.NoError(t, err) + out := readRemoveOutput(t, stdout.String()) + assert.Empty(t, out.Removed) + assert.Empty(t, out.Skipped) + assert.Contains(t, readJSONFile(t, claudePath)["mcpServers"], "genie_one_mcp-logfood") +} + +func TestMcpRemoveFailsWithoutSelection(t *testing.T) { + withHome(t) + stdout, _, err := runMcpCmd(t, "remove", "--output", "json") + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + + out := readRemoveOutput(t, stdout.String()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, out.ErrorCategory) + assert.Equal(t, removeFlagsHint, out.Error) + assert.Empty(t, out.Removed) + assert.Empty(t, out.Skipped) +} + +// TestSelectInstallsServerNameWins checks that a --names value equal to a server name selects that server only: it is +// not also a service or a leaf of other servers, and it is not refused as ambiguous when another schema has the leaf. +func TestSelectInstallsServerNameWins(t *testing.T) { + cases := []struct { + name string + state []mcp.Entry + want []string + }{ + { + name: "server name does not also remove the other profile's server of the same service", + state: []mcp.Entry{ + {Name: "github", Service: "system.ai.github", Profile: "logfood"}, + {Name: "github-dogfood", Service: "system.ai.github", Profile: "dogfood"}, + }, + want: []string{"github"}, + }, + { + name: "server name is not refused as ambiguous when another schema has the leaf", + state: []mcp.Entry{ + {Name: "github", Service: "system.ai.github", Profile: "logfood"}, + {Name: "gh-team", Service: "team.ai.github", Profile: "logfood"}, + }, + want: []string{"github"}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + selected, err := selectInstalls(&mcp.State{Servers: tc.state}, []string{"github"}, "") + require.NoError(t, err) + var names []string + for _, e := range selected { + names = append(names, e.Name) + } + assert.Equal(t, tc.want, names) + }) + } +} + +// TestMcpRemoveFromAnotherDirectoryFindsConfigSetRelative checks that a relative config dir given at add time is +// recorded as an absolute path, so that remove run from another directory still edits the file the entry is in. +func TestMcpRemoveFromAnotherDirectoryFindsConfigSetRelative(t *testing.T) { + withHome(t) + work := t.TempDir() + t.Chdir(work) + require.NoError(t, os.MkdirAll(filepath.Join(work, "cfg"), 0o755)) + t.Setenv("CLAUDE_CONFIG_DIR", "cfg") + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + claudePath := filepath.Join(work, "cfg", ".claude.json") + require.Contains(t, readJSONFile(t, claudePath)["mcpServers"], "genie_one_mcp-logfood") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.True(t, filepath.IsAbs(state.Servers[0].Agents["claude-code"].Path), "recorded path: %s", state.Servers[0].Agents["claude-code"].Path) + + t.Chdir(t.TempDir()) + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--output", "json") + require.NoError(t, err) + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Empty(t, out.Skipped) + assert.NotContains(t, readJSONFile(t, claudePath)["mcpServers"], "genie_one_mcp-logfood", "the entry is removed from the file it was written to") +} + +// TestMcpRemoveTakesEntryFromFileAgentNowReads checks that when the recorded file no longer holds the entry, because the +// user moved the file into the directory the agent now reads, the entry is removed from that file and the record goes. +// It is pruned only when neither file holds it. +func TestMcpRemoveTakesEntryFromFileAgentNowReads(t *testing.T) { + home := withHome(t) + claude := agents.ByName("claude-code") + claudePath := filepath.Join(home, ".claude.json") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + + moved := t.TempDir() + require.NoError(t, os.Rename(claudePath, filepath.Join(moved, ".claude.json"))) + t.Setenv("CLAUDE_CONFIG_DIR", moved) + + stdout, _, err := runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--output", "json") + require.NoError(t, err) + + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Equal(t, []string{"claude-code"}, out.Removed[0].Agents) + assert.Empty(t, out.Skipped) + assert.NotContains(t, readJSONFile(t, filepath.Join(moved, ".claude.json"))["mcpServers"], "genie_one_mcp-logfood", "the entry is gone from the file the agent reads") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Empty(t, state.Servers, "the record went with the entry") +} + +// TestSelectInstallsExactServerNameWins checks that --names matches a server name exactly before it matches without +// case. State can hold names that differ only in case, since add adopts the name an agent entry already has. +func TestSelectInstallsExactServerNameWins(t *testing.T) { + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "Github", Service: "system.ai.github", Profile: "logfood"}, + {Name: "github", Service: "system.ai.github", Profile: "dogfood"}, + }} + + exact, err := selectInstalls(state, []string{"github"}, "") + require.NoError(t, err) + require.Len(t, exact, 1) + assert.Equal(t, "github", exact[0].Name) + + _, err = selectInstalls(state, []string{"GITHUB"}, "") + require.Error(t, err) + assert.Equal(t, "GITHUB matches more than one MCP server (Github, github); pass the exact server name", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + + // Narrowed to one profile, the case-insensitive name is unambiguous again. + one, err := selectInstalls(state, []string{"GITHUB"}, "logfood") + require.NoError(t, err) + require.Len(t, one, 1) + assert.Equal(t, "Github", one[0].Name) +} + +// TestMcpRemoveWithAgentsSkipsRecordWithoutAgents checks that --agents selects only servers installed for those agents. +// An entry with no agent left is not chosen, so it is neither reported as removed nor dropped from state. +func TestMcpRemoveWithAgentsSkipsRecordWithoutAgents(t *testing.T) { + withHome(t) + claude := agents.ByName("claude-code") + genie := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + dangling := mcp.Entry{Name: "dangling", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand} + saveState(t, genie, dangling) + + stdout, _, err := runMcpCmd(t, "remove", "--all", "--agents", "claude-code", "--output", "json") + require.NoError(t, err) + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Removed, 1) + assert.Equal(t, "genie_one_mcp-logfood", out.Removed[0].Name) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Equal(t, "dangling", state.Servers[0].Name) + + // Named explicitly, the same entry is a refusal: no selected server is installed for the agent. + _, _, err = runMcpCmd(t, "remove", "--names", "dangling", "--agents", "claude-code") + require.ErrorContains(t, err, "no selected MCP server is installed for Claude Code") +} + +// TestMcpRemoveKeptReasonForDatabricksEntryOmitsForce checks that an entry this CLI wrote, but that serves another +// binding now, is cleared by hand. add refuses such an entry whatever --force says, so the reason does not offer it. +func TestMcpRemoveKeptReasonForDatabricksEntryOmitsForce(t *testing.T) { + home := withHome(t) + writeDatabricksCfg(t, "[logfood]\nhost = https://adb-1.test\ntoken = t\n") + claude := agents.ByName("claude-code") + entry := seedInstall(t, claude, "genie_one_mcp-logfood", "system.ai.genie_one_mcp", "logfood", "https://adb-1.test") + saveState(t, entry) + claudePath := filepath.Join(home, ".claude.json") + editEntry(t, claudePath, entry.Name, func(server map[string]any) { setServeFlag(t, server, "--profile", "other") }) + + stdout, _, err := runMcpCmd(t, "remove", "--names", entry.Name, "--output", "json") + require.NoError(t, err) + out := readRemoveOutput(t, stdout.String()) + require.Len(t, out.Skipped, 1) + assert.Equal(t, "Claude Code has an entry named genie_one_mcp-logfood that was changed outside this CLI; left in place. "+ + "To remove it, delete the entry from "+filepath.ToSlash(claudePath)+" by hand and run `databricks aitools mcp remove --names genie_one_mcp-logfood` again", + out.Skipped[0].Reason) + assert.NotContains(t, out.Skipped[0].Reason, "--force") +} diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go index ed571a8032d..6625a722351 100644 --- a/cmd/aitools/mcp_test.go +++ b/cmd/aitools/mcp_test.go @@ -31,7 +31,7 @@ func TestMcpCommandIsHidden(t *testing.T) { for _, c := range mcp.Commands() { names = append(names, c.Name()) } - assert.Equal(t, []string{"add"}, names) + assert.Equal(t, []string{"add", "list", "remove"}, names) for _, c := range NewAitoolsCmd().Commands() { if c.Name() == "mcp" { diff --git a/libs/aitools/mcp/agentconfig/toml_test.go b/libs/aitools/mcp/agentconfig/toml_test.go index 96e720a244a..c2aa6157f07 100644 --- a/libs/aitools/mcp/agentconfig/toml_test.go +++ b/libs/aitools/mcp/agentconfig/toml_test.go @@ -52,7 +52,7 @@ func TestTOMLRemoveDeletesOnlyOwnedTables(t *testing.T) { writeFile(t, path, []byte(before), 0o600) a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.True(t, removed) assert.Equal(t, "# keep\nmodel = \"x\"\n\n[mcp_servers.other]\ncommand = \"npx\"\n", string(readFile(t, path))) @@ -79,7 +79,7 @@ func TestTOMLRefusesServerWithoutOwnTables(t *testing.T) { assert.ErrorContains(t, err, testName) assert.Equal(t, tc.data, string(readFile(t, path))) - _, err = Remove(t.Context(), a, testName) + _, err = removeAgent(t.Context(), a, testName) require.Error(t, err) assert.Equal(t, tc.data, string(readFile(t, path))) }) @@ -167,7 +167,7 @@ func TestTOMLEnvTablesLayout(t *testing.T) { assert.Equal(t, StatusAdded, status) got := string(readFile(t, path)) requireTidyTOML(t, got) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Equal(t, map[string]any{"DATABRICKS_CONFIG_FILE": "one"}, entries[testName]["env"]) assert.Contains(t, entries, "other") @@ -207,7 +207,7 @@ func TestTOMLEnvTablesLayout(t *testing.T) { writeFile(t, path, []byte(blockFor("test")+"\n"+envTable+"\n"+other), 0o600) a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.True(t, removed) assert.Equal(t, other, string(readFile(t, path))) @@ -218,7 +218,7 @@ func TestTOMLEnvTablesLayout(t *testing.T) { writeFile(t, path, []byte(blockFor("test")+"\n"+other), 0o600) a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.True(t, removed) assert.Equal(t, other, string(readFile(t, path))) @@ -234,7 +234,7 @@ func TestTOMLQuotedDottedNameIsAnotherServer(t *testing.T) { writeFile(t, path, []byte(before), 0o600) a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.False(t, removed) assert.Equal(t, before, string(readFile(t, path))) @@ -260,7 +260,7 @@ func TestTOMLHeaderLikeLineInMultilineString(t *testing.T) { assert.Equal(t, notes+"\n"+strings.Replace(testBlock, testName, "genie", 1), string(readFile(t, path))) // Removing our server leaves the string as it was. - removed, err := Remove(t.Context(), a, "genie") + removed, err := removeAgent(t.Context(), a, "genie") require.NoError(t, err) assert.True(t, removed) assert.Equal(t, notes, string(readFile(t, path))) @@ -284,7 +284,7 @@ func TestTOMLRemoveKeepsOneSeparator(t *testing.T) { writeFile(t, path, []byte(tc.before), 0o600) a := testAgent(path, mcpconfig.FormatTOML, "mcp_servers") - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.True(t, removed) got := string(readFile(t, path)) @@ -308,7 +308,7 @@ func TestTOMLEditWithNonFiniteFloatElsewhere(t *testing.T) { _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.True(t, removed) assert.Equal(t, before, string(readFile(t, path))) @@ -345,7 +345,7 @@ func TestTOMLAddThenRemoveRestoresBytes(t *testing.T) { _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) require.NoError(t, err) - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.True(t, removed) assert.Equal(t, tc.before, string(readFile(t, path))) diff --git a/libs/aitools/mcp/agentconfig/unmanaged.go b/libs/aitools/mcp/agentconfig/unmanaged.go index a4aad7e6ffe..c0681f8d1b5 100644 --- a/libs/aitools/mcp/agentconfig/unmanaged.go +++ b/libs/aitools/mcp/agentconfig/unmanaged.go @@ -6,13 +6,18 @@ import ( "github.com/databricks/cli/libs/aitools/mcpconfig" ) +// IsUGProxy reports whether entry runs the `ug mcp-proxy` bridge: a ug binary whose first argument is mcp-proxy. +func IsUGProxy(entry map[string]any) bool { + argv, ok := mcpconfig.Argv(entry) + return ok && len(argv) > 1 && mcpconfig.IsBinary(argv[0], "ug", "ug.exe") && argv[1] == "mcp-proxy" +} + // IsUnmanaged reports whether entry connects to Databricks managed MCP servers // without having been written by this CLI: the `ug mcp-proxy` bridge, or a // direct /ai-gateway/mcp-services/ URL. Such entries are listed by `list`, and // Upsert replaces one only with force. func IsUnmanaged(entry map[string]any) bool { - if argv, ok := mcpconfig.Argv(entry); ok && len(argv) > 1 && - mcpconfig.IsBinary(argv[0], "ug", "ug.exe") && argv[1] == "mcp-proxy" { + if IsUGProxy(entry) { return true } // Remote entries name their URL differently per agent: url (Cursor, Claude diff --git a/libs/aitools/mcp/agentconfig/unmanaged_test.go b/libs/aitools/mcp/agentconfig/unmanaged_test.go index 3c4f2f4590c..f61bcfc37b6 100644 --- a/libs/aitools/mcp/agentconfig/unmanaged_test.go +++ b/libs/aitools/mcp/agentconfig/unmanaged_test.go @@ -58,3 +58,11 @@ func TestIsUnmanaged(t *testing.T) { }) } } + +// TestIsUGProxy checks that only a ug binary running mcp-proxy is the ug bridge: an mcp-proxy argument under another +// command is not. +func TestIsUGProxy(t *testing.T) { + assert.True(t, IsUGProxy(map[string]any{"command": "/usr/local/bin/ug", "args": []any{"mcp-proxy"}})) + assert.False(t, IsUGProxy(map[string]any{"command": "npx", "args": []any{"mcp-proxy"}})) + assert.False(t, IsUGProxy(map[string]any{"command": "npx", "args": []any{"mcp-proxy"}, "url": "https://adb-1.test/ai-gateway/mcp-services/x"})) +} diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 74f632a99ea..c4129f75abf 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -100,7 +100,7 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin if !mcpconfig.IsDatabricksEntry(ours) { return "", "", fmt.Errorf("server %q: args must start with aitools mcp serve", name) } - path, err := spec.Path(ctx) + path, err := configPath(ctx, spec) if err != nil { return "", "", err } @@ -149,10 +149,10 @@ func Upsert(ctx context.Context, a *agents.Agent, name, cmd string, args []strin return path, status, nil } -// Remove deletes server name from agent a's config. It reports false, without -// writing, when there is no such entry, and refuses an entry this CLI did not -// write. -func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { +// RemoveAt deletes server name from the config file at path, and refuses an entry this CLI did not write. It reports +// false, without writing, when there is no such entry. The file need not be the one the spec names now: a record names +// the file an entry was written to, and the agent's config may have moved since. A missing file has no entry. +func RemoveAt(a *agents.Agent, path, name string) (bool, error) { if err := checkName(name); err != nil { return false, err } @@ -160,10 +160,6 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { if err != nil { return false, err } - path, err := spec.Path(ctx) - if err != nil { - return false, err - } doc, err := load(spec, path) if err != nil { return false, err @@ -188,17 +184,14 @@ func Remove(ctx context.Context, a *agents.Agent, name string) (bool, error) { return true, nil } -// Read returns every server entry in agent a's config, keyed by name. A missing -// file has no entries. -func Read(ctx context.Context, a *agents.Agent) (map[string]map[string]any, error) { +// ReadAt returns every server entry in the config file at path, keyed by name. A missing file has no entries. The file +// need not be the one the spec names now: a record names the file an entry was written to, and the agent's config may +// have moved since. +func ReadAt(a *agents.Agent, path string) (map[string]map[string]any, error) { spec, err := specOf(a) if err != nil { return nil, err } - path, err := spec.Path(ctx) - if err != nil { - return nil, err - } doc, err := load(spec, path) if err != nil { return nil, err @@ -210,7 +203,27 @@ func Read(ctx context.Context, a *agents.Agent) (map[string]map[string]any, erro return entries, nil } -// checkName refuses a name that no agent would accept. Upsert and Remove both +// Path returns the absolute path of agent a's MCP config file, where Upsert writes. It does not read the file. +func Path(ctx context.Context, a *agents.Agent) (string, error) { + spec, err := specOf(a) + if err != nil { + return "", err + } + return configPath(ctx, spec) +} + +// configPath returns the absolute path of the config file of spec. A config dir set to a relative path is made absolute +// here, not when the path is used, so that a record of the path works from any directory. The path is not cleaned, as +// mcpconfig.AbsoluteAsWritten keeps it, so that a ".." after a symlink still resolves through the link. +func configPath(ctx context.Context, spec *mcpconfig.Spec) (string, error) { + path, err := spec.Path(ctx) + if err != nil { + return "", err + } + return mcpconfig.AbsoluteAsWritten(path) +} + +// checkName refuses a name that no agent would accept. Upsert and RemoveAt both // check it, so a name that cannot be written is never looked up either. func checkName(name string) error { if !mcpconfig.ValidServerName(name) { diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index 4819ff495f7..b9cf3520b88 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -120,13 +120,13 @@ func TestRoundTripPerAgent(t *testing.T) { assert.Equal(t, StatusUnchanged, status) assert.Equal(t, after, readFile(t, path)) - entries, err := Read(ctx, a) + entries, err := readAgent(ctx, a) require.NoError(t, err) require.Contains(t, entries, testName) assert.True(t, mcpconfig.IsDatabricksEntry(entries[testName])) assert.Contains(t, entries, "other") - removed, err := Remove(ctx, a, testName) + removed, err := removeAgent(ctx, a, testName) require.NoError(t, err) assert.True(t, removed) assert.Equal(t, semantic(t, a.MCP.Format, before), semantic(t, a.MCP.Format, readFile(t, path))) @@ -159,7 +159,7 @@ func TestUpsertRefusesUnparsableFile(t *testing.T) { _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) require.ErrorIs(t, err, ErrConfigInvalid) assert.ErrorContains(t, err, filepath.ToSlash(path)) - _, err = Remove(t.Context(), a, testName) + _, err = removeAgent(t.Context(), a, testName) require.ErrorIs(t, err, ErrConfigInvalid) assert.Equal(t, tc.data, string(readFile(t, path))) @@ -180,7 +180,7 @@ func TestUpsertRefusesEntryWeDidNotWrite(t *testing.T) { _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.True(t, mcpconfig.IsDatabricksEntry(entries[testName])) } @@ -205,7 +205,7 @@ func TestUpsertUpdatesOwnedEntry(t *testing.T) { _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Equal(t, testArgs("test"), toStrings(t, entries[testName]["args"])) }) @@ -251,7 +251,7 @@ func TestUpsertRefusesCommandThatIsNotDatabricks(t *testing.T) { func TestReadMissingFileHasNoEntries(t *testing.T) { a := testAgent(filepath.Join(t.TempDir(), "missing.toml"), mcpconfig.FormatTOML, "mcp_servers") - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Empty(t, entries) } @@ -262,7 +262,7 @@ func TestRemoveRefusesEntryWeDidNotWrite(t *testing.T) { writeFile(t, path, []byte(other), 0o600) a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") - _, err := Remove(t.Context(), a, testName) + _, err := removeAgent(t.Context(), a, testName) require.ErrorIs(t, err, ErrNotOwned) assert.Equal(t, other, string(readFile(t, path))) } @@ -271,7 +271,7 @@ func TestRemoveAbsentEntryDoesNotCreateFile(t *testing.T) { path := filepath.Join(t.TempDir(), "missing.json") a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") - removed, err := Remove(t.Context(), a, testName) + removed, err := removeAgent(t.Context(), a, testName) require.NoError(t, err) assert.False(t, removed) _, err = os.Stat(path) @@ -338,7 +338,7 @@ func TestUpsertWritesThroughSymlink(t *testing.T) { info, err := os.Lstat(link) require.NoError(t, err) assert.NotZero(t, info.Mode()&os.ModeSymlink, "link is replaced by a regular file") - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Contains(t, entries, testName) } @@ -379,7 +379,7 @@ func TestWindowsCommandPathRoundTrip(t *testing.T) { require.NoError(t, err) assert.Contains(t, string(readFile(t, path)), tc.want) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) require.Contains(t, entries, testName) assert.Equal(t, winCmd, entries[testName]["command"]) @@ -437,7 +437,7 @@ func TestUpsertKeepsUserKeysJSON(t *testing.T) { _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Equal(t, testArgs("other"), toStrings(t, entries[testName]["args"])) assert.Equal(t, true, entries[testName]["disabled"]) @@ -448,7 +448,7 @@ func TestUpsertKeepsUserKeysJSON(t *testing.T) { _, status, err = Upsert(t.Context(), testAgent(foreign, mcpconfig.FormatJSON, "mcpServers"), testName, testCmd, testArgs("test"), nil, true) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err = Read(t.Context(), testAgent(foreign, mcpconfig.FormatJSON, "mcpServers")) + entries, err = readAgent(t.Context(), testAgent(foreign, mcpconfig.FormatJSON, "mcpServers")) require.NoError(t, err) assert.NotContains(t, entries[testName], "disabled") } @@ -469,7 +469,7 @@ func TestUpsertKeepsUserKeysTOML(t *testing.T) { _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Equal(t, testArgs("other"), toStrings(t, entries[testName]["args"])) assert.Equal(t, false, entries[testName]["enabled"]) @@ -496,7 +496,7 @@ func TestUpsertKeepsToolsNarrowedByUser(t *testing.T) { _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Equal(t, []any{"search"}, entries[testName]["tools"]) } @@ -565,7 +565,7 @@ func TestUpsertUpdateAddsAndRemovesLaunchKeys(t *testing.T) { _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{mcpconfig.ConfigFileEnv: "/cfg"}, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Equal(t, map[string]any{mcpconfig.ConfigFileEnv: "/cfg"}, entries[testName]["env"]) requireStrictJSON(t, string(readFile(t, path))) @@ -573,7 +573,7 @@ func TestUpsertUpdateAddsAndRemovesLaunchKeys(t *testing.T) { _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err = Read(t.Context(), a) + entries, err = readAgent(t.Context(), a) require.NoError(t, err) assert.NotContains(t, entries[testName], "env") requireStrictJSON(t, string(readFile(t, path))) @@ -591,7 +591,7 @@ func TestTOMLUpdateKeepsUserTables(t *testing.T) { _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("other"), nil, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) - entries, err := Read(t.Context(), a) + entries, err := readAgent(t.Context(), a) require.NoError(t, err) assert.Equal(t, map[string]any{"search": map[string]any{"approval_mode": "approve"}}, entries[testName]["tools"]) } @@ -615,7 +615,7 @@ func TestUpsertAndRemoveRefuseDuplicateKeysJSON(t *testing.T) { _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, true) require.ErrorIs(t, err, ErrConfigInvalid) - _, err = Remove(t.Context(), a, testName) + _, err = removeAgent(t.Context(), a, testName) require.ErrorIs(t, err, ErrConfigInvalid) assert.Equal(t, data, string(readFile(t, path))) }) @@ -642,7 +642,7 @@ func TestRemoveRefusesInvalidServerName(t *testing.T) { writeFile(t, path, []byte(data), 0o600) a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") - _, err := Remove(t.Context(), a, "genie.one") + _, err := removeAgent(t.Context(), a, "genie.one") assert.ErrorContains(t, err, "must contain only letters, digits, '-' and '_'") assert.Equal(t, data, string(readFile(t, path))) } @@ -650,7 +650,7 @@ func TestRemoveRefusesInvalidServerName(t *testing.T) { // entryOf returns the entry of server name in agent a's config. func entryOf(t *testing.T, ctx context.Context, a *agents.Agent, name string) map[string]any { t.Helper() - entries, err := Read(ctx, a) + entries, err := readAgent(ctx, a) require.NoError(t, err) require.Contains(t, entries, name) return entries[name] @@ -784,3 +784,73 @@ func TestUpsertRefusesEnvThatIsNotATable(t *testing.T) { require.ErrorIs(t, err, ErrConfigInvalid) assert.Equal(t, data, string(readFile(t, path))) } + +// TestPathIsTheFileTheSpecNames checks that Path returns the file the spec names, without creating or reading it. +func TestPathIsTheFileTheSpecNames(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.json") + a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") + + got, err := Path(t.Context(), a) + require.NoError(t, err) + assert.Equal(t, path, got) + _, err = os.Stat(path) + assert.ErrorIs(t, err, os.ErrNotExist) +} + +func TestPathFailsForAgentWithoutMCP(t *testing.T) { + _, err := Path(t.Context(), &agents.Agent{Name: "pi", DisplayName: "Pi"}) + require.Error(t, err) +} + +// TestReadAtAndRemoveAtUseTheGivenFile checks that ReadAt and RemoveAt work on the file they are given, not on the +// file the agent's spec names now. The entry is written to one file, and the spec names another. +func TestReadAtAndRemoveAtUseTheGivenFile(t *testing.T) { + recorded := filepath.Join(t.TempDir(), "recorded.json") + current := filepath.Join(t.TempDir(), "current.json") + _, _, err := Upsert(t.Context(), testAgent(recorded, mcpconfig.FormatJSON, "mcpServers"), testName, testCmd, testArgs("test"), nil, false) + require.NoError(t, err) + a := testAgent(current, mcpconfig.FormatJSON, "mcpServers") + + entries, err := ReadAt(a, recorded) + require.NoError(t, err) + assert.Contains(t, entries, testName) + + entries, err = readAgent(t.Context(), a) + require.NoError(t, err) + assert.Empty(t, entries, "the spec's file does not have the entry") + + removed, err := RemoveAt(a, recorded, testName) + require.NoError(t, err) + assert.True(t, removed) + entries, err = ReadAt(a, recorded) + require.NoError(t, err) + assert.Empty(t, entries) + _, err = os.Stat(current) + assert.ErrorIs(t, err, os.ErrNotExist, "RemoveAt does not write the spec's file") +} + +// TestReadAtMissingFileHasNoEntries checks that a file that no longer exists reads as having no entries. +func TestReadAtMissingFileHasNoEntries(t *testing.T) { + a := testAgent(filepath.Join(t.TempDir(), "current.json"), mcpconfig.FormatJSON, "mcpServers") + entries, err := ReadAt(a, filepath.Join(t.TempDir(), "gone.json")) + require.NoError(t, err) + assert.Empty(t, entries) +} + +// readAgent reads the entries of agent a's config at the path Path names, as the commands read the config. +func readAgent(ctx context.Context, a *agents.Agent) (map[string]map[string]any, error) { + path, err := Path(ctx, a) + if err != nil { + return nil, err + } + return ReadAt(a, path) +} + +// removeAgent removes server name from agent a's config at the path Path names. +func removeAgent(ctx context.Context, a *agents.Agent, name string) (bool, error) { + path, err := Path(ctx, a) + if err != nil { + return false, err + } + return RemoveAt(a, path, name) +} diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 0b4a2741c73..20f68088306 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -182,9 +182,19 @@ func uncategorizedError(err error) error { return &BindError{Category: BindUncategorized, message: err.Error(), cause: err} } -// removeCommand returns the command that removes the server bound to profileName. -func removeCommand(profileName string) string { - return mcpRemoveCommand + " --profile " + shellquote.BashArg(profileName) +// RemoveNamesCommand returns the command that removes the server named name. Server names are unique in the state. +func RemoveNamesCommand(name string) string { + return mcpRemoveCommand + " --names " + shellquote.BashArg(name) +} + +// RecreateCommand returns the command that logs in to host for profileName again. configFile is the file the login must +// write, passed as DATABRICKS_CONFIG_FILE; pass "" when the login's default file is that file. +func RecreateCommand(host, profileName, configFile string) string { + command := authLoginCommand + " --host " + shellquote.BashArg(host) + " --profile " + shellquote.BashArg(profileName) + if configFile != "" { + command = "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(configFile) + " " + command + } + return command } // profileGoneError builds the PROFILE_NOT_FOUND error for a profile that the config file does not @@ -198,27 +208,31 @@ func profileGoneError(name, profileName, path string, entry *Entry, problem stri message := fmt.Sprintf("MCP server %s needs Databricks profile \"%s\", which %s in %s. Set it up with: %s", name, profileName, problem, path, login) return &BindError{Category: BindProfileNotFound, message: message} } - recreate := authLoginCommand + " --host " + shellquote.BashArg(entry.Host) + " --profile " + shellquote.BashArg(profileName) + pinnedPath := "" if entry.ConfigFile != "" { - recreate = "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(path) + " " + recreate + pinnedPath = path } + recreate := RecreateCommand(entry.Host, profileName, pinnedPath) 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)) + name, profileName, problem, path, recreate, RemoveNamesCommand(name)) return &BindError{Category: BindProfileNotFound, message: message} } -// driftError is the PROFILE_HOST_CHANGED error for an entry whose profile now names another host, -// or another workspace on the same host. -func driftError(name, profileName string, entry Entry, drift Drift) error { +// DriftMessage tells how the profile of entry, which is named profileName, now differs from what the entry recorded: its +// host, or its workspace when the host is the same. subject starts the sentence, for example "MCP server github-logfood" +// or "it". The serve errors and `mcp list` both word a drift with it. +func DriftMessage(subject string, entry Entry, profileName string, drift Drift) string { recordedHost, recordedWorkspaceID := recordedTarget(entry) - var message string if NormalizeHost(recordedHost) != NormalizeHost(drift.CurrentHost) { - message = fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: %s and add it again", - name, recordedHost, profileName, drift.CurrentHost, removeCommand(profileName)) - } else { - message = fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: %s and add it again", - name, recordedWorkspaceID, profileName, drift.CurrentWorkspaceID, removeCommand(profileName)) + return fmt.Sprintf("%s was set up for %s, but profile \"%s\" now points at %s", subject, recordedHost, profileName, drift.CurrentHost) } + return fmt.Sprintf("%s was set up for workspace %s, but profile \"%s\" now points at workspace %s", subject, recordedWorkspaceID, profileName, drift.CurrentWorkspaceID) +} + +// driftError is the PROFILE_HOST_CHANGED error for an entry whose profile now names another host, +// or another workspace on the same host. +func driftError(name, profileName string, entry Entry, drift Drift) error { + message := fmt.Sprintf("%s. Run: %s and add it again", DriftMessage("MCP server "+name, entry, profileName, drift), RemoveNamesCommand(name)) return &BindError{Category: BindProfileHostChanged, message: message} } @@ -226,7 +240,7 @@ func driftError(name, profileName string, entry Entry, drift Drift) error { // use. The profile may name the same host there, but the server was set up against the recorded file. func configFileError(name, profileName, recordedPath, currentPath string) error { message := fmt.Sprintf("MCP server %s was set up with config file %s, but profile \"%s\" is read from %s now. Run: %s and add it again, or set DATABRICKS_CONFIG_FILE to %s", - name, recordedPath, profileName, currentPath, removeCommand(profileName), shellquote.BashArg(recordedPath)) + name, recordedPath, profileName, currentPath, RemoveNamesCommand(name), shellquote.BashArg(recordedPath)) return &BindError{Category: BindProfileHostChanged, message: message} } @@ -259,7 +273,7 @@ func FindEntry(ctx context.Context, state *State, service, profileName, configFi unresolved = errors.Join(unresolved, fmt.Errorf("MCP server %s: %w", entry.Name, err)) continue } - if samePath(path, configFile) { + if SamePath(path, configFile) { return entry, nil } if other == nil { diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 1ab27abf006..2d58a95d138 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -13,6 +13,7 @@ import ( "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/shellquote" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -127,7 +128,7 @@ func TestBindProfileAbsent(t *testing.T) { Host: bindingTestHost, ConfigFile: path, }}}, - want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path), filepath.ToSlash(path)), + want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --names github-gone`, filepath.ToSlash(path), shellquote.BashArg(filepath.ToSlash(path))), }, { name: "without state entry", @@ -175,12 +176,12 @@ func TestBindHostChanged(t *testing.T) { { name: "host", entry: "https://adb-999.test", - want: "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again", + want: "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --names github-logfood and add it again", }, { name: "host with a query naming another workspace on the same host", entry: "https://adb-999.test/?o=111", - want: "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again", + want: "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --names github-logfood and add it again", }, } for _, tt := range tests { @@ -219,7 +220,7 @@ func TestBindHostChangedDoesNotContactNewHost(t *testing.T) { } func TestBindWorkspaceChanged(t *testing.T) { - const want = "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --profile logfood and add it again" + const want = "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --names github-logfood and add it again" tests := []struct { name string entryHost string @@ -423,7 +424,7 @@ func TestBindWorkspaceFromHostQueryChanged(t *testing.T) { Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindProfileHostChanged) - assert.Equal(t, "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) + assert.Equal(t, "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --names github-logfood and add it again", err.Error()) }) } } @@ -663,7 +664,7 @@ func TestBindProfileWithoutHost(t *testing.T) { }) requireBindError(t, err, mcp.BindProfileNotFound) resolved := filepath.ToSlash(bindingTestResolved(t, path)) - assert.Equal(t, fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "logfood", which has no host in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile logfood or remove the server with: databricks aitools mcp remove --profile logfood`, resolved, resolved), err.Error()) + assert.Equal(t, fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "logfood", which has no host in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile logfood or remove the server with: databricks aitools mcp remove --names github-logfood`, resolved, shellquote.BashArg(resolved)), err.Error()) } // TestBindHostMetadataFailureIsNotAuthFailure checks that a host whose metadata cannot be fetched does @@ -776,8 +777,8 @@ func TestBindRecreateHintQuotesValues(t *testing.T) { Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindProfileNotFound) - want := fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "my profile", which no longer exists in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile 'my profile' or remove the server with: databricks aitools mcp remove --profile 'my profile'`, - filepath.ToSlash(bindingTestResolved(t, cfgPath)), filepath.ToSlash(bindingTestResolved(t, cfgPath))) + want := fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "my profile", which no longer exists in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile 'my profile' or remove the server with: databricks aitools mcp remove --names github-logfood`, + filepath.ToSlash(bindingTestResolved(t, cfgPath)), shellquote.BashArg(filepath.ToSlash(bindingTestResolved(t, cfgPath)))) assert.Equal(t, want, err.Error()) } @@ -801,7 +802,7 @@ func TestCheckBindingMatchesBind(t *testing.T) { _, err := bindingTestCheck(t, state) requireBindError(t, err, mcp.BindProfileHostChanged) - assert.Equal(t, "MCP server github-logfood was set up for https://adb-123.test, but profile \"logfood\" now points at "+server.URL+". Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) + 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 --names github-logfood and add it again", err.Error()) } func TestCheckBindingPassesWhenEntryMatchesOrIsAbsent(t *testing.T) { diff --git a/libs/aitools/mcp/configfile.go b/libs/aitools/mcp/configfile.go index 8e2b7346d5d..428b7ad78c1 100644 --- a/libs/aitools/mcp/configfile.go +++ b/libs/aitools/mcp/configfile.go @@ -3,11 +3,11 @@ package mcp import ( "context" "errors" - "fmt" "io/fs" "os" "path/filepath" + "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/databrickscfg" ) @@ -26,31 +26,16 @@ 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 mcpconfig.AbsoluteAsWritten(path) default: return "", err } } -// 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 - } - wd, err := os.Getwd() - if err != nil { - return "", fmt.Errorf("failed to determine working directory: %w", err) - } - return wd + string(filepath.Separator) + path, nil -} - -// samePath reports whether two resolved config file paths name the same file. Existing files are +// SamePath reports whether two resolved config file paths name the same file. Existing files are // compared by identity, which also matches hard links and case variants on case-insensitive file // systems. Paths that do not exist are compared as text. -func samePath(a, b string) bool { +func SamePath(a, b string) bool { fa, errA := os.Stat(a) fb, errB := os.Stat(b) if errA == nil && errB == nil { diff --git a/libs/aitools/mcp/configfile_test.go b/libs/aitools/mcp/configfile_test.go index 7660a8a98cf..92286c1e21c 100644 --- a/libs/aitools/mcp/configfile_test.go +++ b/libs/aitools/mcp/configfile_test.go @@ -81,6 +81,6 @@ func TestSamePathComparesExistingFilesByIdentity(t *testing.T) { t.Skipf("cannot create hard link: %v", err) } - assert.True(t, samePath(a, b)) - assert.False(t, samePath(filepath.Join(dir, "x"), filepath.Join(dir, "X"))) + assert.True(t, SamePath(a, b)) + assert.False(t, SamePath(filepath.Join(dir, "x"), filepath.Join(dir, "X"))) } diff --git a/libs/aitools/mcp/configfiles_test.go b/libs/aitools/mcp/configfiles_test.go new file mode 100644 index 00000000000..37d1559b55f --- /dev/null +++ b/libs/aitools/mcp/configfiles_test.go @@ -0,0 +1,25 @@ +package mcp + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestConfigFilesCachesAFailedRead checks that a config file which cannot be read is read once per lookup: later +// profiles of the same file get the same failure, without a second read that could differ. +func TestConfigFilesCachesAFailedRead(t *testing.T) { + path := filepath.Join(t.TempDir(), ".databrickscfg") + require.NoError(t, os.WriteFile(path, []byte("[broken\n"), 0o600)) + + files := configFiles{} + _, first := files.profile(path, "logfood") + require.Error(t, first) + + require.NoError(t, os.WriteFile(path, []byte("[logfood]\nhost = https://adb-1.test\n"), 0o600)) + _, second := files.profile(path, "logfood") + assert.Equal(t, first, second, "the failure of the file is kept for the lookup") +} diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index bf34adc7d6b..6c448d2543b 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -146,7 +146,7 @@ func oauthProvider(svc *catalog.McpService) string { // returned name. func newService(svc *catalog.McpService) Service { fullName := strings.TrimPrefix(svc.Name, mcpServicePrefix) - leaf := leafOf(fullName) + leaf := LeafOf(fullName) provider := oauthProvider(svc) s := Service{ diff --git a/libs/aitools/mcp/entry.go b/libs/aitools/mcp/entry.go index 5b1161089e7..509e5055ff2 100644 --- a/libs/aitools/mcp/entry.go +++ b/libs/aitools/mcp/entry.go @@ -27,7 +27,7 @@ func EntryServes(ctx context.Context, entry map[string]any, service, profileName if err != nil { return false, fmt.Errorf("MCP server entry for %s on profile %s: %w", service, profileName, err) } - return samePath(path, configFile), nil + return SamePath(path, configFile), nil } // flagValue returns the value that follows flag in argv, or "" when flag is absent. diff --git a/libs/aitools/mcp/naming.go b/libs/aitools/mcp/naming.go index 4fa910c7daa..b7b011e3709 100644 --- a/libs/aitools/mcp/naming.go +++ b/libs/aitools/mcp/naming.go @@ -34,7 +34,7 @@ func Sanitize(s string) string { func ServerName(fullName, profile string) string { parts := strings.Split(fullName, ".") if inSystemAI(fullName) { - parts = []string{leafOf(fullName)} + parts = []string{LeafOf(fullName)} } var name []string @@ -46,8 +46,8 @@ func ServerName(fullName, profile string) string { return strings.Join(name, "-") } -// leafOf returns the service name after the last ".". Unity Catalog names cannot contain ".". -func leafOf(fullName string) string { +// LeafOf returns the service name after the last ".". Unity Catalog names cannot contain ".". +func LeafOf(fullName string) string { return fullName[strings.LastIndex(fullName, ".")+1:] } diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go index 9516442eac5..4e55a8e678e 100644 --- a/libs/aitools/mcp/state.go +++ b/libs/aitools/mcp/state.go @@ -10,9 +10,10 @@ import ( "github.com/databricks/cli/libs/aitools/installer" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/databricks-sdk-go/config" + "gopkg.in/ini.v1" ) -// Drift kinds reported by Reconcile. +// Drift kinds reported by CheckEntries. const ( DriftProfileMissing DriftKind = "profile_missing" DriftHostChanged DriftKind = "host_changed" @@ -48,6 +49,9 @@ type Drift struct { // CurrentWorkspaceID is the workspace the profile names when Kind is DriftHostChanged: its // workspace_id, else the workspace in its host query. It is "" when the profile names none. CurrentWorkspaceID string + // NoHost is set when Kind is DriftProfileMissing because the profile's section has no host, not + // because the section is absent. + NoHost bool } // LoadState reads the MCP section of the aitools state file. A missing file or section @@ -77,83 +81,100 @@ func SaveState(ctx context.Context, state *State) error { return installer.SaveMCPState(dir, state) } -// Reconcile reports entries whose profile is gone or has no host, and entries whose profile now -// names another host, or another workspace when both the entry and the profile name one. Each -// entry is checked against the config file it is pinned to; an entry with no pinned file is checked -// against the default file, ~/.databrickscfg, whatever the environment names. Each config file is -// read once. -// -// An entry that cannot be checked, such as one whose "~" path has no home directory to resolve, does -// not stop the others. Its error is joined into the returned error, and the drifts of the other -// entries are still returned. -func Reconcile(ctx context.Context, state *State) ([]Drift, error) { +// EntryCheck is the result of checking one state entry. Drift is nil when the entry's profile still names the recorded +// host and workspace. Err is set when the entry cannot be checked. Path is the config file the entry is checked against, +// resolved; it is "" when that file cannot be resolved, and Err says why. +type EntryCheck struct { + Drift *Drift + Err error + Path string +} + +// CheckEntries checks each entry of state against its profile and returns one result per entry, in state order. Each +// entry is checked against the config file it is pinned to; an entry with no pinned file is checked against the default +// file, ~/.databrickscfg, whatever the environment names. Each config file is read once for all the entries. An entry +// that cannot be checked, such as one whose "~" path has no home directory to resolve, does not stop the others. +func CheckEntries(ctx context.Context, state *State) []EntryCheck { files := configFiles{} - var drifts []Drift - var errs []error - for _, entry := range state.Servers { - drift, err := checkEntry(ctx, files, entry) - if err != nil { - errs = append(errs, fmt.Errorf("MCP server %s: %w", entry.Name, err)) - continue - } - if drift != nil { - drifts = append(drifts, *drift) - } - } - return drifts, errors.Join(errs...) + checks := make([]EntryCheck, len(state.Servers)) + for i, entry := range state.Servers { + checks[i] = checkEntry(ctx, files, entry) + } + return checks } -// 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) { +// checkEntry checks one entry against the profile in the config file it is pinned to. +func checkEntry(ctx context.Context, files configFiles, entry Entry) EntryCheck { path, err := ResolveConfigFile(ctx, entry.ConfigFile) if err != nil { - return nil, err + return EntryCheck{Err: err} } + check := EntryCheck{Path: path} p, err := files.profile(path, entry.Profile) - if errors.Is(err, errProfileMissing) || errors.Is(err, errProfileNoHost) { - return &Drift{Kind: DriftProfileMissing, Entry: entry}, nil + switch { + case errors.Is(err, errProfileMissing) || errors.Is(err, errProfileNoHost): + check.Drift = &Drift{Kind: DriftProfileMissing, Entry: entry, NoHost: errors.Is(err, errProfileNoHost)} + case err != nil: + check.Err = err + default: + check.Drift = driftOf(entry, p) } - if err != nil { - return nil, err - } - return driftOf(entry, p), nil + return check } -// configFiles holds the config files that one lookup has read, by resolved path, so that each file is -// parsed once. A nil file is one that does not exist. -type configFiles map[string]*config.File +// configFiles holds the config files that one lookup has read, by resolved path, so that each file is parsed once. A +// file that cannot be read keeps its error, which every profile of that file then reports. +type configFiles map[string]configFile + +// configFile is one config file as a lookup read it: the parsed file, which is nil when the file does not exist, or the +// error that reading it gave. +type configFile struct { + file *config.File + err error +} // 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) { - file, ok := c[path] + f, ok := c[path] if !ok { - loaded, err := config.LoadFile(path) - switch { - case errors.Is(err, fs.ErrNotExist): - loaded = nil - case err != nil: - return profile.Profile{}, fmt.Errorf("failed to read Databricks profiles: %w", err) - } - file = loaded - c[path] = file - } - if file == nil { + f = readConfigFile(path) + c[path] = f + } + if f.err != nil { + return profile.Profile{}, f.err + } + if f.file == nil { return profile.Profile{}, errProfileMissing } - section, err := file.GetSection(name) + section, err := f.file.GetSection(name) if err != nil { return profile.Profile{}, errProfileMissing } keys := section.KeysHash() + if len(keys) == 0 && name == ini.DefaultSection { + // The INI parser always has a DEFAULT section, so an empty one means the file has no such profile. + return profile.Profile{}, errProfileMissing + } if keys["host"] == "" { return profile.Profile{}, errProfileNoHost } return profile.Profile{Name: name, Host: keys["host"], WorkspaceID: keys["workspace_id"]}, nil } +// readConfigFile reads the config file at path. A file that does not exist is read as nil, with no error. +func readConfigFile(path string) configFile { + file, err := config.LoadFile(path) + switch { + case errors.Is(err, fs.ErrNotExist): + return configFile{} + case err != nil: + return configFile{err: fmt.Errorf("failed to read Databricks profiles: %w", err)} + } + return configFile{file: file} +} + // driftOf returns the drift of an entry against the profile it names, or nil when the profile still // names the recorded host and workspace. func driftOf(entry Entry, p profile.Profile) *Drift { diff --git a/libs/aitools/mcp/state_test.go b/libs/aitools/mcp/state_test.go index 06cca4350d7..d6881dd754f 100644 --- a/libs/aitools/mcp/state_test.go +++ b/libs/aitools/mcp/state_test.go @@ -3,6 +3,7 @@ package mcp_test import ( "context" "encoding/json" + "errors" "fmt" "os" "path/filepath" @@ -132,7 +133,7 @@ func TestSaveStateUpgradesV2FileToV3(t *testing.T) { // stateTestProfiles is the profiles file used by the drift tests. The profiles named // spog-query, spog-none, spog-both and spog-abc name their workspace in the host query instead of // workspace_id. -// stateTestWriteProfiles writes the profiles that the Reconcile tests check to the default config file +// stateTestWriteProfiles writes the profiles that the CheckEntries tests check to the default config file // of the test home directory. func stateTestWriteProfiles(t *testing.T, ctx context.Context) { t.Helper() @@ -160,7 +161,22 @@ host = https://spog.test/?o=abc&w=222 `, bindingTestProfile, bindingTestHost, stateTestWorkspaceID, stateTestOtherProfile, stateTestOtherHost)) } -func TestReconcile(t *testing.T) { +// driftsOf returns the drifts of checks in state order, and the errors of the entries that cannot be checked. +func driftsOf(checks []mcp.EntryCheck) ([]mcp.Drift, error) { + var drifts []mcp.Drift + var errs []error + for _, c := range checks { + if c.Err != nil { + errs = append(errs, c.Err) + } + if c.Drift != nil { + drifts = append(drifts, *c.Drift) + } + } + return drifts, errors.Join(errs...) +} + +func TestCheckEntries(t *testing.T) { ctx, _ := stateTestCtx(t) stateTestWriteProfiles(t, ctx) tests := []struct { @@ -236,7 +252,7 @@ func TestReconcile(t *testing.T) { t.Run(tt.name, func(t *testing.T) { state := &mcp.State{Servers: []mcp.Entry{tt.entry}} - drifts, err := mcp.Reconcile(ctx, state) + drifts, err := driftsOf(mcp.CheckEntries(ctx, state)) require.NoError(t, err) if tt.wantKind == "" { assert.Empty(t, drifts) @@ -252,34 +268,34 @@ func TestReconcile(t *testing.T) { } } -func TestReconcileWithoutConfigFileReportsEveryProfileMissing(t *testing.T) { +func TestCheckEntriesWithoutConfigFileReportsEveryProfileMissing(t *testing.T) { ctx, _ := stateTestCtx(t) ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) entry := stateTestEntry() state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(ctx, state) + drifts, err := driftsOf(mcp.CheckEntries(ctx, state)) require.NoError(t, err) assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) } -// TestReconcileChecksEntryAgainstPinnedConfigFile checks that an entry pinned to a config file +// TestCheckEntriesChecksEntryAgainstPinnedConfigFile checks that an entry pinned to a config file // is checked against that file, not the one the environment names. -func TestReconcileChecksEntryAgainstPinnedConfigFile(t *testing.T) { +func TestCheckEntriesChecksEntryAgainstPinnedConfigFile(t *testing.T) { pinned := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) entry := stateTestEntry() entry.ConfigFile = pinned state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(ctx, state) + drifts, err := driftsOf(mcp.CheckEntries(ctx, state)) require.NoError(t, err) assert.Empty(t, drifts) } -// TestReconcileUnpinnedEntryChecksDefaultConfigFile checks that an entry with no pinned config file +// TestCheckEntriesUnpinnedEntryChecksDefaultConfigFile checks that an entry with no pinned config file // is checked against ~/.databrickscfg, even when DATABRICKS_CONFIG_FILE names another file. -func TestReconcileUnpinnedEntryChecksDefaultConfigFile(t *testing.T) { +func TestCheckEntriesUnpinnedEntryChecksDefaultConfigFile(t *testing.T) { ctx, _ := stateTestCtx(t) home, err := env.UserHomeDir(ctx) require.NoError(t, err) @@ -288,7 +304,7 @@ func TestReconcileUnpinnedEntryChecksDefaultConfigFile(t *testing.T) { ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", ambient) state := &mcp.State{Servers: []mcp.Entry{stateTestEntry()}} - drifts, err := mcp.Reconcile(ctx, state) + drifts, err := driftsOf(mcp.CheckEntries(ctx, state)) require.NoError(t, err) assert.Equal(t, []mcp.Drift{{ Kind: mcp.DriftHostChanged, @@ -303,52 +319,83 @@ func TestNormalizeHostIgnoresSchemeCaseAndTrailingSlash(t *testing.T) { assert.NotEqual(t, mcp.NormalizeHost("https://adb-123.test"), mcp.NormalizeHost("https://adb-456.test")) } -// TestReconcileProfileWithoutHostIsMissing checks that a profile section with no host is reported as +// TestCheckEntriesProfileWithoutHostIsMissing checks that a profile section with no host is reported as // missing, since it names no host to compare. It reads a real file, because the in-memory profiler // does not model sections without a host. -func TestReconcileProfileWithEmptyHostIsMissing(t *testing.T) { +func TestCheckEntriesProfileWithEmptyHostIsMissing(t *testing.T) { ctx, _ := stateTestCtx(t) path := bindingTestWriteConfig(t, "[logfood]\nhost =\nauth_type = pat\ntoken = dapi-profile-token\n") - ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", path) + // The entry is pinned to the file that has the section, so the check reads that file. entry := stateTestEntry() + entry.ConfigFile = path state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(ctx, state) + drifts, err := driftsOf(mcp.CheckEntries(ctx, state)) require.NoError(t, err) - assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry, NoHost: true}}, drifts) } -func TestReconcileProfileWithoutHostIsMissing(t *testing.T) { +func TestCheckEntriesProfileWithoutHostIsMissing(t *testing.T) { ctx, _ := stateTestCtx(t) path := bindingTestWriteConfig(t, "[logfood]\nauth_type = pat\ntoken = dapi-profile-token\n") - ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", path) entry := stateTestEntry() + entry.ConfigFile = path + state := &mcp.State{Servers: []mcp.Entry{entry}} + + drifts, err := driftsOf(mcp.CheckEntries(ctx, state)) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry, NoHost: true}}, drifts) +} + +// TestCheckEntriesAbsentSectionIsMissingWithoutNoHost checks that a file with no section of the profile's name is +// reported as missing, not as a section with no host. +func TestCheckEntriesAbsentSectionIsMissingWithoutNoHost(t *testing.T) { + ctx, _ := stateTestCtx(t) + path := bindingTestWriteConfig(t, "[other]\nhost = https://adb-456.test\ntoken = dapi-profile-token\n") + entry := stateTestEntry() + entry.ConfigFile = path state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(ctx, state) + drifts, err := driftsOf(mcp.CheckEntries(ctx, state)) require.NoError(t, err) assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) } -// TestReconcileReportsEntryErrorAndKeepsOthers checks that an entry that cannot be checked, here a "~" -// path with no home directory, is reported as an error without stopping the check of the other entries. -func TestReconcileReportsEntryErrorAndKeepsOthers(t *testing.T) { +// TestCheckEntriesKeepsEachResultOnItsEntry checks that CheckEntries returns one result per entry, in state order, +// and that an entry that cannot be checked does not take the result of another entry with it. +func TestCheckEntriesKeepsEachResultOnItsEntry(t *testing.T) { ctx, _ := stateTestCtx(t) ctx = env.WithUserHomeDir(ctx, "") unresolvable := stateTestEntry() unresolvable.Name = "github-home" unresolvable.ConfigFile = "~/.databrickscfg" + healthy := stateTestEntry() + healthy.ConfigFile = bindingTestWriteConfig(t, fmt.Sprintf("[%s]\nhost = %s\nworkspace_id = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, stateTestWorkspaceID, bindingTestToken)) changed := stateTestEntryWith(bindingTestProfile, "https://adb-999.test", stateTestWorkspaceID) - changed.ConfigFile = bindingTestWriteConfig(t, fmt.Sprintf("[%s]\nhost = %s\nworkspace_id = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, stateTestWorkspaceID, bindingTestToken)) - state := &mcp.State{Servers: []mcp.Entry{unresolvable, changed}} + changed.ConfigFile = healthy.ConfigFile + state := &mcp.State{Servers: []mcp.Entry{unresolvable, healthy, changed}} + + checks := mcp.CheckEntries(ctx, state) + require.Len(t, checks, 3) + require.ErrorIs(t, checks[0].Err, env.ErrNoHomeEnv) + assert.Nil(t, checks[0].Drift) + assert.NoError(t, checks[1].Err) + assert.Nil(t, checks[1].Drift) + assert.NoError(t, checks[2].Err) + require.NotNil(t, checks[2].Drift) + assert.Equal(t, mcp.DriftHostChanged, checks[2].Drift.Kind) +} - drifts, err := mcp.Reconcile(ctx, state) - require.ErrorIs(t, err, env.ErrNoHomeEnv) - assert.Contains(t, err.Error(), "MCP server github-home: ") - assert.Equal(t, []mcp.Drift{{ - Kind: mcp.DriftHostChanged, - Entry: changed, - CurrentHost: bindingTestHost, - CurrentWorkspaceID: stateTestWorkspaceID, - }}, drifts) +// TestCheckEntriesDefaultSectionWithoutKeysIsMissing checks that a profile named DEFAULT is missing when the file has no +// [DEFAULT] keys. The INI parser always has a DEFAULT section, so an empty one is not a section with no host. +func TestCheckEntriesDefaultSectionWithoutKeysIsMissing(t *testing.T) { + ctx, _ := stateTestCtx(t) + path := bindingTestWriteConfig(t, "[logfood]\nhost = https://adb-1.test\n") + entry := stateTestEntry() + entry.Profile = "DEFAULT" + entry.ConfigFile = path + + drifts, err := driftsOf(mcp.CheckEntries(ctx, &mcp.State{Servers: []mcp.Entry{entry}})) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) } diff --git a/libs/aitools/mcpconfig/abspath.go b/libs/aitools/mcpconfig/abspath.go new file mode 100644 index 00000000000..975520d897e --- /dev/null +++ b/libs/aitools/mcpconfig/abspath.go @@ -0,0 +1,32 @@ +package mcpconfig + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +// 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 + } + wd, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("failed to determine working directory: %w", err) + } + return joinWorkingDir(path, wd), nil +} + +// joinWorkingDir joins a relative path to wd. A root-relative path, one that starts with a separator but has no volume +// (such as "\claude" on Windows), is on the volume of wd, so it is joined to that volume, not to the whole directory. +func joinWorkingDir(path, wd string) string { + if strings.HasPrefix(path, string(filepath.Separator)) { + return filepath.VolumeName(wd) + path + } + return wd + string(filepath.Separator) + path +} diff --git a/libs/aitools/mcpconfig/abspath_test.go b/libs/aitools/mcpconfig/abspath_test.go new file mode 100644 index 00000000000..265199f995c --- /dev/null +++ b/libs/aitools/mcpconfig/abspath_test.go @@ -0,0 +1,54 @@ +package mcpconfig + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAbsoluteAsWrittenKeepsAbsolutePaths(t *testing.T) { + abs := filepath.Join(t.TempDir(), "config.json") + got, err := AbsoluteAsWritten(abs) + require.NoError(t, err) + assert.Equal(t, abs, got) +} + +func TestAbsoluteAsWrittenJoinsRelativePathToWorkingDirectory(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) + got, err := AbsoluteAsWritten("cfg/../.claude.json") + require.NoError(t, err) + // Not cleaned: the ".." stays, so that it resolves through a symlink as the OS does. + assert.Equal(t, wd+string(filepath.Separator)+filepath.FromSlash("cfg/../.claude.json"), got) +} + +// TestJoinWorkingDirKeepsTheVolumeOfRootRelativePaths checks a path that starts with a separator but has no volume, such as +// "\claude" on Windows. It is on the working directory's volume, not under the whole directory. The expectations use +// filepath only, so the table means the same on every OS. +func TestJoinWorkingDirKeepsTheVolumeOfRootRelativePaths(t *testing.T) { + wd := filepath.FromSlash("C:/work/project") + cases := []struct { + name string + path string + want string + }{ + { + name: "relative path joins the working directory", + path: "cfg/../.claude.json", + want: wd + string(filepath.Separator) + filepath.FromSlash("cfg/../.claude.json"), + }, + { + name: "root-relative path takes the working directory's volume", + path: "/claude", + want: filepath.VolumeName(wd) + filepath.FromSlash("/claude"), + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, joinWorkingDir(filepath.FromSlash(tc.path), wd)) + }) + } +}