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 ed892dd4ef5..1d58c9b50c8 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 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) Global Flags: --debug enable debug logging 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/.databrickscfg b/acceptance/cmd/aitools/mcp/list-check/.databrickscfg new file mode 100644 index 00000000000..18c13702ad5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-check/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-list-check 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..1471761a4d5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-check/output.txt @@ -0,0 +1,96 @@ + +=== 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_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 + +>>> [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 + 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 + +>>> [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], 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 + +=== A recorded binary that is gone is reported as missing + +>>> [CLI] aitools mcp list --check --output json +{ + "profiles": [ + { + "profile": "logfood", + "host": "[DATABRICKS_URL]", + "config_file": "[TEST_TMP_DIR]/.databrickscfg", + "status": "ok", + "servers": [ + { + "name": "genie_one_mcp-logfood", + "service": "system.ai.genie_one_mcp", + "host": "[DATABRICKS_URL]", + "sign_in_required": false, + "status": "ok", + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ], + "check": { + "token": "ok", + "binary": "failed" + } + }, + { + "name": "github-logfood", + "service": "system.ai.github", + "host": "[DATABRICKS_URL]", + "sign_in_required": true, + "status": "ok", + "agents": [ + { + "name": "claude-code", + "path": "[TEST_TMP_DIR]/home/.claude.json", + "status": "installed" + } + ], + "check": { + "token": "ok", + "sign_in": "ok", + "binary": "failed" + } + } + ] + } + ], + "unmanaged": [], + "warnings": [] +} + +=== 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], 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, 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 new file mode 100644 index 00000000000..ca96cde87df --- /dev/null +++ b/acceptance/cmd/aitools/mcp/list-check/script @@ -0,0 +1,28 @@ +# Isolate HOME so the agent config files and the state file land in this test directory. +sethome home +mkdir -p home/.claude +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +# The services of system.ai, created in the fake workspace. The fake 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"}' &> 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 + +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" +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" +# 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" +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..dc8d760a4d3 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-login-flips-active 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..b78235de597 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/output.txt @@ -0,0 +1,89 @@ + +=== 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 +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" +} + +=== 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 .mcp | [.servers[] | {name, service, profile, agents: (.agents | keys)}] home/.databricks/aitools/skills/.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..30652b9abe4 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/script @@ -0,0 +1,39 @@ +# 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. +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"}' &> 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 + +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 +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 + +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 +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 +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..15c152cdbc1 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/login-flips-active/test.toml @@ -0,0 +1,7 @@ +# 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" + +# 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 62e6f43e2b8..2e792a82b9c 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -758,14 +758,25 @@ 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 { + 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/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.go b/cmd/aitools/mcp.go index b275848610e..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 @@ -39,6 +40,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..35ea7a4c86e 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" @@ -19,10 +20,10 @@ 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" + "github.com/databricks/databricks-sdk-go/config" "github.com/spf13/cobra" ) @@ -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. @@ -57,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 { @@ -98,6 +111,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 +179,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 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 } @@ -244,19 +261,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 { @@ -278,6 +295,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} @@ -293,17 +311,22 @@ 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 { + 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. 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. @@ -358,16 +381,21 @@ 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 } + 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 } - 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 @@ -376,9 +404,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) } @@ -835,6 +863,49 @@ 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, 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 := 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) + 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) + } + } + return errors.Join(errs...) +} + +// 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)) +} + +// 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. @@ -909,15 +980,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 } } @@ -951,19 +1019,15 @@ 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 { - cmdio.LogString(ctx, fmt.Sprintf("%s needs sign-in. Run: databricks aitools mcp login %s --profile %s", server.Service, shellquote.BashArg(server.Service), shellquote.BashArg(req.profile))) + if server.SignInRequired && installedAnywhere(server) && !req.login { + 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 1cad4af0ea6..841feaf986b 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 '_'`, @@ -782,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 d80972d75cc..61872786fe6 100644 --- a/cmd/aitools/mcp_list.go +++ b/cmd/aitools/mcp_list.go @@ -6,9 +6,12 @@ import ( "errors" "fmt" "maps" + "os" + "os/exec" "path/filepath" "slices" "strings" + "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" @@ -18,9 +21,16 @@ 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/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" ) +// 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. const ( @@ -73,12 +83,31 @@ 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. 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. entry mcp.Entry drift *mcp.Drift } +// 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 { + Skipped string `json:"skipped,omitempty"` + Token string `json:"token,omitempty"` + SignIn string `json:"sign_in,omitempty"` + Binary string `json:"binary,omitempty"` +} + type mcpListAgent struct { Name string `json:"name"` Path string `json:"path"` @@ -99,10 +128,11 @@ 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 { - 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 +143,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 +175,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 +360,189 @@ func agentStatus(ctx context.Context, files *agentFiles, rec mcp.Entry, a *agent return agentStatusInstalled, loc.path, "", nil } +// 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 +// 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] + 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 + // 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, login, out) + }) + if err != nil { + out.Warnings = append(out.Warnings, fmt.Sprintf("profile %s: %s", p.Profile, err)) + } + } +} + +// 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) { + 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)) + } + for j := range p.Servers { + s := &p.Servers[j] + if skipReason(*s) != "" { + continue + } + 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 { + s.Check.SignIn = statusUnknown + } + continue + } + 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)) + } + } +} + +// 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) { + 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 +} + +// 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, 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 { + if !recorded { + return "", err + } + return statusUnknown, err + } + if !svc.SignInRequired { + return "", nil + } + state, err := mcp.CredentialState(ctx, w, service) + if err != nil { + return statusUnknown, describeRequestError(profileName, pinnedFile, 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. 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)) + return nil +} + +// 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 statusFailed + } + info, err := os.Stat(path) + if err != nil || info.IsDir() { + return statusFailed + } + 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 +588,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 token := profileToken(p); token != "" { + header += " token " + 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,28 +639,76 @@ func profileHeader(p mcpListProfile) string { return fmt.Sprintf("Profile %s (%s)", p.Profile, p.Host) } -// serverHint tells the user how to repair a server whose profile no longer serves it. The remove command names the -// server, so that the other servers of the profile are kept. +// 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 { + if c == nil { + return "" + } + if c.Skipped != "" { + return skippedText(c.Skipped) + } + 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 == statusFailed { + parts = append(parts, "binary missing") + } + 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. 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 "" } 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 c09a9b36202..ba442cd18e7 100644 --- a/cmd/aitools/mcp_list_test.go +++ b/cmd/aitools/mcp_list_test.go @@ -2,11 +2,16 @@ package aitools import ( "bytes" + "context" "encoding/json" + "net/http" + "net/http/httptest" "os" + "os/exec" "path/filepath" "strings" "testing" + "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" @@ -15,8 +20,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" ) @@ -623,6 +630,45 @@ 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) { + 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) { @@ -867,3 +913,505 @@ 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, 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 "/api/2.0/preview/scim/v2/Me": + _, _ = w.Write([]byte(`{"userName":"user@example.test"}`)) + 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") +} + +// 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) { + 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, 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]) +} + +// 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) +} + +// 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) { + 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) { + 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") +} + +// 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 { + 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: 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 { + 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, statusFailed, binaryStatus(missing, found)) + assert.Equal(t, statusOK, binaryStatus("databricks", found)) + 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, statusFailed, binaryStatus("", found)) + assert.Equal(t, statusFailed, binaryStatus(t.TempDir(), 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 + recorded bool + want string + warns bool + }{ + {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) { + 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", tc.recorded) + assert.Equal(t, tc.want, status) + if tc.warns { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + } +} + +// 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.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"}`)) + 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) + }) +} + +// 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, 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]) + 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)) + // 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="+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") +} diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go new file mode 100644 index 00000000000..f7d49e72262 --- /dev/null +++ b/cmd/aitools/mcp_login.go @@ -0,0 +1,268 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "io" + "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/env" + "github.com/databricks/cli/libs/shellquote" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" + "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" + 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 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"` +} + +// 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 { + 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) + + 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 errors.Join(runErr, err) + } + if runErr != nil { + return root.ErrAlreadyPrinted + } + 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) +} + +// 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) + } + // 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 + } + 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 + } + entry, err := mcp.CheckBinding(ctx, state, service, profileName, configFile) + if err != nil { + return err + } + // A server pinned to a config file names that file in the hints, as add does. + pinnedFile := mcp.PinnedFile(entry, configFile) + w, err := profileClient(ctx, profileName, configFile, pinnedFile) + if err != nil { + return err + } + out.Host = w.Config.Host + 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, 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, pinnedFile, err) + } + 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 { + return err + } + if !svc.SignInRequired { + out.State = signInStateNotRequired + fmt.Fprintf(errOut, "%s does not need sign-in.\n", svc.FullName) + return nil + } + + // 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 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, urlShown, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, url, svc) + out.State = state + // The URL is named when it was printed. A sign-in that failed before its page is printed names none. + if urlShown { + out.URL = url + } + 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 + // 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 = signOutStateDeleted + fmt.Fprintf(errOut, "Signed out of %s on profile %s.\n", out.Service, out.Profile) + return nil + } + 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 +} + +// 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)) +} + +// 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 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, + Open: browser.OpenOutputToStderr, + }) + timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) + switch { + case timedOut: + 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, 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 "", urlShown, describeRequestError(profileName, pinnedFile, err) + } + + 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, urlShown, nil +} diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go new file mode 100644 index 00000000000..843d8e98678 --- /dev/null +++ b/cmd/aitools/mcp_login_test.go @@ -0,0 +1,518 @@ +package aitools + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "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" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMcpSignInRequiresFullServiceName(t *testing.T) { + // A name with a character that is URL syntax, or a control character, is refused before any request: the request + // path would name another resource. + for _, service := range []string{"github", "system.github", "system..github", "a.b.c.d", "system.ai.git#hub", "system.ai.git?hub", "system.ai.git/hub", "system.ai.git%hub", "system.ai.git\nhub"} { + 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, 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)) + }) + } +} + +// 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, 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) +} + +// 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) { + 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 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" + 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}, + } + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + var errOut bytes.Buffer + err = signInInstalled(ctx, &errOut, w, req, plans, servers) + require.Error(t, err) + assert.Equal(t, []string{githubPath, geniePath}, reads) + // 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") +} + +// 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) { + 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/") { + 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 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) { + 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") +} + +// 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) { + 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") + assert.NotContains(t, output, "needs sign-in") +} + +// 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, +// 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) { + 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"`) +} + +// 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.AitoolsErrorCategoryStateInvalid, 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) { + 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") +} diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index dd5446eb05e..b8fe267b1a5 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) } @@ -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 a49f8f85693..5724abaa5ba 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 refreshRejected }, + 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) { @@ -197,7 +213,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) @@ -266,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 }, } @@ -298,7 +314,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}) @@ -498,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() @@ -538,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)) @@ -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/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/binding.go b/libs/aitools/mcp/binding.go index a886f378f23..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" @@ -86,7 +87,7 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) if entry == nil { fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s\n", opts.Service, opts.Profile, cfg.Host) } - if err := AuthenticateBound(ctx, cfg, opts.Profile); err != nil { + if err := AuthenticateBound(ctx, cfg, opts.Profile, PinnedFile(entry, configFile)); err != nil { return nil, nil, err } return cfg, entry, nil @@ -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) @@ -110,8 +129,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,11 +139,21 @@ 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 } +// 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 @@ -181,11 +211,17 @@ 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. 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 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 @@ -236,9 +272,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..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)) } @@ -877,3 +879,19 @@ 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") +} + +// 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")) +} diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index 6c448d2543b..485d3c7cb0d 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,20 @@ 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 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) +} + +// 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" @@ -55,6 +71,20 @@ 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, 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: serviceResourceName(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 +} + // ListErrorKind names a listing failure. type ListErrorKind string @@ -106,13 +136,23 @@ 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) +} + +// 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 { - 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 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.go b/libs/aitools/mcp/login.go new file mode 100644 index 00000000000..18e71d9c9bd --- /dev/null +++ b/libs/aitools/mcp/login.go @@ -0,0 +1,187 @@ +package mcp + +import ( + "context" + "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" +) + +// SignInPollInterval and SignInWaitTimeout are the polling settings of provider sign-in. +const ( + SignInPollInterval = 2 * time.Second + SignInWaitTimeout = 5 * time.Minute +) + +// 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") + +// 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 string + Poll time.Duration + Timeout time.Duration + 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: serviceResourceName(service), + }) + if IsNotFound(err) { + 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, 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. 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, false, err + } + switch state { + case catalog.ProvisioningInfoStateActive: + 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, 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) + 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) { + done := make(chan error, 1) + go func() { done <- opts.Open(ctx, url) }() + opened = done + } + fmt.Fprintln(out, "Waiting for sign-in...") + + // 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) + defer timer.Stop() + 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, true, waitEnded(ctx, lastCheck) + 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: + } + + state, err := CredentialState(waitCtx, w, service) + if err != nil { + if waitCtx.Err() != nil { + return false, true, waitEnded(ctx, lastCheck) + } + if !transientPollError(err) { + return false, true, err + } + lastCheck = err + continue + } + lastCheck = nil + // Other states (PROVISIONING, UPDATING, ...) mean the login has not completed yet. + switch state { + case catalog.ProvisioningInfoStateActive: + return false, true, nil + case catalog.ProvisioningInfoStateFailed: + return false, true, ErrSignInFailed + case catalog.ProvisioningInfoStateDegraded, catalog.ProvisioningInfoStateDeleting, + catalog.ProvisioningInfoStateProvisioning, catalog.ProvisioningInfoStateUpdating: + // The login has not completed yet. + } + } +} + +// 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 &SignInTimeoutError{LastCheck: lastCheck} +} + +// 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: serviceResourceName(service), + }) + if IsNotFound(err) { + 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..d4c7d8115c9 --- /dev/null +++ b/libs/aitools/mcp/login_test.go @@ -0,0 +1,565 @@ +package mcp + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync" + "sync/atomic" + "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" + "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 + // 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) { + 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: + 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)] + 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) + } +} + +// 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") +} + +// 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 +} + +// 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(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() + return s.deleted +} + +// 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) + + // 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 <- url + return nil + }, + }) + require.NoError(t, err) + + wantURL := w.Config.Host + "/mcp-service-login?name=system.ai.github&o=123" + 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) +} + +func TestSignInAlreadyActiveDoesNotOpenBrowser(t *testing.T) { + s := &loginTestServer{states: []string{"ACTIVE"}} + w := loginTestClient(t, s) + + var out bytes.Buffer + alreadySignedIn, _, err := SignIn(t.Context(), w, loginTestSignInURL(w), 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.True(t, alreadySignedIn) + 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, loginTestSignInURL(w), 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(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + Open: noopOpen, + Poll: time.Millisecond, + Timeout: 30 * time.Millisecond, + }) + timeout := requireSignInTimeout(t, err) + assert.NoError(t, timeout.LastCheck) + 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) + + // 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{ + Poll: time.Millisecond, + Timeout: time.Minute, + Open: func(context.Context, string) error { + cancel() + return nil + }, + }) + require.ErrorIs(t, err, context.Canceled) + _, timedOut := errors.AsType[*SignInTimeoutError](err) + assert.False(t, timedOut) +} + +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{ + Open: noopOpen, + Poll: time.Millisecond, + Timeout: 10 * time.Second, + }) + 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.NoError(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) + + 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) +} + +// 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) + + deletedBeforeOpen := make(chan bool, 1) + _, _, 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 { + deletedBeforeOpen <- s.isDeleted() + return nil + }, + }) + require.NoError(t, err) + 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.Context()), 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. +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{ + 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(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 200 * time.Millisecond, + Open: noopOpen, + }) + _ = requireSignInTimeout(t, err) + 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) + }) + } +} + +// 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) +} + +// 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() { + _, _, 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 { + <-release + return nil + }, + }) + done <- err + }() + 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) { + 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) + + _, _, err := SignIn(browserOpens(t.Context()), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + Poll: 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") +} diff --git a/libs/browser/browser.go b/libs/browser/browser.go index 7c0763d6fe7..ca6ecfd0f02 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -10,7 +10,8 @@ package browser import ( "context" "fmt" - "io" + "os" + "sync" browserpkg "github.com/pkg/browser" @@ -30,15 +31,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,23 +60,55 @@ 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 +// 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 + +// 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 + // 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 - return browserpkg.OpenURL(url) + browserpkg.Stderr = devNull + if stdoutToStderr { + browserpkg.Stdout = os.Stderr + } + return openDefaultURL(url) } -func openWithCommand(ctx context.Context, browserCmd, url string) error { +// 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 { return err } - e.WithInheritOutput() + if stdoutToStderr { + e.WithInheritOutputToStderr() + } else { + e.WithInheritOutput() + } cmd, err := e.StartCommand(ctx, fmt.Sprintf("%q %q", browserCmd, url)) if err != nil { return err diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index 5739440716e..00844b36f52 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -1,7 +1,17 @@ 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" @@ -36,3 +46,188 @@ 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") } + +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) { + 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() +} + +// 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") + 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 +// 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 + defer func() { os.Stdout, os.Stderr = prevStdout, prevStderr }() + ctx := env.Set(t.Context(), "BROWSER", os.Args[0]) + err = open(ctx, "https://example.com/login") + + 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 +} + +// 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") + }) +} + +// 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 +// 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. +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")) + _, 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 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", "") + replaceDefaultLauncher(t, func(string) error { return nil }) + 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..afb163a3f3b 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. 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 +} + 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() 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) diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index 5e8068b6d02..3dfa5456562 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -247,6 +247,9 @@ type FakeWorkspace struct { RegisteredModels map[string]catalog.RegisteredModelInfo ModelServices map[string]catalog.ModelService McpServices map[string]catalog.McpService + // 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 // RegisteredModelVersions counts the versions registered on each model, keyed by its full name. @@ -544,6 +547,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..0d15cf34c70 100644 --- a/libs/testserver/mcp_services.go +++ b/libs/testserver/mcp_services.go @@ -106,3 +106,24 @@ 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. 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}, + } + s.McpUserCredentials[service] = cred + return Response{ + Body: cred, + } +} 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") +}