diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/.databrickscfg b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/.databrickscfg new file mode 100644 index 00000000000..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/out.test.toml b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt new file mode 100644 index 00000000000..4b1318c2b59 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt @@ -0,0 +1,43 @@ + +=== Before sign-in, tools/list answers with one tool, authenticate; it opened no sign-in + +>>> jq -c select(.id==2) | [.result.tools[].name] serve.stdout.txt +["authenticate"] + +>>> cat stderr-before-authenticate.txt +No install record for system.ai.github on profile "logfood"; using its current host [DATABRICKS_URL] + +=== authenticate waits for the sign-in, then answers and asks the agent to re-list + +>>> jq -c select(.id==3) | .result | {isError, text: .content[0].text} serve.stdout.txt +{"isError":false,"text":"Signed in to system.ai.github on profile logfood. Tools are available now; retry the user's request in this turn."} + +>>> jq -c select(.method=="notifications/tools/list_changed") serve.stdout.txt +{"jsonrpc":"2.0","method":"notifications/tools/list_changed"} + +>>> cat serve.stderr.txt +No install record for system.ai.github on profile "logfood"; using its current host [DATABRICKS_URL] +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. + +=== After the sign-in, tools/list passes through and tools/call reaches the service + +>>> jq -c select(.id==4) | [.result.tools[].name] serve.stdout.txt +["echo"] + +>>> jq -c select(.id==5) | .result.content[0].text serve.stdout.txt +"hello" + +=== Stdout holds only JSON-RPC lines + +>>> jq -c .jsonrpc serve.stdout.txt +"2.0" +"2.0" +"2.0" +"2.0" +"2.0" +"2.0" diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-after.ndjson b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-after.ndjson new file mode 100644 index 00000000000..57d5e4a4066 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-after.ndjson @@ -0,0 +1,2 @@ +{"jsonrpc":"2.0","id":4,"method":"tools/list"} +{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"echo","arguments":{"text":"hello"}}} diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-authenticate.ndjson b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-authenticate.ndjson new file mode 100644 index 00000000000..cc792544a79 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-authenticate.ndjson @@ -0,0 +1 @@ +{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"authenticate","arguments":{}}} diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-list.ndjson b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-list.ndjson new file mode 100644 index 00000000000..84c14278376 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-list.ndjson @@ -0,0 +1,3 @@ +{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test-client","version":"0.0.1"}}} +{"jsonrpc":"2.0","method":"notifications/initialized"} +{"jsonrpc":"2.0","id":2,"method":"tools/list"} diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script new file mode 100644 index 00000000000..36e9cebedf5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script @@ -0,0 +1,43 @@ +# Isolate HOME so the proxy finds no state file and no agent config, as on a fresh machine. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg +# No browser: the proxy prints the sign-in URL and does not open anything. +export BROWSER=none +# The sign-in is polled every 2 s, so the waits below need more attempts than the default. +export RETRY_MAX_ATTEMPTS=60 + +# The fake gets the GitHub service with its OAuth connection; the caller has no credential for it yet. +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":"github"}}}}' &> LOG.create-github + +# Stdin stays open until the last request is answered: the proxy shuts down at EOF and cancels a call +# still in flight after 2 s. Each step waits for the previous answer, so the order is deterministic. +# The last steps wait for ids 4 and 5 too, so the proxy has answered them before stdin closes. +{ + cat requests-list.ndjson + retry --until '"id":2' cat serve.stdout.txt > /dev/null + cat serve.stderr.txt > stderr-before-authenticate.txt + cat requests-authenticate.ndjson + retry --until "Waiting for sign-in" cat serve.stderr.txt > /dev/null + MSYS_NO_PATHCONV=1 $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' &> LOG.sign-in-credential + retry --until '"id":3' cat serve.stdout.txt > /dev/null + cat requests-after.ndjson + retry --until '"id":4' cat serve.stdout.txt > /dev/null + retry --until '"id":5' cat serve.stdout.txt > /dev/null +} | $CLI aitools mcp serve --profile logfood --service system.ai.github --agent claude-code > serve.stdout.txt 2> serve.stderr.txt + +title "Before sign-in, tools/list answers with one tool, authenticate; it opened no sign-in" +trace jq -c 'select(.id==2) | [.result.tools[].name]' serve.stdout.txt +trace cat stderr-before-authenticate.txt + +title "authenticate waits for the sign-in, then answers and asks the agent to re-list" +trace jq -c 'select(.id==3) | .result | {isError, text: .content[0].text}' serve.stdout.txt +trace jq -c 'select(.method=="notifications/tools/list_changed")' serve.stdout.txt +trace cat serve.stderr.txt + +title "After the sign-in, tools/list passes through and tools/call reaches the service" +trace jq -c 'select(.id==4) | [.result.tools[].name]' serve.stdout.txt +trace jq -c 'select(.id==5) | .result.content[0].text' serve.stdout.txt + +title "Stdout holds only JSON-RPC lines" +trace jq -c '.jsonrpc' serve.stdout.txt diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/test.toml b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/test.toml new file mode 100644 index 00000000000..ebccbe7ade7 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/test.toml @@ -0,0 +1,3 @@ +# The script leaves the captured streams of the proxy and the snapshot of its stderr before authenticate in this +# directory. The output is the trace lines, so the captures are ignored. +Ignore = ["stderr-before-authenticate.txt", "serve.stdout.txt", "serve.stderr.txt"] diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 35ea7a4c86e..c37b20a7314 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -18,6 +18,7 @@ import ( "github.com/databricks/cli/libs/aitools/mcp/agentconfig" "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/auth" + "github.com/databricks/cli/libs/browser" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/telemetry/protos" @@ -879,7 +880,7 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp continue } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) - state, _, err := signIn(ctx, errOut, w, req.profile, req.pinned, url, plans[i].service) + state, _, _, err := signIn(ctx, errOut, w, req.pinned, url, plans[i].service, signInOptions{Profile: req.profile, Open: browser.OpenOutputToStderr}) server.SignIn = &addSignInOutput{State: state} if err != nil { server.SignIn.ErrorCategory = errorCategory(err) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index f7d49e72262..4459f93b77e 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -20,7 +20,10 @@ import ( ) // mcpLoginCommand is the command that signs a profile in to a service. The hints name it. -const mcpLoginCommand = "databricks aitools mcp login" +const ( + mcpLoginCommand = "databricks aitools mcp login" + mcpLogoutCommand = "databricks aitools mcp logout" +) // Sign-in states of `mcp login` and `mcp logout`, reported in JSON. const ( @@ -180,7 +183,7 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie return describeRequestError(out.Profile, pinnedFile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } url := mcp.SignInURL(out.Host, svc.FullName, workspaceID) - state, urlShown, err := signIn(ctx, errOut, w, out.Profile, pinnedFile, url, svc) + state, _, urlShown, err := signIn(ctx, errOut, w, pinnedFile, url, svc, signInOptions{Profile: out.Profile, Open: browser.OpenOutputToStderr}) 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 { @@ -218,6 +221,11 @@ func mcpLoginHint(service, profileName, pinnedFile string) string { return mcp.ConfigFileCommand(pinnedFile, mcpLoginCommand+" "+shellquote.BashArg(service)+" --profile "+shellquote.BashArg(profileName)) } +// mcpLogoutHint returns the command that removes profileName's sign-in to service. pinnedFile is as in mcpLoginHint. +func mcpLogoutHint(service, profileName, pinnedFile string) string { + return mcp.ConfigFileCommand(pinnedFile, mcpLogoutCommand+" "+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 { @@ -233,27 +241,38 @@ var ( 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{ +// signInOptions is what a caller of signIn sets: the profile named in the notice before the page, the page opener, and +// the poll callback. signIn adds the polling settings. +type signInOptions struct { + Profile string + Open func(ctx context.Context, url string) error + OnPoll func() +} + +// signIn runs the provider sign-in of svc for the profile named in opts and reports progress on errOut. It returns the +// sign-in state name, whether the caller was already signed in, whether url was printed, and the failure, which is +// categorized for JSON. `add --login` shares this. +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, pinnedFile, url string, svc mcp.Service, opts signInOptions) (state string, alreadySignedIn, urlShown bool, err error) { + profileName := opts.Profile + alreadySignedIn, urlShown, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ Profile: profileName, Poll: signInPollInterval, Timeout: signInWaitTimeout, - Open: browser.OpenOutputToStderr, + Open: opts.Open, + OnPoll: opts.OnPoll, }) timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { case timedOut: - return signInStateTimeout, urlShown, failure(categorySignInTimeout, err, + return signInStateTimeout, false, 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, + return signInStateFailed, false, 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) + return "", false, urlShown, describeRequestError(profileName, pinnedFile, err) } if alreadySignedIn { @@ -264,5 +283,5 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient if svc.Known != nil && svc.Known.Note != "" { fmt.Fprintln(errOut, svc.Known.Note) } - return signInStateActive, urlShown, nil + return signInStateActive, alreadySignedIn, urlShown, nil } diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 843d8e98678..33658f88cef 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -2,6 +2,7 @@ package aitools import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -344,7 +345,8 @@ func TestSignInTimeoutNamesTheLastFailedCheck(t *testing.T) { require.NoError(t, err) ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) - state, _, err := signIn(ctx, io.Discard, w, "logfood", "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", SignInRequired: true}) + state, _, _, err := signIn(ctx, io.Discard, w, "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", SignInRequired: true}, + signInOptions{Profile: "logfood", Open: func(context.Context, string) error { return nil }}) require.Error(t, err) assert.Equal(t, signInStateTimeout, state) _, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index b8fe267b1a5..f1531de44ca 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -21,8 +21,12 @@ import ( "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/aitools/mcp/proxy" "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/auth/u2m" + "github.com/databricks/cli/libs/browser" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" + "github.com/databricks/databricks-sdk-go/config/credentials" "github.com/databricks/databricks-sdk-go/httpclient" "github.com/spf13/cobra" "golang.org/x/oauth2" @@ -110,26 +114,175 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { return serveFailure(cmd, err, serveBindStatus) } - return serveProxy(cmd, proxy.Options{ - In: cmd.InOrStdin(), - Out: cmd.OutOrStdout(), - Err: cmd.ErrOrStderr(), - Endpoint: mcp.ServiceURL(cfg.Host, f.service), - Config: cfg, - Authenticate: cfg.Authenticate, - 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. 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 { + // A server pinned to a config file names that file in its hints, as add and login do. + pinnedFile := mcp.PinnedFile(entry, cfg.ConfigFile) ctx, stop := serveSignalContext(cmd.Context()) defer stop() - refresh := &refreshGuard{authenticate: opts.Authenticate, profile: opts.Profile, pinnedFile: pinnedFile, cancel: stop} + // The bridge and the sign-in client share the guard, so a refresh failure in either one stops the proxy. + refresh := &refreshGuard{authenticate: cfg.Authenticate, profile: profileName, pinnedFile: pinnedFile, cancel: stop} + + // The sign-in client is built only when serveChallenge reads the service or signs in. A server whose record says it + // needs no sign-in makes no call at start. + var w *databricks.WorkspaceClient + if entry == nil || entry.SignInRequired { + w, err = guardedClient(cfg, refresh) + if err != nil { + return serveFailure(cmd, err, 1) + } + } + challenge, err := serveChallenge(ctx, serveTarget{w: w, entry: entry, profile: profileName, service: f.service, pinnedFile: pinnedFile}, cmd.ErrOrStderr()) + if err != nil { + // A rejected token stops serve by cancelling ctx, so the refresh failure is checked before the signal, and its + // message is the one that names the login. + if refreshErr := refresh.failure(); refreshErr != nil { + return serveFailure(cmd, refreshErr, serveBindStatus) + } + if ctx.Err() != nil { + // A signal during the start ends serve as a shutdown does, with no line. + return nil + } + // serveChallenge fails only on an auth failure (see lookupAtStart), which exits serveBindStatus. + return serveFailure(cmd, err, serveBindStatus) + } + + return serveProxy(ctx, cmd, refresh, proxy.Options{ + In: cmd.InOrStdin(), + Out: cmd.OutOrStdout(), + Err: cmd.ErrOrStderr(), + Endpoint: mcp.ServiceURL(cfg.Host, f.service), + Config: cfg, + Profile: profileName, + Agent: serveAgent(f.agent, cmd.ErrOrStderr()), + Transport: serveTransport(cfg), + Challenge: challenge, + }) +} + +// guardedClient returns the client of the bound profile whose requests the refresh guard authenticates. The SDK calls +// the guard for each request the client makes, so a token that cannot be refreshed stops the proxy from any of them. The +// profile's config is read again for the client, since its own credentials are the guard's. +func guardedClient(cfg *config.Config, refresh *refreshGuard) (*databricks.WorkspaceClient, error) { + guarded, err := mcp.BoundConfig(cfg.Profile, cfg.ConfigFile) + if err != nil { + return nil, err + } + guarded.Credentials = guardCredentials{refresh: refresh} + return databricks.NewWorkspaceClient((*databricks.Config)(guarded)) +} + +// guardCredentials is the credentials strategy of the sign-in client: each request it makes is authenticated by the +// refresh guard. +type guardCredentials struct { + refresh *refreshGuard +} + +// Name implements config.CredentialsStrategy. +func (guardCredentials) Name() string { + return "aitools-mcp" +} + +// Configure implements config.CredentialsStrategy. +func (g guardCredentials) Configure(context.Context, *config.Config) (credentials.CredentialsProvider, error) { + return credentials.CredentialsProviderFn(g.refresh.authenticateCall), nil +} + +// serveTarget is the server that a serve session bridges: its service, the bound profile, the install record, and the +// config file it is pinned to. w is the profile's client, which must be guardedClient's. +type serveTarget struct { + w *databricks.WorkspaceClient + entry *mcp.Entry + profile string + service string + pinnedFile string +} + +// serveChallenge returns the sign-in handling of a service that needs provider sign-in, or nil for +// a pure passthrough. The state entry decides when there is one; otherwise the service is read once. A read that fails +// for a reason other than the credential does not stop the serve: the sign-in handling is on then, since it acts only on +// a challenge that names the service, and the failure is written to errOut. +func serveChallenge(ctx context.Context, t serveTarget, errOut io.Writer) (*proxy.Challenge, error) { + signInRequired := t.entry != nil && t.entry.SignInRequired + if t.entry == nil { + svc, err := lookupAtStart(ctx, t.w, t.profile, t.pinnedFile, t.service) + switch { + case err == nil: + signInRequired = svc.SignInRequired + case authFailed(err): + return nil, err + case ctx.Err() != nil: + // The serve is stopping, so the read's failure is its cancellation, which is not reported. + return nil, ctx.Err() + default: + fmt.Fprintf(errOut, "%v; serving it with sign-in handling on\n", err) + signInRequired = true + } + } + if !signInRequired { + return nil, nil + } + return &proxy.Challenge{ + Service: t.service, + Profile: t.profile, + Host: t.w.Config.Host, + Label: mcp.ProviderLabel(t.service), + LoginCommand: mcpLoginHint(t.service, t.profile, t.pinnedFile), + LogoutCommand: mcpLogoutHint(t.service, t.profile, t.pinnedFile), + PinnedFile: t.pinnedFile, + SignIn: serveSignIn(t), + }, nil +} + +// lookupAtStart reads a service for a server with no install record. The read is bounded by profileCheckTimeout, as the +// checks of list --check are, so that a host that does not answer does not hold up the start for the SDK's retry budget. +func lookupAtStart(ctx context.Context, w *databricks.WorkspaceClient, profileName, pinnedFile, service string) (mcp.Service, error) { + ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) + defer cancel() + svc, err := mcp.GetService(ctx, w, service) + if err != nil { + err = describeRequestError(profileName, pinnedFile, err) + return mcp.Service{}, fmt.Errorf("cannot read MCP service %s: %w", service, err) + } + return svc, nil +} + +// serveOpenPage opens the sign-in page for serve. Its stdin is the protocol stream, so the browser command gets none. Tests +// replace it. +var serveOpenPage = browser.OpenOutputToStderrWithoutStdin + +// serveSignIn is the authenticate tool's sign-in, the one `login` runs. Its progress goes to the request's stderr, which the +// proxy serializes, since stdout carries the protocol. The page opens only when req.Open is set, so the proxy decides how +// often the browser opens. w must be guardedClient's. +func serveSignIn(t serveTarget) proxy.SignInFunc { + w, profileName, service, pinnedFile := t.w, t.profile, t.service, t.pinnedFile + // The proxy runs one sign-in at a time, so the workspace ID is resolved by the first sign-in that needs it. + var workspaceID string + return func(ctx context.Context, req proxy.SignInRequest) (bool, bool, error) { + if workspaceID == "" { + id, err := auth.ResolveWorkspaceID(ctx, w) + if err != nil { + return false, false, describeRequestError(profileName, pinnedFile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) + } + workspaceID = id + } + url := mcp.SignInURL(w.Config.Host, service, workspaceID) + // The page opens only when req.Open is set. Otherwise the sign-in only waits, and the proxy's progress names the page. + var openPage func(context.Context, string) error + if req.Open { + // The browser command must outlive the request: the request's context would kill it when the call is cancelled, and + // with it the page the user is on. + openPage = func(pageCtx context.Context, url string) error { + return serveOpenPage(context.WithoutCancel(pageCtx), url) + } + } + svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} + _, alreadySignedIn, urlShown, err := signIn(ctx, req.Stderr, w, pinnedFile, url, svc, signInOptions{Profile: profileName, Open: openPage, OnPoll: func() { req.Progress(url) }}) + return alreadySignedIn, urlShown, err + } +} + +// serveProxy bridges opts to the service until stdin closes or ctx is done. The requests are authenticated by refresh, so +// that a refresh failure stops the proxy; see proxyResult. +func serveProxy(ctx context.Context, cmd *cobra.Command, refresh *refreshGuard, opts proxy.Options) error { opts.Authenticate = refresh.authenticateRequest return proxyResult(cmd, refresh, proxy.Run(ctx, opts)) } @@ -181,6 +334,21 @@ func serveAgent(name string, stderr io.Writer) string { // binding or sign-in from the other failures, which exit 1. const serveBindStatus = 2 +// serveStartStatus is the exit status of a failure while serve starts. A credential the profile cannot provide exits +// with serveBindStatus, as the other binding and auth failures do; any other failure exits 1. +func serveStartStatus(err error) int { + if authFailed(err) { + return serveBindStatus + } + return 1 +} + +// authFailed reports whether err is a credential the profile cannot provide. +func authFailed(err error) bool { + be, ok := errors.AsType[*mcp.BindError](err) + return ok && be.Category == mcp.BindAuthFailed +} + // serveFailure prints err to stderr and returns it with the exit status. Execute does not print it again, // so the message is written exactly once. func serveFailure(cmd *cobra.Command, err error, status int) error { @@ -193,11 +361,11 @@ func exitAlreadyPrinted(err error, status int) error { return errors.Join(root.ErrAlreadyPrinted, &root.ExitStatusError{Status: status, Err: err}) } -// refreshGuard wraps the profile's authenticate for the bridge. A rejected token (see credentialRejected) fails that -// request with an error that names the profile and its login command, then stops the proxy, which exits 2. Later -// requests fail at once, without asking for a token again. A transient failure fails only its request, and the next -// request asks for a token again. A request whose own context ended does not stop it, nor does the session DELETE at -// shutdown, which is skipped after a rejection. +// refreshGuard wraps the profile's authenticate for the bridge and for the sign-in client. A rejected token (see +// credentialRejected) fails that request with an error that names the profile and its login command, then stops the +// proxy, which exits 2. Later requests fail at once, without asking for a token again. A transient failure fails only its +// request, and the next request asks for a token again. A request whose own context ended does not stop it, nor does the +// bridge's session DELETE at shutdown, which is skipped after a rejection. type refreshGuard struct { authenticate func(*http.Request) error profile string @@ -218,19 +386,34 @@ func (g *refreshGuard) authenticateRequest(r *http.Request) error { return g.stoppedError() } err := g.authenticate(r) - if err == nil { - return nil + if err == nil || r.Context().Err() != nil || r.Method == http.MethodDelete { + return err + } + return g.fail(err) +} + +// authenticateCall is the sign-in client's authentication. Unlike authenticateRequest it does not skip a DELETE, since the +// sign-in's own DELETE is a call that needs its credential. +func (g *refreshGuard) authenticateCall(r *http.Request) error { + if g.failure() != nil { + return g.stoppedError() } - if r.Context().Err() != nil || r.Method == http.MethodDelete { + err := g.authenticate(r) + if err == nil || r.Context().Err() != nil { return err } + return g.fail(err) +} + +// fail records err, a token that cannot be refreshed, and returns the error for the request that hit it. The first failure +// stops the proxy. 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. +func (g *refreshGuard) fail(err error) error { if !credentialRejected(err) { // A failure that is not a rejection, such as a network blip, says nothing about the credential: a login would // not fix it, and the next request asks for a token again, so the message names neither. return fmt.Errorf("could not get a token for Databricks profile \"%s\": %w; the next request tries again", g.profile, err) } - // 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, g.pinnedFile, err)) g.mu.Lock() first := g.err == nil diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 5724abaa5ba..17262c8ba2e 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -26,6 +26,7 @@ import ( "github.com/databricks/cli/libs/aitools/mcp/proxy" "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/httpclient" "github.com/stretchr/testify/assert" @@ -33,6 +34,12 @@ import ( "golang.org/x/oauth2" ) +// testService and testProfile are the service and the profile of the serve tests. +const ( + testService = "system.ai.github" + testProfile = "logfood" +) + func TestMcpServeIsHidden(t *testing.T) { assert.True(t, NewMcpServeCmd().Hidden) } @@ -43,7 +50,7 @@ func TestServeParsesArgvThatAddWrites(t *testing.T) { parent := newMCPTestRoot() serve, _, err := parent.Find([]string{"mcp", "serve"}) require.NoError(t, err) - argv := []string{mcpconfig.ProfileFlag, "logfood", mcpconfig.ServiceFlag, "system.ai.github", mcpconfig.AgentFlag, "claude-code"} + argv := []string{mcpconfig.ProfileFlag, testProfile, mcpconfig.ServiceFlag, testService, mcpconfig.AgentFlag, "claude-code"} require.NoError(t, serve.ParseFlags(argv)) profile, err := serve.Flags().GetString("profile") @@ -52,14 +59,14 @@ func TestServeParsesArgvThatAddWrites(t *testing.T) { require.NoError(t, err) agent, err := serve.Flags().GetString("agent") require.NoError(t, err) - assert.Equal(t, []string{"logfood", "system.ai.github", "claude-code"}, []string{profile, service, agent}) + assert.Equal(t, []string{testProfile, testService, "claude-code"}, []string{profile, service, agent}) } func TestRefreshGuardPassesThroughWhileTokensRefresh(t *testing.T) { cancelled := false g := &refreshGuard{ authenticate: func(*http.Request) error { return nil }, - profile: "logfood", + profile: testProfile, cancel: func() { cancelled = true }, } @@ -77,7 +84,7 @@ func TestRefreshGuardStopsProxyOnFirstFailure(t *testing.T) { calls++ return cause }, - profile: "logfood", + profile: testProfile, cancel: cancel, } @@ -99,7 +106,7 @@ func TestRefreshGuardStopsLaterRequestsWithoutAskingAgain(t *testing.T) { calls++ return cause }, - profile: "logfood", + profile: testProfile, cancel: func() {}, } require.Error(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) @@ -116,7 +123,7 @@ func TestRefreshGuardStopsLaterRequestsWithoutAskingAgain(t *testing.T) { func TestRefreshGuardNamesPinnedConfigFile(t *testing.T) { g := &refreshGuard{ authenticate: func(*http.Request) error { return refreshRejected }, - profile: "logfood", + profile: testProfile, pinnedFile: "/work/cfg/logfood.cfg", cancel: func() {}, } @@ -213,7 +220,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(testProfile, "", errors.New(refreshTokenExpired)) err := serveFailure(cmd, cause, serveBindStatus) @@ -264,7 +271,7 @@ func TestRefreshGuardIgnoresRequestThatEnded(t *testing.T) { cancelled := false g := &refreshGuard{ authenticate: func(r *http.Request) error { return r.Context().Err() }, - profile: "logfood", + profile: testProfile, cancel: func() { cancelled = true }, } ctx, cancel := context.WithCancel(t.Context()) @@ -283,7 +290,7 @@ func TestRefreshGuardIgnoresSessionDelete(t *testing.T) { cancelled := false g := &refreshGuard{ authenticate: func(*http.Request) error { return refreshRejected }, - profile: "logfood", + profile: testProfile, cancel: func() { cancelled = true }, } req, err := http.NewRequestWithContext(t.Context(), http.MethodDelete, "https://workspace.test/mcp", nil) @@ -317,7 +324,7 @@ func TestMcpServeRejectsServiceWithoutThreeParts(t *testing.T) { 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}) + parent.SetArgs([]string{"mcp", "serve", "--profile", testProfile, "--service", service}) err := parent.ExecuteContext(cmdio.MockDiscard(t.Context())) require.ErrorContains(t, err, "--service needs a full service name such as system.ai.github") assert.Equal(t, 1, root.ExitStatus(err)) @@ -336,7 +343,7 @@ func TestMcpServeStateReadFailureExitsTwo(t *testing.T) { parent := newMCPTestRoot() var stderr bytes.Buffer parent.SetErr(&stderr) - parent.SetArgs([]string{"mcp", "serve", "--profile", "logfood", "--service", "system.ai.github"}) + parent.SetArgs([]string{"mcp", "serve", "--profile", testProfile, "--service", testService}) err = parent.ExecuteContext(cmdio.MockDiscard(t.Context())) require.ErrorIs(t, err, root.ErrAlreadyPrinted) assert.Equal(t, 2, root.ExitStatus(err)) @@ -353,7 +360,7 @@ func TestRefreshGuardSkipsSessionDeleteAfterFailure(t *testing.T) { asked++ return refreshRejected }, - profile: "logfood", + profile: testProfile, cancel: func() {}, } require.Error(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) @@ -372,7 +379,7 @@ func TestProxyResultRefreshFailureWinsOverStdinError(t *testing.T) { cause := refreshRejected g := &refreshGuard{ authenticate: func(*http.Request) error { return cause }, - profile: "logfood", + profile: testProfile, cancel: func() {}, } require.Error(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) @@ -391,7 +398,7 @@ func TestProxyResultRefreshFailureWinsOverStdinError(t *testing.T) { // TestProxyResultReportsStdinErrorWithoutRefreshFailure checks that without a refresh failure, a stdin error is printed // and exits 1. func TestProxyResultReportsStdinErrorWithoutRefreshFailure(t *testing.T) { - g := &refreshGuard{profile: "logfood"} + g := &refreshGuard{profile: testProfile} cmd := NewMcpServeCmd() var stderr bytes.Buffer cmd.SetErr(&stderr) @@ -506,15 +513,18 @@ func TestServeProxyTransientRefreshFailureKeepsServing(t *testing.T) { cmd.SetContext(t.Context()) cmd.SetErr(&stderr) - err := serveProxy(cmd, proxy.Options{ + ctx, stop := context.WithCancel(t.Context()) + defer stop() + refresh := &refreshGuard{authenticate: failsToolsListOnce(errors.New("dial tcp: i/o timeout")), profile: "logfood", cancel: stop} + err := serveProxy(ctx, cmd, refresh, proxy.Options{ In: in, Out: &stdout, Err: &stderr, Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), Config: &config.Config{Host: server.URL}, - Authenticate: failsToolsListOnce(errors.New("dial tcp: i/o timeout")), + Authenticate: refresh.authenticateRequest, Profile: "logfood", - }, "") + }) require.NoError(t, err) out := stdout.String() @@ -546,15 +556,18 @@ func TestServeProxyRefreshRejectionStopsAfterTransientFailure(t *testing.T) { return expiresOnToolsList(r) } - err := serveProxy(cmd, proxy.Options{ + ctx, stop := context.WithCancel(t.Context()) + defer stop() + refresh := &refreshGuard{authenticate: rejected, profile: "logfood", cancel: stop} + err := serveProxy(ctx, cmd, refresh, proxy.Options{ In: in, Out: &stdout, Err: &stderr, Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), Config: &config.Config{Host: server.URL}, - Authenticate: rejected, + Authenticate: refresh.authenticateRequest, Profile: "logfood", - }, "") + }) require.ErrorIs(t, err, root.ErrAlreadyPrinted) assert.Equal(t, 2, root.ExitStatus(err)) @@ -594,15 +607,18 @@ func runRefreshFailureSession(t *testing.T, end error) (string, bool, error) { cmd := NewMcpServeCmd() cmd.SetContext(t.Context()) cmd.SetErr(&stderr) - err := serveProxy(cmd, proxy.Options{ + ctx, stop := context.WithCancel(t.Context()) + defer stop() + refresh := &refreshGuard{authenticate: expiresOnToolsList, profile: testProfile, cancel: stop} + err := serveProxy(ctx, cmd, refresh, proxy.Options{ In: in, Out: &stdout, Err: &stderr, - Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), + Endpoint: mcp.ServiceURL(server.URL, testService), Config: &config.Config{Host: server.URL}, - Authenticate: expiresOnToolsList, - Profile: "logfood", - }, "") + Authenticate: refresh.authenticateRequest, + Profile: testProfile, + }) return stderr.String(), deleted.Load(), err } @@ -755,3 +771,443 @@ func waitForChildLine(t *testing.T, lines <-chan string, want string) { } } } + +func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { + const githubPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github" + const githubJSON = `{"name":"mcp-services/system.ai.github","config":{"source_connection":{"name":"connections/github",` + + `"options":{"oauth_provider":"github"}}}}` + var lookups atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != githubPath { + http.NotFound(w, r) + return + } + lookups.Add(1) + _, _ = io.WriteString(w, githubJSON) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, entry: &mcp.Entry{SignInRequired: true}, profile: testProfile, service: testService}, io.Discard) + require.NoError(t, err) + require.NotNil(t, challenge) + assert.Equal(t, testService, challenge.Service) + assert.Equal(t, testProfile, challenge.Profile) + assert.Equal(t, server.URL, challenge.Host) + assert.Equal(t, "GitHub", challenge.Label) + assert.Equal(t, int32(0), lookups.Load(), "an install record answers without a lookup") + + challenge, err = serveChallenge(t.Context(), serveTarget{w: w, entry: &mcp.Entry{SignInRequired: false}, profile: testProfile, service: testService}, io.Discard) + require.NoError(t, err) + assert.Nil(t, challenge) + assert.Equal(t, int32(0), lookups.Load()) + + challenge, err = serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) + require.NoError(t, err) + assert.NotNil(t, challenge) + assert.Equal(t, int32(1), lookups.Load(), "without a record the service is read once") +} + +// TestServeChallengeHintNamesPinnedFileAndQuotesProfile checks that a server pinned to a config file names that file in +// the login command its hints print, and that a profile name with a space is quoted for a shell. +func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: "https://workspace.test", Token: "test-token"}) + require.NoError(t, err) + const pinned = "/work/cfg/my file.cfg" + + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, entry: &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, profile: "my profile", service: testService, pinnedFile: pinned}, io.Discard) + require.NoError(t, err) + require.NotNil(t, challenge) + assert.Equal(t, "DATABRICKS_CONFIG_FILE='/work/cfg/my file.cfg' databricks aitools mcp login system.ai.github --profile 'my profile'", challenge.LoginCommand) + assert.Equal(t, "DATABRICKS_CONFIG_FILE='/work/cfg/my file.cfg' databricks aitools mcp logout system.ai.github --profile 'my profile'", challenge.LogoutCommand) +} + +// failingGuard returns a refresh guard whose token cannot be refreshed. cancel stops the serve, as serve's signal +// context does. +func failingGuard(cancel context.CancelFunc) *refreshGuard { + return &refreshGuard{ + authenticate: func(*http.Request) error { return refreshRejected }, + profile: testProfile, + cancel: cancel, + } +} + +// serviceServer answers the service read of the startup lookup and the credential read of the sign-in with status. +// Its body is the JSON of the answer. +func serviceServer(t *testing.T, status int, body string) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { + rw.Header().Set("Content-Type", "application/json") + rw.WriteHeader(status) + _, _ = io.WriteString(rw, body) + })) + t.Cleanup(server.Close) + return server +} + +// TestServeChallengeLookupIsBounded checks that the startup read of a service with no install record ends after the +// profile check timeout, as the checks of list --check do, even when the host keeps answering with a retriable error. The +// serve still starts, with the sign-in handling on. +func TestServeChallengeLookupIsBounded(t *testing.T) { + prev := profileCheckTimeout + t.Cleanup(func() { profileCheckTimeout = prev }) + profileCheckTimeout = 100 * time.Millisecond + server := serviceServer(t, http.StatusServiceUnavailable, `{"error_code":"TEMPORARILY_UNAVAILABLE","message":"try later"}`) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + start := time.Now() + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) + + require.NoError(t, err) + assert.NotNil(t, challenge) + assert.Less(t, time.Since(start), 10*time.Second, "the lookup must not wait for the SDK's retry budget") +} + +// TestServeChallengeLookupFailureStartsWithSignInHandling checks that a startup read which fails for a reason other than +// authentication does not stop the serve. The sign-in handling is on, since it acts only on a challenge that names the +// service, and one line on stderr says that the read failed. +func TestServeChallengeLookupFailureStartsWithSignInHandling(t *testing.T) { + prev := profileCheckTimeout + t.Cleanup(func() { profileCheckTimeout = prev }) + profileCheckTimeout = 100 * time.Millisecond + for name, tc := range map[string]struct { + status int + body string + }{ + "server error": {http.StatusInternalServerError, `{"error_code":"INTERNAL_ERROR","message":"boom"}`}, + "not found": {http.StatusNotFound, `{"error_code":"NOT_FOUND","message":"not found"}`}, + } { + t.Run(name, func(t *testing.T) { + server := serviceServer(t, tc.status, tc.body) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + var stderr bytes.Buffer + + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, &stderr) + + require.NoError(t, err) + require.NotNil(t, challenge) + assert.Equal(t, testService, challenge.Service) + assert.Equal(t, testProfile, challenge.Profile) + lines := strings.Split(strings.TrimSuffix(stderr.String(), "\n"), "\n") + require.Len(t, lines, 1) + assert.Contains(t, lines[0], "cannot read MCP service "+testService) + assert.Contains(t, lines[0], "serving it with sign-in handling on") + }) + } +} + +// TestServeLookupFailureStillOffersAuthenticate checks that a server started after a failed startup read still offers the +// authenticate tool when the service challenges the session, so a failed read does not leave a sign-in-required service +// without its sign-in. +func TestServeLookupFailureStillOffersAuthenticate(t *testing.T) { + prev := profileCheckTimeout + t.Cleanup(func() { profileCheckTimeout = prev }) + profileCheckTimeout = 100 * time.Millisecond + const lookupPath = "/api/2.1/unity-catalog/mcp-services/" + testService + const endpointPath = "/ai-gateway/mcp-services/" + testService + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case lookupPath: + rw.WriteHeader(http.StatusServiceUnavailable) + _, _ = io.WriteString(rw, `{"error_code":"TEMPORARILY_UNAVAILABLE","message":"try later"}`) + case endpointPath: + var msg struct { + ID json.RawMessage `json:"id"` + } + body, _ := io.ReadAll(r.Body) + _ = json.Unmarshal(body, &msg) + rw.Header().Set("Content-Type", "application/json") + _, _ = fmt.Fprintf(rw, `{"jsonrpc":"2.0","id":%s,"error":{"code":-32006,"message":"rejected","data":null}}`, msg.ID) + default: + rw.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) + require.NoError(t, err) + require.NotNil(t, challenge) + + var out bytes.Buffer + err = proxy.Run(t.Context(), proxy.Options{ + In: strings.NewReader(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}` + "\n"), + Out: &out, + Err: io.Discard, + Endpoint: mcp.ServiceURL(server.URL, testService), + Config: &config.Config{Host: server.URL}, + Authenticate: func(*http.Request) error { return nil }, + Profile: testProfile, + Challenge: challenge, + }) + + require.NoError(t, err) + assert.Contains(t, out.String(), `"name":"authenticate"`) +} + +// TestServeChallengeLookupAuthFailureExitsTwo checks that a startup read that the profile's credential fails names the +// profile and its login command, and exits 2 like the other failures of a credential. +func TestServeChallengeLookupAuthFailureExitsTwo(t *testing.T) { + server := serviceServer(t, http.StatusUnauthorized, `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + _, err = serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) + + require.Error(t, err) + assert.Equal(t, serveBindStatus, serveStartStatus(err)) + assert.Contains(t, err.Error(), "databricks auth login --profile logfood") +} + +// TestServeChallengeStopsQuietlyOnSignal checks that a signal during the startup read ends the start as a shutdown does: +// the read's cancellation is returned, and nothing is written, rather than a line that says the read failed. +func TestServeChallengeStopsQuietlyOnSignal(t *testing.T) { + server := serviceServer(t, http.StatusInternalServerError, `{"error_code":"INTERNAL_ERROR","message":"boom"}`) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + var stderr bytes.Buffer + + _, err = serveChallenge(ctx, serveTarget{w: w, profile: testProfile, service: testService}, &stderr) + + require.ErrorIs(t, err, context.Canceled) + assert.Empty(t, stderr.String()) +} + +// TestServeStartStatusIsOneForOtherFailures checks that a startup failure that is not a credential's exits 1. +func TestServeStartStatusIsOneForOtherFailures(t *testing.T) { + assert.Equal(t, 1, serveStartStatus(errors.New("cannot read MCP service system.ai.github: not found"))) +} + +// TestServeRefreshFailureStopsProxy checks that a token that cannot be refreshed during a call the serve makes on its own, +// the startup read or the sign-in, stops the proxy with the login hint, as a refresh failure of a bridge request does. +func TestServeRefreshFailureStopsProxy(t *testing.T) { + t.Setenv("BROWSER", "none") + for name, call := range map[string]func(context.Context, *databricks.WorkspaceClient) error{ + "startup read": func(ctx context.Context, w *databricks.WorkspaceClient) error { + _, err := serveChallenge(ctx, serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) + return err + }, + "sign-in": func(ctx context.Context, w *databricks.WorkspaceClient) error { + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + _, _, err := signIn(ctx, proxy.SignInRequest{Open: true, Progress: func(string) {}, Stderr: io.Discard}) + return err + }, + } { + t.Run(name, func(t *testing.T) { + server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + w := guardedTestClient(t, server.URL, failingGuard(cancel)) + + err := call(ctx, w) + + require.Error(t, err) + assert.Error(t, ctx.Err(), "the proxy is stopped") + assert.Equal(t, serveBindStatus, serveStartStatus(err)) + assert.Contains(t, err.Error(), "databricks auth login --profile logfood, then restart the MCP server in your agent") + }) + } +} + +// TestServeSignInReportsEachPoll checks that the sign-in of the authenticate tool calls the progress of the request on +// each poll, so the proxy can tell the client that the wait is still going. +func TestServeSignInReportsEachPoll(t *testing.T) { + prevPoll, prevWait := signInPollInterval, signInWaitTimeout + t.Cleanup(func() { signInPollInterval, signInWaitTimeout = prevPoll, prevWait }) + signInPollInterval, signInWaitTimeout = time.Millisecond, 10*time.Second + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + var gets atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Path != credentialPath { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + // The caller signs in on the third read: the first is the check before the sign-in, then two polls. + if gets.Add(1) < 3 { + rw.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) + return + } + _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}`) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) + require.NoError(t, err) + + polls := 0 + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func(string) { polls++ }, Stderr: io.Discard}) + + require.NoError(t, err) + assert.Equal(t, 2, polls) +} + +// TestServeSignInAuthenticatesEveryRequest checks that each request the sign-in client makes is authenticated by the +// refresh guard, so a token that cannot be refreshed during a sign-in stops the proxy, whichever call hits it. +func TestServeSignInAuthenticatesEveryRequest(t *testing.T) { + prevPoll, prevWait := signInPollInterval, signInWaitTimeout + t.Cleanup(func() { signInPollInterval, signInWaitTimeout = prevPoll, prevWait }) + signInPollInterval, signInWaitTimeout = time.Millisecond, 10*time.Second + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + var reads atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Path != credentialPath { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + if reads.Add(1) < 3 { + rw.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) + return + } + _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}`) + })) + t.Cleanup(server.Close) + var authenticated atomic.Int32 + guard := &refreshGuard{authenticate: func(*http.Request) error { authenticated.Add(1); return nil }, profile: testProfile, cancel: func() {}} + w := guardedTestClient(t, server.URL, guard) + + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func(string) {}, Stderr: io.Discard}) + + require.NoError(t, err) + assert.Equal(t, reads.Load(), authenticated.Load(), "every request of the sign-in is authenticated by the guard") +} + +// TestServeSignInTransientFailureKeepsServing checks that a token failure which is not a rejection fails only the +// sign-in's request, with the transient message, and does not stop the proxy. +func TestServeSignInTransientFailureKeepsServing(t *testing.T) { + t.Setenv("BROWSER", "none") + server := serviceServer(t, http.StatusOK, `{}`) + var stopped bool + guard := &refreshGuard{ + authenticate: func(*http.Request) error { return errors.New("dial tcp: i/o timeout") }, + profile: testProfile, + cancel: func() { stopped = true }, + } + w := guardedTestClient(t, server.URL, guard) + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func(string) {}, Stderr: io.Discard}) + + require.Error(t, err) + assert.Contains(t, err.Error(), "the next request tries again") + assert.NotContains(t, err.Error(), "restart the MCP server") + assert.False(t, stopped, "the proxy keeps running") + assert.NoError(t, guard.failure()) +} + +// guardedTestClient returns a client for host whose requests the guard authenticates, as the sign-in client of serve is. +func guardedTestClient(t *testing.T, host string, guard *refreshGuard) *databricks.WorkspaceClient { + t.Helper() + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: host, Credentials: guardCredentials{refresh: guard}, WorkspaceID: "1"}) + require.NoError(t, err) + return w +} + +// TestServeSignInProgressNamesThePage checks that the progress of a sign-in names its page on every poll, whether or not +// this attempt opens the page. A client that hides stderr sees the page only through progress. +func TestServeSignInProgressNamesThePage(t *testing.T) { + prevPoll, prevWait := signInPollInterval, signInWaitTimeout + t.Cleanup(func() { signInPollInterval, signInWaitTimeout = prevPoll, prevWait }) + signInPollInterval, signInWaitTimeout = time.Millisecond, 10*time.Second + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + for _, tc := range []struct { + name string + open bool + }{ + {name: "page opened", open: true}, + {name: "page not opened", open: false}, + } { + t.Run(tc.name, func(t *testing.T) { + var reads atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Path != credentialPath { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + if reads.Add(1) < 3 { + rw.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) + return + } + _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}`) + })) + t.Cleanup(server.Close) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) + var pageURLs []string + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: tc.open, Stderr: io.Discard, Progress: func(pageURL string) { + pageURLs = append(pageURLs, pageURL) + }}) + + require.NoError(t, err) + // The fixture is read twice before the caller signs in: the check before the sign-in, then two polls. + want := mcp.SignInURL(server.URL, testService, "1") + assert.Equal(t, []string{want, want}, pageURLs) + }) + } +} + +// TestServeSignInPageIsNotCancelledWithTheRequest checks that the page of a sign-in whose request is cancelled is opened with +// a context that the cancellation does not reach. A browser command the request's context started would be killed with the +// request, and the page the user is on with it. +func TestServeSignInPageIsNotCancelledWithTheRequest(t *testing.T) { + prevPoll, prevWait := signInPollInterval, signInWaitTimeout + t.Cleanup(func() { signInPollInterval, signInWaitTimeout = prevPoll, prevWait }) + signInPollInterval, signInWaitTimeout = time.Millisecond, 10*time.Second + t.Setenv("BROWSER", "") + prevOpen := serveOpenPage + t.Cleanup(func() { serveOpenPage = prevOpen }) + entered, release := make(chan struct{}), make(chan struct{}) + pageErr := make(chan error, 1) + serveOpenPage = func(ctx context.Context, _ string) error { + close(entered) + <-release + pageErr <- ctx.Err() + return nil + } + + // The credential is never signed in, so the sign-in waits until its request is cancelled. + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { + rw.Header().Set("Content-Type", "application/json") + rw.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) + require.NoError(t, err) + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + signInErr := make(chan error, 1) + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + go func() { + _, _, err := signIn(ctx, proxy.SignInRequest{Open: true, Progress: func(string) {}, Stderr: io.Discard}) + signInErr <- err + }() + select { + case <-entered: + case <-time.After(10 * time.Second): + t.Fatal("the page did not open") + } + cancel() + close(release) + + require.ErrorIs(t, <-signInErr, context.Canceled) + assert.NoError(t, <-pageErr, "the page is opened with a context the request's cancellation does not reach") +} diff --git a/libs/aitools/mcp/discovery.go b/libs/aitools/mcp/discovery.go index 485d3c7cb0d..6fabd4eac98 100644 --- a/libs/aitools/mcp/discovery.go +++ b/libs/aitools/mcp/discovery.go @@ -196,8 +196,6 @@ func newService(svc *catalog.McpService) Service { SignInRequired: provider != "", OAuthProvider: provider, } - if known, ok := knownSystemAI[leaf]; ok && inSystemAI(fullName) { - s.Known = &known - } + s.Known = KnownOf(fullName) return s } diff --git a/libs/aitools/mcp/known.go b/libs/aitools/mcp/known.go index 30ea4d9d524..17883fe1a0b 100644 --- a/libs/aitools/mcp/known.go +++ b/libs/aitools/mcp/known.go @@ -1,5 +1,7 @@ package mcp +import "strings" + // SystemAILocation is the schema that ships the Databricks managed MCP services. const SystemAILocation = "system.ai" @@ -58,3 +60,22 @@ var knownSystemAI = map[string]KnownService{ Note: "Scopes ending in .All usually need tenant admin consent.", }, } + +// KnownOf returns the CLI's description of a service, or nil when it is not a system.ai service in the table. The leaf +// is compared without case, as Unity Catalog names are. +func KnownOf(fullName string) *KnownService { + known, ok := knownSystemAI[strings.ToLower(LeafOf(fullName))] + if !ok || !inSystemAI(fullName) { + return nil + } + return &known +} + +// ProviderLabel names the provider of a service in messages: the known label for a system.ai +// service, else the full name. +func ProviderLabel(fullName string) string { + if known := KnownOf(fullName); known != nil { + return known.Label + } + return fullName +} diff --git a/libs/aitools/mcp/known_test.go b/libs/aitools/mcp/known_test.go index 2b8ad314212..b401338982d 100644 --- a/libs/aitools/mcp/known_test.go +++ b/libs/aitools/mcp/known_test.go @@ -37,3 +37,11 @@ func TestKnownSystemAITable(t *testing.T) { }) } } + +func TestProviderLabel(t *testing.T) { + assert.Equal(t, "GitHub", ProviderLabel("system.ai.github")) + assert.Equal(t, "Genie One", ProviderLabel("SYSTEM.AI.genie_one_mcp")) + assert.Equal(t, "system.ai.unknown_service", ProviderLabel("system.ai.unknown_service")) + assert.Equal(t, "main.mcp.github", ProviderLabel("main.mcp.github")) + assert.Equal(t, "GitHub", ProviderLabel("SYSTEM.AI.GITHUB"), "the leaf is compared without case") +} diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 18e71d9c9bd..b3a397475db 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -27,10 +27,12 @@ type SignInTimeoutError struct { LastCheck error } +// Error implements error. func (e *SignInTimeoutError) Error() string { return "sign-in did not complete before the timeout" } +// Unwrap returns the failure of the last poll, so that errors.Is and errors.As see it. func (e *SignInTimeoutError) Unwrap() error { return e.LastCheck } @@ -39,13 +41,14 @@ func (e *SignInTimeoutError) Unwrap() error { 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. +// before the page. Open, when set, opens the page in the background; it is not called when BROWSER=none, because the URL +// is already printed. OnPoll, when set, is called on each poll of the wait. type SignInOptions struct { Profile string Poll time.Duration Timeout time.Duration Open func(ctx context.Context, url string) error + OnPoll func() } // CredentialState returns the provisioning state of the caller's sign-in to a sign-in-required @@ -96,7 +99,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str // The page opens in the background. A browser command can run for as long as the browser is open, and the wait does // not depend on it. Its failure is reported when it returns, if that is before the sign-in completes. var opened <-chan error - if !browser.IsDisabled(ctx) { + if !browser.IsDisabled(ctx) && opts.Open != nil { done := make(chan error, 1) go func() { done <- opts.Open(ctx, url) }() opened = done @@ -126,6 +129,9 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str } case <-ticker.C: } + if opts.OnPoll != nil { + opts.OnPoll() + } state, err := CredentialState(waitCtx, w, service) if err != nil { diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index d4c7d8115c9..7f287c4d3e9 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -563,3 +563,35 @@ func TestSignInReportsOpenerFailure(t *testing.T) { require.NoError(t, err) assert.Contains(t, out.String(), "Could not open a browser (no browser here). Open the URL above.\n") } + +// TestSignInCallsOnPollOnEachPoll checks that OnPoll is called once for each poll of the wait, after the first read of the +// credential, which is not a poll. +func TestSignInCallsOnPollOnEachPoll(t *testing.T) { + s := &loginTestServer{states: []string{"", "PROVISIONING", "ACTIVE"}} + w := loginTestClient(t, s) + polls := 0 + _, _, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, io.Discard, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + Open: func(context.Context, string) error { return nil }, + OnPoll: func() { polls++ }, + }) + require.NoError(t, err) + assert.Equal(t, 2, polls) + assert.Equal(t, 3, s.reads) +} + +// TestSignInWithoutOpenerOnlyPrintsAndWaits checks that a sign-in with no Open prints the URL and waits for the sign-in, with no +// page opened. +func TestSignInWithoutOpenerOnlyPrintsAndWaits(t *testing.T) { + s := &loginTestServer{states: []string{"", "ACTIVE"}} + w := loginTestClient(t, s) + var out bytes.Buffer + _, urlShown, err := SignIn(t.Context(), w, loginTestSignInURL(w), loginTestService, &out, SignInOptions{ + Poll: time.Millisecond, + Timeout: 10 * time.Second, + }) + require.NoError(t, err) + assert.True(t, urlShown) + assert.Contains(t, out.String(), "Open this URL to sign in to "+loginTestService) +} diff --git a/libs/aitools/mcp/proxy/bridge.go b/libs/aitools/mcp/proxy/bridge.go index 5e22aa33bfa..4e90eddf0cb 100644 --- a/libs/aitools/mcp/proxy/bridge.go +++ b/libs/aitools/mcp/proxy/bridge.go @@ -56,8 +56,13 @@ var ( ErrSkipRequest = errors.New("request skipped") ) -// Hooks intercepts upstream messages before they are written to stdout. +// Hooks intercepts client requests and upstream messages. type Hooks interface { + // Local answers a client request without forwarding it when handled is true; the messages it + // returns are written in order. Requests it does not handle are forwarded as usual. Local is + // called from the goroutine of each request, as Result is. It may write a message before its + // answer, such as a progress notification, through notify. + Local(ctx context.Context, request []byte, notify func(msg []byte)) (msgs [][]byte, handled bool) // Result receives each upstream message for a client request, with the HTTP status (0 when the // request failed before a response). It returns the messages to write in its place, in order: // a single copy of message passes it through, an empty slice drops it. Result is called @@ -80,7 +85,8 @@ type Bridge struct { Instructions string // ForceListChanged sets capabilities.tools.listChanged on the initialize result. ForceListChanged bool - // Hooks, when set, sees every upstream message before it is written. + // Hooks, when set, answers the client requests it handles and sees every upstream message + // before it is written. Nil passes everything through unchanged. Hooks Hooks // Transport performs the upstream requests. Nil means http.DefaultTransport, which has a 30 s dial timeout. A caller // that needs the profile's TLS settings or a shorter connect timeout sets it. @@ -260,6 +266,37 @@ func await(ctx context.Context, gate <-chan struct{}) { } } +// answerLocally writes the hooks' answer to a client message that they handle, and reports whether it did. Nothing is +// forwarded upstream then, and no idle watchdog runs for it. +func (c *conn) answerLocally(ctx context.Context, line []byte) bool { + if c.b.Hooks == nil { + return false + } + msgs, handled := c.b.Hooks.Local(ctx, line, c.notifyLocal) + if handled { + c.writeHook(msgs) + } + return handled +} + +// notifyLocal writes a message that a hook sends before its answer. +func (c *conn) notifyLocal(msg []byte) { + c.writeHook([][]byte{msg}) +} + +// writeHook writes the messages a hook produced, as lines and in order. A message that is not JSON is logged, and none is +// written. +func (c *conn) writeHook(msgs [][]byte) { + lines, err := oneLines(msgs) + if err != nil { + c.logf("hook: %v", err) + return + } + if err := c.writeLines(lines); err != nil { + c.logf("stdout: %v", err) + } +} + // forward waits (see await), then POSTs one client message exactly as received and writes what the upstream // answers. The idle watchdog covers every response, plain JSON included. It starts before the POST, so it also // bounds the wait for response headers, and the run time of a hook counts toward it. For a notification, release @@ -270,6 +307,9 @@ func (c *conn) forward(ctx context.Context, gate <-chan struct{}, line []byte, m timer := time.AfterFunc(notificationWait, release) defer timer.Stop() } + if c.answerLocally(ctx, line) { + return + } idleCtx, cancel := context.WithCancelCause(ctx) defer cancel(nil) idle := time.AfterFunc(idleTimeout, func() { cancel(errIdleTimeout) }) @@ -833,6 +873,12 @@ func (x *exchange) lines(reqCtx context.Context, msg []byte) ([][]byte, error) { return nil, err } } + return oneLines(msgs) +} + +// oneLines returns each message as a single line, in order (see oneLine). When one is not JSON, it returns the error and +// no lines. +func oneLines(msgs [][]byte) ([][]byte, error) { lines := make([][]byte, 0, len(msgs)) for _, out := range msgs { line, err := oneLine(out) diff --git a/libs/aitools/mcp/proxy/bridge_test.go b/libs/aitools/mcp/proxy/bridge_test.go index 89fe538b7a8..11c60b775c4 100644 --- a/libs/aitools/mcp/proxy/bridge_test.go +++ b/libs/aitools/mcp/proxy/bridge_test.go @@ -17,6 +17,7 @@ import ( "testing" "time" + "github.com/databricks/databricks-sdk-go/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -827,6 +828,10 @@ func (f hookFunc) Result(ctx context.Context, request []byte, status int, msg [] return f(ctx, request, status, msg) } +func (hookFunc) Local(context.Context, []byte, func([]byte)) ([][]byte, bool) { + return nil, false +} + func TestRunHooksReplaceAndInjectMessages(t *testing.T) { const request = `{"jsonrpc":"2.0","id":2,"method":"tools/list"}` const synthetic = `{"jsonrpc":"2.0","id":2,"result":{"tools":[]}}` @@ -2194,3 +2199,145 @@ func TestRunClosesStaleResponseBeforeReplay(t *testing.T) { require.Len(t, out, 2) assert.Nil(t, parseReply(t, out[1]).Error) } + +// answerHooks answers a request whose body names "local" with msgs, and forwards the others. +type answerHooks struct{ msgs [][]byte } + +func (answerHooks) Result(_ context.Context, _ []byte, _ int, msg []byte) ([][]byte, error) { + return [][]byte{msg}, nil +} + +func (h answerHooks) Local(_ context.Context, request []byte, _ func([]byte)) ([][]byte, bool) { + if !bytes.Contains(request, []byte("local")) { + return nil, false + } + return h.msgs, true +} + +// TestAnswerLocallyWritesHookMessagesInOrder checks that a request the hooks handle is answered with their messages, one +// line each and in order, and that nothing is written for a request they do not handle. +func TestAnswerLocallyWritesHookMessagesInOrder(t *testing.T) { + var out, errOut syncBuffer + c := &conn{b: &Bridge{Out: &out, Err: &errOut, Hooks: answerHooks{msgs: [][]byte{[]byte(`{"n":1}`), []byte(`{"n":2}`)}}}} + + assert.True(t, c.answerLocally(t.Context(), []byte(`{"jsonrpc":"2.0","id":1,"method":"local"}`))) + assert.Equal(t, "{\"n\":1}\n{\"n\":2}\n", out.String()) + + assert.False(t, c.answerLocally(t.Context(), []byte(`{"jsonrpc":"2.0","id":2,"method":"remote"}`))) + assert.Equal(t, "{\"n\":1}\n{\"n\":2}\n", out.String()) +} + +// TestAnswerLocallyWithoutHooksForwardsEverything checks that a bridge without hooks handles no request. +func TestAnswerLocallyWithoutHooksForwardsEverything(t *testing.T) { + var out syncBuffer + c := &conn{b: &Bridge{Out: &out, Err: &syncBuffer{}}} + + assert.False(t, c.answerLocally(t.Context(), []byte(`{"jsonrpc":"2.0","id":1,"method":"local"}`))) + assert.Empty(t, out.String()) +} + +// scriptedHooks answers every request with msgs, after notify has been called with notify, when handled is true. Upstream +// messages pass through. +type scriptedHooks struct { + notify []byte + msgs [][]byte + handled bool +} + +func (h scriptedHooks) Local(_ context.Context, _ []byte, notify func([]byte)) ([][]byte, bool) { + if h.notify != nil { + notify(h.notify) + } + return h.msgs, h.handled +} + +func (scriptedHooks) Result(_ context.Context, _ []byte, _ int, msg []byte) ([][]byte, error) { + return [][]byte{msg}, nil +} + +// TestAnswerLocallyCompactsMessages checks that the messages a hook writes itself, its progress and its answer, are one +// stdout line each, as an upstream answer is. +func TestAnswerLocallyCompactsMessages(t *testing.T) { + b := bridgeFor("http://unused.test") + b.Hooks = scriptedHooks{ + notify: []byte("{\n\"jsonrpc\": \"2.0\",\n\"method\": \"notifications/progress\"}"), + msgs: [][]byte{[]byte("{\n\"jsonrpc\": \"2.0\",\n\"id\": 3,\n\"result\": {}}")}, + handled: true, + } + + out, _ := runAll(t, b, `{"jsonrpc":"2.0","id":3,"method":"tools/call"}`+"\n") + + assert.Equal(t, []string{ + `{"jsonrpc":"2.0","method":"notifications/progress"}`, + `{"jsonrpc":"2.0","id":3,"result":{}}`, + }, out) +} + +// TestAnswerLocallyWritesNothingWhenNotHandled checks that a request a hook does not handle is forwarded, and that the +// hook's messages for it are not written. +func TestAnswerLocallyWritesNothingWhenNotHandled(t *testing.T) { + srv, fake := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + b := bridgeFor(srv.URL) + b.Hooks = scriptedHooks{msgs: [][]byte{[]byte(listChangedMsg)}, handled: false} + + out, _ := runAll(t, b, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") + + assert.Equal(t, []string{listTools}, out) + assert.Len(t, fake.requests(), 1, "the request is forwarded") +} + +// TestSignInWritesShareTheBridgeStderr checks that the progress a sign-in writes to its stderr is serialized with the +// bridge's own diagnostics. The stderr here is a plain buffer, so a write that is not serialized is a data race, which +// the race detector reports. +func TestSignInWritesShareTheBridgeStderr(t *testing.T) { + const progressLines = 200 + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + if m, _ := parseMessage(body); m.Method == "tools/list" { + answerWith(http.StatusOK, listChallenge)(w, r) + return + } + // A body that is not JSON is logged by the bridge, once per request. + answerWith(http.StatusOK, "not json")(w, r) + }) + signIn := func(_ context.Context, req SignInRequest) (bool, bool, error) { + for i := range progressLines { + fmt.Fprintf(req.Stderr, "sign-in progress %d\n", i) + } + return false, true, nil + } + var stderr bytes.Buffer + var stdout syncBuffer + in, pw := io.Pipe() + done := make(chan error, 1) + go func() { + done <- Run(t.Context(), Options{ + In: in, + Out: &stdout, + Err: &stderr, + Endpoint: srv.URL, + Config: &config.Config{Host: testHost}, + Authenticate: func(*http.Request) error { return nil }, + Profile: "logfood", + Challenge: &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}, + }) + }() + + // The session is challenged once the list is answered, so the authenticate call waits for that answer. + fmt.Fprintln(pw, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) + require.Eventually(t, func() bool { return strings.Contains(stdout.String(), `"authenticate"`) }, 10*time.Second, time.Millisecond) + fmt.Fprintln(pw, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + for id := 10; id < 30; id++ { + fmt.Fprintf(pw, `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"echo","arguments":{}}}`+"\n", id) + } + require.NoError(t, pw.Close()) + require.NoError(t, <-done) + + var progress int + for line := range strings.SplitSeq(stderr.String(), "\n") { + if strings.HasPrefix(line, "sign-in progress ") { + progress++ + } + } + assert.Equal(t, progressLines, progress, "every progress line is written whole") +} diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go new file mode 100644 index 00000000000..a1959a18e49 --- /dev/null +++ b/libs/aitools/mcp/proxy/challenge.go @@ -0,0 +1,540 @@ +package proxy + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "regexp" + "strings" + "sync" + "time" + + "github.com/databricks/cli/libs/aitools/mcp" +) + +const ( + authenticateTool = "authenticate" + + codeSignInRequired = -32006 + codeURLElicitation = -32042 +) + +// urlPattern finds the http and https URLs in a text. A URL ends at a space or at a character that cannot be in one +// here, such as a bracket, a quote or an emphasis mark, so the URL of a markdown link or a `` is found too. +var urlPattern = sync.OnceValue(func() *regexp.Regexp { + return regexp.MustCompile("https?://[^\\s<>()\\[\\]\"'*`]+") +}) + +// queueProgressInterval is how often a call that waits for the sign-in ahead of it reports progress. Tests shorten it. +var queueProgressInterval = 2 * time.Second + +var listChangedNotification = []byte(`{"jsonrpc":"` + jsonrpcVersion + `","method":"` + methodListChanged + `"}`) + +// SignInRequest says how one sign-in runs. +type SignInRequest struct { + // Open opens the sign-in page. It is false when the page must not open again in this session. + Open bool + // Progress is called on each poll of the wait with the sign-in page, so that the client can show where the user + // finishes, whether or not this attempt opened the page. It is never nil. + Progress func(pageURL string) + // Stderr receives the progress lines of the sign-in. Writes to it are serialized with the bridge's diagnostics. + Stderr io.Writer +} + +// SignInFunc runs the sign-in for the authenticate tool and waits for it. It reports alreadySignedIn when the caller +// needed no sign-in, and urlShown when the sign-in page's URL was printed, which is so even when the wait is then +// cancelled. Its error is the answer's text. +type SignInFunc func(ctx context.Context, req SignInRequest) (alreadySignedIn, urlShown bool, err error) + +// Challenge turns on sign-in handling for a service that needs a provider sign-in. +type Challenge struct { + // Service is the full name of the MCP service, for example system.ai.github. + Service string + // Profile is the bound profile's name. + Profile string + // Host is the bound profile's workspace URL. Sign-in URLs must be on this host. + Host string + // Label names the provider in messages. + Label string + // LoginCommand is the command that signs the profile in to the service, as the hints print it. + LoginCommand string + // PinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). The auth hint names it. + PinnedFile string + // LogoutCommand is the command that removes the profile's sign-in to the service. The answer names it when the service + // still asks for a sign-in after the credential reads as signed in. + LogoutCommand string + // SignIn runs the sign-in. The command provides it, so the proxy never polls or opens a browser itself. Its error is + // the answer's text, so it names the service, the profile and the fix. + SignIn SignInFunc +} + +// signInHooks applies the sign-in rules to the bridge. Services without a challenge get no hooks. +type signInHooks struct { + c *Challenge + // stderr is the bridge's stderr, which SignIn writes its progress to. + stderr io.Writer + + // turn is held by the authenticate call whose sign-in runs, so that the calls run one at a time. It has room for one + // call. A call waits for it under its own context (see acquire). pageCancelled is read and written under it. + turn chan struct{} + // pageCancelled means the last attempt showed the sign-in page and the client cancelled it before the sign-in + // completed. The user may still be on that page, so the next attempt does not open it again. + pageCancelled bool + + // stateMu guards the fields below. It is held only briefly, so an answer to a request is not held up by a sign-in. + stateMu sync.Mutex + // exposed means the agent's list shows the authenticate tool: a challenge sets it, and a tools/list that passes through + // unchallenged clears it, unless a call challenge is pending (see callChallenged). The tool is the proxy's only while it + // is exposed. + exposed bool + // ownAuthenticate means the first page of the service's last list had a tool named authenticate, which the proxy neither + // adds nor intercepts. A challenge that shows the proxy's own list clears it. + ownAuthenticate bool + // alreadySignedIn means the last authenticate answered "Already signed in". + alreadySignedIn bool + // stillChallenged means the service challenged the session after an "Already signed in" answer: the credential reads as + // signed in, but the service does not accept it. The next authenticate does not sign in, since the proxy never deletes a + // credential; it answers with the logout command instead, and clears this. + stillChallenged bool + // callChallenged means the last challenge came from a tools/call, and no sign-in has ended and no call has passed + // since. The agent was told to call authenticate, so a tools/list that passes still lists it. + callChallenged bool + // callPending means the last challenge came from a tools/call, and no tools/call has succeeded since. A sign-in that + // ends does not clear it, so a tools/list that passes does not end the "Already signed in" state of that challenge. + callPending bool +} + +// newSignInHooks returns the sign-in hooks of c. Progress lines of a sign-in are written to stderr. +func newSignInHooks(c *Challenge, stderr io.Writer) *signInHooks { + return &signInHooks{c: c, stderr: stderr, turn: make(chan struct{}, 1)} +} + +// Local answers the authenticate tool call once the session has been challenged. Before that, and for other requests and +// messages that do not decode, the request is forwarded. +func (h *signInHooks) Local(ctx context.Context, request []byte, notify func([]byte)) ([][]byte, bool) { + var req struct { + message + Params struct { + Name string `json:"name"` + Meta struct { + ProgressToken *json.RawMessage `json:"progressToken"` + } `json:"_meta"` + } `json:"params"` + } + if json.Unmarshal(request, &req) != nil || !req.isRequest() || req.Method != methodToolsCall || req.Params.Name != authenticateTool { + return nil, false + } + h.stateMu.Lock() + intercept := h.exposed && !h.ownAuthenticate + h.stateMu.Unlock() + if !intercept { + return nil, false + } + var progressToken json.RawMessage + if req.Params.Meta.ProgressToken != nil { + progressToken = *req.Params.Meta.ProgressToken + } + return h.authenticate(ctx, req.ID, progressToken, notify), true +} + +// Result replaces a challenged tools/list answer with the authenticate tool. A challenged tools/call +// answer becomes a sign-in message, followed by tools/list_changed so an agent that cached its list re-lists. A 401 that +// is not a challenge becomes the auth failure (see authFailure). Only the response to the request is read; any other +// message passes through untouched. +func (h *signInHooks) Result(_ context.Context, request []byte, status int, message []byte) ([][]byte, error) { + req, err := parseMessage(request) + if err != nil { + return nil, err + } + // Only the responses to tools/list and tools/call are read for a challenge or a success. An HTTP 401 is read for any + // request, since it is an auth failure unless its body asks for a sign-in. + if req.Method != methodToolsList && req.Method != methodToolsCall && status != http.StatusUnauthorized { + return [][]byte{message}, nil + } + // A message that is not the response to the request, or whose shape the checks do not read, is neither a challenge nor + // a success, so it passes through. + reply, ok := readReply(message, req) + if !ok { + return [][]byte{message}, nil + } + challenged := h.challenged(&reply) + if status == http.StatusUnauthorized && !challenged { + return [][]byte{errorResponse(req.ID, codeUpstream, h.authFailure())}, nil + } + if req.Method != methodToolsList && req.Method != methodToolsCall { + return [][]byte{message}, nil + } + if challenged { + h.recordChallenge(req.Method) + if req.Method == methodToolsList { + return [][]byte{h.authenticateList(req.ID)}, nil + } + return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil + } + // A result with no error and no isError means the service accepts the caller's credential. An error answer, such as a + // JSON-RPC error on HTTP 200, a failure the bridge wrote for the request, or an isError result without a sign-in URL, + // says nothing about it. + if status/100 == 2 && reply.Error == nil && reply.Result != nil && !reply.Result.IsError { + msg, err := h.succeeded(request, req.Method, message) + if err != nil { + return nil, err + } + return [][]byte{msg}, nil + } + return [][]byte{message}, nil +} + +// recordChallenge records a challenge to a request of method. The authenticate tool is listed from now on, and a challenge +// that follows an "Already signed in" answer leaves the next authenticate with the logout answer. +func (h *signInHooks) recordChallenge(method string) { + h.stateMu.Lock() + defer h.stateMu.Unlock() + h.exposed = true + h.callChallenged = method == methodToolsCall + h.callPending = h.callChallenged + if method == methodToolsList { + // The list the agent gets is the proxy's one-tool list, so the service's authenticate tool is not on it. + h.ownAuthenticate = false + } + if h.alreadySignedIn { + h.stillChallenged = true + h.alreadySignedIn = false + } +} + +// succeeded records a successful answer to a request of method, and returns the message to write. A successful tools/call +// ends the "Already signed in" state, the pending logout answer, and a call challenge. A tools/list answer ends the first +// two too, unless the pending challenge came from a tools/call, which only a successful tools/call ends. It also takes the +// authenticate tool off the list, since the list is the service's again, unless a call challenge is still pending; then +// the tool stays on it. +func (h *signInHooks) succeeded(request []byte, method string, message []byte) ([]byte, error) { + h.stateMu.Lock() + defer h.stateMu.Unlock() + if method == methodToolsCall { + h.alreadySignedIn = false + h.stillChallenged = false + h.callPending = false + h.callChallenged = false + return message, nil + } + if !h.callPending { + h.alreadySignedIn = false + h.stillChallenged = false + } + if !h.callChallenged { + h.exposed = false + return message, nil + } + return h.withAuthenticate(request, message) +} + +// withAuthenticate adds the authenticate tool to the tools of a tools/list result, which has a result. Only the first page +// gets it, since a page that has no cursor is the start of the list. A list that already has a tool named authenticate +// keeps its own, and the proxy neither adds one nor intercepts calls to it. +func (h *signInHooks) withAuthenticate(request, msg []byte) ([]byte, error) { + firstPage, err := isFirstPage(request) + if err != nil { + return nil, err + } + if !firstPage { + return msg, nil + } + var env map[string]json.RawMessage + if err := json.Unmarshal(msg, &env); err != nil { + return nil, err + } + result, err := decodeObject(env[keyResult]) + if err != nil { + return nil, fmt.Errorf("tools/list result: %w", err) + } + var tools []json.RawMessage + if err := json.Unmarshal(member(result, keyTools), &tools); err != nil { + return nil, fmt.Errorf("tools/list tools: %w", err) + } + h.ownAuthenticate = false + for _, raw := range tools { + var tool struct { + Name string `json:"name"` + } + if err := json.Unmarshal(raw, &tool); err != nil { + return nil, fmt.Errorf("tools/list tool: %w", err) + } + if tool.Name == authenticateTool { + h.ownAuthenticate = true + return msg, nil + } + } + tool, err := json.Marshal(h.authenticateToolDef()) + if err != nil { + return nil, err + } + if result[keyTools], err = json.Marshal(append(tools, tool)); err != nil { + return nil, err + } + if env[keyResult], err = json.Marshal(result); err != nil { + return nil, err + } + return json.Marshal(env) +} + +// isFirstPage reports whether a tools/list request asks for the first page, which has no cursor. +func isFirstPage(request []byte) (bool, error) { + var req struct { + Params struct { + Cursor string `json:"cursor"` + } `json:"params"` + } + if err := json.Unmarshal(request, &req); err != nil { + return false, err + } + return req.Params.Cursor == "", nil +} + +// readReply decodes an upstream message as challenge detection reads it. It reports false when the message is not the +// response to request, or when its shape is not one the checks read. +func readReply(message []byte, request message) (upstreamReply, bool) { + var reply upstreamReply + if json.Unmarshal(message, &reply) != nil || !reply.isResponseTo(request) { + return reply, false + } + return reply, true +} + +// upstreamReply is an upstream message as challenge detection reads it: its envelope, and its error or result. +type upstreamReply struct { + message + Error *struct { + Code int `json:"code"` + Data json.RawMessage `json:"data"` + } `json:"error"` + Result *struct { + IsError bool `json:"isError"` + Content []textContent `json:"content"` + } `json:"result"` +} + +type textContent struct { + Text string `json:"text"` +} + +// challenged reports whether an upstream answer says the caller must sign in: JSON-RPC -32006, a -32042 whose elicitations +// are all sign-in URLs, or an isError result whose text has one. An HTTP 401 is a challenge only with one of these bodies. +func (h *signInHooks) challenged(reply *upstreamReply) bool { + if reply.Error != nil { + switch reply.Error.Code { + case codeSignInRequired: + return true + case codeURLElicitation: + return h.urlElicitation(reply.Error.Data) + } + return false + } + return reply.Result != nil && reply.Result.IsError && h.textHasSignInURL(reply.Result.Content) +} + +// urlElicitation reports whether data lists one or more URL elicitations, all of them sign-in URLs. +// A mixed challenge, such as an approval request, is not a sign-in. +func (h *signInHooks) urlElicitation(data json.RawMessage) bool { + var d struct { + Elicitations []struct { + Mode string `json:"mode"` + URL string `json:"url"` + } `json:"elicitations"` + } + if json.Unmarshal(data, &d) != nil || len(d.Elicitations) == 0 { + return false + } + for _, e := range d.Elicitations { + if e.Mode != "url" || !h.isSignInURL(e.URL) { + return false + } + } + return true +} + +// textHasSignInURL reports whether one of the texts contains a sign-in URL. A period, comma or colon after the URL is +// the punctuation of the sentence, not part of the URL. +func (h *signInHooks) textHasSignInURL(content []textContent) bool { + for _, c := range content { + for _, found := range urlPattern().FindAllString(c.Text, -1) { + if h.isSignInURL(strings.TrimRight(found, ".,:;")) { + return true + } + } + } + return false +} + +// isSignInURL reports whether raw is this service's sign-in page on the profile's host. +func (h *signInHooks) isSignInURL(raw string) bool { + u, err := url.Parse(raw) + if err != nil { + return false + } + base, err := url.Parse(h.c.Host) + if err != nil { + return false + } + return u.Scheme == base.Scheme && strings.EqualFold(u.Host, base.Host) && + u.Path == mcp.SignInPath && strings.EqualFold(u.Query().Get("name"), h.c.Service) +} + +// authFailure is the answer to a 401 that does not ask for a sign-in: the Databricks credential was rejected, and the +// profile's login fixes that. The text is the refresh guard's, so the two read alike. +func (h *signInHooks) authFailure() string { + cause := errors.New("the MCP service rejected the credential of the profile (HTTP 401)") + return fmt.Sprintf("%v, then restart the MCP server in your agent", mcp.AuthError(h.c.Profile, h.c.PinnedFile, cause)) +} + +// authenticateList is the one-tool list a challenged service answers with. +func (h *signInHooks) authenticateList(id json.RawMessage) []byte { + return resultResponse(id, map[string]any{keyTools: []any{h.authenticateToolDef()}}) +} + +// authenticateToolDef is the definition of the authenticate tool, as a tools/list result lists it. +func (h *signInHooks) authenticateToolDef() map[string]any { + return map[string]any{ + "name": authenticateTool, + "description": fmt.Sprintf("Sign-in is required before this Databricks MCP service (%s, workspace %s, CLI profile %s) can list or run its tools. "+ + "Call this tool when the user asks for this service. It opens a browser for the user to sign in to %s with the same account as the Databricks profile "+ + "and waits until the sign-in completes. After it succeeds, retry the user's original request in the same turn.", + h.c.Service, h.c.Host, h.c.Profile, h.c.Label), + "inputSchema": map[string]any{"type": "object", "properties": map[string]any{}, "additionalProperties": false}, + } +} + +// signInRequired answers a challenged tool call without opening a browser. +func (h *signInHooks) signInRequired(id json.RawMessage) []byte { + text := fmt.Sprintf("%s on profile %s needs a one-time sign-in to %s. Call the authenticate tool, or run: %s. "+ + "If the consent screen says your organization requires approval, share the app name shown there with your Databricks admin.", + h.c.Service, h.c.Profile, h.c.Label, h.c.LoginCommand) + return toolResult(id, text, true) +} + +// authenticate runs the sign-in and answers the tool call. The page opens unless the last attempt was cancelled while its +// page was showing (see pageCancelled). With a progressToken, each poll is reported through notify. +func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.RawMessage, notify func([]byte)) [][]byte { + var polls int + report := func(pageURL string) { + // A poll that fires after the client cancelled the call reports nothing for it. + if len(progressToken) == 0 || ctx.Err() != nil { + return + } + polls++ + notify(progressNotification(progressToken, polls, h.c.Label, pageURL)) + } + if !h.acquire(ctx, report) { + return nil + } + defer h.release() + + h.stateMu.Lock() + stillChallenged := h.stillChallenged + h.stillChallenged = false + h.stateMu.Unlock() + if stillChallenged { + // A sign-in cannot change what the service accepts, and the proxy does not delete the credential, so the caller logs + // out first. The page is not opened. + if ctx.Err() != nil { + return nil + } + return [][]byte{toolResult(id, h.stillChallengedText(), true)} + } + + alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Progress: report, Stderr: h.stderr}) + h.pageCancelled = urlShown && err != nil && ctx.Err() != nil + h.stateMu.Lock() + h.alreadySignedIn = err == nil && alreadySignedIn + if err == nil { + // The sign-in ended, so the next list is the service's own, unless it is challenged again. + h.callChallenged = false + } + h.stateMu.Unlock() + if ctx.Err() != nil { + // A cancelled request is not answered. + return nil + } + switch { + case err == nil && alreadySignedIn: + // The credential was there already, for example from `mcp login` in a terminal, while the agent's list still + // shows only authenticate, so the agent lists again. + return [][]byte{ + toolResult(id, fmt.Sprintf("Already signed in to %s on profile %s. Tools are available now; retry the user's request in this turn.", h.c.Service, h.c.Profile), false), + listChangedNotification, + } + case err == nil: + return [][]byte{ + toolResult(id, fmt.Sprintf("Signed in to %s on profile %s. Tools are available now; retry the user's request in this turn.", h.c.Service, h.c.Profile), false), + listChangedNotification, + } + default: + return [][]byte{toolResult(id, err.Error(), true)} + } +} + +// stillChallengedText is the answer to authenticate while the service still asks for a sign-in after the credential +// reads as signed in. +func (h *signInHooks) stillChallengedText() string { + return fmt.Sprintf("%s on profile %s reads as signed in, but the service still asks for a sign-in. Run: %s, then call authenticate again.", + h.c.Service, h.c.Profile, h.c.LogoutCommand) +} + +// acquire takes the turn that lets an authenticate call run its sign-in. While the call waits for the turn, it reports +// progress every queueProgressInterval, so that a client does not give up on it, and it returns false when ctx is done +// first. A call whose ctx is done once it has the turn gives the turn back and returns false, so a call the client +// cancelled before its own attempt starts touches no state. +func (h *signInHooks) acquire(ctx context.Context, report func(pageURL string)) bool { + ticker := time.NewTicker(queueProgressInterval) + defer ticker.Stop() + for { + select { + case h.turn <- struct{}{}: + if ctx.Err() != nil { + h.release() + return false + } + return true + case <-ticker.C: + report("") + case <-ctx.Done(): + return false + } + } +} + +// release gives the turn taken by acquire to the next call. +func (h *signInHooks) release() { + <-h.turn +} + +// progressNotification is the notifications/progress message for the n-th poll of a sign-in that the client asked to +// hear about through token. A non-empty pageURL is the page the user finishes at. +func progressNotification(token json.RawMessage, n int, label, pageURL string) []byte { + message := fmt.Sprintf("Waiting for the sign-in to %s to complete.", label) + if pageURL != "" { + message += " Finish at " + pageURL + } + // Cannot fail: the token came from a parsed request and the rest holds plain values. + b, _ := json.Marshal(map[string]any{ + "jsonrpc": jsonrpcVersion, + "method": methodProgress, + "params": map[string]any{ + "progressToken": token, + "progress": n, + "message": message, + }, + }) + return b +} + +// toolResult builds a tool call result with one text content item. +func toolResult(id json.RawMessage, text string, isError bool) []byte { + return resultResponse(id, map[string]any{ + "content": []any{map[string]any{"type": "text", "text": text}}, + "isError": isError, + }) +} diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go new file mode 100644 index 00000000000..0a399b0e1bb --- /dev/null +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -0,0 +1,1229 @@ +package proxy + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + testService = "system.ai.github" + testHost = "https://workspace.test" + testURL = testHost + "/mcp-service-login?name=system.ai.github&o=1" + // Each message is one line, so an upstream body that passes through is byte-equal on stdout. An upstream answer + // carries the id of its request, so each request id has its own fixtures. + listChallenge = `{"jsonrpc":"2.0","id":2,"error":{"code":-32006,"message":"rejected","data":null}}` + callChallenge = `{"jsonrpc":"2.0","id":3,"error":{"code":-32006,"message":"rejected","data":null}}` + listTools = `{"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}}]}}` + listToolsLater = `{"jsonrpc":"2.0","id":4,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}}]}}` + listChangedMsg = `{"jsonrpc":"2.0","method":"notifications/tools/list_changed"}` +) + +// signInStub replaces the command's sign-in. Each call records its request and returns reply(call), with the page +// reported as shown. +type signInStub struct { + mu sync.Mutex + reqs []SignInRequest + reply func(call int) (bool, error) +} + +func (s *signInStub) SignIn(_ context.Context, req SignInRequest) (bool, bool, error) { + s.mu.Lock() + defer s.mu.Unlock() + s.reqs = append(s.reqs, req) + alreadySignedIn, err := s.reply(len(s.reqs)) + return alreadySignedIn, true, err +} + +// calls returns whether each sign-in opened the page. +func (s *signInStub) calls() []bool { + s.mu.Lock() + defer s.mu.Unlock() + var opens []bool + for _, req := range s.reqs { + opens = append(opens, req.Open) + } + return opens +} + +// requests returns the request of each sign-in. +func (s *signInStub) requests() []SignInRequest { + s.mu.Lock() + defer s.mu.Unlock() + return append([]SignInRequest(nil), s.reqs...) +} + +// loginCommand is the hint that the command builds for a pinned config file and a profile name with a space. +const loginCommand = `DATABRICKS_CONFIG_FILE='/work/cfg/my file.cfg' databricks aitools mcp login system.ai.github --profile 'my profile'` + +// challengedBridge returns a bridge to url with the sign-in hooks of a service that needs sign-in. +func challengedBridge(url string, signIn SignInFunc) *Bridge { + b := bridgeFor(url) + b.Hooks = newSignInHooks(testChallenge(signIn), io.Discard) + return b +} + +// answerWith replies to every request with status and body. +func answerWith(status int, body string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + _, _ = io.WriteString(w, body) + } +} + +// challengedService answers tools/list with the sign-in challenge and every other request with 202, as a service that +// needs sign-in does until the caller signs in. +func challengedService(t *testing.T) (*httptest.Server, *fakeEndpoint) { + return newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + if m, _ := parseMessage(body); m.Method != "tools/list" { + w.WriteHeader(http.StatusAccepted) + return + } + answerWith(http.StatusOK, listChallenge)(w, r) + }) +} + +// challenge lists the tools of the session, which the challengedService answers with the challenge, and waits for the +// answer. The calls after it see a challenged session. +func challenge(t *testing.T, p *piped) { + t.Helper() + p.send(t, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) + p.waitLines(t, 1) +} + +// unauthorized answers 401 with a plain body, as the gateway does to a caller it cannot authenticate. +func unauthorized(w http.ResponseWriter, _ *http.Request) { + http.Error(w, "unauthorized", http.StatusUnauthorized) +} + +// resultText returns the text of a tool call result line. +func resultText(t *testing.T, line string) string { + t.Helper() + return parseReply(t, line).Result["content"].([]any)[0].(map[string]any)["text"].(string) +} + +// toolNames returns the tool names of a tools/list result line. +func toolNames(t *testing.T, line string) []string { + t.Helper() + reply := parseReply(t, line) + var names []string + for _, tool := range reply.Result["tools"].([]any) { + names = append(names, tool.(map[string]any)["name"].(string)) + } + return names +} + +func TestChallengeReplacesListWithAuthenticateTool(t *testing.T) { + cases := map[string]http.HandlerFunc{ + "rpc -32006": answerWith(http.StatusOK, listChallenge), + "http 401 with a sign-in body": answerWith(http.StatusUnauthorized, listChallenge), + "url elicitation": answerWith(http.StatusOK, `{"jsonrpc":"2.0","id":2,"error":{"code":-32042,"message":"x","data":{"elicitations":[`+ + `{"mode":"url","url":"`+testURL+`"}]}}}`), + } + for name, handle := range cases { + t.Run(name, func(t *testing.T) { + srv, _ := newFakeEndpoint(t, handle) + signIn := &signInStub{reply: func(int) (bool, error) { return false, nil }} + out, _ := runAll(t, challengedBridge(srv.URL, signIn.SignIn), `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") + + require.Len(t, out, 1) + assert.Equal(t, []string{"authenticate"}, toolNames(t, out[0])) + reply := parseReply(t, out[0]) + assert.JSONEq(t, `2`, string(reply.ID)) + assert.Empty(t, signIn.calls(), "tools/list must not start a sign-in") + }) + } +} + +func TestChallengeListPassesThroughWhenSignedIn(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + out, _ := runAll(t, challengedBridge(srv.URL, nil), `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") + + assert.Equal(t, []string{listTools}, out) +} + +func TestChallengeIgnoresOtherErrors(t *testing.T) { + cases := map[string]string{ + "generic error": `{"jsonrpc":"2.0","id":2,"error":{"code":-32000,"message":"boom"}}`, + "mixed elicitation": `{"jsonrpc":"2.0","id":2,"error":{"code":-32042,"message":"x","data":{"elicitations":[` + + `{"mode":"url","url":"` + testURL + `"},{"mode":"form"}]}}}`, + "other host": `{"jsonrpc":"2.0","id":2,"error":{"code":-32042,"message":"x","data":{"elicitations":[` + + `{"mode":"url","url":"https://elsewhere.test/mcp-service-login?name=system.ai.github"}]}}}`, + "other service": `{"jsonrpc":"2.0","id":2,"error":{"code":-32042,"message":"x","data":{"elicitations":[` + + `{"mode":"url","url":"` + testHost + `/mcp-service-login?name=system.ai.slack"}]}}}`, + "other path": `{"jsonrpc":"2.0","id":2,"error":{"code":-32042,"message":"x","data":{"elicitations":[` + + `{"mode":"url","url":"` + testHost + `/oidc?name=system.ai.github"}]}}}`, + } + for name, body := range cases { + t.Run(name, func(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, body)) + signIn := &signInStub{reply: func(int) (bool, error) { return false, nil }} + out, _ := runAll(t, challengedBridge(srv.URL, signIn.SignIn), `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") + + assert.Equal(t, []string{body}, out) + assert.Empty(t, signIn.calls()) + }) + } +} + +func TestChallengeToolsCallAnswersWithoutBrowser(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, callChallenge)) + signIn := &signInStub{reply: func(int) (bool, error) { return false, nil }} + out, _ := runAll(t, challengedBridge(srv.URL, signIn.SignIn), + `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`+"\n") + + require.Len(t, out, 2) + reply := parseReply(t, out[0]) + assert.JSONEq(t, `3`, string(reply.ID)) + text := resultText(t, out[0]) + assert.Equal(t, true, reply.Result["isError"]) + assert.Contains(t, text, "system.ai.github on profile logfood needs a one-time sign-in to GitHub.") + assert.Contains(t, text, "databricks aitools mcp login system.ai.github --profile logfood") + assert.Equal(t, listChangedMsg, out[1]) + assert.Empty(t, signIn.calls(), "a challenged tools/call must not open the sign-in page") +} + +func TestChallengeToolsCallWithSignInURLInTextIsChallenged(t *testing.T) { + body := `{"jsonrpc":"2.0","id":3,"result":{"isError":true,"content":[{"type":"text","text":"Sign in first: ` + testURL + `."}]}}` + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, body)) + out, _ := runAll(t, challengedBridge(srv.URL, nil), + `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`+"\n") + + require.Len(t, out, 2) + assert.Equal(t, listChangedMsg, out[1]) + assert.Contains(t, out[0], "needs a one-time sign-in") +} + +func TestChallengeNeverTriggeredByInitialize(t *testing.T) { + srv, _ := newFakeEndpoint(t, unauthorized) + out, _ := runAll(t, challengedBridge(srv.URL, nil), `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`+"\n") + + require.Len(t, out, 1) + reply := parseReply(t, out[0]) + require.NotNil(t, reply.Error) + assert.Equal(t, codeUpstream, reply.Error.Code) + assert.Nil(t, reply.Result) +} + +func TestAuthenticateSignsInAndSendsListChanged(t *testing.T) { + srv, fake := challengedService(t) + signIn := &signInStub{reply: func(int) (bool, error) { return false, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + challenge(t, p) + p.send(t, `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines := p.waitLines(t, 3) + require.NoError(t, p.finish(t)) + + assert.Equal(t, []bool{true}, signIn.calls()) + assert.Len(t, fake.requests(), 1, "authenticate is answered locally, never sent upstream; only the list is") + reply := parseReply(t, lines[1]) + assert.JSONEq(t, `4`, string(reply.ID)) + assert.Equal(t, false, reply.Result["isError"]) + assert.Contains(t, lines[1], "Signed in to system.ai.github on profile logfood. Tools are available now") + assert.Equal(t, listChangedMsg, lines[2]) +} + +func TestAuthenticateSaysAlreadySignedInAfterSignIn(t *testing.T) { + srv, _ := challengedService(t) + signIn := &signInStub{reply: func(call int) (bool, error) { return call > 1, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + challenge(t, p) + + p.send(t, fmt.Sprintf(authenticate, 5)) + p.waitLines(t, 3) + p.send(t, fmt.Sprintf(authenticate, 6)) + lines := p.waitLines(t, 5) + + assert.Equal(t, []bool{true, true}, signIn.calls(), "a sign-in that completed does not hold the page back") + assert.Contains(t, lines[3], "Already signed in to system.ai.github") + assert.Equal(t, listChangedMsg, lines[4], "an agent whose list still shows only authenticate is asked to list again") + require.NoError(t, p.finish(t)) +} + +func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { + srv, _ := challengedService(t) + signIn := &signInStub{reply: func(call int) (bool, error) { + if call == 1 { + return false, errors.New("sign-in to system.ai.github on profile logfood did not complete in 5 minutes. Finish at " + testURL + + " then retry: databricks aitools mcp login system.ai.github --profile logfood") + } + return false, nil + }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + challenge(t, p) + + p.send(t, fmt.Sprintf(authenticate, 7)) + lines := p.waitLines(t, 2) + require.Len(t, lines, 2) + assert.Equal(t, true, parseReply(t, lines[1]).Result["isError"]) + text := resultText(t, lines[1]) + assert.Contains(t, text, "did not complete in 5 minutes. Finish at "+testURL) + assert.Contains(t, text, "then retry: databricks aitools mcp login system.ai.github --profile logfood") + + p.send(t, fmt.Sprintf(authenticate, 8)) + lines = p.waitLines(t, 4) + + assert.Equal(t, []bool{true, true}, signIn.calls(), "a timed-out attempt may open the page again") + assert.Contains(t, lines[2], "Signed in to system.ai.github") + require.NoError(t, p.finish(t)) +} + +func TestAuthenticateReportsFailureWithoutListChanged(t *testing.T) { + srv, _ := challengedService(t) + // The error names the service and the profile itself, as the sign-in's errors do, so the answer does not repeat them. + signIn := &signInStub{reply: func(int) (bool, error) { + return false, errors.New("sign-in to system.ai.github failed on profile logfood. Run: databricks aitools mcp login system.ai.github") + }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + challenge(t, p) + p.send(t, `{"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines := p.waitLines(t, 2) + require.NoError(t, p.finish(t)) + + require.Len(t, lines, 2, "the failure is the only answer to authenticate, and no list_changed follows it") + assert.Equal(t, true, parseReply(t, lines[1]).Result["isError"]) + assert.Equal(t, "sign-in to system.ai.github failed on profile logfood. Run: databricks aitools mcp login system.ai.github", resultText(t, lines[1])) +} + +// TestChallengeTurnsOnAfterSignIn runs the flow an agent sees: a challenged list, authenticate, then the +// list passes through once the credential exists upstream. +func TestChallengeTurnsOnAfterSignIn(t *testing.T) { + var signedIn atomic.Bool + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + if signedIn.Load() { + answerWith(http.StatusOK, listToolsLater)(w, r) + return + } + answerWith(http.StatusOK, listChallenge)(w, r) + }) + signIn := &signInStub{reply: func(int) (bool, error) { + signedIn.Store(true) + return false, nil + }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + + p.send(t, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) + lines := p.waitLines(t, 1) + assert.Equal(t, []string{"authenticate"}, toolNames(t, lines[0])) + + p.send(t, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines = p.waitLines(t, 3) + assert.Contains(t, lines[1], "Signed in to system.ai.github") + assert.Equal(t, listChangedMsg, lines[2]) + + p.send(t, `{"jsonrpc":"2.0","id":4,"method":"tools/list"}`) + lines = p.waitLines(t, 4) + assert.Equal(t, []string{"echo"}, toolNames(t, lines[3])) + require.NoError(t, p.finish(t)) +} + +// TestChallengeHintsPrintTheLoginCommand checks that the challenge and the timeout print the login command that the +// command built, so a pinned config file and a profile name with a space are quoted for a shell. +func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, callChallenge)) + var calls atomic.Int32 + signIn := func(ctx context.Context, _ SignInRequest) (bool, bool, error) { + calls.Add(1) + return false, true, errors.New("sign-in to system.ai.github did not complete in 5 minutes. Finish at " + testURL + " then retry: " + loginCommand) + } + b := bridgeFor(srv.URL) + b.Hooks = newSignInHooks(&Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}, io.Discard) + p := startPiped(t, t.Context(), b) + + p.send(t, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`) + lines := p.waitLines(t, 2) + assert.Contains(t, resultText(t, lines[0]), "Call the authenticate tool, or run: "+loginCommand+". If") + + p.send(t, `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines = p.waitLines(t, 3) + require.NoError(t, p.finish(t)) + assert.EqualValues(t, 1, calls.Load()) + assert.Contains(t, resultText(t, lines[2]), "then retry: "+loginCommand) +} + +// TestAuthenticateWritesNothingWhenCancelled checks that an authenticate call the client cancelled is not answered: the +// MCP spec has no response to a cancelled request, and the client has stopped waiting for one. +func TestAuthenticateWritesNothingWhenCancelled(t *testing.T) { + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + if m, _ := parseMessage(body); m.Method != "tools/list" { + w.WriteHeader(http.StatusAccepted) + return + } + answerWith(http.StatusOK, listChallenge)(w, r) + }) + entered, returned := make(chan struct{}), make(chan struct{}) + signIn := func(ctx context.Context, _ SignInRequest) (bool, bool, error) { + close(entered) + <-ctx.Done() + close(returned) + return false, true, ctx.Err() + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + + p.send(t, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) + p.waitLines(t, 1) + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + waitClosed(t, entered, "the sign-in to start") + p.send(t, `{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":5}}`) + waitClosed(t, returned, "the sign-in to return") + + require.NoError(t, p.finish(t)) + lines := outLines(p.out.String()) + require.Len(t, lines, 1, "only the tools/list answer is written") + assert.Equal(t, []string{"authenticate"}, toolNames(t, lines[0])) +} + +// attempt is the outcome of one sign-in in a test. A cancelled attempt waits for its context, as a call the client +// cancelled does, and then returns the context's error. +type attempt struct { + cancel bool + urlShown bool + alreadySignedIn bool + err error +} + +// TestAuthenticateOpensPageAgainUnlessACancelledAttemptShowedIt checks when a later authenticate call opens the sign-in +// page again. The page is held back only while a cancelled attempt that showed it may still be open in the user's +// browser. A timeout, a failure, an answer that needs no sign-in, a success that is later revoked, and a cancelled +// attempt that never showed the page all open it again. +func TestAuthenticateOpensPageAgainUnlessACancelledAttemptShowedIt(t *testing.T) { + cases := map[string]struct { + first attempt + wantOpen bool + }{ + "cancelled after the page was shown": {first: attempt{cancel: true, urlShown: true}, wantOpen: false}, + "cancelled before the page was shown": {first: attempt{cancel: true}, wantOpen: true}, + "timed out": {first: attempt{urlShown: true, err: errors.New("timed out")}, wantOpen: true}, + "failed": {first: attempt{urlShown: true, err: errors.New("sign-in failed")}, wantOpen: true}, + "error before the page": {first: attempt{err: errors.New("credential read failed")}, wantOpen: true}, + "already signed in": {first: attempt{alreadySignedIn: true, urlShown: true}, wantOpen: true}, + "signed in, then revoked": {first: attempt{urlShown: true}, wantOpen: true}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listChallenge)) + entered, done := make(chan struct{}), make(chan struct{}) + var mu sync.Mutex + var opens []bool + signIn := func(ctx context.Context, req SignInRequest) (bool, bool, error) { + mu.Lock() + opens = append(opens, req.Open) + first := len(opens) == 1 + mu.Unlock() + if !first { + return false, true, nil + } + if !tc.first.cancel { + return tc.first.alreadySignedIn, tc.first.urlShown, tc.first.err + } + close(entered) + <-ctx.Done() + close(done) + return false, tc.first.urlShown, ctx.Err() + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + + p.send(t, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) + p.waitLines(t, 1) + p.send(t, fmt.Sprintf(authenticate, 5)) + if tc.first.cancel { + waitClosed(t, entered, "the first sign-in to start") + p.send(t, `{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":5}}`) + waitClosed(t, done, "the first sign-in to return") + } + p.send(t, fmt.Sprintf(authenticate, 6)) + require.Eventually(t, func() bool { + mu.Lock() + defer mu.Unlock() + return len(opens) == 2 + }, 10*time.Second, time.Millisecond) + require.NoError(t, p.finish(t)) + + mu.Lock() + defer mu.Unlock() + require.Len(t, opens, 2) + assert.True(t, opens[0], "the first attempt opens the page") + assert.Equal(t, tc.wantOpen, opens[1]) + }) + } +} + +// TestAuthenticateIsForwardedBeforeAChallenge checks that before the service challenges the session, a tool named +// authenticate is the upstream's tool and is forwarded. The proxy's tool exists only after a challenge. +func TestAuthenticateIsForwardedBeforeAChallenge(t *testing.T) { + const upstream = `{"jsonrpc":"2.0","id":4,"result":{"content":[{"type":"text","text":"upstream"}]}}` + srv, fake := newFakeEndpoint(t, answerWith(http.StatusOK, upstream)) + signIn := &signInStub{reply: func(int) (bool, error) { return false, nil }} + out, _ := runAll(t, challengedBridge(srv.URL, signIn.SignIn), + `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`+"\n") + + assert.Empty(t, signIn.calls(), "no sign-in runs for a tool the service answers") + assert.Len(t, fake.requests(), 1) + assert.Equal(t, []string{upstream}, out) +} + +// TestChallengeMatchesServiceNameWithoutCase checks that a service named in another case is challenged by its sign-in +// URL. Binding and Unity Catalog names are compared without case, so `--service SYSTEM.AI.GITHUB` is a service too. +func TestChallengeMatchesServiceNameWithoutCase(t *testing.T) { + const body = `{"jsonrpc":"2.0","id":2,"error":{"code":-32042,"message":"x","data":{"elicitations":[` + + `{"mode":"url","url":"` + testURL + `"}]}}}` + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, body)) + b := bridgeFor(srv.URL) + b.Hooks = newSignInHooks(&Challenge{Service: "SYSTEM.AI.GITHUB", Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: loginCommand}, io.Discard) + out, _ := runAll(t, b, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") + + require.Len(t, out, 1) + assert.Equal(t, []string{"authenticate"}, toolNames(t, out[0])) +} + +// TestChallengeTextAroundSignInURL checks that a sign-in URL in the text of an error result is recognized whatever +// punctuation surrounds it, as a sentence or a list puts it around the URL. +func TestChallengeTextAroundSignInURL(t *testing.T) { + for _, around := range []struct{ before, after string }{ + {"Sign in first: ", "."}, + {"Sign in (", ")"}, + {"Sign in [", "]"}, + {`Sign in "`, `".`}, + {"Sign in '", "';"}, + {"[Sign in](", ")"}, + {"<", ">"}, + {"**", "**"}, + {"Open **", "** now."}, + } { + t.Run(around.before+"URL"+around.after, func(t *testing.T) { + text, err := json.Marshal(around.before + testURL + around.after) + require.NoError(t, err) + body := `{"jsonrpc":"2.0","id":3,"result":{"isError":true,"content":[{"type":"text","text":` + string(text) + `}]}}` + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, body)) + out, _ := runAll(t, challengedBridge(srv.URL, nil), + `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`+"\n") + + require.Len(t, out, 2) + assert.Equal(t, listChangedMsg, out[1]) + assert.Contains(t, out[0], "needs a one-time sign-in") + }) + } +} + +// TestChallengeAfterAlreadySignedInAsksForLogout checks that when the service challenges again after an "Already signed in" +// answer, the next authenticate does not sign in. The credential reads ACTIVE but the service refuses it, so the answer names +// the logout command. The authenticate after that runs the sign-in as usual. +func TestChallengeAfterAlreadySignedInAsksForLogout(t *testing.T) { + srv, _ := challengedService(t) + signIn := &signInStub{reply: func(call int) (bool, error) { return call == 1, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + challenge(t, p) + + // "Already signed in" is followed by tools/list_changed. + p.send(t, fmt.Sprintf(authenticate, 5)) + p.waitLines(t, 3) + p.send(t, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) + p.waitLines(t, 4) + p.send(t, fmt.Sprintf(authenticate, 7)) + p.waitLines(t, 5) + p.send(t, fmt.Sprintf(authenticate, 8)) + lines := p.waitLines(t, 7) + require.NoError(t, p.finish(t)) + + // The answer to 7 names the logout command and sends no list_changed; the answer to 8 is the sign-in's. + assert.Len(t, signIn.requests(), 2, "the answer with the logout command starts no sign-in") + assert.Equal(t, "system.ai.github on profile logfood reads as signed in, but the service still asks for a sign-in. "+ + "Run: databricks aitools mcp logout system.ai.github --profile logfood, then call authenticate again.", resultText(t, lines[4])) + assert.Contains(t, resultText(t, lines[5]), "Signed in to system.ai.github on profile logfood.") + assert.Equal(t, listChangedMsg, lines[6]) +} + +// TestAuthenticateSendsProgressOnEachPoll checks that an authenticate call with a progress token gets a progress +// notification on each poll of its wait, numbered in order, before its answer. Progress keeps a client that times out +// requests without progress from giving up while the user signs in. +func TestAuthenticateSendsProgressOnEachPoll(t *testing.T) { + srv, _ := challengedService(t) + proceed := make(chan struct{}) + signIn := func(_ context.Context, req SignInRequest) (bool, bool, error) { + for range 2 { + req.Progress("") + <-proceed + } + return false, true, nil + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + challenge(t, p) + + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{},"_meta":{"progressToken":"sign-in-1"}}}`) + lines := p.waitLines(t, 2) + assert.JSONEq(t, `{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"sign-in-1","progress":1,"message":"Waiting for the sign-in to GitHub to complete."}}`, lines[1]) + proceed <- struct{}{} + + lines = p.waitLines(t, 3) + assert.JSONEq(t, `{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"sign-in-1","progress":2,"message":"Waiting for the sign-in to GitHub to complete."}}`, lines[2]) + proceed <- struct{}{} + + lines = p.waitLines(t, 5) + require.NoError(t, p.finish(t)) + assert.Contains(t, lines[3], "Signed in to system.ai.github") + assert.Equal(t, listChangedMsg, lines[4]) +} + +// TestAuthenticateSendsNoProgressWithoutToken checks that a call without a progress token gets no progress notification. +func TestAuthenticateSendsNoProgressWithoutToken(t *testing.T) { + srv, _ := challengedService(t) + signIn := func(_ context.Context, req SignInRequest) (bool, bool, error) { + req.Progress("") + req.Progress("") + return false, true, nil + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + challenge(t, p) + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines := p.waitLines(t, 3) + require.NoError(t, p.finish(t)) + + require.Len(t, lines, 3) + assert.NotContains(t, strings.Join(lines, "\n"), "notifications/progress") +} + +// TestAuthenticateSendsNoProgressForNullToken checks that a progressToken of null is no token: the client asked for no +// progress, so none is sent. +func TestAuthenticateSendsNoProgressForNullToken(t *testing.T) { + srv, _ := challengedService(t) + signIn := func(_ context.Context, req SignInRequest) (bool, bool, error) { + req.Progress("") + return false, true, nil + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + challenge(t, p) + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{},"_meta":{"progressToken":null}}}`) + lines := p.waitLines(t, 3) + require.NoError(t, p.finish(t)) + + assert.NotContains(t, strings.Join(lines, "\n"), "notifications/progress") +} + +// listAnswer answers each tools/list with the challenge while challenged is set, and with one tool otherwise. A tools/call +// is answered by the upstream's tool, as a signed-in service does. Unlike the fixtures above it answers with the id of +// each request, so later requests match their answers. +func listAnswer(challenged *atomic.Bool) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + m, _ := parseMessage(body) + w.Header().Set("Content-Type", "application/json") + switch { + case m.Method == "tools/list" && challenged.Load(): + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"error":{"code":-32006,"message":"rejected","data":null}}`, m.ID) + case m.Method == "tools/list": + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}}]}}`, m.ID) + case m.Method == methodToolsCall: + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"content":[{"type":"text","text":"upstream"}],"isError":false}}`, m.ID) + default: + w.WriteHeader(http.StatusAccepted) + } + } +} + +// TestChallengeAfterSuccessDoesNotAskForLogout checks that a challenge long after an "Already signed in" answer does not +// get the logout answer. A successful answer from the service ends that state, so only a challenge that follows the answer +// with no success in between does. +func TestChallengeAfterSuccessDoesNotAskForLogout(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, listAnswer(&challenged)) + signIn := &signInStub{reply: func(int) (bool, error) { return true, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + challenge(t, p) + + p.send(t, fmt.Sprintf(authenticate, 5)) + p.waitLines(t, 3) + challenged.Store(false) + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/list"}`) + p.waitLines(t, 4) + challenged.Store(true) + p.send(t, `{"jsonrpc":"2.0","id":7,"method":"tools/list"}`) + p.waitLines(t, 5) + p.send(t, fmt.Sprintf(authenticate, 8)) + lines := p.waitLines(t, 6) + require.NoError(t, p.finish(t)) + + assert.Len(t, signIn.requests(), 2, "a challenge after a success is not a challenge to the credential that was reported signed in") + assert.Contains(t, resultText(t, lines[5]), "Already signed in to system.ai.github") +} + +// TestAuthenticateIsForwardedOnceTheListPasses checks that the authenticate tool is the proxy's only while its list is +// shown. Once a tools/list passes through unchallenged, the list is the service's again, and a tool of the service named +// authenticate is forwarded to it. +func TestAuthenticateIsForwardedOnceTheListPasses(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, listAnswer(&challenged)) + signIn := &signInStub{reply: func(int) (bool, error) { + challenged.Store(false) + return false, nil + }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + challenge(t, p) + + p.send(t, fmt.Sprintf(authenticate, 5)) + p.waitLines(t, 3) + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/list"}`) + p.waitLines(t, 4) + p.send(t, fmt.Sprintf(authenticate, 7)) + lines := p.waitLines(t, 5) + require.NoError(t, p.finish(t)) + + assert.Equal(t, []bool{true}, signIn.calls(), "the forwarded call starts no sign-in") + assert.Equal(t, `{"jsonrpc":"2.0","id":7,"result":{"content":[{"type":"text","text":"upstream"}],"isError":false}}`, lines[4]) +} + +// TestQueuedAuthenticateCancelledTouchesNothing checks that an authenticate call the client cancels while it waits for +// the sign-in ahead of it neither answers nor starts a sign-in, and does not clear the record that the page was cancelled. +// The next call therefore does not open the page again. +func TestQueuedAuthenticateCancelledTouchesNothing(t *testing.T) { + srv, _ := challengedService(t) + entered, returned := make(chan struct{}), make(chan struct{}) + var mu sync.Mutex + var opens []bool + signIn := func(ctx context.Context, req SignInRequest) (bool, bool, error) { + mu.Lock() + opens = append(opens, req.Open) + first := len(opens) == 1 + mu.Unlock() + if !first { + return false, false, nil + } + close(entered) + <-ctx.Done() + close(returned) + return false, true, ctx.Err() + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + challenge(t, p) + + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + waitClosed(t, entered, "the first sign-in to start") + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + p.send(t, `{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":6}}`) + p.send(t, `{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":5}}`) + waitClosed(t, returned, "the first sign-in to return") + p.send(t, `{"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + require.Eventually(t, func() bool { + mu.Lock() + defer mu.Unlock() + return len(opens) == 2 + }, 10*time.Second, time.Millisecond) + require.NoError(t, p.finish(t)) + + mu.Lock() + defer mu.Unlock() + assert.Equal(t, []bool{true, false}, opens, "the call after the cancelled ones keeps the page closed") + out := p.out.String() + assert.NotContains(t, out, `"id":5`, "a cancelled call is not answered") + assert.NotContains(t, out, `"id":6`, "a call cancelled while it waits is not answered") +} + +// TestQueuedAuthenticateReportsProgress checks that an authenticate call waiting for the sign-in ahead of it reports +// progress on its progress token, so that a client that times out requests without progress does not give up on it. +func TestQueuedAuthenticateReportsProgress(t *testing.T) { + prev := queueProgressInterval + queueProgressInterval = time.Millisecond + t.Cleanup(func() { queueProgressInterval = prev }) + srv, _ := challengedService(t) + entered, release := make(chan struct{}), make(chan struct{}) + var mu sync.Mutex + calls := 0 + signIn := func(context.Context, SignInRequest) (bool, bool, error) { + mu.Lock() + calls++ + first := calls == 1 + mu.Unlock() + if first { + close(entered) + <-release + return false, true, nil + } + return true, false, nil + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + challenge(t, p) + + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + waitClosed(t, entered, "the first sign-in to start") + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"authenticate","arguments":{},"_meta":{"progressToken":"queued"}}}`) + require.Eventually(t, func() bool { + return strings.Contains(p.out.String(), `"progressToken":"queued"`) + }, 10*time.Second, time.Millisecond) + close(release) + require.Eventually(t, func() bool { + return strings.Contains(p.out.String(), "Already signed in") + }, 10*time.Second, time.Millisecond) + require.NoError(t, p.finish(t)) + + lines := outLines(p.out.String()) + var queued, signedIn int + for _, line := range lines { + if strings.Contains(line, `"progressToken":"queued"`) { + queued++ + } + if strings.Contains(line, "Signed in to") { + signedIn = queued + } + } + assert.Positive(t, signedIn, "the queued call's progress comes before the first sign-in answers") +} + +// TestAuthenticateAnswersWithTheSignInError checks that a sign-in that ends in an error, such as a timeout, is answered +// with the error's text. The text is the sign-in's own, so it names the service, the profile and the fix. +func TestAuthenticateAnswersWithTheSignInError(t *testing.T) { + const text = "sign-in to system.ai.github on profile logfood did not complete in 5 minutes (the last check failed: HTTP 503: unavailable). " + + "Finish at " + testURL + " then retry: databricks aitools mcp login system.ai.github --profile logfood" + srv, _ := challengedService(t) + signIn := &signInStub{reply: func(int) (bool, error) { return false, errors.New(text) }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + challenge(t, p) + + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines := p.waitLines(t, 2) + require.NoError(t, p.finish(t)) + + assert.Equal(t, true, parseReply(t, lines[1]).Result["isError"]) + assert.Equal(t, text, resultText(t, lines[1])) +} + +// TestAuthenticateProgressNamesThePageWhenItIsNotOpened checks that progress names the sign-in page when the sign-in says +// the page was not opened, so the client can show the user where to finish, and names no page otherwise. +func TestAuthenticateProgressNamesThePageWhenItIsNotOpened(t *testing.T) { + srv, _ := challengedService(t) + var mu sync.Mutex + calls := 0 + signIn := func(_ context.Context, req SignInRequest) (bool, bool, error) { + mu.Lock() + calls++ + second := calls == 2 + mu.Unlock() + if second { + req.Progress(testURL) + } else { + req.Progress("") + } + return false, true, nil + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + challenge(t, p) + + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{},"_meta":{"progressToken":"t"}}}`) + p.waitLines(t, 3) + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"authenticate","arguments":{},"_meta":{"progressToken":"t"}}}`) + lines := p.waitLines(t, 6) + require.NoError(t, p.finish(t)) + + assert.JSONEq(t, `{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"t","progress":1,"message":"Waiting for the sign-in to GitHub to complete."}}`, lines[1]) + assert.JSONEq(t, `{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"t","progress":1,"message":"Waiting for the sign-in to GitHub to complete. Finish at `+testURL+`"}}`, lines[4]) +} + +// TestUnauthorizedIsAuthFailureUnlessItAsksForSignIn checks that a 401 is a provider sign-in challenge only when its body +// asks for one. A 401 that is bare, or whose body is a Databricks authentication error, rejects the Databricks credential, +// which a provider sign-in does not fix. It is answered with the login hint, and offers no authenticate tool. +func TestUnauthorizedIsAuthFailureUnlessItAsksForSignIn(t *testing.T) { + cases := map[string]string{ + "bare": "unauthorized", + "databricks error": `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`, + } + for name, body := range cases { + t.Run(name, func(t *testing.T) { + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, _ *http.Request) { + http.Error(w, body, http.StatusUnauthorized) + }) + signIn := &signInStub{reply: func(int) (bool, error) { return false, nil }} + out, _ := runAll(t, challengedBridge(srv.URL, signIn.SignIn), `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") + + require.Len(t, out, 1) + reply := parseReply(t, out[0]) + require.NotNil(t, reply.Error) + assert.Equal(t, codeUpstream, reply.Error.Code) + assert.Contains(t, reply.Error.Message, "Run: databricks auth login --profile logfood, then restart the MCP server in your agent") + assert.Empty(t, signIn.calls(), "a rejected credential starts no sign-in") + }) + } +} + +// TestRejectedCredentialDoesNotAskForLogout checks a rejected Databricks token after "Already signed in": the credential reads +// as signed in, and the service rejects the token with a 401. The 401 is an auth failure, not a challenge, so the next +// authenticate runs the sign-in as usual, and the logout answer is not given. +func TestRejectedCredentialDoesNotAskForLogout(t *testing.T) { + var rejected atomic.Bool + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + if rejected.Load() { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + listAnswer(&challenged)(w, r) + }) + signIn := &signInStub{reply: func(int) (bool, error) { return true, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + challenge(t, p) + p.send(t, fmt.Sprintf(authenticate, 5)) + p.waitLines(t, 3) + + // The credential reads as signed in, and the service now rejects the token on the re-list. + rejected.Store(true) + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/list"}`) + p.waitLines(t, 4) + p.send(t, fmt.Sprintf(authenticate, 7)) + lines := p.waitLines(t, 6) + require.NoError(t, p.finish(t)) + + assert.Len(t, signIn.requests(), 2) + assert.Contains(t, resultText(t, lines[4]), "Already signed in to system.ai.github") +} + +// TestAuthFailureNamesPinnedConfigFile checks that the auth hint names the config file a server is pinned to, as the +// other hints do, and quotes it for a shell. +func TestAuthFailureNamesPinnedConfigFile(t *testing.T) { + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, _ *http.Request) { + http.Error(w, "unauthorized", http.StatusUnauthorized) + }) + b := bridgeFor(srv.URL) + b.Hooks = newSignInHooks(&Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, PinnedFile: "/work/cfg/my file.cfg"}, io.Discard) + out, _ := runAll(t, b, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") + + require.Len(t, out, 1) + assert.Contains(t, parseReply(t, out[0]).Error.Message, "Run: DATABRICKS_CONFIG_FILE='/work/cfg/my file.cfg' databricks auth login --profile logfood, then restart") +} + +// TestOnlyTheResponseToTheRequestEndsTheSignInState checks which upstream messages can change the sign-in state. A +// notification that comes before the answer is not the answer, and neither is an answer with a 200 status that is an +// error, so "Already signed in" and the authenticate tool stay as they were. +func TestOnlyTheResponseToTheRequestEndsTheSignInState(t *testing.T) { + progress := `{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"t","progress":1}}` + cases := []struct { + name, request, id, challenge string + }{ + {"tools/list", `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, "2", listChallenge}, + {"tools/call", `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`, "3", callChallenge}, + } + for _, tc := range cases { + t.Run(tc.name+" progress before the challenge", func(t *testing.T) { + h := newSignInHooks(testChallenge(nil), io.Discard) + h.alreadySignedIn = true + + out, err := h.Result(t.Context(), []byte(tc.request), http.StatusOK, []byte(progress)) + require.NoError(t, err) + assert.Equal(t, [][]byte{[]byte(progress)}, out, "a notification passes through untouched") + assert.True(t, h.alreadySignedIn, "a notification does not end the state") + + _, err = h.Result(t.Context(), []byte(tc.request), http.StatusOK, []byte(tc.challenge)) + require.NoError(t, err) + assert.True(t, h.stillChallenged, "the challenge after the answer leaves the logout answer for the next authenticate") + }) + t.Run(tc.name+" answers that are not results", func(t *testing.T) { + rpcError := fmt.Sprintf(`{"jsonrpc":"2.0","id":%s,"error":{"code":-32603,"message":"rate limited"}}`, tc.id) + bridgeError := errorResponse(json.RawMessage(tc.id), codeUpstream, "no data from upstream for 2m0s") + for _, msg := range []string{rpcError, string(bridgeError)} { + h := newSignInHooks(testChallenge(nil), io.Discard) + h.exposed = true + h.alreadySignedIn = true + + out, err := h.Result(t.Context(), []byte(tc.request), http.StatusOK, []byte(msg)) + require.NoError(t, err) + assert.Equal(t, [][]byte{[]byte(msg)}, out, "the answer passes through untouched") + assert.True(t, h.exposed, "the authenticate tool stays listed") + assert.True(t, h.alreadySignedIn, "the answer is not a success") + } + }) + } +} + +// testChallenge is the challenge of the tests that drive the hooks directly. signIn is its sign-in. +func testChallenge(signIn SignInFunc) *Challenge { + return &Challenge{ + Service: testService, + Profile: "logfood", + Host: testHost, + Label: "GitHub", + LoginCommand: "databricks aitools mcp login system.ai.github --profile logfood", + LogoutCommand: "databricks aitools mcp logout system.ai.github --profile logfood", + SignIn: signIn, + } +} + +// TestSuccessEndsStillChallenged checks that a challenge after "Already signed in" leaves the logout answer only until the +// next success. The agent may not call authenticate before the service answers again, so a state left pending would answer a +// much later, unrelated authenticate with the logout command. +func TestSuccessEndsStillChallenged(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, listAnswer(&challenged)) + signIn := &signInStub{reply: func(call int) (bool, error) { return call == 1, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + challenge(t, p) + + p.send(t, fmt.Sprintf(authenticate, 5)) + p.waitLines(t, 3) + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/list"}`) + p.waitLines(t, 4) + challenged.Store(false) + p.send(t, `{"jsonrpc":"2.0","id":7,"method":"tools/list"}`) + p.waitLines(t, 5) + challenged.Store(true) + p.send(t, `{"jsonrpc":"2.0","id":8,"method":"tools/list"}`) + p.waitLines(t, 6) + p.send(t, fmt.Sprintf(authenticate, 9)) + lines := p.waitLines(t, 8) + require.NoError(t, p.finish(t)) + + assert.Len(t, signIn.requests(), 2, "a success after the challenge ends the state, so the authenticate signs in") + assert.NotContains(t, strings.Join(lines, "\n"), "reads as signed in") + assert.Contains(t, resultText(t, lines[6]), "Signed in to system.ai.github on profile logfood.") +} + +// callAnswer answers a tools/call with the sign-in challenge while challenged is set, and each tools/list with the +// service's one tool. A challenge to a call does not challenge the list, which is the service's, so the list passes. +func callAnswer(challenged *atomic.Bool) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + m, _ := parseMessage(body) + w.Header().Set("Content-Type", "application/json") + switch { + case m.Method == methodToolsCall && challenged.Load(): + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"error":{"code":-32006,"message":"rejected","data":null}}`, m.ID) + case m.Method == methodToolsList || m.Method == methodToolsCall: + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}}]}}`, m.ID) + default: + w.WriteHeader(http.StatusAccepted) + } + } +} + +// TestCallChallengeKeepsAuthenticateListed checks the agent's list after a challenged tools/call. The list passes, since +// the service does not challenge its tools, but the agent is told to call authenticate, so the list carries it until a +// sign-in ends. Once the sign-in ends, the next list is the service's own. +func TestCallChallengeKeepsAuthenticateListed(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, callAnswer(&challenged)) + signIn := &signInStub{reply: func(int) (bool, error) { + challenged.Store(false) + return false, nil + }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + + p.send(t, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`) + p.waitLines(t, 2) + p.send(t, `{"jsonrpc":"2.0","id":4,"method":"tools/list"}`) + lines := p.waitLines(t, 3) + assert.Equal(t, []string{"echo", "authenticate"}, toolNames(t, lines[2]), "the list carries the authenticate tool after the call challenge") + + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines = p.waitLines(t, 5) + assert.Contains(t, resultText(t, lines[3]), "Signed in to system.ai.github on profile logfood.") + + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/list"}`) + lines = p.waitLines(t, 6) + require.NoError(t, p.finish(t)) + + assert.Equal(t, []bool{true}, signIn.calls()) + assert.Equal(t, []string{"echo"}, toolNames(t, lines[5]), "after the sign-in the list is the service's own") +} + +// TestProgressIsNotSentAfterCancel checks that a poll which fires after the client cancelled the call writes no progress +// for it. The sign-in reports its poll once the cancellation has arrived, as a poll that was already in flight does. +func TestProgressIsNotSentAfterCancel(t *testing.T) { + srv, _ := challengedService(t) + entered, returned := make(chan struct{}), make(chan struct{}) + signIn := func(ctx context.Context, req SignInRequest) (bool, bool, error) { + close(entered) + <-ctx.Done() + req.Progress(testURL) + close(returned) + return false, true, ctx.Err() + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) + challenge(t, p) + + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{},"_meta":{"progressToken":"t"}}}`) + waitClosed(t, entered, "the sign-in to start") + p.send(t, `{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":5}}`) + waitClosed(t, returned, "the sign-in to return") + require.NoError(t, p.finish(t)) + + for _, line := range outLines(p.out.String()) { + assert.NotContains(t, line, "notifications/progress", "a cancelled call gets no progress") + } +} + +// TestURLPatternFindsURLsInText checks that a sign-in URL is found in the text of a tool result, both in a markdown link +// and between angle brackets, and that the punctuation around it is left out. +func TestURLPatternFindsURLsInText(t *testing.T) { + text := "Open [the page](" + testURL + ") or <" + testHost + "/x>." + assert.Equal(t, []string{testURL, testHost + "/x"}, urlPattern().FindAllString(text, -1)) +} + +// TestUndecodableResponsePassesThrough checks that a response whose shape the sign-in checks do not read, a result that is +// not an object or an error code that is not an integer, passes through on a sign-in service. It is neither a challenge nor +// a success, so the request is not failed and the sign-in state does not change. +func TestUndecodableResponsePassesThrough(t *testing.T) { + for name, msg := range map[string]string{ + "result is not an object": `{"jsonrpc":"2.0","id":2,"result":5}`, + "error code is not an integer": `{"jsonrpc":"2.0","id":2,"error":{"code":"x","message":"m"}}`, + } { + t.Run(name, func(t *testing.T) { + h := newSignInHooks(testChallenge(nil), io.Discard) + h.alreadySignedIn = true + + out, err := h.Result(t.Context(), []byte(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`), http.StatusOK, []byte(msg)) + require.NoError(t, err) + assert.Equal(t, [][]byte{[]byte(msg)}, out) + assert.True(t, h.alreadySignedIn, "the response is not a success") + assert.False(t, h.exposed, "the response is not a challenge") + }) + } +} + +// TestAlreadySignedInSurvivesListAfterCallChallenge checks the sequence where a challenged tools/call is answered with "Already +// signed in" and the list then passes. The list does not end that state, since the call challenge is still pending, so +// the next challenge to a call gets the logout answer. +func TestAlreadySignedInSurvivesListAfterCallChallenge(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, callAnswer(&challenged)) + signIn := &signInStub{reply: func(call int) (bool, error) { return call == 1, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + authenticate := `{"jsonrpc":"2.0","id":%d,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}` + + p.send(t, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`) + p.waitLines(t, 2) + p.send(t, fmt.Sprintf(authenticate, 5)) + lines := p.waitLines(t, 4) + assert.Contains(t, resultText(t, lines[2]), "Already signed in to system.ai.github") + + p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/list"}`) + p.waitLines(t, 5) + p.send(t, `{"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":"echo","arguments":{}}}`) + p.waitLines(t, 7) + p.send(t, fmt.Sprintf(authenticate, 8)) + lines = p.waitLines(t, 8) + require.NoError(t, p.finish(t)) + + assert.Len(t, signIn.requests(), 1, "the challenge after the answer, with the list passing in between, gets the logout answer") + assert.Contains(t, resultText(t, lines[7]), "reads as signed in, but the service still asks for a sign-in") +} + +// TestIsErrorResultChangesNoSignInState checks that a tools/call result with isError and no sign-in URL changes no state. +// It is neither a challenge nor a success, so "Already signed in", the pending logout answer and a call challenge all stay. +func TestIsErrorResultChangesNoSignInState(t *testing.T) { + result := `{"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"boom"}],"isError":true}}` + h := newSignInHooks(testChallenge(nil), io.Discard) + h.exposed = true + h.alreadySignedIn = true + h.stillChallenged = true + h.callChallenged = true + h.callPending = true + + out, err := h.Result(t.Context(), []byte(`{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`), http.StatusOK, []byte(result)) + require.NoError(t, err) + assert.Equal(t, [][]byte{[]byte(result)}, out) + assert.Equal(t, signInHooks{exposed: true, alreadySignedIn: true, stillChallenged: true, callChallenged: true, callPending: true}, signInHooks{ + exposed: h.exposed, alreadySignedIn: h.alreadySignedIn, stillChallenged: h.stillChallenged, callChallenged: h.callChallenged, callPending: h.callPending, + }) +} + +// TestCallChallengeListsAuthenticateOnFirstPageOnly checks a paginated list after a challenged tools/call. The authenticate +// tool is added to the first page, which has no cursor, and not to the pages after it. +func TestCallChallengeListsAuthenticateOnFirstPageOnly(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + m, _ := parseMessage(body) + w.Header().Set("Content-Type", "application/json") + var req struct { + Params struct { + Cursor string `json:"cursor"` + } `json:"params"` + } + _ = json.Unmarshal(body, &req) + switch { + case m.Method == methodToolsCall && challenged.Load(): + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"error":{"code":-32006,"message":"rejected","data":null}}`, m.ID) + case m.Method == methodToolsList && req.Params.Cursor == "": + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}}],"nextCursor":"page-2"}}`, m.ID) + case m.Method == methodToolsList: + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"more","inputSchema":{"type":"object"}}]}}`, m.ID) + default: + w.WriteHeader(http.StatusAccepted) + } + }) + p := startPiped(t, t.Context(), challengedBridge(srv.URL, (&signInStub{reply: func(int) (bool, error) { return false, nil }}).SignIn)) + + p.send(t, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`) + p.waitLines(t, 2) + p.send(t, `{"jsonrpc":"2.0","id":4,"method":"tools/list"}`) + p.waitLines(t, 3) + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/list","params":{"cursor":"page-2"}}`) + lines := p.waitLines(t, 4) + require.NoError(t, p.finish(t)) + + assert.Equal(t, []string{"echo", "authenticate"}, toolNames(t, lines[2])) + assert.Equal(t, []string{"more"}, toolNames(t, lines[3]), "the next page gets no authenticate tool") +} + +// TestCallChallengeKeepsTheServicesAuthenticateTool checks a service whose own list has a tool named authenticate. The list +// keeps that one alone, and a call to it is forwarded to the service, since the service's tool is not the sign-in. +func TestCallChallengeKeepsTheServicesAuthenticateTool(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + m, _ := parseMessage(body) + w.Header().Set("Content-Type", "application/json") + switch { + case m.Method == methodToolsCall && challenged.Load(): + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"error":{"code":-32006,"message":"rejected","data":null}}`, m.ID) + case m.Method == methodToolsCall: + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"content":[{"type":"text","text":"upstream"}],"isError":false}}`, m.ID) + case m.Method == methodToolsList: + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}},{"name":"authenticate","inputSchema":{"type":"object"}}]}}`, m.ID) + default: + w.WriteHeader(http.StatusAccepted) + } + }) + signIn := &signInStub{reply: func(int) (bool, error) { return false, nil }} + p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) + + p.send(t, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`) + p.waitLines(t, 2) + p.send(t, `{"jsonrpc":"2.0","id":4,"method":"tools/list"}`) + lines := p.waitLines(t, 3) + assert.Equal(t, []string{"echo", "authenticate"}, toolNames(t, lines[2]), "the service's authenticate is listed once") + + challenged.Store(false) + p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`) + lines = p.waitLines(t, 4) + require.NoError(t, p.finish(t)) + + assert.Empty(t, signIn.calls(), "a call to the service's authenticate starts no sign-in") + assert.Equal(t, `{"jsonrpc":"2.0","id":5,"result":{"content":[{"type":"text","text":"upstream"}],"isError":false}}`, lines[3]) +} diff --git a/libs/aitools/mcp/proxy/messages.go b/libs/aitools/mcp/proxy/messages.go index 2fc637d28dc..c78767500ab 100644 --- a/libs/aitools/mcp/proxy/messages.go +++ b/libs/aitools/mcp/proxy/messages.go @@ -15,7 +15,11 @@ const ( methodInitialize = "initialize" methodInitialized = "notifications/initialized" methodCancelled = "notifications/cancelled" + methodProgress = "notifications/progress" + methodListChanged = "notifications/tools/list_changed" methodPing = "ping" + methodToolsList = "tools/list" + methodToolsCall = "tools/call" keyResult = "result" keyInstructions = "instructions" @@ -24,6 +28,9 @@ const ( keyListChanged = "listChanged" ) +// jsonrpcVersion is the version member of each JSON-RPC message the bridge writes. +const jsonrpcVersion = "2.0" + var ( // errNotObject is returned for valid JSON whose top-level value is not an object. errNotObject = errors.New("message is not a JSON object") @@ -54,11 +61,12 @@ type rpcError struct { Message string `json:"message"` } -// rpcErrorResponse is a JSON-RPC error response. -type rpcErrorResponse struct { +// rpcResponse is a JSON-RPC response the bridge writes: a result, or an error. +type rpcResponse struct { JSONRPC string `json:"jsonrpc"` ID json.RawMessage `json:"id"` - Error rpcError `json:"error"` + Result any `json:"result,omitempty"` + Error *rpcError `json:"error,omitempty"` } // parseMessage decodes the envelope of one JSON-RPC message. A syntax error is reported as a @@ -166,7 +174,14 @@ func errorResponse(id json.RawMessage, code int, text string) []byte { id = jsonNull } // Cannot fail: the id came from a parsed message and the other fields are plain values. - b, _ := json.Marshal(rpcErrorResponse{JSONRPC: "2.0", ID: id, Error: rpcError{Code: code, Message: text}}) + b, _ := json.Marshal(rpcResponse{JSONRPC: jsonrpcVersion, ID: id, Error: &rpcError{Code: code, Message: text}}) + return b +} + +// resultResponse builds a JSON-RPC success response. +func resultResponse(id json.RawMessage, result any) []byte { + // Cannot fail: the id came from a parsed request and result holds only plain values. + b, _ := json.Marshal(rpcResponse{JSONRPC: jsonrpcVersion, ID: id, Result: result}) return b } diff --git a/libs/aitools/mcp/proxy/run.go b/libs/aitools/mcp/proxy/run.go index 5aa60d6322c..93bc86eecea 100644 --- a/libs/aitools/mcp/proxy/run.go +++ b/libs/aitools/mcp/proxy/run.go @@ -6,6 +6,7 @@ import ( "io" "maps" "net/http" + "sync" "github.com/databricks/cli/libs/auth" "github.com/databricks/databricks-sdk-go/config" @@ -38,6 +39,8 @@ type Options struct { Agent string // Transport performs the upstream requests. Nil means http.DefaultTransport, as in Bridge. Transport http.RoundTripper + // Challenge enables sign-in handling for a sign-in-required service. Nil passes everything through. + Challenge *Challenge } // Run bridges opts.In to the service endpoint until In closes or ctx is done. It returns an error @@ -54,15 +57,35 @@ func Run(ctx context.Context, opts Options) error { headers := map[string]string{"User-Agent": useragent.FromContext(ctx)} maps.Copy(headers, auth.WorkspaceIDHeaders(opts.Config)) + // The bridge's diagnostics and the progress of a sign-in are written to the same stderr, from different goroutines. + stderr := &serialWriter{w: opts.Err} b := &Bridge{ In: opts.In, Out: opts.Out, - Err: opts.Err, + Err: stderr, Endpoint: opts.Endpoint, Authenticate: opts.Authenticate, Headers: headers, Transport: opts.Transport, Instructions: fmt.Sprintf("This server is bound to Databricks workspace %s (CLI profile \"%s\"). Its tools act on that workspace only.", opts.Config.Host, opts.Profile), } + if opts.Challenge != nil { + // The initialize result must advertise tools/list_changed so an agent re-lists after sign-in. + b.ForceListChanged = true + b.Hooks = newSignInHooks(opts.Challenge, stderr) + } return b.Run(ctx) } + +// serialWriter writes to w one write at a time, so that the writes of different goroutines do not interleave or race. +type serialWriter struct { + mu sync.Mutex + w io.Writer +} + +// Write implements io.Writer. +func (s *serialWriter) Write(p []byte) (int, error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.w.Write(p) +} diff --git a/libs/aitools/mcp/proxy/run_test.go b/libs/aitools/mcp/proxy/run_test.go index 852298a6df0..8a8f195027d 100644 --- a/libs/aitools/mcp/proxy/run_test.go +++ b/libs/aitools/mcp/proxy/run_test.go @@ -89,3 +89,18 @@ func TestRunNamesWorkspaceAndProfileInInstructions(t *testing.T) { "Base.\n\nThis server is bound to Databricks workspace https://workspace.test (CLI profile \"logfood\"). Its tools act on that workspace only.", reply.Result["instructions"]) } + +func TestRunAdvertisesListChangedOnlyForSignInServices(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerInitialize) + initialize := `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}` + "\n" + cfg := &config.Config{Host: testHost} + + opts, out, _ := runOptions(t, srv.URL, cfg, initialize) + require.NoError(t, Run(t.Context(), opts)) + assert.Contains(t, out.String(), `"listChanged":false`) + + opts, out, _ = runOptions(t, srv.URL, cfg, initialize) + opts.Challenge = &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub"} + require.NoError(t, Run(t.Context(), opts)) + assert.Contains(t, out.String(), `"listChanged":true`) +} diff --git a/libs/aitools/mcp/url.go b/libs/aitools/mcp/url.go index 609c0626cd4..cb13be05a54 100644 --- a/libs/aitools/mcp/url.go +++ b/libs/aitools/mcp/url.go @@ -8,8 +8,11 @@ func ServiceURL(host, fullName string) string { return host + "/ai-gateway/mcp-services/" + url.PathEscape(fullName) } +// SignInPath is the path of the page where a user signs in to a sign-in-required service, on the workspace host. +const SignInPath = "/mcp-service-login" + // SignInURL returns the page where a user signs in to a sign-in-required service. func SignInURL(host, fullName, workspaceID string) string { query := url.Values{"name": {fullName}, "o": {workspaceID}} - return host + "/mcp-service-login?" + query.Encode() + return host + SignInPath + "?" + query.Encode() } diff --git a/libs/browser/browser.go b/libs/browser/browser.go index ca6ecfd0f02..1a13e70c360 100644 --- a/libs/browser/browser.go +++ b/libs/browser/browser.go @@ -31,17 +31,24 @@ 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) + return openURL(ctx, url, false, 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) + return openURL(ctx, url, true, false) } -// openURL implements Open and OpenOutputToStderr. stdoutToStderr sends the output of the browser to stderr. -func openURL(ctx context.Context, url string, stdoutToStderr bool) error { +// OpenOutputToStderrWithoutStdin is OpenOutputToStderr for a caller whose stdin is a protocol stream, such as the stdin of +// an MCP server. A browser command gets no stdin, so it cannot read that stream. The other behaviors are OpenOutputToStderr's. +func OpenOutputToStderrWithoutStdin(ctx context.Context, url string) error { + return openURL(ctx, url, true, true) +} + +// openURL implements the exported openers. stdoutToStderr sends the output of the browser to stderr, and noStdin gives a +// browser command no stdin. The default opener has no stdin to give up. +func openURL(ctx context.Context, url string, stdoutToStderr, noStdin bool) error { browserCmd := env.Get(ctx, "BROWSER") switch browserCmd { case "": @@ -50,7 +57,7 @@ func openURL(ctx context.Context, url string, stdoutToStderr bool) error { cmdio.LogString(ctx, "Open this URL in your browser:\n"+url) return nil default: - return openWithCommand(ctx, browserCmd, url, stdoutToStderr) + return openWithCommand(ctx, browserCmd, url, stdoutToStderr, noStdin) } } @@ -98,8 +105,8 @@ func openDefault(url string, stdoutToStderr bool) 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 { +// to stdout, or to stderr when stdoutToStderr is set. It reads no stdin when noStdin is set. +func openWithCommand(ctx context.Context, browserCmd, url string, stdoutToStderr, noStdin bool) error { e, err := exec.NewCommandExecutor(".") if err != nil { return err @@ -109,6 +116,9 @@ func openWithCommand(ctx context.Context, browserCmd, url string, stdoutToStderr } else { e.WithInheritOutput() } + if noStdin { + e.WithoutStdin() + } 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 00844b36f52..6be9e5620b2 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -3,6 +3,7 @@ package browser import ( "context" "fmt" + "io" "io/fs" "os" "os/exec" @@ -57,6 +58,8 @@ const ( 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 + // openerStdinEnv, when set in the browser command, makes it read up to 16 bytes of its stdin and print them. + openerStdinEnv = "LIBS_BROWSER_TEST_STDIN" ) // TestMain lets the tests use this test binary as a browser command that writes to stdout, as a launcher can. @@ -76,6 +79,11 @@ func TestOpenerChild(t *testing.T) { t.Skip("runs only as the browser command of the tests below") } fmt.Print("opener stdout") + if os.Getenv(openerStdinEnv) != "" { + buf := make([]byte, 16) + n, _ := os.Stdin.Read(buf) + fmt.Fprintf(os.Stderr, "stdin: %q", buf[:n]) + } sleepFile := os.Getenv(openerBackgroundEnv) if sleepFile == "" { return @@ -231,3 +239,35 @@ func TestOpenDefaultOverlappingOpensRestoreStreams(t *testing.T) { assert.Equal(t, stdout, browserpkg.Stdout) assert.Equal(t, stderr, browserpkg.Stderr) } + +// TestOpenWithoutStdinGivesCommandNoInput checks that a browser command opened without stdin reads none, so it cannot read +// the stream on this process's stdin, while the other openers give it that stream. The command writes what it read to +// stderr. +func TestOpenWithoutStdinGivesCommandNoInput(t *testing.T) { + t.Setenv(openerStdinEnv, "1") + // stdinWith makes the stdin of this process a pipe that holds data and stays open, so a command that inherits it + // would wait for more input than the 16 bytes it reads. + stdinWith := func(data string) { + r, w, err := os.Pipe() + require.NoError(t, err) + t.Cleanup(func() { + r.Close() + w.Close() + }) + _, err = io.WriteString(w, data) + require.NoError(t, err) + prevStdin := os.Stdin + os.Stdin = r + t.Cleanup(func() { os.Stdin = prevStdin }) + } + + stdinWith("protocol") + _, stderr, err := runOpener(t, OpenOutputToStderrWithoutStdin) + require.NoError(t, err) + assert.Contains(t, stderr, `stdin: ""`) + + stdinWith("protocol") + _, stderr, err = runOpener(t, OpenOutputToStderr) + require.NoError(t, err) + assert.Contains(t, stderr, `stdin: "protocol"`) +} diff --git a/libs/exec/exec.go b/libs/exec/exec.go index afb163a3f3b..26b4a9e26f2 100644 --- a/libs/exec/exec.go +++ b/libs/exec/exec.go @@ -78,6 +78,8 @@ type Executor struct { inheritOutput bool // stdoutToStderr sends the command's stdout to stderr when inheritOutput is set. stdoutToStderr bool + // noStdin gives the command no stdin when inheritOutput is set, instead of this process's stdin. + noStdin bool } // NewCommandExecutor creates an Executor with default output behavior (no inheritance) @@ -104,6 +106,12 @@ func (e *Executor) WithInheritOutputToStderr() { e.stdoutToStderr = true } +// WithoutStdin gives the command no stdin, so that it reads no input, with the output set as WithInheritOutput or +// WithInheritOutputToStderr sets it. It is for a caller whose stdin is a stream the command must not read. +func (e *Executor) WithoutStdin() { + e.noStdin = true +} + func NewCommandExecutorWithExecutable(dir string, execType ExecutableType) (*Executor, error) { f, ok := finders[execType] if !ok { @@ -145,7 +153,9 @@ func (e *Executor) start(cmd *osexec.Cmd, ec *execContext) (Command, error) { cmd.Stdout = os.Stderr } cmd.Stderr = os.Stderr - cmd.Stdin = os.Stdin + if !e.noStdin { + cmd.Stdin = os.Stdin + } return &command{cmd, ec, nil, nil}, cmd.Start() } stdout, err := cmd.StdoutPipe() diff --git a/libs/testserver/mcp_endpoint.go b/libs/testserver/mcp_endpoint.go index 52b600370b1..2b7323920d7 100644 --- a/libs/testserver/mcp_endpoint.go +++ b/libs/testserver/mcp_endpoint.go @@ -37,9 +37,9 @@ type mcpRPC struct { // McpEndpoint fakes POST /ai-gateway/mcp-services/{name}, a streamable HTTP MCP server with one // tool, echo. A name the workspace has no service for is 404. A service stored with an OAuth source -// connection is external: no credential is ever mapped to it here, so tools/list and tools/call -// answer -32006 as the gateway does before sign-in. Notifications and client responses, which have -// no method or no id, get 202 with no body. +// connection is external: until a credential exists for it in McpUserCredentials, tools/list and +// tools/call answer JSON-RPC -32006, as the gateway does before sign-in. Notifications and client +// responses, which have no method or no id, get 202 with no body. func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { defer s.LockUnlock()() @@ -56,6 +56,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { } external := mcpServiceIsExternal(service) + challenged := external && !s.mcpSignedIn(name) switch msg.Method { case "initialize": return rpcResult(msg.ID, map[string]any{ @@ -65,7 +66,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { "instructions": fakeMcpInstructions, }) case "tools/list": - if external { + if challenged { return signInRequired(msg.ID) } return rpcResult(msg.ID, map[string]any{"tools": []any{ @@ -79,7 +80,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { }, }}) case "tools/call": - if external { + if challenged { return signInRequired(msg.ID) } if msg.Params.Name != fakeMcpEchoTool { @@ -106,6 +107,16 @@ func (s *FakeWorkspace) mcpServiceNamed(name string) (catalog.McpService, bool) return catalog.McpService{}, false } +// mcpSignedIn reports whether the caller has a credential for the service. Like mcpServiceNamed, the match ignores case. +func (s *FakeWorkspace) mcpSignedIn(name string) bool { + for key := range s.McpUserCredentials { + if strings.EqualFold(key, name) { + return true + } + } + return false +} + // mcpServiceIsExternal reports whether the service takes an OAuth source connection. func mcpServiceIsExternal(ms catalog.McpService) bool { return ms.Config != nil && ms.Config.SourceConnection != nil && diff --git a/libs/testserver/mcp_endpoint_test.go b/libs/testserver/mcp_endpoint_test.go index 8c17f3ca621..fa87d059ea7 100644 --- a/libs/testserver/mcp_endpoint_test.go +++ b/libs/testserver/mcp_endpoint_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "testing" + "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -94,3 +95,32 @@ func TestMcpEndpointNoBodyForNotificationsAndResponses(t *testing.T) { }) } } + +func TestMcpEndpointPassesThroughOnceCredentialExists(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + const name = "system.ai.github" + mcpServicesTestSeed(workspace, name, "GitHub", mcpServicesTestGitHubProvider) + const list = `{"jsonrpc":"2.0","id":1,"method":"tools/list"}` + + assert.EqualValues(t, -32006, mcpEndpointTestCall(t, workspace, name, list)["error"].(map[string]any)["code"]) + + workspace.McpUserCredentialCreate(name) + + reply := mcpEndpointTestCall(t, workspace, name, list) + assert.Nil(t, reply["error"]) + assert.NotNil(t, reply["result"]) + + workspace.McpUserCredentials = map[string]catalog.McpServiceUserMappedCredential{} + assert.EqualValues(t, -32006, mcpEndpointTestCall(t, workspace, name, list)["error"].(map[string]any)["code"]) +} + +func TestMcpEndpointCredentialMatchesNameWithoutCase(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + const name = "system.ai.github" + mcpServicesTestSeed(workspace, name, "GitHub", mcpServicesTestGitHubProvider) + workspace.McpUserCredentialCreate(name) + const list = `{"jsonrpc":"2.0","id":1,"method":"tools/list"}` + + reply := mcpEndpointTestCall(t, workspace, "SYSTEM.AI.GITHUB", list) + assert.Nil(t, reply["error"], "the service and the credential match the name without case") +}