From 43c9234a4f5a603f83cfe84a0f6a1cc40e0bbf8a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 12:39:50 +0000 Subject: [PATCH 01/66] 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 b275848610e..72ad6fac747 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -39,6 +39,8 @@ func NewMcpCmd() *cobra.Command { 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 288e884a0b9..442c73a4c8c 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" @@ -40,6 +41,8 @@ type mcpAddFlags struct { agents string name string force bool + login bool + noLogin bool } // addOutput is the `mcp add --output json` document. @@ -98,6 +101,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 @@ -164,6 +169,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 } @@ -278,6 +285,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} @@ -299,11 +307,19 @@ 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) } // validateMCPAddFlags rejects flag combinations that cannot run, before anything is read or written. func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames []string, jsonMode bool) error { + if f.login && f.noLogin { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--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. @@ -835,6 +851,30 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%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. @@ -962,7 +1002,7 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut cursor = true } } - 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 cursor { diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 1cad4af0ea6..df17a91239b 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -68,6 +68,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 d80972d75cc..25e7b0e316d 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 ( @@ -63,6 +74,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 { @@ -73,12 +87,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"` @@ -102,7 +125,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 @@ -113,13 +137,17 @@ Entries that run 'aitools mcp serve' but have no install record are listed as un --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.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() profileName := profileFlag(cmd) out, err := buildMcpList(ctx, profileName) + if check { + addChecks(ctx, &out) + } if !installOutputIsJSON(cmd) { if err != nil { return err @@ -141,6 +169,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 @@ -324,6 +354,59 @@ func agentStatus(ctx context.Context, files *agentFiles, rec mcp.Entry, a *agent return agentStatusInstalled, loc.path, "", nil } +// 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 { @@ -369,9 +452,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) } @@ -412,6 +503,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 e4fa4036c4d..8a70c18bbad 100644 --- a/cmd/aitools/mcp_test.go +++ b/cmd/aitools/mcp_test.go @@ -31,7 +31,7 @@ func TestMcpCommandIsHidden(t *testing.T) { for _, c := range mcp.Commands() { names = append(names, c.Name()) } - assert.Equal(t, []string{"add", "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 6c448d2543b..1950e923dfb 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -55,6 +55,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 9e91a98949b..41779977f39 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -935,6 +935,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 1a2c9c05095..523283242ea 100644 --- a/libs/testserver/mcp_services.go +++ b/libs/testserver/mcp_services.go @@ -106,3 +106,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 21a47469f8db0c655e0eba77013a9d8994279406 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:40:28 +0000 Subject: [PATCH 02/66] 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 9ca8b7a078d5c44ea47f4e22bfc5bcfb7c1dc4df Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 14:40:29 +0000 Subject: [PATCH 03/66] 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 4d576311f2059bd6612c2cd0ec57709845e6a3d2 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:12:55 +0000 Subject: [PATCH 04/66] 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 3745c1ca80a554c99e06e0cbab23f3cd86b9b26b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:37:10 +0000 Subject: [PATCH 05/66] 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 97a649acb9b8c70cfa29a1434a9685e241ed83b5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:37:11 +0000 Subject: [PATCH 06/66] 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 | 2 ++ .../cmd/aitools/mcp/list-check/output.txt | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index ed892dd4ef5..0904e7a3a96 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -27,8 +27,10 @@ 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 --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 enable debug logging 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 3dce9f644c831e5e632d8146e21f767f70724e08 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:54:35 +0000 Subject: [PATCH 07/66] 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 | 18 ++++++------------ 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, 49 insertions(+), 53 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 442c73a4c8c..abe8d917d40 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -858,7 +858,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 } } @@ -949,15 +950,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 } } @@ -991,18 +989,14 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut for _, server := range servers { cmdio.LogString(ctx, "") cmdio.LogString(ctx, server.Name+" "+server.Service) - succeeded := false cursor := false for _, a := range server.Agents { cmdio.LogString(ctx, " "+agentResultLine(ctx, a)) - if a.Status != statusFailed { - succeeded = true - } if a.Name == agents.NameCursor && a.Status == statusInstalled { cursor = true } } - if server.SignInRequired && succeeded && !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 cursor { diff --git a/cmd/aitools/mcp_list.go b/cmd/aitools/mcp_list.go index 25e7b0e316d..3cfb6b65a70 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -74,9 +74,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 { @@ -363,13 +363,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 { @@ -392,16 +392,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 @@ -453,8 +451,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 fccdbbaf5d10ea8631c0b7f245f066d693551c78 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:57:02 +0000 Subject: [PATCH 08/66] 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 1950e923dfb..f6ef2904e0f 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -56,12 +56,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 41660b2afc0..b3b51782ddb 100644 --- a/libs/aitools/mcp/discovery_test.go +++ b/libs/aitools/mcp/discovery_test.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "net/http/httptest" + "strings" "sync/atomic" "testing" @@ -303,3 +304,34 @@ func TestMapListErrorLeavesOtherErrorsUnclassified(t *testing.T) { assert.False(t, ok) assert.ErrorIs(t, err, apierr.ErrUnauthenticated) } + +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 523283242ea..0d15cf34c70 100644 --- a/libs/testserver/mcp_services.go +++ b/libs/testserver/mcp_services.go @@ -109,10 +109,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 c7166bbc908..cec9a6bbb03 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 9cff3c673a5bb1961650338b43770e40bb18f6a6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 02:58:56 +0000 Subject: [PATCH 09/66] 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 3bde911a0eb4c6acd3b0a831268752cd79716762 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:00:37 +0000 Subject: [PATCH 10/66] 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 abe8d917d40..c3f10506a9f 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -301,18 +301,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. @@ -851,19 +848,26 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%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. @@ -997,7 +1001,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 cursor { cmdio.LogString(ctx, cursorApprovalHint(server.Name)) diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index df17a91239b..944e873e9aa 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1387,3 +1387,15 @@ func writeFakeCLI(t *testing.T, binary string) { require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"), 0o755)) t.Setenv("PATH", dir) } + +// 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 f50a1cf741b6e955b0f3f3634848fffb31b2edbd Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:02:43 +0000 Subject: [PATCH 11/66] 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 3cfb6b65a70..736f8969fa0 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" ) @@ -354,42 +355,85 @@ func agentStatus(ctx context.Context, files *agentFiles, rec mcp.Entry, a *agent return agentStatusInstalled, loc.path, "", nil } -// 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 c09a9b36202..84950b078f8 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" ) @@ -867,3 +873,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 5075b462dfbfadea723bb18b26e979915beb405a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:04:47 +0000 Subject: [PATCH 12/66] 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 c3f10506a9f..afcfed0e586 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -20,7 +20,6 @@ import ( "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" - "github.com/databricks/cli/libs/shellquote" "github.com/databricks/cli/libs/telemetry/protos" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" @@ -867,7 +866,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 736f8969fa0..f0e4a68ee77 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" @@ -123,8 +122,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 84f4173e760a279a49f095900aee025fd4402668 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:05:17 +0000 Subject: [PATCH 13/66] 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 0e144a97a7238a8382df17fc0b8d71d42d368f79 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:05:56 +0000 Subject: [PATCH 14/66] 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 84950b078f8..aaa88705129 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -876,7 +876,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 fab1acee8c94d0d6c21951f78a9cb4adb8a678a3 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:29:17 +0000 Subject: [PATCH 15/66] 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 f6ef2904e0f..bc2b02d288c 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" @@ -59,7 +74,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 526f11633573523560b74ac64267c776d6a716ff Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:29:28 +0000 Subject: [PATCH 16/66] 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 e4cb6167982233664fdfc863942446fbb5be6e2b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:29:30 +0000 Subject: [PATCH 17/66] 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 dd5446eb05e..f793e80daf0 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -92,7 +92,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 a49f8f85693..705b29576d4 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -298,7 +298,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 cf2754058cfc93a5df627265396247651d485a70 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:30:14 +0000 Subject: [PATCH 18/66] 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 995402e0df617e84a0bf814f98b6461b97919557 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:30:26 +0000 Subject: [PATCH 19/66] 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 bc2b02d288c..a170e1335e1 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -135,13 +135,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 40fc86dda812232082af9379e31de4cba4b769e9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:31:12 +0000 Subject: [PATCH 20/66] 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 a170e1335e1..ba512343d62 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -141,6 +141,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 301d348cf124f0b56ac5bd0c652d1296173d195c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:31:54 +0000 Subject: [PATCH 21/66] 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 1ab14a2d4fbd1282cb9ff831d3aab22c7e05a8c9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:32:12 +0000 Subject: [PATCH 22/66] 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 f0e4a68ee77..9bd6417dbbe 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" ) @@ -384,11 +383,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) @@ -399,16 +398,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 aaa88705129..817f5724eba 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1096,3 +1096,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 09f8a9ae71f86eb667867e350f7a7d54d855dcb7 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:33:07 +0000 Subject: [PATCH 23/66] 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 9bd6417dbbe..848d2210db8 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" ) @@ -375,6 +376,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 817f5724eba..514c407d0a7 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -882,6 +882,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) @@ -1120,6 +1122,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"}`)) @@ -1137,3 +1141,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 49a84a60fbc01b1e3396c88e60ec874eb24c1b08 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:33:29 +0000 Subject: [PATCH 24/66] 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 848d2210db8..2768e29fcea 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -441,14 +441,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 514c407d0a7..1b834dbe307 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1060,6 +1060,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 fdecdc78b4be36bd418f467f881abab44833d11e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:34:24 +0000 Subject: [PATCH 25/66] 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 11253434904c3be0732078d804bddd19b0581f95 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:34:50 +0000 Subject: [PATCH 26/66] 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 7d6ac3eb15b83969b18c9b8e3ecd2b3a2a859636 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:36:38 +0000 Subject: [PATCH 27/66] 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 d3377bfb91a64bb4aabe46ede2fbf82814abe1d4 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:37:01 +0000 Subject: [PATCH 28/66] 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 07f9353d32a251edd989478c1df30e155ac06e37 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:38:32 +0000 Subject: [PATCH 29/66] 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 30c17d6aa387e5ad8c3be2640d046eb91b20e5fa Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:39:46 +0000 Subject: [PATCH 30/66] 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 a886f378f23..d008393be6c 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -181,11 +181,16 @@ func RemoveNamesCommand(name 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 12ed9b3cb398a61414600810710edee38074b94a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:41:45 +0000 Subject: [PATCH 31/66] 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 afcfed0e586..1c774a10adb 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -250,19 +250,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 { @@ -370,7 +370,7 @@ func addConfigFile(ctx context.Context) (path, pinned string, err error) { // 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 @@ -379,7 +379,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 @@ -388,9 +388,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) } @@ -856,8 +856,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) } } @@ -865,8 +865,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. @@ -1000,7 +1000,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 cursor { cmdio.LogString(ctx, cursorApprovalHint(server.Name)) diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 944e873e9aa..a843924db9d 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -787,7 +787,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 2768e29fcea..7671fea6ccb 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -375,11 +375,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 @@ -397,7 +397,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)) @@ -408,8 +408,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 } @@ -418,7 +418,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 1b834dbe307..8dd5cd14064 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1091,7 +1091,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) @@ -1170,3 +1170,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 f793e80daf0..198bf6ef40b 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -228,7 +228,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 705b29576d4..7c64d269b58 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -197,7 +197,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 d008393be6c..7e6a37e220e 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -86,7 +86,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 @@ -110,8 +110,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) @@ -119,7 +120,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 } @@ -241,9 +242,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 af26cead5fb..4e77943efe9 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 62576d5cc52b21ba6c8ad9becd7cd019c0b7947e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:42:59 +0000 Subject: [PATCH 32/66] 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 1c774a10adb..701207d77d8 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -848,7 +848,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 { @@ -857,13 +858,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 26a519539be9e01cb50f1cec0af181bd5a44183f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:44:15 +0000 Subject: [PATCH 33/66] 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 7671fea6ccb..68f4f6c469a 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -427,18 +427,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 8dd5cd14064..2eaf2aad7f1 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1021,6 +1021,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 0dcd5dcfd56ef859e648c94a201fec6875c3fee4 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:45:27 +0000 Subject: [PATCH 34/66] 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 72ad6fac747..c829a1298c5 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -26,6 +26,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 68f4f6c469a..eaa8dbfa360 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. @@ -384,7 +382,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 { @@ -452,11 +450,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 } @@ -572,7 +570,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 2eaf2aad7f1..7840c44e64e 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -964,7 +964,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]) @@ -1044,8 +1044,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 { @@ -1069,12 +1069,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. @@ -1176,7 +1176,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 73ec6668adfd2f4aba4ce27db48fca644c1deb20 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:45:28 +0000 Subject: [PATCH 35/66] 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 701207d77d8..c473057d2e2 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -168,7 +168,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 a843924db9d..841feaf986b 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1387,15 +1387,3 @@ func writeFakeCLI(t *testing.T, binary string) { require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"), 0o755)) t.Setenv("PATH", dir) } - -// 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 fec02111b5e67048f418d51814c837ec23bf8e04 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:46:41 +0000 Subject: [PATCH 36/66] 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 | 4 ++-- acceptance/cmd/aitools/mcp/hidden/output.txt | 2 +- acceptance/cmd/aitools/mcp/list-and-remove/output.txt | 2 +- acceptance/cmd/aitools/mcp/list-check/output.txt | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/add-text/output.txt b/acceptance/cmd/aitools/mcp/add-text/output.txt index d35665a25fc..f961048ccec 100644 --- a/acceptance/cmd/aitools/mcp/add-text/output.txt +++ b/acceptance/cmd/aitools/mcp/add-text/output.txt @@ -13,7 +13,7 @@ Cursor may ask you to approve genie_one_mcp-logfood. Run: cursor-agent mcp enabl 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 +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 @@ -29,7 +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 +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 0904e7a3a96..1d58c9b50c8 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -27,7 +27,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 145ee12df94..22e41ca38be 100644 --- a/acceptance/cmd/aitools/mcp/list-and-remove/output.txt +++ b/acceptance/cmd/aitools/mcp/list-and-remove/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 >>> [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 2380f660c0fdcf046c73898f25b004bb40fa2009 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 03:49:36 +0000 Subject: [PATCH 37/66] 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 26d44d6bdfd0700920cb1cc24e3f50061465d963 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:13:28 +0000 Subject: [PATCH 38/66] 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 7840c44e64e..8fdb624c4e7 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1197,8 +1197,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 898858e96ed022f23437fa4c7f0a4ddbaf1b7175 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:13:52 +0000 Subject: [PATCH 39/66] 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 7e6a37e220e..b810d36b9b2 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -186,12 +186,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 4e77943efe9..79bc2d08017 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 1f9885b89f052db75cbcc935e8be0a7b5043304e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:15:20 +0000 Subject: [PATCH 40/66] 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 fb67ddb0fd4c733d48d87a5f224bb6a518baff9b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:17:05 +0000 Subject: [PATCH 41/66] 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 | 17 ++++++++++------- cmd/aitools/mcp_serve_test.go | 18 +++++++++++++++++- libs/aitools/mcp/binding.go | 12 +++++++++++- libs/aitools/mcp/binding_test.go | 6 ++++-- 5 files changed, 43 insertions(+), 15 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 198bf6ef40b..b8fe267b1a5 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -100,7 +100,7 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { if err != nil { return serveFailure(cmd, err, serveBindStatus) } - cfg, _, err := mcp.Bind(cmd.Context(), mcp.BindOptions{ + cfg, entry, err := mcp.Bind(cmd.Context(), mcp.BindOptions{ Profile: profileName, Service: f.service, State: state, @@ -120,15 +120,16 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { Profile: profileName, Agent: serveAgent(f.agent, cmd.ErrOrStderr()), Transport: serveTransport(cfg), - }) + }, mcp.PinnedFile(entry, 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. -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)) } @@ -200,7 +201,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 @@ -228,7 +231,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 7c64d269b58..b4db3695d35 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -111,6 +111,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) { @@ -586,7 +602,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 b810d36b9b2..68dab4c2484 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -86,7 +86,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 @@ -125,6 +125,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 79bc2d08017..86b7b15b495 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 1585c043035552f3fcc6fc75c74ac561603a8df1 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:18:21 +0000 Subject: [PATCH 42/66] 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 eaa8dbfa360..77130b184f0 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -361,8 +361,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)) @@ -370,14 +373,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 @@ -395,7 +399,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)) @@ -587,17 +591,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 8fdb624c4e7..db3e870c874 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1205,3 +1205,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 c8e4efc79a686a21e4e16f9a4cfa56c0dac0f722 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:18:52 +0000 Subject: [PATCH 43/66] 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 e37fe7085c4511485fc4253e1656d265e4bc00b9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:19:33 +0000 Subject: [PATCH 44/66] 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 c473057d2e2..718aa1e955e 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -848,8 +848,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 { @@ -858,21 +859,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 3388d61867d7fdd4f4d1e1f4b6377895c3c09102 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:20:31 +0000 Subject: [PATCH 45/66] 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 77130b184f0..e565698b4e0 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -75,6 +75,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 { @@ -359,6 +361,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 @@ -399,7 +402,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)) @@ -409,10 +412,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 { @@ -517,6 +525,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 db3e870c874..391d772abec 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -629,6 +629,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) { @@ -939,6 +959,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) { @@ -1084,14 +1121,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) { @@ -1103,7 +1143,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 ad2c90e93de0810ea9c8df55c6004b52e24bb5c9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:21:03 +0000 Subject: [PATCH 46/66] 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 32aeab8eec1ac03e11a840670bbe0833e333ee9d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:23:51 +0000 Subject: [PATCH 47/66] 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 ba512343d62..485d3c7cb0d 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 19d8c85d90ace3e1ee8680b2b9067fc4915eecbe Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:23:52 +0000 Subject: [PATCH 48/66] 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 718aa1e955e..a92915119c5 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -858,7 +858,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 06f2f5b422a93c42d7bf6e2e4aa4a6505f8bfa51 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:24:34 +0000 Subject: [PATCH 49/66] 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 b97453fead7fff9eb8e58712408ba5d1bf9830ad Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:28:53 +0000 Subject: [PATCH 50/66] 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 e01c5e243ae78bb82618779fdf0c76f287bcd6f5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:48:21 +0000 Subject: [PATCH 51/66] 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 8aa98bce9a3e763070f0f20b0238a9ec6c7e9142 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:48:59 +0000 Subject: [PATCH 52/66] 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 04c597e621f58b3425fb8f44744cf69a2f5a1dc8 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:49:26 +0000 Subject: [PATCH 53/66] 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 53002538588794d6405498bab85d5a23ae9466b9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:53:27 +0000 Subject: [PATCH 54/66] 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 74ff994120a313e9515da6c3cb2bdc1507d947b6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:56:33 +0000 Subject: [PATCH 55/66] 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 97eacf5085d430a8dfbbdae3797a46cbdcd60ca6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 04:56:34 +0000 Subject: [PATCH 56/66] 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 e565698b4e0..6f230831e33 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -590,12 +590,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 391d772abec..98be1164dd3 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -629,6 +629,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 6b00388d4f460ca602f9aa4720b025f38e93f1be Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:03:47 +0000 Subject: [PATCH 57/66] 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 a92915119c5..c5f67c46fb0 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -23,6 +23,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" ) @@ -375,6 +376,11 @@ func profileClient(ctx context.Context, name, configFile, pinnedFile string) (*d if err != nil { return nil, err } + 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 6f230831e33..7ea8e8dc579 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" @@ -354,6 +355,9 @@ func agentStatus(ctx context.Context, files *agentFiles, rec mcp.Entry, a *agent return agentStatusInstalled, loc.path, "", nil } +// 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. @@ -376,20 +380,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 { @@ -410,6 +415,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 98be1164dd3..6d41a1176d2 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" @@ -1026,6 +1027,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 68dab4c2484..b4c127894f7 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" @@ -97,11 +98,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 fa645c055f64faebffbe038418ba7c50dbb54933 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:05:51 +0000 Subject: [PATCH 58/66] 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 7ea8e8dc579..a7b48a8fcfa 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -76,8 +76,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 { @@ -88,7 +86,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. @@ -96,11 +95,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 { @@ -358,14 +366,15 @@ func agentStatus(ctx context.Context, files *agentFiles, rec mcp.Entry, a *agent // 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 @@ -380,9 +389,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() @@ -399,6 +408,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. @@ -437,6 +449,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 @@ -552,8 +586,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 != "" { @@ -602,6 +634,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: @@ -617,6 +652,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 6d41a1176d2..1d27fd164d9 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1080,6 +1080,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 6bd6bc14efae88f0c1bbfbc11c6a0dfd8ac58cb5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:07:10 +0000 Subject: [PATCH 59/66] 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 c5f67c46fb0..d5741449696 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -60,6 +60,16 @@ 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"` +} + +// 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 { @@ -853,18 +863,27 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%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 3e6594cb090dd24f5972e5bd1f82607a59a10085 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:10:17 +0000 Subject: [PATCH 60/66] 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 a7b48a8fcfa..c15ded0ae4e 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -73,9 +73,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 { @@ -402,7 +399,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)) } @@ -411,7 +408,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 { @@ -578,8 +575,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 { @@ -628,6 +625,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 fd51d23794c2d3b39b4a5cd0a495a4399838846e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:10:19 +0000 Subject: [PATCH 61/66] 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 d5bf45d755bdeee3c6d00efd4f5bd6c48a9e6c33 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:11:13 +0000 Subject: [PATCH 62/66] 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 d5741449696..35ea7a4c86e 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -890,7 +890,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 c15ded0ae4e..f1b7e698288 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -468,11 +468,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 c69ce88b8fbbd16ff221d49be3eb854fc6796b05 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:15:27 +0000 Subject: [PATCH 63/66] 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 3922c6d78b24ce2b82d5af9b22f47abb4274510c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:23:35 +0000 Subject: [PATCH 64/66] 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 f1b7e698288..61872786fe6 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -360,7 +360,8 @@ func agentStatus(ctx context.Context, files *agentFiles, rec mcp.Entry, a *agent return agentStatusInstalled, loc.path, "", nil } -// 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 @@ -386,19 +387,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)) @@ -416,7 +409,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)) @@ -424,6 +417,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 1d27fd164d9..ba442cd18e7 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -1080,6 +1080,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 8eb4d40e2c72352c369039a3f682c3434460f7da Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:15:56 +0000 Subject: [PATCH 65/66] 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 b4db3695d35..5724abaa5ba 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -115,7 +115,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() {}, @@ -282,7 +282,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 }, } @@ -514,7 +514,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() @@ -554,7 +554,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 4a8c08cc50f2d6d4d4fcda1b1f94b96539193897 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 14:50:51 +0000 Subject: [PATCH 66/66] 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