From 28a42301145a1e6cc074cb54d04a36c29bba64bd Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 12:39:50 +0000 Subject: [PATCH 01/87] Add aitools mcp login, logout and list --check Provider sign-in for the external MCP services (GitHub, Slack and others). login opens the sign-in page, prints its URL first and polls the caller's credential until it is ACTIVE. logout deletes the credential and treats a missing one as success. add gains --login and --no-login, and list gains --check, which mints a token per profile, reads sign-in state and checks the recorded binary. The testserver serves the user-credentials GET, POST and DELETE routes. A POST completes a sign-in, which the acceptance scripts use in place of the browser. Co-authored-by: Isaac --- .../cmd/aitools/mcp/list-check/.databrickscfg | 3 + .../cmd/aitools/mcp/list-check/out.test.toml | 1 + .../cmd/aitools/mcp/list-check/output.txt | 88 +++++++ acceptance/cmd/aitools/mcp/list-check/script | 22 ++ .../mcp/login-flips-active/.databrickscfg | 3 + .../mcp/login-flips-active/out.test.toml | 1 + .../aitools/mcp/login-flips-active/output.txt | 91 +++++++ .../cmd/aitools/mcp/login-flips-active/script | 34 +++ .../aitools/mcp/login-flips-active/test.toml | 27 ++ cmd/aitools/mcp.go | 2 + cmd/aitools/mcp_add.go | 42 ++- cmd/aitools/mcp_add_test.go | 5 + cmd/aitools/mcp_list.go | 122 ++++++++- cmd/aitools/mcp_login.go | 248 ++++++++++++++++++ cmd/aitools/mcp_login_test.go | 23 ++ cmd/aitools/mcp_test.go | 2 +- libs/aitools/mcp/discovery.go | 10 + libs/aitools/mcp/login.go | 119 +++++++++ libs/aitools/mcp/login_test.go | 211 +++++++++++++++ libs/testserver/fake_workspace.go | 3 + libs/testserver/handlers.go | 12 + libs/testserver/mcp_services.go | 15 ++ 22 files changed, 1077 insertions(+), 7 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/list-check/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/list-check/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/list-check/output.txt create mode 100644 acceptance/cmd/aitools/mcp/list-check/script create mode 100644 acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/login-flips-active/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/login-flips-active/output.txt create mode 100644 acceptance/cmd/aitools/mcp/login-flips-active/script create mode 100644 acceptance/cmd/aitools/mcp/login-flips-active/test.toml create mode 100644 cmd/aitools/mcp_login.go create mode 100644 cmd/aitools/mcp_login_test.go create mode 100644 libs/aitools/mcp/login.go create mode 100644 libs/aitools/mcp/login_test.go diff --git a/acceptance/cmd/aitools/mcp/list-check/.databrickscfg b/acceptance/cmd/aitools/mcp/list-check/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-check/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/list-check/out.test.toml b/acceptance/cmd/aitools/mcp/list-check/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-check/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/list-check/output.txt b/acceptance/cmd/aitools/mcp/list-check/output.txt new file mode 100644 index 00000000000..8ec864d8256 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -0,0 +1,88 @@ + +=== Install a service that needs no sign-in and one that does + +>>> [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 + +=== list --check: the token mints, and GitHub needs sign-in + +>>> [CLI] aitools mcp list --check +Profile logfood ([DATABRICKS_URL]) token ok + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + github-logfood system.ai.github Claude Code sign-in needed + +=== Once the sign-in completes, GitHub is signed in + +>>> [CLI] api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json {} + +>>> [CLI] aitools mcp list --check +Profile logfood ([DATABRICKS_URL]) token ok + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code + github-logfood system.ai.github Claude Code signed in + +=== A recorded binary that is gone is reported as missing + +>>> [CLI] aitools mcp list --check --output json +{ + "profiles": [ + { + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "status": "ok", + "servers": [ + { + "name": "genie_one_mcp-logfood", + "service": "system.ai.genie_one_mcp", + "sign_in_required": false, + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "modified" + } + ], + "check": { + "token": "ok", + "binary": "failed" + } + }, + { + "name": "github-logfood", + "service": "system.ai.github", + "sign_in_required": true, + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ], + "check": { + "token": "ok", + "sign_in": "ok", + "binary": "ok" + } + } + ] + } + ], + "unmanaged": [] +} + +=== logout ends the sign-in; list --check shows it needed again + +>>> [CLI] aitools mcp logout system.ai.github --profile logfood +Signed out of system.ai.github on profile logfood. + +>>> [CLI] aitools mcp list --check +Profile logfood ([DATABRICKS_URL]) token ok + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code (modified) binary missing + github-logfood system.ai.github Claude Code sign-in needed diff --git a/acceptance/cmd/aitools/mcp/list-check/script b/acceptance/cmd/aitools/mcp/list-check/script new file mode 100644 index 00000000000..fdf6b16e7a7 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-check/script @@ -0,0 +1,22 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +title "Install a service that needs no sign-in and one that does" +trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code + +title "list --check: the token mints, and GitHub needs sign-in" +trace $CLI aitools mcp list --check + +title "Once the sign-in completes, GitHub is signed in" +trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null +trace $CLI aitools mcp list --check + +title "A recorded binary that is gone is reported as missing" +jq '(.servers[] | select(.service == "system.ai.genie_one_mcp") | .command) = "/nonexistent/databricks"' home/.databricks/aitools/mcp/state.json > state.tmp && mv state.tmp home/.databricks/aitools/mcp/state.json +trace $CLI aitools mcp list --check --output json + +title "logout ends the sign-in; list --check shows it needed again" +trace $CLI aitools mcp logout system.ai.github --profile logfood +trace $CLI aitools mcp list --check diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg b/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/out.test.toml b/acceptance/cmd/aitools/mcp/login-flips-active/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt new file mode 100644 index 00000000000..8add9dc2180 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -0,0 +1,91 @@ + +=== A service that needs no sign-in exits 0 and says so + +>>> [CLI] aitools mcp login system.ai.genie_one_mcp --profile logfood +system.ai.genie_one_mcp does not need sign-in. + +=== login prints the sign-in URL and waits; the sign-in completes in the browser + +>>> retry --until Waiting for sign-in cat login.txt +Sign in with the same account you use for Databricks profile logfood. +Open this URL to sign in to system.ai.github: +[DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] +Waiting for sign-in... + +>>> [CLI] api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json {} + +>>> cat login.txt +Sign in with the same account you use for Databricks profile logfood. +Open this URL to sign in to system.ai.github: +[DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] +Waiting for sign-in... +Signed in to system.ai.github on profile logfood. +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector + +=== login again when already signed in returns at once + +>>> [CLI] aitools mcp login system.ai.github --profile logfood --output json +Sign in with the same account you use for Databricks profile logfood. +Signed in to system.ai.github on profile logfood. +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector +{ + "service": "system.ai.github", + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "state": "ACTIVE", + "url": "[DATABRICKS_URL]/mcp-service-login?name=system.ai.github\u0026o=[WORKSPACE_ID]" +} + +=== logout removes the sign-in; a second logout finds nothing and still exits 0 + +>>> [CLI] aitools mcp logout system.ai.github --profile logfood +Signed out of system.ai.github on profile logfood. + +>>> [CLI] aitools mcp logout system.ai.github --profile logfood --output json +Not signed in to system.ai.github on profile logfood. +{ + "service": "system.ai.github", + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "state": "NOT_SIGNED_IN" +} + +=== add --login installs the server and signs in during the same run + +>>> retry --until Waiting for sign-in cat add.txt +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code + +github-logfood system.ai.github + claude-code installed [TEST_TMP_DIR]/home/.claude.json +Sign in with the same account you use for Databricks profile logfood. +Open this URL to sign in to system.ai.github: +[DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] +Waiting for sign-in... + +>>> [CLI] api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json {} + +>>> cat add.txt +Profile logfood ([DATABRICKS_URL]) +Agents (named with --agents): claude-code + +github-logfood system.ai.github + claude-code installed [TEST_TMP_DIR]/home/.claude.json +Sign in with the same account you use for Databricks profile logfood. +Open this URL to sign in to system.ai.github: +[DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] +Waiting for sign-in... +Signed in to system.ai.github on profile logfood. +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector + +>>> jq [.servers[] | {name, service, profile, agents: (.agents | keys)}] home/.databricks/aitools/mcp/state.json +[ + { + "name": "github-logfood", + "service": "system.ai.github", + "profile": "logfood", + "agents": [ + "claude-code" + ] + } +] diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/script b/acceptance/cmd/aitools/mcp/login-flips-active/script new file mode 100644 index 00000000000..36796fbbdaa --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/script @@ -0,0 +1,34 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg +# No browser: the CLI prints the sign-in URL and polls. +export BROWSER=none + +title "A service that needs no sign-in exits 0 and says so" +trace $CLI aitools mcp login system.ai.genie_one_mcp --profile logfood + +title "login prints the sign-in URL and waits; the sign-in completes in the browser" +$CLI aitools mcp login system.ai.github --profile logfood > login.txt 2>&1 & +PID=$! +trace retry --until "Waiting for sign-in" cat login.txt +trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null +wait $PID +trace cat login.txt + +title "login again when already signed in returns at once" +trace $CLI aitools mcp login system.ai.github --profile logfood --output json + +title "logout removes the sign-in; a second logout finds nothing and still exits 0" +trace $CLI aitools mcp logout system.ai.github --profile logfood +trace $CLI aitools mcp logout system.ai.github --profile logfood --output json + +title "add --login installs the server and signs in during the same run" +$CLI aitools mcp add --profile logfood --names system.ai.github --agents claude-code --login > add.txt 2>&1 & +PID=$! +trace retry --until "Waiting for sign-in" cat add.txt +trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null +wait $PID +trace cat add.txt +trace jq '[.servers[] | {name, service, profile, agents: (.agents | keys)}]' home/.databricks/aitools/mcp/state.json +rm login.txt add.txt diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/test.toml b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml new file mode 100644 index 00000000000..a76a55565f9 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml @@ -0,0 +1,27 @@ +# github needs sign-in and genie_one_mcp does not. The sign-in is completed by the script, which +# creates the credential on the fake while login waits for it. +[[Server]] +Pattern = "GET /api/2.1/unity-catalog/mcp-services/system.ai.github" +Response.Body = ''' +{ + "name": "mcp-services/system.ai.github", + "comment": "Repositories, issues, pull requests", + "config": { + "source_connection": { + "name": "connections/github", + "options": { + "oauth_provider": "github" + } + } + } +} +''' + +[[Server]] +Pattern = "GET /api/2.1/unity-catalog/mcp-services/system.ai.genie_one_mcp" +Response.Body = ''' +{ + "name": "mcp-services/system.ai.genie_one_mcp", + "comment": "Ask questions about your data" +} +''' diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index e2db6d77a2d..aa2012f379c 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -42,6 +42,8 @@ Use 'add' to install them into coding agents, bound to a Databricks profile.`, cmd.AddCommand(NewMcpAddCmd()) cmd.AddCommand(NewMcpListCmd()) cmd.AddCommand(NewMcpRemoveCmd()) + cmd.AddCommand(NewMcpLoginCmd()) + cmd.AddCommand(NewMcpLogoutCmd()) cmd.AddCommand(NewMcpServeCmd()) return cmd } diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 550d00cd2b7..eeeb358ae39 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "path/filepath" "slices" "strings" @@ -44,6 +45,8 @@ type mcpAddFlags struct { agents string name string force bool + login bool + noLogin bool } // addOutput is the `mcp add --output json` document. @@ -110,6 +113,8 @@ type addRequest struct { // refused holds the agents named with --agents that are not installed, with the reason. Their // config is not written. refused map[*agents.Agent]error + // login signs in to the sign-in-required servers after they are installed. + login bool } // slashPaths rewrites the agent config paths of the document to forward slashes, as the text output @@ -177,6 +182,8 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to install for, comma-separated (default: every detected MCP-capable agent)") cmd.Flags().StringVar(&f.name, "name", "", "Server name to use instead of the generated one; only with exactly one service") cmd.Flags().BoolVar(&f.force, "force", false, "Overwrite an existing server with the same name that this CLI did not write") + cmd.Flags().BoolVar(&f.login, "login", false, "Sign in to each sign-in-required server right after it is installed") + cmd.Flags().BoolVar(&f.noLogin, "no-login", false, "Do not sign in; print the login commands for sign-in-required servers (default)") return cmd } @@ -304,6 +311,7 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode configFile: configFile, pinned: pinned, refused: refused, + login: f.login, } if pinned != "" { req.env = map[string]string{mcpconfig.ConfigFileEnv: pinned} @@ -325,6 +333,11 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if recordErr != nil { return recordErr } + if req.login { + if err := signInInstalled(ctx, cmd.ErrOrStderr(), w, req, plans, servers); err != nil { + return err + } + } return agentFailureError(req, servers) } @@ -334,6 +347,9 @@ func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames [ if f.agents != "" && len(agentNames) == 0 { return failure(protos.AitoolsErrorCategoryInvalidFlags, "--agents names no agent; pass one or more of: %s", strings.Join(mcpAgentNames(), ", ")) } + if f.login && f.noLogin { + return failure(protos.AitoolsErrorCategoryInvalidFlags, "--login and --no-login cannot be used together") + } if f.name != "" { // A name must come out of mcp.Sanitize unchanged, so that mcp____ splits the same way as // a generated name. @@ -1013,6 +1029,30 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { return failure(protos.AitoolsErrorCategoryUnknownAgent, "%s is not installed; install it first or leave it out of --agents", a.DisplayName) } +// signInInstalled signs in to each sign-in-required server that reached an agent, in order. It stops +// at the first failure; the servers are installed either way. +func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, req addRequest, plans []addPlan, servers []addServerOutput) error { + for i, server := range servers { + if !server.SignInRequired || !installedAnywhere(server) { + continue + } + if _, err := signIn(ctx, errOut, w, req.profile, req.workspaceID, plans[i].service); err != nil { + return err + } + } + return nil +} + +// installedAnywhere reports whether the server reached at least one agent. +func installedAnywhere(server addServerOutput) bool { + for _, a := range server.Agents { + if a.Status != statusFailed { + return true + } + } + return false +} + // recordInstalls adds each server that reached at least one agent to the state file. The file is // re-read first, so entries written since planning are kept. Each server is recorded on its own, so that one // failure does not keep the others from being saved: their agent files are already written. @@ -1145,7 +1185,7 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut cursorServers = append(cursorServers, server.Name) } } - if server.SignInRequired && succeeded { + if server.SignInRequired && succeeded && !req.login { cmdio.LogString(ctx, fmt.Sprintf(" %s needs sign-in. Run: databricks aitools mcp login %s --profile %s", server.Service, shellquote.BashArg(server.Service), shellquote.BashArg(req.profile))) } if server.note != "" && succeeded { diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index bfa3aa80e8b..ac5eafc918c 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -67,6 +67,11 @@ func TestValidateMCPAddFlags(t *testing.T) { want: `--name "a-" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, }, {name: "valid name", flags: mcpAddFlags{name: "my_github-1"}, names: []string{"github"}}, + { + name: "login and no-login conflict", flags: mcpAddFlags{login: true, noLogin: true}, names: []string{"github"}, + want: "--login and --no-login cannot be used together", + }, + {name: "login alone", flags: mcpAddFlags{login: true}, names: []string{"github"}}, { name: "name must be lowercase", flags: mcpAddFlags{name: "GitHub"}, names: []string{"github"}, want: `--name "GitHub" is not a valid server name: use lowercase letters, digits, '-' and '_', with no "__" and no leading or trailing '-' or '_'`, diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index d1f25b565c2..1f3ff87af88 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "maps" + "os" + "os/exec" "path/filepath" "slices" "strings" @@ -18,9 +20,18 @@ import ( "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) +// Results of the `mcp list --check` checks that are not server statuses. They are the JSON values of SPEC 4.6; +// statusOK and statusUnknown carry the other two. +const ( + checkFailed = "failed" + checkNeeded = "needed" +) + // 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 ( @@ -64,6 +75,9 @@ type mcpListProfile struct { configPath string // noHost is set when Status is profile_missing because the profile's section has no host. noHost bool + // Token is the --check result of minting a token for the profile. The JSON carries it in each server's check, + // so it is text only. + Token string `json:"-"` } type mcpListServer struct { @@ -74,12 +88,21 @@ type mcpListServer struct { SignInRequired bool `json:"sign_in_required"` Status string `json:"status"` Agents []mcpListAgent `json:"agents"` + // Check is the --check result of the server. It is nil without --check, and for a profile that drifted. + Check *mcpListCheck `json:"check,omitempty"` // 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 } +// mcpListCheck is the --check result of one server. SignIn is set only for sign-in-required servers. +type mcpListCheck struct { + Token string `json:"token"` + SignIn string `json:"sign_in,omitempty"` + Binary string `json:"binary"` +} + type mcpListAgent struct { Name string `json:"name"` Path string `json:"path"` @@ -106,7 +129,8 @@ type profileKey struct { // 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{ + var check bool + cmd := &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 @@ -126,8 +150,9 @@ Each agent of a server has one of these statuses. The JSON output uses the same --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.`, +Without --check this command makes no network calls. With --check, each profile's token is minted, the +sign-in state of each sign-in-required server is read, and the recorded binary is checked. It always exits 0 +when entries have drifted or cannot be checked; an entry or agent config that cannot be checked is a warning.`, Example: ` databricks aitools mcp list databricks aitools mcp list --profile logfood databricks aitools mcp list --output json`, @@ -136,6 +161,9 @@ an entry or agent config that cannot be checked is a warning.`, ctx := cmd.Context() profileName := profileFlag(cmd) out, err := buildMcpList(ctx, profileName) + if check { + addChecks(ctx, &out) + } if !installOutputIsJSON(cmd) { if err != nil { return err @@ -157,6 +185,8 @@ an entry or agent config that cannot be checked is a warning.`, return nil }, } + cmd.Flags().BoolVar(&check, "check", false, "Also check tokens, sign-in state and the recorded binary (uses the network)") + return cmd } // buildMcpList reads the state and the agent configs and returns the listing. profileName, when set, lists the @@ -376,6 +406,59 @@ func pinnedEnv(rec mcp.Entry) map[string]string { return map[string]string{mcpconfig.ConfigFileEnv: rec.ConfigFile} } +// addChecks runs the --check tests of SPEC 4.2 for each profile that did not drift: a token mint, the +// sign-in state of each sign-in-required server, and whether the recorded binary exists. +func addChecks(ctx context.Context, out *mcpListOutput) { + for i := range out.Profiles { + p := &out.Profiles[i] + if p.Status != statusOK { + continue + } + w, err := profileClient(ctx, p.Profile, p.configPath) + p.Token = statusOK + if err != nil { + p.Token = checkFailed + } + for j := range p.Servers { + s := &p.Servers[j] + s.Check = &mcpListCheck{Token: p.Token, Binary: binaryStatus(s.entry.Command)} + if s.SignInRequired { + s.Check.SignIn = statusUnknown + if err == nil { + s.Check.SignIn = signInStatus(ctx, w, s.Service) + } + } + } + } +} + +// signInStatus reads the caller's sign-in to a service. Only an ACTIVE credential counts as signed in. +func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, service string) string { + state, err := mcp.CredentialState(ctx, w, service) + switch { + case err != nil: + return statusUnknown + case state == catalog.ProvisioningInfoStateActive: + return statusOK + } + return checkNeeded +} + +// binaryStatus reports whether the recorded command exists: an absolute path on disk, or a bare name +// on PATH. +func binaryStatus(command string) string { + var err error + if filepath.IsAbs(command) { + _, err = os.Stat(command) + } else { + _, err = exec.LookPath(command) + } + if err != nil { + return checkFailed + } + return statusOK +} + // 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 { @@ -421,9 +504,17 @@ func renderMcpList(ctx context.Context, out mcpListOutput, profileName string) { if i > 0 { cmdio.LogString(ctx, "") } - cmdio.LogString(ctx, profileHeader(p)) + header := profileHeader(p) + if p.Token != "" { + header += " token " + p.Token + } + cmdio.LogString(ctx, header) for _, s := range p.Servers { - cmdio.LogString(ctx, fmt.Sprintf(" %-24s %-30s %s", s.Name, s.Service, listAgentsText(s.Agents))) + line := fmt.Sprintf(" %-24s %-30s %s", s.Name, s.Service, listAgentsText(s.Agents)) + if text := checkText(s.Check); text != "" { + line += " " + text + } + cmdio.LogString(ctx, line) if hint := serverHint(ctx, p, s); hint != "" { cmdio.LogString(ctx, " "+hint) } @@ -470,6 +561,27 @@ func profileHeader(p mcpListProfile) string { return fmt.Sprintf("Profile %s (%s)", p.Profile, p.Host) } +// checkText is the trailing text of a server line for the --check results that need attention or +// say something about the server. +func checkText(c *mcpListCheck) string { + if c == nil { + return "" + } + var parts []string + switch c.SignIn { + case statusOK: + parts = append(parts, "signed in") + case checkNeeded: + parts = append(parts, "sign-in needed") + case statusUnknown: + parts = append(parts, "sign-in unknown") + } + if c.Binary == checkFailed { + parts = append(parts, "binary missing") + } + return strings.Join(parts, ", ") +} + // 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 { diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go new file mode 100644 index 00000000000..951c1e1c808 --- /dev/null +++ b/cmd/aitools/mcp_login.go @@ -0,0 +1,248 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "io" + "slices" + "strings" + + "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/auth" + "github.com/databricks/cli/libs/browser" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/spf13/cobra" +) + +// categorySignInTimeout is the error category of a sign-in that did not finish in time. The +// telemetry enum does not define it yet. +const categorySignInTimeout protos.AitoolsErrorCategory = "SIGN_IN_TIMEOUT" + +// Sign-in states of `mcp login` and `mcp logout`, reported in JSON. +const ( + signInStateActive = "ACTIVE" + signInStateTimeout = "TIMEOUT" + signInStateFailed = "FAILED" + signInStateNotRequired = "NOT_REQUIRED" + signOutStateDeleted = "DELETED" + signOutStateNotSignedIn = "NOT_SIGNED_IN" +) + +// mcpSignInOutput is the `mcp login` and `mcp logout` document. +type mcpSignInOutput struct { + Service string `json:"service"` + Profile string `json:"profile"` + Host string `json:"host"` + State string `json:"state"` + URL string `json:"url,omitempty"` + ErrorCategory protos.AitoolsErrorCategory `json:"error_category,omitempty"` + Error string `json:"error,omitempty"` + + // configFile is the resolved config file that the profile is read from. It is not in the JSON. + configFile string +} + +// mcpSignInAction runs login or logout against the profile's client and records the result in out. +type mcpSignInAction func(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error + +// NewMcpLoginCmd returns `aitools mcp login`. +func NewMcpLoginCmd() *cobra.Command { + return &cobra.Command{ + Use: "login ", + Short: "Sign in to a Databricks managed MCP service with your account", + Long: `Sign in to a Databricks managed MCP service that uses your account with an external provider, such +as GitHub. The sign-in page opens in the browser. The command waits for up to five minutes for the +sign-in to complete. Without a browser, open the printed URL yourself. + + is a full name such as system.ai.github. The profile is the one whose user signs in.`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runMcpSignInCommand(cmd, args[0], mcpLogin) + }, + } +} + +// NewMcpLogoutCmd returns `aitools mcp logout`. +func NewMcpLogoutCmd() *cobra.Command { + return &cobra.Command{ + Use: "logout ", + Short: "Remove your sign-in to a Databricks managed MCP service", + Long: `Remove your sign-in to a Databricks managed MCP service on a profile. The install of the server +stays in place. Running it when you are not signed in is not an error. + + is a full name such as system.ai.github.`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runMcpSignInCommand(cmd, args[0], mcpLogout) + }, + } +} + +// runMcpSignInCommand resolves the profile for service and runs action. In JSON mode the document is +// the only stdout output; progress goes to stderr. +func runMcpSignInCommand(cmd *cobra.Command, service string, action mcpSignInAction) error { + ctx := cmd.Context() + jsonMode := installOutputIsJSON(cmd) + if jsonMode { + ctx = cmdio.WithQuiet(ctx) + } + + out := &mcpSignInOutput{Service: service} + runErr := runMcpSignIn(ctx, cmd.ErrOrStderr(), profileFlag(cmd), service, action, out) + if !jsonMode { + return runErr + } + if runErr != nil { + out.Error = runErr.Error() + out.ErrorCategory = errorCategory(runErr) + } + if err := renderJSON(cmd.OutOrStdout(), out); err != nil { + return err + } + if runErr != nil { + return root.ErrAlreadyPrinted + } + return nil +} + +func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service string, action mcpSignInAction, out *mcpSignInOutput) error { + if parts := strings.Split(service, "."); len(parts) != 3 || slices.Contains(parts, "") { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q", service) + } + // The config file is resolved once, as add resolves it, so that the profile lookup and the client read the same file. + configFile, _, err := addConfigFile(ctx) + if err != nil { + return err + } + ctx = env.Set(ctx, mcpconfig.ConfigFileEnv, configFile) + profileName, err := resolveProfile(ctx, flagValue) + if err != nil { + return err + } + if err := requireProfile(ctx, profileName); err != nil { + return err + } + w, err := profileClient(ctx, profileName, configFile) + if err != nil { + return err + } + out.Profile = profileName + out.Host = w.Config.Host + out.configFile = configFile + return action(ctx, errOut, w, out) +} + +func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { + svc, err := mcp.GetService(ctx, w, out.Service) + if errors.Is(err, apierr.ErrNotFound) { + return failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", out.Service, out.Profile) + } + if err != nil { + return err + } + if !svc.SignInRequired { + out.State = signInStateNotRequired + fmt.Fprintf(errOut, "%s does not need sign-in.\n", svc.FullName) + return nil + } + + workspaceID, err := signInWorkspaceID(ctx, w, out.configFile, out.Profile, svc.FullName) + if err != nil { + return err + } + out.URL = mcp.SignInURL(out.Host, svc.FullName, workspaceID) + state, err := signIn(ctx, errOut, w, out.Profile, workspaceID, svc) + out.State = state + return err +} + +func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { + deleted, err := mcp.SignOut(ctx, w, out.Service) + if err != nil { + return err + } + if !deleted { + out.State = signOutStateNotSignedIn + fmt.Fprintf(errOut, "Not signed in to %s on profile %s.\n", out.Service, out.Profile) + return nil + } + out.State = signOutStateDeleted + fmt.Fprintf(errOut, "Signed out of %s on profile %s.\n", out.Service, out.Profile) + return nil +} + +// signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns +// the sign-in state name and the failure, which is categorized for JSON. `add --login` shares it. +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, workspaceID string, svc mcp.Service) (string, error) { + fmt.Fprintf(errOut, "Sign in with the same account you use for Databricks profile %s.\n", profileName) + err := mcp.SignIn(ctx, w, w.Config.Host, workspaceID, svc.FullName, errOut, mcp.SignInOptions{ + Poll: mcp.SignInPollInterval, + Timeout: mcp.SignInWaitTimeout, + Open: browser.Open, + }) + url := mcp.SignInURL(w.Config.Host, svc.FullName, workspaceID) + switch { + case errors.Is(err, mcp.ErrSignInTimeout): + return signInStateTimeout, failure(categorySignInTimeout, err, + "Sign-in to %s did not complete in %g minutes. Finish at %s then retry: databricks aitools mcp login %s --profile %s", + svc.FullName, mcp.SignInWaitTimeout.Minutes(), url, svc.FullName, profileName) + case errors.Is(err, mcp.ErrSignInFailed): + return signInStateFailed, failure(protos.AitoolsErrorCategoryUncategorized, err, + "Sign-in to %s failed on profile %s. Run: databricks aitools mcp login %s --profile %s", + svc.FullName, profileName, svc.FullName, profileName) + case err != nil: + return "", err + } + + fmt.Fprintf(errOut, "Signed in to %s on profile %s.\n", svc.FullName, profileName) + if svc.Known != nil && svc.Known.Note != "" { + fmt.Fprintln(errOut, svc.Known.Note) + } + return signInStateActive, nil +} + +// signInWorkspaceID returns the workspace ID for the sign-in URL: the one recorded when the service +// was installed for the profile in configFile, else the profile's own. +func signInWorkspaceID(ctx context.Context, w *databricks.WorkspaceClient, configFile, profileName, service string) (string, error) { + state, err := mcp.LoadState(ctx) + if err != nil { + return "", err + } + entry, err := mcp.FindEntry(ctx, state, service, profileName, configFile) + if err != nil { + return "", err + } + if entry != nil { + return entry.WorkspaceID, nil + } + workspaceID, err := auth.ResolveWorkspaceID(ctx, w) + if err != nil { + return "", describeRequestError(profileName, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) + } + return workspaceID, nil +} + +// requireProfile fails when the named profile is not in the Databricks config file. +func requireProfile(ctx context.Context, name string) error { + profiler := profile.GetProfiler(ctx) + profiles, err := profiler.LoadProfiles(ctx, profile.WithName(name)) + if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { + return err + } + if len(profiles) > 0 { + return nil + } + path, err := profiler.GetPath(ctx) + if err != nil { + return err + } + return failure(protos.AitoolsErrorCategory(mcp.BindProfileNotFound), nil, "Profile %s not found in %s", name, path) +} diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go new file mode 100644 index 00000000000..ca135c34672 --- /dev/null +++ b/cmd/aitools/mcp_login_test.go @@ -0,0 +1,23 @@ +package aitools + +import ( + "bytes" + "testing" + + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMcpSignInRequiresFullServiceName(t *testing.T) { + for _, service := range []string{"github", "system.github", "system..github", "a.b.c.d"} { + t.Run(service, func(t *testing.T) { + var out mcpSignInOutput + err := runMcpSignIn(cmdio.MockDiscard(t.Context()), &bytes.Buffer{}, "", service, mcpLogin, &out) + require.Error(t, err) + assert.Equal(t, `service must be a full name such as system.ai.github, got "`+service+`"`, err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) + }) + } +} diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go index da2ca7bdc87..3f9635798f7 100644 --- a/cmd/aitools/mcp_test.go +++ b/cmd/aitools/mcp_test.go @@ -32,7 +32,7 @@ func TestMcpCommandIsHidden(t *testing.T) { for _, c := range mcp.Commands() { names = append(names, c.Name()) } - assert.Equal(t, []string{"add", "list", "remove", "serve"}, names) + assert.Equal(t, []string{"add", "list", "login", "logout", "remove", "serve"}, names) for _, c := range NewAitoolsCmd().Commands() { if c.Name() == "mcp" { diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index d20e376c524..8e5510e98d3 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -65,6 +65,16 @@ func Discover(ctx context.Context, w *databricks.WorkspaceClient, location strin return services, nil } +// GetService returns one MCP service by full name, with its sign-in requirement. The API +// answers NOT_FOUND (apierr.ErrNotFound) for a service the profile cannot see. +func GetService(ctx context.Context, w *databricks.WorkspaceClient, fullName string) (Service, error) { + svc, err := w.AiGateway.GetMcpService(ctx, catalog.GetMcpServiceRequest{Name: mcpServicePrefix + fullName}) + if err != nil { + return Service{}, err + } + return newService(svc), nil +} + // ListErrorKind names a listing failure. type ListErrorKind string diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go new file mode 100644 index 00000000000..f99c0e53769 --- /dev/null +++ b/libs/aitools/mcp/login.go @@ -0,0 +1,119 @@ +package mcp + +import ( + "context" + "errors" + "fmt" + "io" + "time" + + "github.com/databricks/cli/libs/browser" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/service/catalog" +) + +// SignInPollInterval and SignInWaitTimeout are the polling settings of provider sign-in (SPEC 12). +const ( + SignInPollInterval = 2 * time.Second + SignInWaitTimeout = 5 * time.Minute +) + +// ErrSignInTimeout means the caller's credential did not become ACTIVE before the wait ended. +var ErrSignInTimeout = errors.New("sign-in did not complete before the timeout") + +// ErrSignInFailed means the credential reached the FAILED provisioning state. +var ErrSignInFailed = errors.New("sign-in failed") + +// SignInOptions controls how SignIn waits. Callers set both durations. +type SignInOptions struct { + Poll time.Duration + Timeout time.Duration + // Open opens the sign-in page. Nil leaves the page to the user. It is not called when + // BROWSER=none, because the URL is already printed. + Open func(ctx context.Context, url string) error +} + +// CredentialState returns the provisioning state of the caller's sign-in to a sign-in-required +// service. It is "" when the caller has not signed in: the API answers NOT_FOUND until then. +func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service string) (catalog.ProvisioningInfoState, error) { + cred, err := w.AiGateway.GetMcpServiceUserMappedCredential(ctx, catalog.GetMcpServiceUserMappedCredentialRequest{ + Name: mcpServicePrefix + service, + }) + if errors.Is(err, apierr.ErrNotFound) { + return "", nil + } + if err != nil { + return "", err + } + if cred.ProvisioningInfo == nil { + return "", nil + } + return cred.ProvisioningInfo.State, nil +} + +// SignIn signs the caller in to a sign-in-required service. It returns nil at once when the +// caller is already signed in. Otherwise it prints the sign-in URL to out, opens it, and polls +// until the credential is ACTIVE. The errors are ErrSignInTimeout, ErrSignInFailed, or an API error. +func SignIn(ctx context.Context, w *databricks.WorkspaceClient, host, workspaceID, service string, out io.Writer, opts SignInOptions) error { + state, err := CredentialState(ctx, w, service) + if err != nil { + return err + } + if state == catalog.ProvisioningInfoStateActive { + return nil + } + + url := SignInURL(host, service, workspaceID) + fmt.Fprintf(out, "Open this URL to sign in to %s:\n%s\n", service, url) + if opts.Open != nil && !browser.IsDisabled(ctx) { + if err := opts.Open(ctx, url); err != nil { + fmt.Fprintf(out, "Could not open a browser (%v). Open the URL above.\n", err) + } + } + fmt.Fprintln(out, "Waiting for sign-in...") + + deadline := time.NewTimer(opts.Timeout) + defer deadline.Stop() + ticker := time.NewTicker(opts.Poll) + defer ticker.Stop() + + // The deadline is a timer, not a context deadline: the SDK rate limiter refuses a request + // whose context would expire before it may run, which would hide the timeout. + for { + select { + case <-ctx.Done(): + return ctx.Err() + case <-deadline.C: + return ErrSignInTimeout + case <-ticker.C: + } + + state, err := CredentialState(ctx, w, service) + if err != nil { + return err + } + // Other states (PROVISIONING, UPDATING, ...) mean the login has not completed yet. + if state == catalog.ProvisioningInfoStateActive { + return nil + } + if state == catalog.ProvisioningInfoStateFailed { + return ErrSignInFailed + } + } +} + +// SignOut deletes the caller's sign-in to a service. It reports false when the caller was not +// signed in, which is not an error. +func SignOut(ctx context.Context, w *databricks.WorkspaceClient, service string) (bool, error) { + _, err := w.AiGateway.DeleteMcpServiceUserMappedCredential(ctx, catalog.DeleteMcpServiceUserMappedCredentialRequest{ + Name: mcpServicePrefix + service, + }) + if errors.Is(err, apierr.ErrNotFound) { + return false, nil + } + if err != nil { + return false, err + } + return true, nil +} diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go new file mode 100644 index 00000000000..ed8441a6fea --- /dev/null +++ b/libs/aitools/mcp/login_test.go @@ -0,0 +1,211 @@ +package mcp + +import ( + "bytes" + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/service/catalog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + loginTestService = "system.ai.github" + loginTestCredPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + loginTestServicePath = "/api/2.1/unity-catalog/mcp-services/system.ai.github" +) + +// loginTestServer answers the credential endpoints from a script: each GET returns the next +// entry of states, where "" means NOT_FOUND, and the last entry repeats. DELETE answers +// deleted (204) or not found (404). +type loginTestServer struct { + mu sync.Mutex + states []string + reads int + deleted bool + // signedIn is what DELETE answers: true for 200, false for 404. + signedIn bool +} + +func (s *loginTestServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + s.mu.Lock() + defer s.mu.Unlock() + rw.Header().Set("Content-Type", "application/json") + switch { + case r.URL.Path == loginTestServicePath && r.Method == http.MethodGet: + _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","config":{"source_connection":{"options":{"oauth_provider":"OAUTH_PROVIDER_GITHUB_MCP"}}}}`) + case r.URL.Path == loginTestCredPath && r.Method == http.MethodGet: + state := s.states[min(s.reads, len(s.states)-1)] + s.reads++ + if state == "" { + http.Error(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`, http.StatusNotFound) + return + } + body, _ := json.Marshal(catalog.McpServiceUserMappedCredential{ + ProvisioningInfo: &catalog.ProvisioningInfo{State: catalog.ProvisioningInfoState(state)}, + }) + _, _ = rw.Write(body) + case r.URL.Path == loginTestCredPath && r.Method == http.MethodDelete: + if !s.signedIn { + http.Error(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`, http.StatusNotFound) + return + } + s.deleted = true + _, _ = io.WriteString(rw, `{}`) + default: + // The SDK resolves host metadata with requests of its own; those are not the endpoints under test. + http.NotFound(rw, r) + } +} + +// loginTestClient starts the fake and returns a client for it. +func loginTestClient(t *testing.T, s *loginTestServer) *databricks.WorkspaceClient { + t.Helper() + server := httptest.NewServer(s) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + return w +} + +func TestSignInPollsUntilActive(t *testing.T) { + s := &loginTestServer{states: []string{"", "PROVISIONING", "ACTIVE"}} + w := loginTestClient(t, s) + + var opened []string + var out bytes.Buffer + err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + Open: func(_ context.Context, url string) error { + opened = append(opened, url) + return nil + }, + }) + require.NoError(t, err) + + wantURL := w.Config.Host + "/mcp-service-login?name=system.ai.github&o=123" + assert.Equal(t, []string{wantURL}, opened) + // The URL is printed before the browser is opened, and polling starts after that. + assert.Contains(t, out.String(), "Open this URL to sign in to system.ai.github:\n"+wantURL+"\n") + assert.Equal(t, 3, s.reads) +} + +func TestSignInAlreadyActiveDoesNotOpenBrowser(t *testing.T) { + s := &loginTestServer{states: []string{"ACTIVE"}} + w := loginTestClient(t, s) + + var out bytes.Buffer + err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + Poll: time.Millisecond, + Timeout: time.Second, + Open: func(context.Context, string) error { + t.Fatal("browser must not open when already signed in") + return nil + }, + }) + require.NoError(t, err) + assert.Empty(t, out.String()) +} + +func TestSignInBrowserNoneOnlyPrintsURL(t *testing.T) { + t.Setenv("BROWSER", "none") + s := &loginTestServer{states: []string{"", "ACTIVE"}} + w := loginTestClient(t, s) + + var out bytes.Buffer + err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + Open: func(context.Context, string) error { + t.Fatal("browser must not open with BROWSER=none") + return nil + }, + }) + require.NoError(t, err) + assert.Equal(t, 1, strings.Count(out.String(), "/mcp-service-login?"), "the URL is printed once") +} + +func TestSignInTimeout(t *testing.T) { + s := &loginTestServer{states: []string{""}} + w := loginTestClient(t, s) + + var out bytes.Buffer + err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + Poll: time.Millisecond, + Timeout: 30 * time.Millisecond, + }) + require.ErrorIs(t, err, ErrSignInTimeout) + assert.Contains(t, out.String(), "/mcp-service-login?name=system.ai.github&o=123") +} + +func TestSignInCancelledIsNotATimeout(t *testing.T) { + s := &loginTestServer{states: []string{""}} + w := loginTestClient(t, s) + + ctx, cancel := context.WithCancel(t.Context()) + cancel() + err := SignIn(ctx, w, w.Config.Host, "123", loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: time.Minute, + }) + require.ErrorIs(t, err, context.Canceled) + assert.NotErrorIs(t, err, ErrSignInTimeout) +} + +func TestSignInFailed(t *testing.T) { + s := &loginTestServer{states: []string{"", "FAILED"}} + w := loginTestClient(t, s) + + err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + }) + require.ErrorIs(t, err, ErrSignInFailed) +} + +func TestCredentialStateNotSignedIn(t *testing.T) { + s := &loginTestServer{states: []string{""}} + w := loginTestClient(t, s) + + state, err := CredentialState(t.Context(), w, loginTestService) + require.NoError(t, err) + assert.Empty(t, state) +} + +func TestSignOut(t *testing.T) { + s := &loginTestServer{signedIn: true} + w := loginTestClient(t, s) + + deleted, err := SignOut(t.Context(), w, loginTestService) + require.NoError(t, err) + assert.True(t, deleted) + assert.True(t, s.deleted) + + // Once signed out, the server answers NOT_FOUND, which is not an error. + s.signedIn = false + deleted, err = SignOut(t.Context(), w, loginTestService) + require.NoError(t, err) + assert.False(t, deleted) +} + +func TestGetServiceSignInRequired(t *testing.T) { + s := &loginTestServer{} + w := loginTestClient(t, s) + + svc, err := GetService(t.Context(), w, loginTestService) + require.NoError(t, err) + assert.True(t, svc.SignInRequired) + assert.Equal(t, "OAUTH_PROVIDER_GITHUB_MCP", svc.OAuthProvider) + require.NotNil(t, svc.Known) + assert.Equal(t, "GitHub", svc.Known.Label) +} diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index 5e8068b6d02..455104c69ed 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -247,6 +247,8 @@ type FakeWorkspace struct { RegisteredModels map[string]catalog.RegisteredModelInfo ModelServices map[string]catalog.ModelService McpServices map[string]catalog.McpService + // McpUserCredentials holds each caller's sign-in to an MCP service, keyed by the service full name. + McpUserCredentials map[string]catalog.McpServiceUserMappedCredential ModelProviderServices map[string]catalog.ModelProviderService // RegisteredModelVersions counts the versions registered on each model, keyed by its full name. @@ -544,6 +546,7 @@ func NewFakeWorkspace(url, token string) *FakeWorkspace { RegisteredModels: map[string]catalog.RegisteredModelInfo{}, ModelServices: map[string]catalog.ModelService{}, McpServices: map[string]catalog.McpService{}, + McpUserCredentials: map[string]catalog.McpServiceUserMappedCredential{}, ModelProviderServices: map[string]catalog.ModelProviderService{}, Features: map[string]ml.Feature{}, MaterializationSets: map[string]ml.MaterializationSet{}, diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index a5dbe998353..56f2fe1351d 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -937,6 +937,18 @@ func AddDefaultHandlers(server *Server) { return req.Workspace.McpServicesUpdate(req, req.Vars["name"]) }) + server.Handle("GET", "/api/2.1/unity-catalog/mcp-services/{name}/user-credentials", func(req Request) any { + return MapGet(req.Workspace, req.Workspace.McpUserCredentials, req.Vars["name"]) + }) + + server.Handle("POST", "/api/2.1/unity-catalog/mcp-services/{name}/user-credentials", func(req Request) any { + return req.Workspace.McpUserCredentialCreate(req.Vars["name"]) + }) + + server.Handle("DELETE", "/api/2.1/unity-catalog/mcp-services/{name}/user-credentials", func(req Request) any { + return MapDelete(req.Workspace, req.Workspace.McpUserCredentials, req.Vars["name"]) + }) + server.Handle("DELETE", "/api/2.1/unity-catalog/mcp-services/{name}", func(req Request) any { return MapDelete(req.Workspace, req.Workspace.McpServices, req.Vars["name"]) }) diff --git a/libs/testserver/mcp_services.go b/libs/testserver/mcp_services.go index 0286250dc11..4006f058ae8 100644 --- a/libs/testserver/mcp_services.go +++ b/libs/testserver/mcp_services.go @@ -103,3 +103,18 @@ func (s *FakeWorkspace) McpServicesList(req Request) Response { Body: catalog.ListMcpServicesResponse{McpServices: services}, } } + +// McpUserCredentialCreate fakes POST .../mcp-services/{name}/user-credentials, which completes a +// caller's provider sign-in. Tests use it to flip a credential from absent to ACTIVE while the CLI +// polls. The login body is not checked. +func (s *FakeWorkspace) McpUserCredentialCreate(service string) Response { + defer s.LockUnlock()() + + cred := catalog.McpServiceUserMappedCredential{ + ProvisioningInfo: &catalog.ProvisioningInfo{State: catalog.ProvisioningInfoStateActive}, + } + s.McpUserCredentials[service] = cred + return Response{ + Body: cred, + } +} From f0a9662409ab58885f37b20649108180c6573ee9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:40:28 +0000 Subject: [PATCH 02/87] Create the Claude Code config dir in the list-check and login acceptance cases mcp add now refuses an agent named with --agents whose config directory is missing. These two cases add for claude-code in a fresh HOME, so they create the directory first. Their goldens do not change. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/list-check/script | 1 + acceptance/cmd/aitools/mcp/login-flips-active/script | 1 + 2 files changed, 2 insertions(+) diff --git a/acceptance/cmd/aitools/mcp/list-check/script b/acceptance/cmd/aitools/mcp/list-check/script index fdf6b16e7a7..79b1bcd5543 100644 --- a/acceptance/cmd/aitools/mcp/list-check/script +++ b/acceptance/cmd/aitools/mcp/list-check/script @@ -1,5 +1,6 @@ # Isolate HOME so the agent config files and the state file land in this test directory. sethome home +mkdir -p home/.claude envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/script b/acceptance/cmd/aitools/mcp/login-flips-active/script index 36796fbbdaa..e0ab1741569 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/script +++ b/acceptance/cmd/aitools/mcp/login-flips-active/script @@ -1,5 +1,6 @@ # Isolate HOME so the agent config files and the state file land in this test directory. sethome home +mkdir -p home/.claude envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg # No browser: the CLI prints the sign-in URL and polls. From 8965233603815f5c32f08e523ba0c8311caaf704 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:40:29 +0000 Subject: [PATCH 03/87] Update the public-repository note in the login acceptance golden The note ends with a period since "Tidy MCP discovery comments, notes and error mapping" on the discovery branch. The stack now includes that commit, so the golden shows the period. Nothing else in the output changes. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/login-flips-active/output.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt index 8add9dc2180..087daf4541c 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -20,14 +20,14 @@ Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] Waiting for sign-in... Signed in to system.ai.github on profile logfood. -Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. === login again when already signed in returns at once >>> [CLI] aitools mcp login system.ai.github --profile logfood --output json Sign in with the same account you use for Databricks profile logfood. Signed in to system.ai.github on profile logfood. -Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. { "service": "system.ai.github", "profile": "logfood", @@ -76,7 +76,7 @@ Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] Waiting for sign-in... Signed in to system.ai.github on profile logfood. -Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. >>> jq [.servers[] | {name, service, profile, agents: (.agents | keys)}] home/.databricks/aitools/mcp/state.json [ From 3095413738ad65b38a744a7140f0d9a30b3dddf3 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:12:55 +0000 Subject: [PATCH 04/87] Read the MCP state from the shared skills state file in login acceptance Card 03 moved MCP records into the `mcp` section of ~/.databricks/aitools/skills/.state.json. The list-check and login-flips-active scripts read or edited the old path. The login golden changes only in the echoed jq command. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/list-check/script | 2 +- acceptance/cmd/aitools/mcp/login-flips-active/output.txt | 2 +- acceptance/cmd/aitools/mcp/login-flips-active/script | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/list-check/script b/acceptance/cmd/aitools/mcp/list-check/script index 79b1bcd5543..35b1a07dc79 100644 --- a/acceptance/cmd/aitools/mcp/list-check/script +++ b/acceptance/cmd/aitools/mcp/list-check/script @@ -15,7 +15,7 @@ trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-cr trace $CLI aitools mcp list --check title "A recorded binary that is gone is reported as missing" -jq '(.servers[] | select(.service == "system.ai.genie_one_mcp") | .command) = "/nonexistent/databricks"' home/.databricks/aitools/mcp/state.json > state.tmp && mv state.tmp home/.databricks/aitools/mcp/state.json +jq '(.mcp.servers[] | select(.service == "system.ai.genie_one_mcp") | .command) = "/nonexistent/databricks"' home/.databricks/aitools/skills/.state.json > state.tmp && mv state.tmp home/.databricks/aitools/skills/.state.json trace $CLI aitools mcp list --check --output json title "logout ends the sign-in; list --check shows it needed again" diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt index 087daf4541c..413d2d7830d 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -78,7 +78,7 @@ Waiting for sign-in... Signed in to system.ai.github on profile logfood. Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. ->>> jq [.servers[] | {name, service, profile, agents: (.agents | keys)}] home/.databricks/aitools/mcp/state.json +>>> jq .mcp | [.servers[] | {name, service, profile, agents: (.agents | keys)}] home/.databricks/aitools/skills/.state.json [ { "name": "github-logfood", diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/script b/acceptance/cmd/aitools/mcp/login-flips-active/script index e0ab1741569..d30dfbb7bc5 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/script +++ b/acceptance/cmd/aitools/mcp/login-flips-active/script @@ -31,5 +31,5 @@ trace retry --until "Waiting for sign-in" cat add.txt trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null wait $PID trace cat add.txt -trace jq '[.servers[] | {name, service, profile, agents: (.agents | keys)}]' home/.databricks/aitools/mcp/state.json +trace jq '.mcp | [.servers[] | {name, service, profile, agents: (.agents | keys)}]' home/.databricks/aitools/skills/.state.json rm login.txt add.txt From a071943fc16628d49f18d2dafc5782f44947c9ed Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:37:10 +0000 Subject: [PATCH 05/87] Seed MCP services in the sign-in acceptance tests The sign-in acceptance tests stubbed the service GET with [[Server]] entries, but `add` also lists the services of system.ai, and the stubs do not answer the list. Create the services in the fake workspace from the scripts, as the other MCP acceptance tests do. The sign-in state is kept per workspace, and tests without stubs share one fake workspace per token. Give each sign-in test its own token, so that one test's sign-in cannot end another test's wait. Co-authored-by: Isaac --- .../cmd/aitools/mcp/list-check/.databrickscfg | 2 +- acceptance/cmd/aitools/mcp/list-check/script | 4 +++ .../mcp/login-flips-active/.databrickscfg | 2 +- .../cmd/aitools/mcp/login-flips-active/script | 4 +++ .../aitools/mcp/login-flips-active/test.toml | 27 ------------------- 5 files changed, 10 insertions(+), 29 deletions(-) delete mode 100644 acceptance/cmd/aitools/mcp/login-flips-active/test.toml diff --git a/acceptance/cmd/aitools/mcp/list-check/.databrickscfg b/acceptance/cmd/aitools/mcp/list-check/.databrickscfg index 0369be633c5..18c13702ad5 100644 --- a/acceptance/cmd/aitools/mcp/list-check/.databrickscfg +++ b/acceptance/cmd/aitools/mcp/list-check/.databrickscfg @@ -1,3 +1,3 @@ [logfood] host = $DATABRICKS_HOST -token = test-token-logfood +token = test-token-list-check diff --git a/acceptance/cmd/aitools/mcp/list-check/script b/acceptance/cmd/aitools/mcp/list-check/script index 35b1a07dc79..b20525705ba 100644 --- a/acceptance/cmd/aitools/mcp/list-check/script +++ b/acceptance/cmd/aitools/mcp/list-check/script @@ -4,6 +4,10 @@ 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 serves what the scripts create, as in add-text. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' > /dev/null +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' > /dev/null + title "Install a service that needs no sign-in and one that does" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg b/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg index 0369be633c5..dc8d760a4d3 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg +++ b/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg @@ -1,3 +1,3 @@ [logfood] host = $DATABRICKS_HOST -token = test-token-logfood +token = test-token-login-flips-active diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/script b/acceptance/cmd/aitools/mcp/login-flips-active/script index d30dfbb7bc5..a2bd27ab5ec 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/script +++ b/acceptance/cmd/aitools/mcp/login-flips-active/script @@ -6,6 +6,10 @@ export DATABRICKS_CONFIG_FILE=.databrickscfg # No browser: the CLI prints the sign-in URL and polls. export BROWSER=none +# The services of system.ai, created in the fake workspace. The fake serves what the scripts create, as in add-text. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' > /dev/null +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' > /dev/null + title "A service that needs no sign-in exits 0 and says so" trace $CLI aitools mcp login system.ai.genie_one_mcp --profile logfood diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/test.toml b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml deleted file mode 100644 index a76a55565f9..00000000000 --- a/acceptance/cmd/aitools/mcp/login-flips-active/test.toml +++ /dev/null @@ -1,27 +0,0 @@ -# github needs sign-in and genie_one_mcp does not. The sign-in is completed by the script, which -# creates the credential on the fake while login waits for it. -[[Server]] -Pattern = "GET /api/2.1/unity-catalog/mcp-services/system.ai.github" -Response.Body = ''' -{ - "name": "mcp-services/system.ai.github", - "comment": "Repositories, issues, pull requests", - "config": { - "source_connection": { - "name": "connections/github", - "options": { - "oauth_provider": "github" - } - } - } -} -''' - -[[Server]] -Pattern = "GET /api/2.1/unity-catalog/mcp-services/system.ai.genie_one_mcp" -Response.Body = ''' -{ - "name": "mcp-services/system.ai.genie_one_mcp", - "comment": "Ask questions about your data" -} -''' From db6e2465f4d158b032a36e4a54778b21edf4db27 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:37:11 +0000 Subject: [PATCH 06/87] Update the MCP golden outputs for the list and add flags The add help lists the new --login and --no-login flags. The list output is the one the list rewrite produces: profiles name their config file when their servers are pinned to one, the JSON carries each server's host and status, and an unchanged agent entry is installed even when the recorded binary is gone, which the binary check reports. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/hidden/output.txt | 24 ++++++++++--------- .../cmd/aitools/mcp/list-check/output.txt | 18 +++++++++----- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index fcee4a7cc7a..745775325d6 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -34,7 +34,7 @@ Install Databricks managed MCP servers into coding agents, bound to a Databricks Each server runs through 'databricks aitools mcp serve' with that profile's credentials. Services come from --names: full names such as system.ai.github, or leaf names such as github. Leaf names -resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share when there are no leaf names. +resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share. Agents come from --agents. Without it, every detected MCP-capable agent is used. Interactive selection is not available yet: in a terminal, pass both --names and --agents. @@ -50,15 +50,17 @@ Examples: databricks aitools mcp add --names github,web_search --agents claude-code,cursor --profile dogfood Flags: - --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) - --force[=true|false] Overwrite an existing server with the same name that this CLI did not write - -h, --help[=true|false] help for add - --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share when there are no leaf names) - --name string Server name to use instead of the generated one; only with exactly one service - --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated + --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) + --force Overwrite an existing server with the same name that this CLI did not write + -h, --help help for add + --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share) + --login Sign in to each sign-in-required server right after it is installed + --name string Server name to use instead of the generated one; only with exactly one service + --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated + --no-login Do not sign in; print the login commands for sign-in-required servers (default) Global Flags: - --debug[=true|false] enable debug logging - -o, --output type output type: text or json (default text) - -p, --profile string ~/.databrickscfg profile - -t, --target string bundle target to use (if applicable) + --debug enable debug logging + -o, --output type output type: text or json (default text) + -p, --profile string ~/.databrickscfg profile + -t, --target string bundle target to use (if applicable) diff --git a/acceptance/cmd/aitools/mcp/list-check/output.txt b/acceptance/cmd/aitools/mcp/list-check/output.txt index 8ec864d8256..afc3ebb077a 100644 --- a/acceptance/cmd/aitools/mcp/list-check/output.txt +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -15,7 +15,7 @@ system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.gith === list --check: the token mints, and GitHub needs sign-in >>> [CLI] aitools mcp list --check -Profile logfood ([DATABRICKS_URL]) token ok +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) token ok genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code github-logfood system.ai.github Claude Code sign-in needed @@ -24,7 +24,7 @@ Profile logfood ([DATABRICKS_URL]) token ok >>> [CLI] api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json {} >>> [CLI] aitools mcp list --check -Profile logfood ([DATABRICKS_URL]) token ok +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) token ok genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code github-logfood system.ai.github Claude Code signed in @@ -36,17 +36,20 @@ Profile logfood ([DATABRICKS_URL]) token ok { "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": "modified" + "status": "installed" } ], "check": { @@ -57,7 +60,9 @@ Profile logfood ([DATABRICKS_URL]) token ok { "name": "github-logfood", "service": "system.ai.github", + "host": "[DATABRICKS_URL]", "sign_in_required": true, + "status": "ok", "agents": [ { "name": "claude-code", @@ -74,7 +79,8 @@ Profile logfood ([DATABRICKS_URL]) token ok ] } ], - "unmanaged": [] + "unmanaged": [], + "warnings": [] } === logout ends the sign-in; list --check shows it needed again @@ -83,6 +89,6 @@ Profile logfood ([DATABRICKS_URL]) token ok Signed out of system.ai.github on profile logfood. >>> [CLI] aitools mcp list --check -Profile logfood ([DATABRICKS_URL]) token ok - genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code (modified) binary missing +Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) token ok + genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code binary missing github-logfood system.ai.github Claude Code sign-in needed From 2879f3fc46a838cd3cbe5d28c7a369396f3855bb Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:54:35 +0000 Subject: [PATCH 07/87] Share the sign-in helpers between login, logout and add Refactor without behavior change: SignIn takes the sign-in URL the caller builds, and no longer takes the host and workspace ID separately; the URL is built where it is printed and recorded. binaryStatus resolves the recorded command with binaryPathFor, so it shares the lookup seam. The full-name check, the agent-reached check, and the unexported token field of the list profile also reuse the existing helpers. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 20 +++++++------------- cmd/aitools/mcp_list.go | 30 ++++++++++++++---------------- cmd/aitools/mcp_login.go | 14 ++++---------- libs/aitools/mcp/login.go | 16 +++++++--------- libs/aitools/mcp/login_test.go | 24 ++++++++++++++++++------ 5 files changed, 50 insertions(+), 54 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index eeeb358ae39..53fdd3dde19 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -1036,7 +1036,8 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp if !server.SignInRequired || !installedAnywhere(server) { continue } - if _, err := signIn(ctx, errOut, w, req.profile, req.workspaceID, plans[i].service); err != nil { + url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) + if _, err := signIn(ctx, errOut, w, req.profile, url, plans[i].service); err != nil { return err } } @@ -1127,15 +1128,12 @@ func agentFailureError(req addRequest, servers []addServerOutput) error { var errs []error fail := req.explicit for _, server := range servers { - reached := false for _, a := range server.Agents { - if a.err == nil { - reached = true - continue + if a.err != nil { + errs = appendDistinct(errs, a.err) } - errs = appendDistinct(errs, a.err) } - if !reached { + if !installedAnywhere(server) { fail = true } } @@ -1175,20 +1173,16 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut for _, note := range server.notes { cmdio.LogString(ctx, " "+note) } - succeeded := false for _, a := range server.Agents { cmdio.LogString(ctx, " "+agentResultLine(ctx, a)) - if a.Status != statusFailed { - succeeded = true - } if a.Name == agents.NameCursor && a.Status == statusInstalled { cursorServers = append(cursorServers, server.Name) } } - if server.SignInRequired && succeeded && !req.login { + if server.SignInRequired && installedAnywhere(server) && !req.login { cmdio.LogString(ctx, fmt.Sprintf(" %s needs sign-in. Run: databricks aitools mcp login %s --profile %s", server.Service, shellquote.BashArg(server.Service), shellquote.BashArg(req.profile))) } - if server.note != "" && succeeded { + if server.note != "" && installedAnywhere(server) { cmdio.LogString(ctx, " "+server.note) } } diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 1f3ff87af88..617176f905f 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -75,9 +75,9 @@ type mcpListProfile struct { configPath string // noHost is set when Status is profile_missing because the profile's section has no host. noHost bool - // Token is the --check result of minting a token for the profile. The JSON carries it in each server's check, + // token is the --check result of minting a token for the profile. The JSON carries it in each server's check, // so it is text only. - Token string `json:"-"` + token string } type mcpListServer struct { @@ -415,13 +415,13 @@ func addChecks(ctx context.Context, out *mcpListOutput) { continue } w, err := profileClient(ctx, p.Profile, p.configPath) - p.Token = statusOK + p.token = statusOK if err != nil { - p.Token = checkFailed + p.token = checkFailed } for j := range p.Servers { s := &p.Servers[j] - s.Check = &mcpListCheck{Token: p.Token, Binary: binaryStatus(s.entry.Command)} + s.Check = &mcpListCheck{Token: p.token, Binary: binaryStatus(s.entry.Command, exec.LookPath)} if s.SignInRequired { s.Check.SignIn = statusUnknown if err == nil { @@ -444,16 +444,14 @@ func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, service st return checkNeeded } -// binaryStatus reports whether the recorded command exists: an absolute path on disk, or a bare name -// on PATH. -func binaryStatus(command string) string { - var err error - if filepath.IsAbs(command) { - _, err = os.Stat(command) - } else { - _, err = exec.LookPath(command) +// binaryStatus reports whether the recorded command exists. It resolves the command as binaryPathFor does: an +// absolute path must be on disk, and a bare name must be found by lookPath. +func binaryStatus(command string, lookPath func(string) (string, error)) string { + path, absolute := binaryPathFor(command, lookPath) + if !absolute { + return checkFailed } - if err != nil { + if _, err := os.Stat(path); err != nil { return checkFailed } return statusOK @@ -505,8 +503,8 @@ func renderMcpList(ctx context.Context, out mcpListOutput, profileName string) { cmdio.LogString(ctx, "") } header := profileHeader(p) - if p.Token != "" { - header += " token " + p.Token + if p.token != "" { + header += " token " + p.token } cmdio.LogString(ctx, header) for _, s := range p.Servers { diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 951c1e1c808..e463564ec93 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "slices" "strings" "github.com/databricks/cli/cmd/root" @@ -13,7 +12,6 @@ import ( "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/browser" - "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/telemetry/protos" @@ -91,9 +89,6 @@ stays in place. Running it when you are not signed in is not an error. func runMcpSignInCommand(cmd *cobra.Command, service string, action mcpSignInAction) error { ctx := cmd.Context() jsonMode := installOutputIsJSON(cmd) - if jsonMode { - ctx = cmdio.WithQuiet(ctx) - } out := &mcpSignInOutput{Service: service} runErr := runMcpSignIn(ctx, cmd.ErrOrStderr(), profileFlag(cmd), service, action, out) @@ -114,7 +109,7 @@ func runMcpSignInCommand(cmd *cobra.Command, service string, action mcpSignInAct } func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service string, action mcpSignInAction, out *mcpSignInOutput) error { - if parts := strings.Split(service, "."); len(parts) != 3 || slices.Contains(parts, "") { + if !isFullName(strings.Split(service, ".")) { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q", service) } // The config file is resolved once, as add resolves it, so that the profile lookup and the client read the same file. @@ -159,7 +154,7 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie return err } out.URL = mcp.SignInURL(out.Host, svc.FullName, workspaceID) - state, err := signIn(ctx, errOut, w, out.Profile, workspaceID, svc) + state, err := signIn(ctx, errOut, w, out.Profile, out.URL, svc) out.State = state return err } @@ -181,14 +176,13 @@ func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceCli // signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns // the sign-in state name and the failure, which is categorized for JSON. `add --login` shares it. -func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, workspaceID string, svc mcp.Service) (string, error) { +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, url string, svc mcp.Service) (string, error) { fmt.Fprintf(errOut, "Sign in with the same account you use for Databricks profile %s.\n", profileName) - err := mcp.SignIn(ctx, w, w.Config.Host, workspaceID, svc.FullName, errOut, mcp.SignInOptions{ + err := mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ Poll: mcp.SignInPollInterval, Timeout: mcp.SignInWaitTimeout, Open: browser.Open, }) - url := mcp.SignInURL(w.Config.Host, svc.FullName, workspaceID) switch { case errors.Is(err, mcp.ErrSignInTimeout): return signInStateTimeout, failure(categorySignInTimeout, err, diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index f99c0e53769..6ae2e084f0c 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -25,12 +25,11 @@ var ErrSignInTimeout = errors.New("sign-in did not complete before the timeout") // ErrSignInFailed means the credential reached the FAILED provisioning state. var ErrSignInFailed = errors.New("sign-in failed") -// SignInOptions controls how SignIn waits. Callers set both durations. +// SignInOptions controls how SignIn waits. Callers set all three fields. type SignInOptions struct { Poll time.Duration Timeout time.Duration - // Open opens the sign-in page. Nil leaves the page to the user. It is not called when - // BROWSER=none, because the URL is already printed. + // Open opens the sign-in page. It is not called when BROWSER=none, because the URL is already printed. Open func(ctx context.Context, url string) error } @@ -52,10 +51,10 @@ func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service return cred.ProvisioningInfo.State, nil } -// SignIn signs the caller in to a sign-in-required service. It returns nil at once when the -// caller is already signed in. Otherwise it prints the sign-in URL to out, opens it, and polls -// until the credential is ACTIVE. The errors are ErrSignInTimeout, ErrSignInFailed, or an API error. -func SignIn(ctx context.Context, w *databricks.WorkspaceClient, host, workspaceID, service string, out io.Writer, opts SignInOptions) error { +// SignIn signs the caller in to a sign-in-required service, at url from SignInURL. It returns nil at once when the +// caller is already signed in. Otherwise it prints url to out, opens it, and polls until the credential is ACTIVE. +// The errors are ErrSignInTimeout, ErrSignInFailed, or an API error. +func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service string, out io.Writer, opts SignInOptions) error { state, err := CredentialState(ctx, w, service) if err != nil { return err @@ -64,9 +63,8 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, host, workspaceI return nil } - url := SignInURL(host, service, workspaceID) fmt.Fprintf(out, "Open this URL to sign in to %s:\n%s\n", service, url) - if opts.Open != nil && !browser.IsDisabled(ctx) { + if !browser.IsDisabled(ctx) { if err := opts.Open(ctx, url); err != nil { fmt.Fprintf(out, "Could not open a browser (%v). Open the URL above.\n", err) } diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index ed8441a6fea..bca201120d0 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -67,6 +67,15 @@ func (s *loginTestServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { } } +// loginTestSignInURL is the sign-in URL that SignIn is given for the fake's workspace, with workspace ID 123. +func loginTestSignInURL(w *databricks.WorkspaceClient) string { + return SignInURL(w.Config.Host, loginTestService, "123") +} + +func noopOpen(context.Context, string) error { + return nil +} + // loginTestClient starts the fake and returns a client for it. func loginTestClient(t *testing.T, s *loginTestServer) *databricks.WorkspaceClient { t.Helper() @@ -83,7 +92,7 @@ func TestSignInPollsUntilActive(t *testing.T) { var opened []string var out bytes.Buffer - err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(_ context.Context, url string) error { @@ -105,7 +114,7 @@ func TestSignInAlreadyActiveDoesNotOpenBrowser(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: time.Second, Open: func(context.Context, string) error { @@ -123,7 +132,7 @@ func TestSignInBrowserNoneOnlyPrintsURL(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -140,7 +149,8 @@ func TestSignInTimeout(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, &out, SignInOptions{ + err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + Open: noopOpen, Poll: time.Millisecond, Timeout: 30 * time.Millisecond, }) @@ -154,7 +164,8 @@ func TestSignInCancelledIsNotATimeout(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) cancel() - err := SignIn(ctx, w, w.Config.Host, "123", loginTestService, io.Discard, SignInOptions{ + err := SignIn(ctx, w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Open: noopOpen, Poll: time.Millisecond, Timeout: time.Minute, }) @@ -166,7 +177,8 @@ func TestSignInFailed(t *testing.T) { s := &loginTestServer{states: []string{"", "FAILED"}} w := loginTestClient(t, s) - err := SignIn(t.Context(), w, w.Config.Host, "123", loginTestService, io.Discard, SignInOptions{ + err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Open: noopOpen, Poll: time.Millisecond, Timeout: 10 * time.Second, }) From 4e7eb44d7c48ca26fb67209f562a33ce260f8892 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:57:02 +0000 Subject: [PATCH 08/87] Check the binding in login and logout, and classify their failures login and logout check the profile binding before any request, as add and serve do, so a profile whose host or workspace changed is refused with the same message, and a missing profile gets add's wording. login names the profile's current workspace in the sign-in URL. logout looks the service up first: a service the profile cannot see is an error instead of 'not signed in'. A service whose source connection was deleted is not found, as Discover drops it. Failures from the API are described as add describes them, so a rejected credential is AUTH_FAILED with the login command. The fake answers a sign-in to a service that does not exist with NOT_FOUND. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 73 +++++++++--------------- cmd/aitools/mcp_login_test.go | 84 ++++++++++++++++++++++++++++ libs/aitools/mcp/discovery.go | 6 +- libs/aitools/mcp/discovery_test.go | 32 +++++++++++ libs/aitools/mcp/login_test.go | 18 +----- libs/testserver/mcp_services.go | 8 ++- libs/testserver/mcp_services_test.go | 15 +++++ 7 files changed, 173 insertions(+), 63 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index e463564ec93..1043eb46198 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -12,7 +12,6 @@ import ( "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/browser" - "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" @@ -122,7 +121,12 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri if err != nil { return err } - if err := requireProfile(ctx, profileName); err != nil { + // The binding is checked before the profile's host is contacted, as add and serve check it. + state, err := mcp.LoadState(ctx) + if err != nil { + return err + } + if _, err := mcp.CheckBinding(ctx, state, service, profileName, configFile); err != nil { return err } w, err := profileClient(ctx, profileName, configFile) @@ -135,11 +139,21 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri return action(ctx, errOut, w, out) } -func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { +// signInService returns the service of a sign-in command. A service the profile cannot see is an error, so that a +// misspelled name is reported rather than treated as a service with no sign-in. +func signInService(ctx context.Context, w *databricks.WorkspaceClient, out *mcpSignInOutput) (mcp.Service, error) { svc, err := mcp.GetService(ctx, w, out.Service) if errors.Is(err, apierr.ErrNotFound) { - return failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", out.Service, out.Profile) + return mcp.Service{}, failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", out.Service, out.Profile) } + if err != nil { + return mcp.Service{}, describeRequestError(out.Profile, err) + } + return svc, nil +} + +func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { + svc, err := signInService(ctx, w, out) if err != nil { return err } @@ -149,9 +163,11 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie return nil } - workspaceID, err := signInWorkspaceID(ctx, w, out.configFile, out.Profile, svc.FullName) + // The sign-in URL names the workspace the profile is in now, as add does. The binding check above has already + // refused a profile whose workspace changed since the server was installed. + workspaceID, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return err + return describeRequestError(out.Profile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } out.URL = mcp.SignInURL(out.Host, svc.FullName, workspaceID) state, err := signIn(ctx, errOut, w, out.Profile, out.URL, svc) @@ -160,9 +176,12 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie } func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { + if _, err := signInService(ctx, w, out); err != nil { + return err + } deleted, err := mcp.SignOut(ctx, w, out.Service) if err != nil { - return err + return describeRequestError(out.Profile, err) } if !deleted { out.State = signOutStateNotSignedIn @@ -193,7 +212,7 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient "Sign-in to %s failed on profile %s. Run: databricks aitools mcp login %s --profile %s", svc.FullName, profileName, svc.FullName, profileName) case err != nil: - return "", err + return "", describeRequestError(profileName, err) } fmt.Fprintf(errOut, "Signed in to %s on profile %s.\n", svc.FullName, profileName) @@ -202,41 +221,3 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient } return signInStateActive, nil } - -// signInWorkspaceID returns the workspace ID for the sign-in URL: the one recorded when the service -// was installed for the profile in configFile, else the profile's own. -func signInWorkspaceID(ctx context.Context, w *databricks.WorkspaceClient, configFile, profileName, service string) (string, error) { - state, err := mcp.LoadState(ctx) - if err != nil { - return "", err - } - entry, err := mcp.FindEntry(ctx, state, service, profileName, configFile) - if err != nil { - return "", err - } - if entry != nil { - return entry.WorkspaceID, nil - } - workspaceID, err := auth.ResolveWorkspaceID(ctx, w) - if err != nil { - return "", describeRequestError(profileName, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) - } - return workspaceID, nil -} - -// requireProfile fails when the named profile is not in the Databricks config file. -func requireProfile(ctx context.Context, name string) error { - profiler := profile.GetProfiler(ctx) - profiles, err := profiler.LoadProfiles(ctx, profile.WithName(name)) - if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { - return err - } - if len(profiles) > 0 { - return nil - } - path, err := profiler.GetPath(ctx) - if err != nil { - return err - } - return failure(protos.AitoolsErrorCategory(mcp.BindProfileNotFound), nil, "Profile %s not found in %s", name, path) -} diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index ca135c34672..2e41f24d809 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -2,8 +2,13 @@ package aitools import ( "bytes" + "io" + "net/http" + "net/http/httptest" + "strings" "testing" + "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/telemetry/protos" "github.com/stretchr/testify/assert" @@ -21,3 +26,82 @@ func TestMcpSignInRequiresFullServiceName(t *testing.T) { }) } } + +// loginTestServer returns a server that answers every API call with the status and body given. The SDK still reads +// host metadata when it configures a profile, so paths outside /api/ are not found. +func loginTestServer(t *testing.T, status int, body string) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + _, _ = w.Write([]byte(body)) + })) + t.Cleanup(server.Close) + return server +} + +// TestMcpLoginRefusesDriftBeforeAnyRequest checks that login refuses a profile whose host changed since the server was +// installed, with the same message as add and serve, before it sends a request to the profile's host. +func TestMcpLoginRefusesDriftBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test"}) + + out := mcpSignInOutput{Service: "system.ai.github"} + err := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.github", mcpLogin, &out) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err)) + assert.Contains(t, err.Error(), "MCP server github-logfood was set up for https://adb-123.test, but profile \"logfood\" now points at ") + assert.Contains(t, err.Error(), "aitools mcp remove --names github-logfood") +} + +// TestMcpLoginMissingProfileMatchesAdd checks that login names a missing profile the way add and serve do, with the same +// message, rather than with a message of its own. +func TestMcpLoginMissingProfileMatchesAdd(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[other]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-other\n") + + out := mcpSignInOutput{Service: "system.ai.github"} + loginErr := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.github", mcpLogin, &out) + require.Error(t, loginErr) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(loginErr)) + + addErr := runAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code") + require.Error(t, addErr) + assert.Equal(t, addErr.Error(), loginErr.Error()) +} + +// TestMcpLoginRejectedTokenIsAuthFailed checks that a credential the workspace rejects on the first API call is +// AUTH_FAILED with the login command for the profile, as it is in add. +func TestMcpLoginRejectedTokenIsAuthFailed(t *testing.T) { + home := withHome(t) + server := loginTestServer(t, http.StatusUnauthorized, `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-expired\n") + + out := mcpSignInOutput{Service: "system.ai.github"} + err := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.github", mcpLogin, &out) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(err)) + assert.Contains(t, err.Error(), "Run: databricks auth login --profile logfood") +} + +// TestMcpLogoutUnknownServiceIsAnError checks that logout of a service the profile cannot see fails, rather than +// reporting that the caller is not signed in, so that a typo in the name is seen. +func TestMcpLogoutUnknownServiceIsAnError(t *testing.T) { + home := withHome(t) + server := loginTestServer(t, http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + out := mcpSignInOutput{Service: "system.ai.gihtub"} + err := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.gihtub", mcpLogout, &out) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(categoryServiceNotFound), errorCategory(err)) + assert.Contains(t, err.Error(), "system.ai.gihtub is not an MCP service visible to profile logfood") + assert.Empty(t, out.State) +} diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index 8e5510e98d3..b3400cb5c9c 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -66,12 +66,16 @@ func Discover(ctx context.Context, w *databricks.WorkspaceClient, location strin } // GetService returns one MCP service by full name, with its sign-in requirement. The API -// answers NOT_FOUND (apierr.ErrNotFound) for a service the profile cannot see. +// answers NOT_FOUND (apierr.ErrNotFound) for a service the profile cannot see, and so does a +// service whose source connection was deleted, which Discover drops. func GetService(ctx context.Context, w *databricks.WorkspaceClient, fullName string) (Service, error) { svc, err := w.AiGateway.GetMcpService(ctx, catalog.GetMcpServiceRequest{Name: mcpServicePrefix + fullName}) if err != nil { return Service{}, err } + if isDeletedSource(svc) { + return Service{}, fmt.Errorf("%s has no source connection: %w", fullName, apierr.ErrNotFound) + } return newService(svc), nil } diff --git a/libs/aitools/mcp/discovery_test.go b/libs/aitools/mcp/discovery_test.go index 7434cca6462..ab43c0f4ce9 100644 --- a/libs/aitools/mcp/discovery_test.go +++ b/libs/aitools/mcp/discovery_test.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/http/httptest" + "strings" "sync/atomic" "testing" @@ -316,3 +317,34 @@ func TestDiscoverRejectsLocationWithoutSchema(t *testing.T) { }) } } + +func TestGetServiceSignInRequired(t *testing.T) { + s := &loginTestServer{} + w := loginTestClient(t, s) + + svc, err := GetService(t.Context(), w, loginTestService) + require.NoError(t, err) + assert.True(t, svc.SignInRequired) + assert.Equal(t, "OAUTH_PROVIDER_GITHUB_MCP", svc.OAuthProvider) + require.NotNil(t, svc.Known) + assert.Equal(t, "GitHub", svc.Known.Label) +} + +// TestGetServiceDeletedSourceIsNotFound checks that a service whose source connection was deleted is NOT_FOUND, as +// Discover drops it, so that login and logout treat it the same way as add does. +func TestGetServiceDeletedSourceIsNotFound(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","config":{"source_connection":{"is_deleted":true,"options":{"oauth_provider":"OAUTH_PROVIDER_GITHUB_MCP"}}}}`) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + _, err = GetService(t.Context(), w, loginTestService) + require.ErrorIs(t, err, apierr.ErrNotFound) +} diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index bca201120d0..0218d477690 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -150,7 +150,7 @@ func TestSignInTimeout(t *testing.T) { var out bytes.Buffer err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ - Open: noopOpen, + Open: noopOpen, Poll: time.Millisecond, Timeout: 30 * time.Millisecond, }) @@ -165,7 +165,7 @@ func TestSignInCancelledIsNotATimeout(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) cancel() err := SignIn(ctx, w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ - Open: noopOpen, + Open: noopOpen, Poll: time.Millisecond, Timeout: time.Minute, }) @@ -178,7 +178,7 @@ func TestSignInFailed(t *testing.T) { w := loginTestClient(t, s) err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ - Open: noopOpen, + Open: noopOpen, Poll: time.Millisecond, Timeout: 10 * time.Second, }) @@ -209,15 +209,3 @@ func TestSignOut(t *testing.T) { require.NoError(t, err) assert.False(t, deleted) } - -func TestGetServiceSignInRequired(t *testing.T) { - s := &loginTestServer{} - w := loginTestClient(t, s) - - svc, err := GetService(t.Context(), w, loginTestService) - require.NoError(t, err) - assert.True(t, svc.SignInRequired) - assert.Equal(t, "OAUTH_PROVIDER_GITHUB_MCP", svc.OAuthProvider) - require.NotNil(t, svc.Known) - assert.Equal(t, "GitHub", svc.Known.Label) -} diff --git a/libs/testserver/mcp_services.go b/libs/testserver/mcp_services.go index 4006f058ae8..fa53db38922 100644 --- a/libs/testserver/mcp_services.go +++ b/libs/testserver/mcp_services.go @@ -106,10 +106,16 @@ func (s *FakeWorkspace) McpServicesList(req Request) Response { // McpUserCredentialCreate fakes POST .../mcp-services/{name}/user-credentials, which completes a // caller's provider sign-in. Tests use it to flip a credential from absent to ACTIVE while the CLI -// polls. The login body is not checked. +// polls. The login body is not checked. A service that does not exist is NOT_FOUND, as the API answers. func (s *FakeWorkspace) McpUserCredentialCreate(service string) Response { defer s.LockUnlock()() + if _, ok := s.McpServices[service]; !ok { + return Response{ + StatusCode: http.StatusNotFound, + Body: fmt.Sprintf("mcp service %s not found", service), + } + } cred := catalog.McpServiceUserMappedCredential{ ProvisioningInfo: &catalog.ProvisioningInfo{State: catalog.ProvisioningInfoStateActive}, } diff --git a/libs/testserver/mcp_services_test.go b/libs/testserver/mcp_services_test.go index 99bbad84921..18dc7e185bb 100644 --- a/libs/testserver/mcp_services_test.go +++ b/libs/testserver/mcp_services_test.go @@ -1,6 +1,7 @@ package testserver import ( + "net/http" "net/url" "testing" @@ -97,3 +98,17 @@ func mcpServicesTestSeed(s *FakeWorkspace, fullName, comment, oauthProvider stri } s.McpServices[fullName] = ms } + +// TestMcpUserCredentialCreateNeedsTheService checks that a sign-in to a service that does not exist is NOT_FOUND, as +// the API answers, and that nothing is recorded for it. +func TestMcpUserCredentialCreateNeedsTheService(t *testing.T) { + workspace := mcpServicesTestWorkspace(t) + + response := workspace.McpUserCredentialCreate("system.ai.gihtub") + assert.Equal(t, http.StatusNotFound, response.StatusCode) + assert.Empty(t, workspace.McpUserCredentials) + + response = workspace.McpUserCredentialCreate("system.ai.github") + assert.Equal(t, 0, response.StatusCode) + assert.Contains(t, workspace.McpUserCredentials, "system.ai.github") +} From e4e9fb53ed0246698c54b728c9876ef2b6da5e64 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:58:56 +0000 Subject: [PATCH 09/87] Bound the sign-in wait and ignore a FAILED state left by an earlier attempt A FAILED credential from an earlier sign-in is no longer the result of a new one: FAILED counts only once the state has changed since the attempt started. The wait is cancelled after the timeout, which also cuts off a poll that is still in flight, so the command ends at the timeout rather than when the SDK's retries of that poll end. Co-authored-by: Isaac --- libs/aitools/mcp/login.go | 38 ++++++++++++------ libs/aitools/mcp/login_test.go | 70 +++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 13 deletions(-) diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 6ae2e084f0c..33d4c2df420 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -13,7 +13,7 @@ import ( "github.com/databricks/databricks-sdk-go/service/catalog" ) -// SignInPollInterval and SignInWaitTimeout are the polling settings of provider sign-in (SPEC 12). +// SignInPollInterval and SignInWaitTimeout are the polling settings of provider sign-in. const ( SignInPollInterval = 2 * time.Second SignInWaitTimeout = 5 * time.Minute @@ -71,36 +71,52 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str } fmt.Fprintln(out, "Waiting for sign-in...") - deadline := time.NewTimer(opts.Timeout) - defer deadline.Stop() + // The timeout cancels the wait from a timer, not with a context deadline: the SDK rate limiter refuses a request + // whose wait would exceed a deadline, which would hide the timeout. A poll still in flight is cut off too, so the + // SDK's retries of a slow poll cannot hold the command past the timeout. + waitCtx, cancel := context.WithCancel(ctx) + defer cancel() + timer := time.AfterFunc(opts.Timeout, cancel) + defer timer.Stop() ticker := time.NewTicker(opts.Poll) defer ticker.Stop() - // The deadline is a timer, not a context deadline: the SDK rate limiter refuses a request - // whose context would expire before it may run, which would hide the timeout. + // A FAILED state left by an earlier attempt is still there when this one starts. FAILED counts only once the + // state has changed since the previous read, so it is the result of this attempt. + previous := state for { select { - case <-ctx.Done(): - return ctx.Err() - case <-deadline.C: - return ErrSignInTimeout + case <-waitCtx.Done(): + return waitEnded(ctx) case <-ticker.C: } - state, err := CredentialState(ctx, w, service) + state, err := CredentialState(waitCtx, w, service) if err != nil { + if waitCtx.Err() != nil { + return waitEnded(ctx) + } return err } // Other states (PROVISIONING, UPDATING, ...) mean the login has not completed yet. if state == catalog.ProvisioningInfoStateActive { return nil } - if state == catalog.ProvisioningInfoStateFailed { + if state == catalog.ProvisioningInfoStateFailed && state != previous { return ErrSignInFailed } + previous = state } } +// waitEnded returns why the sign-in wait ended: the caller's cancellation, or else the timeout. +func waitEnded(ctx context.Context) error { + if err := ctx.Err(); err != nil { + return err + } + return ErrSignInTimeout +} + // SignOut deletes the caller's sign-in to a service. It reports false when the caller was not // signed in, which is not an error. func SignOut(ctx context.Context, w *databricks.WorkspaceClient, service string) (bool, error) { diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 0218d477690..22dd85c16f2 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "strings" "sync" + "sync/atomic" "testing" "time" @@ -162,12 +163,15 @@ func TestSignInCancelledIsNotATimeout(t *testing.T) { s := &loginTestServer{states: []string{""}} w := loginTestClient(t, s) + // The caller cancels once the sign-in page is open, so the wait is what is cancelled. ctx, cancel := context.WithCancel(t.Context()) - cancel() err := SignIn(ctx, w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ - Open: noopOpen, Poll: time.Millisecond, Timeout: time.Minute, + Open: func(context.Context, string) error { + cancel() + return nil + }, }) require.ErrorIs(t, err, context.Canceled) assert.NotErrorIs(t, err, ErrSignInTimeout) @@ -209,3 +213,65 @@ func TestSignOut(t *testing.T) { require.NoError(t, err) assert.False(t, deleted) } + +// TestSignInFailedFromEarlierAttemptIsNotTheResult checks that a FAILED credential left by an earlier attempt is not the +// result of this one. The state only counts as failed once it changes, so the new sign-in can still complete. +func TestSignInFailedFromEarlierAttemptIsNotTheResult(t *testing.T) { + s := &loginTestServer{states: []string{"FAILED", "FAILED", "ACTIVE"}} + w := loginTestClient(t, s) + + err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + Open: noopOpen, + }) + require.NoError(t, err) + assert.Equal(t, 3, s.reads) +} + +// TestSignInFailedAfterProgressIsFailed checks that a FAILED state that follows a change of state fails the sign-in. +func TestSignInFailedAfterProgressIsFailed(t *testing.T) { + s := &loginTestServer{states: []string{"FAILED", "PROVISIONING", "FAILED"}} + w := loginTestClient(t, s) + + err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + Open: noopOpen, + }) + require.ErrorIs(t, err, ErrSignInFailed) +} + +// TestSignInTimeoutCutsOffPendingRequest checks that a poll still in flight when the wait ends is cut off, and that the +// end of the wait is ErrSignInTimeout, not the API error of the cut request. +func TestSignInTimeoutCutsOffPendingRequest(t *testing.T) { + var reads atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + rw.WriteHeader(http.StatusNotFound) + return + } + if reads.Add(1) == 1 { + // The first read, before the wait, finds no credential. + http.Error(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`, http.StatusNotFound) + return + } + // A later poll hangs, as a slow workspace does, until the client gives up. + select { + case <-r.Context().Done(): + case <-time.After(10 * time.Second): + } + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + start := time.Now() + err = SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 200 * time.Millisecond, + Open: noopOpen, + }) + require.ErrorIs(t, err, ErrSignInTimeout) + assert.Less(t, time.Since(start), 5*time.Second) +} From 9fefd7ddb21522bcdec1c77b7db37a12e5c39977 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:00:37 +0000 Subject: [PATCH 10/87] Sign in to every server in add --login, and keep sign-in failures add --login attempts each installed sign-in server, so one failed sign-in no longer leaves the later servers unsigned. Each server that failed gets the login command, as a server that was not signed in by a plain add does. The sign-in failures are joined with the agent failures, so the exit status and the JSON error both report them. topLevelFailure keeps the failures that are not per-agent when a run error joins both; install and remove never join them, so they are unchanged. Co-authored-by: Isaac --- cmd/aitools/install.go | 15 +++++++++-- cmd/aitools/mcp_add.go | 30 ++++++++++++--------- cmd/aitools/mcp_add_test.go | 12 +++++++++ cmd/aitools/mcp_login_test.go | 51 +++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 15 deletions(-) diff --git a/cmd/aitools/install.go b/cmd/aitools/install.go index 62e6f43e2b8..424b2b8a8e0 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -762,10 +762,21 @@ func forgetStalePluginRecords(ctx context.Context, scope string, items []agentPl // when the failure is already reported per agent — so a per-agent failure is not // duplicated in the top-level error/errorCategory fields. func topLevelFailure(runErr error) error { - if _, ok := errors.AsType[*agentErrors](runErr); ok { + if _, ok := errors.AsType[*agentErrors](runErr); !ok { + return runErr + } + // A run error that joins per-agent failures with others keeps the others. + joined, ok := runErr.(interface{ Unwrap() []error }) + if !ok { return nil } - return runErr + var others []error + for _, err := range joined.Unwrap() { + if _, ok := errors.AsType[*agentErrors](err); !ok { + others = append(others, err) + } + } + return errors.Join(others...) } // executePlan carries out the plan and returns each agent's outcome. Skills diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 53fdd3dde19..4cbad78bbfc 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -327,18 +327,15 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode } out.Servers = servers - // The results are printed even when the state cannot be saved: the agent files were written. + // The results are printed even when the state cannot be saved: the agent files were written. The sign-in does not + // depend on the state, so it still runs, and every failure is returned. recordErr := recordInstalls(ctx, req, plans, servers) printAddResults(ctx, req, servers) - if recordErr != nil { - return recordErr - } + var signErr error if req.login { - if err := signInInstalled(ctx, cmd.ErrOrStderr(), w, req, plans, servers); err != nil { - return err - } + signErr = signInInstalled(ctx, cmd.ErrOrStderr(), w, req, plans, servers) } - return agentFailureError(req, servers) + return errors.Join(recordErr, signErr, agentFailureError(req, servers)) } // validateMCPAddFlags rejects flag combinations that cannot run, before anything is read or written. @@ -1029,19 +1026,26 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { return failure(protos.AitoolsErrorCategoryUnknownAgent, "%s is not installed; install it first or leave it out of --agents", a.DisplayName) } -// signInInstalled signs in to each sign-in-required server that reached an agent, in order. It stops -// at the first failure; the servers are installed either way. +// signInInstalled signs in to each sign-in-required server that reached an agent, in order. A failure does not stop the +// others; each failed server gets the login hint, and the failures are joined. The servers are installed either way. func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, req addRequest, plans []addPlan, servers []addServerOutput) error { + var errs []error for i, server := range servers { if !server.SignInRequired || !installedAnywhere(server) { continue } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) if _, err := signIn(ctx, errOut, w, req.profile, url, plans[i].service); err != nil { - return err + fmt.Fprintln(errOut, signInHint(server.Service, req.profile)) + errs = append(errs, err) } } - return nil + return errors.Join(errs...) +} + +// signInHint is the command that signs the profile in to a sign-in-required service. +func signInHint(service, profileName string) string { + return fmt.Sprintf("%s needs sign-in. Run: databricks aitools mcp login %s --profile %s", service, shellquote.BashArg(service), shellquote.BashArg(profileName)) } // installedAnywhere reports whether the server reached at least one agent. @@ -1180,7 +1184,7 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut } } if server.SignInRequired && installedAnywhere(server) && !req.login { - cmdio.LogString(ctx, fmt.Sprintf(" %s needs sign-in. Run: databricks aitools mcp login %s --profile %s", server.Service, shellquote.BashArg(server.Service), shellquote.BashArg(req.profile))) + cmdio.LogString(ctx, " "+signInHint(server.Service, req.profile)) } if server.note != "" && installedAnywhere(server) { cmdio.LogString(ctx, " "+server.note) diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index ac5eafc918c..e1895c160e4 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1638,3 +1638,15 @@ func TestPlanningRefusalsCountsRefusedAgents(t *testing.T) { assert.NoError(t, planningRefusals(plans, targets, nil)) } + +// TestTopLevelFailureKeepsErrorsOutsideAgents checks that a run error which joins per-agent failures with another failure, +// such as a sign-in that failed, keeps the other failure as the document's top-level error. +func TestTopLevelFailureKeepsErrorsOutsideAgents(t *testing.T) { + signInErr := errors.New("sign-in failed") + agentErr := &agentErrors{err: errors.New("agent failed")} + + assert.NoError(t, topLevelFailure(agentErr)) + top := topLevelFailure(errors.Join(agentErr, signInErr)) + require.ErrorIs(t, top, signInErr) + assert.Equal(t, signInErr.Error(), top.Error()) +} diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 2e41f24d809..d043466aa7c 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -6,11 +6,14 @@ import ( "net/http" "net/http/httptest" "strings" + "sync" "testing" + "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -105,3 +108,51 @@ func TestMcpLogoutUnknownServiceIsAnError(t *testing.T) { assert.Contains(t, err.Error(), "system.ai.gihtub is not an MCP service visible to profile logfood") assert.Empty(t, out.State) } + +// TestSignInInstalledSignsInToEveryServer checks that add --login attempts every installed sign-in server after one of +// them fails. The failed server gets the login hint, and the failure is returned, so the others are still signed in. +func TestSignInInstalledSignsInToEveryServer(t *testing.T) { + const ( + githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + geniePath = "/api/2.1/unity-catalog/mcp-services/system.ai.genie_one_mcp/user-credentials" + ) + var mu sync.Mutex + var reads []string + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + rw.WriteHeader(http.StatusNotFound) + return + } + mu.Lock() + reads = append(reads, r.URL.Path) + mu.Unlock() + rw.Header().Set("Content-Type", "application/json") + if r.URL.Path == githubPath { + rw.WriteHeader(http.StatusForbidden) + _, _ = rw.Write([]byte(`{"error_code":"PERMISSION_DENIED","message":"no access"}`)) + return + } + _, _ = rw.Write([]byte(`{"provisioning_info":{"state":"ACTIVE"}}`)) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + req := addRequest{profile: "logfood", workspaceID: "123", agents: []*agents.Agent{agents.ByName("claude-code")}} + plans := []addPlan{ + {service: mcp.Service{FullName: "system.ai.github", SignInRequired: true}}, + {service: mcp.Service{FullName: "system.ai.genie_one_mcp", SignInRequired: true}}, + } + installed := []addAgentOutput{{Name: "claude-code", Status: statusInstalled}} + servers := []addServerOutput{ + {Name: "github-logfood", Service: "system.ai.github", SignInRequired: true, Agents: installed}, + {Name: "genie-logfood", Service: "system.ai.genie_one_mcp", SignInRequired: true, Agents: installed}, + } + + var errOut bytes.Buffer + err = signInInstalled(t.Context(), &errOut, w, req, plans, servers) + require.Error(t, err) + assert.Equal(t, []string{githubPath, geniePath}, reads) + assert.Contains(t, errOut.String(), "system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood\n") + assert.NotContains(t, errOut.String(), "system.ai.genie_one_mcp needs sign-in") +} From c7f1c09e5cc50b4d1202bd08c233c20f33688abd Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:02:43 +0000 Subject: [PATCH 11/87] Report list --check failures as warnings and check each profile in its own file Each profile group is checked with its own config file, in the context and in the process environment the token store reads, and the environment is restored afterwards. Two groups of one profile name in two files no longer share the ambient file's token store. A token that cannot be minted, or a sign-in that cannot be read, is a warning that names the profile or server and says why, and the result stays unknown. A service the profile can no longer see is a warning with unknown sign-in, not a sign-in that is needed. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 88 ++++++++++---- cmd/aitools/mcp_list_test.go | 229 +++++++++++++++++++++++++++++++++++ 2 files changed, 295 insertions(+), 22 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 617176f905f..f6d5b0d608a 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -21,6 +21,7 @@ import ( "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -406,42 +407,85 @@ func pinnedEnv(rec mcp.Entry) map[string]string { return map[string]string{mcpconfig.ConfigFileEnv: rec.ConfigFile} } -// addChecks runs the --check tests of SPEC 4.2 for each profile that did not drift: a token mint, the -// sign-in state of each sign-in-required server, and whether the recorded binary exists. +// addChecks runs the --check tests for each profile that did not drift: a token mint, the sign-in state of each +// sign-in-required server, and whether the recorded binary exists. Each profile is checked with the config file of its +// group. A check that cannot run is a warning, and its result is unknown. func addChecks(ctx context.Context, out *mcpListOutput) { for i := range out.Profiles { p := &out.Profiles[i] if p.Status != statusOK { continue } - w, err := profileClient(ctx, p.Profile, p.configPath) - p.token = statusOK + err := withConfigFile(ctx, p.configPath, func(ctx context.Context) { + addProfileChecks(ctx, p, out) + }) if err != nil { - p.token = checkFailed + out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) } - for j := range p.Servers { - s := &p.Servers[j] - s.Check = &mcpListCheck{Token: p.token, Binary: binaryStatus(s.entry.Command, exec.LookPath)} - if s.SignInRequired { - s.Check.SignIn = statusUnknown - if err == nil { - s.Check.SignIn = signInStatus(ctx, w, s.Service) - } - } + } +} + +// addProfileChecks runs the checks of one profile, whose config file is in ctx. A profile whose token cannot be minted +// has no sign-in checks: its warning says why. +func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput) { + w, err := profileClient(ctx, p.Profile, p.configPath) + p.token = statusOK + if err != nil { + p.token = checkFailed + out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) + } + for j := range p.Servers { + s := &p.Servers[j] + s.Check = &mcpListCheck{Token: p.token, Binary: binaryStatus(s.entry.Command, exec.LookPath)} + if !s.SignInRequired { + continue + } + s.Check.SignIn = statusUnknown + if err != nil { + continue + } + status, signInErr := signInStatus(ctx, w, p.Profile, s.Service) + s.Check.SignIn = status + if signInErr != nil { + out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", s.Name, signInErr)) } } } -// signInStatus reads the caller's sign-in to a service. Only an ACTIVE credential counts as signed in. -func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, service string) string { +// signInStatus reads the caller's sign-in to a service. Only an ACTIVE credential counts as signed in. A service the +// profile can no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error +// says why. +func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, service string) (string, error) { + _, err := mcp.GetService(ctx, w, service) + if errors.Is(err, apierr.ErrNotFound) { + return statusUnknown, fmt.Errorf("%s is not an MCP service visible to profile %s", service, profileName) + } + if err != nil { + return statusUnknown, describeRequestError(profileName, err) + } state, err := mcp.CredentialState(ctx, w, service) - switch { - case err != nil: - return statusUnknown - case state == catalog.ProvisioningInfoStateActive: - return statusOK + if err != nil { + return statusUnknown, describeRequestError(profileName, err) + } + if state == catalog.ProvisioningInfoStateActive { + return statusOK, nil + } + return checkNeeded, nil +} + +// withConfigFile runs fn with path as the config file of a profile: in the context that fn receives, and in the process +// environment, which the token store reads (see mcp.Bind). The environment is restored afterwards, and its error is +// returned. +func withConfigFile(ctx context.Context, path string, fn func(ctx context.Context)) error { + previous, wasSet := os.LookupEnv(mcpconfig.ConfigFileEnv) + if err := os.Setenv(mcpconfig.ConfigFileEnv, path); err != nil { + return err + } + fn(env.Set(ctx, mcpconfig.ConfigFileEnv, path)) + if !wasSet { + return os.Unsetenv(mcpconfig.ConfigFileEnv) } - return checkNeeded + return os.Setenv(mcpconfig.ConfigFileEnv, previous) } // binaryStatus reports whether the recorded command exists. It resolves the command as binaryPathFor does: an diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 78cd9a14f9c..2bb8f6e81be 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -2,8 +2,12 @@ package aitools import ( "bytes" + "context" "encoding/json" + "net/http" + "net/http/httptest" "os" + "os/exec" "path/filepath" "strings" "testing" @@ -15,8 +19,10 @@ import ( "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/shellquote" "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -924,3 +930,226 @@ func TestBuildMcpListAndRemoveAgreeAfterConfigMoved(t *testing.T) { }) } } + +// listCheckServer answers the GitHub service and the caller's sign-in to it, for --check. The service answers with +// serviceStatus, and the credential with credentialStatus and credentialBody. Other API paths are NOT_FOUND. +func listCheckServer(t *testing.T, serviceStatus int, credentialStatus int, credentialBody string) *httptest.Server { + t.Helper() + const servicePath = "/api/2.1/unity-catalog/mcp-services/system.ai.github" + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case servicePath: + if serviceStatus != http.StatusOK { + w.WriteHeader(serviceStatus) + _, _ = w.Write([]byte(`{"error_code":"NOT_FOUND","message":"not found"}`)) + return + } + _, _ = w.Write([]byte(`{"name":"mcp-services/system.ai.github","config":{"source_connection":{"options":{"oauth_provider":"OAUTH_PROVIDER_GITHUB_MCP"}}}}`)) + case servicePath + "/user-credentials": + w.WriteHeader(credentialStatus) + _, _ = w.Write([]byte(credentialBody)) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(server.Close) + return server +} + +// listCheckGitHub returns the state record of a sign-in-required GitHub server for the profile at host, pinned to +// configFile when it is set. +func listCheckGitHub(t *testing.T, name, profileName, host, configFile string) mcp.Entry { + t.Helper() + entry := seedInstallEntry(t, agents.ByName("claude-code"), seed{name, "system.ai.github", profileName, host, testDatabricksCommand, configFile}) + entry.SignInRequired = true + return entry +} + +// checkedListServer runs buildMcpList and the --check tests, and returns the one server listed for the profile. +func checkedListServer(t *testing.T) (mcpListServer, mcpListOutput) { + t.Helper() + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + addChecks(t.Context(), &out) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, 1) + return out.Profiles[0].Servers[0], out +} + +// TestAddChecksWarnsWhenTheSignInCannotBeRead checks that a sign-in whose state cannot be read is unknown, and that the +// failure is a warning with the login command, not silently dropped. +func TestAddChecksWarnsWhenTheSignInCannotBeRead(t *testing.T) { + home := withHome(t) + server := listCheckServer(t, http.StatusOK, http.StatusUnauthorized, `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Equal(t, statusOK, s.Check.Token) + assert.Equal(t, statusUnknown, s.Check.SignIn) + require.Len(t, out.Warnings, 1) + assert.Contains(t, out.Warnings[0], "MCP server github-logfood: ") + assert.Contains(t, out.Warnings[0], "Run: databricks auth login --profile logfood") +} + +// TestAddChecksServiceGoneIsAWarningNotSignInNeeded checks that a service the profile no longer sees is a warning, and that +// its sign-in is unknown rather than needed, since the sign-in is not what is missing. +func TestAddChecksServiceGoneIsAWarningNotSignInNeeded(t *testing.T) { + home := withHome(t) + server := listCheckServer(t, http.StatusNotFound, http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Equal(t, statusUnknown, s.Check.SignIn) + require.Len(t, out.Warnings, 1) + assert.Equal(t, "MCP server github-logfood: system.ai.github is not an MCP service visible to profile logfood", out.Warnings[0]) +} + +// TestAddChecksWarnsWhenProfileCannotAuthenticate checks that a profile whose credential cannot be read is a warning +// naming the profile, and that its servers' sign-ins are not checked. +func TestAddChecksWarnsWhenProfileCannotAuthenticate(t *testing.T) { + home := withHome(t) + server := listCheckServer(t, http.StatusOK, http.StatusOK, `{}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Equal(t, checkFailed, s.Check.Token) + assert.Equal(t, statusUnknown, s.Check.SignIn) + require.Len(t, out.Warnings, 1) + assert.True(t, strings.HasPrefix(out.Warnings[0], "profile logfood: "), out.Warnings[0]) +} + +// TestAddChecksReadsEachProfileFromItsOwnConfigFile checks that two groups of one profile name, in two config files, are +// checked with their own file's credentials, and that the config file environment is the caller's again afterwards. +func TestAddChecksReadsEachProfileFromItsOwnConfigFile(t *testing.T) { + withHome(t) + signedIn := listCheckServer(t, http.StatusOK, http.StatusOK, `{"provisioning_info":{"state":"ACTIVE"}}`) + notSignedIn := listCheckServer(t, http.StatusOK, http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`) + dir := t.TempDir() + first := filepath.Join(dir, "first.cfg") + second := filepath.Join(dir, "second.cfg") + require.NoError(t, os.WriteFile(first, []byte("[logfood]\nhost = "+signedIn.URL+"\nauth_type = pat\ntoken = dapi-first\n"), 0o600)) + require.NoError(t, os.WriteFile(second, []byte("[logfood]\nhost = "+notSignedIn.URL+"\nauth_type = pat\ntoken = dapi-second\n"), 0o600)) + t.Setenv("DATABRICKS_CONFIG_FILE", first) + saveState(t, listCheckGitHub(t, "github-first", "logfood", signedIn.URL, first), listCheckGitHub(t, "github-second", "logfood", notSignedIn.URL, second)) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + addChecks(t.Context(), &out) + require.Len(t, out.Profiles, 2) + for _, p := range out.Profiles { + require.Len(t, p.Servers, 1) + assert.Equal(t, statusOK, p.Servers[0].Check.Token) + } + signInByServer := map[string]string{} + for _, p := range out.Profiles { + signInByServer[p.Servers[0].Name] = p.Servers[0].Check.SignIn + } + assert.Equal(t, map[string]string{"github-first": statusOK, "github-second": checkNeeded}, signInByServer) + assert.Equal(t, first, os.Getenv("DATABRICKS_CONFIG_FILE")) + assert.Empty(t, out.Warnings) +} + +// TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt checks that the config file of a profile is set in the +// context and in the process environment while a check runs, and that the caller's environment is restored after. +func TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt(t *testing.T) { + t.Setenv(mcpconfig.ConfigFileEnv, "ambient.cfg") + var inContext, inProcess string + err := withConfigFile(t.Context(), "pinned.cfg", func(ctx context.Context) { + inContext = env.Get(ctx, mcpconfig.ConfigFileEnv) + inProcess = os.Getenv(mcpconfig.ConfigFileEnv) + }) + require.NoError(t, err) + assert.Equal(t, "pinned.cfg", inContext) + assert.Equal(t, "pinned.cfg", inProcess) + assert.Equal(t, "ambient.cfg", os.Getenv(mcpconfig.ConfigFileEnv)) + + require.NoError(t, os.Unsetenv(mcpconfig.ConfigFileEnv)) + require.NoError(t, withConfigFile(t.Context(), "pinned.cfg", func(context.Context) {})) + _, set := os.LookupEnv(mcpconfig.ConfigFileEnv) + assert.False(t, set, "a variable that was unset is unset again") +} + +// TestCheckText checks the words that --check adds to a server's line. +func TestCheckText(t *testing.T) { + cases := []struct { + name string + check *mcpListCheck + want string + }{ + {name: "no check", check: nil, want: ""}, + {name: "signed in and binary ok", check: &mcpListCheck{SignIn: statusOK, Binary: statusOK}, want: "signed in"}, + {name: "sign-in needed", check: &mcpListCheck{SignIn: checkNeeded, Binary: statusOK}, want: "sign-in needed"}, + {name: "sign-in unknown", check: &mcpListCheck{SignIn: statusUnknown, Binary: statusOK}, want: "sign-in unknown"}, + {name: "binary missing", check: &mcpListCheck{Binary: checkFailed}, want: "binary missing"}, + {name: "sign-in needed and binary missing", check: &mcpListCheck{SignIn: checkNeeded, Binary: checkFailed}, want: "sign-in needed, binary missing"}, + {name: "server without sign-in", check: &mcpListCheck{Binary: statusOK}, want: ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, checkText(tc.check)) + }) + } +} + +// TestBinaryStatus checks that a recorded absolute path must be on disk, and that a bare name is looked up on PATH. +func TestBinaryStatus(t *testing.T) { + present := filepath.Join(t.TempDir(), "databricks") + require.NoError(t, os.WriteFile(present, []byte("binary"), 0o700)) + missing := filepath.Join(t.TempDir(), "databricks") + found := func(name string) (string, error) { + if name == "databricks" { + return present, nil + } + return "", exec.ErrNotFound + } + notFound := func(string) (string, error) { return "", exec.ErrNotFound } + + assert.Equal(t, statusOK, binaryStatus(present, notFound)) + assert.Equal(t, checkFailed, binaryStatus(missing, found)) + assert.Equal(t, statusOK, binaryStatus("databricks", found)) + assert.Equal(t, checkFailed, binaryStatus("databricks", notFound)) +} + +// TestSignInStatus checks the sign-in state that --check reports for one service. +func TestSignInStatus(t *testing.T) { + cases := []struct { + name string + serviceOK bool + credential int + body string + want string + warns bool + }{ + {name: "active", serviceOK: true, credential: http.StatusOK, body: `{"provisioning_info":{"state":"ACTIVE"}}`, want: statusOK}, + {name: "provisioning", serviceOK: true, credential: http.StatusOK, body: `{"provisioning_info":{"state":"PROVISIONING"}}`, want: checkNeeded}, + {name: "not signed in", serviceOK: true, credential: http.StatusNotFound, body: `{"error_code":"NOT_FOUND","message":"not found"}`, want: checkNeeded}, + {name: "rejected token", serviceOK: true, credential: http.StatusUnauthorized, body: `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`, want: statusUnknown, warns: true}, + {name: "service gone", serviceOK: false, credential: http.StatusNotFound, body: `{"error_code":"NOT_FOUND","message":"not found"}`, want: statusUnknown, warns: true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + serviceStatus := http.StatusOK + if !tc.serviceOK { + serviceStatus = http.StatusNotFound + } + server := listCheckServer(t, serviceStatus, tc.credential, tc.body) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + status, err := signInStatus(t.Context(), w, "logfood", "system.ai.github") + assert.Equal(t, tc.want, status) + if tc.warns { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + } +} From f87cb1cfb23e4739c588bc7105c5f632d4c1aa5a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:04:47 +0000 Subject: [PATCH 12/87] Tidy the sign-in messages and drop the internal references from comments The sign-in hints shell-quote the service and profile they print, and share one login command. The sign-in notice names the profile only when the page is about to open, so a login that is already signed in does not print it. The sign-in error messages start lowercase, as the package's other errors do. list's --check comments and command docs no longer cite the design document, and the list command documents that only --check makes network calls. Co-authored-by: Isaac --- .../aitools/mcp/login-flips-active/output.txt | 1 - cmd/aitools/mcp_add.go | 3 +-- cmd/aitools/mcp_list.go | 7 +++---- cmd/aitools/mcp_login.go | 19 ++++++++++++++----- cmd/aitools/mcp_login_test.go | 6 ++++++ libs/aitools/mcp/login.go | 5 ++++- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt index 413d2d7830d..16f4d556124 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -25,7 +25,6 @@ Public repositories only until a repository owner installs https://github.com/ap === login again when already signed in returns at once >>> [CLI] aitools mcp login system.ai.github --profile logfood --output json -Sign in with the same account you use for Databricks profile logfood. Signed in to system.ai.github on profile logfood. Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. { diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 4cbad78bbfc..464e4f2fcbc 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -21,7 +21,6 @@ import ( "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" - "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" @@ -1045,7 +1044,7 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp // signInHint is the command that signs the profile in to a sign-in-required service. func signInHint(service, profileName string) string { - return fmt.Sprintf("%s needs sign-in. Run: databricks aitools mcp login %s --profile %s", service, shellquote.BashArg(service), shellquote.BashArg(profileName)) + return fmt.Sprintf("%s needs sign-in. Run: %s", service, mcpLoginHint(service, profileName)) } // installedAnywhere reports whether the server reached at least one agent. diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index f6d5b0d608a..ca3df951ffb 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -26,8 +26,7 @@ import ( "github.com/spf13/cobra" ) -// Results of the `mcp list --check` checks that are not server statuses. They are the JSON values of SPEC 4.6; -// statusOK and statusUnknown carry the other two. +// Results of the `mcp list --check` checks that are not server statuses. statusOK and statusUnknown carry the other two. const ( checkFailed = "failed" checkNeeded = "needed" @@ -127,8 +126,8 @@ type profileKey struct { 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. +// NewMcpListCmd returns `aitools mcp list`. Without --check it reads the state file and the agent configs only, and +// makes no network calls. Drift and unreadable entries are reported, not failed: the command exits 0. func NewMcpListCmd() *cobra.Command { var check bool cmd := &cobra.Command{ diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 1043eb46198..e7f611c904a 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -13,6 +13,7 @@ import ( "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/browser" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" @@ -193,11 +194,19 @@ func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceCli return nil } +// mcpLoginCommand is the command that signs a profile in to a service. The hints name it. +const mcpLoginCommand = "databricks aitools mcp login" + +// mcpLoginHint returns the command that signs profileName in to service. +func mcpLoginHint(service, profileName string) string { + return mcpLoginCommand + " " + shellquote.BashArg(service) + " --profile " + shellquote.BashArg(profileName) +} + // signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns // the sign-in state name and the failure, which is categorized for JSON. `add --login` shares it. func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, url string, svc mcp.Service) (string, error) { - fmt.Fprintf(errOut, "Sign in with the same account you use for Databricks profile %s.\n", profileName) err := mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ + Profile: profileName, Poll: mcp.SignInPollInterval, Timeout: mcp.SignInWaitTimeout, Open: browser.Open, @@ -205,12 +214,12 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient switch { case errors.Is(err, mcp.ErrSignInTimeout): return signInStateTimeout, failure(categorySignInTimeout, err, - "Sign-in to %s did not complete in %g minutes. Finish at %s then retry: databricks aitools mcp login %s --profile %s", - svc.FullName, mcp.SignInWaitTimeout.Minutes(), url, svc.FullName, profileName) + "sign-in to %s did not complete in %g minutes. Finish at %s then retry: %s", + svc.FullName, mcp.SignInWaitTimeout.Minutes(), url, mcpLoginHint(svc.FullName, profileName)) case errors.Is(err, mcp.ErrSignInFailed): return signInStateFailed, failure(protos.AitoolsErrorCategoryUncategorized, err, - "Sign-in to %s failed on profile %s. Run: databricks aitools mcp login %s --profile %s", - svc.FullName, profileName, svc.FullName, profileName) + "sign-in to %s failed on profile %s. Run: %s", + svc.FullName, profileName, mcpLoginHint(svc.FullName, profileName)) case err != nil: return "", describeRequestError(profileName, err) } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index d043466aa7c..4400e43396e 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -156,3 +156,9 @@ func TestSignInInstalledSignsInToEveryServer(t *testing.T) { assert.Contains(t, errOut.String(), "system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood\n") assert.NotContains(t, errOut.String(), "system.ai.genie_one_mcp needs sign-in") } + +// TestMcpLoginHintQuotesItsArguments checks that the login command a hint prints can be pasted into a shell as written. +func TestMcpLoginHintQuotesItsArguments(t *testing.T) { + assert.Equal(t, "databricks aitools mcp login system.ai.github --profile logfood", mcpLoginHint("system.ai.github", "logfood")) + assert.Equal(t, "databricks aitools mcp login system.ai.github --profile 'my profile'", mcpLoginHint("system.ai.github", "my profile")) +} diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 33d4c2df420..467e3548151 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -25,8 +25,10 @@ var ErrSignInTimeout = errors.New("sign-in did not complete before the timeout") // ErrSignInFailed means the credential reached the FAILED provisioning state. var ErrSignInFailed = errors.New("sign-in failed") -// SignInOptions controls how SignIn waits. Callers set all three fields. +// SignInOptions controls how SignIn waits. Callers set all four fields. type SignInOptions struct { + // Profile is the profile whose user signs in. The notice before the page names it. + Profile string Poll time.Duration Timeout time.Duration // Open opens the sign-in page. It is not called when BROWSER=none, because the URL is already printed. @@ -63,6 +65,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str return nil } + fmt.Fprintf(out, "Sign in with the same account you use for Databricks profile %s.\n", opts.Profile) fmt.Fprintf(out, "Open this URL to sign in to %s:\n%s\n", service, url) if !browser.IsDisabled(ctx) { if err := opts.Open(ctx, url); err != nil { From d4f795a05b9183d9d066d2b5fec93d73b53b004b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:05:17 +0000 Subject: [PATCH 13/87] Acceptance: set MSYS_NO_PATHCONV on the sign-in credential calls and wait longer for the background sign-in Git Bash on Windows rewrites the /api/ path of the credential POSTs unless MSYS_NO_PATHCONV is set. The login acceptance test waits for the background command's sign-in line with a retry budget of 30 seconds, instead of the default 2.5 seconds. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/list-check/script | 2 +- acceptance/cmd/aitools/mcp/login-flips-active/script | 4 ++-- acceptance/cmd/aitools/mcp/login-flips-active/test.toml | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/login-flips-active/test.toml diff --git a/acceptance/cmd/aitools/mcp/list-check/script b/acceptance/cmd/aitools/mcp/list-check/script index b20525705ba..ce01abd27a8 100644 --- a/acceptance/cmd/aitools/mcp/list-check/script +++ b/acceptance/cmd/aitools/mcp/list-check/script @@ -15,7 +15,7 @@ title "list --check: the token mints, and GitHub needs sign-in" trace $CLI aitools mcp list --check title "Once the sign-in completes, GitHub is signed in" -trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null +MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null trace $CLI aitools mcp list --check title "A recorded binary that is gone is reported as missing" diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/script b/acceptance/cmd/aitools/mcp/login-flips-active/script index a2bd27ab5ec..207e0e18a70 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/script +++ b/acceptance/cmd/aitools/mcp/login-flips-active/script @@ -17,7 +17,7 @@ title "login prints the sign-in URL and waits; the sign-in completes in the brow $CLI aitools mcp login system.ai.github --profile logfood > login.txt 2>&1 & PID=$! trace retry --until "Waiting for sign-in" cat login.txt -trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null +MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null wait $PID trace cat login.txt @@ -32,7 +32,7 @@ title "add --login installs the server and signs in during the same run" $CLI aitools mcp add --profile logfood --names system.ai.github --agents claude-code --login > add.txt 2>&1 & PID=$! trace retry --until "Waiting for sign-in" cat add.txt -trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null +MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null wait $PID trace cat add.txt trace jq '.mcp | [.servers[] | {name, service, profile, agents: (.agents | keys)}]' home/.databricks/aitools/skills/.state.json diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/test.toml b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml new file mode 100644 index 00000000000..59082bdcb7e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml @@ -0,0 +1,4 @@ +# The background login and add print their sign-in line after a moment, which a slow runner can take longer than the +# retry default of 2.5 seconds to show. The retries wait up to 30 seconds. +Env.RETRY_MAX_ATTEMPTS = "60" +Env.RETRY_INTERVAL_MS = "500" From ba9fe086eff83654196416ca4b375dc9b89e031f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:05:56 +0000 Subject: [PATCH 14/87] Format and lint the list and login tests Co-authored-by: Isaac --- cmd/aitools/mcp_list_test.go | 2 +- cmd/aitools/mcp_login_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 2bb8f6e81be..bb647217ca7 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -933,7 +933,7 @@ func TestBuildMcpListAndRemoveAgreeAfterConfigMoved(t *testing.T) { // listCheckServer answers the GitHub service and the caller's sign-in to it, for --check. The service answers with // serviceStatus, and the credential with credentialStatus and credentialBody. Other API paths are NOT_FOUND. -func listCheckServer(t *testing.T, serviceStatus int, credentialStatus int, credentialBody string) *httptest.Server { +func listCheckServer(t *testing.T, serviceStatus, credentialStatus int, credentialBody string) *httptest.Server { t.Helper() const servicePath = "/api/2.1/unity-catalog/mcp-services/system.ai.github" server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 4400e43396e..9ec3b1dde82 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -104,7 +104,7 @@ func TestMcpLogoutUnknownServiceIsAnError(t *testing.T) { out := mcpSignInOutput{Service: "system.ai.gihtub"} err := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.gihtub", mcpLogout, &out) require.Error(t, err) - assert.Equal(t, protos.AitoolsErrorCategory(categoryServiceNotFound), errorCategory(err)) + assert.Equal(t, categoryServiceNotFound, errorCategory(err)) assert.Contains(t, err.Error(), "system.ai.gihtub is not an MCP service visible to profile logfood") assert.Empty(t, out.State) } From d944fd6b382d43aff63d177d8c1ced934790ec10 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:29:17 +0000 Subject: [PATCH 15/87] Escape service names in credential and service requests The SDK puts a resource name into the request path unescaped. A service name with a '#' or '?' cut the path short, so logout of such a name sent DELETE to the service itself. The service part of each request is now escaped, and PathSafeName reports the names that change a path. --- libs/aitools/mcp/discovery.go | 17 +++++++++++++- libs/aitools/mcp/login.go | 4 ++-- libs/aitools/mcp/login_test.go | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index b3400cb5c9c..22840ebbcc6 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -6,8 +6,10 @@ import ( "context" "errors" "fmt" + "net/url" "slices" "strings" + "unicode" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" @@ -17,6 +19,19 @@ import ( // mcpServicePrefix is the resource-name prefix of an MCP service. const mcpServicePrefix = "mcp-services/" +// PathSafeName reports whether fullName is free of the characters that change a request path: "/", "?", "#", "%" and +// control characters. A service name never contains them, so a name that has one is refused, not escaped. +func PathSafeName(fullName string) bool { + return !strings.ContainsAny(fullName, "/?#%") && !strings.ContainsFunc(fullName, unicode.IsControl) +} + +// serviceResourceName returns the resource name of the service fullName for a request. The SDK puts the name into the +// request path as it is, so the name is escaped here: an unescaped "#" or "?" would cut the path short, and "/" or "%" +// would name another resource. +func serviceResourceName(fullName string) string { + return mcpServicePrefix + url.PathEscape(fullName) +} + // oauthProviderOption is the source connection option that marks a service as needing sign-in. const oauthProviderOption = "oauth_provider" @@ -69,7 +84,7 @@ func Discover(ctx context.Context, w *databricks.WorkspaceClient, location strin // answers NOT_FOUND (apierr.ErrNotFound) for a service the profile cannot see, and so does a // service whose source connection was deleted, which Discover drops. func GetService(ctx context.Context, w *databricks.WorkspaceClient, fullName string) (Service, error) { - svc, err := w.AiGateway.GetMcpService(ctx, catalog.GetMcpServiceRequest{Name: mcpServicePrefix + fullName}) + svc, err := w.AiGateway.GetMcpService(ctx, catalog.GetMcpServiceRequest{Name: serviceResourceName(fullName)}) if err != nil { return Service{}, err } diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 467e3548151..17807a0dc3c 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -39,7 +39,7 @@ type SignInOptions struct { // service. It is "" when the caller has not signed in: the API answers NOT_FOUND until then. func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service string) (catalog.ProvisioningInfoState, error) { cred, err := w.AiGateway.GetMcpServiceUserMappedCredential(ctx, catalog.GetMcpServiceUserMappedCredentialRequest{ - Name: mcpServicePrefix + service, + Name: serviceResourceName(service), }) if errors.Is(err, apierr.ErrNotFound) { return "", nil @@ -124,7 +124,7 @@ func waitEnded(ctx context.Context) error { // signed in, which is not an error. func SignOut(ctx context.Context, w *databricks.WorkspaceClient, service string) (bool, error) { _, err := w.AiGateway.DeleteMcpServiceUserMappedCredential(ctx, catalog.DeleteMcpServiceUserMappedCredentialRequest{ - Name: mcpServicePrefix + service, + Name: serviceResourceName(service), }) if errors.Is(err, apierr.ErrNotFound) { return false, nil diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 22dd85c16f2..b7fc349b30c 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/http/httptest" + "net/url" "strings" "sync" "sync/atomic" @@ -14,6 +15,7 @@ import ( "time" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -275,3 +277,42 @@ func TestSignInTimeoutCutsOffPendingRequest(t *testing.T) { require.ErrorIs(t, err, ErrSignInTimeout) assert.Less(t, time.Since(start), 5*time.Second) } + +// TestServiceNamesReachTheirOwnCredential checks that a service name with a character that means something in a URL path +// is escaped in the request, so that the request names the credential and never the service. The SDK puts the name into +// the path as it is, so an unescaped "#" would send a DELETE to the service itself. +func TestServiceNamesReachTheirOwnCredential(t *testing.T) { + for _, name := range []string{"system.ai.git#hub", "system.ai.git?hub", "system.ai.git/hub", "system.ai.git%hub"} { + t.Run(name, func(t *testing.T) { + var mu sync.Mutex + var requests []string + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + rw.WriteHeader(http.StatusNotFound) + return + } + mu.Lock() + requests = append(requests, r.Method+" "+r.URL.EscapedPath()) + mu.Unlock() + http.Error(rw, `{"error_code":"NOT_FOUND","message":"not found"}`, http.StatusNotFound) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + _, err = SignOut(t.Context(), w, name) + require.NoError(t, err) + _, err = CredentialState(t.Context(), w, name) + require.NoError(t, err) + _, err = GetService(t.Context(), w, name) + require.ErrorIs(t, err, apierr.ErrNotFound) + + escaped := "/api/2.1/unity-catalog/mcp-services/" + url.PathEscape(name) + assert.Equal(t, []string{ + "DELETE " + escaped + "/user-credentials", + "GET " + escaped + "/user-credentials", + "GET " + escaped, + }, requests) + }) + } +} From 7d437220135f649f8b85847b35ebd8d6e9408de8 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:29:28 +0000 Subject: [PATCH 16/87] Refuse service names with URL syntax in login and logout A service name with "/", "?", "#", "%" or a control character is refused before any request, with the same message as a name without three parts. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 7 ++++++- cmd/aitools/mcp_login_test.go | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index e7f611c904a..1c27912d2ca 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -108,8 +108,13 @@ func runMcpSignInCommand(cmd *cobra.Command, service string, action mcpSignInAct return nil } +// isServiceName reports whether service is a full name that can be sent in a request path as it is. +func isServiceName(service string) bool { + return isFullName(strings.Split(service, ".")) && mcp.PathSafeName(service) +} + func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service string, action mcpSignInAction, out *mcpSignInOutput) error { - if !isFullName(strings.Split(service, ".")) { + if !isServiceName(service) { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q", service) } // The config file is resolved once, as add resolves it, so that the profile lookup and the client read the same file. diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 9ec3b1dde82..0ea585a5a0b 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -2,6 +2,7 @@ package aitools import ( "bytes" + "fmt" "io" "net/http" "net/http/httptest" @@ -19,12 +20,14 @@ import ( ) func TestMcpSignInRequiresFullServiceName(t *testing.T) { - for _, service := range []string{"github", "system.github", "system..github", "a.b.c.d"} { + // A name with a character that is URL syntax, or a control character, is refused before any request: the request + // path would name another resource. + for _, service := range []string{"github", "system.github", "system..github", "a.b.c.d", "system.ai.git#hub", "system.ai.git?hub", "system.ai.git/hub", "system.ai.git%hub", "system.ai.git\nhub"} { t.Run(service, func(t *testing.T) { var out mcpSignInOutput err := runMcpSignIn(cmdio.MockDiscard(t.Context()), &bytes.Buffer{}, "", service, mcpLogin, &out) require.Error(t, err) - assert.Equal(t, `service must be a full name such as system.ai.github, got "`+service+`"`, err.Error()) + assert.Equal(t, fmt.Sprintf("service must be a full name such as system.ai.github, got %q", service), err.Error()) assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) }) } From 6a6414c2bf40ce79f91723c83e80c03eef0add5d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:29:30 +0000 Subject: [PATCH 17/87] Refuse service names with URL syntax in serve The endpoint already escapes the name. This refuses the same names as login and logout before serve reads the state, so the proxy never starts for them. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 2 +- cmd/aitools/mcp_serve_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index afa2fbbf6cd..66195d34d9c 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -214,7 +214,7 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { if f.service == "" { return fmt.Errorf("%s is required", mcpconfig.ServiceFlag) } - if !isFullName(strings.Split(f.service, ".")) { + if !isServiceName(f.service) { return fmt.Errorf("%s needs a full service name such as system.ai.github", mcpconfig.ServiceFlag) } diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 6f0ec68e1db..3541727eed0 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -300,7 +300,7 @@ func TestServeAgentKeepsOnlyKnownAgents(t *testing.T) { // any binding check, with the flag named. func TestMcpServeRejectsServiceWithoutThreeParts(t *testing.T) { withHome(t) - for _, service := range []string{"github", "system.ai", "system..github", "a.b.c.d"} { + for _, service := range []string{"github", "system.ai", "system..github", "a.b.c.d", "system.ai.git#hub", "system.ai.git?hub", "system.ai.git/hub"} { t.Run(service, func(t *testing.T) { parent := newMCPTestRoot() parent.SetArgs([]string{"mcp", "serve", "--profile", "logfood", "--service", service}) From 6d9d398d42df8eb980e3e9eb17e59f450364cc99 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:30:14 +0000 Subject: [PATCH 18/87] Delete a FAILED sign-in left by an earlier attempt before the page opens A FAILED credential from an earlier attempt stayed FAILED when the new attempt failed too, so the wait never saw a change and ended in a timeout. SignIn now deletes a FAILED credential before it opens the page, so any FAILED state it then sees is the result of this attempt. Co-authored-by: Isaac --- libs/aitools/mcp/login.go | 16 +++++++----- libs/aitools/mcp/login_test.go | 48 +++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 17807a0dc3c..2939629b074 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -54,7 +54,8 @@ func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service } // SignIn signs the caller in to a sign-in-required service, at url from SignInURL. It returns nil at once when the -// caller is already signed in. Otherwise it prints url to out, opens it, and polls until the credential is ACTIVE. +// caller is already signed in. Otherwise it deletes a FAILED credential left by an earlier attempt, prints url to out, +// opens it, and polls until the credential is ACTIVE. // The errors are ErrSignInTimeout, ErrSignInFailed, or an API error. func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service string, out io.Writer, opts SignInOptions) error { state, err := CredentialState(ctx, w, service) @@ -64,6 +65,13 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str if state == catalog.ProvisioningInfoStateActive { return nil } + if state == catalog.ProvisioningInfoStateFailed { + // A FAILED credential is left by an earlier attempt. It is deleted, so that a FAILED state seen while waiting is + // the result of this attempt, and so that the page starts from no credential. + if _, err := SignOut(ctx, w, service); err != nil { + return err + } + } fmt.Fprintf(out, "Sign in with the same account you use for Databricks profile %s.\n", opts.Profile) fmt.Fprintf(out, "Open this URL to sign in to %s:\n%s\n", service, url) @@ -84,9 +92,6 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str ticker := time.NewTicker(opts.Poll) defer ticker.Stop() - // A FAILED state left by an earlier attempt is still there when this one starts. FAILED counts only once the - // state has changed since the previous read, so it is the result of this attempt. - previous := state for { select { case <-waitCtx.Done(): @@ -105,10 +110,9 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str if state == catalog.ProvisioningInfoStateActive { return nil } - if state == catalog.ProvisioningInfoStateFailed && state != previous { + if state == catalog.ProvisioningInfoStateFailed { return ErrSignInFailed } - previous = state } } diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index b7fc349b30c..7078e5f3fea 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/catalog" @@ -79,6 +80,18 @@ func noopOpen(context.Context, string) error { return nil } +// browserOpens returns a context in which the browser is not disabled, so that SignIn calls the test's Open. The +// runner's BROWSER=none would otherwise skip it. +func browserOpens(t *testing.T) context.Context { + return env.Set(t.Context(), "BROWSER", "test-opener") +} + +func (s *loginTestServer) isDeleted() bool { + s.mu.Lock() + defer s.mu.Unlock() + return s.deleted +} + // loginTestClient starts the fake and returns a client for it. func loginTestClient(t *testing.T, s *loginTestServer) *databricks.WorkspaceClient { t.Helper() @@ -216,19 +229,40 @@ func TestSignOut(t *testing.T) { assert.False(t, deleted) } -// TestSignInFailedFromEarlierAttemptIsNotTheResult checks that a FAILED credential left by an earlier attempt is not the -// result of this one. The state only counts as failed once it changes, so the new sign-in can still complete. -func TestSignInFailedFromEarlierAttemptIsNotTheResult(t *testing.T) { - s := &loginTestServer{states: []string{"FAILED", "FAILED", "ACTIVE"}} +// TestSignInDeletesFailedCredentialFromEarlierAttempt checks that a FAILED credential left by an earlier attempt is +// deleted before the page opens, so the FAILED state the wait then sees is not the earlier attempt's, and the new +// sign-in can complete. +func TestSignInDeletesFailedCredentialFromEarlierAttempt(t *testing.T) { + s := &loginTestServer{states: []string{"FAILED", "", "ACTIVE"}, signedIn: true} w := loginTestClient(t, s) - err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + deletedBeforeOpen := make(chan bool, 1) + err := SignIn(browserOpens(t), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, - Open: noopOpen, + Open: func(context.Context, string) error { + deletedBeforeOpen <- s.isDeleted() + return nil + }, }) require.NoError(t, err) - assert.Equal(t, 3, s.reads) + assert.True(t, <-deletedBeforeOpen) +} + +// TestSignInFailedCredentialThatFailsAgainIsFailed checks that a sign-in that fails again, after the FAILED credential +// of an earlier attempt was deleted, is reported as failed at once. Without the deletion the state would never change, +// and the wait would end in a timeout. +func TestSignInFailedCredentialThatFailsAgainIsFailed(t *testing.T) { + s := &loginTestServer{states: []string{"FAILED"}, signedIn: true} + w := loginTestClient(t, s) + + err := SignIn(browserOpens(t), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 2 * time.Second, + Open: noopOpen, + }) + require.ErrorIs(t, err, ErrSignInFailed) + assert.True(t, s.isDeleted()) } // TestSignInFailedAfterProgressIsFailed checks that a FAILED state that follows a change of state fails the sign-in. From 5efccfff20240b0fadcf34a92217848850a9c556 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:30:26 +0000 Subject: [PATCH 19/87] Name the unrelated-error check in mapListError Behavior is unchanged. The check moves into isUnrelatedError so that the sign-in lookups can use it too. Co-authored-by: Isaac --- libs/aitools/mcp/discovery.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index 22840ebbcc6..a34b6d2d4b7 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -145,13 +145,17 @@ func listServices(ctx context.Context, w *databricks.WorkspaceClient, location s // blocked the request. They keep the server's message and get no location or grants hint. var unrelatedErrorCodes = []string{"ENDPOINT_NOT_FOUND", "FEATURE_DISABLED", "PRIVATE_LINK_VALIDATION_ERROR"} +// isUnrelatedError reports whether err is one of the unrelatedErrorCodes. +func isUnrelatedError(err error) bool { + apiErr, ok := errors.AsType[*apierr.APIError](err) + return ok && slices.Contains(unrelatedErrorCodes, apiErr.ErrorCode) +} + // mapListError classifies a listing failure. Other errors are returned with context only. func mapListError(location string, err error) error { - apiErr, isAPIErr := errors.AsType[*apierr.APIError](err) - var kind ListErrorKind switch { - case isAPIErr && slices.Contains(unrelatedErrorCodes, apiErr.ErrorCode): + case isUnrelatedError(err): // Not about the schema or grants: leave it unclassified. case errors.Is(err, apierr.ErrNotFound): kind = ListErrLocationNotFound From 52ebf5595473aaadfa901e895323385c29fefff3 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:31:12 +0000 Subject: [PATCH 20/87] Treat only the service's own 404 as not found in sign-in A 404 for an endpoint the API does not have (ENDPOINT_NOT_FOUND) was read as "not signed in" or "not an MCP service", so a failure of the API looked like an answer about the caller. IsNotFound excludes the same unrelated codes that discovery leaves unclassified, and sign-in, logout and login use it. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 3 +-- cmd/aitools/mcp_login_test.go | 14 ++++++++++++++ libs/aitools/mcp/discovery.go | 6 ++++++ libs/aitools/mcp/login.go | 5 ++--- libs/aitools/mcp/login_test.go | 23 +++++++++++++++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 1c27912d2ca..b5203544bb9 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -16,7 +16,6 @@ import ( "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/apierr" "github.com/spf13/cobra" ) @@ -149,7 +148,7 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri // misspelled name is reported rather than treated as a service with no sign-in. func signInService(ctx context.Context, w *databricks.WorkspaceClient, out *mcpSignInOutput) (mcp.Service, error) { svc, err := mcp.GetService(ctx, w, out.Service) - if errors.Is(err, apierr.ErrNotFound) { + if mcp.IsNotFound(err) { return mcp.Service{}, failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", out.Service, out.Profile) } if err != nil { diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 0ea585a5a0b..e31c0bae760 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -112,6 +112,20 @@ func TestMcpLogoutUnknownServiceIsAnError(t *testing.T) { assert.Empty(t, out.State) } +// TestMcpLogoutEndpointNotFoundIsNotAServiceError checks that a 404 for an endpoint the API does not have is not read as a +// service the profile cannot see, which would send the user to look for a typo in the name. +func TestMcpLogoutEndpointNotFoundIsNotAServiceError(t *testing.T) { + home := withHome(t) + server := loginTestServer(t, http.StatusNotFound, `{"error_code":"ENDPOINT_NOT_FOUND","message":"endpoint is not available"}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + out := mcpSignInOutput{Service: "system.ai.github"} + err := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.github", mcpLogout, &out) + require.Error(t, err) + assert.NotEqual(t, categoryServiceNotFound, errorCategory(err)) + assert.NotContains(t, err.Error(), "is not an MCP service visible") +} + // TestSignInInstalledSignsInToEveryServer checks that add --login attempts every installed sign-in server after one of // them fails. The failed server gets the login hint, and the failure is returned, so the others are still signed in. func TestSignInInstalledSignsInToEveryServer(t *testing.T) { diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index a34b6d2d4b7..cbbf4bde1e6 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -151,6 +151,12 @@ func isUnrelatedError(err error) bool { return ok && slices.Contains(unrelatedErrorCodes, apiErr.ErrorCode) } +// IsNotFound reports whether err is the API's answer that the resource does not exist. An unrelated 404, such as +// ENDPOINT_NOT_FOUND, is not: the API did not answer about the resource. +func IsNotFound(err error) bool { + return errors.Is(err, apierr.ErrNotFound) && !isUnrelatedError(err) +} + // mapListError classifies a listing failure. Other errors are returned with context only. func mapListError(location string, err error) error { var kind ListErrorKind diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 2939629b074..5f2353c6919 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -9,7 +9,6 @@ import ( "github.com/databricks/cli/libs/browser" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/catalog" ) @@ -41,7 +40,7 @@ func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service cred, err := w.AiGateway.GetMcpServiceUserMappedCredential(ctx, catalog.GetMcpServiceUserMappedCredentialRequest{ Name: serviceResourceName(service), }) - if errors.Is(err, apierr.ErrNotFound) { + if IsNotFound(err) { return "", nil } if err != nil { @@ -130,7 +129,7 @@ func SignOut(ctx context.Context, w *databricks.WorkspaceClient, service string) _, err := w.AiGateway.DeleteMcpServiceUserMappedCredential(ctx, catalog.DeleteMcpServiceUserMappedCredentialRequest{ Name: serviceResourceName(service), }) - if errors.Is(err, apierr.ErrNotFound) { + if IsNotFound(err) { return false, nil } if err != nil { diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 7078e5f3fea..33c17a77ee2 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -350,3 +350,26 @@ func TestServiceNamesReachTheirOwnCredential(t *testing.T) { }) } } + +// TestEndpointNotFoundIsNotTheAnswerAboutTheCredential checks that a 404 for an endpoint the API does not have is an +// error. It does not mean that the caller is not signed in, or that the credential is gone. +func TestEndpointNotFoundIsNotTheAnswerAboutTheCredential(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + rw.WriteHeader(http.StatusNotFound) + return + } + http.Error(rw, `{"error_code":"ENDPOINT_NOT_FOUND","message":"endpoint is not available"}`, http.StatusNotFound) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + state, err := CredentialState(t.Context(), w, loginTestService) + require.Error(t, err) + assert.Empty(t, state) + + deleted, err := SignOut(t.Context(), w, loginTestService) + require.Error(t, err) + assert.False(t, deleted) +} From f2ce7169de0c3e4c8f5bc1a2ce5af392d22e4f42 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:31:54 +0000 Subject: [PATCH 21/87] Share the service lookup of sign-in commands serviceFor is the one lookup that login and logout make of a service, with its not-found and request-error messages. The next change makes list --check use it as well. Behavior is unchanged. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index b5203544bb9..1f913943e60 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -144,21 +144,21 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri return action(ctx, errOut, w, out) } -// signInService returns the service of a sign-in command. A service the profile cannot see is an error, so that a -// misspelled name is reported rather than treated as a service with no sign-in. -func signInService(ctx context.Context, w *databricks.WorkspaceClient, out *mcpSignInOutput) (mcp.Service, error) { - svc, err := mcp.GetService(ctx, w, out.Service) +// serviceFor returns the service with its sign-in requirement, as the profile sees it. A service the profile cannot see +// is an error, so that a misspelled name is reported rather than treated as a service with no sign-in. +func serviceFor(ctx context.Context, w *databricks.WorkspaceClient, profileName, service string) (mcp.Service, error) { + svc, err := mcp.GetService(ctx, w, service) if mcp.IsNotFound(err) { - return mcp.Service{}, failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", out.Service, out.Profile) + return mcp.Service{}, failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", service, profileName) } if err != nil { - return mcp.Service{}, describeRequestError(out.Profile, err) + return mcp.Service{}, describeRequestError(profileName, err) } return svc, nil } func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { - svc, err := signInService(ctx, w, out) + svc, err := serviceFor(ctx, w, out.Profile, out.Service) if err != nil { return err } @@ -181,7 +181,7 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie } func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { - if _, err := signInService(ctx, w, out); err != nil { + if _, err := serviceFor(ctx, w, out.Profile, out.Service); err != nil { return err } deleted, err := mcp.SignOut(ctx, w, out.Service) From 65634e1e91b4222d326a482377a01580e7832187 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:32:12 +0000 Subject: [PATCH 22/87] Check sign-in against the service's requirement in list --check --check trusted the requirement recorded at install time. A service that now needs sign-in was never checked, and one that no longer needs it was reported as needing sign-in. Every server is now looked up, and the service's own requirement decides whether its sign-in is checked. A server that does not need sign-in has no sign_in value. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 25 +++++++++++----------- cmd/aitools/mcp_list_test.go | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index ca3df951ffb..edcc6361db7 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -21,7 +21,6 @@ import ( "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -436,11 +435,11 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput for j := range p.Servers { s := &p.Servers[j] s.Check = &mcpListCheck{Token: p.token, Binary: binaryStatus(s.entry.Command, exec.LookPath)} - if !s.SignInRequired { - continue - } - s.Check.SignIn = statusUnknown if err != nil { + // The sign-in is unknown when the profile cannot authenticate. The record says whether it was required. + if s.SignInRequired { + s.Check.SignIn = statusUnknown + } continue } status, signInErr := signInStatus(ctx, w, p.Profile, s.Service) @@ -451,16 +450,16 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput } } -// signInStatus reads the caller's sign-in to a service. Only an ACTIVE credential counts as signed in. A service the -// profile can no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error -// says why. +// signInStatus reads the caller's sign-in to a service, as the service requires it now. Only an ACTIVE credential +// counts as signed in. A service that does not require sign-in is "", which is not a status. A service the profile can +// no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error says why. func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, service string) (string, error) { - _, err := mcp.GetService(ctx, w, service) - if errors.Is(err, apierr.ErrNotFound) { - return statusUnknown, fmt.Errorf("%s is not an MCP service visible to profile %s", service, profileName) - } + svc, err := serviceFor(ctx, w, profileName, service) if err != nil { - return statusUnknown, describeRequestError(profileName, err) + return statusUnknown, err + } + if !svc.SignInRequired { + return "", nil } state, err := mcp.CredentialState(ctx, w, service) if err != nil { diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index bb647217ca7..cbcb0171985 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1153,3 +1153,44 @@ func TestSignInStatus(t *testing.T) { }) } } + +// TestAddChecksUsesTheRequirementTheServiceHasNow checks that --check asks the service whether it needs sign-in, rather +// than trusting the record: a service that now needs sign-in is checked, and one that no longer needs it is not reported +// as needing sign-in. +func TestAddChecksUsesTheRequirementTheServiceHasNow(t *testing.T) { + t.Run("now requires sign-in", func(t *testing.T) { + home := withHome(t) + server := listCheckServer(t, http.StatusOK, http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + entry := listCheckGitHub(t, "github-logfood", "logfood", server.URL, "") + entry.SignInRequired = false + saveState(t, entry) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Equal(t, checkNeeded, s.Check.SignIn) + assert.Empty(t, out.Warnings) + }) + + t.Run("no longer requires sign-in", func(t *testing.T) { + home := withHome(t) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.URL.Path { + case "/api/2.1/unity-catalog/mcp-services/system.ai.github": + // The service has no source connection with an OAuth provider any more. + _, _ = w.Write([]byte(`{"name":"mcp-services/system.ai.github"}`)) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(server.Close) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Empty(t, s.Check.SignIn) + assert.Empty(t, out.Warnings) + }) +} From b4bf0a4fdeaef111af364f9dfa566c870f1b1e41 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:33:07 +0000 Subject: [PATCH 23/87] Check the profile's token with a request in list --check A personal access token is sent only with a request, so the SDK's authenticate step succeeded for a revoked token and --check reported it as ok. Each profile now makes one current-user request, and a rejected token is failed with the login command, as in the other commands. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 7 +++++++ cmd/aitools/mcp_list_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index edcc6361db7..0bc94138e7d 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -22,6 +22,7 @@ import ( "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/catalog" + "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" ) @@ -427,6 +428,12 @@ func addChecks(ctx context.Context, out *mcpListOutput) { // has no sign-in checks: its warning says why. func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput) { w, err := profileClient(ctx, p.Profile, p.configPath) + if err == nil { + // A personal access token is sent only with a request, so the token is checked with one. + if _, err = w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { + err = describeRequestError(p.Profile, err) + } + } p.token = statusOK if err != nil { p.token = checkFailed diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index cbcb0171985..2f7831c8d4f 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -939,6 +939,8 @@ func listCheckServer(t *testing.T, serviceStatus, credentialStatus int, credenti server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.URL.Path { + case "/api/2.0/preview/scim/v2/Me": + _, _ = w.Write([]byte(`{"userName":"user@example.test"}`)) case servicePath: if serviceStatus != http.StatusOK { w.WriteHeader(serviceStatus) @@ -1177,6 +1179,8 @@ func TestAddChecksUsesTheRequirementTheServiceHasNow(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.URL.Path { + case "/api/2.0/preview/scim/v2/Me": + _, _ = w.Write([]byte(`{"userName":"user@example.test"}`)) case "/api/2.1/unity-catalog/mcp-services/system.ai.github": // The service has no source connection with an OAuth provider any more. _, _ = w.Write([]byte(`{"name":"mcp-services/system.ai.github"}`)) @@ -1194,3 +1198,29 @@ func TestAddChecksUsesTheRequirementTheServiceHasNow(t *testing.T) { assert.Empty(t, out.Warnings) }) } + +// TestAddChecksRejectedTokenFailsTheTokenCheck checks that --check asks the workspace whether the profile's token is +// accepted. A personal access token is sent only with a request, so a revoked token would otherwise be reported as ok. +func TestAddChecksRejectedTokenFailsTheTokenCheck(t *testing.T) { + home := withHome(t) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/2.0/preview/scim/v2/Me" { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`)) + return + } + w.WriteHeader(http.StatusNotFound) + })) + t.Cleanup(server.Close) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-revoked\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Equal(t, checkFailed, s.Check.Token) + assert.Equal(t, statusUnknown, s.Check.SignIn) + require.Len(t, out.Warnings, 1) + assert.True(t, strings.HasPrefix(out.Warnings[0], "profile logfood: "), out.Warnings[0]) + assert.Contains(t, out.Warnings[0], "Run: databricks auth login --profile logfood") +} From c07d09f98ade40196d384e668cffb5838751c232 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:33:29 +0000 Subject: [PATCH 24/87] Report a directory or an empty command as a missing binary in list --check An empty recorded command resolves to the working directory, and a directory was reported ok. Neither is a binary now. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 8 +++++--- cmd/aitools/mcp_list_test.go | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 0bc94138e7d..9e572eb37ed 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -493,14 +493,16 @@ func withConfigFile(ctx context.Context, path string, fn func(ctx context.Contex return os.Setenv(mcpconfig.ConfigFileEnv, previous) } -// binaryStatus reports whether the recorded command exists. It resolves the command as binaryPathFor does: an -// absolute path must be on disk, and a bare name must be found by lookPath. +// binaryStatus reports whether the recorded command is a binary. It resolves the command as binaryPathFor does: an +// absolute path must be a file on disk, and a bare name must be found by lookPath. An empty command resolves to the +// working directory, and a directory is not a binary, so both are missing. func binaryStatus(command string, lookPath func(string) (string, error)) string { path, absolute := binaryPathFor(command, lookPath) if !absolute { return checkFailed } - if _, err := os.Stat(path); err != nil { + info, err := os.Stat(path) + if err != nil || info.IsDir() { return checkFailed } return statusOK diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 2f7831c8d4f..024e036a17c 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1117,6 +1117,9 @@ func TestBinaryStatus(t *testing.T) { assert.Equal(t, checkFailed, binaryStatus(missing, found)) assert.Equal(t, statusOK, binaryStatus("databricks", found)) assert.Equal(t, checkFailed, binaryStatus("databricks", notFound)) + // An empty command resolves to the working directory, and a directory is not a binary. + assert.Equal(t, checkFailed, binaryStatus("", found)) + assert.Equal(t, checkFailed, binaryStatus(t.TempDir(), notFound)) } // TestSignInStatus checks the sign-in state that --check reports for one service. From 2e1a0c1e9c9f6a972cfcf95ad3aa1569094f6811 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:34:24 +0000 Subject: [PATCH 25/87] Name the profile in login and logout JSON as soon as it is resolved A failure in the binding check left the profile empty in the document. The profile is now set when it resolves and the host when the client exists, and empty profile, host and state fields are left out. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 15 ++++++--------- cmd/aitools/mcp_login_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 1f913943e60..9389de479b0 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -35,16 +35,14 @@ const ( // mcpSignInOutput is the `mcp login` and `mcp logout` document. type mcpSignInOutput struct { - Service string `json:"service"` - Profile string `json:"profile"` - Host string `json:"host"` - State string `json:"state"` + Service string `json:"service"` + // Profile and Host are set once they are known, so a failure before that leaves them out. + Profile string `json:"profile,omitempty"` + Host string `json:"host,omitempty"` + State string `json:"state,omitempty"` URL string `json:"url,omitempty"` ErrorCategory protos.AitoolsErrorCategory `json:"error_category,omitempty"` Error string `json:"error,omitempty"` - - // configFile is the resolved config file that the profile is read from. It is not in the JSON. - configFile string } // mcpSignInAction runs login or logout against the profile's client and records the result in out. @@ -131,6 +129,7 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri if err != nil { return err } + out.Profile = profileName if _, err := mcp.CheckBinding(ctx, state, service, profileName, configFile); err != nil { return err } @@ -138,9 +137,7 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri if err != nil { return err } - out.Profile = profileName out.Host = w.Config.Host - out.configFile = configFile return action(ctx, errOut, w, out) } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index e31c0bae760..e6ec46e9d61 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -2,6 +2,7 @@ package aitools import ( "bytes" + "encoding/json" "fmt" "io" "net/http" @@ -10,6 +11,7 @@ import ( "sync" "testing" + "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/cmdio" @@ -179,3 +181,26 @@ func TestMcpLoginHintQuotesItsArguments(t *testing.T) { assert.Equal(t, "databricks aitools mcp login system.ai.github --profile logfood", mcpLoginHint("system.ai.github", "logfood")) assert.Equal(t, "databricks aitools mcp login system.ai.github --profile 'my profile'", mcpLoginHint("system.ai.github", "my profile")) } + +// TestMcpLoginJSONNamesProfileWhenBindingFails checks that a failure after the profile is resolved still names the profile +// in the document, and that a state the command never reached is left out rather than sent empty. +func TestMcpLoginJSONNamesProfileWhenBindingFails(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test"}) + + parent := newMCPTestRoot() + var stdout bytes.Buffer + parent.SetOut(&stdout) + parent.SetErr(&bytes.Buffer{}) + parent.SetArgs([]string{"mcp", "login", "system.ai.github", "--profile", "logfood", "--output", "json"}) + err := parent.ExecuteContext(cmdio.MockDiscard(t.Context())) + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + + var out mcpSignInOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &out), stdout.String()) + assert.Equal(t, "logfood", out.Profile) + assert.Equal(t, "PROFILE_HOST_CHANGED", string(out.ErrorCategory)) + assert.NotContains(t, stdout.String(), `"state"`) +} From 50af91655f3f271e416bb6ed602b3d7d39bb0c12 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:34:50 +0000 Subject: [PATCH 26/87] Keep the run error when login or logout JSON cannot be written A failed write returned only the write error, so the reason the sign-in failed was lost. Both errors are returned, as add and remove do. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 2 +- cmd/aitools/mcp_login_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 9389de479b0..a85c2292efc 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -97,7 +97,7 @@ func runMcpSignInCommand(cmd *cobra.Command, service string, action mcpSignInAct out.ErrorCategory = errorCategory(runErr) } if err := renderJSON(cmd.OutOrStdout(), out); err != nil { - return err + return errors.Join(runErr, err) } if runErr != nil { return root.ErrAlreadyPrinted diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index e6ec46e9d61..ea2a0fd868d 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -204,3 +204,21 @@ func TestMcpLoginJSONNamesProfileWhenBindingFails(t *testing.T) { assert.Equal(t, "PROFILE_HOST_CHANGED", string(out.ErrorCategory)) assert.NotContains(t, stdout.String(), `"state"`) } + +// TestMcpSignInJSONWriteFailureKeepsRunError checks that a document that cannot be written does not hide why the command +// failed, as add and remove keep it. +func TestMcpSignInJSONWriteFailureKeepsRunError(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveEntry(t, mcp.Entry{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test"}) + + parent := newMCPTestRoot() + parent.SetOut(failingWriter{}) + parent.SetErr(&bytes.Buffer{}) + parent.SetArgs([]string{"mcp", "login", "system.ai.github", "--profile", "logfood", "--output", "json"}) + err := parent.ExecuteContext(cmdio.MockDiscard(t.Context())) + require.Error(t, err) + assert.ErrorContains(t, err, "disk full") + assert.ErrorContains(t, err, "MCP server github-logfood was set up for https://adb-123.test") +} From 9e870deb288fd6e810c0e6bd3ec00ba0549cfa79 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:36:38 +0000 Subject: [PATCH 27/87] Set the browser explicitly in the sign-in tests that expect it to open The tests inherited BROWSER=none from the runner, so the page was never opened and the tests waited for it, or failed their URL assertion. They now pass a context in which the browser is not disabled. Co-authored-by: Isaac --- libs/aitools/mcp/login_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 33c17a77ee2..d1f37fdbb1c 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -82,8 +82,8 @@ func noopOpen(context.Context, string) error { // browserOpens returns a context in which the browser is not disabled, so that SignIn calls the test's Open. The // runner's BROWSER=none would otherwise skip it. -func browserOpens(t *testing.T) context.Context { - return env.Set(t.Context(), "BROWSER", "test-opener") +func browserOpens(ctx context.Context) context.Context { + return env.Set(ctx, "BROWSER", "test-opener") } func (s *loginTestServer) isDeleted() bool { @@ -108,7 +108,7 @@ func TestSignInPollsUntilActive(t *testing.T) { var opened []string var out bytes.Buffer - err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(_ context.Context, url string) error { @@ -165,7 +165,7 @@ func TestSignInTimeout(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 30 * time.Millisecond, @@ -180,7 +180,7 @@ func TestSignInCancelledIsNotATimeout(t *testing.T) { // The caller cancels once the sign-in page is open, so the wait is what is cancelled. ctx, cancel := context.WithCancel(t.Context()) - err := SignIn(ctx, w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + err := SignIn(browserOpens(ctx), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: time.Minute, Open: func(context.Context, string) error { @@ -196,7 +196,7 @@ func TestSignInFailed(t *testing.T) { s := &loginTestServer{states: []string{"", "FAILED"}} w := loginTestClient(t, s) - err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 10 * time.Second, @@ -237,7 +237,7 @@ func TestSignInDeletesFailedCredentialFromEarlierAttempt(t *testing.T) { w := loginTestClient(t, s) deletedBeforeOpen := make(chan bool, 1) - err := SignIn(browserOpens(t), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -256,7 +256,7 @@ func TestSignInFailedCredentialThatFailsAgainIsFailed(t *testing.T) { s := &loginTestServer{states: []string{"FAILED"}, signedIn: true} w := loginTestClient(t, s) - err := SignIn(browserOpens(t), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 2 * time.Second, Open: noopOpen, @@ -270,7 +270,7 @@ func TestSignInFailedAfterProgressIsFailed(t *testing.T) { s := &loginTestServer{states: []string{"FAILED", "PROVISIONING", "FAILED"}} w := loginTestClient(t, s) - err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: noopOpen, @@ -303,7 +303,7 @@ func TestSignInTimeoutCutsOffPendingRequest(t *testing.T) { require.NoError(t, err) start := time.Now() - err = SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + err = SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 200 * time.Millisecond, Open: noopOpen, From fba500337b71e2b1c9c49355e2550a48176783ba Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:37:01 +0000 Subject: [PATCH 28/87] Poll for the sign-in while the browser command is still running SignIn ran the opener in the foreground, so a custom BROWSER command that stays open kept the wait from starting, and the command could not see a completed sign-in until the browser exited. The opener now runs in the background, and its failure is reported when it returns. Co-authored-by: Isaac --- libs/aitools/mcp/login.go | 14 ++++++++--- libs/aitools/mcp/login_test.go | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 5f2353c6919..a33bd5f82d4 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -74,10 +74,13 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str fmt.Fprintf(out, "Sign in with the same account you use for Databricks profile %s.\n", opts.Profile) fmt.Fprintf(out, "Open this URL to sign in to %s:\n%s\n", service, url) + // The page opens in the background. A browser command can run for as long as the browser is open, and the wait does + // not depend on it. Its failure is reported when it returns, if that is before the sign-in completes. + var opened <-chan error if !browser.IsDisabled(ctx) { - if err := opts.Open(ctx, url); err != nil { - fmt.Fprintf(out, "Could not open a browser (%v). Open the URL above.\n", err) - } + done := make(chan error, 1) + go func() { done <- opts.Open(ctx, url) }() + opened = done } fmt.Fprintln(out, "Waiting for sign-in...") @@ -95,6 +98,11 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str select { case <-waitCtx.Done(): return waitEnded(ctx) + case err := <-opened: + opened = nil + if err != nil { + fmt.Fprintf(out, "Could not open a browser (%v). Open the URL above.\n", err) + } case <-ticker.C: } diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index d1f37fdbb1c..1cf4c67b004 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "io" "net/http" "net/http/httptest" @@ -373,3 +374,48 @@ func TestEndpointNotFoundIsNotTheAnswerAboutTheCredential(t *testing.T) { require.Error(t, err) assert.False(t, deleted) } + +// TestSignInDoesNotWaitForTheOpener checks that a browser command still running does not hold up the wait for the +// sign-in. The command can stay open as long as the browser is. +func TestSignInDoesNotWaitForTheOpener(t *testing.T) { + s := &loginTestServer{states: []string{"", "ACTIVE"}} + w := loginTestClient(t, s) + + release := make(chan struct{}) + t.Cleanup(func() { close(release) }) + done := make(chan error, 1) + go func() { + done <- SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + Open: func(context.Context, string) error { + <-release + return nil + }, + }) + }() + select { + case err := <-done: + require.NoError(t, err) + case <-time.After(5 * time.Second): + t.Fatal("the sign-in waited for the browser command to return") + } +} + +// TestSignInReportsOpenerFailure checks that an opener that fails is reported while the wait goes on. The URL is printed +// before the page opens, so the user can still finish the sign-in by hand. +func TestSignInReportsOpenerFailure(t *testing.T) { + s := &loginTestServer{states: []string{"", "ACTIVE"}} + w := loginTestClient(t, s) + + var out bytes.Buffer + err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + Poll: 50 * time.Millisecond, + Timeout: 10 * time.Second, + Open: func(context.Context, string) error { + return errors.New("no browser here") + }, + }) + require.NoError(t, err) + assert.Contains(t, out.String(), "Could not open a browser (no browser here). Open the URL above.\n") +} From 4bab9d0587ad7ee9fbe19dd2453007d29a09d398 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:38:32 +0000 Subject: [PATCH 29/87] Send the output of the browser command to stderr during sign-in A custom BROWSER command inherited stdout, so its output landed before the JSON document of login and add --login. browser.OpenOutputToStderr is Open with the command's stdout sent to stderr, and sign-in uses it. Open itself is unchanged for the other callers. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 2 +- libs/browser/browser.go | 50 ++++++++++++++++++++++----- libs/browser/browser_test.go | 67 ++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 9 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index a85c2292efc..74eecf09398 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -210,7 +210,7 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient Profile: profileName, Poll: mcp.SignInPollInterval, Timeout: mcp.SignInWaitTimeout, - Open: browser.Open, + Open: browser.OpenOutputToStderr, }) switch { case errors.Is(err, mcp.ErrSignInTimeout): diff --git a/libs/browser/browser.go b/libs/browser/browser.go index 7c0763d6fe7..0a782d79549 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -11,6 +11,8 @@ import ( "context" "fmt" "io" + "os" + "sync" browserpkg "github.com/pkg/browser" @@ -30,15 +32,26 @@ import ( // Callers that want to override the "none" message should branch on IsDisabled // before calling Open. func Open(ctx context.Context, url string) error { + return openURL(ctx, url, false) +} + +// OpenOutputToStderr is Open for a caller whose stdout is a document, such as `--output json`. The output of the +// browser command goes to stderr, as its error output does. The other behaviors are Open's. +func OpenOutputToStderr(ctx context.Context, url string) error { + return openURL(ctx, url, true) +} + +// openURL implements Open and OpenOutputToStderr. stdoutToStderr sends the output of the browser to stderr. +func openURL(ctx context.Context, url string, stdoutToStderr bool) error { browserCmd := env.Get(ctx, "BROWSER") switch browserCmd { case "": - return openDefault(url) + return openDefault(url, stdoutToStderr) case "none": cmdio.LogString(ctx, "Open this URL in your browser:\n"+url) return nil default: - return openWithCommand(ctx, browserCmd, url) + return openWithCommand(ctx, browserCmd, url, stdoutToStderr) } } @@ -48,26 +61,47 @@ func IsDisabled(ctx context.Context) bool { return env.Get(ctx, "BROWSER") == "none" } -func openDefault(url string) error { - // github.com/pkg/browser writes xdg-open's error output to os.Stderr even - // when the open succeeds, producing spurious noise on Linux desktops. - originalStderr := browserpkg.Stderr +func openDefault(url string, stdoutToStderr bool) error { + // github.com/pkg/browser writes xdg-open's output to os.Stdout and its error output to os.Stderr, even when the + // open succeeds, producing spurious noise on Linux desktops. The error output is discarded. + originalStdout, originalStderr := browserpkg.Stdout, browserpkg.Stderr defer func() { + browserpkg.Stdout = originalStdout browserpkg.Stderr = originalStderr }() browserpkg.Stderr = io.Discard + if stdoutToStderr { + browserpkg.Stdout = os.Stderr + } return browserpkg.OpenURL(url) } -func openWithCommand(ctx context.Context, browserCmd, url string) error { +func openWithCommand(ctx context.Context, browserCmd, url string, stdoutToStderr bool) error { e, err := exec.NewCommandExecutor(".") if err != nil { return err } - e.WithInheritOutput() + if !stdoutToStderr { + e.WithInheritOutput() + } cmd, err := e.StartCommand(ctx, fmt.Sprintf("%q %q", browserCmd, url)) if err != nil { return err } + if !stdoutToStderr { + return cmd.Wait() + } + // The command's stdout is a pipe, and Wait closes it, so it is copied to stderr before Wait. + var copied sync.WaitGroup + copied.Add(2) + go func() { + defer copied.Done() + _, _ = io.Copy(os.Stderr, cmd.Stdout()) + }() + go func() { + defer copied.Done() + _, _ = io.Copy(os.Stderr, cmd.Stderr()) + }() + copied.Wait() return cmd.Wait() } diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index 5739440716e..d0ae4cca868 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -1,6 +1,10 @@ package browser import ( + "context" + "fmt" + "os" + "path/filepath" "testing" "github.com/databricks/cli/libs/cmdio" @@ -36,3 +40,66 @@ func TestOpenWithBrowserNonePrintsURL(t *testing.T) { assert.Contains(t, stderr.String(), "Open this URL in your browser:") assert.Contains(t, stderr.String(), "https://example.com/resource") } + +// openerChildEnv marks the child process that the tests below start as the browser command. +const openerChildEnv = "LIBS_BROWSER_TEST_OPENER" + +// TestMain lets the tests use this test binary as a browser command that writes to stdout, as a launcher can. +func TestMain(m *testing.M) { + if os.Getenv(openerChildEnv) != "" { + os.Args = []string{os.Args[0], "-test.run=^TestOpenerChild$"} + } + m.Run() +} + +// TestOpenerChild is the browser command. It runs only in the child process that TestMain starts. +func TestOpenerChild(t *testing.T) { + if os.Getenv(openerChildEnv) == "" { + t.Skip("runs only as the browser command of the tests below") + } + fmt.Print("opener stdout") +} + +// runOpener calls open with this test binary as the browser command, and returns what the command wrote to stdout and +// to stderr. The process streams are replaced for the call. +func runOpener(t *testing.T, open func(context.Context, string) error) (stdout, stderr string, err error) { + t.Helper() + t.Setenv(openerChildEnv, "1") + dir := t.TempDir() + stdoutFile, err := os.Create(filepath.Join(dir, "stdout")) + require.NoError(t, err) + defer stdoutFile.Close() + stderrFile, err := os.Create(filepath.Join(dir, "stderr")) + require.NoError(t, err) + defer stderrFile.Close() + + prevStdout, prevStderr := os.Stdout, os.Stderr + os.Stdout, os.Stderr = stdoutFile, stderrFile + ctx := env.Set(t.Context(), "BROWSER", os.Args[0]) + err = open(ctx, "https://example.com/login") + os.Stdout, os.Stderr = prevStdout, prevStderr + + gotStdout, readErr := os.ReadFile(stdoutFile.Name()) + require.NoError(t, readErr) + gotStderr, readErr := os.ReadFile(stderrFile.Name()) + require.NoError(t, readErr) + return string(gotStdout), string(gotStderr), err +} + +// TestOpenOutputToStderrSendsCommandStdoutToStderr checks that the output of a custom browser command goes to stderr, so +// that it does not land before a JSON document on stdout. +func TestOpenOutputToStderrSendsCommandStdoutToStderr(t *testing.T) { + stdout, stderr, err := runOpener(t, OpenOutputToStderr) + require.NoError(t, err) + assert.Empty(t, stdout) + assert.Contains(t, stderr, "opener stdout") +} + +// TestOpenKeepsCommandStdoutOnStdout checks that Open, which other commands use, still sends the command's stdout to +// stdout. +func TestOpenKeepsCommandStdoutOnStdout(t *testing.T) { + stdout, stderr, err := runOpener(t, Open) + require.NoError(t, err) + assert.Contains(t, stdout, "opener stdout") + assert.NotContains(t, stderr, "opener stdout") +} From 829fc7c12e25e695da34c9f96bef8e9670de6e88 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:39:46 +0000 Subject: [PATCH 30/87] Share the DATABRICKS_CONFIG_FILE prefix of hint commands RecreateCommand builds the prefix inline. ConfigFileCommand is the one place that builds it, for the auth and login hints that follow. Behavior is unchanged. Co-authored-by: Isaac --- libs/aitools/mcp/binding.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 6e4cbc1ca29..89dd319a8af 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -188,11 +188,16 @@ func AddServiceCommand(service, profileName, agentName string) string { // 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 ConfigFileCommand(configFile, authLoginCommand+" --host "+shellquote.BashArg(host)+" --profile "+shellquote.BashArg(profileName)) +} + +// ConfigFileCommand returns command with DATABRICKS_CONFIG_FILE set to configFile, when configFile is not "". A command +// that a hint prints names the file of a profile that is not the one the shell reads. +func ConfigFileCommand(configFile, command string) string { + if configFile == "" { + return command } - return command + return "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(configFile) + " " + command } // profileGoneError builds the PROFILE_NOT_FOUND error for a profile that the config file does not From 0cfd32259889713dc035c427cec66932c7246413 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:41:45 +0000 Subject: [PATCH 31/87] Name the config file in auth and login hints for a pinned server A server pinned to a config file other than the shell's got a bare `databricks auth login --profile` or `databricks aitools mcp login` hint, which signs in or looks up the profile in the wrong file. The hints for a pinned server now set DATABRICKS_CONFIG_FILE, as the recreate hint does. The change covers the auth and login hints of login, logout, add --login and list --check; the serve hint is not changed. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 24 +++++++-------- cmd/aitools/mcp_add_test.go | 2 +- cmd/aitools/mcp_list.go | 12 ++++---- cmd/aitools/mcp_list_test.go | 22 +++++++++++++- cmd/aitools/mcp_login.go | 50 ++++++++++++++++++-------------- cmd/aitools/mcp_login_test.go | 11 +++++-- cmd/aitools/mcp_serve.go | 2 +- cmd/aitools/mcp_serve_test.go | 2 +- libs/aitools/mcp/binding.go | 16 +++++----- libs/aitools/mcp/binding_test.go | 7 +++++ 10 files changed, 96 insertions(+), 52 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 464e4f2fcbc..d09625c0ff1 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -276,19 +276,19 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode return err } - w, err := profileClient(ctx, profileName, configFile) + w, err := profileClient(ctx, profileName, configFile, pinned) if err != nil { return err } out.Host = w.Config.Host workspaceID, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return describeRequestError(profileName, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) + return describeRequestError(profileName, pinned, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } services, err := mcp.Discover(ctx, w, location) if err != nil { - return describeRequestError(profileName, err) + return describeRequestError(profileName, pinned, err) } picked, err := pickServices(location, fullNames, services) if err != nil { @@ -425,7 +425,7 @@ func withExistingProfiles(ctx context.Context, state *mcp.State, profileName str // profileClient returns a workspace client bound to exactly the named profile in configFile. Its config is the one // Bind builds, so the environment's credentials are never sent to the profile's host. -func profileClient(ctx context.Context, name, configFile string) (*databricks.WorkspaceClient, error) { +func profileClient(ctx context.Context, name, configFile, pinnedFile string) (*databricks.WorkspaceClient, error) { cfg, err := mcp.BoundConfig(name, configFile) if err != nil { return nil, err @@ -440,7 +440,7 @@ func profileClient(ctx context.Context, name, configFile string) (*databricks.Wo if err != nil { return nil, err } - if err := mcp.AuthenticateBound(ctx, cfg, name); err != nil { + if err := mcp.AuthenticateBound(ctx, cfg, name, pinnedFile); err != nil { return nil, err } return w, nil @@ -449,9 +449,9 @@ func profileClient(ctx context.Context, name, configFile string) (*databricks.Wo // describeRequestError classifies a failed request to the profile's workspace. A credential the workspace // rejects is AUTH_FAILED, with the login command for the profile. Other failures are described by // describeListError. -func describeRequestError(profileName string, err error) error { +func describeRequestError(profileName, pinnedFile string, err error) error { if errors.Is(err, apierr.ErrUnauthenticated) { - return mcp.AuthError(profileName, err) + return mcp.AuthError(profileName, pinnedFile, err) } return describeListError(profileName, err) } @@ -1034,8 +1034,8 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp continue } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) - if _, err := signIn(ctx, errOut, w, req.profile, url, plans[i].service); err != nil { - fmt.Fprintln(errOut, signInHint(server.Service, req.profile)) + if _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service); err != nil { + fmt.Fprintln(errOut, signInHint(server.Service, req.profile, req.pinned)) errs = append(errs, err) } } @@ -1043,8 +1043,8 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp } // signInHint is the command that signs the profile in to a sign-in-required service. -func signInHint(service, profileName string) string { - return fmt.Sprintf("%s needs sign-in. Run: %s", service, mcpLoginHint(service, profileName)) +func signInHint(service, profileName, pinnedFile string) string { + return fmt.Sprintf("%s needs sign-in. Run: %s", service, mcpLoginHint(service, profileName, pinnedFile)) } // installedAnywhere reports whether the server reached at least one agent. @@ -1183,7 +1183,7 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut } } if server.SignInRequired && installedAnywhere(server) && !req.login { - cmdio.LogString(ctx, " "+signInHint(server.Service, req.profile)) + cmdio.LogString(ctx, " "+signInHint(server.Service, req.profile, req.pinned)) } if server.note != "" && installedAnywhere(server) { cmdio.LogString(ctx, " "+server.note) diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index e1895c160e4..e312736a753 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -879,7 +879,7 @@ func TestErrorCategoryMapsTypedFailures(t *testing.T) { errorCategory(fmt.Errorf("claude-code: server %q: %w", "github", agentconfig.ErrNotOwned))) assert.Equal(t, protos.AitoolsErrorCategoryTimeout, errorCategory(fmt.Errorf("list: %w", context.DeadlineExceeded))) assert.Equal(t, protos.AitoolsErrorCategoryPermissionDenied, errorCategory(&fs.PathError{Op: "open", Path: "x", Err: fs.ErrPermission})) - assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(mcp.AuthError("logfood", errors.New("no token")))) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindAuthFailed), errorCategory(mcp.AuthError("logfood", "", errors.New("no token")))) } // TestMcpAddRefusesMixedCaseNamesBeforeAnyRequest checks that mixed-case service and location names bind to the same entry as their lowercase forms, so a profile diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 9e572eb37ed..d1af5f9a63e 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -427,11 +427,11 @@ func addChecks(ctx context.Context, out *mcpListOutput) { // addProfileChecks runs the checks of one profile, whose config file is in ctx. A profile whose token cannot be minted // has no sign-in checks: its warning says why. func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput) { - w, err := profileClient(ctx, p.Profile, p.configPath) + w, err := profileClient(ctx, p.Profile, p.configPath, p.ConfigFile) if err == nil { // A personal access token is sent only with a request, so the token is checked with one. if _, err = w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { - err = describeRequestError(p.Profile, err) + err = describeRequestError(p.Profile, p.ConfigFile, err) } } p.token = statusOK @@ -449,7 +449,7 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput } continue } - status, signInErr := signInStatus(ctx, w, p.Profile, s.Service) + status, signInErr := signInStatus(ctx, w, p.Profile, p.ConfigFile, s.Service) s.Check.SignIn = status if signInErr != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", s.Name, signInErr)) @@ -460,8 +460,8 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput // signInStatus reads the caller's sign-in to a service, as the service requires it now. Only an ACTIVE credential // counts as signed in. A service that does not require sign-in is "", which is not a status. A service the profile can // no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error says why. -func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, service string) (string, error) { - svc, err := serviceFor(ctx, w, profileName, service) +func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string) (string, error) { + svc, err := serviceFor(ctx, w, profileName, pinnedFile, service) if err != nil { return statusUnknown, err } @@ -470,7 +470,7 @@ func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileNam } state, err := mcp.CredentialState(ctx, w, service) if err != nil { - return statusUnknown, describeRequestError(profileName, err) + return statusUnknown, describeRequestError(profileName, pinnedFile, err) } if state == catalog.ProvisioningInfoStateActive { return statusOK, nil diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 024e036a17c..5bfdcded8d3 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1148,7 +1148,7 @@ func TestSignInStatus(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - status, err := signInStatus(t.Context(), w, "logfood", "system.ai.github") + status, err := signInStatus(t.Context(), w, "logfood", "", "system.ai.github") assert.Equal(t, tc.want, status) if tc.warns { require.Error(t, err) @@ -1227,3 +1227,23 @@ func TestAddChecksRejectedTokenFailsTheTokenCheck(t *testing.T) { assert.True(t, strings.HasPrefix(out.Warnings[0], "profile logfood: "), out.Warnings[0]) assert.Contains(t, out.Warnings[0], "Run: databricks auth login --profile logfood") } + +// TestAddChecksAuthWarningNamesPinnedConfigFile checks that the login command in the warning for a rejected token sets +// DATABRICKS_CONFIG_FILE for a group pinned to a config file, so that the login reads the file the profile is in. +func TestAddChecksAuthWarningNamesPinnedConfigFile(t *testing.T) { + withHome(t) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`)) + })) + t.Cleanup(server.Close) + configFile := filepath.Join(t.TempDir(), "logfood.cfg") + require.NoError(t, os.WriteFile(configFile, []byte("[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-revoked\n"), 0o600)) + t.Setenv("DATABRICKS_CONFIG_FILE", configFile) + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, configFile)) + + _, out := checkedListServer(t) + require.Len(t, out.Warnings, 1) + assert.Contains(t, out.Warnings[0], "Run: DATABRICKS_CONFIG_FILE="+configFile+" databricks auth login --profile logfood") +} diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 74eecf09398..638c5d103c0 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -45,8 +45,9 @@ type mcpSignInOutput struct { Error string `json:"error,omitempty"` } -// mcpSignInAction runs login or logout against the profile's client and records the result in out. -type mcpSignInAction func(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error +// mcpSignInAction runs login or logout against the profile's client and records the result in out. pinnedFile is the +// config file the server is pinned to, or "" (see mcpLoginHint). +type mcpSignInAction func(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput, pinnedFile string) error // NewMcpLoginCmd returns `aitools mcp login`. func NewMcpLoginCmd() *cobra.Command { @@ -130,32 +131,38 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri return err } out.Profile = profileName - if _, err := mcp.CheckBinding(ctx, state, service, profileName, configFile); err != nil { + entry, err := mcp.CheckBinding(ctx, state, service, profileName, configFile) + if err != nil { return err } - w, err := profileClient(ctx, profileName, configFile) + // A server pinned to a config file names that file in the hints, as add does. + pinnedFile := "" + if entry != nil && entry.ConfigFile != "" { + pinnedFile = configFile + } + w, err := profileClient(ctx, profileName, configFile, pinnedFile) if err != nil { return err } out.Host = w.Config.Host - return action(ctx, errOut, w, out) + return action(ctx, errOut, w, out, pinnedFile) } // serviceFor returns the service with its sign-in requirement, as the profile sees it. A service the profile cannot see // is an error, so that a misspelled name is reported rather than treated as a service with no sign-in. -func serviceFor(ctx context.Context, w *databricks.WorkspaceClient, profileName, service string) (mcp.Service, error) { +func serviceFor(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string) (mcp.Service, error) { svc, err := mcp.GetService(ctx, w, service) if mcp.IsNotFound(err) { return mcp.Service{}, failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", service, profileName) } if err != nil { - return mcp.Service{}, describeRequestError(profileName, err) + return mcp.Service{}, describeRequestError(profileName, pinnedFile, err) } return svc, nil } -func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { - svc, err := serviceFor(ctx, w, out.Profile, out.Service) +func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput, pinnedFile string) error { + svc, err := serviceFor(ctx, w, out.Profile, pinnedFile, out.Service) if err != nil { return err } @@ -169,21 +176,21 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie // refused a profile whose workspace changed since the server was installed. workspaceID, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return describeRequestError(out.Profile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) + return describeRequestError(out.Profile, pinnedFile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } out.URL = mcp.SignInURL(out.Host, svc.FullName, workspaceID) - state, err := signIn(ctx, errOut, w, out.Profile, out.URL, svc) + state, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, out.URL, svc) out.State = state return err } -func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput) error { - if _, err := serviceFor(ctx, w, out.Profile, out.Service); err != nil { +func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput, pinnedFile string) error { + if _, err := serviceFor(ctx, w, out.Profile, pinnedFile, out.Service); err != nil { return err } deleted, err := mcp.SignOut(ctx, w, out.Service) if err != nil { - return describeRequestError(out.Profile, err) + return describeRequestError(out.Profile, pinnedFile, err) } if !deleted { out.State = signOutStateNotSignedIn @@ -198,14 +205,15 @@ func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceCli // mcpLoginCommand is the command that signs a profile in to a service. The hints name it. const mcpLoginCommand = "databricks aitools mcp login" -// mcpLoginHint returns the command that signs profileName in to service. -func mcpLoginHint(service, profileName string) string { - return mcpLoginCommand + " " + shellquote.BashArg(service) + " --profile " + shellquote.BashArg(profileName) +// mcpLoginHint returns the command that signs profileName in to service. pinnedFile is the config file the server is +// pinned to, which the command sets, or "" for the file the shell reads. +func mcpLoginHint(service, profileName, pinnedFile string) string { + return mcp.ConfigFileCommand(pinnedFile, mcpLoginCommand+" "+shellquote.BashArg(service)+" --profile "+shellquote.BashArg(profileName)) } // signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns // the sign-in state name and the failure, which is categorized for JSON. `add --login` shares it. -func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, url string, svc mcp.Service) (string, error) { +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service) (string, error) { err := mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ Profile: profileName, Poll: mcp.SignInPollInterval, @@ -216,13 +224,13 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient case errors.Is(err, mcp.ErrSignInTimeout): return signInStateTimeout, failure(categorySignInTimeout, err, "sign-in to %s did not complete in %g minutes. Finish at %s then retry: %s", - svc.FullName, mcp.SignInWaitTimeout.Minutes(), url, mcpLoginHint(svc.FullName, profileName)) + svc.FullName, mcp.SignInWaitTimeout.Minutes(), url, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case errors.Is(err, mcp.ErrSignInFailed): return signInStateFailed, failure(protos.AitoolsErrorCategoryUncategorized, err, "sign-in to %s failed on profile %s. Run: %s", - svc.FullName, profileName, mcpLoginHint(svc.FullName, profileName)) + svc.FullName, profileName, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case err != nil: - return "", describeRequestError(profileName, err) + return "", describeRequestError(profileName, pinnedFile, err) } fmt.Fprintf(errOut, "Signed in to %s on profile %s.\n", svc.FullName, profileName) diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index ea2a0fd868d..12442a96718 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -178,8 +178,15 @@ func TestSignInInstalledSignsInToEveryServer(t *testing.T) { // TestMcpLoginHintQuotesItsArguments checks that the login command a hint prints can be pasted into a shell as written. func TestMcpLoginHintQuotesItsArguments(t *testing.T) { - assert.Equal(t, "databricks aitools mcp login system.ai.github --profile logfood", mcpLoginHint("system.ai.github", "logfood")) - assert.Equal(t, "databricks aitools mcp login system.ai.github --profile 'my profile'", mcpLoginHint("system.ai.github", "my profile")) + assert.Equal(t, "databricks aitools mcp login system.ai.github --profile logfood", mcpLoginHint("system.ai.github", "logfood", "")) + assert.Equal(t, "databricks aitools mcp login system.ai.github --profile 'my profile'", mcpLoginHint("system.ai.github", "my profile", "")) +} + +// TestMcpLoginHintNamesPinnedConfigFile checks that the hint for a server pinned to a config file sets DATABRICKS_CONFIG_FILE, +// so that the login reads the file the profile is in. +func TestMcpLoginHintNamesPinnedConfigFile(t *testing.T) { + assert.Equal(t, "DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks aitools mcp login system.ai.github --profile logfood", + mcpLoginHint("system.ai.github", "logfood", "/work/cfg/logfood.cfg")) } // TestMcpLoginJSONNamesProfileWhenBindingFails checks that a failure after the profile is resolved still names the profile diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 66195d34d9c..907ba00ae8f 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -362,7 +362,7 @@ func (g *refreshGuard) authenticateRequest(r *http.Request) error { } // The hint says to restart the server: a new spawn re-runs the binding checks, and it is the one that picks up the // new login. - authErr := fmt.Errorf("%w, then restart the MCP server in your agent", mcp.AuthError(g.profile, err)) + authErr := fmt.Errorf("%w, then restart the MCP server in your agent", mcp.AuthError(g.profile, "", err)) g.mu.Lock() first := g.err == nil if first { diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 3541727eed0..fcf97ec01e1 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -199,7 +199,7 @@ func TestServeFailurePrintsOnceWithStatus(t *testing.T) { cmd := NewMcpServeCmd() var stderr bytes.Buffer cmd.SetErr(&stderr) - cause := mcp.AuthError("logfood", errors.New(refreshTokenExpired)) + cause := mcp.AuthError("logfood", "", errors.New(refreshTokenExpired)) err := serveFailure(cmd, cause, serveBindStatus) diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 89dd319a8af..b839b537bca 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -87,7 +87,7 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) if entry == nil { fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s\n", opts.Service, opts.Profile, cfg.Host) } - if err := AuthenticateBound(ctx, cfg, opts.Profile); err != nil { + if err := AuthenticateBound(ctx, cfg, opts.Profile, ""); err != nil { return nil, nil, err } return cfg, entry, nil @@ -111,8 +111,9 @@ func BoundConfig(profileName, configFile string) (*config.Config, error) { } // AuthenticateBound checks that cfg, from BoundConfig for profileName, has a credential. A credential the profile cannot -// provide is AUTH_FAILED, with the login command for the profile. -func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName string) error { +// provide is AUTH_FAILED, with the login command for the profile. pinnedFile is the config file the server is pinned to, +// or "" (see AuthError). +func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName, pinnedFile string) error { // Authenticate only writes headers onto req, which is never sent. The request takes // the command's context, as in cmd/root/auth.go. req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil) @@ -120,7 +121,7 @@ func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName stri return uncategorizedError(err) } if err := cfg.Authenticate(req); err != nil { - return AuthError(profileName, err) + return AuthError(profileName, pinnedFile, err) } return nil } @@ -248,9 +249,10 @@ func configFileError(name, profileName, recordedPath, currentPath string) error } // AuthError is the AUTH_FAILED error for a credential that the profile's own settings could not -// provide. The message points at the login command for the profile. -func AuthError(profileName string, cause error) error { - message := fmt.Sprintf("%s\nRun: %s --profile %s", cause, authLoginCommand, shellquote.BashArg(profileName)) +// provide. The message points at the login command for the profile. pinnedFile is the config file +// the server is pinned to, which the command names when it is not "". +func AuthError(profileName, pinnedFile string, cause error) error { + message := fmt.Sprintf("%s\nRun: %s --profile %s", cause, ConfigFileCommand(pinnedFile, authLoginCommand), shellquote.BashArg(profileName)) return &BindError{Category: BindAuthFailed, message: message, cause: cause} } diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 46e8d0734fb..a2170756687 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -877,3 +877,10 @@ func TestCheckBindingChecksProfileWithoutEntry(t *testing.T) { }) } } + +func TestAuthErrorNamesPinnedConfigFile(t *testing.T) { + cause := errors.New("Invalid access token.") + assert.EqualError(t, mcp.AuthError("logfood", "/work/cfg/logfood.cfg", cause), + "Invalid access token.\nRun: DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks auth login --profile logfood") + assert.EqualError(t, mcp.AuthError("logfood", "", cause), "Invalid access token.\nRun: databricks auth login --profile logfood") +} From a8f38e1877128f9b2897d11adfd2c1eb2eacec5d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:42:59 +0000 Subject: [PATCH 32/87] Print the add --login sign-in hint once and with the right command A failed sign-in printed the login hint on stderr, although its error already named the command to run, and an auth failure got the login command instead of the auth command. The hint now goes through the same output as the other add hints, and only for a failure whose error does not name a command. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 13 ++++++-- cmd/aitools/mcp_login_test.go | 60 +++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index d09625c0ff1..8aec8f2cfd6 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -1026,7 +1026,8 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { } // signInInstalled signs in to each sign-in-required server that reached an agent, in order. A failure does not stop the -// others; each failed server gets the login hint, and the failures are joined. The servers are installed either way. +// others, and the failures are joined. The servers are installed either way. A failed server gets the login hint, on the +// same stream as the hints of the results, unless its error already names the command to run. func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, req addRequest, plans []addPlan, servers []addServerOutput) error { var errs []error for i, server := range servers { @@ -1035,13 +1036,21 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) if _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service); err != nil { - fmt.Fprintln(errOut, signInHint(server.Service, req.profile, req.pinned)) + if !namesSignInCommand(err) { + cmdio.LogString(ctx, signInHint(server.Service, req.profile, req.pinned)) + } errs = append(errs, err) } } return errors.Join(errs...) } +// namesSignInCommand reports whether a sign-in failure already names the command to run next: a sign-in that timed out +// or failed names the login command, and a credential the workspace rejected names the auth command. +func namesSignInCommand(err error) bool { + return errors.Is(err, mcp.ErrSignInTimeout) || errors.Is(err, mcp.ErrSignInFailed) || errors.Is(err, apierr.ErrUnauthenticated) +} + // signInHint is the command that signs the profile in to a sign-in-required service. func signInHint(service, profileName, pinnedFile string) string { return fmt.Sprintf("%s needs sign-in. Run: %s", service, mcpLoginHint(service, profileName, pinnedFile)) diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 12442a96718..7b9930f552a 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -168,12 +168,66 @@ func TestSignInInstalledSignsInToEveryServer(t *testing.T) { {Name: "genie-logfood", Service: "system.ai.genie_one_mcp", SignInRequired: true, Agents: installed}, } + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) var errOut bytes.Buffer - err = signInInstalled(t.Context(), &errOut, w, req, plans, servers) + err = signInInstalled(ctx, &errOut, w, req, plans, servers) require.Error(t, err) assert.Equal(t, []string{githubPath, geniePath}, reads) - assert.Contains(t, errOut.String(), "system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood\n") - assert.NotContains(t, errOut.String(), "system.ai.genie_one_mcp needs sign-in") + assert.Contains(t, stderr.String(), "system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood\n") + assert.NotContains(t, stderr.String(), "system.ai.genie_one_mcp needs sign-in") +} + +// signInInstalledOne signs in to one installed sign-in server of the test's workspace and returns the error and what the +// command wrote to stderr, on either stream. The server answers with status and body for the credential. +func signInInstalledOne(t *testing.T, status int, body string) (error, string) { + t.Helper() + const githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + rw.WriteHeader(http.StatusNotFound) + return + } + if r.URL.Path == githubPath { + rw.Header().Set("Content-Type", "application/json") + rw.WriteHeader(status) + _, _ = rw.Write([]byte(body)) + return + } + rw.WriteHeader(http.StatusNotFound) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + req := addRequest{profile: "logfood", workspaceID: "123", agents: []*agents.Agent{agents.ByName("claude-code")}} + plans := []addPlan{{service: mcp.Service{FullName: "system.ai.github", SignInRequired: true}}} + servers := []addServerOutput{{ + Name: "github-logfood", Service: "system.ai.github", SignInRequired: true, + Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled}}, + }} + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + var errOut bytes.Buffer + err = signInInstalled(ctx, &errOut, w, req, plans, servers) + return err, errOut.String() + stderr.String() +} + +// TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand checks that a sign-in that failed does not print the +// login hint again, since its error already names the command to run. +func TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand(t *testing.T) { + err, output := signInInstalledOne(t, http.StatusOK, `{"provisioning_info":{"state":"FAILED"}}`) + require.ErrorIs(t, err, mcp.ErrSignInFailed) + assert.Contains(t, err.Error(), "Run: databricks aitools mcp login system.ai.github --profile logfood") + assert.NotContains(t, output, "needs sign-in") +} + +// TestSignInInstalledAuthFailureNamesTheAuthCommand checks that a credential the workspace rejects gets the auth command +// in its error, and no login hint, which would not fix it. +func TestSignInInstalledAuthFailureNamesTheAuthCommand(t *testing.T) { + err, output := signInInstalledOne(t, http.StatusUnauthorized, `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`) + require.Error(t, err) + assert.Contains(t, err.Error(), "Run: databricks auth login --profile logfood") + assert.NotContains(t, output, "mcp login") + assert.NotContains(t, output, "needs sign-in") } // TestMcpLoginHintQuotesItsArguments checks that the login command a hint prints can be pasted into a shell as written. From 230ca0d132c2a64ba1ad3d6d7087c7602efbc411 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:44:15 +0000 Subject: [PATCH 33/87] Restore the config file environment in a defer in list --check The environment was restored after fn returned, so a panic left the next profile in another profile's config file. The restore is deferred. The variable stays in the process environment, because the SDK configures the credentials with its own context, which does not carry the one the caller sets. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 19 ++++++++++++------- cmd/aitools/mcp_list_test.go | 12 ++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index d1af5f9a63e..5d15b2b961b 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -479,18 +479,23 @@ func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileNam } // withConfigFile runs fn with path as the config file of a profile: in the context that fn receives, and in the process -// environment, which the token store reads (see mcp.Bind). The environment is restored afterwards, and its error is -// returned. -func withConfigFile(ctx context.Context, path string, fn func(ctx context.Context)) error { +// environment. The token store reads the variable from the process environment, because the SDK configures the +// credentials with a context of its own (see mcp.Bind). The environment is restored when fn returns, or panics, and a +// failure to restore it is returned. +func withConfigFile(ctx context.Context, path string, fn func(ctx context.Context)) (err error) { previous, wasSet := os.LookupEnv(mcpconfig.ConfigFileEnv) if err := os.Setenv(mcpconfig.ConfigFileEnv, path); err != nil { return err } + defer func() { + if !wasSet { + err = errors.Join(err, os.Unsetenv(mcpconfig.ConfigFileEnv)) + return + } + err = errors.Join(err, os.Setenv(mcpconfig.ConfigFileEnv, previous)) + }() fn(env.Set(ctx, mcpconfig.ConfigFileEnv, path)) - if !wasSet { - return os.Unsetenv(mcpconfig.ConfigFileEnv) - } - return os.Setenv(mcpconfig.ConfigFileEnv, previous) + return nil } // binaryStatus reports whether the recorded command is a binary. It resolves the command as binaryPathFor does: an diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 5bfdcded8d3..8ac4c4a9998 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1078,6 +1078,18 @@ func TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt(t *testing.T) { assert.False(t, set, "a variable that was unset is unset again") } +// TestWithConfigFileRestoresEnvironmentWhenFnPanics checks that the environment is restored even when the call panics, so +// that a panic does not leave the next profile in another profile's config file. +func TestWithConfigFileRestoresEnvironmentWhenFnPanics(t *testing.T) { + t.Setenv(mcpconfig.ConfigFileEnv, "ambient.cfg") + assert.Panics(t, func() { + _ = withConfigFile(t.Context(), "pinned.cfg", func(context.Context) { + panic("boom") + }) + }) + assert.Equal(t, "ambient.cfg", os.Getenv(mcpconfig.ConfigFileEnv)) +} + // TestCheckText checks the words that --check adds to a server's line. func TestCheckText(t *testing.T) { cases := []struct { From 3dd73048ca37212a1b80035e109c6098aa5e0643 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:45:27 +0000 Subject: [PATCH 34/87] Reuse the failed status and group the timeout category with the others list --check had its own "failed" result, which is the status of a failed add. The timeout category is now declared with the categories that the telemetry enum does not define yet. No behavior changes. Co-authored-by: Isaac --- cmd/aitools/mcp.go | 1 + cmd/aitools/mcp_list.go | 16 +++++++--------- cmd/aitools/mcp_list_test.go | 16 ++++++++-------- cmd/aitools/mcp_login.go | 4 ---- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index aa2012f379c..d64e4792d7c 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -27,6 +27,7 @@ const ( categoryProfileRequired protos.AitoolsErrorCategory = "PROFILE_REQUIRED" categoryPermissionDenied protos.AitoolsErrorCategory = "MCP_PERMISSION_DENIED" categoryLocationNotFound protos.AitoolsErrorCategory = "MCP_LOCATION_NOT_FOUND" + categorySignInTimeout protos.AitoolsErrorCategory = "SIGN_IN_TIMEOUT" ) // NewMcpCmd returns the `aitools mcp` group. It stays hidden until the whole MCP diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 5d15b2b961b..f9e25551106 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -26,11 +26,9 @@ import ( "github.com/spf13/cobra" ) -// Results of the `mcp list --check` checks that are not server statuses. statusOK and statusUnknown carry the other two. -const ( - checkFailed = "failed" - checkNeeded = "needed" -) +// checkNeeded is the sign-in result of a server whose sign-in is not complete. The other results of a check are +// statusOK, statusFailed and statusUnknown. +const checkNeeded = "needed" // 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. @@ -436,7 +434,7 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput } p.token = statusOK if err != nil { - p.token = checkFailed + p.token = statusFailed out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) } for j := range p.Servers { @@ -504,11 +502,11 @@ func withConfigFile(ctx context.Context, path string, fn func(ctx context.Contex func binaryStatus(command string, lookPath func(string) (string, error)) string { path, absolute := binaryPathFor(command, lookPath) if !absolute { - return checkFailed + return statusFailed } info, err := os.Stat(path) if err != nil || info.IsDir() { - return checkFailed + return statusFailed } return statusOK } @@ -630,7 +628,7 @@ func checkText(c *mcpListCheck) string { case statusUnknown: parts = append(parts, "sign-in unknown") } - if c.Binary == checkFailed { + if c.Binary == statusFailed { parts = append(parts, "binary missing") } return strings.Join(parts, ", ") diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 8ac4c4a9998..5298a6440ac 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1021,7 +1021,7 @@ func TestAddChecksWarnsWhenProfileCannotAuthenticate(t *testing.T) { s, out := checkedListServer(t) require.NotNil(t, s.Check) - assert.Equal(t, checkFailed, s.Check.Token) + assert.Equal(t, statusFailed, s.Check.Token) assert.Equal(t, statusUnknown, s.Check.SignIn) require.Len(t, out.Warnings, 1) assert.True(t, strings.HasPrefix(out.Warnings[0], "profile logfood: "), out.Warnings[0]) @@ -1101,8 +1101,8 @@ func TestCheckText(t *testing.T) { {name: "signed in and binary ok", check: &mcpListCheck{SignIn: statusOK, Binary: statusOK}, want: "signed in"}, {name: "sign-in needed", check: &mcpListCheck{SignIn: checkNeeded, Binary: statusOK}, want: "sign-in needed"}, {name: "sign-in unknown", check: &mcpListCheck{SignIn: statusUnknown, Binary: statusOK}, want: "sign-in unknown"}, - {name: "binary missing", check: &mcpListCheck{Binary: checkFailed}, want: "binary missing"}, - {name: "sign-in needed and binary missing", check: &mcpListCheck{SignIn: checkNeeded, Binary: checkFailed}, want: "sign-in needed, binary missing"}, + {name: "binary missing", check: &mcpListCheck{Binary: statusFailed}, want: "binary missing"}, + {name: "sign-in needed and binary missing", check: &mcpListCheck{SignIn: checkNeeded, Binary: statusFailed}, want: "sign-in needed, binary missing"}, {name: "server without sign-in", check: &mcpListCheck{Binary: statusOK}, want: ""}, } for _, tc := range cases { @@ -1126,12 +1126,12 @@ func TestBinaryStatus(t *testing.T) { notFound := func(string) (string, error) { return "", exec.ErrNotFound } assert.Equal(t, statusOK, binaryStatus(present, notFound)) - assert.Equal(t, checkFailed, binaryStatus(missing, found)) + assert.Equal(t, statusFailed, binaryStatus(missing, found)) assert.Equal(t, statusOK, binaryStatus("databricks", found)) - assert.Equal(t, checkFailed, binaryStatus("databricks", notFound)) + assert.Equal(t, statusFailed, binaryStatus("databricks", notFound)) // An empty command resolves to the working directory, and a directory is not a binary. - assert.Equal(t, checkFailed, binaryStatus("", found)) - assert.Equal(t, checkFailed, binaryStatus(t.TempDir(), notFound)) + assert.Equal(t, statusFailed, binaryStatus("", found)) + assert.Equal(t, statusFailed, binaryStatus(t.TempDir(), notFound)) } // TestSignInStatus checks the sign-in state that --check reports for one service. @@ -1233,7 +1233,7 @@ func TestAddChecksRejectedTokenFailsTheTokenCheck(t *testing.T) { s, out := checkedListServer(t) require.NotNil(t, s.Check) - assert.Equal(t, checkFailed, s.Check.Token) + assert.Equal(t, statusFailed, s.Check.Token) assert.Equal(t, statusUnknown, s.Check.SignIn) require.Len(t, out.Warnings, 1) assert.True(t, strings.HasPrefix(out.Warnings[0], "profile logfood: "), out.Warnings[0]) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 638c5d103c0..10fd2e0598b 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -19,10 +19,6 @@ import ( "github.com/spf13/cobra" ) -// categorySignInTimeout is the error category of a sign-in that did not finish in time. The -// telemetry enum does not define it yet. -const categorySignInTimeout protos.AitoolsErrorCategory = "SIGN_IN_TIMEOUT" - // Sign-in states of `mcp login` and `mcp logout`, reported in JSON. const ( signInStateActive = "ACTIVE" From c730b4c33adf18fff18c4eb8111affba51d82d8b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:45:28 +0000 Subject: [PATCH 35/87] Fix the top-level failure docs, the --login help and the sign-in fixtures topLevelFailure's doc did not describe joined non-agent errors, and its test was in the add tests. The test moves to the install tests. The --login help no longer says the sign-in follows each install, since all installs run first. SignInOptions documents which fields are required. The fake's credentials are one caller's, and the login acceptance test ignores the files its script removes. Co-authored-by: Isaac --- .../cmd/aitools/mcp/login-flips-active/test.toml | 3 +++ cmd/aitools/install.go | 6 +++--- cmd/aitools/install_test.go | 12 ++++++++++++ cmd/aitools/mcp_add.go | 2 +- cmd/aitools/mcp_add_test.go | 12 ------------ libs/aitools/mcp/login.go | 8 ++++---- libs/testserver/fake_workspace.go | 3 ++- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/test.toml b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml index 59082bdcb7e..15c152cdbc1 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/test.toml +++ b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml @@ -2,3 +2,6 @@ # retry default of 2.5 seconds to show. The retries wait up to 30 seconds. Env.RETRY_MAX_ATTEMPTS = "60" Env.RETRY_INTERVAL_MS = "500" + +# The background login and add write these files, and the script removes them. +Ignore = ["login.txt", "add.txt"] diff --git a/cmd/aitools/install.go b/cmd/aitools/install.go index 424b2b8a8e0..2e792a82b9c 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -758,9 +758,9 @@ func forgetStalePluginRecords(ctx context.Context, scope string, items []agentPl } } -// topLevelFailure returns the run error when it has no per-agent entry, or nil -// when the failure is already reported per agent — so a per-agent failure is not -// duplicated in the top-level error/errorCategory fields. +// topLevelFailure returns the errors of runErr that have no per-agent entry, joined, or nil when every +// error is a per-agent failure. A per-agent failure is reported in its agent entry and is not repeated in the +// top-level error and errorCategory fields. func topLevelFailure(runErr error) error { if _, ok := errors.AsType[*agentErrors](runErr); !ok { return runErr diff --git a/cmd/aitools/install_test.go b/cmd/aitools/install_test.go index f98e0697e79..78a55ed3904 100644 --- a/cmd/aitools/install_test.go +++ b/cmd/aitools/install_test.go @@ -1822,3 +1822,15 @@ func TestNoAgentsMessageMatchesTheDetection(t *testing.T) { printNoAgentsMessage(ctx, installInvocation{scope: installer.ScopeGlobal}) assert.Contains(t, stderr.String(), "No supported coding agents found on PATH.") } + +// TestTopLevelFailureKeepsErrorsOutsideAgents checks that a run error which joins per-agent failures with another failure, +// such as a sign-in that failed, keeps the other failure as the document's top-level error. +func TestTopLevelFailureKeepsErrorsOutsideAgents(t *testing.T) { + signInErr := errors.New("sign-in failed") + agentErr := &agentErrors{err: errors.New("agent failed")} + + assert.NoError(t, topLevelFailure(agentErr)) + top := topLevelFailure(errors.Join(agentErr, signInErr)) + require.ErrorIs(t, top, signInErr) + assert.Equal(t, signInErr.Error(), top.Error()) +} diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 8aec8f2cfd6..c5d5e3a0fbc 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -181,7 +181,7 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to install for, comma-separated (default: every detected MCP-capable agent)") cmd.Flags().StringVar(&f.name, "name", "", "Server name to use instead of the generated one; only with exactly one service") cmd.Flags().BoolVar(&f.force, "force", false, "Overwrite an existing server with the same name that this CLI did not write") - cmd.Flags().BoolVar(&f.login, "login", false, "Sign in to each sign-in-required server right after it is installed") + cmd.Flags().BoolVar(&f.login, "login", false, "Sign in to each sign-in-required server that was installed, after the installs") cmd.Flags().BoolVar(&f.noLogin, "no-login", false, "Do not sign in; print the login commands for sign-in-required servers (default)") return cmd } diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index e312736a753..88639a54625 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1638,15 +1638,3 @@ func TestPlanningRefusalsCountsRefusedAgents(t *testing.T) { assert.NoError(t, planningRefusals(plans, targets, nil)) } - -// TestTopLevelFailureKeepsErrorsOutsideAgents checks that a run error which joins per-agent failures with another failure, -// such as a sign-in that failed, keeps the other failure as the document's top-level error. -func TestTopLevelFailureKeepsErrorsOutsideAgents(t *testing.T) { - signInErr := errors.New("sign-in failed") - agentErr := &agentErrors{err: errors.New("agent failed")} - - assert.NoError(t, topLevelFailure(agentErr)) - top := topLevelFailure(errors.Join(agentErr, signInErr)) - require.ErrorIs(t, top, signInErr) - assert.Equal(t, signInErr.Error(), top.Error()) -} diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index a33bd5f82d4..36b49b50a73 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -24,14 +24,14 @@ var ErrSignInTimeout = errors.New("sign-in did not complete before the timeout") // ErrSignInFailed means the credential reached the FAILED provisioning state. var ErrSignInFailed = errors.New("sign-in failed") -// SignInOptions controls how SignIn waits. Callers set all four fields. +// SignInOptions controls how SignIn waits. Poll and Timeout are required. Profile names the profile in the notice +// before the page. Open opens the page in the background, and is not called when BROWSER=none, because the URL is +// already printed. type SignInOptions struct { - // Profile is the profile whose user signs in. The notice before the page names it. Profile string Poll time.Duration Timeout time.Duration - // Open opens the sign-in page. It is not called when BROWSER=none, because the URL is already printed. - Open func(ctx context.Context, url string) error + Open func(ctx context.Context, url string) error } // CredentialState returns the provisioning state of the caller's sign-in to a sign-in-required diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index 455104c69ed..3dfa5456562 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -247,7 +247,8 @@ type FakeWorkspace struct { RegisteredModels map[string]catalog.RegisteredModelInfo ModelServices map[string]catalog.ModelService McpServices map[string]catalog.McpService - // McpUserCredentials holds each caller's sign-in to an MCP service, keyed by the service full name. + // McpUserCredentials holds the caller's sign-in to each MCP service, keyed by the service full name. The fake has one + // caller. McpUserCredentials map[string]catalog.McpServiceUserMappedCredential ModelProviderServices map[string]catalog.ModelProviderService From 59d4ebb490cb6639e453505acb686034988dadec Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:46:41 +0000 Subject: [PATCH 36/87] Update the aitools goldens for the pinned hints and the --login help The sign-in hints of servers pinned to DATABRICKS_CONFIG_FILE now set it, and the --login help describes when the sign-in runs. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/add-text/output.txt | 10 ++++------ acceptance/cmd/aitools/mcp/hidden/output.txt | 2 +- acceptance/cmd/aitools/mcp/list-and-remove/output.txt | 3 +-- acceptance/cmd/aitools/mcp/list-check/output.txt | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt index a5f05b70a05..f961048ccec 100644 --- a/acceptance/cmd/aitools/mcp/add-text/output.txt +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -8,14 +8,13 @@ Agents (named with --agents): claude-code, cursor genie_one_mcp-logfood system.ai.genie_one_mcp claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json +Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enable genie_one_mcp-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json - system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). - -Cursor may ask you to approve genie_one_mcp-logfood, github-logfood. Run cursor-agent mcp enable for each (`agent` in the Cursor docs), or approve them in Cursor's MCP settings. +system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood +Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. === Running it again re-asserts the entries, and nothing is written twice @@ -30,8 +29,7 @@ genie_one_mcp-logfood system.ai.genie_one_mcp github-logfood system.ai.github claude-code already installed, re-asserted [TEST_TMP_DIR]/home/.claude.json cursor already installed, re-asserted [TEST_TMP_DIR]/home/.cursor/mcp.json - system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). +system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood === Cursor config diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index 745775325d6..b196d51baaa 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -54,7 +54,7 @@ Flags: --force Overwrite an existing server with the same name that this CLI did not write -h, --help help for add --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share) - --login Sign in to each sign-in-required server right after it is installed + --login Sign in to each sign-in-required server that was installed, after the installs --name string Server name to use instead of the generated one; only with exactly one service --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated --no-login Do not sign in; print the login commands for sign-in-required servers (default) diff --git a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt index 3e2b010c830..22e41ca38be 100644 --- a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt +++ b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt @@ -10,8 +10,7 @@ genie_one_mcp-logfood system.ai.genie_one_mcp 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 - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). +system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg 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]) diff --git a/acceptance/cmd/aitools/mcp/list-check/output.txt b/acceptance/cmd/aitools/mcp/list-check/output.txt index afc3ebb077a..8e17648f078 100644 --- a/acceptance/cmd/aitools/mcp/list-check/output.txt +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -10,7 +10,7 @@ genie_one_mcp-logfood system.ai.genie_one_mcp 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 +system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood === list --check: the token mints, and GitHub needs sign-in From 0f914022100d11ac7dcf133de87d38c9e0cfc477 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:49:36 +0000 Subject: [PATCH 37/87] Order the results of the sign-in test helper as the linter expects Co-authored-by: Isaac --- cmd/aitools/mcp_login_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 7b9930f552a..a41cd06f26f 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -179,7 +179,7 @@ func TestSignInInstalledSignsInToEveryServer(t *testing.T) { // signInInstalledOne signs in to one installed sign-in server of the test's workspace and returns the error and what the // command wrote to stderr, on either stream. The server answers with status and body for the credential. -func signInInstalledOne(t *testing.T, status int, body string) (error, string) { +func signInInstalledOne(t *testing.T, status int, body string) (string, error) { t.Helper() const githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { @@ -208,13 +208,13 @@ func signInInstalledOne(t *testing.T, status int, body string) (error, string) { ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) var errOut bytes.Buffer err = signInInstalled(ctx, &errOut, w, req, plans, servers) - return err, errOut.String() + stderr.String() + return errOut.String() + stderr.String(), err } // TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand checks that a sign-in that failed does not print the // login hint again, since its error already names the command to run. func TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand(t *testing.T) { - err, output := signInInstalledOne(t, http.StatusOK, `{"provisioning_info":{"state":"FAILED"}}`) + output, err := signInInstalledOne(t, http.StatusOK, `{"provisioning_info":{"state":"FAILED"}}`) require.ErrorIs(t, err, mcp.ErrSignInFailed) assert.Contains(t, err.Error(), "Run: databricks aitools mcp login system.ai.github --profile logfood") assert.NotContains(t, output, "needs sign-in") @@ -223,7 +223,7 @@ func TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand(t *testing. // TestSignInInstalledAuthFailureNamesTheAuthCommand checks that a credential the workspace rejects gets the auth command // in its error, and no login hint, which would not fix it. func TestSignInInstalledAuthFailureNamesTheAuthCommand(t *testing.T) { - err, output := signInInstalledOne(t, http.StatusUnauthorized, `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`) + output, err := signInInstalledOne(t, http.StatusUnauthorized, `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`) require.Error(t, err) assert.Contains(t, err.Error(), "Run: databricks auth login --profile logfood") assert.NotContains(t, output, "mcp login") From af4ce3c62c16ab2be4ed2f6ad08d6d7731c76cdc Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:13:28 +0000 Subject: [PATCH 38/87] Expect the resolved config file in the list --check warning test On macOS the temporary directory is a symlink, so the warning names the resolved path under /private. The test now expects the resolved path, with forward slashes. Co-authored-by: Isaac --- cmd/aitools/mcp_list_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 5298a6440ac..b3186224a58 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1254,8 +1254,11 @@ func TestAddChecksAuthWarningNamesPinnedConfigFile(t *testing.T) { require.NoError(t, os.WriteFile(configFile, []byte("[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-revoked\n"), 0o600)) t.Setenv("DATABRICKS_CONFIG_FILE", configFile) saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, configFile)) + // The warning names the file as it resolves, with forward slashes. A temporary directory is a symlink on macOS. + resolved, err := mcp.ResolveConfigFile(t.Context(), configFile) + require.NoError(t, err) _, out := checkedListServer(t) require.Len(t, out.Warnings, 1) - assert.Contains(t, out.Warnings[0], "Run: DATABRICKS_CONFIG_FILE="+configFile+" databricks auth login --profile logfood") + assert.Contains(t, out.Warnings[0], "Run: DATABRICKS_CONFIG_FILE="+shellquote.BashArg(filepath.ToSlash(resolved))+" databricks auth login --profile logfood") } From face43874b6422512c77e294f4dda57bdde0f0bc Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:13:52 +0000 Subject: [PATCH 39/87] Write pinned config file paths with forward slashes in hints ConfigFileCommand is the one place a pinned config file enters a hint. On Windows the file was printed with backslashes, which broke the golden outputs and the rule that hints use forward slashes. Co-authored-by: Isaac --- libs/aitools/mcp/binding.go | 5 +++-- libs/aitools/mcp/binding_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index b839b537bca..532b40ae76d 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -193,12 +193,13 @@ func RecreateCommand(host, profileName, configFile string) string { } // ConfigFileCommand returns command with DATABRICKS_CONFIG_FILE set to configFile, when configFile is not "". A command -// that a hint prints names the file of a profile that is not the one the shell reads. +// that a hint prints names the file of a profile that is not the one the shell reads. The file is written with forward +// slashes on every platform, as the other paths in the hints are. func ConfigFileCommand(configFile, command string) string { if configFile == "" { return command } - return "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(configFile) + " " + command + return "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(filepath.ToSlash(configFile)) + " " + command } // profileGoneError builds the PROFILE_NOT_FOUND error for a profile that the config file does not diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index a2170756687..2f3ef2dd5a8 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -884,3 +884,12 @@ func TestAuthErrorNamesPinnedConfigFile(t *testing.T) { "Invalid access token.\nRun: DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks auth login --profile logfood") assert.EqualError(t, mcp.AuthError("logfood", "", cause), "Invalid access token.\nRun: databricks auth login --profile logfood") } + +// TestConfigFileCommandUsesForwardSlashes checks that a pinned config file is named with forward slashes, so that the +// hints read the same on Windows. A path that filepath builds there has backslashes, which the shell command must not +// carry. On other systems the two forms are the same, so the test guards the Windows run. +func TestConfigFileCommandUsesForwardSlashes(t *testing.T) { + path := filepath.Join("work", "cfg", "logfood.cfg") + assert.Equal(t, "DATABRICKS_CONFIG_FILE="+filepath.ToSlash(path)+" databricks auth login --profile logfood", + mcp.ConfigFileCommand(path, "databricks auth login --profile logfood")) +} From 7f6150bf2600d42dc090a30366b3b887cffa69db Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:15:20 +0000 Subject: [PATCH 40/87] Serialize the default browser opener and return when a browser command backgrounds a child The default opener swaps the output streams of pkg/browser for each open, so two opens that overlap (sign-in can start one while add --login starts another) could leave the streams swapped. A mutex now covers the swap and the open. A custom BROWSER command ran with piped stdout and stderr that were copied to stderr before Wait. A command that starts a child and returns, such as one that backgrounds the browser, kept the pipes open, so the copy never ended and the call hung. The command's output now goes to stderr directly, as the streams of the inherited output do, and stdin is inherited as it is for Open. Co-authored-by: Isaac --- libs/browser/browser.go | 25 ++++------- libs/browser/browser_test.go | 81 ++++++++++++++++++++++++++++++++++-- libs/exec/exec.go | 12 ++++++ 3 files changed, 99 insertions(+), 19 deletions(-) diff --git a/libs/browser/browser.go b/libs/browser/browser.go index 0a782d79549..a38089c5175 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -61,7 +61,13 @@ func IsDisabled(ctx context.Context) bool { return env.Get(ctx, "BROWSER") == "none" } +// defaultOpenMu serializes the default opener, which swaps the streams of pkg/browser for the duration of an open. +// Sign-in can start an open while another one runs. +var defaultOpenMu sync.Mutex + func openDefault(url string, stdoutToStderr bool) error { + defaultOpenMu.Lock() + defer defaultOpenMu.Unlock() // github.com/pkg/browser writes xdg-open's output to os.Stdout and its error output to os.Stderr, even when the // open succeeds, producing spurious noise on Linux desktops. The error output is discarded. originalStdout, originalStderr := browserpkg.Stdout, browserpkg.Stderr @@ -81,27 +87,14 @@ func openWithCommand(ctx context.Context, browserCmd, url string, stdoutToStderr if err != nil { return err } - if !stdoutToStderr { + if stdoutToStderr { + e.WithInheritOutputToStderr() + } else { e.WithInheritOutput() } cmd, err := e.StartCommand(ctx, fmt.Sprintf("%q %q", browserCmd, url)) if err != nil { return err } - if !stdoutToStderr { - return cmd.Wait() - } - // The command's stdout is a pipe, and Wait closes it, so it is copied to stderr before Wait. - var copied sync.WaitGroup - copied.Add(2) - go func() { - defer copied.Done() - _, _ = io.Copy(os.Stderr, cmd.Stdout()) - }() - go func() { - defer copied.Done() - _, _ = io.Copy(os.Stderr, cmd.Stderr()) - }() - copied.Wait() return cmd.Wait() } diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index d0ae4cca868..20a8b9ee43b 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -3,9 +3,15 @@ package browser import ( "context" "fmt" + "io/fs" "os" + "os/exec" "path/filepath" + "sync" "testing" + "time" + + browserpkg "github.com/pkg/browser" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" @@ -41,12 +47,24 @@ func TestOpenWithBrowserNonePrintsURL(t *testing.T) { assert.Contains(t, stderr.String(), "https://example.com/resource") } -// openerChildEnv marks the child process that the tests below start as the browser command. -const openerChildEnv = "LIBS_BROWSER_TEST_OPENER" +const ( + // openerChildEnv marks the child process that the tests below start as the browser command. + openerChildEnv = "LIBS_BROWSER_TEST_OPENER" + // openerBackgroundEnv, when set in the browser command, makes it start a sleeper and return, as a launcher does when + // it backgrounds the browser. Its value is the file the sleeper writes when it ends. + openerBackgroundEnv = "LIBS_BROWSER_TEST_BACKGROUND" + // openerSleeperEnv marks the child that openerBackgroundEnv starts, and names the file it writes. + openerSleeperEnv = "LIBS_BROWSER_TEST_SLEEPER" + // openerSleepTime is how long the sleeper runs. It is short, so that the tests end soon even when the wait is wrong. + openerSleepTime = 3 * time.Second +) // TestMain lets the tests use this test binary as a browser command that writes to stdout, as a launcher can. func TestMain(m *testing.M) { - if os.Getenv(openerChildEnv) != "" { + switch { + case os.Getenv(openerSleeperEnv) != "": + os.Args = []string{os.Args[0], "-test.run=^TestOpenerSleeper$"} + case os.Getenv(openerChildEnv) != "": os.Args = []string{os.Args[0], "-test.run=^TestOpenerChild$"} } m.Run() @@ -58,6 +76,28 @@ func TestOpenerChild(t *testing.T) { t.Skip("runs only as the browser command of the tests below") } fmt.Print("opener stdout") + sleepFile := os.Getenv(openerBackgroundEnv) + if sleepFile == "" { + return + } + // The sleeper inherits this process's output, as a browser started in the background does, and outlives the command. + sleeper := exec.Command(os.Args[0], "-test.run=^TestOpenerSleeper$") + sleeper.Env = append(os.Environ(), openerSleeperEnv+"="+sleepFile) + sleeper.Stdout = os.Stdout + sleeper.Stderr = os.Stderr + if err := sleeper.Start(); err != nil { + t.Fatal(err) + } +} + +// TestOpenerSleeper is the child that openerBackgroundEnv starts. It runs only in that child process. +func TestOpenerSleeper(t *testing.T) { + file := os.Getenv(openerSleeperEnv) + if file == "" { + t.Skip("runs only as the child of the browser command") + } + time.Sleep(openerSleepTime) + _ = os.WriteFile(file, nil, 0o600) } // runOpener calls open with this test binary as the browser command, and returns what the command wrote to stdout and @@ -103,3 +143,38 @@ func TestOpenKeepsCommandStdoutOnStdout(t *testing.T) { assert.Contains(t, stdout, "opener stdout") assert.NotContains(t, stderr, "opener stdout") } + +// TestOpenOutputToStderrReturnsWhenCommandBackgroundsChild checks that a browser command that starts a child and returns +// does not hold up the caller. The child outlives the command, as a launcher that backgrounds the browser does, and writes +// its file only when it ends. The file must not exist when the call returns. +func TestOpenOutputToStderrReturnsWhenCommandBackgroundsChild(t *testing.T) { + t.Setenv(openerChildEnv, "1") + sleepFile := filepath.Join(t.TempDir(), "sleeper-done") + t.Setenv(openerBackgroundEnv, sleepFile) + ctx := env.Set(t.Context(), "BROWSER", os.Args[0]) + + require.NoError(t, OpenOutputToStderr(ctx, "https://example.com/login")) + _, err := os.Stat(sleepFile) + assert.ErrorIs(t, err, fs.ErrNotExist, "the call waited for the child that the browser command started") +} + +// TestOpenDefaultOverlappingOpensRestoreStreams checks that two default opens that run at once, as sign-in can start +// one while add --login starts another, leave the streams of pkg/browser as they were. The PATH is empty, so the +// opener cannot start a browser: the opens fail at once, after they have swapped the streams. The race detector reports +// the unsynchronized swaps when two opens overlap. +func TestOpenDefaultOverlappingOpensRestoreStreams(t *testing.T) { + t.Setenv("BROWSER", "") + t.Setenv("PATH", t.TempDir()) + stdout, stderr := browserpkg.Stdout, browserpkg.Stderr + + var opens sync.WaitGroup + for range 2 { + opens.Go(func() { + _ = OpenOutputToStderr(t.Context(), "https://example.com/login") + }) + } + opens.Wait() + + assert.Equal(t, stdout, browserpkg.Stdout) + assert.Equal(t, stderr, browserpkg.Stderr) +} diff --git a/libs/exec/exec.go b/libs/exec/exec.go index feea8af0f92..e40d91ca7e9 100644 --- a/libs/exec/exec.go +++ b/libs/exec/exec.go @@ -76,6 +76,8 @@ type Executor struct { shell shell dir string inheritOutput bool + // stdoutToStderr sends the command's stdout to stderr when inheritOutput is set. + stdoutToStderr bool } // NewCommandExecutor creates an Executor with default output behavior (no inheritance) @@ -95,6 +97,13 @@ func (e *Executor) WithInheritOutput() { e.inheritOutput = true } +// WithInheritOutputToStderr is WithInheritOutput, except that the command's stdout goes to stderr. A command that holds +// its stdout open after it returns does not hold up Wait. +func (e *Executor) WithInheritOutputToStderr() { + e.inheritOutput = true + e.stdoutToStderr = true +} + func NewCommandExecutorWithExecutable(dir string, execType ExecutableType) (*Executor, error) { f, ok := finders[execType] if !ok { @@ -132,6 +141,9 @@ func (e *Executor) StartCommand(ctx context.Context, command string) (Command, e func (e *Executor) start(cmd *osexec.Cmd, ec *execContext) (Command, error) { if e.inheritOutput { cmd.Stdout = os.Stdout + if e.stdoutToStderr { + cmd.Stdout = os.Stderr + } cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin return &command{cmd, ec, nil, nil}, cmd.Start() From 2e6ef773d204e57b2041592a5cdb31887e087ebd Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:17:05 +0000 Subject: [PATCH 41/87] Name the pinned config file in the login hint of a refresh or binding failure Serve and Bind passed an empty pinned file to the auth error, so the login command for a server pinned to a config file was bare and logged in to the file the shell reads. A new helper, PinnedFile, gives the file for an entry pinned to one, and the sign-in command uses it too. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 5 +---- cmd/aitools/mcp_serve.go | 15 +++++++++------ cmd/aitools/mcp_serve_test.go | 18 +++++++++++++++++- libs/aitools/mcp/binding.go | 12 +++++++++++- libs/aitools/mcp/binding_test.go | 6 ++++-- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 10fd2e0598b..a2696118436 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -132,10 +132,7 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri return err } // A server pinned to a config file names that file in the hints, as add does. - pinnedFile := "" - if entry != nil && entry.ConfigFile != "" { - pinnedFile = configFile - } + pinnedFile := mcp.PinnedFile(entry, configFile) w, err := profileClient(ctx, profileName, configFile, pinnedFile) if err != nil { return err diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 907ba00ae8f..d9d0e7bd6e3 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -246,7 +246,7 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { profile: profileName, configFile: pinnedConfigFile(entry, cfg), }, - }) + }, mcp.PinnedFile(entry, cfg.ConfigFile)) } // pinnedConfigFile returns the config file that entry pins its profile to, or "" when the entry has none. @@ -258,11 +258,12 @@ func pinnedConfigFile(entry *mcp.Entry, cfg *config.Config) string { } // serveProxy bridges opts to the service until stdin closes or a signal arrives. Authenticate goes through a -// refreshGuard, so a rejected token stops the proxy; see proxyResult. -func serveProxy(cmd *cobra.Command, opts proxy.Options) error { +// refreshGuard, so a rejected token stops the proxy; see proxyResult. pinnedFile is the config file the server is +// pinned to, or "" (see mcp.PinnedFile). +func serveProxy(cmd *cobra.Command, opts proxy.Options, pinnedFile string) error { ctx, stop := serveSignalContext(cmd.Context()) defer stop() - refresh := &refreshGuard{authenticate: opts.Authenticate, profile: opts.Profile, cancel: stop} + refresh := &refreshGuard{authenticate: opts.Authenticate, profile: opts.Profile, pinnedFile: pinnedFile, cancel: stop} opts.Authenticate = refresh.authenticateRequest return proxyResult(cmd, refresh, proxy.Run(ctx, opts)) } @@ -334,7 +335,9 @@ func exitAlreadyPrinted(err error, status int) error { type refreshGuard struct { authenticate func(*http.Request) error profile string - cancel context.CancelFunc + // pinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). + pinnedFile string + cancel context.CancelFunc mu sync.Mutex err error @@ -362,7 +365,7 @@ func (g *refreshGuard) authenticateRequest(r *http.Request) error { } // The hint says to restart the server: a new spawn re-runs the binding checks, and it is the one that picks up the // new login. - authErr := fmt.Errorf("%w, then restart the MCP server in your agent", mcp.AuthError(g.profile, "", err)) + authErr := fmt.Errorf("%w, then restart the MCP server in your agent", mcp.AuthError(g.profile, g.pinnedFile, err)) g.mu.Lock() first := g.err == nil if first { diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index fcf97ec01e1..eccccdc4984 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -113,6 +113,22 @@ func TestRefreshGuardStopsLaterRequestsWithoutAskingAgain(t *testing.T) { assert.ErrorIs(t, g.failure(), cause) } +// TestRefreshGuardNamesPinnedConfigFile checks that a refresh failure of a server pinned to a config file names that file +// in its login command, so that the command logs in to the file the profile is read from. +func TestRefreshGuardNamesPinnedConfigFile(t *testing.T) { + g := &refreshGuard{ + authenticate: func(*http.Request) error { return errors.New(refreshTokenExpired) }, + profile: "logfood", + pinnedFile: "/work/cfg/logfood.cfg", + cancel: func() {}, + } + + _ = g.authenticateRequest(&http.Request{Header: http.Header{}}) + + require.Error(t, g.failure()) + assert.Contains(t, g.failure().Error(), "Run: DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks auth login --profile logfood") +} + // TestRefreshGuardStoresAuthError checks that the refresh failure reaches the user as the AUTH_FAILED error, which names // the profile and its login command. func TestRefreshGuardStoresAuthError(t *testing.T) { @@ -668,7 +684,7 @@ func runRefreshFailureSession(t *testing.T, end error) (string, bool, error) { Config: &config.Config{Host: server.URL}, Authenticate: expiresOnToolsList, Profile: "logfood", - }) + }, "") return stderr.String(), deleted.Load(), err } diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 532b40ae76d..4243a2c3018 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -87,7 +87,7 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) if entry == nil { fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s\n", opts.Service, opts.Profile, cfg.Host) } - if err := AuthenticateBound(ctx, cfg, opts.Profile, ""); err != nil { + if err := AuthenticateBound(ctx, cfg, opts.Profile, PinnedFile(entry, configFile)); err != nil { return nil, nil, err } return cfg, entry, nil @@ -126,6 +126,16 @@ func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName, pin return nil } +// PinnedFile returns the config file that a hint for entry must name, or "" when the hint's command reads the same file +// without one. An entry pinned to a config file names configFile, the file the profile is read from. A nil entry has no +// pin. +func PinnedFile(entry *Entry, configFile string) string { + if entry != nil && entry.ConfigFile != "" { + return configFile + } + return "" +} + // CheckBinding returns the state entry for service and profileName bound to configFile, the config file the caller // resolved with ResolveConfigFile, or nil when there is none. It returns the error Bind returns when the profile does not // match: the profile is gone or has no host, or with an entry, its host or workspace changed. The profile is checked diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 2f3ef2dd5a8..5171a8f095a 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -297,7 +297,7 @@ func TestBindNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindAuthFailed) - assert.Contains(t, err.Error(), "\nRun: databricks auth login --profile logfood") + assert.Contains(t, err.Error(), "\nRun: DATABRICKS_CONFIG_FILE="+shellquote.BashArg(filepath.ToSlash(bindingTestResolved(t, cfgPath)))+" databricks auth login --profile logfood") assert.NotContains(t, err.Error(), "dapi-env-token") assert.Error(t, errors.Unwrap(err)) } @@ -354,7 +354,9 @@ func TestBindTokenCannotBeMinted(t *testing.T) { Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindAuthFailed) - assert.Contains(t, err.Error(), "\nRun: databricks auth login --profile logfood") + // The server is pinned to the file, so the login command names it: the bare command would log in to the file the + // shell reads. + assert.Contains(t, err.Error(), "\nRun: DATABRICKS_CONFIG_FILE="+shellquote.BashArg(filepath.ToSlash(bindingTestResolved(t, cfgPath)))+" databricks auth login --profile logfood") assert.Error(t, errors.Unwrap(err)) } From 7c1984d5f6dc99da5deda3ab77d59f007626e04f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:18:21 +0000 Subject: [PATCH 42/87] Name the file the shell reads in the login hint of an unpinned list --check warning The rejected-token warning for an unpinned profile gave a bare login command, which writes the file DATABRICKS_CONFIG_FILE names. The file is now derived from the caller's context before the check sets the environment to the profile's file, as the drift hints already do. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 33 +++++++++++++++++++-------------- cmd/aitools/mcp_list_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index f9e25551106..756f57d8a43 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -413,8 +413,11 @@ func addChecks(ctx context.Context, out *mcpListOutput) { if p.Status != statusOK { continue } + // The login hints name the file that login writes, which the shell's environment decides. It is derived before + // withConfigFile sets the environment to the profile's file. + login := loginFile(ctx, *p, p.ConfigFile != "") err := withConfigFile(ctx, p.configPath, func(ctx context.Context) { - addProfileChecks(ctx, p, out) + addProfileChecks(ctx, p, login, out) }) if err != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) @@ -422,14 +425,15 @@ func addChecks(ctx context.Context, out *mcpListOutput) { } } -// addProfileChecks runs the checks of one profile, whose config file is in ctx. A profile whose token cannot be minted -// has no sign-in checks: its warning says why. -func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput) { - w, err := profileClient(ctx, p.Profile, p.configPath, p.ConfigFile) +// addProfileChecks runs the checks of one profile, whose config file is in ctx. login is the config file that the +// profile's login hints name, or "" (see loginFile). A profile whose token cannot be minted has no sign-in checks: its +// warning says why. +func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out *mcpListOutput) { + w, err := profileClient(ctx, p.Profile, p.configPath, login) if err == nil { // A personal access token is sent only with a request, so the token is checked with one. if _, err = w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { - err = describeRequestError(p.Profile, p.ConfigFile, err) + err = describeRequestError(p.Profile, login, err) } } p.token = statusOK @@ -447,7 +451,7 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, out *mcpListOutput } continue } - status, signInErr := signInStatus(ctx, w, p.Profile, p.ConfigFile, s.Service) + status, signInErr := signInStatus(ctx, w, p.Profile, login, s.Service) s.Check.SignIn = status if signInErr != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", s.Name, signInErr)) @@ -645,17 +649,18 @@ func serverHint(ctx context.Context, p mcpListProfile, s mcpListServer) string { } 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: run `%s` or `%s`", remove, mcp.RecreateCommand(s.entry.Host, p.Profile, loginFile(ctx, p, s.entry.ConfigFile != ""))) } 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 != "" { +// loginFile returns the config file that a login hint for the servers of profile p must name, or "" when the login's +// default file is that file. pinned says whether the servers are pinned to a config 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. ctx must be the caller's, not one that withConfigFile has set to the profile's file. +func loginFile(ctx context.Context, p mcpListProfile, pinned bool) string { + if pinned { return filepath.ToSlash(p.configPath) } environment := env.Get(ctx, mcpconfig.ConfigFileEnv) diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index b3186224a58..44b4e6534dc 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1262,3 +1262,29 @@ func TestAddChecksAuthWarningNamesPinnedConfigFile(t *testing.T) { require.Len(t, out.Warnings, 1) assert.Contains(t, out.Warnings[0], "Run: DATABRICKS_CONFIG_FILE="+shellquote.BashArg(filepath.ToSlash(resolved))+" databricks auth login --profile logfood") } + +// TestAddChecksAuthWarningNamesFileTheShellNames checks the login command in the warning for a rejected token of an +// unpinned server. The profile is checked in ~/.databrickscfg, and login writes the file DATABRICKS_CONFIG_FILE names, so +// the command names the checked file when the shell names another one. +func TestAddChecksAuthWarningNamesFileTheShellNames(t *testing.T) { + home := withHome(t) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/2.0/preview/scim/v2/Me" { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`)) + return + } + w.WriteHeader(http.StatusNotFound) + })) + t.Cleanup(server.Close) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-revoked\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "other.cfg")) + checked, err := mcp.ResolveConfigFile(t.Context(), "") + require.NoError(t, err) + + _, out := checkedListServer(t) + require.Len(t, out.Warnings, 1) + assert.Contains(t, out.Warnings[0], "Run: DATABRICKS_CONFIG_FILE="+shellquote.BashArg(filepath.ToSlash(checked))+" databricks auth login --profile logfood") +} From 0c1a58f7752d11642e557d26ea2c375255799fc7 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:18:52 +0000 Subject: [PATCH 43/87] Let logout remove a credential whose service the profile can no longer see Logout looked up the service before it deleted the credential, so a credential for a service whose source connection was deleted or unshared could not be removed. The credential is deleted first. The service is looked up only when there is no credential, to tell a name that is not a service from a caller who is not signed in. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 19 +++++++++++-------- cmd/aitools/mcp_login_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index a2696118436..77226c4731d 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -178,20 +178,23 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie } func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput, pinnedFile string) error { - if _, err := serviceFor(ctx, w, out.Profile, pinnedFile, out.Service); err != nil { - return err - } + // The credential is deleted first, so that the credential of a service the profile can no longer see is removed too. + // The service is looked up only when there is no credential, to tell a name that is not a service from a caller who + // is not signed in. deleted, err := mcp.SignOut(ctx, w, out.Service) if err != nil { return describeRequestError(out.Profile, pinnedFile, err) } - if !deleted { - out.State = signOutStateNotSignedIn - fmt.Fprintf(errOut, "Not signed in to %s on profile %s.\n", out.Service, out.Profile) + if deleted { + out.State = signOutStateDeleted + fmt.Fprintf(errOut, "Signed out of %s on profile %s.\n", out.Service, out.Profile) return nil } - out.State = signOutStateDeleted - fmt.Fprintf(errOut, "Signed out of %s on profile %s.\n", out.Service, out.Profile) + if _, err := serviceFor(ctx, w, out.Profile, pinnedFile, out.Service); err != nil { + return err + } + out.State = signOutStateNotSignedIn + fmt.Fprintf(errOut, "Not signed in to %s on profile %s.\n", out.Service, out.Profile) return nil } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index a41cd06f26f..2a2d1ffa6ba 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -114,6 +114,39 @@ func TestMcpLogoutUnknownServiceIsAnError(t *testing.T) { assert.Empty(t, out.State) } +// TestMcpLogoutRemovesCredentialOfDeletedService checks that logout removes a credential whose service is gone from the +// profile's view, because its source connection was deleted. The service lookup fails, but the credential is still there. +func TestMcpLogoutRemovesCredentialOfDeletedService(t *testing.T) { + home := withHome(t) + const ( + servicePath = "/api/2.1/unity-catalog/mcp-services/system.ai.github" + credentialPath = servicePath + "/user-credentials" + ) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + switch { + case r.URL.Path == servicePath && r.Method == http.MethodGet: + _, _ = w.Write([]byte(`{"name":"mcp-services/system.ai.github","config":{"source_connection":{"is_deleted":true}}}`)) + case r.URL.Path == credentialPath && r.Method == http.MethodDelete: + _, _ = w.Write([]byte(`{}`)) + default: + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"error_code":"NOT_FOUND","message":"not found"}`)) + } + })) + t.Cleanup(server.Close) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + + out := mcpSignInOutput{Service: "system.ai.github"} + err := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.github", mcpLogout, &out) + require.NoError(t, err) + assert.Equal(t, signOutStateDeleted, out.State) +} + // TestMcpLogoutEndpointNotFoundIsNotAServiceError checks that a 404 for an endpoint the API does not have is not read as a // service the profile cannot see, which would send the user to look for a typo in the name. func TestMcpLogoutEndpointNotFoundIsNotAServiceError(t *testing.T) { From c53dde16d6010bb70cadcd00d269a2e148eb8dce Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:19:33 +0000 Subject: [PATCH 44/87] Print no login hint after an add --login failure that a login does not fix The hint after a failed sign-in in add --login was printed for every failure except a few, so a refused sign-in (PERMISSION_DENIED) or a cancelled command got a login hint that would not fix it, and the hint was printed in JSON mode, unlike the other hints. The timeout and failed errors already name the login command, so the hint is removed. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 14 +++----------- cmd/aitools/mcp_login_test.go | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index c5d5e3a0fbc..724741f6eb0 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -1026,8 +1026,9 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { } // signInInstalled signs in to each sign-in-required server that reached an agent, in order. A failure does not stop the -// others, and the failures are joined. The servers are installed either way. A failed server gets the login hint, on the -// same stream as the hints of the results, unless its error already names the command to run. +// others, and the failures are joined. The servers are installed either way. A failure names the command to run next +// when a sign-in can fix it, as the timeout and failed errors do. Other failures get no hint, since logging in again +// would not fix them. func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, req addRequest, plans []addPlan, servers []addServerOutput) error { var errs []error for i, server := range servers { @@ -1036,21 +1037,12 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) if _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service); err != nil { - if !namesSignInCommand(err) { - cmdio.LogString(ctx, signInHint(server.Service, req.profile, req.pinned)) - } errs = append(errs, err) } } return errors.Join(errs...) } -// namesSignInCommand reports whether a sign-in failure already names the command to run next: a sign-in that timed out -// or failed names the login command, and a credential the workspace rejected names the auth command. -func namesSignInCommand(err error) bool { - return errors.Is(err, mcp.ErrSignInTimeout) || errors.Is(err, mcp.ErrSignInFailed) || errors.Is(err, apierr.ErrUnauthenticated) -} - // signInHint is the command that signs the profile in to a sign-in-required service. func signInHint(service, profileName, pinnedFile string) string { return fmt.Sprintf("%s needs sign-in. Run: %s", service, mcpLoginHint(service, profileName, pinnedFile)) diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 2a2d1ffa6ba..4d36d83f41b 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -17,6 +17,7 @@ import ( "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -162,7 +163,7 @@ func TestMcpLogoutEndpointNotFoundIsNotAServiceError(t *testing.T) { } // TestSignInInstalledSignsInToEveryServer checks that add --login attempts every installed sign-in server after one of -// them fails. The failed server gets the login hint, and the failure is returned, so the others are still signed in. +// them fails. The failure is returned, and the other servers are still signed in. func TestSignInInstalledSignsInToEveryServer(t *testing.T) { const ( githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" @@ -206,14 +207,16 @@ func TestSignInInstalledSignsInToEveryServer(t *testing.T) { err = signInInstalled(ctx, &errOut, w, req, plans, servers) require.Error(t, err) assert.Equal(t, []string{githubPath, geniePath}, reads) - assert.Contains(t, stderr.String(), "system.ai.github needs sign-in. Run: databricks aitools mcp login system.ai.github --profile logfood\n") - assert.NotContains(t, stderr.String(), "system.ai.genie_one_mcp needs sign-in") + // The workspace refused the sign-in with PERMISSION_DENIED, which a login does not fix, so no login hint is printed. + assert.NotContains(t, stderr.String(), "needs sign-in") } // signInInstalledOne signs in to one installed sign-in server of the test's workspace and returns the error and what the // command wrote to stderr, on either stream. The server answers with status and body for the credential. func signInInstalledOne(t *testing.T, status int, body string) (string, error) { t.Helper() + // BROWSER=none keeps the test from opening a browser; the page is only printed. + t.Setenv("BROWSER", "none") const githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { if !strings.HasPrefix(r.URL.Path, "/api/") { @@ -253,6 +256,14 @@ func TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand(t *testing. assert.NotContains(t, output, "needs sign-in") } +// TestSignInInstalledNoLoginHintForFailureLoginDoesNotFix checks that a sign-in the workspace refuses gets no login hint: +// logging in again would not fix a refusal, and the hint would be printed in JSON mode too. +func TestSignInInstalledNoLoginHintForFailureLoginDoesNotFix(t *testing.T) { + output, err := signInInstalledOne(t, http.StatusForbidden, `{"error_code":"PERMISSION_DENIED","message":"no access"}`) + require.ErrorIs(t, err, apierr.ErrPermissionDenied) + assert.NotContains(t, output, "needs sign-in") +} + // TestSignInInstalledAuthFailureNamesTheAuthCommand checks that a credential the workspace rejects gets the auth command // in its error, and no login hint, which would not fix it. func TestSignInInstalledAuthFailureNamesTheAuthCommand(t *testing.T) { From 69f146a527ef1c9858da73be97ae1da4d2edba2c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:20:31 +0000 Subject: [PATCH 45/87] Report a skipped list --check and the sign-in of a server the service no longer shows A profile whose binding drifted is not checked, and its text listing gave no sign of that. The text now says the check was skipped and why, only when --check ran. A server that never needed sign-in gets no sign-in value when its service cannot be read, and the warning is the only report; a server that did need sign-in stays unknown. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 16 ++++++++--- cmd/aitools/mcp_list_test.go | 52 +++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 756f57d8a43..664d9ec3a5b 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -76,6 +76,8 @@ type mcpListProfile struct { // token is the --check result of minting a token for the profile. The JSON carries it in each server's check, // so it is text only. token string + // checkSkipped is set by --check for a profile that drifted, which is not checked. The text says so. + checkSkipped bool } type mcpListServer struct { @@ -411,6 +413,7 @@ func addChecks(ctx context.Context, out *mcpListOutput) { for i := range out.Profiles { p := &out.Profiles[i] if p.Status != statusOK { + p.checkSkipped = true continue } // The login hints name the file that login writes, which the shell's environment decides. It is derived before @@ -451,7 +454,7 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out } continue } - status, signInErr := signInStatus(ctx, w, p.Profile, login, s.Service) + status, signInErr := signInStatus(ctx, w, p.Profile, login, s.Service, s.SignInRequired) s.Check.SignIn = status if signInErr != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", s.Name, signInErr)) @@ -461,10 +464,15 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out // signInStatus reads the caller's sign-in to a service, as the service requires it now. Only an ACTIVE credential // counts as signed in. A service that does not require sign-in is "", which is not a status. A service the profile can -// no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error says why. -func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string) (string, error) { +// no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error says why. A +// service that cannot be read is unknown only when the record says the server needed sign-in (recorded); otherwise there +// is no sign-in to report, and the error is the warning. +func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string, recorded bool) (string, error) { svc, err := serviceFor(ctx, w, profileName, pinnedFile, service) if err != nil { + if !recorded { + return "", err + } return statusUnknown, err } if !svc.SignInRequired { @@ -569,6 +577,8 @@ func renderMcpList(ctx context.Context, out mcpListOutput, profileName string) { line := fmt.Sprintf(" %-24s %-30s %s", s.Name, s.Service, listAgentsText(s.Agents)) if text := checkText(s.Check); text != "" { line += " " + text + } else if p.checkSkipped { + line += " check skipped: the profile no longer matches this server" } cmdio.LogString(ctx, line) if hint := serverHint(ctx, p, s); hint != "" { diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 44b4e6534dc..257552438ae 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -686,6 +686,26 @@ func TestRenderMcpListLoginHintNamesCheckedFile(t *testing.T) { } } +// TestRenderMcpListSaysWhyCheckIsSkipped checks that a server whose profile drifted says in the text that its check was +// skipped, and that a listing without --check does not say so. +func TestRenderMcpListSaysWhyCheckIsSkipped(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", "https://adb-1.test", "")) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.NotContains(t, stderr.String(), "check skipped") + + addChecks(t.Context(), &out) + ctx, stderr = cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.Contains(t, stderr.String(), "check skipped: the profile no longer matches this server") +} + // 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) { @@ -996,6 +1016,23 @@ func TestAddChecksWarnsWhenTheSignInCannotBeRead(t *testing.T) { assert.Contains(t, out.Warnings[0], "Run: databricks auth login --profile logfood") } +// TestAddChecksServiceGoneWithoutSignInIsAWarningOnly checks that a server that never needed sign-in, whose service the +// profile can no longer see, gets a warning and no sign-in value. Its sign-in is not what is unknown. +func TestAddChecksServiceGoneWithoutSignInIsAWarningOnly(t *testing.T) { + home := withHome(t) + server := listCheckServer(t, http.StatusNotFound, http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + entry := listCheckGitHub(t, "github-logfood", "logfood", server.URL, "") + entry.SignInRequired = false + saveState(t, entry) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Empty(t, s.Check.SignIn) + require.Len(t, out.Warnings, 1) + assert.Equal(t, "MCP server github-logfood: system.ai.github is not an MCP service visible to profile logfood", out.Warnings[0]) +} + // TestAddChecksServiceGoneIsAWarningNotSignInNeeded checks that a service the profile no longer sees is a warning, and that // its sign-in is unknown rather than needed, since the sign-in is not what is missing. func TestAddChecksServiceGoneIsAWarningNotSignInNeeded(t *testing.T) { @@ -1141,14 +1178,17 @@ func TestSignInStatus(t *testing.T) { serviceOK bool credential int body string + recorded bool want string warns bool }{ - {name: "active", serviceOK: true, credential: http.StatusOK, body: `{"provisioning_info":{"state":"ACTIVE"}}`, want: statusOK}, - {name: "provisioning", serviceOK: true, credential: http.StatusOK, body: `{"provisioning_info":{"state":"PROVISIONING"}}`, want: checkNeeded}, - {name: "not signed in", serviceOK: true, credential: http.StatusNotFound, body: `{"error_code":"NOT_FOUND","message":"not found"}`, want: checkNeeded}, - {name: "rejected token", serviceOK: true, credential: http.StatusUnauthorized, body: `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`, want: statusUnknown, warns: true}, - {name: "service gone", serviceOK: false, credential: http.StatusNotFound, body: `{"error_code":"NOT_FOUND","message":"not found"}`, want: statusUnknown, warns: true}, + {name: "active", serviceOK: true, credential: http.StatusOK, body: `{"provisioning_info":{"state":"ACTIVE"}}`, recorded: true, want: statusOK}, + {name: "provisioning", serviceOK: true, credential: http.StatusOK, body: `{"provisioning_info":{"state":"PROVISIONING"}}`, recorded: true, want: checkNeeded}, + {name: "not signed in", serviceOK: true, credential: http.StatusNotFound, body: `{"error_code":"NOT_FOUND","message":"not found"}`, recorded: true, want: checkNeeded}, + {name: "rejected token", serviceOK: true, credential: http.StatusUnauthorized, body: `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`, recorded: true, want: statusUnknown, warns: true}, + {name: "service gone", serviceOK: false, credential: http.StatusNotFound, body: `{"error_code":"NOT_FOUND","message":"not found"}`, recorded: true, want: statusUnknown, warns: true}, + // The record says the server never needed sign-in, so an unreadable service has no sign-in to report. + {name: "service gone, never needed sign-in", serviceOK: false, credential: http.StatusNotFound, body: `{"error_code":"NOT_FOUND","message":"not found"}`, want: "", warns: true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -1160,7 +1200,7 @@ func TestSignInStatus(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - status, err := signInStatus(t.Context(), w, "logfood", "", "system.ai.github") + status, err := signInStatus(t.Context(), w, "logfood", "", "system.ai.github", tc.recorded) assert.Equal(t, tc.want, status) if tc.warns { require.Error(t, err) From 6791c3cba51e18dd1e65f7b7ded0be9020c58016 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:21:03 +0000 Subject: [PATCH 46/87] Synchronize the sign-in test that reads the opened URL, and gate the opener-failure test TestSignInPollsUntilActive read a slice the opener goroutine wrote, with no synchronization. The URL now arrives on a buffered channel. TestSignInReportsOpenerFailure depended on the poll interval winning against the opener goroutine. The credential now stays NOT_FOUND until the failure is printed, so the wait cannot end first. Co-authored-by: Isaac --- libs/aitools/mcp/login_test.go | 47 ++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 1cf4c67b004..f5998639e68 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -39,6 +39,8 @@ type loginTestServer struct { deleted bool // signedIn is what DELETE answers: true for 200, false for 404. signedIn bool + // gate, when set, holds the credential at NOT_FOUND until it returns true. The script does not advance meanwhile. + gate func() bool } func (s *loginTestServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { @@ -49,8 +51,11 @@ func (s *loginTestServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { case r.URL.Path == loginTestServicePath && r.Method == http.MethodGet: _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","config":{"source_connection":{"options":{"oauth_provider":"OAUTH_PROVIDER_GITHUB_MCP"}}}}`) case r.URL.Path == loginTestCredPath && r.Method == http.MethodGet: - state := s.states[min(s.reads, len(s.states)-1)] - s.reads++ + state := "" + if s.gate == nil || s.gate() { + state = s.states[min(s.reads, len(s.states)-1)] + s.reads++ + } if state == "" { http.Error(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`, http.StatusNotFound) return @@ -77,6 +82,24 @@ func loginTestSignInURL(w *databricks.WorkspaceClient) string { return SignInURL(w.Config.Host, loginTestService, "123") } +// syncBuffer is a bytes.Buffer that the sign-in writes and a fake server reads, from different goroutines. +type syncBuffer struct { + mu sync.Mutex + buf bytes.Buffer +} + +func (b *syncBuffer) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.Write(p) +} + +func (b *syncBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.String() +} + func noopOpen(context.Context, string) error { return nil } @@ -107,20 +130,26 @@ func TestSignInPollsUntilActive(t *testing.T) { s := &loginTestServer{states: []string{"", "PROVISIONING", "ACTIVE"}} w := loginTestClient(t, s) - var opened []string + // The browser is opened in the background, so the URL it is given arrives on a channel. + opened := make(chan string, 1) var out bytes.Buffer err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(_ context.Context, url string) error { - opened = append(opened, url) + opened <- url return nil }, }) require.NoError(t, err) wantURL := w.Config.Host + "/mcp-service-login?name=system.ai.github&o=123" - assert.Equal(t, []string{wantURL}, opened) + select { + case got := <-opened: + assert.Equal(t, wantURL, got) + case <-time.After(10 * time.Second): + t.Fatal("the browser was not opened") + } // The URL is printed before the browser is opened, and polling starts after that. assert.Contains(t, out.String(), "Open this URL to sign in to system.ai.github:\n"+wantURL+"\n") assert.Equal(t, 3, s.reads) @@ -405,12 +434,14 @@ func TestSignInDoesNotWaitForTheOpener(t *testing.T) { // TestSignInReportsOpenerFailure checks that an opener that fails is reported while the wait goes on. The URL is printed // before the page opens, so the user can still finish the sign-in by hand. func TestSignInReportsOpenerFailure(t *testing.T) { - s := &loginTestServer{states: []string{"", "ACTIVE"}} + var out syncBuffer + s := &loginTestServer{states: []string{"ACTIVE"}} + // The credential becomes ACTIVE only after the failure is printed, so the wait cannot end before the message. + s.gate = func() bool { return strings.Contains(out.String(), "Could not open a browser") } w := loginTestClient(t, s) - var out bytes.Buffer err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ - Poll: 50 * time.Millisecond, + Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { return errors.New("no browser here") From a31e92169d21442eace8892d290796dab1037425 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:23:51 +0000 Subject: [PATCH 47/87] Describe refused service names in PathSafeName's doc as the commands' rule The doc said such names are refused, while the library escapes the names it reads from state. The commands refuse them before any request, and the library escapes the others. Co-authored-by: Isaac --- libs/aitools/mcp/discovery.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index cbbf4bde1e6..e2a70897ade 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -20,7 +20,8 @@ import ( const mcpServicePrefix = "mcp-services/" // PathSafeName reports whether fullName is free of the characters that change a request path: "/", "?", "#", "%" and -// control characters. A service name never contains them, so a name that has one is refused, not escaped. +// control characters. A service name never contains them, so the commands refuse a name that has one, before any +// request. Names read from the state are escaped by the library instead. func PathSafeName(fullName string) bool { return !strings.ContainsAny(fullName, "/?#%") && !strings.ContainsFunc(fullName, unicode.IsControl) } From c159abb7481542f9d332a1bb654b13d25a10c843 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:23:52 +0000 Subject: [PATCH 48/87] Name the profile of a failed state read, and say when login finds the sign-in already active login and logout set the profile in the JSON document only after the state is read, so a failure to read it left the profile out. The profile is now set once it is resolved. A login when the credential is already ACTIVE printed 'Signed in' and the sign-in URL, although no page was opened. SignIn now reports that the caller was already signed in. The command says 'Already signed in' and leaves the URL out of the document, and add --login says the same. Co-authored-by: Isaac --- .../aitools/mcp/login-flips-active/output.txt | 5 ++- cmd/aitools/mcp_add.go | 2 +- cmd/aitools/mcp_login.go | 33 ++++++++++++------- cmd/aitools/mcp_login_test.go | 26 +++++++++++++++ libs/aitools/mcp/login.go | 24 +++++++------- libs/aitools/mcp/login_test.go | 26 ++++++++------- 6 files changed, 76 insertions(+), 40 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt index 16f4d556124..b78235de597 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -25,14 +25,13 @@ Public repositories only until a repository owner installs https://github.com/ap === login again when already signed in returns at once >>> [CLI] aitools mcp login system.ai.github --profile logfood --output json -Signed in to system.ai.github on profile logfood. +Already signed in to system.ai.github on profile logfood. Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. { "service": "system.ai.github", "profile": "logfood", "host": "[DATABRICKS_URL]", - "state": "ACTIVE", - "url": "[DATABRICKS_URL]/mcp-service-login?name=system.ai.github\u0026o=[WORKSPACE_ID]" + "state": "ACTIVE" } === logout removes the sign-in; a second logout finds nothing and still exits 0 diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 724741f6eb0..699351716c7 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -1036,7 +1036,7 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp continue } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) - if _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service); err != nil { + if _, _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service); err != nil { errs = append(errs, err) } } diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 77226c4731d..a266996f36b 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -121,12 +121,12 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri if err != nil { return err } + out.Profile = profileName // The binding is checked before the profile's host is contacted, as add and serve check it. state, err := mcp.LoadState(ctx) if err != nil { return err } - out.Profile = profileName entry, err := mcp.CheckBinding(ctx, state, service, profileName, configFile) if err != nil { return err @@ -171,9 +171,14 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie if err != nil { return describeRequestError(out.Profile, pinnedFile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } - out.URL = mcp.SignInURL(out.Host, svc.FullName, workspaceID) - state, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, out.URL, svc) + url := mcp.SignInURL(out.Host, svc.FullName, workspaceID) + state, alreadySignedIn, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, url, svc) out.State = state + // The URL is named only when a page was opened for the sign-in, or when the sign-in failed and the URL is the way to + // finish it. + if !alreadySignedIn { + out.URL = url + } return err } @@ -207,10 +212,10 @@ func mcpLoginHint(service, profileName, pinnedFile string) string { return mcp.ConfigFileCommand(pinnedFile, mcpLoginCommand+" "+shellquote.BashArg(service)+" --profile "+shellquote.BashArg(profileName)) } -// signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns -// the sign-in state name and the failure, which is categorized for JSON. `add --login` shares it. -func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service) (string, error) { - err := mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ +// signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns the sign-in state +// name, whether the caller was already signed in, and the failure, which is categorized for JSON. `add --login` shares it. +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service) (state string, alreadySignedIn bool, err error) { + alreadySignedIn, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ Profile: profileName, Poll: mcp.SignInPollInterval, Timeout: mcp.SignInWaitTimeout, @@ -218,20 +223,24 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient }) switch { case errors.Is(err, mcp.ErrSignInTimeout): - return signInStateTimeout, failure(categorySignInTimeout, err, + return signInStateTimeout, false, failure(categorySignInTimeout, err, "sign-in to %s did not complete in %g minutes. Finish at %s then retry: %s", svc.FullName, mcp.SignInWaitTimeout.Minutes(), url, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case errors.Is(err, mcp.ErrSignInFailed): - return signInStateFailed, failure(protos.AitoolsErrorCategoryUncategorized, err, + return signInStateFailed, false, failure(protos.AitoolsErrorCategoryUncategorized, err, "sign-in to %s failed on profile %s. Run: %s", svc.FullName, profileName, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case err != nil: - return "", describeRequestError(profileName, pinnedFile, err) + return "", false, describeRequestError(profileName, pinnedFile, err) } - fmt.Fprintf(errOut, "Signed in to %s on profile %s.\n", svc.FullName, profileName) + if alreadySignedIn { + fmt.Fprintf(errOut, "Already signed in to %s on profile %s.\n", svc.FullName, profileName) + } else { + fmt.Fprintf(errOut, "Signed in to %s on profile %s.\n", svc.FullName, profileName) + } if svc.Known != nil && svc.Known.Note != "" { fmt.Fprintln(errOut, svc.Known.Note) } - return signInStateActive, nil + return signInStateActive, alreadySignedIn, nil } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 4d36d83f41b..7fafa468c0c 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -7,12 +7,15 @@ import ( "io" "net/http" "net/http/httptest" + "os" + "path/filepath" "strings" "sync" "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/cmdio" "github.com/databricks/cli/libs/telemetry/protos" @@ -310,6 +313,29 @@ func TestMcpLoginJSONNamesProfileWhenBindingFails(t *testing.T) { assert.NotContains(t, stdout.String(), `"state"`) } +// TestMcpLoginJSONNamesProfileWhenStateCannotBeRead checks that a failure to read the state, which comes after the profile is +// resolved, still names the profile in the document. +func TestMcpLoginJSONNamesProfileWhenStateCannotBeRead(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)) + + parent := newMCPTestRoot() + var stdout bytes.Buffer + parent.SetOut(&stdout) + parent.SetErr(&bytes.Buffer{}) + parent.SetArgs([]string{"mcp", "login", "system.ai.github", "--profile", "logfood", "--output", "json"}) + err = parent.ExecuteContext(cmdio.MockDiscard(t.Context())) + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + + var out mcpSignInOutput + require.NoError(t, json.Unmarshal(stdout.Bytes(), &out), stdout.String()) + assert.Equal(t, "logfood", out.Profile) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, out.ErrorCategory) +} + // TestMcpSignInJSONWriteFailureKeepsRunError checks that a document that cannot be written does not hide why the command // failed, as add and remove keep it. func TestMcpSignInJSONWriteFailureKeepsRunError(t *testing.T) { diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 36b49b50a73..7a946346c51 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -52,23 +52,23 @@ func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service return cred.ProvisioningInfo.State, nil } -// SignIn signs the caller in to a sign-in-required service, at url from SignInURL. It returns nil at once when the -// caller is already signed in. Otherwise it deletes a FAILED credential left by an earlier attempt, prints url to out, -// opens it, and polls until the credential is ACTIVE. +// SignIn signs the caller in to a sign-in-required service, at url from SignInURL. When the caller is already signed in +// it returns alreadySignedIn true and nil, and prints nothing and opens no page. Otherwise it deletes a FAILED credential +// left by an earlier attempt, prints url to out, opens it, and polls until the credential is ACTIVE. // The errors are ErrSignInTimeout, ErrSignInFailed, or an API error. -func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service string, out io.Writer, opts SignInOptions) error { +func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service string, out io.Writer, opts SignInOptions) (alreadySignedIn bool, err error) { state, err := CredentialState(ctx, w, service) if err != nil { - return err + return false, err } if state == catalog.ProvisioningInfoStateActive { - return nil + return true, nil } if state == catalog.ProvisioningInfoStateFailed { // A FAILED credential is left by an earlier attempt. It is deleted, so that a FAILED state seen while waiting is // the result of this attempt, and so that the page starts from no credential. if _, err := SignOut(ctx, w, service); err != nil { - return err + return false, err } } @@ -97,7 +97,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str for { select { case <-waitCtx.Done(): - return waitEnded(ctx) + return false, waitEnded(ctx) case err := <-opened: opened = nil if err != nil { @@ -109,16 +109,16 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str state, err := CredentialState(waitCtx, w, service) if err != nil { if waitCtx.Err() != nil { - return waitEnded(ctx) + return false, waitEnded(ctx) } - return err + return false, err } // Other states (PROVISIONING, UPDATING, ...) mean the login has not completed yet. if state == catalog.ProvisioningInfoStateActive { - return nil + return false, nil } if state == catalog.ProvisioningInfoStateFailed { - return ErrSignInFailed + return false, ErrSignInFailed } } } diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index f5998639e68..3f60a565f73 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -133,7 +133,7 @@ func TestSignInPollsUntilActive(t *testing.T) { // The browser is opened in the background, so the URL it is given arrives on a channel. opened := make(chan string, 1) var out bytes.Buffer - err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(_ context.Context, url string) error { @@ -160,7 +160,7 @@ func TestSignInAlreadyActiveDoesNotOpenBrowser(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + alreadySignedIn, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: time.Second, Open: func(context.Context, string) error { @@ -169,6 +169,7 @@ func TestSignInAlreadyActiveDoesNotOpenBrowser(t *testing.T) { }, }) require.NoError(t, err) + assert.True(t, alreadySignedIn) assert.Empty(t, out.String()) } @@ -178,7 +179,7 @@ func TestSignInBrowserNoneOnlyPrintsURL(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -195,7 +196,7 @@ func TestSignInTimeout(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 30 * time.Millisecond, @@ -210,7 +211,7 @@ func TestSignInCancelledIsNotATimeout(t *testing.T) { // The caller cancels once the sign-in page is open, so the wait is what is cancelled. ctx, cancel := context.WithCancel(t.Context()) - err := SignIn(browserOpens(ctx), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, err := SignIn(browserOpens(ctx), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: time.Minute, Open: func(context.Context, string) error { @@ -226,7 +227,7 @@ func TestSignInFailed(t *testing.T) { s := &loginTestServer{states: []string{"", "FAILED"}} w := loginTestClient(t, s) - err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 10 * time.Second, @@ -267,7 +268,7 @@ func TestSignInDeletesFailedCredentialFromEarlierAttempt(t *testing.T) { w := loginTestClient(t, s) deletedBeforeOpen := make(chan bool, 1) - err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -286,7 +287,7 @@ func TestSignInFailedCredentialThatFailsAgainIsFailed(t *testing.T) { s := &loginTestServer{states: []string{"FAILED"}, signedIn: true} w := loginTestClient(t, s) - err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 2 * time.Second, Open: noopOpen, @@ -300,7 +301,7 @@ func TestSignInFailedAfterProgressIsFailed(t *testing.T) { s := &loginTestServer{states: []string{"FAILED", "PROVISIONING", "FAILED"}} w := loginTestClient(t, s) - err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: noopOpen, @@ -333,7 +334,7 @@ func TestSignInTimeoutCutsOffPendingRequest(t *testing.T) { require.NoError(t, err) start := time.Now() - err = SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, err = SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 200 * time.Millisecond, Open: noopOpen, @@ -414,7 +415,7 @@ func TestSignInDoesNotWaitForTheOpener(t *testing.T) { t.Cleanup(func() { close(release) }) done := make(chan error, 1) go func() { - done <- SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -422,6 +423,7 @@ func TestSignInDoesNotWaitForTheOpener(t *testing.T) { return nil }, }) + done <- err }() select { case err := <-done: @@ -440,7 +442,7 @@ func TestSignInReportsOpenerFailure(t *testing.T) { s.gate = func() bool { return strings.Contains(out.String(), "Could not open a browser") } w := loginTestClient(t, s) - err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { From 632f9cabda0f68d410245f26f264edd52352b25d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:24:34 +0000 Subject: [PATCH 49/87] Document the sign-in commands, switch on the sign-in state, and link the stream and rate limiter sources Doc comments for runMcpSignIn, mcpLogin and mcpLogout say what each step records. The state checks in SignIn are switches. mcpLoginCommand moves to the top of its file. The comments about pkg/browser's streams and the SDK's rate limiter link to the source they describe. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 13 ++++++++++--- libs/aitools/mcp/login.go | 20 +++++++++++--------- libs/browser/browser.go | 3 ++- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index a266996f36b..0115c5fbfa8 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -19,6 +19,9 @@ import ( "github.com/spf13/cobra" ) +// mcpLoginCommand is the command that signs a profile in to a service. The hints name it. +const mcpLoginCommand = "databricks aitools mcp login" + // Sign-in states of `mcp login` and `mcp logout`, reported in JSON. const ( signInStateActive = "ACTIVE" @@ -107,6 +110,9 @@ func isServiceName(service string) bool { return isFullName(strings.Split(service, ".")) && mcp.PathSafeName(service) } +// runMcpSignIn checks the service name, resolves the profile and checks its binding to the server, then runs action with +// the profile's client. out records the outcome: the profile is set once it is resolved, and the host once the client +// exists. Its errors are returned for the command to print. func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service string, action mcpSignInAction, out *mcpSignInOutput) error { if !isServiceName(service) { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q", service) @@ -154,6 +160,8 @@ func serviceFor(ctx context.Context, w *databricks.WorkspaceClient, profileName, return svc, nil } +// mcpLogin signs the profile in to the service, when the service needs sign-in. A service that does not need it is +// NOT_REQUIRED and nothing is done. The sign-in URL is the one for the workspace the profile is in now. func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput, pinnedFile string) error { svc, err := serviceFor(ctx, w, out.Profile, pinnedFile, out.Service) if err != nil { @@ -182,6 +190,8 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie return err } +// mcpLogout removes the profile's sign-in to the service. The state is DELETED, or NOT_SIGNED_IN when there was no +// sign-in, which is not an error. A service the profile cannot see is an error, since a misspelled name looks the same. func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, out *mcpSignInOutput, pinnedFile string) error { // The credential is deleted first, so that the credential of a service the profile can no longer see is removed too. // The service is looked up only when there is no credential, to tell a name that is not a service from a caller who @@ -203,9 +213,6 @@ func mcpLogout(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceCli return nil } -// mcpLoginCommand is the command that signs a profile in to a service. The hints name it. -const mcpLoginCommand = "databricks aitools mcp login" - // mcpLoginHint returns the command that signs profileName in to service. pinnedFile is the config file the server is // pinned to, which the command sets, or "" for the file the shell reads. func mcpLoginHint(service, profileName, pinnedFile string) string { diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 7a946346c51..e297caf6033 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -61,10 +61,10 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str if err != nil { return false, err } - if state == catalog.ProvisioningInfoStateActive { + switch state { + case catalog.ProvisioningInfoStateActive: return true, nil - } - if state == catalog.ProvisioningInfoStateFailed { + case catalog.ProvisioningInfoStateFailed: // A FAILED credential is left by an earlier attempt. It is deleted, so that a FAILED state seen while waiting is // the result of this attempt, and so that the page starts from no credential. if _, err := SignOut(ctx, w, service); err != nil { @@ -84,9 +84,11 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str } fmt.Fprintln(out, "Waiting for sign-in...") - // The timeout cancels the wait from a timer, not with a context deadline: the SDK rate limiter refuses a request - // whose wait would exceed a deadline, which would hide the timeout. A poll still in flight is cut off too, so the - // SDK's retries of a slow poll cannot hold the command past the timeout. + // The timeout cancels the wait from a timer, not with a context deadline: the SDK's rate limiter refuses a request + // whose wait would exceed a deadline, which would hide the timeout (see + // https://github.com/databricks/databricks-sdk-go/blob/v0.197.0/httpclient/api_client.go and + // https://github.com/golang/time/blob/v0.14.0/rate/rate.go). A poll still in flight is cut off too, so the SDK's + // retries of a slow poll cannot hold the command past the timeout. waitCtx, cancel := context.WithCancel(ctx) defer cancel() timer := time.AfterFunc(opts.Timeout, cancel) @@ -114,10 +116,10 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str return false, err } // Other states (PROVISIONING, UPDATING, ...) mean the login has not completed yet. - if state == catalog.ProvisioningInfoStateActive { + switch state { + case catalog.ProvisioningInfoStateActive: return false, nil - } - if state == catalog.ProvisioningInfoStateFailed { + case catalog.ProvisioningInfoStateFailed: return false, ErrSignInFailed } } diff --git a/libs/browser/browser.go b/libs/browser/browser.go index a38089c5175..adb7863e505 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -69,7 +69,8 @@ func openDefault(url string, stdoutToStderr bool) error { defaultOpenMu.Lock() defer defaultOpenMu.Unlock() // github.com/pkg/browser writes xdg-open's output to os.Stdout and its error output to os.Stderr, even when the - // open succeeds, producing spurious noise on Linux desktops. The error output is discarded. + // open succeeds, producing spurious noise on Linux desktops. The error output is discarded. The streams are the + // package variables Stdout and Stderr (https://github.com/pkg/browser/blob/5ac0b6a4141c/browser.go). originalStdout, originalStderr := browserpkg.Stdout, browserpkg.Stderr defer func() { browserpkg.Stdout = originalStdout From 11800deceedd54140c27e8234e52a658a17b1833 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:28:53 +0000 Subject: [PATCH 50/87] List the remaining sign-in states explicitly in the switches The exhaustive linter requires every ProvisioningInfoState value in a switch. The states the sign-in does not act on are listed with the wait. Co-authored-by: Isaac --- libs/aitools/mcp/login.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index e297caf6033..2edd2f6ff23 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -70,6 +70,9 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str if _, err := SignOut(ctx, w, service); err != nil { return false, err } + case catalog.ProvisioningInfoStateDegraded, catalog.ProvisioningInfoStateDeleting, + catalog.ProvisioningInfoStateProvisioning, catalog.ProvisioningInfoStateUpdating: + // Not a state this sign-in starts from; the wait below handles it. } fmt.Fprintf(out, "Sign in with the same account you use for Databricks profile %s.\n", opts.Profile) @@ -121,6 +124,9 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str return false, nil case catalog.ProvisioningInfoStateFailed: return false, ErrSignInFailed + case catalog.ProvisioningInfoStateDegraded, catalog.ProvisioningInfoStateDeleting, + catalog.ProvisioningInfoStateProvisioning, catalog.ProvisioningInfoStateUpdating: + // The login has not completed yet. } } } From 2cb84477d064cbb8bf298b49d2c8d77afe33d2ae Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:48:21 +0000 Subject: [PATCH 51/87] libs/browser: stub the default launcher in the tests The overlapping-opens test relied on an empty PATH to stop the default opener, which does not hold on Windows, where pkg/browser uses ShellExecute. The launcher is now a variable that the tests replace, so no browser opens on any OS. The stream restore in the test helper runs in a defer, and the test with a backgrounded child waits for that child. Co-authored-by: Isaac --- libs/browser/browser.go | 5 ++++- libs/browser/browser_test.go | 31 ++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/libs/browser/browser.go b/libs/browser/browser.go index adb7863e505..4e112abeb8f 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -65,6 +65,9 @@ func IsDisabled(ctx context.Context) bool { // Sign-in can start an open while another one runs. var defaultOpenMu sync.Mutex +// openDefaultURL is the launcher of the default opener. Tests replace it, so that no browser opens on any OS. +var openDefaultURL = browserpkg.OpenURL + func openDefault(url string, stdoutToStderr bool) error { defaultOpenMu.Lock() defer defaultOpenMu.Unlock() @@ -80,7 +83,7 @@ func openDefault(url string, stdoutToStderr bool) error { if stdoutToStderr { browserpkg.Stdout = os.Stderr } - return browserpkg.OpenURL(url) + return openDefaultURL(url) } func openWithCommand(ctx context.Context, browserCmd, url string, stdoutToStderr bool) error { diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index 20a8b9ee43b..85248e7192f 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -115,9 +115,9 @@ func runOpener(t *testing.T, open func(context.Context, string) error) (stdout, prevStdout, prevStderr := os.Stdout, os.Stderr os.Stdout, os.Stderr = stdoutFile, stderrFile + defer func() { os.Stdout, os.Stderr = prevStdout, prevStderr }() ctx := env.Set(t.Context(), "BROWSER", os.Args[0]) err = open(ctx, "https://example.com/login") - os.Stdout, os.Stderr = prevStdout, prevStderr gotStdout, readErr := os.ReadFile(stdoutFile.Name()) require.NoError(t, readErr) @@ -126,6 +126,26 @@ func runOpener(t *testing.T, open func(context.Context, string) error) (stdout, return string(gotStdout), string(gotStderr), err } +// replaceDefaultLauncher sets the launcher of the default opener to fn for the test. +func replaceDefaultLauncher(t *testing.T, fn func(url string) error) { + t.Helper() + prev := openDefaultURL + t.Cleanup(func() { openDefaultURL = prev }) + openDefaultURL = fn +} + +// waitForSleeper registers a cleanup that waits until the sleeper that a test's browser command started has ended, so +// that no child outlives the test. +func waitForSleeper(t *testing.T, sleepFile string) { + t.Helper() + t.Cleanup(func() { + assert.Eventually(t, func() bool { + _, err := os.Stat(sleepFile) + return err == nil + }, 5*openerSleepTime, 10*time.Millisecond, "the sleeper did not end") + }) +} + // TestOpenOutputToStderrSendsCommandStdoutToStderr checks that the output of a custom browser command goes to stderr, so // that it does not land before a JSON document on stdout. func TestOpenOutputToStderrSendsCommandStdoutToStderr(t *testing.T) { @@ -151,6 +171,7 @@ func TestOpenOutputToStderrReturnsWhenCommandBackgroundsChild(t *testing.T) { t.Setenv(openerChildEnv, "1") sleepFile := filepath.Join(t.TempDir(), "sleeper-done") t.Setenv(openerBackgroundEnv, sleepFile) + waitForSleeper(t, sleepFile) ctx := env.Set(t.Context(), "BROWSER", os.Args[0]) require.NoError(t, OpenOutputToStderr(ctx, "https://example.com/login")) @@ -159,12 +180,12 @@ func TestOpenOutputToStderrReturnsWhenCommandBackgroundsChild(t *testing.T) { } // TestOpenDefaultOverlappingOpensRestoreStreams checks that two default opens that run at once, as sign-in can start -// one while add --login starts another, leave the streams of pkg/browser as they were. The PATH is empty, so the -// opener cannot start a browser: the opens fail at once, after they have swapped the streams. The race detector reports -// the unsynchronized swaps when two opens overlap. +// one while add --login starts another, leave the streams of pkg/browser as they were. The launcher is a stub that +// starts nothing, so no browser opens on any OS. The race detector reports the unsynchronized swaps when two opens +// overlap. func TestOpenDefaultOverlappingOpensRestoreStreams(t *testing.T) { t.Setenv("BROWSER", "") - t.Setenv("PATH", t.TempDir()) + replaceDefaultLauncher(t, func(string) error { return nil }) stdout, stderr := browserpkg.Stdout, browserpkg.Stderr var opens sync.WaitGroup From dad74678c1d93cb0446c1a37b14f59760a79c651 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:48:59 +0000 Subject: [PATCH 52/87] libs/browser: do not wait for the browser the default opener starts The default opener sent the launcher's error output to io.Discard. exec then copies that output through a pipe, so Run waited for every process still holding the pipe, including the browser the launcher started. The opener held its mutex for that whole time, so a second open, such as the second sign-in of add --login, waited too and printed nothing. The error output now goes to os.DevNull, a file that exec passes through directly. The test stubs the launcher with a command that leaves a child running. Co-authored-by: Isaac --- libs/browser/browser.go | 17 +++++++++++++---- libs/browser/browser_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/libs/browser/browser.go b/libs/browser/browser.go index 4e112abeb8f..9c6683beb9d 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -10,7 +10,6 @@ package browser import ( "context" "fmt" - "io" "os" "sync" @@ -69,17 +68,27 @@ var defaultOpenMu sync.Mutex var openDefaultURL = browserpkg.OpenURL func openDefault(url string, stdoutToStderr bool) error { - defaultOpenMu.Lock() - defer defaultOpenMu.Unlock() // github.com/pkg/browser writes xdg-open's output to os.Stdout and its error output to os.Stderr, even when the // open succeeds, producing spurious noise on Linux desktops. The error output is discarded. The streams are the // package variables Stdout and Stderr (https://github.com/pkg/browser/blob/5ac0b6a4141c/browser.go). + // + // The streams must be files. exec passes a file to the command as it is, so the command's Run returns when the + // launcher exits. Any other writer is copied through a pipe, and Run waits for the pipe to close, which a browser + // started by the launcher keeps open. + devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) + if err != nil { + return err + } + defer devNull.Close() + + defaultOpenMu.Lock() + defer defaultOpenMu.Unlock() originalStdout, originalStderr := browserpkg.Stdout, browserpkg.Stderr defer func() { browserpkg.Stdout = originalStdout browserpkg.Stderr = originalStderr }() - browserpkg.Stderr = io.Discard + browserpkg.Stderr = devNull if stdoutToStderr { browserpkg.Stdout = os.Stderr } diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index 85248e7192f..72ef8b7a076 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -164,6 +164,30 @@ func TestOpenKeepsCommandStdoutOnStdout(t *testing.T) { assert.NotContains(t, stderr, "opener stdout") } +// TestOpenDoesNotWaitForLauncherChild checks that the default opener returns when its launcher returns, although the +// browser the launcher started is still running. The launcher is a stub that runs this test binary the way pkg/browser's +// runCmd runs xdg-open: with the package's Stdout and Stderr, and waiting only for the command. The sleeper is the +// browser, and it writes sleepFile when it ends. Neither open may wait for it. +func TestOpenDoesNotWaitForLauncherChild(t *testing.T) { + t.Setenv(openerChildEnv, "1") + sleepFile := filepath.Join(t.TempDir(), "sleeper-done") + t.Setenv(openerBackgroundEnv, sleepFile) + waitForSleeper(t, sleepFile) + replaceDefaultLauncher(t, func(url string) error { + cmd := exec.Command(os.Args[0], url) + cmd.Stdout = browserpkg.Stdout + cmd.Stderr = browserpkg.Stderr + return cmd.Run() + }) + ctx := env.Set(t.Context(), "BROWSER", "") + + for range 2 { + require.NoError(t, Open(ctx, "https://example.com/login")) + _, err := os.Stat(sleepFile) + assert.ErrorIs(t, err, fs.ErrNotExist, "the open waited for the browser that the launcher started") + } +} + // TestOpenOutputToStderrReturnsWhenCommandBackgroundsChild checks that a browser command that starts a child and returns // does not hold up the caller. The child outlives the command, as a launcher that backgrounds the browser does, and writes // its file only when it ends. The file must not exist when the call returns. From c6555256d897ef45c3de4ca3f24e1f5cb7ed46e4 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:49:26 +0000 Subject: [PATCH 53/87] libs/exec: test WithInheritOutputToStderr and correct its doc The test checks that the command's stdout reaches the process's stderr and stays off stdout. The doc said that a command holding its output does not hold up Wait. That is true, but for the reason that the output is not piped, so the doc now says that. Co-authored-by: Isaac --- libs/exec/exec.go | 4 ++-- libs/exec/exec_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/libs/exec/exec.go b/libs/exec/exec.go index e40d91ca7e9..afb163a3f3b 100644 --- a/libs/exec/exec.go +++ b/libs/exec/exec.go @@ -97,8 +97,8 @@ func (e *Executor) WithInheritOutput() { e.inheritOutput = true } -// WithInheritOutputToStderr is WithInheritOutput, except that the command's stdout goes to stderr. A command that holds -// its stdout open after it returns does not hold up Wait. +// WithInheritOutputToStderr is WithInheritOutput, except that the command's stdout goes to stderr. The output is not +// piped, so Wait does not wait for a child that the command leaves holding its output open. func (e *Executor) WithInheritOutputToStderr() { e.inheritOutput = true e.stdoutToStderr = true diff --git a/libs/exec/exec_test.go b/libs/exec/exec_test.go index ffcf7a0a023..9df22accae9 100644 --- a/libs/exec/exec_test.go +++ b/libs/exec/exec_test.go @@ -6,6 +6,7 @@ import ( "io" "os" osexec "os/exec" + "path/filepath" "runtime" "sync" "testing" @@ -41,6 +42,36 @@ func TestExecutorWithStderr(t *testing.T) { assert.Equal(t, "Hello\nError\n", string(out)) } +// TestExecutorWithInheritOutputToStderrSendsStdoutToStderr checks that the command's stdout goes to the process's stderr, +// as the browser command's does for JSON output, and that its stderr stays on stderr. +func TestExecutorWithInheritOutputToStderrSendsStdoutToStderr(t *testing.T) { + dir := t.TempDir() + stdoutFile, err := os.Create(filepath.Join(dir, "stdout")) + require.NoError(t, err) + defer stdoutFile.Close() + stderrFile, err := os.Create(filepath.Join(dir, "stderr")) + require.NoError(t, err) + defer stderrFile.Close() + prevStdout, prevStderr := os.Stdout, os.Stderr + os.Stdout, os.Stderr = stdoutFile, stderrFile + defer func() { os.Stdout, os.Stderr = prevStdout, prevStderr }() + + executor, err := NewCommandExecutor(".") + require.NoError(t, err) + executor.WithInheritOutputToStderr() + cmd, err := executor.StartCommand(t.Context(), "echo 'Hello' && >&2 echo 'Error'") + require.NoError(t, err) + require.NoError(t, cmd.Wait()) + + gotStdout, err := os.ReadFile(stdoutFile.Name()) + require.NoError(t, err) + gotStderr, err := os.ReadFile(stderrFile.Name()) + require.NoError(t, err) + assert.Empty(t, gotStdout) + assert.Contains(t, string(gotStderr), "Hello") + assert.Contains(t, string(gotStderr), "Error") +} + func TestExecutorWithInvalidCommand(t *testing.T) { executor, err := NewCommandExecutor(".") assert.NoError(t, err) From 19480b3b6505fbe269d069d6c210deadf5d81714 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:53:27 +0000 Subject: [PATCH 54/87] aitools mcp login: keep polling through server and network errors A poll of the credential that failed with a server error or a network failure ended the sign-in, while the user may still be on the provider's page. Those failures are now polled again until the wait ends. Any other failed poll, such as a refused credential, still ends it at once. The timeout is now SignInTimeoutError, which carries the failure of the last poll that failed, and the timeout message names it. A poll that reads the credential clears it. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 26 ++++++-- cmd/aitools/mcp_login_test.go | 45 +++++++++++++ libs/aitools/mcp/login.go | 53 ++++++++++++--- libs/aitools/mcp/login_test.go | 117 ++++++++++++++++++++++++++++++++- 4 files changed, 223 insertions(+), 18 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 0115c5fbfa8..83ff7ab109f 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -219,20 +219,36 @@ func mcpLoginHint(service, profileName, pinnedFile string) string { return mcp.ConfigFileCommand(pinnedFile, mcpLoginCommand+" "+shellquote.BashArg(service)+" --profile "+shellquote.BashArg(profileName)) } +// lastCheckText names the failure of the last poll of a sign-in that timed out, for the timeout message. It is empty +// when the last poll read the credential. +func lastCheckText(timeout *mcp.SignInTimeoutError) string { + if timeout.LastCheck == nil { + return "" + } + return " (the last check failed: " + timeout.LastCheck.Error() + ")" +} + +// signInPollInterval and signInWaitTimeout are the polling settings of a sign-in. Tests shorten them. +var ( + signInPollInterval = mcp.SignInPollInterval + signInWaitTimeout = mcp.SignInWaitTimeout +) + // signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns the sign-in state // name, whether the caller was already signed in, and the failure, which is categorized for JSON. `add --login` shares it. func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service) (state string, alreadySignedIn bool, err error) { alreadySignedIn, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ Profile: profileName, - Poll: mcp.SignInPollInterval, - Timeout: mcp.SignInWaitTimeout, + Poll: signInPollInterval, + Timeout: signInWaitTimeout, Open: browser.OpenOutputToStderr, }) + timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { - case errors.Is(err, mcp.ErrSignInTimeout): + case timedOut: return signInStateTimeout, false, failure(categorySignInTimeout, err, - "sign-in to %s did not complete in %g minutes. Finish at %s then retry: %s", - svc.FullName, mcp.SignInWaitTimeout.Minutes(), url, mcpLoginHint(svc.FullName, profileName, pinnedFile)) + "sign-in to %s did not complete in %g minutes%s. Finish at %s then retry: %s", + svc.FullName, signInWaitTimeout.Minutes(), lastCheckText(timeout), url, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case errors.Is(err, mcp.ErrSignInFailed): return signInStateFailed, false, failure(protos.AitoolsErrorCategoryUncategorized, err, "sign-in to %s failed on profile %s. Run: %s", diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 7fafa468c0c..247c829560f 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -3,6 +3,7 @@ package aitools import ( "bytes" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -12,6 +13,7 @@ import ( "strings" "sync" "testing" + "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" @@ -250,6 +252,49 @@ func signInInstalledOne(t *testing.T, status int, body string) (string, error) { return errOut.String() + stderr.String(), err } +// TestSignInTimeoutNamesTheLastFailedCheck checks that a sign-in that times out after failed polls names the last failure in +// its error, so that the user can see why the credential did not become ACTIVE, and that its state is TIMEOUT. +func TestSignInTimeoutNamesTheLastFailedCheck(t *testing.T) { + t.Setenv("BROWSER", "none") + prevPoll, prevWait := signInPollInterval, signInWaitTimeout + t.Cleanup(func() { signInPollInterval, signInWaitTimeout = prevPoll, prevWait }) + signInPollInterval, signInWaitTimeout = time.Millisecond, 100*time.Millisecond + + const githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + var mu sync.Mutex + gets := 0 + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Path != githubPath { + rw.WriteHeader(http.StatusNotFound) + return + } + mu.Lock() + gets++ + first := gets == 1 + mu.Unlock() + rw.Header().Set("Content-Type", "application/json") + if first { + rw.WriteHeader(http.StatusNotFound) + _, _ = rw.Write([]byte(`{"error_code":"NOT_FOUND","message":"Credential is not found"}`)) + return + } + rw.WriteHeader(http.StatusInternalServerError) + _, _ = rw.Write([]byte(`{"error_code":"INTERNAL_ERROR","message":"try later"}`)) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) + state, _, err := signIn(ctx, io.Discard, w, "logfood", "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", SignInRequired: true}) + require.Error(t, err) + assert.Equal(t, signInStateTimeout, state) + _, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) + assert.True(t, timedOut) + assert.Contains(t, err.Error(), "(the last check failed: ") + assert.Contains(t, err.Error(), "try later") +} + // TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand checks that a sign-in that failed does not print the // login hint again, since its error already names the command to run. func TestSignInInstalledHintIsNotRepeatedWhenTheErrorNamesTheCommand(t *testing.T) { diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 2edd2f6ff23..82a391b9ddc 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -5,10 +5,13 @@ import ( "errors" "fmt" "io" + "net/http" + "net/url" "time" "github.com/databricks/cli/libs/browser" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/catalog" ) @@ -18,8 +21,19 @@ const ( SignInWaitTimeout = 5 * time.Minute ) -// ErrSignInTimeout means the caller's credential did not become ACTIVE before the wait ended. -var ErrSignInTimeout = errors.New("sign-in did not complete before the timeout") +// SignInTimeoutError means the caller's credential did not become ACTIVE before the wait ended. LastCheck is the +// failure of the last poll, when that poll failed; a poll that reads the credential clears it. +type SignInTimeoutError struct { + LastCheck error +} + +func (e *SignInTimeoutError) Error() string { + return "sign-in did not complete before the timeout" +} + +func (e *SignInTimeoutError) Unwrap() error { + return e.LastCheck +} // ErrSignInFailed means the credential reached the FAILED provisioning state. var ErrSignInFailed = errors.New("sign-in failed") @@ -54,8 +68,9 @@ func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service // SignIn signs the caller in to a sign-in-required service, at url from SignInURL. When the caller is already signed in // it returns alreadySignedIn true and nil, and prints nothing and opens no page. Otherwise it deletes a FAILED credential -// left by an earlier attempt, prints url to out, opens it, and polls until the credential is ACTIVE. -// The errors are ErrSignInTimeout, ErrSignInFailed, or an API error. +// left by an earlier attempt, prints url to out, opens it, and polls until the credential is ACTIVE. A poll that fails +// with a server error or a network error is polled again until the wait ends; any other failed poll ends the sign-in. +// The errors are a *SignInTimeoutError, ErrSignInFailed, or an API error. func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service string, out io.Writer, opts SignInOptions) (alreadySignedIn bool, err error) { state, err := CredentialState(ctx, w, service) if err != nil { @@ -99,10 +114,12 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str ticker := time.NewTicker(opts.Poll) defer ticker.Stop() + // lastCheck is the failure of the last poll, if that poll failed. The timeout reports it. + var lastCheck error for { select { case <-waitCtx.Done(): - return false, waitEnded(ctx) + return false, waitEnded(ctx, lastCheck) case err := <-opened: opened = nil if err != nil { @@ -114,10 +131,15 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str state, err := CredentialState(waitCtx, w, service) if err != nil { if waitCtx.Err() != nil { - return false, waitEnded(ctx) + return false, waitEnded(ctx, lastCheck) } - return false, err + if !transientPollError(err) { + return false, err + } + lastCheck = err + continue } + lastCheck = nil // Other states (PROVISIONING, UPDATING, ...) mean the login has not completed yet. switch state { case catalog.ProvisioningInfoStateActive: @@ -131,12 +153,23 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str } } -// waitEnded returns why the sign-in wait ended: the caller's cancellation, or else the timeout. -func waitEnded(ctx context.Context) error { +// transientPollError reports whether a failed poll is worth polling again: a server error, or a network failure. Any +// other error, such as a refused credential, ends the sign-in. +func transientPollError(err error) bool { + if apiErr, ok := errors.AsType[*apierr.APIError](err); ok { + return apiErr.StatusCode >= http.StatusInternalServerError + } + _, ok := errors.AsType[*url.Error](err) + return ok +} + +// waitEnded returns why the sign-in wait ended: the caller's cancellation, or else the timeout, with the failure of the +// last poll. +func waitEnded(ctx context.Context, lastCheck error) error { if err := ctx.Err(); err != nil { return err } - return ErrSignInTimeout + return &SignInTimeoutError{LastCheck: lastCheck} } // SignOut deletes the caller's sign-in to a service. It reports false when the caller was not diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 3f60a565f73..afd9557fc78 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -41,6 +41,10 @@ type loginTestServer struct { signedIn bool // gate, when set, holds the credential at NOT_FOUND until it returns true. The script does not advance meanwhile. gate func() bool + // gets counts the credential GETs, and fail, when set, answers GET number get (from 1) with the status it returns, + // or 0 to answer from the script. + gets int + fail func(get int) int } func (s *loginTestServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { @@ -51,6 +55,13 @@ func (s *loginTestServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { case r.URL.Path == loginTestServicePath && r.Method == http.MethodGet: _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","config":{"source_connection":{"options":{"oauth_provider":"OAUTH_PROVIDER_GITHUB_MCP"}}}}`) case r.URL.Path == loginTestCredPath && r.Method == http.MethodGet: + s.gets++ + if s.fail != nil { + if status := s.fail(s.gets); status != 0 { + http.Error(rw, `{"error_code":"TEMPORARILY_UNAVAILABLE","message":"try again"}`, status) + return + } + } state := "" if s.gate == nil || s.gate() { state = s.states[min(s.reads, len(s.states)-1)] @@ -110,6 +121,12 @@ func browserOpens(ctx context.Context) context.Context { return env.Set(ctx, "BROWSER", "test-opener") } +func (s *loginTestServer) credentialGets() int { + s.mu.Lock() + defer s.mu.Unlock() + return s.gets +} + func (s *loginTestServer) isDeleted() bool { s.mu.Lock() defer s.mu.Unlock() @@ -201,7 +218,8 @@ func TestSignInTimeout(t *testing.T) { Poll: time.Millisecond, Timeout: 30 * time.Millisecond, }) - require.ErrorIs(t, err, ErrSignInTimeout) + timeout := requireSignInTimeout(t, err) + assert.Nil(t, timeout.LastCheck) assert.Contains(t, out.String(), "/mcp-service-login?name=system.ai.github&o=123") } @@ -220,7 +238,8 @@ func TestSignInCancelledIsNotATimeout(t *testing.T) { }, }) require.ErrorIs(t, err, context.Canceled) - assert.NotErrorIs(t, err, ErrSignInTimeout) + _, timedOut := errors.AsType[*SignInTimeoutError](err) + assert.False(t, timedOut) } func TestSignInFailed(t *testing.T) { @@ -235,6 +254,98 @@ func TestSignInFailed(t *testing.T) { require.ErrorIs(t, err, ErrSignInFailed) } +// requireSignInTimeout checks that err is the timeout of a sign-in, and returns it. +func requireSignInTimeout(t *testing.T, err error) *SignInTimeoutError { + t.Helper() + timeout, ok := errors.AsType[*SignInTimeoutError](err) + require.True(t, ok, "got %v", err) + return timeout +} + +// TestSignInTimeoutReportsLastFailedCheck checks that a timeout after failed polls reports the last of them, so that the +// user can see why the sign-in did not complete. +func TestSignInTimeoutReportsLastFailedCheck(t *testing.T) { + s := &loginTestServer{states: []string{""}, fail: func(get int) int { + if get > 1 { + return http.StatusInternalServerError + } + return 0 + }} + w := loginTestClient(t, s) + + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Open: noopOpen, + Poll: time.Millisecond, + Timeout: 50 * time.Millisecond, + }) + timeout := requireSignInTimeout(t, err) + apiErr, ok := errors.AsType[*apierr.APIError](timeout.LastCheck) + require.True(t, ok, "got %v", timeout.LastCheck) + assert.Equal(t, http.StatusInternalServerError, apiErr.StatusCode) +} + +// TestSignInTimeoutDoesNotReportAFailedCheckThatWasRecovered checks that a poll which reads the credential after a failed +// one clears the failure, so that a later timeout does not report a check that no longer applies. +func TestSignInTimeoutDoesNotReportAFailedCheckThatWasRecovered(t *testing.T) { + s := &loginTestServer{states: []string{"", "PROVISIONING"}, fail: func(get int) int { + if get == 2 { + return http.StatusInternalServerError + } + return 0 + }} + w := loginTestClient(t, s) + + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Open: noopOpen, + Poll: time.Millisecond, + Timeout: 50 * time.Millisecond, + }) + assert.Nil(t, requireSignInTimeout(t, err).LastCheck) +} + +// TestSignInKeepsPollingThroughServerErrors checks that a poll which fails with a server error does not end the sign-in: +// the user may still be on the provider's page, and the next poll reads the credential. +func TestSignInKeepsPollingThroughServerErrors(t *testing.T) { + s := &loginTestServer{states: []string{"", "ACTIVE"}, fail: func(get int) int { + if get == 2 || get == 3 { + return http.StatusInternalServerError + } + return 0 + }} + w := loginTestClient(t, s) + + alreadySignedIn, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Open: noopOpen, + Poll: time.Millisecond, + Timeout: 10 * time.Second, + }) + require.NoError(t, err) + assert.False(t, alreadySignedIn) + assert.Equal(t, 4, s.credentialGets(), "the read before the page, two failed polls, and the poll that is ACTIVE") +} + +// TestSignInAuthenticationErrorEndsAtOnce checks that a poll which is refused for the caller's credential ends the sign-in +// at once, with that error, rather than polling until the timeout. +func TestSignInAuthenticationErrorEndsAtOnce(t *testing.T) { + s := &loginTestServer{states: []string{"", "ACTIVE"}, fail: func(get int) int { + if get == 2 { + return http.StatusUnauthorized + } + return 0 + }} + w := loginTestClient(t, s) + + _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Open: noopOpen, + Poll: time.Millisecond, + Timeout: 10 * time.Second, + }) + apiErr, ok := errors.AsType[*apierr.APIError](err) + require.True(t, ok, "got %v", err) + assert.Equal(t, http.StatusUnauthorized, apiErr.StatusCode) + assert.Equal(t, 2, s.credentialGets(), "the sign-in ends at the first poll that is refused") +} + func TestCredentialStateNotSignedIn(t *testing.T) { s := &loginTestServer{states: []string{""}} w := loginTestClient(t, s) @@ -339,7 +450,7 @@ func TestSignInTimeoutCutsOffPendingRequest(t *testing.T) { Timeout: 200 * time.Millisecond, Open: noopOpen, }) - require.ErrorIs(t, err, ErrSignInTimeout) + requireSignInTimeout(t, err) assert.Less(t, time.Since(start), 5*time.Second) } From 9678a0c06ba5fd41e679a025abf61010f9ac3c7f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:56:33 +0000 Subject: [PATCH 55/87] aitools mcp login: name the sign-in URL only when the page was shown The JSON of login and of add --login named the URL whenever the sign-in was not already active, including when it failed before the page was printed, such as when the credential cannot be read. A URL that was never shown is not a way to finish the sign-in. SignIn now reports whether the URL was printed, and the URL is named only then. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 21 ++++++++--------- cmd/aitools/mcp_login_test.go | 43 ++++++++++++++++++++++++++++++++++ libs/aitools/mcp/login.go | 25 ++++++++++---------- libs/aitools/mcp/login_test.go | 32 ++++++++++++------------- 4 files changed, 82 insertions(+), 39 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 83ff7ab109f..f7d49e72262 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -180,11 +180,10 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie return describeRequestError(out.Profile, pinnedFile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } url := mcp.SignInURL(out.Host, svc.FullName, workspaceID) - state, alreadySignedIn, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, url, svc) + state, urlShown, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, url, svc) out.State = state - // The URL is named only when a page was opened for the sign-in, or when the sign-in failed and the URL is the way to - // finish it. - if !alreadySignedIn { + // The URL is named when it was printed. A sign-in that failed before its page is printed names none. + if urlShown { out.URL = url } return err @@ -235,9 +234,9 @@ var ( ) // signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns the sign-in state -// name, whether the caller was already signed in, and the failure, which is categorized for JSON. `add --login` shares it. -func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service) (state string, alreadySignedIn bool, err error) { - alreadySignedIn, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ +// name, whether url was printed, and the failure, which is categorized for JSON. `add --login` shares it. +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service) (state string, urlShown bool, err error) { + alreadySignedIn, urlShown, err := mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ Profile: profileName, Poll: signInPollInterval, Timeout: signInWaitTimeout, @@ -246,15 +245,15 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { case timedOut: - return signInStateTimeout, false, failure(categorySignInTimeout, err, + return signInStateTimeout, urlShown, failure(categorySignInTimeout, err, "sign-in to %s did not complete in %g minutes%s. Finish at %s then retry: %s", svc.FullName, signInWaitTimeout.Minutes(), lastCheckText(timeout), url, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case errors.Is(err, mcp.ErrSignInFailed): - return signInStateFailed, false, failure(protos.AitoolsErrorCategoryUncategorized, err, + return signInStateFailed, urlShown, failure(protos.AitoolsErrorCategoryUncategorized, err, "sign-in to %s failed on profile %s. Run: %s", svc.FullName, profileName, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case err != nil: - return "", false, describeRequestError(profileName, pinnedFile, err) + return "", urlShown, describeRequestError(profileName, pinnedFile, err) } if alreadySignedIn { @@ -265,5 +264,5 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient if svc.Known != nil && svc.Known.Note != "" { fmt.Fprintln(errOut, svc.Known.Note) } - return signInStateActive, alreadySignedIn, nil + return signInStateActive, urlShown, nil } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 247c829560f..060c0f0f3bf 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -335,6 +335,49 @@ func TestMcpLoginHintNamesPinnedConfigFile(t *testing.T) { mcpLoginHint("system.ai.github", "logfood", "/work/cfg/logfood.cfg")) } +// TestMcpLoginJSONURLIsNamedOnlyWhenThePageIsShown checks that the document names the sign-in URL when a page was shown, +// or when the sign-in failed and the URL is how to finish it. A failure before the page is shown names no URL. +func TestMcpLoginJSONURLIsNamedOnlyWhenThePageIsShown(t *testing.T) { + t.Setenv("BROWSER", "none") + prevPoll := signInPollInterval + t.Cleanup(func() { signInPollInterval = prevPoll }) + signInPollInterval = time.Millisecond + cases := []struct { + name string + credentialCode int + credentialBody string + wantURL bool + }{ + { + name: "the credential cannot be read before the page", + credentialCode: http.StatusForbidden, + credentialBody: `{"error_code":"PERMISSION_DENIED","message":"no access"}`, + }, + { + name: "the sign-in fails after the page", + credentialCode: http.StatusOK, + credentialBody: `{"provisioning_info":{"state":"FAILED"}}`, + wantURL: true, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + home := withHome(t) + server := listCheckServer(t, http.StatusOK, tc.credentialCode, tc.credentialBody) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\nworkspace_id = 123\n") + + out := mcpSignInOutput{Service: "system.ai.github"} + err := runMcpSignIn(t.Context(), io.Discard, "logfood", "system.ai.github", mcpLogin, &out) + require.Error(t, err) + if tc.wantURL { + assert.Equal(t, server.URL+"/mcp-service-login?name=system.ai.github&o=123", out.URL) + } else { + assert.Empty(t, out.URL) + } + }) + } +} + // TestMcpLoginJSONNamesProfileWhenBindingFails checks that a failure after the profile is resolved still names the profile // in the document, and that a state the command never reached is left out rather than sent empty. func TestMcpLoginJSONNamesProfileWhenBindingFails(t *testing.T) { diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 82a391b9ddc..78ae34eaa58 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -68,22 +68,23 @@ func CredentialState(ctx context.Context, w *databricks.WorkspaceClient, service // SignIn signs the caller in to a sign-in-required service, at url from SignInURL. When the caller is already signed in // it returns alreadySignedIn true and nil, and prints nothing and opens no page. Otherwise it deletes a FAILED credential -// left by an earlier attempt, prints url to out, opens it, and polls until the credential is ACTIVE. A poll that fails -// with a server error or a network error is polled again until the wait ends; any other failed poll ends the sign-in. -// The errors are a *SignInTimeoutError, ErrSignInFailed, or an API error. -func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service string, out io.Writer, opts SignInOptions) (alreadySignedIn bool, err error) { +// left by an earlier attempt, prints url to out, opens it, and polls until the credential is ACTIVE. urlShown reports +// whether url was printed, which is not so when a read or the delete of a FAILED credential fails first. A poll that +// fails with a server error or a network error is polled again until the wait ends; any other failed poll ends the +// sign-in. The errors are a *SignInTimeoutError, ErrSignInFailed, or an API error. +func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service string, out io.Writer, opts SignInOptions) (alreadySignedIn, urlShown bool, err error) { state, err := CredentialState(ctx, w, service) if err != nil { - return false, err + return false, false, err } switch state { case catalog.ProvisioningInfoStateActive: - return true, nil + return true, false, nil case catalog.ProvisioningInfoStateFailed: // A FAILED credential is left by an earlier attempt. It is deleted, so that a FAILED state seen while waiting is // the result of this attempt, and so that the page starts from no credential. if _, err := SignOut(ctx, w, service); err != nil { - return false, err + return false, false, err } case catalog.ProvisioningInfoStateDegraded, catalog.ProvisioningInfoStateDeleting, catalog.ProvisioningInfoStateProvisioning, catalog.ProvisioningInfoStateUpdating: @@ -119,7 +120,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str for { select { case <-waitCtx.Done(): - return false, waitEnded(ctx, lastCheck) + return false, true, waitEnded(ctx, lastCheck) case err := <-opened: opened = nil if err != nil { @@ -131,10 +132,10 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str state, err := CredentialState(waitCtx, w, service) if err != nil { if waitCtx.Err() != nil { - return false, waitEnded(ctx, lastCheck) + return false, true, waitEnded(ctx, lastCheck) } if !transientPollError(err) { - return false, err + return false, true, err } lastCheck = err continue @@ -143,9 +144,9 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str // Other states (PROVISIONING, UPDATING, ...) mean the login has not completed yet. switch state { case catalog.ProvisioningInfoStateActive: - return false, nil + return false, true, nil case catalog.ProvisioningInfoStateFailed: - return false, ErrSignInFailed + return false, true, ErrSignInFailed case catalog.ProvisioningInfoStateDegraded, catalog.ProvisioningInfoStateDeleting, catalog.ProvisioningInfoStateProvisioning, catalog.ProvisioningInfoStateUpdating: // The login has not completed yet. diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index afd9557fc78..950b0b13353 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -150,7 +150,7 @@ func TestSignInPollsUntilActive(t *testing.T) { // The browser is opened in the background, so the URL it is given arrives on a channel. opened := make(chan string, 1) var out bytes.Buffer - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(_ context.Context, url string) error { @@ -177,7 +177,7 @@ func TestSignInAlreadyActiveDoesNotOpenBrowser(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - alreadySignedIn, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + alreadySignedIn, _, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: time.Second, Open: func(context.Context, string) error { @@ -196,7 +196,7 @@ func TestSignInBrowserNoneOnlyPrintsURL(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - _, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, _, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -213,7 +213,7 @@ func TestSignInTimeout(t *testing.T) { w := loginTestClient(t, s) var out bytes.Buffer - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 30 * time.Millisecond, @@ -229,7 +229,7 @@ func TestSignInCancelledIsNotATimeout(t *testing.T) { // The caller cancels once the sign-in page is open, so the wait is what is cancelled. ctx, cancel := context.WithCancel(t.Context()) - _, err := SignIn(browserOpens(ctx), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(ctx), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: time.Minute, Open: func(context.Context, string) error { @@ -246,7 +246,7 @@ func TestSignInFailed(t *testing.T) { s := &loginTestServer{states: []string{"", "FAILED"}} w := loginTestClient(t, s) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 10 * time.Second, @@ -273,7 +273,7 @@ func TestSignInTimeoutReportsLastFailedCheck(t *testing.T) { }} w := loginTestClient(t, s) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 50 * time.Millisecond, @@ -295,7 +295,7 @@ func TestSignInTimeoutDoesNotReportAFailedCheckThatWasRecovered(t *testing.T) { }} w := loginTestClient(t, s) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 50 * time.Millisecond, @@ -314,7 +314,7 @@ func TestSignInKeepsPollingThroughServerErrors(t *testing.T) { }} w := loginTestClient(t, s) - alreadySignedIn, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + alreadySignedIn, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 10 * time.Second, @@ -335,7 +335,7 @@ func TestSignInAuthenticationErrorEndsAtOnce(t *testing.T) { }} w := loginTestClient(t, s) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Open: noopOpen, Poll: time.Millisecond, Timeout: 10 * time.Second, @@ -379,7 +379,7 @@ func TestSignInDeletesFailedCredentialFromEarlierAttempt(t *testing.T) { w := loginTestClient(t, s) deletedBeforeOpen := make(chan bool, 1) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -398,7 +398,7 @@ func TestSignInFailedCredentialThatFailsAgainIsFailed(t *testing.T) { s := &loginTestServer{states: []string{"FAILED"}, signedIn: true} w := loginTestClient(t, s) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 2 * time.Second, Open: noopOpen, @@ -412,7 +412,7 @@ func TestSignInFailedAfterProgressIsFailed(t *testing.T) { s := &loginTestServer{states: []string{"FAILED", "PROVISIONING", "FAILED"}} w := loginTestClient(t, s) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: noopOpen, @@ -445,7 +445,7 @@ func TestSignInTimeoutCutsOffPendingRequest(t *testing.T) { require.NoError(t, err) start := time.Now() - _, err = SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err = SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 200 * time.Millisecond, Open: noopOpen, @@ -526,7 +526,7 @@ func TestSignInDoesNotWaitForTheOpener(t *testing.T) { t.Cleanup(func() { close(release) }) done := make(chan error, 1) go func() { - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { @@ -553,7 +553,7 @@ func TestSignInReportsOpenerFailure(t *testing.T) { s.gate = func() bool { return strings.Contains(out.String(), "Could not open a browser") } w := loginTestClient(t, s) - _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ Poll: time.Millisecond, Timeout: 10 * time.Second, Open: func(context.Context, string) error { From c0738f051c3a84344c01cb51c2e57135428c005b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:56:34 +0000 Subject: [PATCH 56/87] aitools mcp list --check: say how to sign in when a sign-in is needed The text listing said 'sign-in needed' without the login command that add prints for the same state. The server line now has a line below it with the login command, pinned to the config file the server is pinned to, as the other hints are. Co-authored-by: Isaac --- .../cmd/aitools/mcp/list-check/output.txt | 2 ++ cmd/aitools/mcp_list.go | 8 ++++++-- cmd/aitools/mcp_list_test.go | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/list-check/output.txt b/acceptance/cmd/aitools/mcp/list-check/output.txt index 8e17648f078..08a788546ef 100644 --- a/acceptance/cmd/aitools/mcp/list-check/output.txt +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -18,6 +18,7 @@ system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.data Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) token ok genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code github-logfood system.ai.github Claude Code sign-in needed + to sign in: run `DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood` === Once the sign-in completes, GitHub is signed in @@ -92,3 +93,4 @@ Signed out of system.ai.github on profile logfood. Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) token ok genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code binary missing github-logfood system.ai.github Claude Code sign-in needed + to sign in: run `DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood` diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 664d9ec3a5b..e65a213a32b 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -648,12 +648,16 @@ func checkText(c *mcpListCheck) string { return strings.Join(parts, ", ") } -// 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. +// serverHint tells the user what to do about a server: how to sign in, when its sign-in is needed, or how to repair it +// when its 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.Check != nil && s.Check.SignIn == checkNeeded { + return fmt.Sprintf("to sign in: run `%s`", mcpLoginHint(s.Service, p.Profile, loginFile(ctx, p, s.entry.ConfigFile != ""))) + } if s.drift == nil { return "" } diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 257552438ae..ad74ca16af1 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -686,6 +686,25 @@ func TestRenderMcpListLoginHintNamesCheckedFile(t *testing.T) { } } +// TestRenderMcpListSignInNeededNamesTheLoginCommand checks that a server whose sign-in is needed says how to sign in: the +// login command, which add prints for the same state. +func TestRenderMcpListSignInNeededNamesTheLoginCommand(t *testing.T) { + home := withHome(t) + t.Setenv("DATABRICKS_CONFIG_FILE", "") + server := listCheckServer(t, http.StatusOK, http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + + s, _ := checkedListServer(t) + require.Equal(t, checkNeeded, s.Check.SignIn) + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + addChecks(t.Context(), &out) + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.Contains(t, stderr.String(), " to sign in: run `databricks aitools mcp login system.ai.github --profile logfood`") +} + // TestRenderMcpListSaysWhyCheckIsSkipped checks that a server whose profile drifted says in the text that its check was // skipped, and that a listing without --check does not say so. func TestRenderMcpListSaysWhyCheckIsSkipped(t *testing.T) { From a4f3e771bf8ec895be2878b37212e2d6b423eb62 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:03:47 +0000 Subject: [PATCH 57/87] aitools mcp list --check: bound each profile and mark only refused tokens failed Any error from minting a profile's token marked the token failed, so an unreachable host, such as an offline laptop, was told to log in again. A token is now failed only when the credential is refused, and unknown otherwise, with the warning kept. The checks of a profile also ran on the SDK's five-minute retry budget, one profile after another, so one host that did not answer held the listing for minutes. Each profile's checks now end after the same five-second bound that cmd/auth gives profile validation, and the SDK's request and retry budgets for the profile are bounded too, since resolving the profile does not take the check's context. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 6 ++++ cmd/aitools/mcp_list.go | 39 ++++++++++++++++++++++---- cmd/aitools/mcp_list_test.go | 54 ++++++++++++++++++++++++++++++++++++ libs/aitools/mcp/binding.go | 23 +++++++++++++-- 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 699351716c7..9cca36ef7fe 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -24,6 +24,7 @@ import ( "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" ) @@ -436,6 +437,11 @@ func profileClient(ctx context.Context, name, configFile, pinnedFile string) (*d if cfg.AccountID != "" && (cfg.WorkspaceID == "" || cfg.WorkspaceID == profile.WorkspaceIDNone) { return nil, failure(protos.AitoolsErrorCategoryUncategorized, "profile %q is account-level, with no workspace_id in %s; MCP servers need a profile for one workspace", name, filepath.ToSlash(configFile)) } + return boundClient(ctx, cfg, name, pinnedFile) +} + +// boundClient returns a workspace client for cfg, a bound config of the named profile, once cfg has a credential. +func boundClient(ctx context.Context, cfg *config.Config, name, pinnedFile string) (*databricks.WorkspaceClient, error) { w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) if err != nil { return nil, err diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index e65a213a32b..52d4a79ec44 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -11,6 +11,7 @@ import ( "path/filepath" "slices" "strings" + "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" @@ -406,6 +407,9 @@ func pinnedEnv(rec mcp.Entry) map[string]string { return map[string]string{mcpconfig.ConfigFileEnv: rec.ConfigFile} } +// profileCheckTimeout bounds the --check tests of one profile. Tests shorten it. +var profileCheckTimeout = 5 * time.Second + // addChecks runs the --check tests for each profile that did not drift: a token mint, the sign-in state of each // sign-in-required server, and whether the recorded binary exists. Each profile is checked with the config file of its // group. A check that cannot run is a warning, and its result is unknown. @@ -428,20 +432,21 @@ func addChecks(ctx context.Context, out *mcpListOutput) { } } -// addProfileChecks runs the checks of one profile, whose config file is in ctx. login is the config file that the -// profile's login hints name, or "" (see loginFile). A profile whose token cannot be minted has no sign-in checks: its -// warning says why. +// addProfileChecks runs the checks of one profile, whose config file is in ctx. The checks end after profileCheckTimeout. +// login is the config file that the profile's login hints name, or "" (see loginFile). A profile whose token cannot be +// checked has no sign-in checks: its warning says why. func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out *mcpListOutput) { - w, err := profileClient(ctx, p.Profile, p.configPath, login) + ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) + defer cancel() + w, err := profileCheckClient(ctx, p, login) if err == nil { // A personal access token is sent only with a request, so the token is checked with one. if _, err = w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { err = describeRequestError(p.Profile, login, err) } } - p.token = statusOK + p.token = tokenStatus(err) if err != nil { - p.token = statusFailed out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) } for j := range p.Servers { @@ -462,6 +467,28 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out } } +// profileCheckClient returns the client of a profile for its --check tests. Resolving the profile's config is bounded too, +// by profileCheckTimeout, so that a host that never answers ends the checks of the profile. +func profileCheckClient(ctx context.Context, p *mcpListProfile, login string) (*databricks.WorkspaceClient, error) { + cfg, err := mcp.BoundConfigWithin(p.Profile, p.configPath, profileCheckTimeout) + if err != nil { + return nil, err + } + return boundClient(ctx, cfg, p.Profile, login) +} + +// tokenStatus is the --check result of a profile's token. A credential that the workspace or the profile refuses is +// failed. Any other failure, such as a host that cannot be reached, says nothing about the credential, so it is unknown. +func tokenStatus(err error) string { + if err == nil { + return statusOK + } + if be, ok := errors.AsType[*mcp.BindError](err); ok && be.Category == mcp.BindAuthFailed { + return statusFailed + } + return statusUnknown +} + // signInStatus reads the caller's sign-in to a service, as the service requires it now. Only an ACTIVE credential // counts as signed in. A service that does not require sign-in is "", which is not a status. A service the profile can // no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error says why. A diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index ad74ca16af1..5045e2b35bb 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -11,6 +11,7 @@ import ( "path/filepath" "strings" "testing" + "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" @@ -1083,6 +1084,59 @@ func TestAddChecksWarnsWhenProfileCannotAuthenticate(t *testing.T) { assert.True(t, strings.HasPrefix(out.Warnings[0], "profile logfood: "), out.Warnings[0]) } +// shortProfileCheck makes each profile's --check tests give up after one second, for the test. +func shortProfileCheck(t *testing.T) { + t.Helper() + prev := profileCheckTimeout + t.Cleanup(func() { profileCheckTimeout = prev }) + profileCheckTimeout = time.Second +} + +// TestAddChecksUnreachableProfileIsUnknownNotFailed checks that a profile whose host cannot be reached, as on an offline +// laptop, is unknown and not failed: the failed token says the login is refused, which the check did not learn. +func TestAddChecksUnreachableProfileIsUnknownNotFailed(t *testing.T) { + home := withHome(t) + shortProfileCheck(t) + // A closed server refuses every connection. + closed := httptest.NewServer(http.NotFoundHandler()) + host := closed.URL + closed.Close() + writeHomeConfig(t, home, "[logfood]\nhost = "+host+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", host, "")) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Equal(t, statusUnknown, s.Check.Token) + assert.Equal(t, statusUnknown, s.Check.SignIn) + require.Len(t, out.Warnings, 1) + assert.True(t, strings.HasPrefix(out.Warnings[0], "profile logfood: "), out.Warnings[0]) +} + +// TestAddChecksBoundsAProfileThatNeverAnswers checks that a profile whose workspace never answers does not hold the +// listing: its checks end within the profile's bound, and the profile is unknown with a warning. +func TestAddChecksBoundsAProfileThatNeverAnswers(t *testing.T) { + home := withHome(t) + shortProfileCheck(t) + release := make(chan struct{}) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + select { + case <-release: + case <-r.Context().Done(): + } + })) + t.Cleanup(server.Close) + t.Cleanup(func() { close(release) }) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", server.URL, "")) + + start := time.Now() + s, out := checkedListServer(t) + assert.Less(t, time.Since(start), 30*time.Second, "the checks of the profile were not bounded") + require.NotNil(t, s.Check) + assert.Equal(t, statusUnknown, s.Check.Token) + require.NotEmpty(t, out.Warnings) +} + // TestAddChecksReadsEachProfileFromItsOwnConfigFile checks that two groups of one profile name, in two config files, are // checked with their own file's credentials, and that the config file environment is the caller's again afterwards. func TestAddChecksReadsEachProfileFromItsOwnConfigFile(t *testing.T) { diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 4243a2c3018..641b41ad870 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -8,6 +8,7 @@ import ( "net/http" "path/filepath" "strings" + "time" "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/databrickscfg" @@ -98,11 +99,29 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) // from the environment, and a token meant for another workspace must not reach this one. It does not authenticate; see // AuthenticateBound. func BoundConfig(profileName, configFile string) (*config.Config, error) { - cfg := &config.Config{ + return resolveBound(&config.Config{ Profile: profileName, ConfigFile: configFile, Loaders: []config.Loader{databrickscfg.ResolveNonAuthFromEnv, config.ConfigFile}, - } + }) +} + +// BoundConfigWithin is BoundConfig with the SDK's request and retry budgets set to budget, in whole seconds. Resolving the +// profile runs on a context of its own, so a context deadline does not reach it, and these budgets are what bound it (see +// profileMetadata.Load in cmd/auth). +func BoundConfigWithin(profileName, configFile string, budget time.Duration) (*config.Config, error) { + seconds := int(budget / time.Second) + return resolveBound(&config.Config{ + Profile: profileName, + ConfigFile: configFile, + Loaders: []config.Loader{databrickscfg.ResolveNonAuthFromEnv, config.ConfigFile}, + HTTPTimeoutSeconds: seconds, + RetryTimeoutSeconds: seconds, + }) +} + +// resolveBound resolves cfg, as BoundConfig does. +func resolveBound(cfg *config.Config) (*config.Config, error) { if err := cfg.EnsureResolved(); err != nil { // Configuration that the SDK rejects is not a credential failure, so a login would not fix it. return nil, uncategorizedError(err) From c96c7675b970f79803f6f4f013484071e68616dc Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:05:51 +0000 Subject: [PATCH 58/87] aitools mcp list --check: skip only the servers that cannot be checked A profile with a drifted server was not checked at all, so a server that still matched its profile, added after the host changed, was reported as not matching it. Each server is now skipped on its own status, and says why in the text and in its JSON check: drifted, when the profile no longer matches the server, or unknown, when the server could not be read. A profile with no server to check is not contacted. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 77 ++++++++++++++++++++++++++++-------- cmd/aitools/mcp_list_test.go | 49 +++++++++++++++++++++++ 2 files changed, 109 insertions(+), 17 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 52d4a79ec44..f90a0937fb9 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -77,8 +77,6 @@ type mcpListProfile struct { // token is the --check result of minting a token for the profile. The JSON carries it in each server's check, // so it is text only. token string - // checkSkipped is set by --check for a profile that drifted, which is not checked. The text says so. - checkSkipped bool } type mcpListServer struct { @@ -89,7 +87,8 @@ type mcpListServer struct { SignInRequired bool `json:"sign_in_required"` Status string `json:"status"` Agents []mcpListAgent `json:"agents"` - // Check is the --check result of the server. It is nil without --check, and for a profile that drifted. + // Check is the --check result of the server. It is nil without --check. A server that is not checked has only its + // Skipped reason set. Check *mcpListCheck `json:"check,omitempty"` // entry is the state record, and drift the drift of the server from its profile, when it has one. @@ -97,11 +96,20 @@ type mcpListServer struct { drift *mcp.Drift } -// mcpListCheck is the --check result of one server. SignIn is set only for sign-in-required servers. +// Reasons why the --check tests of a server are skipped, which are the values of mcpListCheck.Skipped. A drifted server +// does not match its profile. An unknown server could not be read, so there is nothing to say about it. +const ( + checkSkippedDrifted = "drifted" + checkSkippedUnknown = "unknown" +) + +// mcpListCheck is the --check result of one server. SignIn is set only for sign-in-required servers. Skipped is set +// instead of the results when the server is not checked, and says why. type mcpListCheck struct { - Token string `json:"token"` - SignIn string `json:"sign_in,omitempty"` - Binary string `json:"binary"` + Skipped string `json:"skipped,omitempty"` + Token string `json:"token,omitempty"` + SignIn string `json:"sign_in,omitempty"` + Binary string `json:"binary,omitempty"` } type mcpListAgent struct { @@ -410,14 +418,15 @@ func pinnedEnv(rec mcp.Entry) map[string]string { // profileCheckTimeout bounds the --check tests of one profile. Tests shorten it. var profileCheckTimeout = 5 * time.Second -// addChecks runs the --check tests for each profile that did not drift: a token mint, the sign-in state of each -// sign-in-required server, and whether the recorded binary exists. Each profile is checked with the config file of its -// group. A check that cannot run is a warning, and its result is unknown. +// addChecks runs the --check tests for each server that did not drift from its profile and can be read: a token mint, the +// sign-in state of each sign-in-required server, and whether the recorded binary exists. Each profile is checked with the +// config file of its group. A server that is not checked says why, and a profile with no server to check is not contacted. +// A check that cannot run is a warning, and its result is unknown. func addChecks(ctx context.Context, out *mcpListOutput) { for i := range out.Profiles { p := &out.Profiles[i] - if p.Status != statusOK { - p.checkSkipped = true + skipUncheckable(p) + if !slices.ContainsFunc(p.Servers, func(s mcpListServer) bool { return skipReason(s) == "" }) { continue } // The login hints name the file that login writes, which the shell's environment decides. It is derived before @@ -432,9 +441,9 @@ func addChecks(ctx context.Context, out *mcpListOutput) { } } -// addProfileChecks runs the checks of one profile, whose config file is in ctx. The checks end after profileCheckTimeout. -// login is the config file that the profile's login hints name, or "" (see loginFile). A profile whose token cannot be -// checked has no sign-in checks: its warning says why. +// addProfileChecks runs the checks of the servers of one profile that can be checked, whose config file is in ctx. The +// checks end after profileCheckTimeout. login is the config file that the profile's login hints name, or "" (see +// loginFile). A profile whose token cannot be checked has no sign-in checks: its warning says why. func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out *mcpListOutput) { ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) defer cancel() @@ -451,6 +460,9 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out } for j := range p.Servers { s := &p.Servers[j] + if skipReason(*s) != "" { + continue + } s.Check = &mcpListCheck{Token: p.token, Binary: binaryStatus(s.entry.Command, exec.LookPath)} if err != nil { // The sign-in is unknown when the profile cannot authenticate. The record says whether it was required. @@ -489,6 +501,28 @@ func tokenStatus(err error) string { return statusUnknown } +// skipReason returns why the --check tests do not run for a server, or "" when they do. A server that drifted from its +// profile is not checked, and neither is one whose entry could not be read. +func skipReason(s mcpListServer) string { + switch { + case s.drift != nil: + return checkSkippedDrifted + case s.Status == statusUnknown: + return checkSkippedUnknown + } + return "" +} + +// skipUncheckable records on each server of p that is not checked why it is not. The servers that are checked get +// their results in addProfileChecks. +func skipUncheckable(p *mcpListProfile) { + for j := range p.Servers { + if reason := skipReason(p.Servers[j]); reason != "" { + p.Servers[j].Check = &mcpListCheck{Skipped: reason} + } + } +} + // signInStatus reads the caller's sign-in to a service, as the service requires it now. Only an ACTIVE credential // counts as signed in. A service that does not require sign-in is "", which is not a status. A service the profile can // no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error says why. A @@ -604,8 +638,6 @@ func renderMcpList(ctx context.Context, out mcpListOutput, profileName string) { line := fmt.Sprintf(" %-24s %-30s %s", s.Name, s.Service, listAgentsText(s.Agents)) if text := checkText(s.Check); text != "" { line += " " + text - } else if p.checkSkipped { - line += " check skipped: the profile no longer matches this server" } cmdio.LogString(ctx, line) if hint := serverHint(ctx, p, s); hint != "" { @@ -660,6 +692,9 @@ func checkText(c *mcpListCheck) string { if c == nil { return "" } + if c.Skipped != "" { + return skippedText(c.Skipped) + } var parts []string switch c.SignIn { case statusOK: @@ -675,6 +710,14 @@ func checkText(c *mcpListCheck) string { return strings.Join(parts, ", ") } +// skippedText says why the --check tests of a server did not run. +func skippedText(reason string) string { + if reason == checkSkippedDrifted { + return "check skipped: the profile no longer matches this server" + } + return "check skipped: this server could not be checked" +} + // serverHint tells the user what to do about a server: how to sign in, when its sign-in is needed, or how to repair it // when its profile no longer serves it. The remove command names the server, so that the other servers of the profile // are kept. diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 5045e2b35bb..47c9528e0ad 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1137,6 +1137,55 @@ func TestAddChecksBoundsAProfileThatNeverAnswers(t *testing.T) { require.NotEmpty(t, out.Warnings) } +// TestAddChecksChecksAServerThatMatchesADriftedProfile checks that a server which still matches its profile is checked +// when another server of the same profile has drifted: the drift is the other server's, not the profile's. +func TestAddChecksChecksAServerThatMatchesADriftedProfile(t *testing.T) { + home := withHome(t) + server := listCheckServer(t, http.StatusOK, http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + // github-old was set up for another host. github-new was set up for the profile's host now. + saveState(t, listCheckGitHub(t, "github-old", "logfood", "https://adb-old.test", ""), listCheckGitHub(t, "github-new", "logfood", server.URL, "")) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + addChecks(t.Context(), &out) + require.Len(t, out.Profiles, 1) + servers := map[string]mcpListServer{} + for _, s := range out.Profiles[0].Servers { + servers[s.Name] = s + } + + require.NotNil(t, servers["github-old"].Check) + assert.Equal(t, checkSkippedDrifted, servers["github-old"].Check.Skipped) + assert.Empty(t, servers["github-old"].Check.Token) + require.NotNil(t, servers["github-new"].Check) + assert.Equal(t, statusOK, servers["github-new"].Check.Token) + assert.Equal(t, checkNeeded, servers["github-new"].Check.SignIn) + assert.Empty(t, out.Warnings) +} + +// TestAddChecksSkipsAServerThatCannotBeReadAndSaysWhy checks that a server whose config file cannot be read is skipped +// with its own reason, in the JSON check and in the text. It did not drift, so the text does not say the profile changed. +func TestAddChecksSkipsAServerThatCannotBeReadAndSaysWhy(t *testing.T) { + withHome(t) + // A directory is not a config file, so the profile cannot be read from it. + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", "https://adb-1.test", t.TempDir())) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + addChecks(t.Context(), &out) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, 1) + s := out.Profiles[0].Servers[0] + require.NotNil(t, s.Check) + assert.Equal(t, checkSkippedUnknown, s.Check.Skipped) + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + renderMcpList(ctx, out, "") + assert.Contains(t, stderr.String(), "check skipped: this server could not be checked") + assert.NotContains(t, stderr.String(), "no longer matches") +} + // TestAddChecksReadsEachProfileFromItsOwnConfigFile checks that two groups of one profile name, in two config files, are // checked with their own file's credentials, and that the config file environment is the caller's again afterwards. func TestAddChecksReadsEachProfileFromItsOwnConfigFile(t *testing.T) { From af2a85608303260bc79285c0e4c5988d217d2827 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:07:10 +0000 Subject: [PATCH 59/87] aitools mcp add --login: record each server's sign-in in the JSON document The document of add --login had no sign-in per server. The successful, already signed in and skipped sign-ins were not visible at all, and a failure appeared only in the top-level error, which joins the failures of every server. Each server now has a sign_in object with the state that login reports, and the error category and message when the sign-in failed. It is absent without --login, and for a server that reached no agent, where no sign-in was attempted. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 33 +++++++++++++++----- cmd/aitools/mcp_login_test.go | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 9cca36ef7fe..2b898df3a90 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -64,6 +64,8 @@ type addServerOutput struct { Service string `json:"service"` SignInRequired bool `json:"sign_in_required"` Agents []addAgentOutput `json:"agents"` + // SignIn is the sign-in of the server under --login, with the state that login reports. It is absent without --login. + SignIn *addSignInOutput `json:"sign_in,omitempty"` // notes explain the name and the entries that were replaced. The text output prints them; the JSON does not. notes []string @@ -71,6 +73,14 @@ type addServerOutput struct { note string } +// addSignInOutput is the sign-in of one server under --login: its state, as login reports it, and the failure when the +// sign-in failed. +type addSignInOutput struct { + State string `json:"state,omitempty"` + ErrorCategory protos.AitoolsErrorCategory `json:"error_category,omitempty"` + Error string `json:"error,omitempty"` +} + type addAgentOutput struct { Name string `json:"name"` Status string `json:"status"` @@ -1031,18 +1041,27 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { return failure(protos.AitoolsErrorCategoryUnknownAgent, "%s is not installed; install it first or leave it out of --agents", a.DisplayName) } -// signInInstalled signs in to each sign-in-required server that reached an agent, in order. A failure does not stop the -// others, and the failures are joined. The servers are installed either way. A failure names the command to run next -// when a sign-in can fix it, as the timeout and failed errors do. Other failures get no hint, since logging in again -// would not fix them. +// signInInstalled signs in to each sign-in-required server that reached an agent, in order, and records the sign-in of +// each server in servers. A failure does not stop the others, and the failures are joined. The servers are installed +// either way. A failure names the command to run next when a sign-in can fix it, as the timeout and failed errors do. +// Other failures get no hint, since logging in again would not fix them. func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, req addRequest, plans []addPlan, servers []addServerOutput) error { var errs []error - for i, server := range servers { - if !server.SignInRequired || !installedAnywhere(server) { + for i := range servers { + server := &servers[i] + if !server.SignInRequired { + server.SignIn = &addSignInOutput{State: signInStateNotRequired} + continue + } + if !installedAnywhere(*server) { continue } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) - if _, _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service); err != nil { + state, _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service) + server.SignIn = &addSignInOutput{State: state} + if err != nil { + server.SignIn.ErrorCategory = errorCategory(err) + server.SignIn.Error = err.Error() errs = append(errs, err) } } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 060c0f0f3bf..1707095ff11 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -216,6 +216,64 @@ func TestSignInInstalledSignsInToEveryServer(t *testing.T) { assert.NotContains(t, stderr.String(), "needs sign-in") } +// TestSignInInstalledRecordsTheSignInOfEachServer checks that each server of add --login records its sign-in, with the state +// that login reports: a server that does not need sign-in, one that is signed in, and one whose sign-in failed. A server +// that reached no agent gets no sign-in, since none was attempted. +func TestSignInInstalledRecordsTheSignInOfEachServer(t *testing.T) { + t.Setenv("BROWSER", "none") + prevPoll := signInPollInterval + t.Cleanup(func() { signInPollInterval = prevPoll }) + signInPollInterval = time.Millisecond + const ( + githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + ) + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, "/api/") { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + if r.URL.Path == githubPath { + _, _ = rw.Write([]byte(`{"provisioning_info":{"state":"FAILED"}}`)) + return + } + _, _ = rw.Write([]byte(`{"provisioning_info":{"state":"ACTIVE"}}`)) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + req := addRequest{profile: "logfood", workspaceID: "123", agents: []*agents.Agent{agents.ByName("claude-code")}} + plans := []addPlan{ + {service: mcp.Service{FullName: "system.ai.github", SignInRequired: true}}, + {service: mcp.Service{FullName: "system.ai.genie_one_mcp", SignInRequired: true}}, + {service: mcp.Service{FullName: "system.ai.plain"}}, + {service: mcp.Service{FullName: "system.ai.unused", SignInRequired: true}}, + } + installed := []addAgentOutput{{Name: "claude-code", Status: statusInstalled}} + servers := []addServerOutput{ + {Name: "github-logfood", Service: "system.ai.github", SignInRequired: true, Agents: installed}, + {Name: "genie-logfood", Service: "system.ai.genie_one_mcp", SignInRequired: true, Agents: installed}, + {Name: "plain-logfood", Service: "system.ai.plain", Agents: installed}, + {Name: "unused-logfood", Service: "system.ai.unused", SignInRequired: true, Agents: []addAgentOutput{{Name: "claude-code", Status: statusFailed}}}, + } + + ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) + var errOut bytes.Buffer + require.Error(t, signInInstalled(ctx, &errOut, w, req, plans, servers)) + + require.NotNil(t, servers[0].SignIn) + assert.Equal(t, signInStateFailed, servers[0].SignIn.State) + assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, servers[0].SignIn.ErrorCategory) + assert.Contains(t, servers[0].SignIn.Error, "sign-in to system.ai.github failed on profile logfood") + require.NotNil(t, servers[1].SignIn) + assert.Equal(t, signInStateActive, servers[1].SignIn.State) + assert.Empty(t, servers[1].SignIn.Error) + require.NotNil(t, servers[2].SignIn) + assert.Equal(t, signInStateNotRequired, servers[2].SignIn.State) + assert.Nil(t, servers[3].SignIn) +} + // signInInstalledOne signs in to one installed sign-in server of the test's workspace and returns the error and what the // command wrote to stderr, on either stream. The server answers with status and body for the credential. func signInInstalledOne(t *testing.T, status int, body string) (string, error) { From a2682e22439b7bde65b57189313266b37f2ce65b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:10:17 +0000 Subject: [PATCH 60/87] aitools mcp list: derive the profile token from the servers' checks The profile carried its own copy of the token check, which every server of the profile repeats in its check. The text now reads the token from the checked servers, and the profile keeps no copy. No output changes. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index f90a0937fb9..a65d4856deb 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -74,9 +74,6 @@ type mcpListProfile struct { configPath string // noHost is set when Status is profile_missing because the profile's section has no host. noHost bool - // token is the --check result of minting a token for the profile. The JSON carries it in each server's check, - // so it is text only. - token string } type mcpListServer struct { @@ -454,7 +451,7 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out err = describeRequestError(p.Profile, login, err) } } - p.token = tokenStatus(err) + token := tokenStatus(err) if err != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) } @@ -463,7 +460,7 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out if skipReason(*s) != "" { continue } - s.Check = &mcpListCheck{Token: p.token, Binary: binaryStatus(s.entry.Command, exec.LookPath)} + s.Check = &mcpListCheck{Token: token, Binary: binaryStatus(s.entry.Command, exec.LookPath)} if err != nil { // The sign-in is unknown when the profile cannot authenticate. The record says whether it was required. if s.SignInRequired { @@ -630,8 +627,8 @@ func renderMcpList(ctx context.Context, out mcpListOutput, profileName string) { cmdio.LogString(ctx, "") } header := profileHeader(p) - if p.token != "" { - header += " token " + p.token + if token := profileToken(p); token != "" { + header += " token " + token } cmdio.LogString(ctx, header) for _, s := range p.Servers { @@ -686,6 +683,17 @@ func profileHeader(p mcpListProfile) string { return fmt.Sprintf("Profile %s (%s)", p.Profile, p.Host) } +// profileToken is the --check result of the token of a profile, which each checked server of the profile carries. It is +// empty when no server of the profile was checked. +func profileToken(p mcpListProfile) string { + for _, s := range p.Servers { + if s.Check != nil && s.Check.Token != "" { + return s.Check.Token + } + } + return "" +} + // checkText is the trailing text of a server line for the --check results that need attention or // say something about the server. func checkText(c *mcpListCheck) string { From 267e56e442e694529c731678ef959a8afda54d5d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:10:19 +0000 Subject: [PATCH 61/87] acceptance: keep setup output in LOG files and edit the state with update_file.py The setup posts of the list-check and login-flips-active scripts sent their output to /dev/null, where a failure would not show. They write LOG files, as the other aitools scripts do. The list-check script edited the state with inline jq; it now uses update_file.py, with the binary path JSON-escaped so the replacement matches on Windows too. Both servers of that scenario record the binary, so both now report it missing. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/list-check/output.txt | 4 ++-- acceptance/cmd/aitools/mcp/list-check/script | 7 ++++--- acceptance/cmd/aitools/mcp/login-flips-active/script | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/list-check/output.txt b/acceptance/cmd/aitools/mcp/list-check/output.txt index 08a788546ef..1471761a4d5 100644 --- a/acceptance/cmd/aitools/mcp/list-check/output.txt +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -74,7 +74,7 @@ Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) "check": { "token": "ok", "sign_in": "ok", - "binary": "ok" + "binary": "failed" } } ] @@ -92,5 +92,5 @@ Signed out of system.ai.github on profile logfood. >>> [CLI] aitools mcp list --check Profile logfood ([DATABRICKS_URL], config file [TEST_TMP_DIR]/.databrickscfg) token ok genie_one_mcp-logfood system.ai.genie_one_mcp Claude Code binary missing - github-logfood system.ai.github Claude Code sign-in needed + github-logfood system.ai.github Claude Code sign-in needed, binary missing to sign in: run `DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood` diff --git a/acceptance/cmd/aitools/mcp/list-check/script b/acceptance/cmd/aitools/mcp/list-check/script index ce01abd27a8..ca96cde87df 100644 --- a/acceptance/cmd/aitools/mcp/list-check/script +++ b/acceptance/cmd/aitools/mcp/list-check/script @@ -5,8 +5,8 @@ envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg # The services of system.ai, created in the fake workspace. The fake serves what the scripts create, as in add-text. -MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' > /dev/null -MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' > /dev/null +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' &> LOG.create-github-logfood title "Install a service that needs no sign-in and one that does" trace $CLI aitools mcp add --profile logfood --names system.ai.genie_one_mcp,system.ai.github --agents claude-code @@ -19,7 +19,8 @@ MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.1/unity-catalog/mcp-services/syste trace $CLI aitools mcp list --check title "A recorded binary that is gone is reported as missing" -jq '(.mcp.servers[] | select(.service == "system.ai.genie_one_mcp") | .command) = "/nonexistent/databricks"' home/.databricks/aitools/skills/.state.json > state.tmp && mv state.tmp home/.databricks/aitools/skills/.state.json +# The state records the binary that add was started with, which is $CLI, as JSON writes it. Both servers record it. +update_file.py home/.databricks/aitools/skills/.state.json "$(python3 -I -c 'import json,sys; print(json.dumps(sys.argv[1])[1:-1])' "$CLI")" /nonexistent/databricks trace $CLI aitools mcp list --check --output json title "logout ends the sign-in; list --check shows it needed again" diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/script b/acceptance/cmd/aitools/mcp/login-flips-active/script index 207e0e18a70..30652b9abe4 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/script +++ b/acceptance/cmd/aitools/mcp/login-flips-active/script @@ -7,8 +7,8 @@ export DATABRICKS_CONFIG_FILE=.databrickscfg export BROWSER=none # The services of system.ai, created in the fake workspace. The fake serves what the scripts create, as in add-text. -MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' > /dev/null -MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' > /dev/null +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment": "Repositories, issues, pull requests", "config": {"source_connection": {"name": "connections/github", "options": {"oauth_provider": "OAUTH_PROVIDER_GITHUB_MCP"}}}}' &> LOG.create-github-logfood title "A service that needs no sign-in exits 0 and says so" trace $CLI aitools mcp login system.ai.genie_one_mcp --profile logfood From db94f4279071b3bc2e66b66ea135152ae492ac98 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:11:13 +0000 Subject: [PATCH 62/87] aitools, browser: table tests and doc comments The two login hint tests and the two browser stdout tests are table tests now. The doc of signInHint describes the sentence it returns, not a command. openDefault and openWithCommand have doc comments, and the long comments of the sign-in wait and of signInStatus are shorter. No behavior changes. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 3 ++- cmd/aitools/mcp_list.go | 9 ++++---- cmd/aitools/mcp_login_test.go | 39 ++++++++++++++++++++++++---------- libs/aitools/mcp/login.go | 8 +++---- libs/browser/browser.go | 4 ++++ libs/browser/browser_test.go | 40 +++++++++++++++++++++-------------- 6 files changed, 65 insertions(+), 38 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 2b898df3a90..e4037cb870c 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -1068,7 +1068,8 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp return errors.Join(errs...) } -// signInHint is the command that signs the profile in to a sign-in-required service. +// signInHint returns the sentence that says a sign-in-required service needs sign-in, with the command that signs the +// profile in. func signInHint(service, profileName, pinnedFile string) string { return fmt.Sprintf("%s needs sign-in. Run: %s", service, mcpLoginHint(service, profileName, pinnedFile)) } diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index a65d4856deb..acc95186f7e 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -520,11 +520,10 @@ func skipUncheckable(p *mcpListProfile) { } } -// signInStatus reads the caller's sign-in to a service, as the service requires it now. Only an ACTIVE credential -// counts as signed in. A service that does not require sign-in is "", which is not a status. A service the profile can -// no longer see is unknown, not a sign-in that is needed, and a read that fails is unknown too; the error says why. A -// service that cannot be read is unknown only when the record says the server needed sign-in (recorded); otherwise there -// is no sign-in to report, and the error is the warning. +// signInStatus reads the caller's sign-in to a service, as the service requires it now. Only an ACTIVE credential counts +// as signed in, and a service that does not require sign-in gives "", which is not a status. A failed read of the +// credential is unknown. A service the profile can no longer see is unknown only when the record says the server needed +// sign-in (recorded); otherwise there is no sign-in to report, and the error is the warning. func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string, recorded bool) (string, error) { svc, err := serviceFor(ctx, w, profileName, pinnedFile, service) if err != nil { diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 1707095ff11..7e676b8bc56 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -380,17 +380,34 @@ func TestSignInInstalledAuthFailureNamesTheAuthCommand(t *testing.T) { assert.NotContains(t, output, "needs sign-in") } -// TestMcpLoginHintQuotesItsArguments checks that the login command a hint prints can be pasted into a shell as written. -func TestMcpLoginHintQuotesItsArguments(t *testing.T) { - assert.Equal(t, "databricks aitools mcp login system.ai.github --profile logfood", mcpLoginHint("system.ai.github", "logfood", "")) - assert.Equal(t, "databricks aitools mcp login system.ai.github --profile 'my profile'", mcpLoginHint("system.ai.github", "my profile", "")) -} - -// TestMcpLoginHintNamesPinnedConfigFile checks that the hint for a server pinned to a config file sets DATABRICKS_CONFIG_FILE, -// so that the login reads the file the profile is in. -func TestMcpLoginHintNamesPinnedConfigFile(t *testing.T) { - assert.Equal(t, "DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks aitools mcp login system.ai.github --profile logfood", - mcpLoginHint("system.ai.github", "logfood", "/work/cfg/logfood.cfg")) +// TestMcpLoginHint checks the login command that a hint prints: it can be pasted into a shell as written, and a server +// pinned to a config file sets DATABRICKS_CONFIG_FILE, so that the login reads the file the profile is in. +func TestMcpLoginHint(t *testing.T) { + tests := []struct { + name, profile, pinnedFile, want string + }{ + { + name: "plain profile", + profile: "logfood", + want: "databricks aitools mcp login system.ai.github --profile logfood", + }, + { + name: "profile name that needs quoting", + profile: "my profile", + want: "databricks aitools mcp login system.ai.github --profile 'my profile'", + }, + { + name: "pinned to a config file", + profile: "logfood", + pinnedFile: "/work/cfg/logfood.cfg", + want: "DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks aitools mcp login system.ai.github --profile logfood", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, mcpLoginHint("system.ai.github", tt.profile, tt.pinnedFile)) + }) + } } // TestMcpLoginJSONURLIsNamedOnlyWhenThePageIsShown checks that the document names the sign-in URL when a page was shown, diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 78ae34eaa58..18e71d9c9bd 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -103,11 +103,9 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str } fmt.Fprintln(out, "Waiting for sign-in...") - // The timeout cancels the wait from a timer, not with a context deadline: the SDK's rate limiter refuses a request - // whose wait would exceed a deadline, which would hide the timeout (see - // https://github.com/databricks/databricks-sdk-go/blob/v0.197.0/httpclient/api_client.go and - // https://github.com/golang/time/blob/v0.14.0/rate/rate.go). A poll still in flight is cut off too, so the SDK's - // retries of a slow poll cannot hold the command past the timeout. + // The timeout cancels the wait from a timer, not a context deadline: the SDK's rate limiter refuses a request whose + // wait would exceed the deadline, which would hide the timeout (https://github.com/golang/time/blob/v0.14.0/rate/rate.go). + // A poll still in flight is cut off too. waitCtx, cancel := context.WithCancel(ctx) defer cancel() timer := time.AfterFunc(opts.Timeout, cancel) diff --git a/libs/browser/browser.go b/libs/browser/browser.go index 9c6683beb9d..ca6ecfd0f02 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -67,6 +67,8 @@ var defaultOpenMu sync.Mutex // openDefaultURL is the launcher of the default opener. Tests replace it, so that no browser opens on any OS. var openDefaultURL = browserpkg.OpenURL +// openDefault opens url with pkg/browser. stdoutToStderr sends the output of the launcher to stderr, as +// OpenOutputToStderr does. The launcher's error output is discarded. func openDefault(url string, stdoutToStderr bool) error { // github.com/pkg/browser writes xdg-open's output to os.Stdout and its error output to os.Stderr, even when the // open succeeds, producing spurious noise on Linux desktops. The error output is discarded. The streams are the @@ -95,6 +97,8 @@ func openDefault(url string, stdoutToStderr bool) error { return openDefaultURL(url) } +// openWithCommand runs browserCmd with url as its argument, through libs/exec, and waits for it to exit. Its output goes +// to stdout, or to stderr when stdoutToStderr is set. func openWithCommand(ctx context.Context, browserCmd, url string, stdoutToStderr bool) error { e, err := exec.NewCommandExecutor(".") if err != nil { diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index 72ef8b7a076..00844b36f52 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -146,22 +146,30 @@ func waitForSleeper(t *testing.T, sleepFile string) { }) } -// TestOpenOutputToStderrSendsCommandStdoutToStderr checks that the output of a custom browser command goes to stderr, so -// that it does not land before a JSON document on stdout. -func TestOpenOutputToStderrSendsCommandStdoutToStderr(t *testing.T) { - stdout, stderr, err := runOpener(t, OpenOutputToStderr) - require.NoError(t, err) - assert.Empty(t, stdout) - assert.Contains(t, stderr, "opener stdout") -} - -// TestOpenKeepsCommandStdoutOnStdout checks that Open, which other commands use, still sends the command's stdout to -// stdout. -func TestOpenKeepsCommandStdoutOnStdout(t *testing.T) { - stdout, stderr, err := runOpener(t, Open) - require.NoError(t, err) - assert.Contains(t, stdout, "opener stdout") - assert.NotContains(t, stderr, "opener stdout") +// TestOpenCommandOutput checks where the output of a custom browser command goes. OpenOutputToStderr sends it to stderr, +// so that it does not land before a JSON document on stdout. Open, which other commands use, keeps it on stdout. +func TestOpenCommandOutput(t *testing.T) { + tests := []struct { + name string + open func(context.Context, string) error + wantStdout bool + }{ + {name: "OpenOutputToStderr sends the output to stderr", open: OpenOutputToStderr, wantStdout: false}, + {name: "Open keeps the output on stdout", open: Open, wantStdout: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := runOpener(t, tt.open) + require.NoError(t, err) + if tt.wantStdout { + assert.Contains(t, stdout, "opener stdout") + assert.NotContains(t, stderr, "opener stdout") + return + } + assert.Empty(t, stdout) + assert.Contains(t, stderr, "opener stdout") + }) + } } // TestOpenDoesNotWaitForLauncherChild checks that the default opener returns when its launcher returns, although the From 040c1cb09ea1da87d59fef25bd1a882c4d4ebe0d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:15:27 +0000 Subject: [PATCH 63/87] libs/aitools/mcp: discard the timeout helper's result in a test The helper returns the timeout error for the test's later assertions. The one test that only needs the error to be a timeout discards it explicitly, which the errcheck linter accepts. Co-authored-by: Isaac --- libs/aitools/mcp/login_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 950b0b13353..d4c7d8115c9 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -219,7 +219,7 @@ func TestSignInTimeout(t *testing.T) { Timeout: 30 * time.Millisecond, }) timeout := requireSignInTimeout(t, err) - assert.Nil(t, timeout.LastCheck) + assert.NoError(t, timeout.LastCheck) assert.Contains(t, out.String(), "/mcp-service-login?name=system.ai.github&o=123") } @@ -300,7 +300,7 @@ func TestSignInTimeoutDoesNotReportAFailedCheckThatWasRecovered(t *testing.T) { Poll: time.Millisecond, Timeout: 50 * time.Millisecond, }) - assert.Nil(t, requireSignInTimeout(t, err).LastCheck) + assert.NoError(t, requireSignInTimeout(t, err).LastCheck) } // TestSignInKeepsPollingThroughServerErrors checks that a poll which fails with a server error does not end the sign-in: @@ -450,7 +450,7 @@ func TestSignInTimeoutCutsOffPendingRequest(t *testing.T) { Timeout: 200 * time.Millisecond, Open: noopOpen, }) - requireSignInTimeout(t, err) + _ = requireSignInTimeout(t, err) assert.Less(t, time.Since(start), 5*time.Second) } From 8ebeb6408e242b9e351e18f300fff8912a39d839 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:23:35 +0000 Subject: [PATCH 64/87] aitools mcp list --check: bound each request, not the whole profile The time limit covered every request of a profile together: its token check plus two requests per sign-in server. A profile with several sign-in servers on a slow but healthy workspace ran out of time and reported its later servers as unknown. Each request now gets the limit on its own, so a host that never answers still ends the profile's checks. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 41 ++++++++++++++++++++++++------------ cmd/aitools/mcp_list_test.go | 22 +++++++++++++++++++ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index acc95186f7e..31fce3b7637 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -412,7 +412,8 @@ func pinnedEnv(rec mcp.Entry) map[string]string { return map[string]string{mcpconfig.ConfigFileEnv: rec.ConfigFile} } -// profileCheckTimeout bounds the --check tests of one profile. Tests shorten it. +// profileCheckTimeout bounds each --check request of a profile, so that a host that never answers ends the profile's +// checks while a profile with many servers still gets every answer. Tests shorten it. var profileCheckTimeout = 5 * time.Second // addChecks runs the --check tests for each server that did not drift from its profile and can be read: a token mint, the @@ -438,19 +439,11 @@ func addChecks(ctx context.Context, out *mcpListOutput) { } } -// addProfileChecks runs the checks of the servers of one profile that can be checked, whose config file is in ctx. The -// checks end after profileCheckTimeout. login is the config file that the profile's login hints name, or "" (see +// addProfileChecks runs the checks of the servers of one profile that can be checked, whose config file is in ctx. Each +// request ends after profileCheckTimeout. login is the config file that the profile's login hints name, or "" (see // loginFile). A profile whose token cannot be checked has no sign-in checks: its warning says why. func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out *mcpListOutput) { - ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) - defer cancel() - w, err := profileCheckClient(ctx, p, login) - if err == nil { - // A personal access token is sent only with a request, so the token is checked with one. - if _, err = w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { - err = describeRequestError(p.Profile, login, err) - } - } + w, err := checkProfileToken(ctx, p, login) token := tokenStatus(err) if err != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) @@ -468,7 +461,7 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out } continue } - status, signInErr := signInStatus(ctx, w, p.Profile, login, s.Service, s.SignInRequired) + status, signInErr := checkServerSignIn(ctx, w, p.Profile, login, s) s.Check.SignIn = status if signInErr != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", s.Name, signInErr)) @@ -476,6 +469,28 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out } } +// checkProfileToken returns the client of a profile for its --check tests and checks its token. A personal access token is +// sent only with a request, so the token is checked with one, bounded by profileCheckTimeout. +func checkProfileToken(ctx context.Context, p *mcpListProfile, login string) (*databricks.WorkspaceClient, error) { + ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) + defer cancel() + w, err := profileCheckClient(ctx, p, login) + if err != nil { + return nil, err + } + if _, err := w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { + return nil, describeRequestError(p.Profile, login, err) + } + return w, nil +} + +// checkServerSignIn returns the sign-in status of one server, with its requests bounded by profileCheckTimeout. +func checkServerSignIn(ctx context.Context, w *databricks.WorkspaceClient, profileName, login string, s *mcpListServer) (string, error) { + ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) + defer cancel() + return signInStatus(ctx, w, profileName, login, s.Service, s.SignInRequired) +} + // profileCheckClient returns the client of a profile for its --check tests. Resolving the profile's config is bounded too, // by profileCheckTimeout, so that a host that never answers ends the checks of the profile. func profileCheckClient(ctx context.Context, p *mcpListProfile, login string) (*databricks.WorkspaceClient, error) { diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 47c9528e0ad..ebf7372e59b 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1137,6 +1137,28 @@ func TestAddChecksBoundsAProfileThatNeverAnswers(t *testing.T) { require.NotEmpty(t, out.Warnings) } +// TestAddChecksBoundsEachRequestNotTheProfile checks that the time limit applies to each --check request: a profile whose +// requests each answer within the limit gets every answer, even when together they take longer than the limit. +func TestAddChecksBoundsEachRequestNotTheProfile(t *testing.T) { + home := withHome(t) + shortProfileCheck(t) + signedIn := listCheckServer(t, http.StatusOK, http.StatusOK, `{"provisioning_info":{"state":"ACTIVE"}}`) + // Three requests of 400 ms each: the token, the service and the sign-in, 1.2 s together against a 1 s limit. + slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(400 * time.Millisecond) + signedIn.Config.Handler.ServeHTTP(w, r) + })) + t.Cleanup(slow.Close) + writeHomeConfig(t, home, "[logfood]\nhost = "+slow.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + saveState(t, listCheckGitHub(t, "github-logfood", "logfood", slow.URL, "")) + + s, out := checkedListServer(t) + require.NotNil(t, s.Check) + assert.Equal(t, statusOK, s.Check.Token) + assert.Equal(t, statusOK, s.Check.SignIn) + assert.Empty(t, out.Warnings) +} + // TestAddChecksChecksAServerThatMatchesADriftedProfile checks that a server which still matches its profile is checked // when another server of the same profile has drifted: the drift is the other server's, not the profile's. func TestAddChecksChecksAServerThatMatchesADriftedProfile(t *testing.T) { From 63db3a37737093c3d0426ff7ca11d59ab40c876a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:15:56 +0000 Subject: [PATCH 65/87] Adapt serve's tests to the rejected-token refresh rule A plain error is now a transient token failure, so the guard tests that expect the proxy to stop use a rejected token, and serveProxy takes the pinned config file in the tests added for the transient case. Co-authored-by: Isaac --- cmd/aitools/mcp_serve_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index eccccdc4984..a03c0359771 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -117,7 +117,7 @@ func TestRefreshGuardStopsLaterRequestsWithoutAskingAgain(t *testing.T) { // in its login command, so that the command logs in to the file the profile is read from. func TestRefreshGuardNamesPinnedConfigFile(t *testing.T) { g := &refreshGuard{ - authenticate: func(*http.Request) error { return errors.New(refreshTokenExpired) }, + authenticate: func(*http.Request) error { return refreshRejected }, profile: "logfood", pinnedFile: "/work/cfg/logfood.cfg", cancel: func() {}, @@ -284,7 +284,7 @@ func TestRefreshGuardIgnoresRequestThatEnded(t *testing.T) { func TestRefreshGuardIgnoresSessionDelete(t *testing.T) { cancelled := false g := &refreshGuard{ - authenticate: func(*http.Request) error { return errors.New(refreshTokenExpired) }, + authenticate: func(*http.Request) error { return refreshRejected }, profile: "logfood", cancel: func() { cancelled = true }, } @@ -516,7 +516,7 @@ func TestServeProxyTransientRefreshFailureKeepsServing(t *testing.T) { Config: &config.Config{Host: server.URL}, Authenticate: failsToolsListOnce(errors.New("dial tcp: i/o timeout")), Profile: "logfood", - }) + }, "") require.NoError(t, err) out := stdout.String() @@ -556,7 +556,7 @@ func TestServeProxyRefreshRejectionStopsAfterTransientFailure(t *testing.T) { Config: &config.Config{Host: server.URL}, Authenticate: rejected, Profile: "logfood", - }) + }, "") require.ErrorIs(t, err, root.ErrAlreadyPrinted) assert.Equal(t, 2, root.ExitStatus(err)) From d84867f95894064af1c437e0930d56d2da93925b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 14:50:51 +0000 Subject: [PATCH 66/87] Expect STATE_INVALID for an unreadable state file in mcp login Main classifies a StateError as STATE_INVALID, so a state file that does not parse reports that category in the JSON sign-in output rather than UNCATEGORIZED. Co-authored-by: Isaac --- cmd/aitools/mcp_login_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 7e676b8bc56..843d8e98678 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -496,7 +496,7 @@ func TestMcpLoginJSONNamesProfileWhenStateCannotBeRead(t *testing.T) { var out mcpSignInOutput require.NoError(t, json.Unmarshal(stdout.Bytes(), &out), stdout.String()) assert.Equal(t, "logfood", out.Profile) - assert.Equal(t, protos.AitoolsErrorCategoryUncategorized, out.ErrorCategory) + assert.Equal(t, protos.AitoolsErrorCategoryStateInvalid, out.ErrorCategory) } // TestMcpSignInJSONWriteFailureKeepsRunError checks that a document that cannot be written does not hide why the command From aa7370e6e8e5fd184afa24d7e26cc32e38c0f87c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 12:42:06 +0000 Subject: [PATCH 67/87] Regenerate the hidden help golden for pflag 1.1 boolean flags main bumped github.com/spf13/pflag to 1.1.0 (#7635), which prints boolean flags as --force[=true|false] in help output and widens the flag column. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/hidden/output.txt | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index b196d51baaa..7ee015cabad 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -50,17 +50,17 @@ Examples: databricks aitools mcp add --names github,web_search --agents claude-code,cursor --profile dogfood Flags: - --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) - --force Overwrite an existing server with the same name that this CLI did not write - -h, --help help for add - --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share) - --login Sign in to each sign-in-required server that was installed, after the installs - --name string Server name to use instead of the generated one; only with exactly one service - --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated - --no-login Do not sign in; print the login commands for sign-in-required servers (default) + --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) + --force[=true|false] Overwrite an existing server with the same name that this CLI did not write + -h, --help[=true|false] help for add + --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share) + --login[=true|false] Sign in to each sign-in-required server that was installed, after the installs + --name string Server name to use instead of the generated one; only with exactly one service + --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated + --no-login[=true|false] Do not sign in; print the login commands for sign-in-required servers (default) Global Flags: - --debug enable debug logging - -o, --output type output type: text or json (default text) - -p, --profile string ~/.databrickscfg profile - -t, --target string bundle target to use (if applicable) + --debug[=true|false] enable debug logging + -o, --output type output type: text or json (default text) + -p, --profile string ~/.databrickscfg profile + -t, --target string bundle target to use (if applicable) From 39b51ff4f988d55e2dabd42cf1e3e1cd6bb6248a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 13:36:50 +0000 Subject: [PATCH 68/87] List the existing profiles when login names a missing one add now lists the existing profiles when --profile names one that does not exist and no server is installed for it. login and logout check the same binding, so they go through the same helper, and the parity test keeps the two messages equal. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index f7d49e72262..163a3361674 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -135,7 +135,8 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri } entry, err := mcp.CheckBinding(ctx, state, service, profileName, configFile) if err != nil { - return err + // A mistyped --profile gets the existing profiles, as add lists them. + return withExistingProfiles(ctx, state, profileName, err) } // A server pinned to a config file names that file in the hints, as add does. pinnedFile := mcp.PinnedFile(entry, configFile) From 8e9fa58b3c035dd5dc3f2040afe9efdccf82b49e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 13:36:51 +0000 Subject: [PATCH 69/87] Regenerate the add and help goldens after the reworded add help The Cursor approval hint now names `agent` as the Cursor docs do, and the hidden test prints the group help, which lists this card's list, remove, login and logout commands. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/add-text/output.txt | 4 ++-- acceptance/cmd/aitools/mcp/hidden/output.txt | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt index f961048ccec..1f3e7dae581 100644 --- a/acceptance/cmd/aitools/mcp/add-text/output.txt +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -8,13 +8,13 @@ Agents (named with --agents): claude-code, cursor genie_one_mcp-logfood system.ai.genie_one_mcp claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json -Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enable genie_one_mcp-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. +Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enable genie_one_mcp-logfood (`agent` in the Cursor docs), or approve it in Cursor's MCP settings. github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood -Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (the Cursor docs call the CLI agent), or approve it in Cursor's MCP settings. +Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (`agent` in the Cursor docs), or approve it in Cursor's MCP settings. === Running it again re-asserts the entries, and nothing is written twice diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index 7ee015cabad..93f8a9a7951 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -16,6 +16,8 @@ Usage: Available Commands: add Install Databricks managed MCP servers into coding agents list List Databricks managed MCP servers installed by this CLI + login Sign in to a Databricks managed MCP service with your account + logout Remove your sign-in to a Databricks managed MCP service remove Remove Databricks managed MCP servers from coding agents Flags: From 7eeb9e70eebaa089add988d3fc85f496a412ed64 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 14:17:51 +0000 Subject: [PATCH 70/87] Regenerate the list and remove help golden for list --check list gains --check in this card, so its help in the help golden names the flag and says when the command uses the network. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/help/output.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/help/output.txt b/acceptance/cmd/aitools/mcp/help/output.txt index e6cbfd23068..783cfa1c5ed 100644 --- a/acceptance/cmd/aitools/mcp/help/output.txt +++ b/acceptance/cmd/aitools/mcp/help/output.txt @@ -19,8 +19,9 @@ Each agent of a server has one of these statuses. The JSON output uses the same --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. +Without --check this command makes no network calls. With --check, each profile's token is minted, the +sign-in state of each sign-in-required server is read, and the recorded binary is checked. It always exits 0 +when entries have drifted or cannot be checked; an entry or agent config that cannot be checked is a warning. Usage: databricks aitools mcp list [flags] @@ -31,7 +32,8 @@ Examples: databricks aitools mcp list --output json Flags: - -h, --help[=true|false] help for list + --check[=true|false] Also check tokens, sign-in state and the recorded binary (uses the network) + -h, --help[=true|false] help for list Global Flags: --debug[=true|false] enable debug logging From 3f1c897c3f2858c2f087be282afde04e82509009 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 14:27:09 +0000 Subject: [PATCH 71/87] Pass the pinned file to serveProxy in the redirect and debug-log tests serveProxy takes the pinned config file in this card. The two serve tests that card 06b added call it for an unpinned server, so they pass "". Co-authored-by: Isaac --- cmd/aitools/mcp_serve_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index a03c0359771..1e8e8648a26 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -624,7 +624,7 @@ func TestServeLogsEachUpstreamRequestAtDebug(t *testing.T) { }, Profile: "logfood", Transport: newUpstreamLog(http.DefaultTransport), - }) + }, "") require.NoError(t, err) lines := strings.Split(strings.TrimSpace(logs.String()), "\n") @@ -896,7 +896,7 @@ func TestServeRedirectFailsRequestWithLoginHintAndKeepsServing(t *testing.T) { Authenticate: func(*http.Request) error { return nil }, Profile: "logfood", Transport: &signInRedirect{next: http.DefaultTransport, profile: "logfood"}, - }) + }, "") require.NoError(t, err) out := stdout.String() From ab8898b0167037f6e7a93544c25f45c5c8091bb5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 13:17:17 +0000 Subject: [PATCH 72/87] aitools mcp login: plainer help, examples, and a hint for leaf names Reword the list --check and login help in plain words, add examples to login and logout, and suggest the full name when login or logout gets a leaf name such as github. The full name is still required. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 4 ++-- cmd/aitools/mcp_login.go | 20 ++++++++++++++++++-- cmd/aitools/mcp_login_test.go | 12 +++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 31fce3b7637..361087bb28f 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -156,8 +156,8 @@ Each agent of a server has one of these statuses. The JSON output uses the same --profile lists the servers of that profile only. Unmanaged entries have no profile, so they are not listed then. -Without --check this command makes no network calls. With --check, each profile's token is minted, the -sign-in state of each sign-in-required server is read, and the recorded binary is checked. It always exits 0 +Without --check this command makes no network calls. With --check, a token is requested for each profile, +the sign-in state of each sign-in-required server is read, and the recorded binary is checked. It always exits 0 when entries have drifted or cannot be checked; an entry or agent config that cannot be checked is a warning.`, Example: ` databricks aitools mcp list databricks aitools mcp list --profile logfood diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 163a3361674..532311d8514 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -57,7 +57,11 @@ func NewMcpLoginCmd() *cobra.Command { as GitHub. The sign-in page opens in the browser. The command waits for up to five minutes for the sign-in to complete. Without a browser, open the printed URL yourself. - is a full name such as system.ai.github. The profile is the one whose user signs in.`, + is a full name such as system.ai.github. Signs in as the user of --profile. + +Examples: + databricks aitools mcp login system.ai.github + databricks aitools mcp login system.ai.github --profile dev`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { return runMcpSignInCommand(cmd, args[0], mcpLogin) @@ -73,7 +77,11 @@ func NewMcpLogoutCmd() *cobra.Command { Long: `Remove your sign-in to a Databricks managed MCP service on a profile. The install of the server stays in place. Running it when you are not signed in is not an error. - is a full name such as system.ai.github.`, + is a full name such as system.ai.github. + +Examples: + databricks aitools mcp logout system.ai.github + databricks aitools mcp logout system.ai.github --profile dev`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { return runMcpSignInCommand(cmd, args[0], mcpLogout) @@ -110,11 +118,19 @@ func isServiceName(service string) bool { return isFullName(strings.Split(service, ".")) && mcp.PathSafeName(service) } +// isLeafName reports whether service is a leaf name such as github, which add and remove accept but sign-in does not. +func isLeafName(service string) bool { + return service != "" && !strings.Contains(service, ".") && mcp.PathSafeName(service) +} + // runMcpSignIn checks the service name, resolves the profile and checks its binding to the server, then runs action with // the profile's client. out records the outcome: the profile is set once it is resolved, and the host once the client // exists. Its errors are returned for the command to print. func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service string, action mcpSignInAction, out *mcpSignInOutput) error { if !isServiceName(service) { + if isLeafName(service) { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q; did you mean system.ai.%s?", service, service) + } return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q", service) } // The config file is resolved once, as add resolves it, so that the profile lookup and the client read the same file. diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 843d8e98678..f15649b0e3b 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -30,7 +30,7 @@ import ( func TestMcpSignInRequiresFullServiceName(t *testing.T) { // A name with a character that is URL syntax, or a control character, is refused before any request: the request // path would name another resource. - for _, service := range []string{"github", "system.github", "system..github", "a.b.c.d", "system.ai.git#hub", "system.ai.git?hub", "system.ai.git/hub", "system.ai.git%hub", "system.ai.git\nhub"} { + for _, service := range []string{"system.github", "system..github", "a.b.c.d", "git#hub", "system.ai.git#hub", "system.ai.git?hub", "system.ai.git/hub", "system.ai.git%hub", "system.ai.git\nhub", ""} { t.Run(service, func(t *testing.T) { var out mcpSignInOutput err := runMcpSignIn(cmdio.MockDiscard(t.Context()), &bytes.Buffer{}, "", service, mcpLogin, &out) @@ -41,6 +41,16 @@ func TestMcpSignInRequiresFullServiceName(t *testing.T) { } } +// TestMcpSignInLeafNameSuggestsFullName checks that a leaf name such as github is still refused before any request, and +// that the error names the full name it most likely meant. +func TestMcpSignInLeafNameSuggestsFullName(t *testing.T) { + var out mcpSignInOutput + err := runMcpSignIn(cmdio.MockDiscard(t.Context()), &bytes.Buffer{}, "", "github", mcpLogin, &out) + require.Error(t, err) + assert.Equal(t, `service must be a full name such as system.ai.github, got "github"; did you mean system.ai.github?`, err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) +} + // loginTestServer returns a server that answers every API call with the status and body given. The SDK still reads // host metadata when it configures a profile, so paths outside /api/ are not found. func loginTestServer(t *testing.T, status int, body string) *httptest.Server { From 7829aa4bbac3b09b741770614b598dd39b27579c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 13:21:21 +0000 Subject: [PATCH 73/87] aitools mcp list --check: run a profile's sign-in checks concurrently Check up to four servers of a profile at a time, and keep the order of the servers and of their warnings. Profiles stay sequential, since each one sets the config file environment that its checks read. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 27 ++++++-- cmd/aitools/mcp_list_test.go | 116 +++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 6 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 361087bb28f..2db330938fc 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -25,6 +25,7 @@ import ( "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" + "golang.org/x/sync/errgroup" ) // checkNeeded is the sign-in result of a server whose sign-in is not complete. The other results of a check are @@ -416,6 +417,10 @@ func pinnedEnv(rec mcp.Entry) map[string]string { // checks while a profile with many servers still gets every answer. Tests shorten it. var profileCheckTimeout = 5 * time.Second +// profileCheckConcurrency bounds how many sign-in checks of one profile run at once. Profiles are checked one after +// another, because each one sets the config file environment that its checks read. +const profileCheckConcurrency = 4 + // addChecks runs the --check tests for each server that did not drift from its profile and can be read: a token mint, the // sign-in state of each sign-in-required server, and whether the recorded binary exists. Each profile is checked with the // config file of its group. A server that is not checked says why, and a profile with no server to check is not contacted. @@ -439,15 +444,19 @@ func addChecks(ctx context.Context, out *mcpListOutput) { } } -// addProfileChecks runs the checks of the servers of one profile that can be checked, whose config file is in ctx. Each -// request ends after profileCheckTimeout. login is the config file that the profile's login hints name, or "" (see -// loginFile). A profile whose token cannot be checked has no sign-in checks: its warning says why. +// addProfileChecks runs the checks of the servers of one profile that can be checked, whose config file is in ctx. The +// sign-in checks of the servers run concurrently, and their warnings are added in the order of the servers. Each request +// ends after profileCheckTimeout. login is the config file that the profile's login hints name, or "" (see loginFile). +// A profile whose token cannot be checked has no sign-in checks: its warning says why. func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out *mcpListOutput) { w, err := checkProfileToken(ctx, p, login) token := tokenStatus(err) if err != nil { out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) } + signInErrs := make([]error, len(p.Servers)) + var g errgroup.Group + g.SetLimit(profileCheckConcurrency) for j := range p.Servers { s := &p.Servers[j] if skipReason(*s) != "" { @@ -461,10 +470,16 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out } continue } - status, signInErr := checkServerSignIn(ctx, w, p.Profile, login, s) - s.Check.SignIn = status + g.Go(func() error { + s.Check.SignIn, signInErrs[j] = checkServerSignIn(ctx, w, p.Profile, login, s) + return nil + }) + } + // Each check keeps its error in signInErrs, so the group never returns one. + _ = g.Wait() + for j, signInErr := range signInErrs { if signInErr != nil { - out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", s.Name, signInErr)) + out.Warnings = append(out.Warnings, fmt.Sprintf("MCP server %s: %s", p.Servers[j].Name, signInErr)) } } } diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index ebf7372e59b..3b36f852e04 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -10,6 +10,7 @@ import ( "os/exec" "path/filepath" "strings" + "sync/atomic" "testing" "time" @@ -1241,6 +1242,121 @@ func TestAddChecksReadsEachProfileFromItsOwnConfigFile(t *testing.T) { // TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt checks that the config file of a profile is set in the // context and in the process environment while a check runs, and that the caller's environment is restored after. +// multiServiceServer answers the token check, and for each service the service read and the credential read. Each +// service read calls beforeRead with the service name first, and each credential read is answered by credential. +func multiServiceServer(t *testing.T, beforeRead func(service string), credential func(service string) (int, string)) *httptest.Server { + t.Helper() + const prefix = "/api/2.1/unity-catalog/mcp-services/" + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.URL.Path == "/api/2.0/preview/scim/v2/Me" { + _, _ = w.Write([]byte(`{"userName":"user@example.test"}`)) + return + } + rest, ok := strings.CutPrefix(r.URL.Path, prefix) + if !ok { + w.WriteHeader(http.StatusNotFound) + return + } + if service, isCredential := strings.CutSuffix(rest, "/user-credentials"); isCredential { + status, body := credential(service) + w.WriteHeader(status) + _, _ = w.Write([]byte(body)) + return + } + beforeRead(rest) + _, _ = w.Write([]byte(`{"name":"mcp-services/` + rest + `","config":{"source_connection":{"options":{"oauth_provider":"OAUTH_PROVIDER_GITHUB_MCP"}}}}`)) + })) + t.Cleanup(server.Close) + return server +} + +// seedSignInServers records one sign-in-required server per service in a profile logfood on host. +func seedSignInServers(t *testing.T, host string, services ...string) { + t.Helper() + entries := make([]mcp.Entry, 0, len(services)) + for _, service := range services { + name := strings.TrimPrefix(service, "system.ai.") + "-logfood" + entry := seedInstallEntry(t, agents.ByName("claude-code"), seed{name, service, "logfood", host, testDatabricksCommand, ""}) + entry.SignInRequired = true + entries = append(entries, entry) + } + saveState(t, entries...) +} + +// TestAddChecksRunsServerChecksConcurrently checks that the servers of one profile are checked at the same time: each +// service read waits until all of them are in flight. Checked one after another, each read would wait out the wait. +func TestAddChecksRunsServerChecksConcurrently(t *testing.T) { + home := withHome(t) + services := []string{"system.ai.atlassian", "system.ai.github", "system.ai.slack", "system.ai.workiq"} + var inFlight, peak atomic.Int32 + server := multiServiceServer(t, func(string) { + n := inFlight.Add(1) + defer inFlight.Add(-1) + for { + p := peak.Load() + if n <= p || peak.CompareAndSwap(p, n) { + break + } + } + // The wait ends at once when every check is in flight, and after 3 s otherwise. + deadline := time.Now().Add(3 * time.Second) + for inFlight.Load() < int32(len(services)) && time.Now().Before(deadline) { + time.Sleep(time.Millisecond) + } + }, func(string) (int, string) { + return http.StatusOK, `{"provisioning_info":{"state":"ACTIVE"}}` + }) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + seedSignInServers(t, server.URL, services...) + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + addChecks(t.Context(), &out) + require.Len(t, out.Profiles, 1) + require.Len(t, out.Profiles[0].Servers, len(services)) + assert.Equal(t, int32(len(services)), peak.Load()) + for _, s := range out.Profiles[0].Servers { + require.NotNil(t, s.Check) + assert.Equal(t, statusOK, s.Check.SignIn, s.Name) + } + assert.Empty(t, out.Warnings) +} + +// TestAddChecksKeepsServerOrderWhenChecksFinishOutOfOrder checks that the servers are listed in the order of the state, +// and their warnings in that order too, when a check that started first finishes last. +func TestAddChecksKeepsServerOrderWhenChecksFinishOutOfOrder(t *testing.T) { + home := withHome(t) + server := multiServiceServer(t, func(service string) { + if service == "system.ai.atlassian" { + time.Sleep(300 * time.Millisecond) + } + }, func(service string) (int, string) { + if service == "system.ai.github" || service == "system.ai.workiq" { + return http.StatusUnauthorized, `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}` + } + return http.StatusOK, `{"provisioning_info":{"state":"ACTIVE"}}` + }) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + // The state lists the slow server first, so it is the first check to start and the last to finish. + seedSignInServers(t, server.URL, "system.ai.atlassian", "system.ai.github", "system.ai.slack", "system.ai.workiq") + + out, err := buildMcpList(t.Context(), "") + require.NoError(t, err) + addChecks(t.Context(), &out) + require.Len(t, out.Profiles, 1) + var names, signIns []string + for _, s := range out.Profiles[0].Servers { + names = append(names, s.Name) + signIns = append(signIns, s.Check.SignIn) + } + assert.Equal(t, []string{"atlassian-logfood", "github-logfood", "slack-logfood", "workiq-logfood"}, names) + assert.Equal(t, []string{statusOK, statusUnknown, statusOK, statusUnknown}, signIns) + require.Len(t, out.Warnings, 2) + assert.Contains(t, out.Warnings[0], "MCP server github-logfood:") + assert.Contains(t, out.Warnings[1], "MCP server workiq-logfood:") +} + func TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt(t *testing.T) { t.Setenv(mcpconfig.ConfigFileEnv, "ambient.cfg") var inContext, inProcess string From 6e87133bbd29fb337a6eca2389890d7c45917e11 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 14:54:09 +0000 Subject: [PATCH 74/87] Name every mcp command in the group help and point remove at logout The group help now says what list, remove, login and logout are for, and remove's help says sign-ins are kept and that logout removes one. The login and logout examples move to the Example field, as in add, list and remove. The help golden also picks up the list --check wording from the previous commit. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/help/output.txt | 7 ++++--- acceptance/cmd/aitools/mcp/hidden/output.txt | 4 +++- cmd/aitools/mcp.go | 4 +++- cmd/aitools/mcp_login.go | 16 ++++++---------- cmd/aitools/mcp_remove.go | 3 ++- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/help/output.txt b/acceptance/cmd/aitools/mcp/help/output.txt index 783cfa1c5ed..888f1a8129c 100644 --- a/acceptance/cmd/aitools/mcp/help/output.txt +++ b/acceptance/cmd/aitools/mcp/help/output.txt @@ -19,8 +19,8 @@ Each agent of a server has one of these statuses. The JSON output uses the same --profile lists the servers of that profile only. Unmanaged entries have no profile, so they are not listed then. -Without --check this command makes no network calls. With --check, each profile's token is minted, the -sign-in state of each sign-in-required server is read, and the recorded binary is checked. It always exits 0 +Without --check this command makes no network calls. With --check, a token is requested for each profile, +the sign-in state of each sign-in-required server is read, and the recorded binary is checked. It always exits 0 when entries have drifted or cannot be checked; an entry or agent config that cannot be checked is a warning. Usage: @@ -53,7 +53,8 @@ With --profile or --all and nothing installed to remove, the command succeeds an 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. +'aitools mcp list' shows them as unmanaged. Your sign-ins to services are kept; 'aitools mcp logout' +removes one. Usage: databricks aitools mcp remove [flags] diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index 93f8a9a7951..1e03965b87b 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -8,7 +8,9 @@ aitools help does not list mcp >>> [CLI] aitools mcp --help Databricks managed MCP servers give coding agents tools for Databricks services, such as GitHub or web search. -Use 'add' to install them into coding agents, bound to a Databricks profile. +Use 'add' to install them into coding agents, bound to a Databricks profile, 'list' to see what is installed, +and 'remove' to uninstall them. Some services use your account with an external provider, such as GitHub: +'login' signs you in to one and 'logout' removes the sign-in. Usage: databricks aitools mcp [command] diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index d64e4792d7c..e8a7802efec 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -37,7 +37,9 @@ func NewMcpCmd() *cobra.Command { Use: "mcp", Short: "Manage Databricks managed MCP servers for coding agents", Long: `Databricks managed MCP servers give coding agents tools for Databricks services, such as GitHub or web search. -Use 'add' to install them into coding agents, bound to a Databricks profile.`, +Use 'add' to install them into coding agents, bound to a Databricks profile, 'list' to see what is installed, +and 'remove' to uninstall them. Some services use your account with an external provider, such as GitHub: +'login' signs you in to one and 'logout' removes the sign-in.`, Hidden: true, } cmd.AddCommand(NewMcpAddCmd()) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 532311d8514..498efad8319 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -57,11 +57,9 @@ func NewMcpLoginCmd() *cobra.Command { as GitHub. The sign-in page opens in the browser. The command waits for up to five minutes for the sign-in to complete. Without a browser, open the printed URL yourself. - is a full name such as system.ai.github. Signs in as the user of --profile. - -Examples: - databricks aitools mcp login system.ai.github - databricks aitools mcp login system.ai.github --profile dev`, + is a full name such as system.ai.github. Signs in as the user of --profile.`, + Example: ` databricks aitools mcp login system.ai.github + databricks aitools mcp login system.ai.github --profile logfood`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { return runMcpSignInCommand(cmd, args[0], mcpLogin) @@ -77,11 +75,9 @@ func NewMcpLogoutCmd() *cobra.Command { Long: `Remove your sign-in to a Databricks managed MCP service on a profile. The install of the server stays in place. Running it when you are not signed in is not an error. - is a full name such as system.ai.github. - -Examples: - databricks aitools mcp logout system.ai.github - databricks aitools mcp logout system.ai.github --profile dev`, + is a full name such as system.ai.github.`, + Example: ` databricks aitools mcp logout system.ai.github + databricks aitools mcp logout system.ai.github --profile logfood`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { return runMcpSignInCommand(cmd, args[0], mcpLogout) diff --git a/cmd/aitools/mcp_remove.go b/cmd/aitools/mcp_remove.go index 7cc090a134c..5b5848336be 100644 --- a/cmd/aitools/mcp_remove.go +++ b/cmd/aitools/mcp_remove.go @@ -89,7 +89,8 @@ With --profile or --all and nothing installed to remove, the command succeeds an 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.`, +'aitools mcp list' shows them as unmanaged. Your sign-ins to services are kept; 'aitools mcp logout' +removes one.`, Example: ` databricks aitools mcp remove --names github-logfood databricks aitools mcp remove --profile logfood --agents cursor databricks aitools mcp remove --all`, From e60b9d03ef845edf4320275c732c502b188c4726 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 15:01:23 +0000 Subject: [PATCH 75/87] Regenerate the add golden for the single Cursor approval hint Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/add-text/output.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt index 1f3e7dae581..21f537cef5a 100644 --- a/acceptance/cmd/aitools/mcp/add-text/output.txt +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -8,13 +8,13 @@ Agents (named with --agents): claude-code, cursor genie_one_mcp-logfood system.ai.genie_one_mcp claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json -Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enable genie_one_mcp-logfood (`agent` in the Cursor docs), or approve it in Cursor's MCP settings. github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood -Cursor may ask you to approve github-logfood. Run: cursor-agent mcp enable github-logfood (`agent` in the Cursor docs), or approve it in Cursor's MCP settings. + +Cursor may ask you to approve genie_one_mcp-logfood, github-logfood. Run cursor-agent mcp enable for each (`agent` in the Cursor docs), or approve them in Cursor's MCP settings. === Running it again re-asserts the entries, and nothing is written twice From 608d7f702028d4195a133d87c6d8671e31f1809d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Mon, 16 Nov 2026 15:44:45 +0000 Subject: [PATCH 76/87] Call SignInRequired as a method in login, list --check and their tests Card 02 replaced the SignInRequired field of mcp.Service with a method derived from OAuthProvider. Test services that need sign-in now set an OAuthProvider. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 2 +- cmd/aitools/mcp_login.go | 2 +- cmd/aitools/mcp_login_test.go | 14 +++++++------- libs/aitools/mcp/discovery_test.go | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 2db330938fc..b535caa53ba 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -562,7 +562,7 @@ func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileNam } return statusUnknown, err } - if !svc.SignInRequired { + if !svc.SignInRequired() { return "", nil } state, err := mcp.CredentialState(ctx, w, service) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 498efad8319..b7ba36d5982 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -180,7 +180,7 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie if err != nil { return err } - if !svc.SignInRequired { + if !svc.SignInRequired() { out.State = signInStateNotRequired fmt.Fprintf(errOut, "%s does not need sign-in.\n", svc.FullName) return nil diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index f15649b0e3b..d711afdd756 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -208,8 +208,8 @@ func TestSignInInstalledSignsInToEveryServer(t *testing.T) { req := addRequest{profile: "logfood", workspaceID: "123", agents: []*agents.Agent{agents.ByName("claude-code")}} plans := []addPlan{ - {service: mcp.Service{FullName: "system.ai.github", SignInRequired: true}}, - {service: mcp.Service{FullName: "system.ai.genie_one_mcp", SignInRequired: true}}, + {service: mcp.Service{FullName: "system.ai.github", OAuthProvider: "OAUTH_PROVIDER_TEST"}}, + {service: mcp.Service{FullName: "system.ai.genie_one_mcp", OAuthProvider: "OAUTH_PROVIDER_TEST"}}, } installed := []addAgentOutput{{Name: "claude-code", Status: statusInstalled}} servers := []addServerOutput{ @@ -255,10 +255,10 @@ func TestSignInInstalledRecordsTheSignInOfEachServer(t *testing.T) { req := addRequest{profile: "logfood", workspaceID: "123", agents: []*agents.Agent{agents.ByName("claude-code")}} plans := []addPlan{ - {service: mcp.Service{FullName: "system.ai.github", SignInRequired: true}}, - {service: mcp.Service{FullName: "system.ai.genie_one_mcp", SignInRequired: true}}, + {service: mcp.Service{FullName: "system.ai.github", OAuthProvider: "OAUTH_PROVIDER_TEST"}}, + {service: mcp.Service{FullName: "system.ai.genie_one_mcp", OAuthProvider: "OAUTH_PROVIDER_TEST"}}, {service: mcp.Service{FullName: "system.ai.plain"}}, - {service: mcp.Service{FullName: "system.ai.unused", SignInRequired: true}}, + {service: mcp.Service{FullName: "system.ai.unused", OAuthProvider: "OAUTH_PROVIDER_TEST"}}, } installed := []addAgentOutput{{Name: "claude-code", Status: statusInstalled}} servers := []addServerOutput{ @@ -309,7 +309,7 @@ func signInInstalledOne(t *testing.T, status int, body string) (string, error) { require.NoError(t, err) req := addRequest{profile: "logfood", workspaceID: "123", agents: []*agents.Agent{agents.ByName("claude-code")}} - plans := []addPlan{{service: mcp.Service{FullName: "system.ai.github", SignInRequired: true}}} + plans := []addPlan{{service: mcp.Service{FullName: "system.ai.github", OAuthProvider: "OAUTH_PROVIDER_TEST"}}} servers := []addServerOutput{{ Name: "github-logfood", Service: "system.ai.github", SignInRequired: true, Agents: []addAgentOutput{{Name: "claude-code", Status: statusInstalled}}, @@ -354,7 +354,7 @@ func TestSignInTimeoutNamesTheLastFailedCheck(t *testing.T) { require.NoError(t, err) ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) - state, _, err := signIn(ctx, io.Discard, w, "logfood", "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", SignInRequired: true}) + state, _, err := signIn(ctx, io.Discard, w, "logfood", "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", OAuthProvider: "OAUTH_PROVIDER_TEST"}) require.Error(t, err) assert.Equal(t, signInStateTimeout, state) _, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) diff --git a/libs/aitools/mcp/discovery_test.go b/libs/aitools/mcp/discovery_test.go index ab43c0f4ce9..0e0e3f8c415 100644 --- a/libs/aitools/mcp/discovery_test.go +++ b/libs/aitools/mcp/discovery_test.go @@ -324,7 +324,7 @@ func TestGetServiceSignInRequired(t *testing.T) { svc, err := GetService(t.Context(), w, loginTestService) require.NoError(t, err) - assert.True(t, svc.SignInRequired) + assert.True(t, svc.SignInRequired()) assert.Equal(t, "OAUTH_PROVIDER_GITHUB_MCP", svc.OAuthProvider) require.NotNil(t, svc.Known) assert.Equal(t, "GitHub", svc.Known.Label) From 10344cbd5c7de968114f32acad0dbd7d0ffca07d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 09:48:05 +0000 Subject: [PATCH 77/87] Adapt login and add to the failure helper from card 04 Card 04 dropped the cause argument of failure. Login reports three failures whose message does not repeat the cause, so failureWithCause keeps the cause for errors.Is and errors.As without printing it. The account-level profileClient test passes the pinned file that this card adds. Co-authored-by: Isaac --- cmd/aitools/mcp.go | 16 ++++++++++++++++ cmd/aitools/mcp_add_test.go | 2 +- cmd/aitools/mcp_login.go | 10 +++++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index e8a7802efec..961969a0c07 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -66,6 +66,22 @@ func withAgentsHint(err error, agent string) error { return fmt.Errorf("%s is read-only; make it writable or leave %s out of --agents: %w", ro.Path, agent, ro.Err) } +// failureWithCause is failure for a message that does not repeat its cause. cause is kept for errors.Is and errors.As +// but is not printed. +func failureWithCause(category protos.AitoolsErrorCategory, cause error, format string, args ...any) error { + return withCategory(&causedError{message: fmt.Sprintf(format, args...), cause: cause}, category) +} + +// causedError is a message with the error that caused it. +type causedError struct { + message string + cause error +} + +func (e *causedError) Error() string { return e.message } + +func (e *causedError) Unwrap() error { return e.cause } + // errorCategory classifies an error by its typed errors only, never by its text. Failures without a // category of their own fall through to the install classification, as in the other aitools commands. func errorCategory(err error) protos.AitoolsErrorCategory { diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 88639a54625..dfe7ce2eb05 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1370,7 +1370,7 @@ func TestProfileClientRefusesAccountOnlyProfile(t *testing.T) { home := withHome(t) writeHomeConfig(t, home, "[acct]\n"+tt.config) - _, err := profileClient(t.Context(), "acct", filepath.Join(home, ".databrickscfg")) + _, err := profileClient(t.Context(), "acct", filepath.Join(home, ".databrickscfg"), "") if tt.refused { assert.ErrorContains(t, err, "profile \"acct\" is account-level") return diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index b7ba36d5982..f5dc20267f1 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -125,9 +125,9 @@ func isLeafName(service string) bool { func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service string, action mcpSignInAction, out *mcpSignInOutput) error { if !isServiceName(service) { if isLeafName(service) { - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q; did you mean system.ai.%s?", service, service) + return failure(protos.AitoolsErrorCategoryInvalidFlags, "service must be a full name such as system.ai.github, got %q; did you mean system.ai.%s?", service, service) } - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "service must be a full name such as system.ai.github, got %q", service) + return failure(protos.AitoolsErrorCategoryInvalidFlags, "service must be a full name such as system.ai.github, got %q", service) } // The config file is resolved once, as add resolves it, so that the profile lookup and the client read the same file. configFile, _, err := addConfigFile(ctx) @@ -165,7 +165,7 @@ func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service stri func serviceFor(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string) (mcp.Service, error) { svc, err := mcp.GetService(ctx, w, service) if mcp.IsNotFound(err) { - return mcp.Service{}, failure(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", service, profileName) + return mcp.Service{}, failureWithCause(categoryServiceNotFound, err, "%s is not an MCP service visible to profile %s", service, profileName) } if err != nil { return mcp.Service{}, describeRequestError(profileName, pinnedFile, err) @@ -258,11 +258,11 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { case timedOut: - return signInStateTimeout, urlShown, failure(categorySignInTimeout, err, + return signInStateTimeout, urlShown, failureWithCause(categorySignInTimeout, err, "sign-in to %s did not complete in %g minutes%s. Finish at %s then retry: %s", svc.FullName, signInWaitTimeout.Minutes(), lastCheckText(timeout), url, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case errors.Is(err, mcp.ErrSignInFailed): - return signInStateFailed, urlShown, failure(protos.AitoolsErrorCategoryUncategorized, err, + return signInStateFailed, urlShown, failureWithCause(protos.AitoolsErrorCategoryUncategorized, err, "sign-in to %s failed on profile %s. Run: %s", svc.FullName, profileName, mcpLoginHint(svc.FullName, profileName, pinnedFile)) case err != nil: From f5084ee1f7c728f46dbee83d3ef0ea8d8fc1e9ae Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 10:23:43 +0000 Subject: [PATCH 78/87] Handle the SignInURL error in login and add --login Card 02 makes SignInURL refuse an unknown workspace ID. login returns that error; add --login records it as the failed sign-in of that server, as it does for a sign-in that fails. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 7 +++++-- cmd/aitools/mcp_login.go | 5 ++++- libs/aitools/mcp/login_test.go | 6 +++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index e4037cb870c..5cd419b717a 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -1056,8 +1056,11 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp if !installedAnywhere(*server) { continue } - url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) - state, _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service) + state := signInStateFailed + url, err := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) + if err == nil { + state, _, err = signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service) + } server.SignIn = &addSignInOutput{State: state} if err != nil { server.SignIn.ErrorCategory = errorCategory(err) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index f5dc20267f1..e4a47911018 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -192,7 +192,10 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie if err != nil { return describeRequestError(out.Profile, pinnedFile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } - url := mcp.SignInURL(out.Host, svc.FullName, workspaceID) + url, err := mcp.SignInURL(out.Host, svc.FullName, workspaceID) + if err != nil { + return err + } state, urlShown, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, url, svc) out.State = state // The URL is named when it was printed. A sign-in that failed before its page is printed names none. diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index d4c7d8115c9..8bb799858e7 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -90,7 +90,11 @@ func (s *loginTestServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { // loginTestSignInURL is the sign-in URL that SignIn is given for the fake's workspace, with workspace ID 123. func loginTestSignInURL(w *databricks.WorkspaceClient) string { - return SignInURL(w.Config.Host, loginTestService, "123") + url, err := SignInURL(w.Config.Host, loginTestService, "123") + if err != nil { + panic(err) + } + return url } // syncBuffer is a bytes.Buffer that the sign-in writes and a fake server reads, from different goroutines. From 9337927e222396e10c98513703a8b047c83291c5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 11:04:10 +0000 Subject: [PATCH 79/87] Import shellquote for the stored-name check that card 04 added Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 5cd419b717a..8403b46a683 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -21,6 +21,7 @@ import ( "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" From 807cf33d42d6c5b046436e1ea3a750929c629922 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 14:36:28 +0000 Subject: [PATCH 80/87] Update the goldens for the card 04 review's sign-in indent and notes The sign-in hint of add is indented, the service's prerequisite note is printed after success, and the --location help names the case with no leaf names. The goldens for these outputs come from the review, so they are regenerated here. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/add-text/output.txt | 6 ++++-- acceptance/cmd/aitools/mcp/hidden/output.txt | 4 ++-- acceptance/cmd/aitools/mcp/list-and-remove/output.txt | 3 ++- acceptance/cmd/aitools/mcp/list-check/output.txt | 3 ++- acceptance/cmd/aitools/mcp/login-flips-active/output.txt | 2 ++ 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt index 21f537cef5a..cd10e23bbc6 100644 --- a/acceptance/cmd/aitools/mcp/add-text/output.txt +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -12,7 +12,8 @@ genie_one_mcp-logfood system.ai.genie_one_mcp github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json -system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. Cursor may ask you to approve genie_one_mcp-logfood, github-logfood. Run cursor-agent mcp enable for each (`agent` in the Cursor docs), or approve them in Cursor's MCP settings. @@ -29,7 +30,8 @@ genie_one_mcp-logfood system.ai.genie_one_mcp github-logfood system.ai.github claude-code already installed, re-asserted [TEST_TMP_DIR]/home/.claude.json cursor already installed, re-asserted [TEST_TMP_DIR]/home/.cursor/mcp.json -system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. === Cursor config diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index 1e03965b87b..add4178e1bb 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -38,7 +38,7 @@ Install Databricks managed MCP servers into coding agents, bound to a Databricks Each server runs through 'databricks aitools mcp serve' with that profile's credentials. Services come from --names: full names such as system.ai.github, or leaf names such as github. Leaf names -resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share. +resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share when there are no leaf names. Agents come from --agents. Without it, every detected MCP-capable agent is used. Interactive selection is not available yet: in a terminal, pass both --names and --agents. @@ -57,7 +57,7 @@ Flags: --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) --force[=true|false] Overwrite an existing server with the same name that this CLI did not write -h, --help[=true|false] help for add - --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share) + --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share when there are no leaf names) --login[=true|false] Sign in to each sign-in-required server that was installed, after the installs --name string Server name to use instead of the generated one; only with exactly one service --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated diff --git a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt index 22e41ca38be..34bec4dfe6a 100644 --- a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt +++ b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt @@ -10,7 +10,8 @@ genie_one_mcp-logfood system.ai.genie_one_mcp github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json -system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. >>> [CLI] aitools mcp add --profile dogfood --names system.ai.genie_one_mcp --agents claude-code Profile dogfood ([DATABRICKS_URL]) diff --git a/acceptance/cmd/aitools/mcp/list-check/output.txt b/acceptance/cmd/aitools/mcp/list-check/output.txt index 1471761a4d5..dcf54d0c7bd 100644 --- a/acceptance/cmd/aitools/mcp/list-check/output.txt +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -10,7 +10,8 @@ genie_one_mcp-logfood system.ai.genie_one_mcp github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json -system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. === list --check: the token mints, and GitHub needs sign-in diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt index b78235de597..d332fd71ae1 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -56,6 +56,7 @@ Agents (named with --agents): claude-code github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. Sign in with the same account you use for Databricks profile logfood. Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] @@ -69,6 +70,7 @@ Agents (named with --agents): claude-code github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. Sign in with the same account you use for Databricks profile logfood. Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] From c1d217968a0f8bc10917d8b98a758d6e018f4a73 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 15:02:30 +0000 Subject: [PATCH 81/87] Regenerate goldens for the GovCloud GitHub app in the service note Card 02 names the AWS GovCloud app in the GitHub note, which add prints. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/add-text/output.txt | 4 ++-- acceptance/cmd/aitools/mcp/list-and-remove/output.txt | 2 +- acceptance/cmd/aitools/mcp/list-check/output.txt | 2 +- .../cmd/aitools/mcp/login-flips-active/output.txt | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt index cd10e23bbc6..69f1f7cb939 100644 --- a/acceptance/cmd/aitools/mcp/add-text/output.txt +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -13,7 +13,7 @@ github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json cursor installed [TEST_TMP_DIR]/home/.cursor/mcp.json system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). Cursor may ask you to approve genie_one_mcp-logfood, github-logfood. Run cursor-agent mcp enable for each (`agent` in the Cursor docs), or approve them in Cursor's MCP settings. @@ -31,7 +31,7 @@ github-logfood system.ai.github claude-code already installed, re-asserted [TEST_TMP_DIR]/home/.claude.json cursor already installed, re-asserted [TEST_TMP_DIR]/home/.cursor/mcp.json system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). === Cursor config diff --git a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt index 34bec4dfe6a..8bd77feac27 100644 --- a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt +++ b/acceptance/cmd/aitools/mcp/list-and-remove/output.txt @@ -11,7 +11,7 @@ genie_one_mcp-logfood system.ai.genie_one_mcp github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). >>> [CLI] aitools mcp add --profile dogfood --names system.ai.genie_one_mcp --agents claude-code Profile dogfood ([DATABRICKS_URL]) diff --git a/acceptance/cmd/aitools/mcp/list-check/output.txt b/acceptance/cmd/aitools/mcp/list-check/output.txt index dcf54d0c7bd..d53a71130d5 100644 --- a/acceptance/cmd/aitools/mcp/list-check/output.txt +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -11,7 +11,7 @@ genie_one_mcp-logfood system.ai.genie_one_mcp github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json system.ai.github needs sign-in. Run: DATABRICKS_CONFIG_FILE=[TEST_TMP_DIR]/.databrickscfg databricks aitools mcp login system.ai.github --profile logfood - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). === list --check: the token mints, and GitHub needs sign-in diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt index d332fd71ae1..41631ca77d6 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -20,13 +20,13 @@ Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] Waiting for sign-in... Signed in to system.ai.github on profile logfood. -Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). === login again when already signed in returns at once >>> [CLI] aitools mcp login system.ai.github --profile logfood --output json Already signed in to system.ai.github on profile logfood. -Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). { "service": "system.ai.github", "profile": "logfood", @@ -56,7 +56,7 @@ Agents (named with --agents): claude-code github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). Sign in with the same account you use for Databricks profile logfood. Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] @@ -70,13 +70,13 @@ Agents (named with --agents): claude-code github-logfood system.ai.github claude-code installed [TEST_TMP_DIR]/home/.claude.json - Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. + Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). Sign in with the same account you use for Databricks profile logfood. Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] Waiting for sign-in... Signed in to system.ai.github on profile logfood. -Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector. +Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). >>> jq .mcp | [.servers[] | {name, service, profile, agents: (.agents | keys)}] home/.databricks/aitools/skills/.state.json [ From 976c9be03d34baf7ea0ae9d1dbd0b851d4afb07b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 16:36:04 +0000 Subject: [PATCH 82/87] aitools mcp login: print the service note once, and drop the trailing ? from the leaf-name error Add --login prints the note with the install, so the sign-in no longer prints it a second time. The leaf-name error ends with a question mark, which the error style does not allow. Co-authored-by: Isaac --- .../cmd/aitools/mcp/login-flips-active/output.txt | 1 - cmd/aitools/mcp_login.go | 14 +++++++++----- cmd/aitools/mcp_login_test.go | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt index 41631ca77d6..5826fce39e5 100644 --- a/acceptance/cmd/aitools/mcp/login-flips-active/output.txt +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -76,7 +76,6 @@ Open this URL to sign in to system.ai.github: [DATABRICKS_URL]/mcp-service-login?name=system.ai.github&o=[WORKSPACE_ID] Waiting for sign-in... Signed in to system.ai.github on profile logfood. -Public repositories only until a repository owner installs https://github.com/apps/databricks-github-connector (on AWS GovCloud, https://github.com/apps/databricks-gh-connector-gov-cloud). >>> jq .mcp | [.servers[] | {name, service, profile, agents: (.agents | keys)}] home/.databricks/aitools/skills/.state.json [ diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index e4a47911018..989bd959adb 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -125,7 +125,7 @@ func isLeafName(service string) bool { func runMcpSignIn(ctx context.Context, errOut io.Writer, flagValue, service string, action mcpSignInAction, out *mcpSignInOutput) error { if !isServiceName(service) { if isLeafName(service) { - return failure(protos.AitoolsErrorCategoryInvalidFlags, "service must be a full name such as system.ai.github, got %q; did you mean system.ai.%s?", service, service) + return failure(protos.AitoolsErrorCategoryInvalidFlags, "service must be a full name such as system.ai.github, got %q; did you mean system.ai.%s", service, service) } return failure(protos.AitoolsErrorCategoryInvalidFlags, "service must be a full name such as system.ai.github, got %q", service) } @@ -202,7 +202,14 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie if urlShown { out.URL = url } - return err + if err != nil { + return err + } + // add --login prints the note with the install, so it is not printed again there. + if svc.Known != nil && svc.Known.Note != "" { + fmt.Fprintln(errOut, svc.Known.Note) + } + return nil } // mcpLogout removes the profile's sign-in to the service. The state is DELETED, or NOT_SIGNED_IN when there was no @@ -277,8 +284,5 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient } else { fmt.Fprintf(errOut, "Signed in to %s on profile %s.\n", svc.FullName, profileName) } - if svc.Known != nil && svc.Known.Note != "" { - fmt.Fprintln(errOut, svc.Known.Note) - } return signInStateActive, urlShown, nil } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index d711afdd756..162afe3dd38 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -47,7 +47,7 @@ func TestMcpSignInLeafNameSuggestsFullName(t *testing.T) { var out mcpSignInOutput err := runMcpSignIn(cmdio.MockDiscard(t.Context()), &bytes.Buffer{}, "", "github", mcpLogin, &out) require.Error(t, err) - assert.Equal(t, `service must be a full name such as system.ai.github, got "github"; did you mean system.ai.github?`, err.Error()) + assert.Equal(t, `service must be a full name such as system.ai.github, got "github"; did you mean system.ai.github`, err.Error()) assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) } From 7e8fb8ba088b148614f5937d3e8a5b6b10ec15ca Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 16:36:05 +0000 Subject: [PATCH 83/87] aitools mcp add --login and list --check: fix sign-in state, cancellation, timeouts and refresh failures list --check bounds each request on its own, so a slow answer does not leave the next request with no time. A profile that is account-only is refused before its token is sent, as the other commands refuse it. A token refresh that fails on the network or with a server error is unknown, not failed. add --login records no sign-in state for a sign-in URL that cannot be built, and stops starting sign-ins once the command is cancelled. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 24 ++++++++++++++----- cmd/aitools/mcp_list.go | 46 ++++++++++++++++++++++++++++-------- cmd/aitools/mcp_list_test.go | 17 +++++++++++-- 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 8403b46a683..be8f57234d3 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -442,15 +442,22 @@ func profileClient(ctx context.Context, name, configFile, pinnedFile string) (*d if err != nil { return nil, err } - // The workspace commands refuse an account-only profile after the SDK has resolved its workspace ID, so this - // checks the resolved config, which includes a workspace given in the host or found by discovery. It runs before - // the credential is sent. - if cfg.AccountID != "" && (cfg.WorkspaceID == "" || cfg.WorkspaceID == profile.WorkspaceIDNone) { - return nil, failure(protos.AitoolsErrorCategoryUncategorized, "profile %q is account-level, with no workspace_id in %s; MCP servers need a profile for one workspace", name, filepath.ToSlash(configFile)) + if err := checkWorkspaceProfile(name, configFile, cfg); err != nil { + return nil, err } return boundClient(ctx, cfg, name, pinnedFile) } +// checkWorkspaceProfile refuses an account-only profile, before the credential is sent. The workspace commands refuse it +// after the SDK has resolved its workspace ID, so this checks the resolved config, which includes a workspace given in the +// host or found by discovery. +func checkWorkspaceProfile(name, configFile string, cfg *config.Config) error { + if cfg.AccountID != "" && (cfg.WorkspaceID == "" || cfg.WorkspaceID == profile.WorkspaceIDNone) { + return failure(protos.AitoolsErrorCategoryUncategorized, "profile %q is account-level, with no workspace_id in %s; MCP servers need a profile for one workspace", name, filepath.ToSlash(configFile)) + } + return nil +} + // boundClient returns a workspace client for cfg, a bound config of the named profile, once cfg has a credential. func boundClient(ctx context.Context, cfg *config.Config, name, pinnedFile string) (*databricks.WorkspaceClient, error) { w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) @@ -1057,7 +1064,12 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp if !installedAnywhere(*server) { continue } - state := signInStateFailed + // A cancelled command ends the sign-ins that have not started, rather than failing each one with the same error. + if err := ctx.Err(); err != nil { + errs = append(errs, err) + break + } + var state string url, err := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) if err == nil { state, _, err = signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index b535caa53ba..16bf152a2f4 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "maps" + "net/http" + "net/url" "os" "os/exec" "path/filepath" @@ -25,6 +27,7 @@ import ( "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" + "golang.org/x/oauth2" "golang.org/x/sync/errgroup" ) @@ -487,22 +490,29 @@ func addProfileChecks(ctx context.Context, p *mcpListProfile, login string, out // checkProfileToken returns the client of a profile for its --check tests and checks its token. A personal access token is // sent only with a request, so the token is checked with one, bounded by profileCheckTimeout. func checkProfileToken(ctx context.Context, p *mcpListProfile, login string) (*databricks.WorkspaceClient, error) { - ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) - defer cancel() w, err := profileCheckClient(ctx, p, login) if err != nil { return nil, err } - if _, err := w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { + _, err = withRequestTimeout(ctx, func(ctx context.Context) (*iam.User, error) { + return w.CurrentUser.Me(ctx, iam.MeRequest{}) + }) + if err != nil { return nil, describeRequestError(p.Profile, login, err) } return w, nil } -// checkServerSignIn returns the sign-in status of one server, with its requests bounded by profileCheckTimeout. -func checkServerSignIn(ctx context.Context, w *databricks.WorkspaceClient, profileName, login string, s *mcpListServer) (string, error) { +// withRequestTimeout runs one request of a --check test with a deadline of profileCheckTimeout of its own. The requests of +// a check do not share one budget, so a slow answer to one does not leave the next with no time. +func withRequestTimeout[T any](ctx context.Context, request func(ctx context.Context) (T, error)) (T, error) { ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) defer cancel() + return request(ctx) +} + +// checkServerSignIn returns the sign-in status of one server. +func checkServerSignIn(ctx context.Context, w *databricks.WorkspaceClient, profileName, login string, s *mcpListServer) (string, error) { return signInStatus(ctx, w, profileName, login, s.Service, s.SignInRequired) } @@ -513,19 +523,31 @@ func profileCheckClient(ctx context.Context, p *mcpListProfile, login string) (* if err != nil { return nil, err } + if err := checkWorkspaceProfile(p.Profile, p.configPath, cfg); err != nil { + return nil, err + } return boundClient(ctx, cfg, p.Profile, login) } // tokenStatus is the --check result of a profile's token. A credential that the workspace or the profile refuses is // failed. Any other failure, such as a host that cannot be reached, says nothing about the credential, so it is unknown. +// A token refresh that fails on the network or on the server is one of those: the SDK reports it as an auth failure, so +// the cause decides. func tokenStatus(err error) string { if err == nil { return statusOK } - if be, ok := errors.AsType[*mcp.BindError](err); ok && be.Category == mcp.BindAuthFailed { - return statusFailed + be, ok := errors.AsType[*mcp.BindError](err) + if !ok || be.Category != mcp.BindAuthFailed { + return statusUnknown + } + if _, network := errors.AsType[*url.Error](err); network { + return statusUnknown } - return statusUnknown + if re, refused := errors.AsType[*oauth2.RetrieveError](err); refused && re.Response.StatusCode >= http.StatusInternalServerError { + return statusUnknown + } + return statusFailed } // skipReason returns why the --check tests do not run for a server, or "" when they do. A server that drifted from its @@ -555,7 +577,9 @@ func skipUncheckable(p *mcpListProfile) { // credential is unknown. A service the profile can no longer see is unknown only when the record says the server needed // sign-in (recorded); otherwise there is no sign-in to report, and the error is the warning. func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string, recorded bool) (string, error) { - svc, err := serviceFor(ctx, w, profileName, pinnedFile, service) + svc, err := withRequestTimeout(ctx, func(ctx context.Context) (mcp.Service, error) { + return serviceFor(ctx, w, profileName, pinnedFile, service) + }) if err != nil { if !recorded { return "", err @@ -565,7 +589,9 @@ func signInStatus(ctx context.Context, w *databricks.WorkspaceClient, profileNam if !svc.SignInRequired() { return "", nil } - state, err := mcp.CredentialState(ctx, w, service) + state, err := withRequestTimeout(ctx, func(ctx context.Context) (catalog.ProvisioningInfoState, error) { + return mcp.CredentialState(ctx, w, service) + }) if err != nil { return statusUnknown, describeRequestError(profileName, pinnedFile, err) } diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index 3b36f852e04..f03c819bb35 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -4,8 +4,10 @@ import ( "bytes" "context" "encoding/json" + "errors" "net/http" "net/http/httptest" + "net/url" "os" "os/exec" "path/filepath" @@ -27,6 +29,7 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "golang.org/x/oauth2" ) const testDatabricksCommand = "/opt/databricks/bin/databricks" @@ -1240,8 +1243,6 @@ func TestAddChecksReadsEachProfileFromItsOwnConfigFile(t *testing.T) { assert.Empty(t, out.Warnings) } -// TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt checks that the config file of a profile is set in the -// context and in the process environment while a check runs, and that the caller's environment is restored after. // multiServiceServer answers the token check, and for each service the service read and the credential read. Each // service read calls beforeRead with the service name first, and each credential read is answered by credential. func multiServiceServer(t *testing.T, beforeRead func(service string), credential func(service string) (int, string)) *httptest.Server { @@ -1357,6 +1358,8 @@ func TestAddChecksKeepsServerOrderWhenChecksFinishOutOfOrder(t *testing.T) { assert.Contains(t, out.Warnings[1], "MCP server workiq-logfood:") } +// TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt checks that the config file of a profile is set in the +// context and in the process environment while a check runs, and that the caller's environment is restored after. func TestWithConfigFileSetsTheEnvironmentForTheCallAndRestoresIt(t *testing.T) { t.Setenv(mcpconfig.ConfigFileEnv, "ambient.cfg") var inContext, inProcess string @@ -1588,3 +1591,13 @@ func TestAddChecksAuthWarningNamesFileTheShellNames(t *testing.T) { require.Len(t, out.Warnings, 1) assert.Contains(t, out.Warnings[0], "Run: DATABRICKS_CONFIG_FILE="+shellquote.BashArg(filepath.ToSlash(checked))+" databricks auth login --profile logfood") } + +// TestTokenStatusNetworkFailureIsUnknown checks that a token that cannot be refreshed because the host is unreachable is +// unknown, not failed: the credential was not refused. +func TestTokenStatusNetworkFailureIsUnknown(t *testing.T) { + network := mcp.AuthError("logfood", "", &url.Error{Op: "Post", URL: "https://example.test/oidc/v1/token", Err: errors.New("connection refused")}) + assert.Equal(t, statusUnknown, tokenStatus(network)) + + refused := mcp.AuthError("logfood", "", &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusBadRequest}}) + assert.Equal(t, statusFailed, tokenStatus(refused)) +} From f86f90fa3c9a2633c693406395fbbfbfc107c32d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 16:36:07 +0000 Subject: [PATCH 84/87] aitools mcp serve: name the config file in the sign-in hint as ConfigFileCommand does The redirect hint built its own command, so on Windows it kept backslashes that the other hints do not. It now uses ConfigFileCommand, and the duplicate of PinnedFile is removed. The auth error test uses a lowercase cause, as the error style asks. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 15 ++------------- libs/aitools/mcp/binding_test.go | 6 +++--- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index d9d0e7bd6e3..e2668001279 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -162,10 +162,7 @@ func (s *signInRedirect) RoundTrip(req *http.Request) (*http.Response, error) { // loginError is the error for a redirect to sign-in. The login command names DATABRICKS_CONFIG_FILE when the profile is // pinned to a config file, since the login must write that file. func (s *signInRedirect) loginError(status int) error { - login := "databricks auth login --profile " + shellquote.BashArg(s.profile) - if s.configFile != "" { - login = "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(s.configFile) + " " + login - } + login := mcp.ConfigFileCommand(s.configFile, "databricks auth login --profile "+shellquote.BashArg(s.profile)) return fmt.Errorf("the MCP endpoint redirected to sign-in (HTTP %d), so it did not accept the token for Databricks profile \"%s\"\nRun: %s", status, s.profile, login) } @@ -244,19 +241,11 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { Transport: &signInRedirect{ next: newUpstreamLog(serveTransport(cfg)), profile: profileName, - configFile: pinnedConfigFile(entry, cfg), + configFile: mcp.PinnedFile(entry, cfg.ConfigFile), }, }, mcp.PinnedFile(entry, cfg.ConfigFile)) } -// pinnedConfigFile returns the config file that entry pins its profile to, or "" when the entry has none. -func pinnedConfigFile(entry *mcp.Entry, cfg *config.Config) string { - if entry == nil || entry.ConfigFile == "" { - return "" - } - return cfg.ConfigFile -} - // serveProxy bridges opts to the service until stdin closes or a signal arrives. Authenticate goes through a // refreshGuard, so a rejected token stops the proxy; see proxyResult. pinnedFile is the config file the server is // pinned to, or "" (see mcp.PinnedFile). diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 5171a8f095a..5639d021d28 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -881,10 +881,10 @@ func TestCheckBindingChecksProfileWithoutEntry(t *testing.T) { } func TestAuthErrorNamesPinnedConfigFile(t *testing.T) { - cause := errors.New("Invalid access token.") + cause := errors.New("invalid access token") assert.EqualError(t, mcp.AuthError("logfood", "/work/cfg/logfood.cfg", cause), - "Invalid access token.\nRun: DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks auth login --profile logfood") - assert.EqualError(t, mcp.AuthError("logfood", "", cause), "Invalid access token.\nRun: databricks auth login --profile logfood") + "invalid access token\nRun: DATABRICKS_CONFIG_FILE=/work/cfg/logfood.cfg databricks auth login --profile logfood") + assert.EqualError(t, mcp.AuthError("logfood", "", cause), "invalid access token\nRun: databricks auth login --profile logfood") } // TestConfigFileCommandUsesForwardSlashes checks that a pinned config file is named with forward slashes, so that the From 01b3f89c0f602c982bf507a4d0275b76bb4ed0a9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 16:56:29 +0000 Subject: [PATCH 85/87] aitools mcp list --check and login: keep a refused OAuth refresh failed A refresh token that the token endpoint refuses arrives as an HTTP error inside a URL error, so a URL error no longer means the host could not be reached. list --check now checks for a refused credential before it treats a URL error as unknown, and a sign-in poll ends at once on a refused refresh instead of waiting out the timeout. Co-authored-by: Isaac --- cmd/aitools/mcp_list.go | 12 +++++------- cmd/aitools/mcp_list_test.go | 10 +++++++--- libs/aitools/mcp/login.go | 7 ++++++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 16bf152a2f4..5b3915fc14b 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "maps" - "net/http" "net/url" "os" "os/exec" @@ -27,7 +26,6 @@ import ( "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" - "golang.org/x/oauth2" "golang.org/x/sync/errgroup" ) @@ -531,8 +529,8 @@ func profileCheckClient(ctx context.Context, p *mcpListProfile, login string) (* // tokenStatus is the --check result of a profile's token. A credential that the workspace or the profile refuses is // failed. Any other failure, such as a host that cannot be reached, says nothing about the credential, so it is unknown. -// A token refresh that fails on the network or on the server is one of those: the SDK reports it as an auth failure, so -// the cause decides. +// The SDK reports a token refresh that fails on the network as an auth failure too, so a refusal is told apart from an +// unreachable host by its cause. func tokenStatus(err error) string { if err == nil { return statusOK @@ -541,10 +539,10 @@ func tokenStatus(err error) string { if !ok || be.Category != mcp.BindAuthFailed { return statusUnknown } - if _, network := errors.AsType[*url.Error](err); network { - return statusUnknown + if credentialRejected(err) { + return statusFailed } - if re, refused := errors.AsType[*oauth2.RetrieveError](err); refused && re.Response.StatusCode >= http.StatusInternalServerError { + if _, network := errors.AsType[*url.Error](err); network { return statusUnknown } return statusFailed diff --git a/cmd/aitools/mcp_list_test.go b/cmd/aitools/mcp_list_test.go index f03c819bb35..60173a4fd4d 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -27,9 +27,9 @@ import ( "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/httpclient" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "golang.org/x/oauth2" ) const testDatabricksCommand = "/opt/databricks/bin/databricks" @@ -1598,6 +1598,10 @@ func TestTokenStatusNetworkFailureIsUnknown(t *testing.T) { network := mcp.AuthError("logfood", "", &url.Error{Op: "Post", URL: "https://example.test/oidc/v1/token", Err: errors.New("connection refused")}) assert.Equal(t, statusUnknown, tokenStatus(network)) - refused := mcp.AuthError("logfood", "", &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusBadRequest}}) - assert.Equal(t, statusFailed, tokenStatus(refused)) + // The CLI's OAuth client wraps an error response in a URL error, and a refused refresh token is still failed. + refreshRefused := mcp.AuthError("logfood", "", &url.Error{Op: "Post", URL: "https://example.test/oidc/v1/token", Err: &httpclient.HttpError{ + Response: &http.Response{StatusCode: http.StatusBadRequest}, + Message: `{"error":"invalid_grant"}`, + }}) + assert.Equal(t, statusFailed, tokenStatus(refreshRefused)) } diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 18e71d9c9bd..ba36bccc165 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -12,6 +12,7 @@ import ( "github.com/databricks/cli/libs/browser" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/httpclient" "github.com/databricks/databricks-sdk-go/service/catalog" ) @@ -153,11 +154,15 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str } // transientPollError reports whether a failed poll is worth polling again: a server error, or a network failure. Any -// other error, such as a refused credential, ends the sign-in. +// other error, such as a refused credential, ends the sign-in. The CLI's OAuth client reports an error response as an +// HTTP error inside a URL error, so that error's status decides before the network failure does. func transientPollError(err error) bool { if apiErr, ok := errors.AsType[*apierr.APIError](err); ok { return apiErr.StatusCode >= http.StatusInternalServerError } + if httpErr, ok := errors.AsType[*httpclient.HttpError](err); ok { + return httpErr.StatusCode >= http.StatusInternalServerError + } _, ok := errors.AsType[*url.Error](err) return ok } From 869c748ae0cac45473d1d71486328c5b16fd2d08 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 16:56:31 +0000 Subject: [PATCH 86/87] aitools mcp add --login: report a cancelled sign-in once, and reuse displayError A cancelled command reports the cancellation once, from the sign-in it interrupted, rather than again for each sign-in that follows. failureWithCause uses displayError, as the other messages with a cause do, instead of a second type with the same fields. BoundConfigWithin rounds a budget up to whole seconds, so a budget under one second stays a bound instead of the SDK default. Co-authored-by: Isaac --- cmd/aitools/mcp.go | 12 +----------- cmd/aitools/mcp_add.go | 5 ++++- libs/aitools/mcp/binding.go | 8 ++++---- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index 961969a0c07..aa6029396b2 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -69,19 +69,9 @@ func withAgentsHint(err error, agent string) error { // failureWithCause is failure for a message that does not repeat its cause. cause is kept for errors.Is and errors.As // but is not printed. func failureWithCause(category protos.AitoolsErrorCategory, cause error, format string, args ...any) error { - return withCategory(&causedError{message: fmt.Sprintf(format, args...), cause: cause}, category) + return withCategory(&displayError{msg: fmt.Sprintf(format, args...), err: cause}, category) } -// causedError is a message with the error that caused it. -type causedError struct { - message string - cause error -} - -func (e *causedError) Error() string { return e.message } - -func (e *causedError) Unwrap() error { return e.cause } - // errorCategory classifies an error by its typed errors only, never by its text. Failures without a // category of their own fall through to the install classification, as in the other aitools commands. func errorCategory(err error) protos.AitoolsErrorCategory { diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index be8f57234d3..4f7b0d36424 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -1065,8 +1065,11 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp continue } // A cancelled command ends the sign-ins that have not started, rather than failing each one with the same error. + // The sign-in that the cancellation interrupted has already reported it. if err := ctx.Err(); err != nil { - errs = append(errs, err) + if len(errs) == 0 { + errs = append(errs, err) + } break } var state string diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 641b41ad870..481632dcae9 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -106,11 +106,11 @@ func BoundConfig(profileName, configFile string) (*config.Config, error) { }) } -// BoundConfigWithin is BoundConfig with the SDK's request and retry budgets set to budget, in whole seconds. Resolving the -// profile runs on a context of its own, so a context deadline does not reach it, and these budgets are what bound it (see -// profileMetadata.Load in cmd/auth). +// BoundConfigWithin is BoundConfig with the SDK's request and retry budgets set to budget, rounded up to whole seconds. A +// budget of zero keeps the SDK's defaults. Resolving the profile runs on a context of its own, so a context deadline does +// not reach it, and these budgets are what bound it (see profileMetadata.Load in cmd/auth). func BoundConfigWithin(profileName, configFile string, budget time.Duration) (*config.Config, error) { - seconds := int(budget / time.Second) + seconds := int((budget + time.Second - 1) / time.Second) return resolveBound(&config.Config{ Profile: profileName, ConfigFile: configFile, From 466befa1d9cd2474c4667140f6627bcca294ed7b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Tue, 17 Nov 2026 16:56:32 +0000 Subject: [PATCH 87/87] Add a changelog fragment for the browser open that no longer waits The default opener no longer waits for the browser process that databricks auth login starts. Co-authored-by: Isaac --- .nextchanges/cli/browser-open-no-wait.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 .nextchanges/cli/browser-open-no-wait.md diff --git a/.nextchanges/cli/browser-open-no-wait.md b/.nextchanges/cli/browser-open-no-wait.md new file mode 100644 index 00000000000..5c83f5893ba --- /dev/null +++ b/.nextchanges/cli/browser-open-no-wait.md @@ -0,0 +1 @@ +* `databricks auth login` no longer waits for the browser that the default opener starts to close before it continues. ([#7551](https://github.com/databricks/cli/pull/7551))