From 44e242d2af19acce2484da326d3ba9f9b6c4a931 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 13:00:43 +0000 Subject: [PATCH 01/55] Add sign-in challenges and the authenticate tool to aitools mcp serve For a service that needs provider sign-in, the gateway rejects tools/list until the user signs in, so an agent sees no tools. The proxy now answers a challenged tools/list with one authenticate tool. Calling it runs the sign-in and asks the agent to re-list. A challenged tools/call names the login command and asks the agent to re-list, without opening a browser. The sign-in page opens at most once per session, and again only after a timed-out attempt. Other services remain a pure passthrough. Co-authored-by: Isaac --- .../serve-authenticate-tool/.databrickscfg | 3 + .../mcp/serve-authenticate-tool/out.test.toml | 1 + .../mcp/serve-authenticate-tool/output.txt | 42 +++ .../requests-after.ndjson | 2 + .../requests-authenticate.ndjson | 1 + .../requests-list.ndjson | 3 + .../mcp/serve-authenticate-tool/script | 42 +++ .../cmd/aitools/mcp/serve-passthrough/script | 3 + cmd/aitools/mcp_serve.go | 71 +++++ cmd/aitools/mcp_serve_test.go | 38 +++ libs/aitools/mcp/known.go | 9 + libs/aitools/mcp/known_test.go | 7 + libs/aitools/mcp/proxy/bridge.go | 25 +- libs/aitools/mcp/proxy/bridge_test.go | 4 + libs/aitools/mcp/proxy/challenge.go | 254 +++++++++++++++ libs/aitools/mcp/proxy/challenge_test.go | 293 ++++++++++++++++++ libs/aitools/mcp/proxy/run.go | 7 + libs/testserver/fake_workspace.go | 5 +- libs/testserver/mcp_endpoint.go | 60 +++- libs/testserver/mcp_endpoint_test.go | 43 +++ 20 files changed, 899 insertions(+), 14 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-after.ndjson create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-authenticate.ndjson create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/requests-list.ndjson create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/script create mode 100644 libs/aitools/mcp/proxy/challenge.go create mode 100644 libs/aitools/mcp/proxy/challenge_test.go 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..219f0417722 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt @@ -0,0 +1,42 @@ + +>>> [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"}}}} + +=== 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]. +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... + +=== 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..5525ed9488d --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script @@ -0,0 +1,42 @@ +# 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. +trace $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"}}}}' > /dev/null + +# 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. +{ + 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 + $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null + retry --until '"id":3' cat serve.stdout.txt > /dev/null + cat requests-after.ndjson +} | $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 + +rm stderr-before-authenticate.txt serve.stdout.txt serve.stderr.txt diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/script b/acceptance/cmd/aitools/mcp/serve-passthrough/script index 9a16ce3999c..cc48e6cbb50 100644 --- a/acceptance/cmd/aitools/mcp/serve-passthrough/script +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/script @@ -3,6 +3,9 @@ sethome home envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg +# With no install record the proxy reads the service to learn whether it needs sign-in, so the fake needs it. +$CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment":"Ask questions about your data"}' > /dev/null + # DATABRICKS_HOST points elsewhere; the proxy must still talk to the profile's host. export DATABRICKS_HOST=https://elsewhere.test diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index b8fe267b1a5..86dde7ceebb 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -21,9 +21,13 @@ 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/httpclient" + "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" "golang.org/x/oauth2" ) @@ -110,6 +114,15 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { return serveFailure(cmd, err, serveBindStatus) } + w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) + if err != nil { + return serveFailure(cmd, err, 1) + } + challenge, err := serveChallenge(cmd.Context(), w, entry, profileName, f.service, cmd.ErrOrStderr()) + if err != nil { + return serveFailure(cmd, err, 1) + } + return serveProxy(cmd, proxy.Options{ In: cmd.InOrStdin(), Out: cmd.OutOrStdout(), @@ -120,9 +133,67 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { Profile: profileName, Agent: serveAgent(f.agent, cmd.ErrOrStderr()), Transport: serveTransport(cfg), + Challenge: challenge, }, mcp.PinnedFile(entry, cfg.ConfigFile)) } +// 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. +func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service string, errOut io.Writer) (*proxy.Challenge, error) { + signInRequired := entry != nil && entry.SignInRequired + if entry == nil { + svc, err := mcp.GetService(ctx, w, service) + if err != nil { + return nil, fmt.Errorf("cannot read MCP service %s: %w", service, err) + } + signInRequired = svc.SignInRequired + } + if !signInRequired { + return nil, nil + } + return &proxy.Challenge{ + Service: service, + Profile: profileName, + Host: w.Config.Host, + Label: mcp.ProviderLabel(service), + SignIn: serveSignIn(w, profileName, service, errOut), + }, nil +} + +// serveSignIn is the authenticate tool's sign-in. Progress goes to errOut, which is stderr because stdout carries the +// protocol. The page opens only when open is set, so the proxy decides how often the browser opens. +func serveSignIn(w *databricks.WorkspaceClient, profileName, service string, errOut io.Writer) proxy.SignInFunc { + return func(ctx context.Context, open bool) (bool, error) { + state, err := mcp.CredentialState(ctx, w, service) + if err != nil { + return false, err + } + if state == catalog.ProvisioningInfoStateActive { + return true, nil + } + workspaceID, err := auth.ResolveWorkspaceID(ctx, w) + if err != nil { + return false, err + } + url := mcp.SignInURL(w.Config.Host, service, workspaceID) + // SignIn always calls Open, so a call that must not open the page passes one that does nothing. + openPage := func(context.Context, string) error { return nil } + if open { + openPage = browser.OpenOutputToStderr + } + alreadySignedIn, _, err := mcp.SignIn(ctx, w, url, service, errOut, mcp.SignInOptions{ + Profile: profileName, + Poll: mcp.SignInPollInterval, + Timeout: mcp.SignInWaitTimeout, + Open: openPage, + }) + if _, timedOut := errors.AsType[*mcp.SignInTimeoutError](err); timedOut { + return false, &proxy.SignInTimeout{URL: url} + } + return alreadySignedIn, err + } +} + // serveProxy bridges opts to the service until stdin closes or a signal arrives. Authenticate goes through a // refreshGuard, so a rejected token stops the proxy; see proxyResult. pinnedFile is the config file the server is // pinned to, or "" (see mcp.PinnedFile). diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 5724abaa5ba..a23cdb79f23 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" @@ -755,3 +756,40 @@ 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(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", io.Discard) + require.NoError(t, err) + require.NotNil(t, challenge) + assert.Equal(t, "system.ai.github", challenge.Service) + assert.Equal(t, "logfood", 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(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", io.Discard) + require.NoError(t, err) + assert.Nil(t, challenge) + assert.Equal(t, int32(0), lookups.Load()) + + challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", 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") +} diff --git a/libs/aitools/mcp/known.go b/libs/aitools/mcp/known.go index 30ea4d9d524..b848b503a4f 100644 --- a/libs/aitools/mcp/known.go +++ b/libs/aitools/mcp/known.go @@ -58,3 +58,12 @@ var knownSystemAI = map[string]KnownService{ Note: "Scopes ending in .All usually need tenant admin consent.", }, } + +// 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, ok := knownSystemAI[LeafOf(fullName)]; ok && inSystemAI(fullName) { + return known.Label + } + return fullName +} diff --git a/libs/aitools/mcp/known_test.go b/libs/aitools/mcp/known_test.go index 2b8ad314212..ddde346fd91 100644 --- a/libs/aitools/mcp/known_test.go +++ b/libs/aitools/mcp/known_test.go @@ -37,3 +37,10 @@ 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")) +} diff --git a/libs/aitools/mcp/proxy/bridge.go b/libs/aitools/mcp/proxy/bridge.go index 5e22aa33bfa..e90098eb2ab 100644 --- a/libs/aitools/mcp/proxy/bridge.go +++ b/libs/aitools/mcp/proxy/bridge.go @@ -56,8 +56,12 @@ 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. + Local(ctx context.Context, request []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 +84,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 +265,19 @@ 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) + for _, msg := range msgs { + c.reply(msg) + } + return handled +} + // 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 +288,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) }) diff --git a/libs/aitools/mcp/proxy/bridge_test.go b/libs/aitools/mcp/proxy/bridge_test.go index 89fe538b7a8..cd4cba1e3b5 100644 --- a/libs/aitools/mcp/proxy/bridge_test.go +++ b/libs/aitools/mcp/proxy/bridge_test.go @@ -827,6 +827,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) ([][]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":[]}}` diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go new file mode 100644 index 00000000000..65f6ddc8597 --- /dev/null +++ b/libs/aitools/mcp/proxy/challenge.go @@ -0,0 +1,254 @@ +package proxy + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/url" + "strings" + "sync" +) + +const ( + authenticateTool = "authenticate" + signInPath = "/mcp-service-login" + + codeSignInRequired = -32006 + codeURLElicitation = -32042 +) + +var listChangedNotification = []byte(`{"jsonrpc":"2.0","method":"notifications/tools/list_changed"}`) + +// SignInTimeout means the sign-in page was opened and the credential did not become ACTIVE in time. +// URL is the page the user can finish at. +type SignInTimeout struct { + URL string +} + +func (e *SignInTimeout) Error() string { + return "sign-in did not complete before the timeout" +} + +// SignInFunc runs the sign-in for the authenticate tool and waits for it. open is false when the +// sign-in page must not be opened again in this session. It reports alreadySignedIn when the caller +// needed no sign-in, and returns a *SignInTimeout when the wait ended first. +type SignInFunc func(ctx context.Context, open bool) (alreadySignedIn 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 + // SignIn runs the sign-in. The command provides it, so the proxy never polls or opens a browser itself. + SignIn SignInFunc +} + +// signInHooks applies the sign-in rules to the bridge. Services without a challenge get no hooks. +type signInHooks struct { + c *Challenge + + // mu serializes authenticate calls and guards the two fields below. + mu sync.Mutex + opened bool // the sign-in page was opened in this session + timedOut bool // the last attempt timed out, so the page may open again +} + +// Local answers the authenticate tool call. Other requests, and messages that do not decode, are forwarded. +func (h *signInHooks) Local(ctx context.Context, request []byte) ([][]byte, bool) { + var req struct { + message + Params struct { + Name string `json:"name"` + } `json:"params"` + } + if json.Unmarshal(request, &req) != nil || !req.isRequest() || req.Method != "tools/call" || req.Params.Name != authenticateTool { + return nil, false + } + return h.authenticate(ctx, req.ID), 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. +func (h *signInHooks) Result(_ context.Context, request []byte, status int, message []byte) ([][]byte, error) { + req, err := parseMessage(request) + if err != nil { + return nil, err + } + switch req.Method { + case "tools/list": + if h.challenged(status, message) { + return [][]byte{h.authenticateList(req.ID)}, nil + } + case "tools/call": + if h.challenged(status, message) { + return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil + } + } + return [][]byte{message}, nil +} + +// upstreamReply is the part of an upstream message that challenge detection reads. +type upstreamReply struct { + 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: HTTP 401, JSON-RPC +// -32006, a -32042 whose elicitations are all sign-in URLs, or an isError result whose text has one. +func (h *signInHooks) challenged(status int, message []byte) bool { + if status == http.StatusUnauthorized { + return true + } + var reply upstreamReply + if err := json.Unmarshal(message, &reply); err != nil { + return false + } + 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 +} + +func (h *signInHooks) textHasSignInURL(content []textContent) bool { + for _, c := range content { + for word := range strings.FieldsSeq(c.Text) { + if h.isSignInURL(strings.TrimRight(word, ".,;:)\"'")) { + 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 == signInPath && u.Query().Get("name") == h.c.Service +} + +// authenticateList is the one-tool list a challenged service answers with. +func (h *signInHooks) authenticateList(id json.RawMessage) []byte { + tool := 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}, + } + return resultResponse(id, map[string]any{"tools": []any{tool}}) +} + +// 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: "+ + "databricks aitools mcp login %s --profile %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.Service, h.c.Profile) + return toolResult(id, text, true) +} + +// authenticate runs the sign-in and answers the tool call. Calls are serialized, so a second call +// waits for the first. The page opens once per session, and again only after an attempt timed out. +func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][]byte { + h.mu.Lock() + defer h.mu.Unlock() + + open := !h.opened || h.timedOut + if open { + h.opened = true + // The telemetry change logs SIGN_IN_PROMPTED here, when the sign-in page opens. + } + alreadySignedIn, err := h.c.SignIn(ctx, open) + timeout, timedOut := errors.AsType[*SignInTimeout](err) + h.timedOut = timedOut + + switch { + case err == nil && alreadySignedIn: + 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, + } + case timedOut: + return [][]byte{toolResult(id, fmt.Sprintf( + "Sign-in to %s did not complete in 5 minutes. Finish at %s then retry, or run: databricks aitools mcp login %s --profile %s", + h.c.Service, timeout.URL, h.c.Service, h.c.Profile), true)} + default: + return [][]byte{toolResult(id, fmt.Sprintf("Sign-in to %s on profile %s failed: %v", h.c.Service, h.c.Profile, err), true)} + } +} + +// 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(struct { + JSONRPC string `json:"jsonrpc"` + ID json.RawMessage `json:"id"` + Result any `json:"result"` + }{JSONRPC: "2.0", ID: id, Result: result}) + 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..3df8977d905 --- /dev/null +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -0,0 +1,293 @@ +package proxy + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "sync" + "sync/atomic" + "testing" + + "github.com/databricks/databricks-sdk-go/config" + "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. + listChallenge = `{"jsonrpc":"2.0","id":2,"error":{"code":-32006,"message":"rejected","data":null}}` + listTools = `{"jsonrpc":"2.0","id":2,"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 open flag and returns reply(call). +type signInStub struct { + mu sync.Mutex + opens []bool + reply func(call int) (bool, error) +} + +func (s *signInStub) SignIn(_ context.Context, open bool) (bool, error) { + s.mu.Lock() + defer s.mu.Unlock() + s.opens = append(s.opens, open) + return s.reply(len(s.opens)) +} + +func (s *signInStub) calls() []bool { + s.mu.Lock() + defer s.mu.Unlock() + return append([]bool(nil), s.opens...) +} + +// 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 = &signInHooks{c: &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", SignIn: signIn}} + 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) + } +} + +// 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": unauthorized, + "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, listChallenge)) + 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 := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + 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") + + require.Len(t, out, 2) + assert.Equal(t, []bool{true}, signIn.calls()) + assert.Empty(t, fake.requests(), "authenticate is answered locally, never sent upstream") + reply := parseReply(t, out[0]) + assert.JSONEq(t, `4`, string(reply.ID)) + assert.Equal(t, false, reply.Result["isError"]) + assert.Contains(t, out[0], "Signed in to system.ai.github on profile logfood. Tools are available now") + assert.Equal(t, listChangedMsg, out[1]) +} + +func TestAuthenticateOpensPageOnceAndSaysAlreadySignedIn(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + 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, fmt.Sprintf(authenticate, 5)) + p.waitLines(t, 2) + p.send(t, fmt.Sprintf(authenticate, 6)) + lines := p.waitLines(t, 4) + + assert.Equal(t, []bool{true, false}, signIn.calls()) + assert.Contains(t, lines[2], "Already signed in to system.ai.github") + require.NoError(t, p.finish(t)) +} + +func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + signIn := &signInStub{reply: func(call int) (bool, error) { + if call == 1 { + return false, &SignInTimeout{URL: testURL} + } + 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":{}}}` + + p.send(t, fmt.Sprintf(authenticate, 7)) + lines := p.waitLines(t, 1) + require.Len(t, lines, 1) + assert.Equal(t, true, parseReply(t, lines[0]).Result["isError"]) + text := resultText(t, lines[0]) + assert.Contains(t, text, "did not complete in 5 minutes. Finish at "+testURL) + assert.Contains(t, text, "databricks aitools mcp login system.ai.github --profile logfood") + + p.send(t, fmt.Sprintf(authenticate, 8)) + lines = p.waitLines(t, 3) + + assert.Equal(t, []bool{true, true}, signIn.calls(), "a timed-out attempt may open the page again") + assert.Contains(t, lines[1], "Signed in to system.ai.github") + require.NoError(t, p.finish(t)) +} + +func TestAuthenticateReportsFailureWithoutListChanged(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + signIn := &signInStub{reply: func(int) (bool, error) { return false, errors.New("sign-in failed for the test") }} + out, _ := runAll(t, challengedBridge(srv.URL, signIn.SignIn), + `{"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`+"\n") + + require.Len(t, out, 1) + assert.Equal(t, true, parseReply(t, out[0]).Result["isError"]) + assert.Contains(t, out[0], "Sign-in to system.ai.github on profile logfood failed: sign-in failed for the test") +} + +// 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, listTools)(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)) +} + +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/proxy/run.go b/libs/aitools/mcp/proxy/run.go index 5aa60d6322c..54c97fc6fd3 100644 --- a/libs/aitools/mcp/proxy/run.go +++ b/libs/aitools/mcp/proxy/run.go @@ -38,6 +38,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 @@ -64,5 +66,10 @@ func Run(ctx context.Context, opts Options) error { 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 = &signInHooks{c: opts.Challenge} + } return b.Run(ctx) } diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index 3dfa5456562..93349ebe945 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -249,7 +249,10 @@ type FakeWorkspace struct { McpServices map[string]catalog.McpService // McpUserCredentials holds the caller's sign-in to each MCP service, keyed by the service full name. The fake has one // caller. - McpUserCredentials map[string]catalog.McpServiceUserMappedCredential + McpUserCredentials map[string]catalog.McpServiceUserMappedCredential + // McpSignInChallenge selects how an external service answers a caller with no credential, keyed by + // the service full name. A missing entry gives McpChallengeRPC. + McpSignInChallenge map[string]McpSignInChallenge ModelProviderServices map[string]catalog.ModelProviderService // RegisteredModelVersions counts the versions registered on each model, keyed by its full name. diff --git a/libs/testserver/mcp_endpoint.go b/libs/testserver/mcp_endpoint.go index 52b600370b1..57403e1d107 100644 --- a/libs/testserver/mcp_endpoint.go +++ b/libs/testserver/mcp_endpoint.go @@ -3,6 +3,7 @@ package testserver import ( "encoding/json" "net/http" + "net/url" "strings" "github.com/databricks/databricks-sdk-go/service/catalog" @@ -16,6 +17,8 @@ const ( // rpcCodeSignInRequired is the gateway's answer to a service that needs a credential the // caller does not have. rpcCodeSignInRequired = -32006 + // rpcCodeURLElicitation is the gateway's URL elicitation answer, which carries the sign-in page. + rpcCodeURLElicitation = -32042 ) const ( @@ -35,11 +38,24 @@ type mcpRPC struct { } `json:"params"` } +// McpSignInChallenge is how the fake answers a caller who has no credential for an external service. +type McpSignInChallenge string + +const ( + // McpChallengeRPC is JSON-RPC -32006, the gateway's answer before sign-in. + McpChallengeRPC McpSignInChallenge = "rpc" + // McpChallengeURL is JSON-RPC -32042 with one URL elicitation to the sign-in page. + McpChallengeURL McpSignInChallenge = "url" + // McpChallengeHTTP is HTTP 401 with no JSON-RPC body. + McpChallengeHTTP McpSignInChallenge = "http" +) + // 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 the challenge selected by McpSignInChallenge (-32006 by default, 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 +72,8 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { } external := mcpServiceIsExternal(service) + _, signedIn := s.McpUserCredentials[name] + challenged := external && !signedIn switch msg.Method { case "initialize": return rpcResult(msg.ID, map[string]any{ @@ -65,8 +83,8 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { "instructions": fakeMcpInstructions, }) case "tools/list": - if external { - return signInRequired(msg.ID) + if challenged { + return s.signInRequired(msg.ID, name) } return rpcResult(msg.ID, map[string]any{"tools": []any{ map[string]any{ @@ -79,8 +97,8 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { }, }}) case "tools/call": - if external { - return signInRequired(msg.ID) + if challenged { + return s.signInRequired(msg.ID, name) } if msg.Params.Name != fakeMcpEchoTool { return Response{Body: rpcError(msg.ID, rpcCodeInvalidParams, "Unknown tool: "+msg.Params.Name)} @@ -126,8 +144,28 @@ func rpcError(id json.RawMessage, code int, message string) map[string]any { } } -// signInRequired is the answer to a request for an external service, which the gateway gives before sign-in. -func signInRequired(id json.RawMessage) Response { - return Response{Body: rpcError(id, rpcCodeSignInRequired, - "The MCP service request was rejected (HTTP 401). Sign-in is required.")} +// signInRequired answers with the challenge the service is configured for. +func (s *FakeWorkspace) signInRequired(id json.RawMessage, name string) Response { + switch s.McpSignInChallenge[name] { + case McpChallengeURL: + query := url.Values{"name": {name}} + return Response{Body: map[string]any{ + "jsonrpc": "2.0", + "id": id, + "error": map[string]any{ + "code": rpcCodeURLElicitation, + "message": "This request requires more information.", + "data": map[string]any{"elicitations": []any{map[string]any{ + "mode": "url", + "message": "Sign in to continue.", + "url": s.url + "/mcp-service-login?" + query.Encode(), + }}}, + }, + }} + case McpChallengeHTTP: + return Response{StatusCode: http.StatusUnauthorized, Body: "Unauthorized"} + default: + return Response{Body: rpcError(id, rpcCodeSignInRequired, + "The MCP service request was rejected (HTTP 401). Sign-in is required.")} + } } diff --git a/libs/testserver/mcp_endpoint_test.go b/libs/testserver/mcp_endpoint_test.go index 8c17f3ca621..5e75f3c943d 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,45 @@ 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 TestMcpEndpointURLElicitationChallenge(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + const name = "system.ai.github" + mcpServicesTestSeed(workspace, name, "GitHub", mcpServicesTestGitHubProvider) + workspace.McpSignInChallenge = map[string]McpSignInChallenge{name: McpChallengeURL} + + reply := mcpEndpointTestCall(t, workspace, name, `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"echo"}}`) + errBody := reply["error"].(map[string]any) + assert.EqualValues(t, -32042, errBody["code"]) + elicitation := errBody["data"].(map[string]any)["elicitations"].([]any)[0].(map[string]any) + assert.Equal(t, "url", elicitation["mode"]) + assert.Equal(t, "http://test/mcp-service-login?name=system.ai.github", elicitation["url"]) +} + +func TestMcpEndpointHTTPUnauthorizedChallenge(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + const name = "system.ai.github" + mcpServicesTestSeed(workspace, name, "GitHub", mcpServicesTestGitHubProvider) + workspace.McpSignInChallenge = map[string]McpSignInChallenge{name: McpChallengeHTTP} + + response := workspace.McpEndpoint(Request{Body: []byte(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`)}, name) + assert.Equal(t, 401, response.StatusCode) +} From cd431a407159f1941d1d921cfb8c52e248108111 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:14:30 +0000 Subject: [PATCH 02/55] Update the no-record notice in the authenticate golden The serve notice for a server with no install record no longer ends with a period, so the golden changes only in those two lines. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt index 219f0417722..432e0efa100 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt @@ -7,7 +7,7 @@ ["authenticate"] >>> cat stderr-before-authenticate.txt -No install record for system.ai.github on profile "logfood"; using its current host [DATABRICKS_URL]. +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 @@ -18,7 +18,7 @@ No install record for system.ai.github on profile "logfood"; using its current h {"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]. +No install record for system.ai.github on profile "logfood"; using its current host [DATABRICKS_URL] 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... From 53b1098db568b32cf99e486531e9a72474452d73 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:32:25 +0000 Subject: [PATCH 03/55] Answer the challenge test requests with their own ids The proxy now accepts an upstream answer only when its id is the id of the request. Two challenge tests answered a request with a fixture that carried another request's id, so the answer was reported as a failed upstream response. Give each request in those tests its own fixture. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 3df8977d905..c0eae732f25 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -19,9 +19,12 @@ 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. + // 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"}` ) @@ -137,7 +140,7 @@ func TestChallengeIgnoresOtherErrors(t *testing.T) { } func TestChallengeToolsCallAnswersWithoutBrowser(t *testing.T) { - srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listChallenge)) + 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") @@ -251,7 +254,7 @@ 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, listTools)(w, r) + answerWith(http.StatusOK, listToolsLater)(w, r) return } answerWith(http.StatusOK, listChallenge)(w, r) From 64f387dd725432bc27db992aebb0991a624b6829 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:32:26 +0000 Subject: [PATCH 04/55] Update the authenticate golden for the sign-in notice The sign-in now names the profile on stderr before the page, as `login` does, because the serve command passes the profile to the shared sign-in. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt index 432e0efa100..02f9b27cf96 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt @@ -19,6 +19,7 @@ No install record for system.ai.github on profile "logfood"; using its current h >>> 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... From 0c9ef32a5f23e6766c0e7cdfabe2154b621b9edd Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 05:59:53 +0000 Subject: [PATCH 05/55] Reuse the login sign-in helper in the authenticate tool The authenticate tool runs the same sign-in that `login` runs. The shared helper now also reports whether the caller was already signed in and whether the sign-in page was shown, so the proxy no longer reads the credential before the sign-in reads it again. The sign-in timeout carries its wait and the failure of its last poll, and the workspace ID is resolved once per session. The shared sign-in helper also prints its progress on stderr. Refactor only, apart from the stderr progress the helper adds to the tool's output. Co-authored-by: Isaac --- .../mcp/serve-authenticate-tool/output.txt | 2 + .../mcp/serve-authenticate-tool/script | 1 - .../mcp/serve-authenticate-tool/test.toml | 3 ++ cmd/aitools/mcp_add.go | 3 +- cmd/aitools/mcp_login.go | 21 ++++++---- cmd/aitools/mcp_login_test.go | 4 +- cmd/aitools/mcp_serve.go | 41 ++++++++----------- libs/aitools/mcp/known.go | 11 ++++- libs/aitools/mcp/proxy/bridge.go | 4 +- libs/aitools/mcp/proxy/bridge_test.go | 36 ++++++++++++++++ libs/aitools/mcp/proxy/challenge.go | 35 +++++++++------- libs/aitools/mcp/proxy/challenge_test.go | 30 ++++++-------- libs/aitools/mcp/proxy/messages.go | 1 + libs/aitools/mcp/proxy/run_test.go | 15 +++++++ 14 files changed, 137 insertions(+), 70 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/serve-authenticate-tool/test.toml diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt index 02f9b27cf96..0bb53ddfb93 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt @@ -23,6 +23,8 @@ 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 diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script index 5525ed9488d..aecdc117b38 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script @@ -39,4 +39,3 @@ 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 -rm stderr-before-authenticate.txt serve.stdout.txt serve.stderr.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..f209d95e65a 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.profile, req.pinned, url, plans[i].service, 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..fdfdb4b6d7b 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -180,7 +180,7 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie 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, out.Profile, pinnedFile, url, svc, 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 { @@ -234,26 +234,29 @@ var ( ) // signIn runs the provider sign-in of svc for the profile and reports progress on errOut. It returns the sign-in state -// name, whether 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{ +// name, whether the caller was already signed in, whether url was printed, and the failure, which is categorized for +// JSON. open opens the page; it is called only when the browser is enabled. `add --login` shares this. +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service, + open func(context.Context, string) error, +) (state string, alreadySignedIn, urlShown bool, err error) { + alreadySignedIn, urlShown, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ Profile: profileName, Poll: signInPollInterval, Timeout: signInWaitTimeout, - Open: browser.OpenOutputToStderr, + Open: open, }) 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 +267,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..f6bfd7ddde4 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, "logfood", "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", SignInRequired: true}, + 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 86dde7ceebb..e8a4a6b3058 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -27,7 +27,6 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/httpclient" - "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" "golang.org/x/oauth2" ) @@ -160,37 +159,31 @@ func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *m }, nil } -// serveSignIn is the authenticate tool's sign-in. Progress goes to errOut, which is stderr because stdout carries the -// protocol. The page opens only when open is set, so the proxy decides how often the browser opens. +// serveSignIn is the authenticate tool's sign-in, the one `login` runs. Progress goes to errOut, which is stderr because +// stdout carries the protocol. The page opens only when open is set, so the proxy decides how often the browser opens. func serveSignIn(w *databricks.WorkspaceClient, profileName, service string, errOut io.Writer) proxy.SignInFunc { - return func(ctx context.Context, open bool) (bool, error) { - state, err := mcp.CredentialState(ctx, w, service) - if err != nil { - return false, err - } - if state == catalog.ProvisioningInfoStateActive { - return true, nil - } - workspaceID, err := auth.ResolveWorkspaceID(ctx, w) - if err != nil { - return false, err + // 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, open bool) (bool, bool, error) { + if workspaceID == "" { + id, err := auth.ResolveWorkspaceID(ctx, w) + if err != nil { + return false, false, err + } + workspaceID = id } url := mcp.SignInURL(w.Config.Host, service, workspaceID) - // SignIn always calls Open, so a call that must not open the page passes one that does nothing. + // The opener is called even when the page must not open, so a call that must not open it passes one that does nothing. openPage := func(context.Context, string) error { return nil } if open { openPage = browser.OpenOutputToStderr } - alreadySignedIn, _, err := mcp.SignIn(ctx, w, url, service, errOut, mcp.SignInOptions{ - Profile: profileName, - Poll: mcp.SignInPollInterval, - Timeout: mcp.SignInWaitTimeout, - Open: openPage, - }) - if _, timedOut := errors.AsType[*mcp.SignInTimeoutError](err); timedOut { - return false, &proxy.SignInTimeout{URL: url} + svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} + _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, profileName, "", url, svc, openPage) + if timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err); timedOut { + return false, urlShown, &proxy.SignInTimeout{URL: url, Wait: signInWaitTimeout, LastCheck: timeout.LastCheck} } - return alreadySignedIn, err + return alreadySignedIn, urlShown, err } } diff --git a/libs/aitools/mcp/known.go b/libs/aitools/mcp/known.go index b848b503a4f..2fdce73a87b 100644 --- a/libs/aitools/mcp/known.go +++ b/libs/aitools/mcp/known.go @@ -59,10 +59,19 @@ var knownSystemAI = map[string]KnownService{ }, } +// KnownOf returns the CLI's description of a service, or nil when it is not a system.ai service in the table. +func KnownOf(fullName string) *KnownService { + known, ok := knownSystemAI[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, ok := knownSystemAI[LeafOf(fullName)]; ok && inSystemAI(fullName) { + if known := KnownOf(fullName); known != nil { return known.Label } return fullName diff --git a/libs/aitools/mcp/proxy/bridge.go b/libs/aitools/mcp/proxy/bridge.go index e90098eb2ab..1c82c089453 100644 --- a/libs/aitools/mcp/proxy/bridge.go +++ b/libs/aitools/mcp/proxy/bridge.go @@ -272,8 +272,8 @@ func (c *conn) answerLocally(ctx context.Context, line []byte) bool { return false } msgs, handled := c.b.Hooks.Local(ctx, line) - for _, msg := range msgs { - c.reply(msg) + if err := c.writeLines(msgs); err != nil { + c.logf("stdout: %v", err) } return handled } diff --git a/libs/aitools/mcp/proxy/bridge_test.go b/libs/aitools/mcp/proxy/bridge_test.go index cd4cba1e3b5..c5c4ebf81ea 100644 --- a/libs/aitools/mcp/proxy/bridge_test.go +++ b/libs/aitools/mcp/proxy/bridge_test.go @@ -2198,3 +2198,39 @@ func TestRunClosesStaleResponseBeforeReplay(t *testing.T) { require.Len(t, out, 2) assert.Nil(t, parseReply(t, out[1]).Error) } + +// localHooks answers a request whose body names "local" with msgs, and forwards the others. +type localHooks struct{ msgs [][]byte } + +func (localHooks) Result(_ context.Context, _ []byte, _ int, msg []byte) ([][]byte, error) { + return [][]byte{msg}, nil +} + +func (h localHooks) Local(_ context.Context, request []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: localHooks{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()) +} diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 65f6ddc8597..b56df0b1f8c 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -9,6 +9,7 @@ import ( "net/url" "strings" "sync" + "time" ) const ( @@ -21,20 +22,27 @@ const ( var listChangedNotification = []byte(`{"jsonrpc":"2.0","method":"notifications/tools/list_changed"}`) -// SignInTimeout means the sign-in page was opened and the credential did not become ACTIVE in time. -// URL is the page the user can finish at. +// SignInTimeout means the sign-in page was shown and the credential did not become ACTIVE within Wait. URL is the page +// the user can finish at. LastCheck is the failure of the last poll, when that poll failed. type SignInTimeout struct { - URL string + URL string + Wait time.Duration + LastCheck error } func (e *SignInTimeout) Error() string { return "sign-in did not complete before the timeout" } +func (e *SignInTimeout) Unwrap() error { + return e.LastCheck +} + // SignInFunc runs the sign-in for the authenticate tool and waits for it. open is false when the // sign-in page must not be opened again in this session. It reports alreadySignedIn when the caller -// needed no sign-in, and returns a *SignInTimeout when the wait ended first. -type SignInFunc func(ctx context.Context, open bool) (alreadySignedIn bool, err error) +// 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. It returns a *SignInTimeout when the wait ended first. +type SignInFunc func(ctx context.Context, open bool) (alreadySignedIn, urlShown bool, err error) // Challenge turns on sign-in handling for a service that needs a provider sign-in. type Challenge struct { @@ -68,7 +76,7 @@ func (h *signInHooks) Local(ctx context.Context, request []byte) ([][]byte, bool Name string `json:"name"` } `json:"params"` } - if json.Unmarshal(request, &req) != nil || !req.isRequest() || req.Method != "tools/call" || req.Params.Name != authenticateTool { + if json.Unmarshal(request, &req) != nil || !req.isRequest() || req.Method != methodToolsCall || req.Params.Name != authenticateTool { return nil, false } return h.authenticate(ctx, req.ID), true @@ -86,7 +94,7 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess if h.challenged(status, message) { return [][]byte{h.authenticateList(req.ID)}, nil } - case "tools/call": + case methodToolsCall: if h.challenged(status, message) { return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil } @@ -152,6 +160,8 @@ func (h *signInHooks) urlElicitation(data json.RawMessage) bool { return true } +// textHasSignInURL reports whether one of the texts has a word that is a sign-in URL. Punctuation around the word, such as +// the brackets of a sentence or the period that ends it, is not part of the URL. func (h *signInHooks) textHasSignInURL(content []textContent) bool { for _, c := range content { for word := range strings.FieldsSeq(c.Text) { @@ -206,11 +216,8 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] defer h.mu.Unlock() open := !h.opened || h.timedOut - if open { - h.opened = true - // The telemetry change logs SIGN_IN_PROMPTED here, when the sign-in page opens. - } - alreadySignedIn, err := h.c.SignIn(ctx, open) + h.opened = true + alreadySignedIn, _, err := h.c.SignIn(ctx, open) timeout, timedOut := errors.AsType[*SignInTimeout](err) h.timedOut = timedOut @@ -227,8 +234,8 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] } case timedOut: return [][]byte{toolResult(id, fmt.Sprintf( - "Sign-in to %s did not complete in 5 minutes. Finish at %s then retry, or run: databricks aitools mcp login %s --profile %s", - h.c.Service, timeout.URL, h.c.Service, h.c.Profile), true)} + "Sign-in to %s did not complete in %g minutes. Finish at %s then retry, or run: databricks aitools mcp login %s --profile %s", + h.c.Service, timeout.Wait.Minutes(), timeout.URL, h.c.Service, h.c.Profile), true)} default: return [][]byte{toolResult(id, fmt.Sprintf("Sign-in to %s on profile %s failed: %v", h.c.Service, h.c.Profile, err), true)} } diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index c0eae732f25..91575fb62a2 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -9,8 +9,8 @@ import ( "sync" "sync/atomic" "testing" + "time" - "github.com/databricks/databricks-sdk-go/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -28,18 +28,20 @@ const ( listChangedMsg = `{"jsonrpc":"2.0","method":"notifications/tools/list_changed"}` ) -// signInStub replaces the command's sign-in. Each call records its open flag and returns reply(call). +// signInStub replaces the command's sign-in. Each call records its open flag and returns reply(call), with the page +// reported as shown. type signInStub struct { mu sync.Mutex opens []bool reply func(call int) (bool, error) } -func (s *signInStub) SignIn(_ context.Context, open bool) (bool, error) { +func (s *signInStub) SignIn(_ context.Context, open bool) (bool, bool, error) { s.mu.Lock() defer s.mu.Unlock() s.opens = append(s.opens, open) - return s.reply(len(s.opens)) + alreadySignedIn, err := s.reply(len(s.opens)) + return alreadySignedIn, true, err } func (s *signInStub) calls() []bool { @@ -214,7 +216,7 @@ func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) signIn := &signInStub{reply: func(call int) (bool, error) { if call == 1 { - return false, &SignInTimeout{URL: testURL} + return false, &SignInTimeout{URL: testURL, Wait: 5 * time.Minute} } return false, nil }} @@ -280,17 +282,11 @@ func TestChallengeTurnsOnAfterSignIn(t *testing.T) { require.NoError(t, p.finish(t)) } -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} +// TestSignInTimeoutKeepsLastCheck checks that the timeout exposes the failure of its last poll through the error chain. +func TestSignInTimeoutKeepsLastCheck(t *testing.T) { + lastCheck := errors.New("the last poll failed") + err := error(&SignInTimeout{URL: testURL, Wait: time.Minute, LastCheck: lastCheck}) - 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`) + assert.ErrorIs(t, err, lastCheck) + assert.Nil(t, errors.Unwrap(&SignInTimeout{URL: testURL})) } diff --git a/libs/aitools/mcp/proxy/messages.go b/libs/aitools/mcp/proxy/messages.go index 2fc637d28dc..18ea1b05fec 100644 --- a/libs/aitools/mcp/proxy/messages.go +++ b/libs/aitools/mcp/proxy/messages.go @@ -16,6 +16,7 @@ const ( methodInitialized = "notifications/initialized" methodCancelled = "notifications/cancelled" methodPing = "ping" + methodToolsCall = "tools/call" keyResult = "result" keyInstructions = "instructions" 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`) +} From 08dad27c2532d4c065a43fb72aea7050fd39abad Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:00:16 +0000 Subject: [PATCH 06/55] Set MSYS_NO_PATHCONV on the new acceptance API calls Git Bash on Windows converts a leading slash in the path argument of $CLI, so the calls made by the new serve acceptance scripts need the variable like the other scripts do. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/serve-authenticate-tool/script | 4 ++-- acceptance/cmd/aitools/mcp/serve-passthrough/script | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script index aecdc117b38..1a7697a430b 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script @@ -8,7 +8,7 @@ export BROWSER=none export RETRY_MAX_ATTEMPTS=60 # The fake gets the GitHub service with its OAuth connection; the caller has no credential for it yet. -trace $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"}}}}' > /dev/null +MSYS_NO_PATHCONV=1 trace $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"}}}}' > /dev/null # 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. @@ -18,7 +18,7 @@ trace $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.a cat serve.stderr.txt > stderr-before-authenticate.txt cat requests-authenticate.ndjson retry --until "Waiting for sign-in" cat serve.stderr.txt > /dev/null - $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null + MSYS_NO_PATHCONV=1 $CLI api post /api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials --profile logfood --json '{}' > /dev/null retry --until '"id":3' cat serve.stdout.txt > /dev/null cat requests-after.ndjson } | $CLI aitools mcp serve --profile logfood --service system.ai.github --agent claude-code > serve.stdout.txt 2> serve.stderr.txt diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/script b/acceptance/cmd/aitools/mcp/serve-passthrough/script index cc48e6cbb50..28beb7858de 100644 --- a/acceptance/cmd/aitools/mcp/serve-passthrough/script +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/script @@ -4,7 +4,7 @@ envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg # With no install record the proxy reads the service to learn whether it needs sign-in, so the fake needs it. -$CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment":"Ask questions about your data"}' > /dev/null +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment":"Ask questions about your data"}' > /dev/null # DATABRICKS_HOST points elsewhere; the proxy must still talk to the profile's host. export DATABRICKS_HOST=https://elsewhere.test From 6966f8c71e6fd5317ef4936f4da8bcb22eac523d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:01:58 +0000 Subject: [PATCH 07/55] Print the login command of the profile's config file in the sign-in hints The sign-in hints of the authenticate tool wrote the login command by hand. The command now builds it with the same helper as add and login, so a server pinned to a config file names that file, and a profile name with a space is quoted. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 24 +++++++++++++--------- cmd/aitools/mcp_serve_test.go | 19 ++++++++++++++--- libs/aitools/mcp/proxy/challenge.go | 13 ++++++------ libs/aitools/mcp/proxy/challenge_test.go | 26 +++++++++++++++++++++++- 4 files changed, 62 insertions(+), 20 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index e8a4a6b3058..646180607c7 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -117,7 +117,9 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { if err != nil { return serveFailure(cmd, err, 1) } - challenge, err := serveChallenge(cmd.Context(), w, entry, profileName, f.service, cmd.ErrOrStderr()) + // A server pinned to a config file names that file in its hints, as add and login do. + pinnedFile := mcp.PinnedFile(entry, cfg.ConfigFile) + challenge, err := serveChallenge(cmd.Context(), w, entry, profileName, f.service, pinnedFile, cmd.ErrOrStderr()) if err != nil { return serveFailure(cmd, err, 1) } @@ -133,12 +135,13 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { Agent: serveAgent(f.agent, cmd.ErrOrStderr()), Transport: serveTransport(cfg), Challenge: challenge, - }, mcp.PinnedFile(entry, cfg.ConfigFile)) + }, pinnedFile) } // 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. -func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service string, errOut io.Writer) (*proxy.Challenge, error) { +// pinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). +func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service, pinnedFile string, errOut io.Writer) (*proxy.Challenge, error) { signInRequired := entry != nil && entry.SignInRequired if entry == nil { svc, err := mcp.GetService(ctx, w, service) @@ -151,17 +154,18 @@ func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *m return nil, nil } return &proxy.Challenge{ - Service: service, - Profile: profileName, - Host: w.Config.Host, - Label: mcp.ProviderLabel(service), - SignIn: serveSignIn(w, profileName, service, errOut), + Service: service, + Profile: profileName, + Host: w.Config.Host, + Label: mcp.ProviderLabel(service), + LoginCommand: mcpLoginHint(service, profileName, pinnedFile), + SignIn: serveSignIn(w, profileName, service, pinnedFile, errOut), }, nil } // serveSignIn is the authenticate tool's sign-in, the one `login` runs. Progress goes to errOut, which is stderr because // stdout carries the protocol. The page opens only when open is set, so the proxy decides how often the browser opens. -func serveSignIn(w *databricks.WorkspaceClient, profileName, service string, errOut io.Writer) proxy.SignInFunc { +func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { // 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, open bool) (bool, bool, error) { @@ -179,7 +183,7 @@ func serveSignIn(w *databricks.WorkspaceClient, profileName, service string, err openPage = browser.OpenOutputToStderr } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} - _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, profileName, "", url, svc, openPage) + _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, profileName, pinnedFile, url, svc, openPage) if timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err); timedOut { return false, urlShown, &proxy.SignInTimeout{URL: url, Wait: signInWaitTimeout, LastCheck: timeout.LastCheck} } diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index a23cdb79f23..80baf1dede9 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -774,7 +774,7 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", io.Discard) + challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "", io.Discard) require.NoError(t, err) require.NotNil(t, challenge) assert.Equal(t, "system.ai.github", challenge.Service) @@ -783,13 +783,26 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { 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(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", io.Discard) + challenge, err = serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "", io.Discard) require.NoError(t, err) assert.Nil(t, challenge) assert.Equal(t, int32(0), lookups.Load()) - challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", io.Discard) + challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", 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(), w, &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", 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) +} diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index b56df0b1f8c..08f54ef70ca 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -54,6 +54,8 @@ type Challenge struct { 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 // SignIn runs the sign-in. The command provides it, so the proxy never polls or opens a browser itself. SignIn SignInFunc } @@ -202,10 +204,9 @@ func (h *signInHooks) authenticateList(id json.RawMessage) []byte { // 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: "+ - "databricks aitools mcp login %s --profile %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.Service, h.c.Profile) + 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) } @@ -234,8 +235,8 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] } case timedOut: return [][]byte{toolResult(id, fmt.Sprintf( - "Sign-in to %s did not complete in %g minutes. Finish at %s then retry, or run: databricks aitools mcp login %s --profile %s", - h.c.Service, timeout.Wait.Minutes(), timeout.URL, h.c.Service, h.c.Profile), true)} + "Sign-in to %s did not complete in %g minutes. Finish at %s then retry, or run: %s", + h.c.Service, timeout.Wait.Minutes(), timeout.URL, h.c.LoginCommand), true)} default: return [][]byte{toolResult(id, fmt.Sprintf("Sign-in to %s on profile %s failed: %v", h.c.Service, h.c.Profile, err), true)} } diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 91575fb62a2..b639c7b6a5e 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -50,10 +50,13 @@ func (s *signInStub) calls() []bool { return append([]bool(nil), s.opens...) } +// 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 = &signInHooks{c: &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", SignIn: signIn}} + b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: "databricks aitools mcp login system.ai.github --profile logfood", SignIn: signIn}} return b } @@ -290,3 +293,24 @@ func TestSignInTimeoutKeepsLastCheck(t *testing.T) { assert.ErrorIs(t, err, lastCheck) assert.Nil(t, errors.Unwrap(&SignInTimeout{URL: testURL})) } + +// 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) { + newBridge := func(url string, signIn SignInFunc) *Bridge { + b := bridgeFor(url) + b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}} + return b + } + srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, callChallenge)) + out, _ := runAll(t, newBridge(srv.URL, nil), `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`+"\n") + require.Len(t, out, 2) + assert.Contains(t, resultText(t, out[0]), "Call the authenticate tool, or run: "+loginCommand+". If") + + timeout := func(context.Context, bool) (bool, bool, error) { + return false, true, &SignInTimeout{URL: testURL, Wait: 5 * time.Minute} + } + out, _ = runAll(t, newBridge(srv.URL, timeout), `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`+"\n") + require.Len(t, out, 1) + assert.Contains(t, resultText(t, out[0]), "then retry, or run: "+loginCommand) +} From ea1ca880b292c99f7b60001f60ee82222f67db5c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:02:39 +0000 Subject: [PATCH 08/55] Answer nothing for an authenticate call the client cancelled A cancelled request has no response in the MCP spec, and the client has stopped waiting for one. The sign-in of a cancelled authenticate call returned a context error that was written as a failed sign-in. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 4 +++ libs/aitools/mcp/proxy/challenge_test.go | 33 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 08f54ef70ca..bcbbf47d754 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -219,6 +219,10 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] open := !h.opened || h.timedOut h.opened = true alreadySignedIn, _, err := h.c.SignIn(ctx, open) + if ctx.Err() != nil { + // The client cancelled the request or shutdown began. Nothing is answered for a cancelled request. + return nil + } timeout, timedOut := errors.AsType[*SignInTimeout](err) h.timedOut = timedOut diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index b639c7b6a5e..63d03b84fde 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -314,3 +314,36 @@ func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { require.Len(t, out, 1) assert.Contains(t, resultText(t, out[0]), "then retry, or run: "+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, _ bool) (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])) +} From d8758b20e467bd221e41bd020290631da39215c6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:03:56 +0000 Subject: [PATCH 09/55] Open the sign-in page again after an attempt that did not hold it The authenticate tool opened the sign-in page once per session and again only after a timeout, so a call that completed, failed, or was refused before the page showed left later calls waiting on a page that was never opened. The page is now held back only while a cancelled attempt that showed it may still be open in the user's browser. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 19 +++--- libs/aitools/mcp/proxy/challenge_test.go | 80 +++++++++++++++++++++++- 2 files changed, 87 insertions(+), 12 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index bcbbf47d754..948f2ababac 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -64,10 +64,11 @@ type Challenge struct { type signInHooks struct { c *Challenge - // mu serializes authenticate calls and guards the two fields below. - mu sync.Mutex - opened bool // the sign-in page was opened in this session - timedOut bool // the last attempt timed out, so the page may open again + // mu serializes authenticate calls and guards the field below. + mu sync.Mutex + // 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 } // Local answers the authenticate tool call. Other requests, and messages that do not decode, are forwarded. @@ -210,21 +211,19 @@ func (h *signInHooks) signInRequired(id json.RawMessage) []byte { return toolResult(id, text, true) } -// authenticate runs the sign-in and answers the tool call. Calls are serialized, so a second call -// waits for the first. The page opens once per session, and again only after an attempt timed out. +// authenticate runs the sign-in and answers the tool call. Calls are serialized, so a second call waits for the first. +// The page opens unless the last attempt was cancelled while its page was showing (see pageCancelled). func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][]byte { h.mu.Lock() defer h.mu.Unlock() - open := !h.opened || h.timedOut - h.opened = true - alreadySignedIn, _, err := h.c.SignIn(ctx, open) + alreadySignedIn, urlShown, err := h.c.SignIn(ctx, !h.pageCancelled) + h.pageCancelled = urlShown && err != nil && ctx.Err() != nil if ctx.Err() != nil { // The client cancelled the request or shutdown began. Nothing is answered for a cancelled request. return nil } timeout, timedOut := errors.AsType[*SignInTimeout](err) - h.timedOut = timedOut switch { case err == nil && alreadySignedIn: diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 63d03b84fde..8281d7f19ea 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -199,7 +199,7 @@ func TestAuthenticateSignsInAndSendsListChanged(t *testing.T) { assert.Equal(t, listChangedMsg, out[1]) } -func TestAuthenticateOpensPageOnceAndSaysAlreadySignedIn(t *testing.T) { +func TestAuthenticateSaysAlreadySignedInAfterSignIn(t *testing.T) { srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) signIn := &signInStub{reply: func(call int) (bool, error) { return call > 1, nil }} p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn.SignIn)) @@ -210,7 +210,7 @@ func TestAuthenticateOpensPageOnceAndSaysAlreadySignedIn(t *testing.T) { p.send(t, fmt.Sprintf(authenticate, 6)) lines := p.waitLines(t, 4) - assert.Equal(t, []bool{true, false}, signIn.calls()) + assert.Equal(t, []bool{true, true}, signIn.calls(), "a sign-in that completed does not hold the page back") assert.Contains(t, lines[2], "Already signed in to system.ai.github") require.NoError(t, p.finish(t)) } @@ -347,3 +347,79 @@ func TestAuthenticateWritesNothingWhenCancelled(t *testing.T) { 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: &SignInTimeout{URL: testURL, Wait: time.Minute}}, 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, open bool) (bool, bool, error) { + mu.Lock() + opens = append(opens, 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]) + }) + } +} From 1f386424d737bf87983b28711cca6b0618d5c9be Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:06:21 +0000 Subject: [PATCH 10/55] Intercept authenticate only after the service challenged the session Before a challenge the proxy has no tool to offer, so a tool named authenticate that the upstream lists is the upstream's and is forwarded. The proxy answers the call once the session has been challenged. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 31 +++++- libs/aitools/mcp/proxy/challenge_test.go | 122 ++++++++++++++++------- 2 files changed, 111 insertions(+), 42 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 948f2ababac..6411462a0ce 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -64,14 +64,20 @@ type Challenge struct { type signInHooks struct { c *Challenge - // mu serializes authenticate calls and guards the field below. + // mu serializes authenticate calls, which may run a sign-in for minutes. pageCancelled is read and written under it. mu sync.Mutex // 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 challenged. It is held only briefly, so an answer to a request is not held up by a sign-in. + stateMu sync.Mutex + // sessionChallenged means a request of this session was challenged. The authenticate tool is the proxy's only after that. + sessionChallenged bool } -// Local answers the authenticate tool call. Other requests, and messages that do not decode, are forwarded. +// 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) ([][]byte, bool) { var req struct { message @@ -82,6 +88,12 @@ func (h *signInHooks) Local(ctx context.Context, request []byte) ([][]byte, bool if json.Unmarshal(request, &req) != nil || !req.isRequest() || req.Method != methodToolsCall || req.Params.Name != authenticateTool { return nil, false } + h.stateMu.Lock() + challenged := h.sessionChallenged + h.stateMu.Unlock() + if !challenged { + return nil, false + } return h.authenticate(ctx, req.ID), true } @@ -94,17 +106,28 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess } switch req.Method { case "tools/list": - if h.challenged(status, message) { + if h.isChallenge(status, message) { return [][]byte{h.authenticateList(req.ID)}, nil } case methodToolsCall: - if h.challenged(status, message) { + if h.isChallenge(status, message) { return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil } } return [][]byte{message}, nil } +// isChallenge reports whether an upstream answer is a challenge, and records that the session has been challenged. +func (h *signInHooks) isChallenge(status int, message []byte) bool { + if !h.challenged(status, message) { + return false + } + h.stateMu.Lock() + h.sessionChallenged = true + h.stateMu.Unlock() + return true +} + // upstreamReply is the part of an upstream message that challenge detection reads. type upstreamReply struct { Error *struct { diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 8281d7f19ea..03cdebed7ca 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "net/http/httptest" "sync" "sync/atomic" "testing" @@ -69,6 +70,27 @@ func answerWith(status int, body string) http.HandlerFunc { } } +// 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) @@ -184,39 +206,42 @@ func TestChallengeNeverTriggeredByInitialize(t *testing.T) { } func TestAuthenticateSignsInAndSendsListChanged(t *testing.T) { - srv, fake := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + srv, fake := challengedService(t) 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") + 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)) - require.Len(t, out, 2) assert.Equal(t, []bool{true}, signIn.calls()) - assert.Empty(t, fake.requests(), "authenticate is answered locally, never sent upstream") - reply := parseReply(t, out[0]) + 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, out[0], "Signed in to system.ai.github on profile logfood. Tools are available now") - assert.Equal(t, listChangedMsg, out[1]) + 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, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + 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, 2) + p.waitLines(t, 3) p.send(t, fmt.Sprintf(authenticate, 6)) - lines := p.waitLines(t, 4) + 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[2], "Already signed in to system.ai.github") + assert.Contains(t, lines[3], "Already signed in to system.ai.github") require.NoError(t, p.finish(t)) } func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { - srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + srv, _ := challengedService(t) signIn := &signInStub{reply: func(call int) (bool, error) { if call == 1 { return false, &SignInTimeout{URL: testURL, Wait: 5 * time.Minute} @@ -225,32 +250,36 @@ func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { }} 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, 1) - require.Len(t, lines, 1) - assert.Equal(t, true, parseReply(t, lines[0]).Result["isError"]) - text := resultText(t, lines[0]) + 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, "databricks aitools mcp login system.ai.github --profile logfood") p.send(t, fmt.Sprintf(authenticate, 8)) - lines = p.waitLines(t, 3) + 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[1], "Signed in to system.ai.github") + assert.Contains(t, lines[2], "Signed in to system.ai.github") require.NoError(t, p.finish(t)) } func TestAuthenticateReportsFailureWithoutListChanged(t *testing.T) { - srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, listTools)) + srv, _ := challengedService(t) signIn := &signInStub{reply: func(int) (bool, error) { return false, errors.New("sign-in failed for the test") }} - out, _ := runAll(t, challengedBridge(srv.URL, signIn.SignIn), - `{"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`+"\n") + 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, out, 1) - assert.Equal(t, true, parseReply(t, out[0]).Result["isError"]) - assert.Contains(t, out[0], "Sign-in to system.ai.github on profile logfood failed: sign-in failed for the test") + 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.Contains(t, lines[1], "Sign-in to system.ai.github on profile logfood failed: sign-in failed for the test") } // TestChallengeTurnsOnAfterSignIn runs the flow an agent sees: a challenged list, authenticate, then the @@ -297,22 +326,25 @@ func TestSignInTimeoutKeepsLastCheck(t *testing.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) { - newBridge := func(url string, signIn SignInFunc) *Bridge { - b := bridgeFor(url) - b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}} - return b - } srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, callChallenge)) - out, _ := runAll(t, newBridge(srv.URL, nil), `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`+"\n") - require.Len(t, out, 2) - assert.Contains(t, resultText(t, out[0]), "Call the authenticate tool, or run: "+loginCommand+". If") - - timeout := func(context.Context, bool) (bool, bool, error) { + var calls atomic.Int32 + signIn := func(ctx context.Context, _ bool) (bool, bool, error) { + calls.Add(1) return false, true, &SignInTimeout{URL: testURL, Wait: 5 * time.Minute} } - out, _ = runAll(t, newBridge(srv.URL, timeout), `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"authenticate","arguments":{}}}`+"\n") - require.Len(t, out, 1) - assert.Contains(t, resultText(t, out[0]), "then retry, or run: "+loginCommand) + b := bridgeFor(srv.URL) + b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}} + 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, or run: "+loginCommand) } // TestAuthenticateWritesNothingWhenCancelled checks that an authenticate call the client cancelled is not answered: the @@ -423,3 +455,17 @@ func TestAuthenticateOpensPageAgainUnlessACancelledAttemptShowedIt(t *testing.T) }) } } + +// 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) +} From deb6121fa0b6dfd7eaae465f92006dce96327fb8 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:07:32 +0000 Subject: [PATCH 11/55] Match service names without case in sign-in URLs and provider labels Binding and Unity Catalog compare service names without case, but a sign-in URL with a different case from the configured service name was not recognized, and the known-service table was looked up by a case-sensitive leaf. The URL text check also missed a sign-in URL that follows an opening bracket or quote. Discovery uses the same lookup, so a service listed in another case gets the same description. Co-authored-by: Isaac --- libs/aitools/mcp/discovery.go | 4 +-- libs/aitools/mcp/known.go | 7 +++-- libs/aitools/mcp/known_test.go | 1 + libs/aitools/mcp/proxy/challenge.go | 4 +-- libs/aitools/mcp/proxy/challenge_test.go | 40 ++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 7 deletions(-) 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 2fdce73a87b..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" @@ -59,9 +61,10 @@ var knownSystemAI = map[string]KnownService{ }, } -// KnownOf returns the CLI's description of a service, or nil when it is not a system.ai service in the table. +// 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[LeafOf(fullName)] + known, ok := knownSystemAI[strings.ToLower(LeafOf(fullName))] if !ok || !inSystemAI(fullName) { return nil } diff --git a/libs/aitools/mcp/known_test.go b/libs/aitools/mcp/known_test.go index ddde346fd91..b401338982d 100644 --- a/libs/aitools/mcp/known_test.go +++ b/libs/aitools/mcp/known_test.go @@ -43,4 +43,5 @@ func TestProviderLabel(t *testing.T) { 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/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 6411462a0ce..8419acd4108 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -191,7 +191,7 @@ func (h *signInHooks) urlElicitation(data json.RawMessage) bool { func (h *signInHooks) textHasSignInURL(content []textContent) bool { for _, c := range content { for word := range strings.FieldsSeq(c.Text) { - if h.isSignInURL(strings.TrimRight(word, ".,;:)\"'")) { + if h.isSignInURL(strings.Trim(word, "([\"'.,;:)]")) { return true } } @@ -210,7 +210,7 @@ func (h *signInHooks) isSignInURL(raw string) bool { return false } return u.Scheme == base.Scheme && strings.EqualFold(u.Host, base.Host) && - u.Path == signInPath && u.Query().Get("name") == h.c.Service + u.Path == signInPath && strings.EqualFold(u.Query().Get("name"), h.c.Service) } // authenticateList is the one-tool list a challenged service answers with. diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 03cdebed7ca..a88581180d6 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -2,6 +2,7 @@ package proxy import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -469,3 +470,42 @@ func TestAuthenticateIsForwardedBeforeAChallenge(t *testing.T) { 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 = &signInHooks{c: &Challenge{Service: "SYSTEM.AI.GITHUB", Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: loginCommand}} + 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 '", "';"}, + } { + 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") + }) + } +} From 0ba0ec2e2e8740b44c67c345f7b8f3857553981d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:09:28 +0000 Subject: [PATCH 12/55] Start a fresh sign-in after a challenge that follows an already-signed-in answer An authenticate call answered "Already signed in" sent tools/list_changed, so a service that still challenged the caller, for example after its grant was revoked, started a loop of re-lists, challenges and the same answer. The answer no longer asks the agent to list again, since nothing changed. A challenge that follows it makes the next authenticate delete the caller's credential first, so the sign-in waits for a new one. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 10 ++- cmd/aitools/mcp_serve_test.go | 64 +++++++++++++++++++ libs/aitools/mcp/proxy/challenge.go | 52 ++++++++++++---- libs/aitools/mcp/proxy/challenge_test.go | 78 ++++++++++++++++++++---- 4 files changed, 179 insertions(+), 25 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 646180607c7..8dde6a3cdd4 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -168,7 +168,13 @@ func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *m func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { // 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, open bool) (bool, bool, error) { + return func(ctx context.Context, req proxy.SignInRequest) (bool, bool, error) { + if req.Restart { + // The credential is deleted first, so that the sign-in below starts from no credential. + if _, err := mcp.SignOut(ctx, w, service); err != nil { + return false, false, err + } + } if workspaceID == "" { id, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { @@ -179,7 +185,7 @@ func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile url := mcp.SignInURL(w.Config.Host, service, workspaceID) // The opener is called even when the page must not open, so a call that must not open it passes one that does nothing. openPage := func(context.Context, string) error { return nil } - if open { + if req.Open { openPage = browser.OpenOutputToStderr } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 80baf1dede9..625839cd393 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -806,3 +806,67 @@ func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { 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) } + +// TestServeSignInRestartDeletesCredentialFirst checks the sign-in of the authenticate tool. A restart deletes the caller's +// credential before the sign-in reads it, so the sign-in waits for a new one rather than answering that the caller is +// already signed in. Without a restart a credential that reads ACTIVE is already signed in and is kept. +func TestServeSignInRestartDeletesCredentialFirst(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" + const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` + for _, tc := range []struct { + name string + req proxy.SignInRequest + wantDeleted bool + wantAlready bool + }{ + {name: "without restart the credential is kept", req: proxy.SignInRequest{Open: true}, wantAlready: true}, + {name: "restart deletes it first", req: proxy.SignInRequest{Open: true, Restart: true}, wantDeleted: true}, + } { + t.Run(tc.name, func(t *testing.T) { + var mu sync.Mutex + deleted, gets := false, 0 + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + if r.URL.Path != credentialPath { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodDelete: + deleted = true + _, _ = io.WriteString(rw, `{}`) + case !deleted: + _, _ = io.WriteString(rw, active) + default: + // The caller has not signed in yet after the delete, and then does. + gets++ + if gets == 1 { + rw.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) + return + } + _, _ = io.WriteString(rw, active) + } + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) + require.NoError(t, err) + + signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) + alreadySignedIn, _, err := signIn(t.Context(), tc.req) + + require.NoError(t, err) + assert.Equal(t, tc.wantAlready, alreadySignedIn) + mu.Lock() + defer mu.Unlock() + assert.Equal(t, tc.wantDeleted, deleted) + }) + } +} diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 8419acd4108..17debbc2f92 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -38,11 +38,19 @@ func (e *SignInTimeout) Unwrap() error { return e.LastCheck } -// SignInFunc runs the sign-in for the authenticate tool and waits for it. open is false when the -// sign-in page must not be opened again in this session. 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. It returns a *SignInTimeout when the wait ended first. -type SignInFunc func(ctx context.Context, open bool) (alreadySignedIn, urlShown bool, err error) +// 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 + // Restart deletes the caller's sign-in before the sign-in runs, so that it starts from no credential. The proxy asks + // for it when the service challenged the session again after an "Already signed in" answer. + Restart bool +} + +// 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. It returns a *SignInTimeout when the wait ended first. +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 { @@ -70,10 +78,16 @@ type signInHooks struct { // completed. The user may still be on that page, so the next attempt does not open it again. pageCancelled bool - // stateMu guards challenged. It is held only briefly, so an answer to a request is not held up by a sign-in. + // stateMu guards the three fields below. It is held only briefly, so an answer to a request is not held up by a + // sign-in. stateMu sync.Mutex // sessionChallenged means a request of this session was challenged. The authenticate tool is the proxy's only after that. sessionChallenged bool + // alreadySignedIn means the last authenticate answered "Already signed in". + alreadySignedIn bool + // restart means the next sign-in starts from no credential. A challenge that follows an "Already signed in" answer + // sets it: the credential reads as signed in, but the service does not accept it. + restart bool } // Local answers the authenticate tool call once the session has been challenged. Before that, and for other requests and @@ -123,8 +137,12 @@ func (h *signInHooks) isChallenge(status int, message []byte) bool { return false } h.stateMu.Lock() + defer h.stateMu.Unlock() h.sessionChallenged = true - h.stateMu.Unlock() + if h.alreadySignedIn { + h.restart = true + h.alreadySignedIn = false + } return true } @@ -240,8 +258,20 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] h.mu.Lock() defer h.mu.Unlock() - alreadySignedIn, urlShown, err := h.c.SignIn(ctx, !h.pageCancelled) + h.stateMu.Lock() + restart := h.restart + h.restart = false + h.stateMu.Unlock() + + alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Restart: restart}) h.pageCancelled = urlShown && err != nil && ctx.Err() != nil + h.stateMu.Lock() + h.alreadySignedIn = err == nil && alreadySignedIn + if ctx.Err() != nil { + // A restart that was cancelled may not have deleted the credential, so the next sign-in restarts again. + h.restart = h.restart || restart + } + h.stateMu.Unlock() if ctx.Err() != nil { // The client cancelled the request or shutdown began. Nothing is answered for a cancelled request. return nil @@ -250,10 +280,8 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] switch { case err == nil && alreadySignedIn: - 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, - } + // The credential was already there, so nothing changed for the agent's tool list. + 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)} 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), diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index a88581180d6..8d4dac415e5 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -30,26 +30,38 @@ const ( listChangedMsg = `{"jsonrpc":"2.0","method":"notifications/tools/list_changed"}` ) -// signInStub replaces the command's sign-in. Each call records its open flag and returns reply(call), with the page +// 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 - opens []bool + reqs []SignInRequest reply func(call int) (bool, error) } -func (s *signInStub) SignIn(_ context.Context, open bool) (bool, bool, error) { +func (s *signInStub) SignIn(_ context.Context, req SignInRequest) (bool, bool, error) { s.mu.Lock() defer s.mu.Unlock() - s.opens = append(s.opens, open) - alreadySignedIn, err := s.reply(len(s.opens)) + 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() - return append([]bool(nil), s.opens...) + 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. @@ -234,10 +246,11 @@ func TestAuthenticateSaysAlreadySignedInAfterSignIn(t *testing.T) { p.send(t, fmt.Sprintf(authenticate, 5)) p.waitLines(t, 3) p.send(t, fmt.Sprintf(authenticate, 6)) - lines := p.waitLines(t, 5) + lines := p.waitLines(t, 4) 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.NotContains(t, lines[3:], listChangedMsg, "an answer that changed nothing does not ask the agent to list again") require.NoError(t, p.finish(t)) } @@ -329,7 +342,7 @@ func TestSignInTimeoutKeepsLastCheck(t *testing.T) { func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, callChallenge)) var calls atomic.Int32 - signIn := func(ctx context.Context, _ bool) (bool, bool, error) { + signIn := func(ctx context.Context, _ SignInRequest) (bool, bool, error) { calls.Add(1) return false, true, &SignInTimeout{URL: testURL, Wait: 5 * time.Minute} } @@ -360,7 +373,7 @@ func TestAuthenticateWritesNothingWhenCancelled(t *testing.T) { answerWith(http.StatusOK, listChallenge)(w, r) }) entered, returned := make(chan struct{}), make(chan struct{}) - signIn := func(ctx context.Context, _ bool) (bool, bool, error) { + signIn := func(ctx context.Context, _ SignInRequest) (bool, bool, error) { close(entered) <-ctx.Done() close(returned) @@ -413,9 +426,9 @@ func TestAuthenticateOpensPageAgainUnlessACancelledAttemptShowedIt(t *testing.T) entered, done := make(chan struct{}), make(chan struct{}) var mu sync.Mutex var opens []bool - signIn := func(ctx context.Context, open bool) (bool, bool, error) { + signIn := func(ctx context.Context, req SignInRequest) (bool, bool, error) { mu.Lock() - opens = append(opens, open) + opens = append(opens, req.Open) first := len(opens) == 1 mu.Unlock() if !first { @@ -509,3 +522,46 @@ func TestChallengeTextAroundSignInURL(t *testing.T) { }) } } + +// TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn checks that when the service challenges again after +// an "Already signed in" answer, the next authenticate starts a new sign-in from no credential. The credential reads +// ACTIVE, but the service does not accept it, so answering "Already signed in" again would loop. +func TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn(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, 2) + p.send(t, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) + p.waitLines(t, 3) + p.send(t, fmt.Sprintf(authenticate, 7)) + lines := p.waitLines(t, 4) + require.NoError(t, p.finish(t)) + + assert.Equal(t, []SignInRequest{{Open: true}, {Open: true, Restart: true}}, signIn.requests()) + assert.Contains(t, lines[1], "Already signed in to system.ai.github") + assert.Contains(t, lines[3], "Signed in to system.ai.github") +} + +// TestAuthenticateStaleAnswerWithoutChallengeKeepsCredential checks that a second authenticate that is not preceded by a +// challenge does not delete the credential, so it answers "Already signed in" again. +func TestAuthenticateStaleAnswerWithoutChallengeKeepsCredential(t *testing.T) { + srv, _ := challengedService(t) + 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, 2) + p.send(t, fmt.Sprintf(authenticate, 6)) + p.waitLines(t, 3) + require.NoError(t, p.finish(t)) + + for _, req := range signIn.requests() { + assert.False(t, req.Restart) + } +} From c9308eae4c2710e02e58811374fd7cb539069b77 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:13:06 +0000 Subject: [PATCH 13/55] Route serve's sign-in client through the refresh guard and bound the startup read The sign-in client authenticated outside the guard that stops the proxy on a token refresh failure, so such a failure during authenticate reported the raw error without the login hint and left the proxy running. A failed sign-in or startup call is now followed by one authentication through the guard. The startup read of a server with no install record is bounded as the list --check checks are, so it does not wait for the SDK's retry budget, and its auth failure exits 2 with the login hint, as the other credential failures do. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 83 +++++++++++++++++------- cmd/aitools/mcp_serve_test.go | 116 +++++++++++++++++++++++++++++++--- 2 files changed, 168 insertions(+), 31 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 8dde6a3cdd4..b24606f9343 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -113,40 +113,45 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { return serveFailure(cmd, err, serveBindStatus) } + // 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() + // The sign-in client and the bridge authenticate through the same guard, so a refresh failure in either stops the proxy. + refresh := &refreshGuard{authenticate: cfg.Authenticate, profile: profileName, pinnedFile: pinnedFile, cancel: stop} + w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) if err != nil { return serveFailure(cmd, err, 1) } - // A server pinned to a config file names that file in its hints, as add and login do. - pinnedFile := mcp.PinnedFile(entry, cfg.ConfigFile) - challenge, err := serveChallenge(cmd.Context(), w, entry, profileName, f.service, pinnedFile, cmd.ErrOrStderr()) + challenge, err := serveChallenge(ctx, w, refresh, entry, profileName, f.service, pinnedFile, cmd.ErrOrStderr()) if err != nil { - return serveFailure(cmd, err, 1) + return serveFailure(cmd, err, serveStartStatus(err)) } - return serveProxy(cmd, proxy.Options{ + 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, - Authenticate: cfg.Authenticate, + Authenticate: refresh.authenticateRequest, Profile: profileName, Agent: serveAgent(f.agent, cmd.ErrOrStderr()), Transport: serveTransport(cfg), Challenge: challenge, - }, pinnedFile) + }) } // 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. // pinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). -func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service, pinnedFile string, errOut io.Writer) (*proxy.Challenge, error) { +func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, refresh *refreshGuard, entry *mcp.Entry, profileName, service, pinnedFile string, errOut io.Writer) (*proxy.Challenge, error) { signInRequired := entry != nil && entry.SignInRequired if entry == nil { - svc, err := mcp.GetService(ctx, w, service) + svc, err := lookupAtStart(ctx, w, refresh, profileName, pinnedFile, service) if err != nil { - return nil, fmt.Errorf("cannot read MCP service %s: %w", service, err) + return nil, err } signInRequired = svc.SignInRequired } @@ -159,26 +164,39 @@ func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *m Host: w.Config.Host, Label: mcp.ProviderLabel(service), LoginCommand: mcpLoginHint(service, profileName, pinnedFile), - SignIn: serveSignIn(w, profileName, service, pinnedFile, errOut), + SignIn: serveSignIn(w, refresh, profileName, service, pinnedFile, errOut), }, 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, refresh *refreshGuard, 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, refresh.callFailed(ctx, err)) + return mcp.Service{}, fmt.Errorf("cannot read MCP service %s: %w", service, err) + } + return svc, nil +} + // serveSignIn is the authenticate tool's sign-in, the one `login` runs. Progress goes to errOut, which is stderr because // stdout carries the protocol. The page opens only when open is set, so the proxy decides how often the browser opens. -func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { +func serveSignIn(w *databricks.WorkspaceClient, refresh *refreshGuard, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { // 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 req.Restart { // The credential is deleted first, so that the sign-in below starts from no credential. if _, err := mcp.SignOut(ctx, w, service); err != nil { - return false, false, err + return false, false, refresh.callFailed(ctx, err) } } if workspaceID == "" { id, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return false, false, err + return false, false, refresh.callFailed(ctx, err) } workspaceID = id } @@ -193,18 +211,13 @@ func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile if timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err); timedOut { return false, urlShown, &proxy.SignInTimeout{URL: url, Wait: signInWaitTimeout, LastCheck: timeout.LastCheck} } - return alreadySignedIn, urlShown, err + return alreadySignedIn, urlShown, refresh.callFailed(ctx, err) } } -// 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 { - ctx, stop := serveSignalContext(cmd.Context()) - defer stop() - refresh := &refreshGuard{authenticate: opts.Authenticate, profile: opts.Profile, pinnedFile: pinnedFile, cancel: stop} - opts.Authenticate = refresh.authenticateRequest +// serveProxy bridges opts to the service until stdin closes or ctx is done. opts.Authenticate must be refresh's, so that a +// refresh failure stops the proxy; see proxyResult. +func serveProxy(ctx context.Context, cmd *cobra.Command, refresh *refreshGuard, opts proxy.Options) error { return proxyResult(cmd, refresh, proxy.Run(ctx, opts)) } @@ -255,6 +268,15 @@ 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 be, ok := errors.AsType[*mcp.BindError](err); ok && be.Category == mcp.BindAuthFailed { + return serveBindStatus + } + return 1 +} + // 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 { @@ -319,6 +341,21 @@ func (g *refreshGuard) authenticateRequest(r *http.Request) error { return authErr } +// callFailed returns the failure of a call that the sign-in client made. The sign-in client authenticates inside the SDK, +// where the guard does not see its requests, so after a failed call the guard authenticates once, as it does for a bridge +// request. A token that cannot be refreshed is then the failure that stops the proxy. Otherwise the call's failure is +// returned. +func (g *refreshGuard) callFailed(ctx context.Context, err error) error { + if err == nil || ctx.Err() != nil { + return err + } + probe := (&http.Request{Method: http.MethodGet, Header: http.Header{}}).WithContext(ctx) + if authErr := g.authenticateRequest(probe); authErr != nil { + return authErr + } + return err +} + // stoppedError is the error for a request that arrives after the refresh failure. It does not repeat the login hint. func (g *refreshGuard) stoppedError() error { return fmt.Errorf("the session is stopping after a token refresh failure for profile %s", g.profile) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 625839cd393..a47ca1637dc 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -595,15 +595,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: "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: expiresOnToolsList, + Authenticate: refresh.authenticateRequest, Profile: "logfood", - }, "") + }) return stderr.String(), deleted.Load(), err } @@ -774,7 +777,7 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "", io.Discard) + challenge, err := serveChallenge(t.Context(), w, passingGuard(), &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "", io.Discard) require.NoError(t, err) require.NotNil(t, challenge) assert.Equal(t, "system.ai.github", challenge.Service) @@ -783,12 +786,12 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { 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(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "", io.Discard) + challenge, err = serveChallenge(t.Context(), w, passingGuard(), &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "", io.Discard) require.NoError(t, err) assert.Nil(t, challenge) assert.Equal(t, int32(0), lookups.Load()) - challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) + challenge, err = serveChallenge(t.Context(), w, passingGuard(), nil, "logfood", "system.ai.github", "", 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") @@ -801,7 +804,7 @@ func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { require.NoError(t, err) const pinned = "/work/cfg/my file.cfg" - challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", pinned, io.Discard) + challenge, err := serveChallenge(t.Context(), w, passingGuard(), &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", 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) @@ -859,7 +862,7 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) require.NoError(t, err) - signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) + signIn := serveSignIn(w, passingGuard(), "logfood", "system.ai.github", "", io.Discard) alreadySignedIn, _, err := signIn(t.Context(), tc.req) require.NoError(t, err) @@ -870,3 +873,100 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { }) } } + +// passingGuard returns a refresh guard whose tokens always refresh, for tests that do not exercise a refresh failure. +func passingGuard() *refreshGuard { + return &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: "logfood", cancel: func() {}} +} + +// failingGuard returns a refresh guard whose token cannot be refreshed, and records whether the proxy was stopped. +func failingGuard(stopped *bool) *refreshGuard { + return &refreshGuard{ + authenticate: func(*http.Request) error { return errors.New(refreshTokenExpired) }, + profile: "logfood", + cancel: func() { *stopped = true }, + } +} + +// 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. +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() + _, err = serveChallenge(t.Context(), w, passingGuard(), nil, "logfood", "system.ai.github", "", io.Discard) + + require.Error(t, err) + assert.Less(t, time.Since(start), 10*time.Second, "the lookup must not wait for the SDK's retry budget") +} + +// TestServeChallengeLookupAuthFailureExitsTwo checks that a startup read the profile's credential does not pass 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(), w, passingGuard(), nil, "logfood", "system.ai.github", "", io.Discard) + + require.Error(t, err) + assert.Equal(t, serveBindStatus, serveStartStatus(err)) + assert.Contains(t, err.Error(), "databricks auth login --profile logfood") +} + +// 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"))) +} + +// TestServeChallengeLookupRefreshFailureStopsProxy checks that a token that cannot be refreshed during the startup read +// stops the proxy with the login hint, as a refresh failure of a bridge request does. +func TestServeChallengeLookupRefreshFailureStopsProxy(t *testing.T) { + server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + var stopped bool + + _, err = serveChallenge(t.Context(), w, failingGuard(&stopped), nil, "logfood", "system.ai.github", "", io.Discard) + + require.Error(t, err) + assert.True(t, stopped, "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") +} + +// TestServeSignInRefreshFailureStopsProxy checks that a token that cannot be refreshed during the sign-in stops the proxy +// with the login hint, as a refresh failure of a bridge request does, instead of an error without the hint. +func TestServeSignInRefreshFailureStopsProxy(t *testing.T) { + t.Setenv("BROWSER", "none") + server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) + require.NoError(t, err) + var stopped bool + signIn := serveSignIn(w, failingGuard(&stopped), "logfood", "system.ai.github", "", io.Discard) + + _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true}) + + require.Error(t, err) + assert.True(t, stopped, "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") +} From ee5b5083020d5e44ee900cc5a649a715522a5582 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:17:04 +0000 Subject: [PATCH 14/55] Send progress notifications while authenticate waits An authenticate call waits up to five minutes, longer than the request timeout of common MCP clients, and sent nothing until it answered. A call that carries a progress token now gets a notifications/progress message on each poll of the sign-in. The sign-in helper takes its options as the mcp package's, so the poll callback reaches it. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 2 +- cmd/aitools/mcp_login.go | 22 ++++------ cmd/aitools/mcp_login_test.go | 4 +- cmd/aitools/mcp_serve.go | 2 +- cmd/aitools/mcp_serve_test.go | 36 ++++++++++++++++ libs/aitools/mcp/login.go | 6 ++- libs/aitools/mcp/login_test.go | 17 ++++++++ libs/aitools/mcp/proxy/bridge.go | 7 +-- libs/aitools/mcp/proxy/bridge_test.go | 4 +- libs/aitools/mcp/proxy/challenge.go | 40 ++++++++++++++--- libs/aitools/mcp/proxy/challenge_test.go | 55 +++++++++++++++++++++++- 11 files changed, 166 insertions(+), 29 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index f209d95e65a..4e5df671248 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -880,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, browser.OpenOutputToStderr) + state, _, _, err := signIn(ctx, errOut, w, req.pinned, url, plans[i].service, mcp.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 fdfdb4b6d7b..3100319ef8c 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -180,7 +180,7 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie 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, browser.OpenOutputToStderr) + state, _, urlShown, err := signIn(ctx, errOut, w, pinnedFile, url, svc, mcp.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 { @@ -233,18 +233,14 @@ 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 the caller was already signed in, whether url was printed, and the failure, which is categorized for -// JSON. open opens the page; it is called only when the browser is enabled. `add --login` shares this. -func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, profileName, pinnedFile, url string, svc mcp.Service, - open func(context.Context, string) error, -) (state string, alreadySignedIn, urlShown bool, err error) { - alreadySignedIn, urlShown, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ - Profile: profileName, - Poll: signInPollInterval, - Timeout: signInWaitTimeout, - Open: open, - }) +// 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. opts sets the page opener and the poll callback; `add --login` shares this. +func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, pinnedFile, url string, svc mcp.Service, opts mcp.SignInOptions) (state string, alreadySignedIn, urlShown bool, err error) { + profileName := opts.Profile + opts.Poll = signInPollInterval + opts.Timeout = signInWaitTimeout + alreadySignedIn, urlShown, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, opts) timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { case timedOut: diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index f6bfd7ddde4..37680fa2163 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -345,8 +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}, - func(context.Context, string) error { return nil }) + state, _, _, err := signIn(ctx, io.Discard, w, "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", SignInRequired: true}, + mcp.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 b24606f9343..0be517f254c 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -207,7 +207,7 @@ func serveSignIn(w *databricks.WorkspaceClient, refresh *refreshGuard, profileNa openPage = browser.OpenOutputToStderr } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} - _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, profileName, pinnedFile, url, svc, openPage) + _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: req.Progress}) if timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err); timedOut { return false, urlShown, &proxy.SignInTimeout{URL: url, Wait: signInWaitTimeout, LastCheck: timeout.LastCheck} } diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index a47ca1637dc..9f7d6b38f0f 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -970,3 +970,39 @@ func TestServeSignInRefreshFailureStopsProxy(t *testing.T) { 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(w, passingGuard(), "logfood", "system.ai.github", "", io.Discard) + _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() { polls++ }}) + + require.NoError(t, err) + assert.Equal(t, 2, polls) +} diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 18e71d9c9bd..c96fce0d8ef 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -40,12 +40,13 @@ 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. +// 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 @@ -126,6 +127,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..894e91d8030 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -563,3 +563,20 @@ 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) +} diff --git a/libs/aitools/mcp/proxy/bridge.go b/libs/aitools/mcp/proxy/bridge.go index 1c82c089453..accfcd60729 100644 --- a/libs/aitools/mcp/proxy/bridge.go +++ b/libs/aitools/mcp/proxy/bridge.go @@ -60,8 +60,9 @@ var ( 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. - Local(ctx context.Context, request []byte) (msgs [][]byte, handled bool) + // 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 @@ -271,7 +272,7 @@ 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) + msgs, handled := c.b.Hooks.Local(ctx, line, c.reply) if err := c.writeLines(msgs); err != nil { c.logf("stdout: %v", err) } diff --git a/libs/aitools/mcp/proxy/bridge_test.go b/libs/aitools/mcp/proxy/bridge_test.go index c5c4ebf81ea..1a8980c2947 100644 --- a/libs/aitools/mcp/proxy/bridge_test.go +++ b/libs/aitools/mcp/proxy/bridge_test.go @@ -827,7 +827,7 @@ func (f hookFunc) Result(ctx context.Context, request []byte, status int, msg [] return f(ctx, request, status, msg) } -func (hookFunc) Local(context.Context, []byte) ([][]byte, bool) { +func (hookFunc) Local(context.Context, []byte, func([]byte)) ([][]byte, bool) { return nil, false } @@ -2206,7 +2206,7 @@ func (localHooks) Result(_ context.Context, _ []byte, _ int, msg []byte) ([][]by return [][]byte{msg}, nil } -func (h localHooks) Local(_ context.Context, request []byte) ([][]byte, bool) { +func (h localHooks) Local(_ context.Context, request []byte, _ func([]byte)) ([][]byte, bool) { if !bytes.Contains(request, []byte("local")) { return nil, false } diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 17debbc2f92..27d7e925de2 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -45,6 +45,8 @@ type SignInRequest struct { // Restart deletes the caller's sign-in before the sign-in runs, so that it starts from no credential. The proxy asks // for it when the service challenged the session again after an "Already signed in" answer. Restart bool + // Progress is called on each poll of the wait. It is never nil. + Progress func() } // SignInFunc runs the sign-in for the authenticate tool and waits for it. It reports alreadySignedIn when the caller @@ -92,11 +94,14 @@ type signInHooks struct { // 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) ([][]byte, bool) { +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 { @@ -108,7 +113,7 @@ func (h *signInHooks) Local(ctx context.Context, request []byte) ([][]byte, bool if !challenged { return nil, false } - return h.authenticate(ctx, req.ID), true + return h.authenticate(ctx, req.ID, req.Params.Meta.ProgressToken, notify), true } // Result replaces a challenged tools/list answer with the authenticate tool. A challenged tools/call @@ -253,8 +258,9 @@ func (h *signInHooks) signInRequired(id json.RawMessage) []byte { } // authenticate runs the sign-in and answers the tool call. Calls are serialized, so a second call waits for the first. -// The page opens unless the last attempt was cancelled while its page was showing (see pageCancelled). -func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][]byte { +// The page opens unless the last attempt was cancelled while its page was showing (see pageCancelled). With a +// progressToken, each poll of the wait is reported to the client through notify. +func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.RawMessage, notify func([]byte)) [][]byte { h.mu.Lock() defer h.mu.Unlock() @@ -263,7 +269,15 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] h.restart = false h.stateMu.Unlock() - alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Restart: restart}) + progress := func() {} + if len(progressToken) > 0 { + polls := 0 + progress = func() { + polls++ + notify(progressNotification(progressToken, polls, h.c.Label)) + } + } + alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Restart: restart, Progress: progress}) h.pageCancelled = urlShown && err != nil && ctx.Err() != nil h.stateMu.Lock() h.alreadySignedIn = err == nil && alreadySignedIn @@ -296,6 +310,22 @@ func (h *signInHooks) authenticate(ctx context.Context, id json.RawMessage) [][] } } +// progressNotification is the notifications/progress message for the n-th poll of a sign-in that the client asked to +// hear about through token. +func progressNotification(token json.RawMessage, n int, label string) []byte { + // Cannot fail: the token came from a parsed request and the rest holds plain values. + b, _ := json.Marshal(map[string]any{ + "jsonrpc": "2.0", + "method": "notifications/progress", + "params": map[string]any{ + "progressToken": token, + "progress": n, + "message": fmt.Sprintf("Waiting for the sign-in to %s to complete.", label), + }, + }) + 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. diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 8d4dac415e5..823511345f8 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/http/httptest" + "strings" "sync" "sync/atomic" "testing" @@ -541,7 +542,10 @@ func TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn(t *testi lines := p.waitLines(t, 4) require.NoError(t, p.finish(t)) - assert.Equal(t, []SignInRequest{{Open: true}, {Open: true, Restart: true}}, signIn.requests()) + reqs := signIn.requests() + require.Len(t, reqs, 2) + assert.False(t, reqs[0].Restart) + assert.True(t, reqs[1].Restart, "the sign-in after the challenge starts from no credential") assert.Contains(t, lines[1], "Already signed in to system.ai.github") assert.Contains(t, lines[3], "Signed in to system.ai.github") } @@ -565,3 +569,52 @@ func TestAuthenticateStaleAnswerWithoutChallengeKeepsCredential(t *testing.T) { assert.False(t, req.Restart) } } + +// 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") +} From f5d7e526e0dd93c39f2bdef209faa5b09bcc2c01 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:17:46 +0000 Subject: [PATCH 15/55] Correct the serve comments on the sign-in options and the shared guard Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 0be517f254c..368118d3c4c 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -117,7 +117,7 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { pinnedFile := mcp.PinnedFile(entry, cfg.ConfigFile) ctx, stop := serveSignalContext(cmd.Context()) defer stop() - // The sign-in client and the bridge authenticate through the same guard, so a refresh failure in either stops the proxy. + // 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} w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) @@ -182,7 +182,7 @@ func lookupAtStart(ctx context.Context, w *databricks.WorkspaceClient, refresh * } // serveSignIn is the authenticate tool's sign-in, the one `login` runs. Progress goes to errOut, which is stderr because -// stdout carries the protocol. The page opens only when open is set, so the proxy decides how often the browser opens. +// stdout carries the protocol. The page opens only when req.Open is set, so the proxy decides how often the browser opens. func serveSignIn(w *databricks.WorkspaceClient, refresh *refreshGuard, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { // 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 From c66c26c3ebf09dae544adbd6cf8607b200a29c5b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:21:14 +0000 Subject: [PATCH 16/55] Apply the whitespace and assertion fixes from the repo checks Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/serve-authenticate-tool/script | 1 - libs/aitools/mcp/proxy/challenge_test.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script index 1a7697a430b..fce69b659bb 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script @@ -38,4 +38,3 @@ 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/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 823511345f8..17233121af9 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -335,7 +335,7 @@ func TestSignInTimeoutKeepsLastCheck(t *testing.T) { err := error(&SignInTimeout{URL: testURL, Wait: time.Minute, LastCheck: lastCheck}) assert.ErrorIs(t, err, lastCheck) - assert.Nil(t, errors.Unwrap(&SignInTimeout{URL: testURL})) + assert.NoError(t, errors.Unwrap(&SignInTimeout{URL: testURL})) } // TestChallengeHintsPrintTheLoginCommand checks that the challenge and the timeout print the login command that the From e5e1e4877939809ce93bff437dd69a3ba9d240dd Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:27:02 +0000 Subject: [PATCH 17/55] Ask the agent to list again after an already-signed-in answer When the credential is already active, for example from a sign-in in a terminal, the agent's cached tool list still shows only authenticate, so the answer is followed by tools/list_changed. A challenge after that answer still makes the next authenticate start a new sign-in, so the two cannot loop. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 9 +++++++-- libs/aitools/mcp/proxy/challenge_test.go | 14 ++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 27d7e925de2..dd26a87f6a9 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -294,8 +294,13 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R switch { case err == nil && alreadySignedIn: - // The credential was already there, so nothing changed for the agent's tool list. - 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)} + // The credential was already there, for example from `mcp login` in a terminal, while the agent's tool list + // still shows only authenticate, so the agent is still asked to list again. If the service challenges again + // after this answer, the next authenticate starts a new sign-in instead. + 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), diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 17233121af9..20e35c1fbda 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -247,11 +247,11 @@ func TestAuthenticateSaysAlreadySignedInAfterSignIn(t *testing.T) { p.send(t, fmt.Sprintf(authenticate, 5)) p.waitLines(t, 3) p.send(t, fmt.Sprintf(authenticate, 6)) - lines := p.waitLines(t, 4) + 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.NotContains(t, lines[3:], listChangedMsg, "an answer that changed nothing does not ask the agent to list again") + 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)) } @@ -534,12 +534,13 @@ func TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn(t *testi 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, 2) - p.send(t, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`) 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)) - lines := p.waitLines(t, 4) + lines := p.waitLines(t, 5) require.NoError(t, p.finish(t)) reqs := signIn.requests() @@ -547,7 +548,8 @@ func TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn(t *testi assert.False(t, reqs[0].Restart) assert.True(t, reqs[1].Restart, "the sign-in after the challenge starts from no credential") assert.Contains(t, lines[1], "Already signed in to system.ai.github") - assert.Contains(t, lines[3], "Signed in to system.ai.github") + assert.Equal(t, listChangedMsg, lines[2]) + assert.Contains(t, lines[4], "Signed in to system.ai.github") } // TestAuthenticateStaleAnswerWithoutChallengeKeepsCredential checks that a second authenticate that is not preceded by a From 2b6b3701df1600e659d0e208d21ccb8b2744dd75 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:45:49 +0000 Subject: [PATCH 18/55] Drop the fake's unused sign-in challenge modes The URL elicitation and HTTP 401 challenge modes of the fake were used only by the fake's own tests, and the proxy's tests cover -32042 and 401 with their own handlers. The fake's credential check now matches the service name without case, as its service lookup does. Co-authored-by: Isaac --- libs/testserver/fake_workspace.go | 5 +-- libs/testserver/mcp_endpoint.go | 65 ++++++++-------------------- libs/testserver/mcp_endpoint_test.go | 23 +++------- 3 files changed, 25 insertions(+), 68 deletions(-) diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index 93349ebe945..3dfa5456562 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -249,10 +249,7 @@ type FakeWorkspace struct { McpServices map[string]catalog.McpService // McpUserCredentials holds the caller's sign-in to each MCP service, keyed by the service full name. The fake has one // caller. - McpUserCredentials map[string]catalog.McpServiceUserMappedCredential - // McpSignInChallenge selects how an external service answers a caller with no credential, keyed by - // the service full name. A missing entry gives McpChallengeRPC. - McpSignInChallenge map[string]McpSignInChallenge + McpUserCredentials map[string]catalog.McpServiceUserMappedCredential ModelProviderServices map[string]catalog.ModelProviderService // RegisteredModelVersions counts the versions registered on each model, keyed by its full name. diff --git a/libs/testserver/mcp_endpoint.go b/libs/testserver/mcp_endpoint.go index 57403e1d107..e397c3de2e3 100644 --- a/libs/testserver/mcp_endpoint.go +++ b/libs/testserver/mcp_endpoint.go @@ -3,7 +3,6 @@ package testserver import ( "encoding/json" "net/http" - "net/url" "strings" "github.com/databricks/databricks-sdk-go/service/catalog" @@ -17,8 +16,6 @@ const ( // rpcCodeSignInRequired is the gateway's answer to a service that needs a credential the // caller does not have. rpcCodeSignInRequired = -32006 - // rpcCodeURLElicitation is the gateway's URL elicitation answer, which carries the sign-in page. - rpcCodeURLElicitation = -32042 ) const ( @@ -38,24 +35,11 @@ type mcpRPC struct { } `json:"params"` } -// McpSignInChallenge is how the fake answers a caller who has no credential for an external service. -type McpSignInChallenge string - -const ( - // McpChallengeRPC is JSON-RPC -32006, the gateway's answer before sign-in. - McpChallengeRPC McpSignInChallenge = "rpc" - // McpChallengeURL is JSON-RPC -32042 with one URL elicitation to the sign-in page. - McpChallengeURL McpSignInChallenge = "url" - // McpChallengeHTTP is HTTP 401 with no JSON-RPC body. - McpChallengeHTTP McpSignInChallenge = "http" -) - // 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: until a credential exists for it in McpUserCredentials, tools/list and -// tools/call answer the challenge selected by McpSignInChallenge (-32006 by default, as the gateway -// does before sign-in). Notifications and client responses, which have no method or no id, get 202 -// with no body. +// 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()() @@ -72,8 +56,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { } external := mcpServiceIsExternal(service) - _, signedIn := s.McpUserCredentials[name] - challenged := external && !signedIn + challenged := external && !s.mcpSignedIn(name) switch msg.Method { case "initialize": return rpcResult(msg.ID, map[string]any{ @@ -84,7 +67,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { }) case "tools/list": if challenged { - return s.signInRequired(msg.ID, name) + return s.signInRequired(msg.ID) } return rpcResult(msg.ID, map[string]any{"tools": []any{ map[string]any{ @@ -98,7 +81,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { }}) case "tools/call": if challenged { - return s.signInRequired(msg.ID, name) + return s.signInRequired(msg.ID) } if msg.Params.Name != fakeMcpEchoTool { return Response{Body: rpcError(msg.ID, rpcCodeInvalidParams, "Unknown tool: "+msg.Params.Name)} @@ -124,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 && @@ -144,28 +137,8 @@ func rpcError(id json.RawMessage, code int, message string) map[string]any { } } -// signInRequired answers with the challenge the service is configured for. -func (s *FakeWorkspace) signInRequired(id json.RawMessage, name string) Response { - switch s.McpSignInChallenge[name] { - case McpChallengeURL: - query := url.Values{"name": {name}} - return Response{Body: map[string]any{ - "jsonrpc": "2.0", - "id": id, - "error": map[string]any{ - "code": rpcCodeURLElicitation, - "message": "This request requires more information.", - "data": map[string]any{"elicitations": []any{map[string]any{ - "mode": "url", - "message": "Sign in to continue.", - "url": s.url + "/mcp-service-login?" + query.Encode(), - }}}, - }, - }} - case McpChallengeHTTP: - return Response{StatusCode: http.StatusUnauthorized, Body: "Unauthorized"} - default: - return Response{Body: rpcError(id, rpcCodeSignInRequired, - "The MCP service request was rejected (HTTP 401). Sign-in is required.")} - } +// signInRequired answers with the gateway's -32006 challenge. +func (s *FakeWorkspace) signInRequired(id json.RawMessage) Response { + return Response{Body: rpcError(id, rpcCodeSignInRequired, + "The MCP service request was rejected (HTTP 401). Sign-in is required.")} } diff --git a/libs/testserver/mcp_endpoint_test.go b/libs/testserver/mcp_endpoint_test.go index 5e75f3c943d..fa87d059ea7 100644 --- a/libs/testserver/mcp_endpoint_test.go +++ b/libs/testserver/mcp_endpoint_test.go @@ -114,26 +114,13 @@ func TestMcpEndpointPassesThroughOnceCredentialExists(t *testing.T) { assert.EqualValues(t, -32006, mcpEndpointTestCall(t, workspace, name, list)["error"].(map[string]any)["code"]) } -func TestMcpEndpointURLElicitationChallenge(t *testing.T) { +func TestMcpEndpointCredentialMatchesNameWithoutCase(t *testing.T) { workspace := NewFakeWorkspace("http://test", "dbapi123") const name = "system.ai.github" mcpServicesTestSeed(workspace, name, "GitHub", mcpServicesTestGitHubProvider) - workspace.McpSignInChallenge = map[string]McpSignInChallenge{name: McpChallengeURL} - - reply := mcpEndpointTestCall(t, workspace, name, `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"echo"}}`) - errBody := reply["error"].(map[string]any) - assert.EqualValues(t, -32042, errBody["code"]) - elicitation := errBody["data"].(map[string]any)["elicitations"].([]any)[0].(map[string]any) - assert.Equal(t, "url", elicitation["mode"]) - assert.Equal(t, "http://test/mcp-service-login?name=system.ai.github", elicitation["url"]) -} - -func TestMcpEndpointHTTPUnauthorizedChallenge(t *testing.T) { - workspace := NewFakeWorkspace("http://test", "dbapi123") - const name = "system.ai.github" - mcpServicesTestSeed(workspace, name, "GitHub", mcpServicesTestGitHubProvider) - workspace.McpSignInChallenge = map[string]McpSignInChallenge{name: McpChallengeHTTP} + workspace.McpUserCredentialCreate(name) + const list = `{"jsonrpc":"2.0","id":1,"method":"tools/list"}` - response := workspace.McpEndpoint(Request{Body: []byte(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`)}, name) - assert.Equal(t, 401, response.StatusCode) + reply := mcpEndpointTestCall(t, workspace, "SYSTEM.AI.GITHUB", list) + assert.Nil(t, reply["error"], "the service and the credential match the name without case") } From 02fb32ec16220d334bed43b1926e2b02853dff0e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:46:57 +0000 Subject: [PATCH 19/55] Share the sign-in path and timeout error between mcp and the proxy The proxy had its own copy of the sign-in page path and its own timeout error. The path is now mcp.SignInPath, and the timeout is mcp.SignInTimeoutError, which carries the page URL and the wait. The serve command no longer maps one error to the other. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 3 --- libs/aitools/mcp/login.go | 21 +++++++++++-------- libs/aitools/mcp/proxy/challenge.go | 26 +++++------------------- libs/aitools/mcp/proxy/challenge_test.go | 16 ++++----------- libs/aitools/mcp/url.go | 5 ++++- 5 files changed, 26 insertions(+), 45 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 368118d3c4c..5790d3c2b9d 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -208,9 +208,6 @@ func serveSignIn(w *databricks.WorkspaceClient, refresh *refreshGuard, profileNa } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: req.Progress}) - if timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err); timedOut { - return false, urlShown, &proxy.SignInTimeout{URL: url, Wait: signInWaitTimeout, LastCheck: timeout.LastCheck} - } return alreadySignedIn, urlShown, refresh.callFailed(ctx, err) } } diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index c96fce0d8ef..5316f6f8eec 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -21,16 +21,21 @@ const ( SignInWaitTimeout = 5 * time.Minute ) -// SignInTimeoutError means the caller's credential did not become ACTIVE before the wait ended. LastCheck is the -// failure of the last poll, when that poll failed; a poll that reads the credential clears it. +// SignInTimeoutError means the caller's credential did not become ACTIVE before the wait ended. URL is the sign-in page +// the user finishes at, and Wait is how long the wait was. LastCheck is the failure of the last poll, when that poll +// failed; a poll that reads the credential clears it. type SignInTimeoutError struct { + URL string + Wait time.Duration 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 } @@ -119,7 +124,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str for { select { case <-waitCtx.Done(): - return false, true, waitEnded(ctx, lastCheck) + return false, true, waitEnded(ctx, url, opts.Timeout, lastCheck) case err := <-opened: opened = nil if err != nil { @@ -134,7 +139,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str state, err := CredentialState(waitCtx, w, service) if err != nil { if waitCtx.Err() != nil { - return false, true, waitEnded(ctx, lastCheck) + return false, true, waitEnded(ctx, url, opts.Timeout, lastCheck) } if !transientPollError(err) { return false, true, err @@ -166,13 +171,13 @@ func transientPollError(err error) bool { return ok } -// waitEnded returns why the sign-in wait ended: the caller's cancellation, or else the timeout, with the failure of the -// last poll. -func waitEnded(ctx context.Context, lastCheck error) error { +// waitEnded returns why the sign-in wait ended: the caller's cancellation, or else the timeout, with the page URL, the +// wait and the failure of the last poll. +func waitEnded(ctx context.Context, url string, wait time.Duration, lastCheck error) error { if err := ctx.Err(); err != nil { return err } - return &SignInTimeoutError{LastCheck: lastCheck} + return &SignInTimeoutError{URL: url, Wait: wait, LastCheck: lastCheck} } // SignOut deletes the caller's sign-in to a service. It reports false when the caller was not diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index dd26a87f6a9..47d66abf4ba 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -9,12 +9,12 @@ import ( "net/url" "strings" "sync" - "time" + + "github.com/databricks/cli/libs/aitools/mcp" ) const ( authenticateTool = "authenticate" - signInPath = "/mcp-service-login" codeSignInRequired = -32006 codeURLElicitation = -32042 @@ -22,22 +22,6 @@ const ( var listChangedNotification = []byte(`{"jsonrpc":"2.0","method":"notifications/tools/list_changed"}`) -// SignInTimeout means the sign-in page was shown and the credential did not become ACTIVE within Wait. URL is the page -// the user can finish at. LastCheck is the failure of the last poll, when that poll failed. -type SignInTimeout struct { - URL string - Wait time.Duration - LastCheck error -} - -func (e *SignInTimeout) Error() string { - return "sign-in did not complete before the timeout" -} - -func (e *SignInTimeout) Unwrap() error { - return e.LastCheck -} - // 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. @@ -51,7 +35,7 @@ type SignInRequest struct { // 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. It returns a *SignInTimeout when the wait ended first. +// cancelled. It returns a *mcp.SignInTimeoutError when the wait ended first. 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. @@ -233,7 +217,7 @@ func (h *signInHooks) isSignInURL(raw string) bool { return false } return u.Scheme == base.Scheme && strings.EqualFold(u.Host, base.Host) && - u.Path == signInPath && strings.EqualFold(u.Query().Get("name"), h.c.Service) + u.Path == mcp.SignInPath && strings.EqualFold(u.Query().Get("name"), h.c.Service) } // authenticateList is the one-tool list a challenged service answers with. @@ -290,7 +274,7 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R // The client cancelled the request or shutdown began. Nothing is answered for a cancelled request. return nil } - timeout, timedOut := errors.AsType[*SignInTimeout](err) + timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { case err == nil && alreadySignedIn: diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 20e35c1fbda..111ecb3bf39 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/databricks/cli/libs/aitools/mcp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -259,7 +260,7 @@ func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { srv, _ := challengedService(t) signIn := &signInStub{reply: func(call int) (bool, error) { if call == 1 { - return false, &SignInTimeout{URL: testURL, Wait: 5 * time.Minute} + return false, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute} } return false, nil }} @@ -329,15 +330,6 @@ func TestChallengeTurnsOnAfterSignIn(t *testing.T) { require.NoError(t, p.finish(t)) } -// TestSignInTimeoutKeepsLastCheck checks that the timeout exposes the failure of its last poll through the error chain. -func TestSignInTimeoutKeepsLastCheck(t *testing.T) { - lastCheck := errors.New("the last poll failed") - err := error(&SignInTimeout{URL: testURL, Wait: time.Minute, LastCheck: lastCheck}) - - assert.ErrorIs(t, err, lastCheck) - assert.NoError(t, errors.Unwrap(&SignInTimeout{URL: testURL})) -} - // 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) { @@ -345,7 +337,7 @@ func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { var calls atomic.Int32 signIn := func(ctx context.Context, _ SignInRequest) (bool, bool, error) { calls.Add(1) - return false, true, &SignInTimeout{URL: testURL, Wait: 5 * time.Minute} + return false, true, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute} } b := bridgeFor(srv.URL) b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}} @@ -415,7 +407,7 @@ func TestAuthenticateOpensPageAgainUnlessACancelledAttemptShowedIt(t *testing.T) }{ "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: &SignInTimeout{URL: testURL, Wait: time.Minute}}, wantOpen: true}, + "timed out": {first: attempt{urlShown: true, err: &mcp.SignInTimeoutError{URL: testURL, Wait: time.Minute}}, 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}, 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() } From b19f0973b170437e2dc3ab8b74cc71d52d5b90b6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:47:36 +0000 Subject: [PATCH 20/55] Find sign-in URLs in markdown and angle brackets, and read a null progress token as none A sign-in URL in a markdown link, between angle brackets or in bold is now recognized: the URL is found by its pattern, not by the words of the text. A progressToken of null asks for no progress, so the call gets no notifications. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 21 +++++++++++++++------ libs/aitools/mcp/proxy/challenge_test.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 47d66abf4ba..150804a8bc0 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "net/url" + "regexp" "strings" "sync" @@ -20,6 +21,10 @@ const ( 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 = regexp.MustCompile("https?://[^\\s<>()\\[\\]\"'*`]+") + var listChangedNotification = []byte(`{"jsonrpc":"2.0","method":"notifications/tools/list_changed"}`) // SignInRequest says how one sign-in runs. @@ -84,7 +89,7 @@ func (h *signInHooks) Local(ctx context.Context, request []byte, notify func([]b Params struct { Name string `json:"name"` Meta struct { - ProgressToken json.RawMessage `json:"progressToken"` + ProgressToken *json.RawMessage `json:"progressToken"` } `json:"_meta"` } `json:"params"` } @@ -97,7 +102,11 @@ func (h *signInHooks) Local(ctx context.Context, request []byte, notify func([]b if !challenged { return nil, false } - return h.authenticate(ctx, req.ID, req.Params.Meta.ProgressToken, notify), true + 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 @@ -193,12 +202,12 @@ func (h *signInHooks) urlElicitation(data json.RawMessage) bool { return true } -// textHasSignInURL reports whether one of the texts has a word that is a sign-in URL. Punctuation around the word, such as -// the brackets of a sentence or the period that ends it, is not part of the URL. +// 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 word := range strings.FieldsSeq(c.Text) { - if h.isSignInURL(strings.Trim(word, "([\"'.,;:)]")) { + for _, found := range urlPattern.FindAllString(c.Text, -1) { + if h.isSignInURL(strings.TrimRight(found, ".,:;")) { return true } } diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 111ecb3bf39..9ac62819816 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -500,6 +500,10 @@ func TestChallengeTextAroundSignInURL(t *testing.T) { {"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) @@ -612,3 +616,20 @@ func TestAuthenticateSendsNoProgressWithoutToken(t *testing.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") +} From 60f891cb798eb2e9dd52ffbf68b8c3cc52241d7c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:49:47 +0000 Subject: [PATCH 21/55] Authenticate the sign-in client through the refresh guard The sign-in client authenticated inside the SDK, so a token that could not be refreshed during a sign-in was found by a probe after each failed call, and each call site had to remember it. The client now has a credentials strategy that runs the refresh guard for every request it makes, as the bridge's requests are. serveProxy sets the bridge's Authenticate from the same guard. A DELETE of the sign-in is authenticated like any other call. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 128 +++++++++++++++++++++------------- cmd/aitools/mcp_serve_test.go | 102 +++++++++++++++++++++------ 2 files changed, 161 insertions(+), 69 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 5790d3c2b9d..f3e09c51993 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -26,6 +26,7 @@ import ( "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" @@ -120,36 +121,63 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { // 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} - w, err := databricks.NewWorkspaceClient((*databricks.Config)(cfg)) + w, err := guardedClient(cfg, refresh) if err != nil { return serveFailure(cmd, err, 1) } - challenge, err := serveChallenge(ctx, w, refresh, entry, profileName, f.service, pinnedFile, cmd.ErrOrStderr()) + challenge, err := serveChallenge(ctx, w, entry, profileName, f.service, pinnedFile, cmd.ErrOrStderr()) if err != nil { return serveFailure(cmd, err, serveStartStatus(err)) } 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, - Authenticate: refresh.authenticateRequest, - Profile: profileName, - Agent: serveAgent(f.agent, cmd.ErrOrStderr()), - Transport: serveTransport(cfg), - Challenge: challenge, + 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 +} + // 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. -// pinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). -func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, refresh *refreshGuard, entry *mcp.Entry, profileName, service, pinnedFile string, errOut io.Writer) (*proxy.Challenge, error) { +// pinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). w must be guardedClient's. +func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service, pinnedFile string, errOut io.Writer) (*proxy.Challenge, error) { signInRequired := entry != nil && entry.SignInRequired if entry == nil { - svc, err := lookupAtStart(ctx, w, refresh, profileName, pinnedFile, service) + svc, err := lookupAtStart(ctx, w, profileName, pinnedFile, service) if err != nil { return nil, err } @@ -164,18 +192,18 @@ func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, refresh Host: w.Config.Host, Label: mcp.ProviderLabel(service), LoginCommand: mcpLoginHint(service, profileName, pinnedFile), - SignIn: serveSignIn(w, refresh, profileName, service, pinnedFile, errOut), + SignIn: serveSignIn(w, profileName, service, pinnedFile, errOut), }, 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, refresh *refreshGuard, profileName, pinnedFile, service string) (mcp.Service, error) { +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, refresh.callFailed(ctx, err)) + err = describeRequestError(profileName, pinnedFile, err) return mcp.Service{}, fmt.Errorf("cannot read MCP service %s: %w", service, err) } return svc, nil @@ -183,20 +211,21 @@ func lookupAtStart(ctx context.Context, w *databricks.WorkspaceClient, refresh * // serveSignIn is the authenticate tool's sign-in, the one `login` runs. Progress goes to errOut, which is stderr because // stdout carries the protocol. The page opens only when req.Open is set, so the proxy decides how often the browser opens. -func serveSignIn(w *databricks.WorkspaceClient, refresh *refreshGuard, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { +// w must be guardedClient's. +func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { // 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 req.Restart { // The credential is deleted first, so that the sign-in below starts from no credential. if _, err := mcp.SignOut(ctx, w, service); err != nil { - return false, false, refresh.callFailed(ctx, err) + return false, false, err } } if workspaceID == "" { id, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return false, false, refresh.callFailed(ctx, err) + return false, false, err } workspaceID = id } @@ -208,13 +237,14 @@ func serveSignIn(w *databricks.WorkspaceClient, refresh *refreshGuard, profileNa } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: req.Progress}) - return alreadySignedIn, urlShown, refresh.callFailed(ctx, err) + return alreadySignedIn, urlShown, err } } -// serveProxy bridges opts to the service until stdin closes or ctx is done. opts.Authenticate must be refresh's, so that a -// refresh failure stops the proxy; see proxyResult. +// 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)) } @@ -286,11 +316,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 @@ -311,19 +341,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 } - if r.Context().Err() != nil || r.Method == http.MethodDelete { + 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() + } + 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 @@ -338,21 +383,6 @@ func (g *refreshGuard) authenticateRequest(r *http.Request) error { return authErr } -// callFailed returns the failure of a call that the sign-in client made. The sign-in client authenticates inside the SDK, -// where the guard does not see its requests, so after a failed call the guard authenticates once, as it does for a bridge -// request. A token that cannot be refreshed is then the failure that stops the proxy. Otherwise the call's failure is -// returned. -func (g *refreshGuard) callFailed(ctx context.Context, err error) error { - if err == nil || ctx.Err() != nil { - return err - } - probe := (&http.Request{Method: http.MethodGet, Header: http.Header{}}).WithContext(ctx) - if authErr := g.authenticateRequest(probe); authErr != nil { - return authErr - } - return err -} - // stoppedError is the error for a request that arrives after the refresh failure. It does not repeat the login hint. func (g *refreshGuard) stoppedError() error { return fmt.Errorf("the session is stopping after a token refresh failure for profile %s", g.profile) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 9f7d6b38f0f..3081a35fcee 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -777,7 +777,7 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - challenge, err := serveChallenge(t.Context(), w, passingGuard(), &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "", io.Discard) + challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "", io.Discard) require.NoError(t, err) require.NotNil(t, challenge) assert.Equal(t, "system.ai.github", challenge.Service) @@ -786,12 +786,12 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { 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(), w, passingGuard(), &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "", io.Discard) + challenge, err = serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "", io.Discard) require.NoError(t, err) assert.Nil(t, challenge) assert.Equal(t, int32(0), lookups.Load()) - challenge, err = serveChallenge(t.Context(), w, passingGuard(), nil, "logfood", "system.ai.github", "", io.Discard) + challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", 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") @@ -804,7 +804,7 @@ func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { require.NoError(t, err) const pinned = "/work/cfg/my file.cfg" - challenge, err := serveChallenge(t.Context(), w, passingGuard(), &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", pinned, io.Discard) + challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", 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) @@ -862,7 +862,7 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) require.NoError(t, err) - signIn := serveSignIn(w, passingGuard(), "logfood", "system.ai.github", "", io.Discard) + signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) alreadySignedIn, _, err := signIn(t.Context(), tc.req) require.NoError(t, err) @@ -874,11 +874,6 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { } } -// passingGuard returns a refresh guard whose tokens always refresh, for tests that do not exercise a refresh failure. -func passingGuard() *refreshGuard { - return &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: "logfood", cancel: func() {}} -} - // failingGuard returns a refresh guard whose token cannot be refreshed, and records whether the proxy was stopped. func failingGuard(stopped *bool) *refreshGuard { return &refreshGuard{ @@ -912,7 +907,7 @@ func TestServeChallengeLookupIsBounded(t *testing.T) { require.NoError(t, err) start := time.Now() - _, err = serveChallenge(t.Context(), w, passingGuard(), nil, "logfood", "system.ai.github", "", io.Discard) + _, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) require.Error(t, err) assert.Less(t, time.Since(start), 10*time.Second, "the lookup must not wait for the SDK's retry budget") @@ -925,7 +920,7 @@ func TestServeChallengeLookupAuthFailureExitsTwo(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - _, err = serveChallenge(t.Context(), w, passingGuard(), nil, "logfood", "system.ai.github", "", io.Discard) + _, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) require.Error(t, err) assert.Equal(t, serveBindStatus, serveStartStatus(err)) @@ -941,11 +936,10 @@ func TestServeStartStatusIsOneForOtherFailures(t *testing.T) { // stops the proxy with the login hint, as a refresh failure of a bridge request does. func TestServeChallengeLookupRefreshFailureStopsProxy(t *testing.T) { server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) - w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) - require.NoError(t, err) var stopped bool + w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - _, err = serveChallenge(t.Context(), w, failingGuard(&stopped), nil, "logfood", "system.ai.github", "", io.Discard) + _, err := serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) require.Error(t, err) assert.True(t, stopped, "the proxy is stopped") @@ -958,12 +952,11 @@ func TestServeChallengeLookupRefreshFailureStopsProxy(t *testing.T) { func TestServeSignInRefreshFailureStopsProxy(t *testing.T) { t.Setenv("BROWSER", "none") server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) - w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) - require.NoError(t, err) var stopped bool - signIn := serveSignIn(w, failingGuard(&stopped), "logfood", "system.ai.github", "", io.Discard) + w := guardedTestClient(t, server.URL, failingGuard(&stopped)) + signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) - _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true}) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true}) require.Error(t, err) assert.True(t, stopped, "the proxy is stopped") @@ -1000,9 +993,78 @@ func TestServeSignInReportsEachPoll(t *testing.T) { require.NoError(t, err) polls := 0 - signIn := serveSignIn(w, passingGuard(), "logfood", "system.ai.github", "", io.Discard) + signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() { polls++ }}) 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: "logfood", cancel: func() {}} + w := guardedTestClient(t, server.URL, guard) + + signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() {}}) + + require.NoError(t, err) + assert.Equal(t, reads.Load(), authenticated.Load(), "every request of the sign-in is authenticated by the guard") +} + +// TestServeSignInRestartDeleteRefreshFailureStopsProxy checks that the DELETE of a restart is authenticated by the guard +// too, so a token that cannot be refreshed for it stops the proxy with the login hint. +func TestServeSignInRestartDeleteRefreshFailureStopsProxy(t *testing.T) { + t.Setenv("BROWSER", "none") + server := serviceServer(t, http.StatusOK, `{}`) + var stopped bool + guard := &refreshGuard{ + authenticate: func(r *http.Request) error { + if r.Method == http.MethodDelete { + return errors.New(refreshTokenExpired) + } + return nil + }, + profile: "logfood", + cancel: func() { stopped = true }, + } + w := guardedTestClient(t, server.URL, guard) + signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) + + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true}) + + require.Error(t, err) + assert.True(t, stopped, "the proxy is stopped") + assert.Contains(t, err.Error(), "databricks auth login --profile logfood, then restart the MCP server in your agent") +} + +// 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 +} From 66783181da710af5c4bbe0035ff9a0b37fcabb64 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:52:37 +0000 Subject: [PATCH 22/55] Serialize sign-in progress with the bridge's stderr and compact hook output The sign-in's progress lines were written to the command's stderr outside the bridge's lock, so they could race with the bridge's own diagnostics on a stderr that is not a file. Run now wraps stderr in a serialized writer that both share, and the sign-in writes its progress through the request's stderr. A message a hook writes itself, its progress or its answer, is compacted to one line as an upstream answer is. A request the hooks do not handle writes none of the hook's messages. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 16 ++-- cmd/aitools/mcp_serve_test.go | 33 +++---- libs/aitools/mcp/proxy/bridge.go | 34 ++++++- libs/aitools/mcp/proxy/bridge_test.go | 117 ++++++++++++++++++++++- libs/aitools/mcp/proxy/challenge.go | 7 +- libs/aitools/mcp/proxy/challenge_test.go | 6 +- libs/aitools/mcp/proxy/run.go | 20 +++- 7 files changed, 193 insertions(+), 40 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index f3e09c51993..8f3a95dc5b1 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -125,7 +125,7 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { if err != nil { return serveFailure(cmd, err, 1) } - challenge, err := serveChallenge(ctx, w, entry, profileName, f.service, pinnedFile, cmd.ErrOrStderr()) + challenge, err := serveChallenge(ctx, w, entry, profileName, f.service, pinnedFile) if err != nil { return serveFailure(cmd, err, serveStartStatus(err)) } @@ -174,7 +174,7 @@ func (g guardCredentials) Configure(context.Context, *config.Config) (credential // 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. // pinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). w must be guardedClient's. -func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service, pinnedFile string, errOut io.Writer) (*proxy.Challenge, error) { +func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service, pinnedFile string) (*proxy.Challenge, error) { signInRequired := entry != nil && entry.SignInRequired if entry == nil { svc, err := lookupAtStart(ctx, w, profileName, pinnedFile, service) @@ -192,7 +192,7 @@ func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *m Host: w.Config.Host, Label: mcp.ProviderLabel(service), LoginCommand: mcpLoginHint(service, profileName, pinnedFile), - SignIn: serveSignIn(w, profileName, service, pinnedFile, errOut), + SignIn: serveSignIn(w, profileName, service, pinnedFile), }, nil } @@ -209,10 +209,10 @@ func lookupAtStart(ctx context.Context, w *databricks.WorkspaceClient, profileNa return svc, nil } -// serveSignIn is the authenticate tool's sign-in, the one `login` runs. Progress goes to errOut, which is stderr because -// 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(w *databricks.WorkspaceClient, profileName, service, pinnedFile string, errOut io.Writer) proxy.SignInFunc { +// 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(w *databricks.WorkspaceClient, profileName, service, pinnedFile string) proxy.SignInFunc { // 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) { @@ -236,7 +236,7 @@ func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile openPage = browser.OpenOutputToStderr } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} - _, alreadySignedIn, urlShown, err := signIn(ctx, errOut, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: req.Progress}) + _, alreadySignedIn, urlShown, err := signIn(ctx, req.Stderr, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: req.Progress}) return alreadySignedIn, urlShown, err } } diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 3081a35fcee..68bebc93176 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -777,7 +777,7 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "", io.Discard) + challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "") require.NoError(t, err) require.NotNil(t, challenge) assert.Equal(t, "system.ai.github", challenge.Service) @@ -786,12 +786,12 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { 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(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "", io.Discard) + challenge, err = serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "") require.NoError(t, err) assert.Nil(t, challenge) assert.Equal(t, int32(0), lookups.Load()) - challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) + challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "") require.NoError(t, err) assert.NotNil(t, challenge) assert.Equal(t, int32(1), lookups.Load(), "without a record the service is read once") @@ -804,7 +804,7 @@ func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { require.NoError(t, err) const pinned = "/work/cfg/my file.cfg" - challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", pinned, io.Discard) + challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", pinned) 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) @@ -862,7 +862,8 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) require.NoError(t, err) - signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) + signIn := serveSignIn(w, "logfood", "system.ai.github", "") + tc.req.Stderr = io.Discard alreadySignedIn, _, err := signIn(t.Context(), tc.req) require.NoError(t, err) @@ -907,7 +908,7 @@ func TestServeChallengeLookupIsBounded(t *testing.T) { require.NoError(t, err) start := time.Now() - _, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) + _, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "") require.Error(t, err) assert.Less(t, time.Since(start), 10*time.Second, "the lookup must not wait for the SDK's retry budget") @@ -920,7 +921,7 @@ func TestServeChallengeLookupAuthFailureExitsTwo(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - _, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) + _, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "") require.Error(t, err) assert.Equal(t, serveBindStatus, serveStartStatus(err)) @@ -939,7 +940,7 @@ func TestServeChallengeLookupRefreshFailureStopsProxy(t *testing.T) { var stopped bool w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - _, err := serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "", io.Discard) + _, err := serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "") require.Error(t, err) assert.True(t, stopped, "the proxy is stopped") @@ -954,9 +955,9 @@ func TestServeSignInRefreshFailureStopsProxy(t *testing.T) { server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) var stopped bool w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) + signIn := serveSignIn(w, "logfood", "system.ai.github", "") - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true}) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Stderr: io.Discard}) require.Error(t, err) assert.True(t, stopped, "the proxy is stopped") @@ -993,8 +994,8 @@ func TestServeSignInReportsEachPoll(t *testing.T) { require.NoError(t, err) polls := 0 - signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) - _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() { polls++ }}) + signIn := serveSignIn(w, "logfood", "system.ai.github", "") + _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() { polls++ }, Stderr: io.Discard}) require.NoError(t, err) assert.Equal(t, 2, polls) @@ -1028,8 +1029,8 @@ func TestServeSignInAuthenticatesEveryRequest(t *testing.T) { guard := &refreshGuard{authenticate: func(*http.Request) error { authenticated.Add(1); return nil }, profile: "logfood", cancel: func() {}} w := guardedTestClient(t, server.URL, guard) - signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() {}}) + signIn := serveSignIn(w, "logfood", "system.ai.github", "") + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() {}, 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") @@ -1052,9 +1053,9 @@ func TestServeSignInRestartDeleteRefreshFailureStopsProxy(t *testing.T) { cancel: func() { stopped = true }, } w := guardedTestClient(t, server.URL, guard) - signIn := serveSignIn(w, "logfood", "system.ai.github", "", io.Discard) + signIn := serveSignIn(w, "logfood", "system.ai.github", "") - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true}) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Stderr: io.Discard}) require.Error(t, err) assert.True(t, stopped, "the proxy is stopped") diff --git a/libs/aitools/mcp/proxy/bridge.go b/libs/aitools/mcp/proxy/bridge.go index accfcd60729..6a03cf04099 100644 --- a/libs/aitools/mcp/proxy/bridge.go +++ b/libs/aitools/mcp/proxy/bridge.go @@ -272,13 +272,31 @@ 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.reply) - if err := c.writeLines(msgs); err != nil { - c.logf("stdout: %v", err) + 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 @@ -855,9 +873,15 @@ 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) + for _, msg := range msgs { + line, err := oneLine(msg) if err != nil { return nil, err } diff --git a/libs/aitools/mcp/proxy/bridge_test.go b/libs/aitools/mcp/proxy/bridge_test.go index 1a8980c2947..d490066e8bd 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" ) @@ -2199,14 +2200,14 @@ func TestRunClosesStaleResponseBeforeReplay(t *testing.T) { assert.Nil(t, parseReply(t, out[1]).Error) } -// localHooks answers a request whose body names "local" with msgs, and forwards the others. -type localHooks struct{ msgs [][]byte } +// answerHooks answers a request whose body names "local" with msgs, and forwards the others. +type answerHooks struct{ msgs [][]byte } -func (localHooks) Result(_ context.Context, _ []byte, _ int, msg []byte) ([][]byte, error) { +func (answerHooks) Result(_ context.Context, _ []byte, _ int, msg []byte) ([][]byte, error) { return [][]byte{msg}, nil } -func (h localHooks) Local(_ context.Context, request []byte, _ func([]byte)) ([][]byte, bool) { +func (h answerHooks) Local(_ context.Context, request []byte, _ func([]byte)) ([][]byte, bool) { if !bytes.Contains(request, []byte("local")) { return nil, false } @@ -2217,7 +2218,7 @@ func (h localHooks) Local(_ context.Context, request []byte, _ func([]byte)) ([] // 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: localHooks{msgs: [][]byte{[]byte(`{"n":1}`), []byte(`{"n":2}`)}}}} + 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()) @@ -2234,3 +2235,109 @@ func TestAnswerLocallyWithoutHooksForwardsEverything(t *testing.T) { 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.Split(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 index 150804a8bc0..23c6c47c31e 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "net/http" "net/url" "regexp" @@ -36,6 +37,8 @@ type SignInRequest struct { Restart bool // Progress is called on each poll of the wait. It is never nil. Progress func() + // 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 @@ -62,6 +65,8 @@ type Challenge struct { // 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 // mu serializes authenticate calls, which may run a sign-in for minutes. pageCancelled is read and written under it. mu sync.Mutex @@ -270,7 +275,7 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R notify(progressNotification(progressToken, polls, h.c.Label)) } } - alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Restart: restart, Progress: progress}) + alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Restart: restart, Progress: progress, Stderr: h.stderr}) h.pageCancelled = urlShown && err != nil && ctx.Err() != nil h.stateMu.Lock() h.alreadySignedIn = err == nil && alreadySignedIn diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 9ac62819816..891aa55d715 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -72,7 +72,7 @@ const loginCommand = `DATABRICKS_CONFIG_FILE='/work/cfg/my file.cfg' databricks // 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 = &signInHooks{c: &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: "databricks aitools mcp login system.ai.github --profile logfood", SignIn: signIn}} + b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: "databricks aitools mcp login system.ai.github --profile logfood", SignIn: signIn}, stderr: io.Discard} return b } @@ -340,7 +340,7 @@ func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { return false, true, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute} } b := bridgeFor(srv.URL) - b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}} + b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}, stderr: io.Discard} p := startPiped(t, t.Context(), b) p.send(t, `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{}}}`) @@ -484,7 +484,7 @@ func TestChallengeMatchesServiceNameWithoutCase(t *testing.T) { `{"mode":"url","url":"` + testURL + `"}]}}}` srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, body)) b := bridgeFor(srv.URL) - b.Hooks = &signInHooks{c: &Challenge{Service: "SYSTEM.AI.GITHUB", Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: loginCommand}} + b.Hooks = &signInHooks{c: &Challenge{Service: "SYSTEM.AI.GITHUB", Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: loginCommand}, stderr: io.Discard} out, _ := runAll(t, b, `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`+"\n") require.Len(t, out, 1) diff --git a/libs/aitools/mcp/proxy/run.go b/libs/aitools/mcp/proxy/run.go index 54c97fc6fd3..07c3023d41b 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" @@ -56,10 +57,12 @@ 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, @@ -69,7 +72,20 @@ func Run(ctx context.Context, opts Options) error { 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 = &signInHooks{c: opts.Challenge} + b.Hooks = &signInHooks{c: opts.Challenge, stderr: 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) +} From 3a7082b89f88e8fa157ed36b1ac0b5a2d4ba79af Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:54:47 +0000 Subject: [PATCH 23/55] Keep the authenticate tool's recovery state in step with the service Three fixes to when authenticate restarts a sign-in from no credential: - An "Already signed in" answer ends when the service answers successfully, so a challenge long after it does not delete a credential that is valid. - A restart whose page was shown is not asked for again when the sign-in is then cancelled: the credential is already deleted, and the page the user may still finish is kept. A restart that failed before its page was shown is asked for again. - After a restart's delete, the sign-in waits, up to a bound, until the credential no longer reads as ACTIVE. A delete can take a moment to show, and the sign-in used to answer that the caller was already signed in, with no page. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 31 +++++++ cmd/aitools/mcp_serve_test.go | 94 ++++++++++++++++++++- libs/aitools/mcp/proxy/challenge.go | 39 ++++++--- libs/aitools/mcp/proxy/challenge_test.go | 102 +++++++++++++++++++++++ 4 files changed, 251 insertions(+), 15 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 8f3a95dc5b1..ef63d66f84e 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -28,6 +28,7 @@ import ( "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/config/credentials" "github.com/databricks/databricks-sdk-go/httpclient" + "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" "golang.org/x/oauth2" ) @@ -209,6 +210,33 @@ func lookupAtStart(ctx context.Context, w *databricks.WorkspaceClient, profileNa return svc, nil } +// restartWaitTimeout bounds the wait, after a restart deletes the credential, for the credential to stop reading as ACTIVE. +// Tests shorten it. +var restartWaitTimeout = 30 * time.Second + +// waitDeleted waits, up to restartWaitTimeout, until the caller's credential no longer reads as ACTIVE. A delete can take +// a moment to show, and a sign-in that read ACTIVE then would answer that the caller is already signed in, with no page. +func waitDeleted(ctx context.Context, w *databricks.WorkspaceClient, service string) error { + deadline := time.Now().Add(restartWaitTimeout) + for { + state, err := mcp.CredentialState(ctx, w, service) + if err != nil { + return err + } + if state != catalog.ProvisioningInfoStateActive { + return nil + } + if time.Now().After(deadline) { + return fmt.Errorf("the deleted sign-in to %s still reads as signed in", service) + } + select { + case <-ctx.Done(): + return ctx.Err() + case <-time.After(signInPollInterval): + } + } +} + // 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. @@ -221,6 +249,9 @@ func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile if _, err := mcp.SignOut(ctx, w, service); err != nil { return false, false, err } + if err := waitDeleted(ctx, w, service); err != nil { + return false, false, err + } } if workspaceID == "" { id, err := auth.ResolveWorkspaceID(ctx, w) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 68bebc93176..85a54e167b5 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -848,9 +848,9 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { case !deleted: _, _ = io.WriteString(rw, active) default: - // The caller has not signed in yet after the delete, and then does. + // The caller has not signed in yet after the delete, which the wait and the sign-in both read, and then does. gets++ - if gets == 1 { + if gets <= 2 { rw.WriteHeader(http.StatusNotFound) _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) return @@ -1069,3 +1069,93 @@ func guardedTestClient(t *testing.T, host string, guard *refreshGuard) *databric require.NoError(t, err) return w } + +// TestServeSignInRestartWaitsForDeletedCredential checks that after a restart's delete the sign-in does not read the +// credential as ACTIVE. A delete can take a moment to show, so the first reads may still say ACTIVE; the sign-in waits +// until they do not, and then opens the page rather than answering that the caller is already signed in. +func TestServeSignInRestartWaitsForDeletedCredential(t *testing.T) { + prevPoll, prevWait := signInPollInterval, signInWaitTimeout + prevRestart := restartWaitTimeout + t.Cleanup(func() { + signInPollInterval, signInWaitTimeout, restartWaitTimeout = prevPoll, prevWait, prevRestart + }) + signInPollInterval, signInWaitTimeout, restartWaitTimeout = time.Millisecond, 10*time.Second, 10*time.Second + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` + var mu sync.Mutex + deleted := false + var reads int + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + if r.URL.Path != credentialPath { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + if r.Method == http.MethodDelete { + deleted = true + _, _ = io.WriteString(rw, `{}`) + return + } + // After the delete, the first two reads still say ACTIVE, two read NOT_FOUND, and the caller then signs in. + reads++ + switch { + case !deleted: + _, _ = io.WriteString(rw, active) + case reads <= 2: + _, _ = io.WriteString(rw, active) + case reads <= 4: + rw.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) + default: + _, _ = io.WriteString(rw, active) + } + })) + t.Cleanup(server.Close) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: "logfood", cancel: func() {}}) + + polls := 0 + signIn := serveSignIn(w, "logfood", "system.ai.github", "") + alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func() { polls++ }, Stderr: io.Discard}) + + require.NoError(t, err) + assert.False(t, alreadySignedIn, "a deleted credential is not reported as signed in") + assert.True(t, urlShown) + assert.Equal(t, 1, polls, "the sign-in waits for the page's sign-in, one poll") +} + +// TestServeSignInRestartGivesUpOnStaleCredential checks that a credential that still reads ACTIVE after a restart's delete +// ends the sign-in with an error, and that no page is shown for it. +func TestServeSignInRestartGivesUpOnStaleCredential(t *testing.T) { + prevPoll, prevRestart := signInPollInterval, restartWaitTimeout + t.Cleanup(func() { signInPollInterval, restartWaitTimeout = prevPoll, prevRestart }) + signInPollInterval, restartWaitTimeout = time.Millisecond, 20*time.Millisecond + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` + 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 r.Method == http.MethodDelete { + _, _ = io.WriteString(rw, `{}`) + return + } + _, _ = io.WriteString(rw, active) + })) + t.Cleanup(server.Close) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: "logfood", cancel: func() {}}) + + signIn := serveSignIn(w, "logfood", "system.ai.github", "") + alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Stderr: io.Discard}) + + require.Error(t, err) + assert.False(t, alreadySignedIn) + assert.False(t, urlShown, "no page is shown while the credential still reads as signed in") +} diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 23c6c47c31e..c5b51d8c2f8 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -121,17 +121,17 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess if err != nil { return nil, err } - switch req.Method { - case "tools/list": - if h.isChallenge(status, message) { - return [][]byte{h.authenticateList(req.ID)}, nil - } - case methodToolsCall: - if h.isChallenge(status, message) { - return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil - } + if req.Method != "tools/list" && req.Method != methodToolsCall { + return [][]byte{message}, nil + } + if !h.isChallenge(status, message) { + h.succeeded(status) + return [][]byte{message}, nil } - return [][]byte{message}, nil + if req.Method == "tools/list" { + return [][]byte{h.authenticateList(req.ID)}, nil + } + return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil } // isChallenge reports whether an upstream answer is a challenge, and records that the session has been challenged. @@ -149,6 +149,18 @@ func (h *signInHooks) isChallenge(status int, message []byte) bool { return true } +// succeeded records an upstream answer that is not a challenge. An answer with a success status means the service accepts +// the caller's credential, so the "Already signed in" state ends: a challenge after this one is not a challenge to that +// answer. +func (h *signInHooks) succeeded(status int) { + if status/100 != 2 { + return + } + h.stateMu.Lock() + defer h.stateMu.Unlock() + h.alreadySignedIn = false +} + // upstreamReply is the part of an upstream message that challenge detection reads. type upstreamReply struct { Error *struct { @@ -279,9 +291,10 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R h.pageCancelled = urlShown && err != nil && ctx.Err() != nil h.stateMu.Lock() h.alreadySignedIn = err == nil && alreadySignedIn - if ctx.Err() != nil { - // A restart that was cancelled may not have deleted the credential, so the next sign-in restarts again. - h.restart = h.restart || restart + if restart && !urlShown && err != nil { + // A restart that failed before its page was shown may not have deleted the credential, so the next sign-in restarts + // again. Once the page was shown the credential is deleted, and a sign-in the user may still finish is kept. + h.restart = true } h.stateMu.Unlock() if ctx.Err() != nil { diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 891aa55d715..7a88ba709b6 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -633,3 +633,105 @@ func TestAuthenticateSendsNoProgressForNullToken(t *testing.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. Unlike the +// fixtures above it answers with the id of each request, so later lists 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) + if m.Method != "tools/list" { + w.WriteHeader(http.StatusAccepted) + return + } + w.Header().Set("Content-Type", "application/json") + if challenged.Load() { + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"error":{"code":-32006,"message":"rejected","data":null}}`, m.ID) + return + } + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}}]}}`, m.ID) + } +} + +// TestChallengeAfterSuccessDoesNotRestartSignIn checks that a challenge long after an "Already signed in" answer does not +// start the next sign-in from no credential. A successful answer from the service ends that state, so only a challenge +// that follows the answer with no success in between does. +func TestChallengeAfterSuccessDoesNotRestartSignIn(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)) + p.waitLines(t, 6) + require.NoError(t, p.finish(t)) + + reqs := signIn.requests() + require.Len(t, reqs, 2) + assert.False(t, reqs[1].Restart, "a challenge after a success is not a challenge to the credential that was reported signed in") +} + +// TestRestartIsNotRepeatedAfterItsPageWasShown checks the restart that a challenge after "Already signed in" asks for. +// Once its page was shown the credential was deleted, and a sign-in cancelled after that does not delete again, since +// the user may finish the page that is still open. A restart whose page was never shown is asked for again. +func TestRestartIsNotRepeatedAfterItsPageWasShown(t *testing.T) { + for name, urlShown := range map[string]bool{"page shown": true, "page not shown": false} { + t.Run(name, func(t *testing.T) { + var challenged atomic.Bool + challenged.Store(true) + srv, _ := newFakeEndpoint(t, listAnswer(&challenged)) + entered, returned := make(chan struct{}), make(chan struct{}) + var mu sync.Mutex + var reqs []SignInRequest + signIn := func(ctx context.Context, req SignInRequest) (bool, bool, error) { + mu.Lock() + reqs = append(reqs, req) + n := len(reqs) + mu.Unlock() + switch n { + case 1: + return true, false, nil + case 2: + close(entered) + <-ctx.Done() + close(returned) + return false, urlShown, ctx.Err() + default: + return false, true, nil + } + } + p := startPiped(t, t.Context(), challengedBridge(srv.URL, 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)) + waitClosed(t, entered, "the restart to start") + p.send(t, `{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":7}}`) + waitClosed(t, returned, "the restart to return") + p.send(t, fmt.Sprintf(authenticate, 8)) + p.waitLines(t, 5) + require.NoError(t, p.finish(t)) + + mu.Lock() + defer mu.Unlock() + require.Len(t, reqs, 3) + assert.True(t, reqs[1].Restart, "the challenge after the answer asks for a restart") + assert.Equal(t, !urlShown, reqs[2].Restart) + }) + } +} From 1ac0b765a1b01ec709de7340ff0097ba2fbd007f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:55:26 +0000 Subject: [PATCH 24/55] Forward authenticate once the service's list passes again The proxy answered every tools/call named authenticate for the rest of the session after one challenge. A service tool of that name was then unreachable after sign-in. The proxy's tool is now answered only while it is on the agent's list: a challenge puts it there, and a tools/list that passes through unchallenged takes it off. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 20 ++++++---- libs/aitools/mcp/proxy/challenge_test.go | 47 +++++++++++++++++++----- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index c5b51d8c2f8..f25c4204c33 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -77,8 +77,9 @@ type signInHooks struct { // stateMu guards the three fields below. It is held only briefly, so an answer to a request is not held up by a // sign-in. stateMu sync.Mutex - // sessionChallenged means a request of this session was challenged. The authenticate tool is the proxy's only after that. - sessionChallenged bool + // exposed means the agent's list shows the authenticate tool: a challenge sets it, and a tools/list that passes through + // unchallenged clears it. The tool is the proxy's only while it is exposed. + exposed bool // alreadySignedIn means the last authenticate answered "Already signed in". alreadySignedIn bool // restart means the next sign-in starts from no credential. A challenge that follows an "Already signed in" answer @@ -102,9 +103,9 @@ func (h *signInHooks) Local(ctx context.Context, request []byte, notify func([]b return nil, false } h.stateMu.Lock() - challenged := h.sessionChallenged + exposed := h.exposed h.stateMu.Unlock() - if !challenged { + if !exposed { return nil, false } var progressToken json.RawMessage @@ -125,7 +126,7 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess return [][]byte{message}, nil } if !h.isChallenge(status, message) { - h.succeeded(status) + h.succeeded(req.Method, status) return [][]byte{message}, nil } if req.Method == "tools/list" { @@ -141,7 +142,7 @@ func (h *signInHooks) isChallenge(status int, message []byte) bool { } h.stateMu.Lock() defer h.stateMu.Unlock() - h.sessionChallenged = true + h.exposed = true if h.alreadySignedIn { h.restart = true h.alreadySignedIn = false @@ -151,14 +152,17 @@ func (h *signInHooks) isChallenge(status int, message []byte) bool { // succeeded records an upstream answer that is not a challenge. An answer with a success status means the service accepts // the caller's credential, so the "Already signed in" state ends: a challenge after this one is not a challenge to that -// answer. -func (h *signInHooks) succeeded(status int) { +// answer. A tools/list answer also takes the authenticate tool off the list, since the list is the service's again. +func (h *signInHooks) succeeded(method string, status int) { if status/100 != 2 { return } h.stateMu.Lock() defer h.stateMu.Unlock() h.alreadySignedIn = false + if method == "tools/list" { + h.exposed = false + } } // upstreamReply is the part of an upstream message that challenge detection reads. diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 7a88ba709b6..464e09641bb 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -634,22 +634,24 @@ func TestAuthenticateSendsNoProgressForNullToken(t *testing.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. Unlike the -// fixtures above it answers with the id of each request, so later lists match their answers. +// 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) - if m.Method != "tools/list" { - w.WriteHeader(http.StatusAccepted) - return - } w.Header().Set("Content-Type", "application/json") - if challenged.Load() { + 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) - return + 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) } - fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"echo","inputSchema":{"type":"object"}}]}}`, m.ID) } } @@ -735,3 +737,30 @@ func TestRestartIsNotRepeatedAfterItsPageWasShown(t *testing.T) { }) } } + +// 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]) +} From f76f6789891774ab6fc3f48c81eacb47b4446068 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:57:11 +0000 Subject: [PATCH 25/55] Let a queued authenticate call be cancelled and report progress Concurrent authenticate calls waited on a mutex that ignored the request's context, so a cancelled call stayed queued and then ran its sign-in, and a queued call sent no progress for as long as the sign-in ahead of it took. The calls now take a turn under their own context and report progress every queueProgressInterval while they wait. A call cancelled before its turn touches no state, so it does not clear the record that the last page was cancelled. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 72 ++++++++++++---- libs/aitools/mcp/proxy/challenge_test.go | 102 ++++++++++++++++++++++- libs/aitools/mcp/proxy/run.go | 2 +- 3 files changed, 156 insertions(+), 20 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index f25c4204c33..a68ad1ee965 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -11,6 +11,7 @@ import ( "regexp" "strings" "sync" + "time" "github.com/databricks/cli/libs/aitools/mcp" ) @@ -26,6 +27,9 @@ const ( // 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 = 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":"2.0","method":"notifications/tools/list_changed"}`) // SignInRequest says how one sign-in runs. @@ -68,8 +72,9 @@ type signInHooks struct { // stderr is the bridge's stderr, which SignIn writes its progress to. stderr io.Writer - // mu serializes authenticate calls, which may run a sign-in for minutes. pageCancelled is read and written under it. - mu sync.Mutex + // 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 @@ -87,6 +92,11 @@ type signInHooks struct { restart 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) { @@ -271,27 +281,29 @@ func (h *signInHooks) signInRequired(id json.RawMessage) []byte { return toolResult(id, text, true) } -// authenticate runs the sign-in and answers the tool call. Calls are serialized, so a second call waits for the first. -// The page opens unless the last attempt was cancelled while its page was showing (see pageCancelled). With a -// progressToken, each poll of the wait is reported to the client through notify. +// authenticate runs the sign-in and answers the tool call. Calls are serialized, so a second call waits for the first. The +// page opens unless the last attempt was cancelled while its page was showing (see pageCancelled). With a progressToken, +// each poll of the wait, and each poll while the call is queued, is reported to the client through notify. func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.RawMessage, notify func([]byte)) [][]byte { - h.mu.Lock() - defer h.mu.Unlock() + var polls int + report := func() { + if len(progressToken) == 0 { + return + } + polls++ + notify(progressNotification(progressToken, polls, h.c.Label)) + } + if !h.acquire(ctx, report) { + return nil + } + defer h.release() h.stateMu.Lock() restart := h.restart h.restart = false h.stateMu.Unlock() - progress := func() {} - if len(progressToken) > 0 { - polls := 0 - progress = func() { - polls++ - notify(progressNotification(progressToken, polls, h.c.Label)) - } - } - alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Restart: restart, Progress: progress, Stderr: h.stderr}) + alreadySignedIn, urlShown, err := h.c.SignIn(ctx, SignInRequest{Open: !h.pageCancelled, Restart: restart, Progress: report, Stderr: h.stderr}) h.pageCancelled = urlShown && err != nil && ctx.Err() != nil h.stateMu.Lock() h.alreadySignedIn = err == nil && alreadySignedIn @@ -330,6 +342,34 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R } } +// 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()) 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. func progressNotification(token json.RawMessage, n int, label string) []byte { diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 464e09641bb..17b7785f841 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -72,7 +72,7 @@ const loginCommand = `DATABRICKS_CONFIG_FILE='/work/cfg/my file.cfg' databricks // 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 = &signInHooks{c: &Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: "databricks aitools mcp login system.ai.github --profile logfood", SignIn: signIn}, stderr: io.Discard} + b.Hooks = newSignInHooks(&Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: "databricks aitools mcp login system.ai.github --profile logfood", SignIn: signIn}, io.Discard) return b } @@ -340,7 +340,7 @@ func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { return false, true, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute} } b := bridgeFor(srv.URL) - b.Hooks = &signInHooks{c: &Challenge{Service: testService, Profile: "my profile", Host: testHost, Label: "GitHub", LoginCommand: loginCommand, SignIn: signIn}, stderr: io.Discard} + 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":{}}}`) @@ -484,7 +484,7 @@ func TestChallengeMatchesServiceNameWithoutCase(t *testing.T) { `{"mode":"url","url":"` + testURL + `"}]}}}` srv, _ := newFakeEndpoint(t, answerWith(http.StatusOK, body)) b := bridgeFor(srv.URL) - b.Hooks = &signInHooks{c: &Challenge{Service: "SYSTEM.AI.GITHUB", Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: loginCommand}, stderr: io.Discard} + 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) @@ -764,3 +764,99 @@ func TestAuthenticateIsForwardedOnceTheListPasses(t *testing.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") +} diff --git a/libs/aitools/mcp/proxy/run.go b/libs/aitools/mcp/proxy/run.go index 07c3023d41b..93bc86eecea 100644 --- a/libs/aitools/mcp/proxy/run.go +++ b/libs/aitools/mcp/proxy/run.go @@ -72,7 +72,7 @@ func Run(ctx context.Context, opts Options) error { 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 = &signInHooks{c: opts.Challenge, stderr: stderr} + b.Hooks = newSignInHooks(opts.Challenge, stderr) } return b.Run(ctx) } From 46c767ccc61296278d0f2272612c6e6fb4035194 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 06:59:54 +0000 Subject: [PATCH 26/55] Name the last failed check and the sign-in page on a timed-out sign-in A timeout now says why the last poll failed, as mcp login does. A progress notification names the sign-in page when the attempt did not open it, so that a retry whose page was not reopened still tells the user where to finish. The page is given to Progress by the sign-in, which knows it. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 7 ++- cmd/aitools/mcp_serve_test.go | 59 ++++++++++++++++++++++-- libs/aitools/mcp/proxy/challenge.go | 31 ++++++++----- libs/aitools/mcp/proxy/challenge_test.go | 56 ++++++++++++++++++++-- 4 files changed, 133 insertions(+), 20 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index ef63d66f84e..8512ff6f755 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -266,8 +266,13 @@ func serveSignIn(w *databricks.WorkspaceClient, profileName, service, pinnedFile if req.Open { openPage = browser.OpenOutputToStderr } + // A poll names the page when this attempt did not open it, so that the client can show where the user finishes. + pageURL := url + if req.Open { + pageURL = "" + } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} - _, alreadySignedIn, urlShown, err := signIn(ctx, req.Stderr, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: req.Progress}) + _, alreadySignedIn, urlShown, err := signIn(ctx, req.Stderr, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: func() { req.Progress(pageURL) }}) return alreadySignedIn, urlShown, err } } diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 85a54e167b5..1f27e5c8d69 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -863,7 +863,7 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { require.NoError(t, err) signIn := serveSignIn(w, "logfood", "system.ai.github", "") - tc.req.Stderr = io.Discard + tc.req.Stderr, tc.req.Progress = io.Discard, func(string) {} alreadySignedIn, _, err := signIn(t.Context(), tc.req) require.NoError(t, err) @@ -995,7 +995,7 @@ func TestServeSignInReportsEachPoll(t *testing.T) { polls := 0 signIn := serveSignIn(w, "logfood", "system.ai.github", "") - _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() { polls++ }, Stderr: io.Discard}) + _, _, err = signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func(string) { polls++ }, Stderr: io.Discard}) require.NoError(t, err) assert.Equal(t, 2, polls) @@ -1030,7 +1030,7 @@ func TestServeSignInAuthenticatesEveryRequest(t *testing.T) { w := guardedTestClient(t, server.URL, guard) signIn := serveSignIn(w, "logfood", "system.ai.github", "") - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Progress: func() {}, Stderr: io.Discard}) + _, _, 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") @@ -1119,7 +1119,7 @@ func TestServeSignInRestartWaitsForDeletedCredential(t *testing.T) { polls := 0 signIn := serveSignIn(w, "logfood", "system.ai.github", "") - alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func() { polls++ }, Stderr: io.Discard}) + alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) { polls++ }, Stderr: io.Discard}) require.NoError(t, err) assert.False(t, alreadySignedIn, "a deleted credential is not reported as signed in") @@ -1159,3 +1159,54 @@ func TestServeSignInRestartGivesUpOnStaleCredential(t *testing.T) { assert.False(t, alreadySignedIn) assert.False(t, urlShown, "no page is shown while the credential still reads as signed in") } + +// TestServeSignInProgressNamesPageWhenNotOpened checks that the progress of a sign-in names its page when this attempt +// does not open it, and names none when it does. +func TestServeSignInProgressNamesPageWhenNotOpened(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: "logfood", cancel: func() {}}) + var pageURLs []string + signIn := serveSignIn(w, "logfood", "system.ai.github", "") + + _, _, 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 := "" + if !tc.open { + want = mcp.SignInURL(server.URL, "system.ai.github", "1") + } + assert.Equal(t, []string{want, want}, pageURLs) + }) + } +} diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index a68ad1ee965..21e8e9271f1 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -39,8 +39,9 @@ type SignInRequest struct { // Restart deletes the caller's sign-in before the sign-in runs, so that it starts from no credential. The proxy asks // for it when the service challenged the session again after an "Already signed in" answer. Restart bool - // Progress is called on each poll of the wait. It is never nil. - Progress func() + // Progress is called on each poll of the wait. Its pageURL is the sign-in page when this attempt did not open the page, + // so that the client can show where the user finishes, and "" otherwise. 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 } @@ -286,12 +287,12 @@ func (h *signInHooks) signInRequired(id json.RawMessage) []byte { // each poll of the wait, and each poll while the call is queued, is reported to the client through notify. func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.RawMessage, notify func([]byte)) [][]byte { var polls int - report := func() { + report := func(pageURL string) { if len(progressToken) == 0 { return } polls++ - notify(progressNotification(progressToken, polls, h.c.Label)) + notify(progressNotification(progressToken, polls, h.c.Label, pageURL)) } if !h.acquire(ctx, report) { return nil @@ -334,9 +335,13 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R listChangedNotification, } case timedOut: + lastCheck := "" + if timeout.LastCheck != nil { + lastCheck = fmt.Sprintf(" (the last check failed: %v)", timeout.LastCheck) + } return [][]byte{toolResult(id, fmt.Sprintf( - "Sign-in to %s did not complete in %g minutes. Finish at %s then retry, or run: %s", - h.c.Service, timeout.Wait.Minutes(), timeout.URL, h.c.LoginCommand), true)} + "Sign-in to %s did not complete in %g minutes%s. Finish at %s then retry, or run: %s", + h.c.Service, timeout.Wait.Minutes(), lastCheck, timeout.URL, h.c.LoginCommand), true)} default: return [][]byte{toolResult(id, fmt.Sprintf("Sign-in to %s on profile %s failed: %v", h.c.Service, h.c.Profile, err), true)} } @@ -346,7 +351,7 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R // 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()) bool { +func (h *signInHooks) acquire(ctx context.Context, report func(pageURL string)) bool { ticker := time.NewTicker(queueProgressInterval) defer ticker.Stop() for { @@ -358,7 +363,7 @@ func (h *signInHooks) acquire(ctx context.Context, report func()) bool { } return true case <-ticker.C: - report() + report("") case <-ctx.Done(): return false } @@ -371,8 +376,12 @@ func (h *signInHooks) release() { } // progressNotification is the notifications/progress message for the n-th poll of a sign-in that the client asked to -// hear about through token. -func progressNotification(token json.RawMessage, n int, label string) []byte { +// 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": "2.0", @@ -380,7 +389,7 @@ func progressNotification(token json.RawMessage, n int, label string) []byte { "params": map[string]any{ "progressToken": token, "progress": n, - "message": fmt.Sprintf("Waiting for the sign-in to %s to complete.", label), + "message": message, }, }) return b diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 17b7785f841..7a53d3015f5 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -576,7 +576,7 @@ func TestAuthenticateSendsProgressOnEachPoll(t *testing.T) { proceed := make(chan struct{}) signIn := func(_ context.Context, req SignInRequest) (bool, bool, error) { for range 2 { - req.Progress() + req.Progress("") <-proceed } return false, true, nil @@ -603,8 +603,8 @@ func TestAuthenticateSendsProgressOnEachPoll(t *testing.T) { func TestAuthenticateSendsNoProgressWithoutToken(t *testing.T) { srv, _ := challengedService(t) signIn := func(_ context.Context, req SignInRequest) (bool, bool, error) { - req.Progress() - req.Progress() + req.Progress("") + req.Progress("") return false, true, nil } p := startPiped(t, t.Context(), challengedBridge(srv.URL, signIn)) @@ -622,7 +622,7 @@ func TestAuthenticateSendsNoProgressWithoutToken(t *testing.T) { func TestAuthenticateSendsNoProgressForNullToken(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)) @@ -860,3 +860,51 @@ func TestQueuedAuthenticateReportsProgress(t *testing.T) { } assert.Positive(t, signedIn, "the queued call's progress comes before the first sign-in answers") } + +// TestAuthenticateTimeoutNamesTheLastFailedCheck checks that a timeout whose last poll failed says why, so that the agent +// and the user can tell a failing service from a sign-in nobody finished. +func TestAuthenticateTimeoutNamesTheLastFailedCheck(t *testing.T) { + srv, _ := challengedService(t) + signIn := &signInStub{reply: func(int) (bool, error) { + return false, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute, LastCheck: errors.New("HTTP 503: unavailable")} + }} + 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.Contains(t, resultText(t, lines[1]), "did not complete in 5 minutes (the last check failed: HTTP 503: unavailable). Finish at "+testURL) +} + +// 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]) +} From a6b6c2183fbcdc67f29b00b94a6016777c16d60c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:02:15 +0000 Subject: [PATCH 27/55] Tidy the sign-in helpers: one serve target, shared method names, clearer errors - serveChallenge and serveSignIn take a serveTarget instead of positional parameters. - The sign-in failure answer is the error text, which already names the service, the profile and the fix. - The proxy's method names live with the other message constants. - signIn's doc says it replaces the caller's poll and timeout settings. - Shorter comments in the sign-in hooks and the test constants for the service and profile. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 3 +- cmd/aitools/mcp_serve.go | 36 ++++++---- cmd/aitools/mcp_serve_test.go | 90 +++++++++++++----------- libs/aitools/mcp/proxy/challenge.go | 30 ++++---- libs/aitools/mcp/proxy/challenge_test.go | 7 +- libs/aitools/mcp/proxy/messages.go | 3 + 6 files changed, 95 insertions(+), 74 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 3100319ef8c..40aebe36265 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -235,7 +235,8 @@ var ( // 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. opts sets the page opener and the poll callback; `add --login` shares this. +// categorized for JSON. opts sets the page opener and the poll callback; `add --login` shares this. signIn sets opts.Poll +// and opts.Timeout to the sign-in settings, so the values a caller sets for them are replaced. func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, pinnedFile, url string, svc mcp.Service, opts mcp.SignInOptions) (state string, alreadySignedIn, urlShown bool, err error) { profileName := opts.Profile opts.Poll = signInPollInterval diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 8512ff6f755..b0f5bf3af4e 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -126,7 +126,7 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { if err != nil { return serveFailure(cmd, err, 1) } - challenge, err := serveChallenge(ctx, w, entry, profileName, f.service, pinnedFile) + challenge, err := serveChallenge(ctx, serveTarget{w: w, entry: entry, profile: profileName, service: f.service, pinnedFile: pinnedFile}) if err != nil { return serveFailure(cmd, err, serveStartStatus(err)) } @@ -172,13 +172,22 @@ func (g guardCredentials) Configure(context.Context, *config.Config) (credential 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. -// pinnedFile is the config file the server is pinned to, or "" (see mcp.PinnedFile). w must be guardedClient's. -func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *mcp.Entry, profileName, service, pinnedFile string) (*proxy.Challenge, error) { - signInRequired := entry != nil && entry.SignInRequired - if entry == nil { - svc, err := lookupAtStart(ctx, w, profileName, pinnedFile, service) +func serveChallenge(ctx context.Context, t serveTarget) (*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) if err != nil { return nil, err } @@ -188,12 +197,12 @@ func serveChallenge(ctx context.Context, w *databricks.WorkspaceClient, entry *m return nil, nil } return &proxy.Challenge{ - Service: service, - Profile: profileName, - Host: w.Config.Host, - Label: mcp.ProviderLabel(service), - LoginCommand: mcpLoginHint(service, profileName, pinnedFile), - SignIn: serveSignIn(w, profileName, service, pinnedFile), + Service: t.service, + Profile: t.profile, + Host: t.w.Config.Host, + Label: mcp.ProviderLabel(t.service), + LoginCommand: mcpLoginHint(t.service, t.profile, t.pinnedFile), + SignIn: serveSignIn(t), }, nil } @@ -240,7 +249,8 @@ func waitDeleted(ctx context.Context, w *databricks.WorkspaceClient, service str // 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(w *databricks.WorkspaceClient, profileName, service, pinnedFile string) proxy.SignInFunc { +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) { diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 1f27e5c8d69..44cc044df30 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -34,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) } @@ -44,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") @@ -53,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 }, } @@ -78,7 +84,7 @@ func TestRefreshGuardStopsProxyOnFirstFailure(t *testing.T) { calls++ return cause }, - profile: "logfood", + profile: testProfile, cancel: cancel, } @@ -100,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{}})) @@ -117,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() {}, } @@ -214,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) @@ -265,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()) @@ -284,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) @@ -318,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)) @@ -337,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)) @@ -354,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{}})) @@ -373,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{}})) @@ -392,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) @@ -597,15 +603,15 @@ func runRefreshFailureSession(t *testing.T, end error) (string, bool, error) { cmd.SetErr(&stderr) ctx, stop := context.WithCancel(t.Context()) defer stop() - refresh := &refreshGuard{authenticate: expiresOnToolsList, profile: "logfood", cancel: 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: refresh.authenticateRequest, - Profile: "logfood", + Profile: testProfile, }) return stderr.String(), deleted.Load(), err } @@ -777,21 +783,21 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) require.NoError(t, err) - challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true}, "logfood", "system.ai.github", "") + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, entry: &mcp.Entry{SignInRequired: true}, profile: testProfile, service: testService}) require.NoError(t, err) require.NotNil(t, challenge) - assert.Equal(t, "system.ai.github", challenge.Service) - assert.Equal(t, "logfood", challenge.Profile) + 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(), w, &mcp.Entry{SignInRequired: false}, "logfood", "system.ai.github", "") + challenge, err = serveChallenge(t.Context(), serveTarget{w: w, entry: &mcp.Entry{SignInRequired: false}, profile: testProfile, service: testService}) require.NoError(t, err) assert.Nil(t, challenge) assert.Equal(t, int32(0), lookups.Load()) - challenge, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "") + challenge, err = serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}) require.NoError(t, err) assert.NotNil(t, challenge) assert.Equal(t, int32(1), lookups.Load(), "without a record the service is read once") @@ -804,7 +810,7 @@ func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { require.NoError(t, err) const pinned = "/work/cfg/my file.cfg" - challenge, err := serveChallenge(t.Context(), w, &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, "my profile", "system.ai.github", pinned) + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, entry: &mcp.Entry{SignInRequired: true, ConfigFile: pinned}, profile: "my profile", service: testService, pinnedFile: pinned}) 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) @@ -862,7 +868,7 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) require.NoError(t, err) - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) tc.req.Stderr, tc.req.Progress = io.Discard, func(string) {} alreadySignedIn, _, err := signIn(t.Context(), tc.req) @@ -879,7 +885,7 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { func failingGuard(stopped *bool) *refreshGuard { return &refreshGuard{ authenticate: func(*http.Request) error { return errors.New(refreshTokenExpired) }, - profile: "logfood", + profile: testProfile, cancel: func() { *stopped = true }, } } @@ -908,20 +914,20 @@ func TestServeChallengeLookupIsBounded(t *testing.T) { require.NoError(t, err) start := time.Now() - _, err = serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "") + _, err = serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}) require.Error(t, err) assert.Less(t, time.Since(start), 10*time.Second, "the lookup must not wait for the SDK's retry budget") } -// TestServeChallengeLookupAuthFailureExitsTwo checks that a startup read the profile's credential does not pass names the +// 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(), w, nil, "logfood", "system.ai.github", "") + _, err = serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}) require.Error(t, err) assert.Equal(t, serveBindStatus, serveStartStatus(err)) @@ -940,7 +946,7 @@ func TestServeChallengeLookupRefreshFailureStopsProxy(t *testing.T) { var stopped bool w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - _, err := serveChallenge(t.Context(), w, nil, "logfood", "system.ai.github", "") + _, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}) require.Error(t, err) assert.True(t, stopped, "the proxy is stopped") @@ -955,7 +961,7 @@ func TestServeSignInRefreshFailureStopsProxy(t *testing.T) { server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) var stopped bool w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Stderr: io.Discard}) @@ -994,7 +1000,7 @@ func TestServeSignInReportsEachPoll(t *testing.T) { require.NoError(t, err) polls := 0 - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + 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) @@ -1026,10 +1032,10 @@ func TestServeSignInAuthenticatesEveryRequest(t *testing.T) { })) t.Cleanup(server.Close) var authenticated atomic.Int32 - guard := &refreshGuard{authenticate: func(*http.Request) error { authenticated.Add(1); return nil }, profile: "logfood", cancel: func() {}} + guard := &refreshGuard{authenticate: func(*http.Request) error { authenticated.Add(1); return nil }, profile: testProfile, cancel: func() {}} w := guardedTestClient(t, server.URL, guard) - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + 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) @@ -1049,11 +1055,11 @@ func TestServeSignInRestartDeleteRefreshFailureStopsProxy(t *testing.T) { } return nil }, - profile: "logfood", + profile: testProfile, cancel: func() { stopped = true }, } w := guardedTestClient(t, server.URL, guard) - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Stderr: io.Discard}) @@ -1115,10 +1121,10 @@ func TestServeSignInRestartWaitsForDeletedCredential(t *testing.T) { } })) t.Cleanup(server.Close) - w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: "logfood", cancel: func() {}}) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) polls := 0 - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) { polls++ }, Stderr: io.Discard}) require.NoError(t, err) @@ -1150,9 +1156,9 @@ func TestServeSignInRestartGivesUpOnStaleCredential(t *testing.T) { _, _ = io.WriteString(rw, active) })) t.Cleanup(server.Close) - w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: "logfood", cancel: func() {}}) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Stderr: io.Discard}) require.Error(t, err) @@ -1192,9 +1198,9 @@ func TestServeSignInProgressNamesPageWhenNotOpened(t *testing.T) { _, _ = 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: "logfood", cancel: func() {}}) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) var pageURLs []string - signIn := serveSignIn(w, "logfood", "system.ai.github", "") + 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) @@ -1204,7 +1210,7 @@ func TestServeSignInProgressNamesPageWhenNotOpened(t *testing.T) { // The fixture is read twice before the caller signs in: the check before the sign-in, then two polls. want := "" if !tc.open { - want = mcp.SignInURL(server.URL, "system.ai.github", "1") + want = mcp.SignInURL(server.URL, testService, "1") } assert.Equal(t, []string{want, want}, pageURLs) }) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 21e8e9271f1..0cadd53e51b 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -30,7 +30,7 @@ var urlPattern = 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":"2.0","method":"notifications/tools/list_changed"}`) +var listChangedNotification = []byte(`{"jsonrpc":"2.0","method":"` + methodListChanged + `"}`) // SignInRequest says how one sign-in runs. type SignInRequest struct { @@ -63,7 +63,8 @@ type Challenge struct { Label string // LoginCommand is the command that signs the profile in to the service, as the hints print it. LoginCommand string - // SignIn runs the sign-in. The command provides it, so the proxy never polls or opens a browser itself. + // 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 } @@ -133,14 +134,14 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess if err != nil { return nil, err } - if req.Method != "tools/list" && req.Method != methodToolsCall { + if req.Method != methodToolsList && req.Method != methodToolsCall { return [][]byte{message}, nil } if !h.isChallenge(status, message) { h.succeeded(req.Method, status) return [][]byte{message}, nil } - if req.Method == "tools/list" { + if req.Method == methodToolsList { return [][]byte{h.authenticateList(req.ID)}, nil } return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil @@ -171,7 +172,7 @@ func (h *signInHooks) succeeded(method string, status int) { h.stateMu.Lock() defer h.stateMu.Unlock() h.alreadySignedIn = false - if method == "tools/list" { + if method == methodToolsList { h.exposed = false } } @@ -282,9 +283,8 @@ func (h *signInHooks) signInRequired(id json.RawMessage) []byte { return toolResult(id, text, true) } -// authenticate runs the sign-in and answers the tool call. Calls are serialized, so a second call waits for the first. The -// page opens unless the last attempt was cancelled while its page was showing (see pageCancelled). With a progressToken, -// each poll of the wait, and each poll while the call is queued, is reported to the client through notify. +// 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) { @@ -309,22 +309,20 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R h.stateMu.Lock() h.alreadySignedIn = err == nil && alreadySignedIn if restart && !urlShown && err != nil { - // A restart that failed before its page was shown may not have deleted the credential, so the next sign-in restarts - // again. Once the page was shown the credential is deleted, and a sign-in the user may still finish is kept. + // A restart that failed before its page was shown may not have deleted the credential, so it is asked for again. h.restart = true } h.stateMu.Unlock() if ctx.Err() != nil { - // The client cancelled the request or shutdown began. Nothing is answered for a cancelled request. + // A cancelled request is not answered. return nil } timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { case err == nil && alreadySignedIn: - // The credential was already there, for example from `mcp login` in a terminal, while the agent's tool list - // still shows only authenticate, so the agent is still asked to list again. If the service challenges again - // after this answer, the next authenticate starts a new sign-in instead. + // 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, @@ -343,7 +341,7 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R "Sign-in to %s did not complete in %g minutes%s. Finish at %s then retry, or run: %s", h.c.Service, timeout.Wait.Minutes(), lastCheck, timeout.URL, h.c.LoginCommand), true)} default: - return [][]byte{toolResult(id, fmt.Sprintf("Sign-in to %s on profile %s failed: %v", h.c.Service, h.c.Profile, err), true)} + return [][]byte{toolResult(id, err.Error(), true)} } } @@ -385,7 +383,7 @@ func progressNotification(token json.RawMessage, n int, label, pageURL string) [ // Cannot fail: the token came from a parsed request and the rest holds plain values. b, _ := json.Marshal(map[string]any{ "jsonrpc": "2.0", - "method": "notifications/progress", + "method": methodProgress, "params": map[string]any{ "progressToken": token, "progress": n, diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 7a53d3015f5..48f48342fbe 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -286,7 +286,10 @@ func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { func TestAuthenticateReportsFailureWithoutListChanged(t *testing.T) { srv, _ := challengedService(t) - signIn := &signInStub{reply: func(int) (bool, error) { return false, errors.New("sign-in failed for the test") }} + // 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":{}}}`) @@ -295,7 +298,7 @@ func TestAuthenticateReportsFailureWithoutListChanged(t *testing.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.Contains(t, lines[1], "Sign-in to system.ai.github on profile logfood failed: sign-in failed for the test") + 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 diff --git a/libs/aitools/mcp/proxy/messages.go b/libs/aitools/mcp/proxy/messages.go index 18ea1b05fec..6d39c8f3349 100644 --- a/libs/aitools/mcp/proxy/messages.go +++ b/libs/aitools/mcp/proxy/messages.go @@ -15,7 +15,10 @@ 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" From a7c7461b29708e6f41918faf74ce077a93297b0b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:04:51 +0000 Subject: [PATCH 28/55] Treat a 401 as a sign-in challenge only when its body asks for sign-in A bare 401, or one whose body is a Databricks authentication error, means the Databricks token was rejected, which a provider sign-in does not fix. Such an answer now gets the login hint, as the refresh guard words it, and no authenticate tool. A 401 with a sign-in body (JSON-RPC -32006, or a sign-in URL elicitation) is still a challenge. Before, a rejected token after an "Already signed in" answer restarted the provider sign-in and deleted a valid credential. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 1 + libs/aitools/mcp/proxy/challenge.go | 30 ++++++--- libs/aitools/mcp/proxy/challenge_test.go | 78 +++++++++++++++++++++++- 3 files changed, 97 insertions(+), 12 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index b0f5bf3af4e..0852a72a18b 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -202,6 +202,7 @@ func serveChallenge(ctx context.Context, t serveTarget) (*proxy.Challenge, error Host: t.w.Config.Host, Label: mcp.ProviderLabel(t.service), LoginCommand: mcpLoginHint(t.service, t.profile, t.pinnedFile), + PinnedFile: t.pinnedFile, SignIn: serveSignIn(t), }, nil } diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 0cadd53e51b..e33110090fb 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -63,6 +63,8 @@ type Challenge struct { 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 // 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 @@ -128,16 +130,20 @@ func (h *signInHooks) Local(ctx context.Context, request []byte, notify func([]b } // 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. +// 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). func (h *signInHooks) Result(_ context.Context, request []byte, status int, message []byte) ([][]byte, error) { req, err := parseMessage(request) if err != nil { return nil, err } + if status == http.StatusUnauthorized && !h.challenged(message) { + return [][]byte{errorResponse(req.ID, codeUpstream, h.authFailure())}, nil + } if req.Method != methodToolsList && req.Method != methodToolsCall { return [][]byte{message}, nil } - if !h.isChallenge(status, message) { + if !h.isChallenge(message) { h.succeeded(req.Method, status) return [][]byte{message}, nil } @@ -148,8 +154,8 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess } // isChallenge reports whether an upstream answer is a challenge, and records that the session has been challenged. -func (h *signInHooks) isChallenge(status int, message []byte) bool { - if !h.challenged(status, message) { +func (h *signInHooks) isChallenge(message []byte) bool { + if !h.challenged(message) { return false } h.stateMu.Lock() @@ -193,12 +199,9 @@ type textContent struct { Text string `json:"text"` } -// challenged reports whether an upstream answer says the caller must sign in: HTTP 401, JSON-RPC -// -32006, a -32042 whose elicitations are all sign-in URLs, or an isError result whose text has one. -func (h *signInHooks) challenged(status int, message []byte) bool { - if status == http.StatusUnauthorized { - return true - } +// 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(message []byte) bool { var reply upstreamReply if err := json.Unmarshal(message, &reply); err != nil { return false @@ -262,6 +265,13 @@ func (h *signInHooks) isSignInURL(raw string) bool { 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 { tool := map[string]any{ diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 48f48342fbe..2287eba06e4 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -130,8 +130,8 @@ func toolNames(t *testing.T, line string) []string { func TestChallengeReplacesListWithAuthenticateTool(t *testing.T) { cases := map[string]http.HandlerFunc{ - "rpc -32006": answerWith(http.StatusOK, listChallenge), - "http 401": unauthorized, + "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+`"}]}}}`), } @@ -911,3 +911,77 @@ func TestAuthenticateProgressNamesThePageWhenItIsNotOpened(t *testing.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") + }) + } +} + +// TestRejectedCredentialDoesNotRestartSignIn checks the loop that a rejected Databricks token made: the credential reads as +// signed in, the service rejects the token with a 401, and a later authenticate used to delete the provider credential. +// The 401 is an auth failure, so no sign-in is restarted. +func TestRejectedCredentialDoesNotRestartSignIn(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)) + p.waitLines(t, 6) + require.NoError(t, p.finish(t)) + + reqs := signIn.requests() + require.Len(t, reqs, 2) + assert.False(t, reqs[1].Restart, "the rejected token does not restart the provider sign-in") +} + +// 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") +} From 90f006cd0922160a657dd6b3bc784b43ebf534e5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:06:37 +0000 Subject: [PATCH 29/55] Start a server whose startup read fails for a reason other than the credential A server with no install record used to exit when its startup read of the service failed, even for a service that never needs sign-in. The read now fails open: the serve starts with the sign-in handling on, which acts only on a challenge that names the service, and one line on stderr says the read failed. A failure of the credential still exits 2 with the login hint. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 17 ++++-- cmd/aitools/mcp_serve_test.go | 103 +++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 14 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 0852a72a18b..416e5693b0c 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -126,7 +126,7 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { if err != nil { return serveFailure(cmd, err, 1) } - challenge, err := serveChallenge(ctx, serveTarget{w: w, entry: entry, profile: profileName, service: f.service, pinnedFile: pinnedFile}) + challenge, err := serveChallenge(ctx, serveTarget{w: w, entry: entry, profile: profileName, service: f.service, pinnedFile: pinnedFile}, cmd.ErrOrStderr()) if err != nil { return serveFailure(cmd, err, serveStartStatus(err)) } @@ -183,15 +183,22 @@ type serveTarget struct { } // 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. -func serveChallenge(ctx context.Context, t serveTarget) (*proxy.Challenge, error) { +// 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) - if err != nil { + switch { + case err == nil: + signInRequired = svc.SignInRequired + case serveStartStatus(err) == serveBindStatus: return nil, err + default: + fmt.Fprintf(errOut, "%v; serving it with sign-in handling on\n", err) + signInRequired = true } - signInRequired = svc.SignInRequired } if !signInRequired { return nil, nil diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 44cc044df30..ec1d2128965 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -783,7 +783,7 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { 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}) + 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) @@ -792,12 +792,12 @@ func TestServeChallengeFollowsStateOrServiceLookup(t *testing.T) { 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}) + 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}) + 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") @@ -810,7 +810,7 @@ func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { 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}) + 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) @@ -904,7 +904,8 @@ func serviceServer(t *testing.T, status int, body string) *httptest.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. +// 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 }) @@ -914,12 +915,96 @@ func TestServeChallengeLookupIsBounded(t *testing.T) { require.NoError(t, err) start := time.Now() - _, err = serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}) + challenge, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) - require.Error(t, err) + 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) { @@ -927,7 +1012,7 @@ func TestServeChallengeLookupAuthFailureExitsTwo(t *testing.T) { 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}) + _, err = serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) require.Error(t, err) assert.Equal(t, serveBindStatus, serveStartStatus(err)) @@ -946,7 +1031,7 @@ func TestServeChallengeLookupRefreshFailureStopsProxy(t *testing.T) { var stopped bool w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - _, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}) + _, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) require.Error(t, err) assert.True(t, stopped, "the proxy is stopped") From d86f4fd39eecec129f0845f92675eac0478a2808 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:09:51 +0000 Subject: [PATCH 30/55] Use SplitSeq in the stderr test Co-authored-by: Isaac --- libs/aitools/mcp/proxy/bridge_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/aitools/mcp/proxy/bridge_test.go b/libs/aitools/mcp/proxy/bridge_test.go index d490066e8bd..11c60b775c4 100644 --- a/libs/aitools/mcp/proxy/bridge_test.go +++ b/libs/aitools/mcp/proxy/bridge_test.go @@ -2334,7 +2334,7 @@ func TestSignInWritesShareTheBridgeStderr(t *testing.T) { require.NoError(t, <-done) var progress int - for _, line := range strings.Split(stderr.String(), "\n") { + for line := range strings.SplitSeq(stderr.String(), "\n") { if strings.HasPrefix(line, "sign-in progress ") { progress++ } From 4d42c2efe4a3297a57cc01adeaa39cf6ccef8a94 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:22:39 +0000 Subject: [PATCH 31/55] Use a rejected token in the serve tests that expect the proxy to stop A token failure that is not a rejection now fails only its request. The tests that expect the proxy to stop use a rejected token, and the two proxy tests that keep serving use a transient failure with the bridge's guard. Co-authored-by: Isaac --- cmd/aitools/mcp_serve_test.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index ec1d2128965..57b8babd487 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -513,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() @@ -553,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)) @@ -884,7 +890,7 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { // failingGuard returns a refresh guard whose token cannot be refreshed, and records whether the proxy was stopped. func failingGuard(stopped *bool) *refreshGuard { return &refreshGuard{ - authenticate: func(*http.Request) error { return errors.New(refreshTokenExpired) }, + authenticate: func(*http.Request) error { return refreshRejected }, profile: testProfile, cancel: func() { *stopped = true }, } @@ -1136,7 +1142,7 @@ func TestServeSignInRestartDeleteRefreshFailureStopsProxy(t *testing.T) { guard := &refreshGuard{ authenticate: func(r *http.Request) error { if r.Method == http.MethodDelete { - return errors.New(refreshTokenExpired) + return refreshRejected } return nil }, From c282b1a5a7bc1fe8bffe1f88fb423d8846e47a0a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 07:22:42 +0000 Subject: [PATCH 32/55] Test that a transient failure of the sign-in client does not stop the proxy A token failure that is not a rejection fails only the sign-in's request, with the transient message, and the guard does not stop the proxy. Co-authored-by: Isaac --- cmd/aitools/mcp_serve_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 57b8babd487..72b29362bc1 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -1133,6 +1133,29 @@ func TestServeSignInAuthenticatesEveryRequest(t *testing.T) { 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, 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()) +} + // TestServeSignInRestartDeleteRefreshFailureStopsProxy checks that the DELETE of a restart is authenticated by the guard // too, so a token that cannot be refreshed for it stops the proxy with the login hint. func TestServeSignInRestartDeleteRefreshFailureStopsProxy(t *testing.T) { From f32bf21072e17e5078278f692d5ec5d3cabc7953 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:38:42 +0000 Subject: [PATCH 33/55] Read sign-in state only from the response to the request The sign-in hooks saw every upstream message of an exchange, including event-stream notifications, and counted any 2xx answer as success. A progress notification or a JSON-RPC error on HTTP 200 could therefore end the "Already signed in" state or the authenticate tool. Only the response to the request is read now, and only a response with a result and no error counts as success. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 59 +++++++++++++----------- libs/aitools/mcp/proxy/challenge_test.go | 48 +++++++++++++++++++ 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index e33110090fb..ec7b583d0cd 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -131,33 +131,45 @@ func (h *signInHooks) Local(ctx context.Context, request []byte, notify func([]b // 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). +// 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 } - if status == http.StatusUnauthorized && !h.challenged(message) { + var reply upstreamReply + if err := json.Unmarshal(message, &reply); err != nil { + return nil, err + } + if !reply.isResponseTo(req) { + 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 !h.isChallenge(message) { - h.succeeded(req.Method, status) - return [][]byte{message}, nil + if challenged { + h.recordChallenge() + if req.Method == methodToolsList { + return [][]byte{h.authenticateList(req.ID)}, nil + } + return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil } - if req.Method == methodToolsList { - return [][]byte{h.authenticateList(req.ID)}, nil + // A result with no error means the service accepts the caller's credential. An error answer, such as a JSON-RPC + // error on HTTP 200 or a failure the bridge wrote for the request, says nothing about it. + if status/100 == 2 && reply.Error == nil && reply.Result != nil { + h.succeeded(req.Method) } - return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil + return [][]byte{message}, nil } -// isChallenge reports whether an upstream answer is a challenge, and records that the session has been challenged. -func (h *signInHooks) isChallenge(message []byte) bool { - if !h.challenged(message) { - return false - } +// recordChallenge records a challenge to the session. The authenticate tool is listed from now on, and a challenge that +// follows an "Already signed in" answer asks for a restart of the sign-in. +func (h *signInHooks) recordChallenge() { h.stateMu.Lock() defer h.stateMu.Unlock() h.exposed = true @@ -165,16 +177,12 @@ func (h *signInHooks) isChallenge(message []byte) bool { h.restart = true h.alreadySignedIn = false } - return true } -// succeeded records an upstream answer that is not a challenge. An answer with a success status means the service accepts -// the caller's credential, so the "Already signed in" state ends: a challenge after this one is not a challenge to that -// answer. A tools/list answer also takes the authenticate tool off the list, since the list is the service's again. -func (h *signInHooks) succeeded(method string, status int) { - if status/100 != 2 { - return - } +// succeeded records a successful answer to a request of method. The "Already signed in" state ends: a challenge after +// this answer is not a challenge to the credential that was reported signed in. A tools/list answer also takes the +// authenticate tool off the list, since the list is the service's again. +func (h *signInHooks) succeeded(method string) { h.stateMu.Lock() defer h.stateMu.Unlock() h.alreadySignedIn = false @@ -183,8 +191,9 @@ func (h *signInHooks) succeeded(method string, status int) { } } -// upstreamReply is the part of an upstream message that challenge detection reads. +// 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"` @@ -201,11 +210,7 @@ type textContent struct { // 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(message []byte) bool { - var reply upstreamReply - if err := json.Unmarshal(message, &reply); err != nil { - return false - } +func (h *signInHooks) challenged(reply *upstreamReply) bool { if reply.Error != nil { switch reply.Error.Code { case codeSignInRequired: diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 2287eba06e4..ed17aac11a4 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -985,3 +985,51 @@ func TestAuthFailureNamesPinnedConfigFile(t *testing.T) { 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.restart, "the challenge after the answer asks for a restart") + }) + 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", SignIn: signIn} +} From 16da5cc1f674ba40b1eb49eb01de70ab7df7f80f Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:39:07 +0000 Subject: [PATCH 34/55] End a pending sign-in restart with the next success A challenge after "Already signed in" asks for a restart of the sign-in. A success from the service before the next authenticate left that restart pending, so a much later, unrelated challenge could delete a working credential. A success now ends the restart too. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 8 +++--- libs/aitools/mcp/proxy/challenge_test.go | 31 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index ec7b583d0cd..1e32c7ae2b9 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -179,13 +179,15 @@ func (h *signInHooks) recordChallenge() { } } -// succeeded records a successful answer to a request of method. The "Already signed in" state ends: a challenge after -// this answer is not a challenge to the credential that was reported signed in. A tools/list answer also takes the -// authenticate tool off the list, since the list is the service's again. +// succeeded records a successful answer to a request of method. The "Already signed in" state and a pending restart end: +// a challenge after this answer is not a challenge to the credential that was reported signed in, and the service +// accepts the credential, so there is nothing to restart. A tools/list answer also takes the authenticate tool off the +// list, since the list is the service's again. func (h *signInHooks) succeeded(method string) { h.stateMu.Lock() defer h.stateMu.Unlock() h.alreadySignedIn = false + h.restart = false if method == methodToolsList { h.exposed = false } diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index ed17aac11a4..ab193ea5885 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1033,3 +1033,34 @@ func TestOnlyTheResponseToTheRequestEndsTheSignInState(t *testing.T) { 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", SignIn: signIn} } + +// TestSuccessEndsAPendingRestart checks that a restart asked for by a challenge after "Already signed in" ends with the +// next success. The agent may not call authenticate before the service answers again, so a restart left pending would +// delete a working credential on a much later, unrelated challenge. +func TestSuccessEndsAPendingRestart(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) + 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)) + p.waitLines(t, 8) + require.NoError(t, p.finish(t)) + + reqs := signIn.requests() + require.Len(t, reqs, 2) + assert.False(t, reqs[1].Restart, "a success after the challenge ends the restart it asked for") +} From d10e158f1abf480d7aede293bc98ce0b1828e08a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:39:58 +0000 Subject: [PATCH 35/55] Keep authenticate listed after a challenged tool call A challenged tools/call tells the agent to call authenticate, but the agent's next tools/list could pass unchallenged, which removed the tool from the list and left the agent without it. While the last challenge came from a call, a passing list now carries the authenticate tool, until a sign-in ends or a call passes. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 76 ++++++++++++++++++++---- libs/aitools/mcp/proxy/challenge_test.go | 49 +++++++++++++++ 2 files changed, 112 insertions(+), 13 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 1e32c7ae2b9..9a5623b199f 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -94,6 +94,9 @@ type signInHooks struct { // restart means the next sign-in starts from no credential. A challenge that follows an "Already signed in" answer // sets it: the credential reads as signed in, but the service does not accept it. restart 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 } // newSignInHooks returns the sign-in hooks of c. Progress lines of a sign-in are written to stderr. @@ -153,7 +156,7 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess return [][]byte{message}, nil } if challenged { - h.recordChallenge() + h.recordChallenge(req.Method) if req.Method == methodToolsList { return [][]byte{h.authenticateList(req.ID)}, nil } @@ -162,35 +165,74 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess // A result with no error means the service accepts the caller's credential. An error answer, such as a JSON-RPC // error on HTTP 200 or a failure the bridge wrote for the request, says nothing about it. if status/100 == 2 && reply.Error == nil && reply.Result != nil { - h.succeeded(req.Method) + msg, err := h.succeeded(req.Method, message) + if err != nil { + return nil, err + } + return [][]byte{msg}, nil } return [][]byte{message}, nil } -// recordChallenge records a challenge to the session. The authenticate tool is listed from now on, and a challenge that -// follows an "Already signed in" answer asks for a restart of the sign-in. -func (h *signInHooks) recordChallenge() { +// 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 asks for a restart of the sign-in. +func (h *signInHooks) recordChallenge(method string) { h.stateMu.Lock() defer h.stateMu.Unlock() h.exposed = true + h.callChallenged = method == methodToolsCall if h.alreadySignedIn { h.restart = true h.alreadySignedIn = false } } -// succeeded records a successful answer to a request of method. The "Already signed in" state and a pending restart end: -// a challenge after this answer is not a challenge to the credential that was reported signed in, and the service -// accepts the credential, so there is nothing to restart. A tools/list answer also takes the authenticate tool off the -// list, since the list is the service's again. -func (h *signInHooks) succeeded(method string) { +// succeeded records a successful answer to a request of method, and returns the message to write. The "Already signed in" +// state and a pending restart end: a challenge after this answer is not a challenge to the credential that was reported +// signed in, and the service accepts the credential, so there is nothing to restart. A successful tools/call ends a call +// challenge. A tools/list answer 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(method string, message []byte) ([]byte, error) { h.stateMu.Lock() defer h.stateMu.Unlock() h.alreadySignedIn = false h.restart = false - if method == methodToolsList { + if method == methodToolsCall { + h.callChallenged = false + return message, nil + } + if !h.callChallenged { h.exposed = false + return message, nil } + return h.withAuthenticate(message) +} + +// withAuthenticate adds the authenticate tool to the tools of a tools/list result, which has a result. +func (h *signInHooks) withAuthenticate(msg []byte) ([]byte, error) { + 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) + } + 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) } // upstreamReply is an upstream message as challenge detection reads it: its envelope, and its error or result. @@ -281,7 +323,12 @@ func (h *signInHooks) authFailure() string { // authenticateList is the one-tool list a challenged service answers with. func (h *signInHooks) authenticateList(id json.RawMessage) []byte { - tool := map[string]any{ + return resultResponse(id, map[string]any{"tools": []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 "+ @@ -289,7 +336,6 @@ func (h *signInHooks) authenticateList(id json.RawMessage) []byte { h.c.Service, h.c.Host, h.c.Profile, h.c.Label), "inputSchema": map[string]any{"type": "object", "properties": map[string]any{}, "additionalProperties": false}, } - return resultResponse(id, map[string]any{"tools": []any{tool}}) } // signInRequired answers a challenged tool call without opening a browser. @@ -325,6 +371,10 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R 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 + } if restart && !urlShown && err != nil { // A restart that failed before its page was shown may not have deleted the credential, so it is asked for again. h.restart = true diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index ab193ea5885..5b6d9919885 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1064,3 +1064,52 @@ func TestSuccessEndsAPendingRestart(t *testing.T) { require.Len(t, reqs, 2) assert.False(t, reqs[1].Restart, "a success after the challenge ends the restart it asked for") } + +// 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") +} From 7c3ed2302b579fdd3c65eab0f99ce450a876866a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:41:19 +0000 Subject: [PATCH 36/55] Send no sign-in progress for a call that was cancelled A poll of a sign-in can fire after the client cancelled the call. Its progress notification was still written for the cancelled request. The progress is now checked against the call's context before it is sent. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 3 ++- libs/aitools/mcp/proxy/challenge_test.go | 26 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 9a5623b199f..b795e719229 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -351,7 +351,8 @@ func (h *signInHooks) signInRequired(id json.RawMessage) []byte { func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.RawMessage, notify func([]byte)) [][]byte { var polls int report := func(pageURL string) { - if len(progressToken) == 0 { + // A poll that fires after the client cancelled the call reports nothing for it. + if len(progressToken) == 0 || ctx.Err() != nil { return } polls++ diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 5b6d9919885..c75d943550c 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1113,3 +1113,29 @@ func TestCallChallengeKeepsAuthenticateListed(t *testing.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") + } +} From 06f440e7d24e697fe6a44e1ee5eec3e054ad584d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:41:21 +0000 Subject: [PATCH 37/55] Name the sign-in page in progress on every poll The progress of a sign-in named the page only when the attempt did not open it, so a client that hides stderr, or a browser that did not open, never saw where to finish until the wait timed out. The page is now named on every poll. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 7 +------ cmd/aitools/mcp_serve_test.go | 11 ++++------- libs/aitools/mcp/proxy/challenge.go | 4 ++-- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 416e5693b0c..937aa04e349 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -284,13 +284,8 @@ func serveSignIn(t serveTarget) proxy.SignInFunc { if req.Open { openPage = browser.OpenOutputToStderr } - // A poll names the page when this attempt did not open it, so that the client can show where the user finishes. - pageURL := url - if req.Open { - pageURL = "" - } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} - _, alreadySignedIn, urlShown, err := signIn(ctx, req.Stderr, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: func() { req.Progress(pageURL) }}) + _, alreadySignedIn, urlShown, err := signIn(ctx, req.Stderr, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: func() { req.Progress(url) }}) return alreadySignedIn, urlShown, err } } diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 72b29362bc1..335fc40af11 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -1280,9 +1280,9 @@ func TestServeSignInRestartGivesUpOnStaleCredential(t *testing.T) { assert.False(t, urlShown, "no page is shown while the credential still reads as signed in") } -// TestServeSignInProgressNamesPageWhenNotOpened checks that the progress of a sign-in names its page when this attempt -// does not open it, and names none when it does. -func TestServeSignInProgressNamesPageWhenNotOpened(t *testing.T) { +// 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 @@ -1322,10 +1322,7 @@ func TestServeSignInProgressNamesPageWhenNotOpened(t *testing.T) { require.NoError(t, err) // The fixture is read twice before the caller signs in: the check before the sign-in, then two polls. - want := "" - if !tc.open { - want = mcp.SignInURL(server.URL, testService, "1") - } + want := mcp.SignInURL(server.URL, testService, "1") assert.Equal(t, []string{want, want}, pageURLs) }) } diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index b795e719229..7db09c77222 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -39,8 +39,8 @@ type SignInRequest struct { // Restart deletes the caller's sign-in before the sign-in runs, so that it starts from no credential. The proxy asks // for it when the service challenged the session again after an "Already signed in" answer. Restart bool - // Progress is called on each poll of the wait. Its pageURL is the sign-in page when this attempt did not open the page, - // so that the client can show where the user finishes, and "" otherwise. It is never nil. + // 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 From 181a61a98194ad10d230bb67469df8b3e8f0ac30 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:43:40 +0000 Subject: [PATCH 38/55] Name the sign-in timeout only in the sign-in's own error The proxy rebuilt the timeout text that the sign-in already returns, from fields that only it used. The timeout now carries the last poll's failure alone, and the proxy answers with the error's text. Co-authored-by: Isaac --- libs/aitools/mcp/login.go | 19 +++++++---------- libs/aitools/mcp/proxy/challenge.go | 12 +---------- libs/aitools/mcp/proxy/challenge_test.go | 27 ++++++++++++------------ 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index 5316f6f8eec..d68d30781ce 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -21,12 +21,9 @@ const ( SignInWaitTimeout = 5 * time.Minute ) -// SignInTimeoutError means the caller's credential did not become ACTIVE before the wait ended. URL is the sign-in page -// the user finishes at, and Wait is how long the wait was. LastCheck is the failure of the last poll, when that poll -// failed; a poll that reads the credential clears it. +// SignInTimeoutError means the caller's credential did not become ACTIVE before the wait ended. LastCheck is the failure +// of the last poll, when that poll failed; a poll that reads the credential clears it. type SignInTimeoutError struct { - URL string - Wait time.Duration LastCheck error } @@ -124,7 +121,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str for { select { case <-waitCtx.Done(): - return false, true, waitEnded(ctx, url, opts.Timeout, lastCheck) + return false, true, waitEnded(ctx, lastCheck) case err := <-opened: opened = nil if err != nil { @@ -139,7 +136,7 @@ func SignIn(ctx context.Context, w *databricks.WorkspaceClient, url, service str state, err := CredentialState(waitCtx, w, service) if err != nil { if waitCtx.Err() != nil { - return false, true, waitEnded(ctx, url, opts.Timeout, lastCheck) + return false, true, waitEnded(ctx, lastCheck) } if !transientPollError(err) { return false, true, err @@ -171,13 +168,13 @@ func transientPollError(err error) bool { return ok } -// waitEnded returns why the sign-in wait ended: the caller's cancellation, or else the timeout, with the page URL, the -// wait and the failure of the last poll. -func waitEnded(ctx context.Context, url string, wait time.Duration, lastCheck error) error { +// waitEnded returns why the sign-in wait ended: the caller's cancellation, or else the timeout, with the failure of the +// last poll. +func waitEnded(ctx context.Context, lastCheck error) error { if err := ctx.Err(); err != nil { return err } - return &SignInTimeoutError{URL: url, Wait: wait, LastCheck: lastCheck} + return &SignInTimeoutError{LastCheck: lastCheck} } // SignOut deletes the caller's sign-in to a service. It reports false when the caller was not diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 7db09c77222..1498c90d2f9 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -48,7 +48,7 @@ type SignInRequest struct { // 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. It returns a *mcp.SignInTimeoutError when the wait ended first. +// 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. @@ -385,8 +385,6 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R // A cancelled request is not answered. return nil } - timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) - switch { case err == nil && alreadySignedIn: // The credential was there already, for example from `mcp login` in a terminal, while the agent's list still @@ -400,14 +398,6 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R 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, } - case timedOut: - lastCheck := "" - if timeout.LastCheck != nil { - lastCheck = fmt.Sprintf(" (the last check failed: %v)", timeout.LastCheck) - } - return [][]byte{toolResult(id, fmt.Sprintf( - "Sign-in to %s did not complete in %g minutes%s. Finish at %s then retry, or run: %s", - h.c.Service, timeout.Wait.Minutes(), lastCheck, timeout.URL, h.c.LoginCommand), true)} default: return [][]byte{toolResult(id, err.Error(), true)} } diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index c75d943550c..34843c8b4ae 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -14,7 +14,6 @@ import ( "testing" "time" - "github.com/databricks/cli/libs/aitools/mcp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -260,7 +259,8 @@ func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { srv, _ := challengedService(t) signIn := &signInStub{reply: func(call int) (bool, error) { if call == 1 { - return false, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute} + 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 }} @@ -274,7 +274,7 @@ func TestAuthenticateReopensPageAfterTimeout(t *testing.T) { 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, "databricks aitools mcp login system.ai.github --profile logfood") + 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) @@ -340,7 +340,7 @@ func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { var calls atomic.Int32 signIn := func(ctx context.Context, _ SignInRequest) (bool, bool, error) { calls.Add(1) - return false, true, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute} + 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) @@ -354,7 +354,7 @@ func TestChallengeHintsPrintTheLoginCommand(t *testing.T) { 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, or run: "+loginCommand) + assert.Contains(t, resultText(t, lines[2]), "then retry: "+loginCommand) } // TestAuthenticateWritesNothingWhenCancelled checks that an authenticate call the client cancelled is not answered: the @@ -410,7 +410,7 @@ func TestAuthenticateOpensPageAgainUnlessACancelledAttemptShowedIt(t *testing.T) }{ "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: &mcp.SignInTimeoutError{URL: testURL, Wait: time.Minute}}, 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}, @@ -864,13 +864,13 @@ func TestQueuedAuthenticateReportsProgress(t *testing.T) { assert.Positive(t, signedIn, "the queued call's progress comes before the first sign-in answers") } -// TestAuthenticateTimeoutNamesTheLastFailedCheck checks that a timeout whose last poll failed says why, so that the agent -// and the user can tell a failing service from a sign-in nobody finished. -func TestAuthenticateTimeoutNamesTheLastFailedCheck(t *testing.T) { +// 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, &mcp.SignInTimeoutError{URL: testURL, Wait: 5 * time.Minute, LastCheck: errors.New("HTTP 503: unavailable")} - }} + 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) @@ -878,7 +878,8 @@ func TestAuthenticateTimeoutNamesTheLastFailedCheck(t *testing.T) { lines := p.waitLines(t, 2) require.NoError(t, p.finish(t)) - assert.Contains(t, resultText(t, lines[1]), "did not complete in 5 minutes (the last check failed: HTTP 503: unavailable). Finish at "+testURL) + 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 From 97f6a708892fcfe359999df428a9a3ff179171ec Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:43:42 +0000 Subject: [PATCH 39/55] Compile the sign-in URL pattern on first use A package-level regexp compiles at init. The pattern now follows the package's other patterns and compiles on first use. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 6 ++++-- libs/aitools/mcp/proxy/challenge_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 1498c90d2f9..d7c1d67d9f1 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -25,7 +25,9 @@ const ( // 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 = regexp.MustCompile("https?://[^\\s<>()\\[\\]\"'*`]+") +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 @@ -291,7 +293,7 @@ func (h *signInHooks) urlElicitation(data json.RawMessage) bool { // 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) { + for _, found := range urlPattern().FindAllString(c.Text, -1) { if h.isSignInURL(strings.TrimRight(found, ".,:;")) { return true } diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 34843c8b4ae..a9820538cea 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1140,3 +1140,10 @@ func TestProgressIsNotSentAfterCancel(t *testing.T) { 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)) +} From 95c2b7a5c0d8209fcc7d875973ddd994aa3fcfb9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:43:43 +0000 Subject: [PATCH 40/55] Pass signIn only the options the caller sets signIn overwrote the poll and timeout of the options it was given, so the fields a caller set for them were silently ignored. Callers now pass the profile, the page opener and the poll callback, and signIn builds the sign-in options itself. Co-authored-by: Isaac --- cmd/aitools/mcp_add.go | 2 +- cmd/aitools/mcp_login.go | 25 ++++++++++++++++++------- cmd/aitools/mcp_login_test.go | 2 +- cmd/aitools/mcp_serve.go | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 4e5df671248..c37b20a7314 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -880,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.pinned, url, plans[i].service, mcp.SignInOptions{Profile: req.profile, Open: browser.OpenOutputToStderr}) + 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 40aebe36265..4fc08a385c9 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -180,7 +180,7 @@ func mcpLogin(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClie 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, pinnedFile, url, svc, mcp.SignInOptions{Profile: out.Profile, Open: browser.OpenOutputToStderr}) + 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 { @@ -233,15 +233,26 @@ var ( signInWaitTimeout = mcp.SignInWaitTimeout ) +// 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. opts sets the page opener and the poll callback; `add --login` shares this. signIn sets opts.Poll -// and opts.Timeout to the sign-in settings, so the values a caller sets for them are replaced. -func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, pinnedFile, url string, svc mcp.Service, opts mcp.SignInOptions) (state string, alreadySignedIn, urlShown bool, err error) { +// 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 - opts.Poll = signInPollInterval - opts.Timeout = signInWaitTimeout - alreadySignedIn, urlShown, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, opts) + alreadySignedIn, urlShown, err = mcp.SignIn(ctx, w, url, svc.FullName, errOut, mcp.SignInOptions{ + Profile: profileName, + Poll: signInPollInterval, + Timeout: signInWaitTimeout, + Open: opts.Open, + OnPoll: opts.OnPoll, + }) timeout, timedOut := errors.AsType[*mcp.SignInTimeoutError](err) switch { case timedOut: diff --git a/cmd/aitools/mcp_login_test.go b/cmd/aitools/mcp_login_test.go index 37680fa2163..33658f88cef 100644 --- a/cmd/aitools/mcp_login_test.go +++ b/cmd/aitools/mcp_login_test.go @@ -346,7 +346,7 @@ func TestSignInTimeoutNamesTheLastFailedCheck(t *testing.T) { ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) state, _, _, err := signIn(ctx, io.Discard, w, "", "https://example.test/login", mcp.Service{FullName: "system.ai.github", SignInRequired: true}, - mcp.SignInOptions{Profile: "logfood", Open: func(context.Context, string) error { return nil }}) + 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 937aa04e349..d950e84c062 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -285,7 +285,7 @@ func serveSignIn(t serveTarget) proxy.SignInFunc { openPage = browser.OpenOutputToStderr } svc := mcp.Service{FullName: service, Known: mcp.KnownOf(service)} - _, alreadySignedIn, urlShown, err := signIn(ctx, req.Stderr, w, pinnedFile, url, svc, mcp.SignInOptions{Profile: profileName, Open: openPage, OnPoll: func() { req.Progress(url) }}) + _, 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 } } From 413d8bbc1a6504ba24b3d3f7769e37749e067c3c Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:52:09 +0000 Subject: [PATCH 41/55] Describe sign-in and profile failures with the profile and fix The sign-in of the authenticate tool returned the errors of the credential delete, the workspace ID read and the wait after a restart as they came. Each names the profile and the login command now, as the login command does. A restart that removes the caller's sign-in says so on the sign-in's stderr before the page. The wait for a restart's delete is bounded as a whole. Its credential reads could hold it for the SDK's retry budget, which is minutes. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 35 ++++---- cmd/aitools/mcp_serve_test.go | 146 ++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 14 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index d950e84c062..585eaefe3a1 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -233,22 +233,25 @@ var restartWaitTimeout = 30 * time.Second // waitDeleted waits, up to restartWaitTimeout, until the caller's credential no longer reads as ACTIVE. A delete can take // a moment to show, and a sign-in that read ACTIVE then would answer that the caller is already signed in, with no page. -func waitDeleted(ctx context.Context, w *databricks.WorkspaceClient, service string) error { - deadline := time.Now().Add(restartWaitTimeout) +// The bound covers the reads too, so a read that does not return ends the wait at the bound. +func waitDeleted(parent context.Context, w *databricks.WorkspaceClient, service, profileName, pinnedFile string) error { + ctx, cancel := context.WithTimeout(parent, restartWaitTimeout) + defer cancel() for { state, err := mcp.CredentialState(ctx, w, service) - if err != nil { - return err - } - if state != catalog.ProvisioningInfoStateActive { + switch { + case err == nil && state != catalog.ProvisioningInfoStateActive: return nil - } - if time.Now().After(deadline) { - return fmt.Errorf("the deleted sign-in to %s still reads as signed in", service) + case parent.Err() != nil: + return parent.Err() + case ctx.Err() != nil: + return fmt.Errorf("the removed sign-in to %s on profile %s still reads as signed in; run: %s", + service, profileName, mcpLoginHint(service, profileName, pinnedFile)) + case err != nil: + return describeRequestError(profileName, pinnedFile, err) } select { case <-ctx.Done(): - return ctx.Err() case <-time.After(signInPollInterval): } } @@ -264,17 +267,21 @@ func serveSignIn(t serveTarget) proxy.SignInFunc { return func(ctx context.Context, req proxy.SignInRequest) (bool, bool, error) { if req.Restart { // The credential is deleted first, so that the sign-in below starts from no credential. - if _, err := mcp.SignOut(ctx, w, service); err != nil { - return false, false, err + deleted, err := mcp.SignOut(ctx, w, service) + if err != nil { + return false, false, describeRequestError(profileName, pinnedFile, err) } - if err := waitDeleted(ctx, w, service); err != nil { + if deleted { + fmt.Fprintf(req.Stderr, "%s did not accept the sign-in on profile %s, so it was removed; sign in again.\n", service, profileName) + } + if err := waitDeleted(ctx, w, service, profileName, pinnedFile); err != nil { return false, false, err } } if workspaceID == "" { id, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { - return false, false, err + return false, false, describeRequestError(profileName, pinnedFile, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) } workspaceID = id } diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 335fc40af11..1e19cd73b2a 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -1327,3 +1327,149 @@ func TestServeSignInProgressNamesThePage(t *testing.T) { }) } } + +// TestServeSignInRestartSaysTheCredentialWasRemoved checks that a restart that removes the caller's sign-in says so on the +// sign-in's stderr before the page, since the agent's user did not ask for the sign-in to be removed. +func TestServeSignInRestartSaysTheCredentialWasRemoved(t *testing.T) { + prevPoll, prevWait := signInPollInterval, signInWaitTimeout + prevRestart := restartWaitTimeout + t.Cleanup(func() { + signInPollInterval, signInWaitTimeout, restartWaitTimeout = prevPoll, prevWait, prevRestart + }) + signInPollInterval, signInWaitTimeout, restartWaitTimeout = time.Millisecond, 10*time.Second, 10*time.Second + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + var mu sync.Mutex + deleted := false + var reads int + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + if r.URL.Path != credentialPath { + rw.WriteHeader(http.StatusNotFound) + return + } + rw.Header().Set("Content-Type", "application/json") + if r.Method == http.MethodDelete { + deleted = true + _, _ = io.WriteString(rw, `{}`) + return + } + // After the delete, the first two reads say NOT_FOUND, and the third, the first poll, says ACTIVE. + if !deleted { + _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}`) + return + } + reads++ + if reads <= 2 { + 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 stderr bytes.Buffer + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) {}, Stderr: &stderr}) + + require.NoError(t, err) + notice := testService + " did not accept the sign-in on profile " + testProfile + ", so it was removed; sign in again." + require.Contains(t, stderr.String(), notice) + assert.Less(t, strings.Index(stderr.String(), notice), strings.Index(stderr.String(), "Open this URL"), "the notice comes before the page") +} + +// TestServeSignInRestartWaitIsBounded checks that the wait for a restart's delete ends at restartWaitTimeout even when a +// read of the credential does not return, so the wait is not held up by the SDK's own retry budget. +func TestServeSignInRestartWaitIsBounded(t *testing.T) { + prevRestart := restartWaitTimeout + t.Cleanup(func() { restartWaitTimeout = prevRestart }) + restartWaitTimeout = 100 * time.Millisecond + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Path != credentialPath { + rw.WriteHeader(http.StatusNotFound) + return + } + if r.Method == http.MethodDelete { + _, _ = io.WriteString(rw, `{}`) + return + } + // A read that hangs until the client gives up. + select { + case <-r.Context().Done(): + case <-time.After(5 * time.Second): + } + })) + t.Cleanup(server.Close) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) + + start := time.Now() + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) {}, Stderr: io.Discard}) + + require.Error(t, err) + assert.Less(t, time.Since(start), 3*time.Second, "the wait ends at its bound, not when the hung read returns") +} + +// TestServeSignInErrorsNameTheProfileAndFix checks that the errors of a sign-in that the authenticate tool answers name +// the profile and the fix, as the other sign-in errors do: a delete the profile may not make, and a wait that ends while +// the removed sign-in still reads as signed in. +func TestServeSignInErrorsNameTheProfileAndFix(t *testing.T) { + prevRestart := restartWaitTimeout + t.Cleanup(func() { restartWaitTimeout = prevRestart }) + restartWaitTimeout = 50 * time.Millisecond + t.Setenv("BROWSER", "none") + + const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" + const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` + cases := map[string]struct { + deleteStatus int + deleteBody string + read string + want string + }{ + "delete refused": { + deleteStatus: http.StatusUnauthorized, + deleteBody: `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`, + read: active, + want: "databricks auth login --profile " + testProfile, + }, + "credential still signed in": { + deleteStatus: http.StatusOK, + deleteBody: `{}`, + read: active, + want: "still reads as signed in; run: databricks aitools mcp login " + testService + " --profile " + testProfile, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + 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 r.Method == http.MethodDelete { + rw.WriteHeader(tc.deleteStatus) + _, _ = io.WriteString(rw, tc.deleteBody) + return + } + _, _ = io.WriteString(rw, tc.read) + })) + t.Cleanup(server.Close) + w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) + + signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) {}, Stderr: io.Discard}) + + require.Error(t, err) + assert.Contains(t, err.Error(), tc.want) + }) + } +} From 9085d79b35a2814cd147b73a2c4e8e4842ec02d3 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:52:10 +0000 Subject: [PATCH 42/55] Start serve quietly on a signal and build the sign-in client when needed A signal during the startup read printed a read failure and served the proxy on a cancelled context. The start now ends as a shutdown does. The sign-in client was built and its profile resolved again on every serve, even for a server whose record says it needs no sign-in, so it is built only when the start reads the service or the record says it needs sign-in. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 21 +++++++++++++++++---- cmd/aitools/mcp_serve_test.go | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 585eaefe3a1..076b12ddc3e 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -122,13 +122,23 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { // 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} - w, err := guardedClient(cfg, refresh) - if err != nil { - return serveFailure(cmd, err, 1) + // 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 { - return serveFailure(cmd, err, serveStartStatus(err)) + 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{ @@ -193,6 +203,9 @@ func serveChallenge(ctx context.Context, t serveTarget, errOut io.Writer) (*prox switch { case err == nil: signInRequired = svc.SignInRequired + case ctx.Err() != nil: + // The serve is stopping, so the read's failure is its cancellation, which is not reported. + return nil, ctx.Err() case serveStartStatus(err) == serveBindStatus: return nil, err default: diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 1e19cd73b2a..a7a843f0fcb 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -1025,6 +1025,22 @@ func TestServeChallengeLookupAuthFailureExitsTwo(t *testing.T) { 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"))) From ee142d70d7ea0d8851dfddd052d5aacb45ce3cd8 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:55:04 +0000 Subject: [PATCH 43/55] Log serve acceptance setup output to LOG files The passthrough scenario created its service twice, and the authenticate scenario sent its setup output to /dev/null. The duplicate create is gone, and the setup commands write to LOG files. The retry waits still discard their output, since it repeats the captured stdout on every poll. Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt | 2 -- acceptance/cmd/aitools/mcp/serve-authenticate-tool/script | 4 ++-- acceptance/cmd/aitools/mcp/serve-passthrough/script | 3 --- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt index 0bb53ddfb93..4b1318c2b59 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/output.txt @@ -1,6 +1,4 @@ ->>> [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"}}}} - === 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 diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script index fce69b659bb..f881f8f4ba8 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script @@ -8,7 +8,7 @@ export BROWSER=none 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 trace $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"}}}}' > /dev/null +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=github" --profile logfood --json '{"comment":"Repositories, issues, pull requests","config":{"source_connection":{"name":"connections/github","options":{"oauth_provider":"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. @@ -18,7 +18,7 @@ MSYS_NO_PATHCONV=1 trace $CLI api post "/api/2.1/unity-catalog/mcp-services?pare 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 '{}' > /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 } | $CLI aitools mcp serve --profile logfood --service system.ai.github --agent claude-code > serve.stdout.txt 2> serve.stderr.txt diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/script b/acceptance/cmd/aitools/mcp/serve-passthrough/script index 28beb7858de..9a16ce3999c 100644 --- a/acceptance/cmd/aitools/mcp/serve-passthrough/script +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/script @@ -3,9 +3,6 @@ sethome home envsubst < .databrickscfg > out && mv out .databrickscfg export DATABRICKS_CONFIG_FILE=.databrickscfg -# With no install record the proxy reads the service to learn whether it needs sign-in, so the fake needs it. -MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment":"Ask questions about your data"}' > /dev/null - # DATABRICKS_HOST points elsewhere; the proxy must still talk to the profile's host. export DATABRICKS_HOST=https://elsewhere.test From 1ce44aa8b9293177bbf8c281c640c1249d48e0ed Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:55:05 +0000 Subject: [PATCH 44/55] Share one JSON-RPC response envelope in the proxy resultResponse lived beside the sign-in code and built its own envelope, while errorResponse had a second one. Both are in messages.go over one response type, with the version as a constant. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 15 ++------------- libs/aitools/mcp/proxy/messages.go | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index d7c1d67d9f1..33ab04f9ee3 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -32,7 +32,7 @@ var urlPattern = sync.OnceValue(func() *regexp.Regexp { // 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":"2.0","method":"` + methodListChanged + `"}`) +var listChangedNotification = []byte(`{"jsonrpc":"` + jsonrpcVersion + `","method":"` + methodListChanged + `"}`) // SignInRequest says how one sign-in runs. type SignInRequest struct { @@ -442,7 +442,7 @@ func progressNotification(token json.RawMessage, n int, label, pageURL string) [ } // Cannot fail: the token came from a parsed request and the rest holds plain values. b, _ := json.Marshal(map[string]any{ - "jsonrpc": "2.0", + "jsonrpc": jsonrpcVersion, "method": methodProgress, "params": map[string]any{ "progressToken": token, @@ -453,17 +453,6 @@ func progressNotification(token json.RawMessage, n int, label, pageURL string) [ 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(struct { - JSONRPC string `json:"jsonrpc"` - ID json.RawMessage `json:"id"` - Result any `json:"result"` - }{JSONRPC: "2.0", ID: id, Result: result}) - 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{ diff --git a/libs/aitools/mcp/proxy/messages.go b/libs/aitools/mcp/proxy/messages.go index 6d39c8f3349..c78767500ab 100644 --- a/libs/aitools/mcp/proxy/messages.go +++ b/libs/aitools/mcp/proxy/messages.go @@ -28,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") @@ -58,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 @@ -170,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 } From 487561481d2395a3a1adf0564b9d81bfb7ed64f9 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:55:06 +0000 Subject: [PATCH 45/55] Test the refresh failure of the startup read and the sign-in together The two tests checked the same stop with the same server and guard, one per call. They are one table test now. Co-authored-by: Isaac --- cmd/aitools/mcp_serve_test.go | 52 +++++++++++++++++------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index a7a843f0fcb..57eb411b3eb 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -1046,36 +1046,34 @@ func TestServeStartStatusIsOneForOtherFailures(t *testing.T) { assert.Equal(t, 1, serveStartStatus(errors.New("cannot read MCP service system.ai.github: not found"))) } -// TestServeChallengeLookupRefreshFailureStopsProxy checks that a token that cannot be refreshed during the startup read -// stops the proxy with the login hint, as a refresh failure of a bridge request does. -func TestServeChallengeLookupRefreshFailureStopsProxy(t *testing.T) { - server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) - var stopped bool - w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - - _, err := serveChallenge(t.Context(), serveTarget{w: w, profile: testProfile, service: testService}, io.Discard) - - require.Error(t, err) - assert.True(t, stopped, "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") -} - -// TestServeSignInRefreshFailureStopsProxy checks that a token that cannot be refreshed during the sign-in stops the proxy -// with the login hint, as a refresh failure of a bridge request does, instead of an error without the hint. -func TestServeSignInRefreshFailureStopsProxy(t *testing.T) { +// 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") - server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) - var stopped bool - w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) + 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"}`) + var stopped bool + w := guardedTestClient(t, server.URL, failingGuard(&stopped)) - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Stderr: io.Discard}) + err := call(t.Context(), w) - require.Error(t, err) - assert.True(t, stopped, "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") + require.Error(t, err) + assert.True(t, stopped, "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 From 263f0603ed9927db936595f5f416646493ebaa22 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 08:58:56 +0000 Subject: [PATCH 46/55] Update the sign-in state comments for the call challenge Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 33ab04f9ee3..e7043265447 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -85,11 +85,11 @@ type signInHooks struct { // completed. The user may still be on that page, so the next attempt does not open it again. pageCancelled bool - // stateMu guards the three fields below. It is held only briefly, so an answer to a request is not held up by a - // sign-in. + // 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. The tool is the proxy's only while it is exposed. + // unchallenged clears it, unless a call challenge is pending (see callChallenged). The tool is the proxy's only while it + // is exposed. exposed bool // alreadySignedIn means the last authenticate answered "Already signed in". alreadySignedIn bool From 7803f2cfbbc07490e24bc3f8304d588bce9ae56a Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:19:49 +0000 Subject: [PATCH 47/55] Stop serve with the login hint when a rejected token fails the startup read A rejected token cancels serve's context, so the startup read returned the cancellation and serve exited 0 with no line. The refresh failure is now checked first, and the auth failure is returned ahead of the cancellation. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 17 ++++++++++++++--- cmd/aitools/mcp_serve_test.go | 16 +++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 076b12ddc3e..6cc71b81bf5 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -133,6 +133,11 @@ func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { } 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 @@ -203,11 +208,11 @@ func serveChallenge(ctx context.Context, t serveTarget, errOut io.Writer) (*prox 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() - case serveStartStatus(err) == serveBindStatus: - return nil, err default: fmt.Fprintf(errOut, "%v; serving it with sign-in handling on\n", err) signInRequired = true @@ -367,12 +372,18 @@ 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 be, ok := errors.AsType[*mcp.BindError](err); ok && be.Category == mcp.BindAuthFailed { + 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 { diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 57eb411b3eb..9ee08a86303 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -887,12 +887,13 @@ func TestServeSignInRestartDeletesCredentialFirst(t *testing.T) { } } -// failingGuard returns a refresh guard whose token cannot be refreshed, and records whether the proxy was stopped. -func failingGuard(stopped *bool) *refreshGuard { +// 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: func() { *stopped = true }, + cancel: cancel, } } @@ -1063,13 +1064,14 @@ func TestServeRefreshFailureStopsProxy(t *testing.T) { } { t.Run(name, func(t *testing.T) { server := serviceServer(t, http.StatusBadRequest, `{"error_code":"BAD_REQUEST","message":"bad"}`) - var stopped bool - w := guardedTestClient(t, server.URL, failingGuard(&stopped)) + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + w := guardedTestClient(t, server.URL, failingGuard(cancel)) - err := call(t.Context(), w) + err := call(ctx, w) require.Error(t, err) - assert.True(t, stopped, "the proxy is stopped") + 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") }) From edbfb769b052a91a3d02a35fb84ae391705ea804 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:20:33 +0000 Subject: [PATCH 48/55] Pass through responses the sign-in checks cannot read Only the responses to tools/list and tools/call are decoded for a challenge or a success, and an HTTP 401 is still read. A response whose result or error has a shape the checks do not read is neither, so it passes through instead of failing the request. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 12 ++++++++---- libs/aitools/mcp/proxy/challenge_test.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index e7043265447..13807652454 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -143,11 +143,15 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess if err != nil { return nil, err } - var reply upstreamReply - if err := json.Unmarshal(message, &reply); 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 } - if !reply.isResponseTo(req) { + var reply upstreamReply + // 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. + if json.Unmarshal(message, &reply) != nil || !reply.isResponseTo(req) { return [][]byte{message}, nil } challenged := h.challenged(&reply) diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index a9820538cea..148c0233aa2 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1147,3 +1147,24 @@ 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") + }) + } +} From c458c8e864a4cda72d39d4badb9592223dc8836b Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:21:19 +0000 Subject: [PATCH 49/55] Keep the Already signed in state until a tool call succeeds A tools/list that passes after a challenged tool call no longer ends the state of that call's "Already signed in" answer, so a later challenge to a call starts a new sign-in. An isError result without a sign-in URL is neither a challenge nor a success and changes no state. Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 30 ++++++++++----- libs/aitools/mcp/proxy/challenge_test.go | 49 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 13807652454..e84cff46eda 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -99,6 +99,9 @@ type signInHooks struct { // 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. @@ -168,9 +171,10 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess } return [][]byte{h.signInRequired(req.ID), listChangedNotification}, nil } - // A result with no error means the service accepts the caller's credential. An error answer, such as a JSON-RPC - // error on HTTP 200 or a failure the bridge wrote for the request, says nothing about it. - if status/100 == 2 && reply.Error == nil && reply.Result != 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(req.Method, message) if err != nil { return nil, err @@ -187,26 +191,32 @@ func (h *signInHooks) recordChallenge(method string) { defer h.stateMu.Unlock() h.exposed = true h.callChallenged = method == methodToolsCall + h.callPending = h.callChallenged if h.alreadySignedIn { h.restart = true h.alreadySignedIn = false } } -// succeeded records a successful answer to a request of method, and returns the message to write. The "Already signed in" -// state and a pending restart end: a challenge after this answer is not a challenge to the credential that was reported -// signed in, and the service accepts the credential, so there is nothing to restart. A successful tools/call ends a call -// challenge. A tools/list answer 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. +// 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, a pending restart, a call challenge. A tools/list answer ends the same state 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(method string, message []byte) ([]byte, error) { h.stateMu.Lock() defer h.stateMu.Unlock() - h.alreadySignedIn = false - h.restart = false if method == methodToolsCall { + h.alreadySignedIn = false + h.restart = false + h.callPending = false h.callChallenged = false return message, nil } + if !h.callPending { + h.alreadySignedIn = false + h.restart = false + } if !h.callChallenged { h.exposed = false return message, nil diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 148c0233aa2..083af50251a 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1168,3 +1168,52 @@ func TestUndecodableResponsePassesThrough(t *testing.T) { }) } } + +// 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 starts the sign-in again from no credential. +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)) + p.waitLines(t, 9) + require.NoError(t, p.finish(t)) + + reqs := signIn.requests() + require.Len(t, reqs, 2) + assert.True(t, reqs[1].Restart, "the challenge after the answer, with the list passing in between, asks for a restart") +} + +// 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", a pending restart 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.restart = 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, restart: true, callChallenged: true, callPending: true}, signInHooks{ + exposed: h.exposed, alreadySignedIn: h.alreadySignedIn, restart: h.restart, callChallenged: h.callChallenged, callPending: h.callPending, + }) +} From a4e627dbc4eff0c10d467cbe0c9e3ebaf1a848e8 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:24:35 +0000 Subject: [PATCH 50/55] Ask for a logout instead of restarting the sign-in after Already signed in The proxy no longer deletes a credential. When the service challenges again after an "Already signed in" answer, the next authenticate answers with an error that names the logout command, and clears the state, so the authenticate after that runs the sign-in as usual. The restart wait, its notice and the restart tests are removed. Co-authored-by: Isaac --- cmd/aitools/mcp_login.go | 10 +- cmd/aitools/mcp_serve.go | 49 +--- cmd/aitools/mcp_serve_test.go | 328 +---------------------- libs/aitools/mcp/proxy/challenge.go | 56 ++-- libs/aitools/mcp/proxy/challenge_test.go | 178 ++++-------- 5 files changed, 102 insertions(+), 519 deletions(-) diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 4fc08a385c9..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 ( @@ -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 { diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 6cc71b81bf5..6b59855fbb1 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -28,7 +28,6 @@ import ( "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/config/credentials" "github.com/databricks/databricks-sdk-go/httpclient" - "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" "golang.org/x/oauth2" ) @@ -226,8 +225,9 @@ func serveChallenge(ctx context.Context, t serveTarget, errOut io.Writer) (*prox Profile: t.profile, Host: t.w.Config.Host, Label: mcp.ProviderLabel(t.service), - LoginCommand: mcpLoginHint(t.service, t.profile, t.pinnedFile), - PinnedFile: t.pinnedFile, + LoginCommand: mcpLoginHint(t.service, t.profile, t.pinnedFile), + LogoutCommand: mcpLogoutHint(t.service, t.profile, t.pinnedFile), + PinnedFile: t.pinnedFile, SignIn: serveSignIn(t), }, nil } @@ -245,36 +245,6 @@ func lookupAtStart(ctx context.Context, w *databricks.WorkspaceClient, profileNa return svc, nil } -// restartWaitTimeout bounds the wait, after a restart deletes the credential, for the credential to stop reading as ACTIVE. -// Tests shorten it. -var restartWaitTimeout = 30 * time.Second - -// waitDeleted waits, up to restartWaitTimeout, until the caller's credential no longer reads as ACTIVE. A delete can take -// a moment to show, and a sign-in that read ACTIVE then would answer that the caller is already signed in, with no page. -// The bound covers the reads too, so a read that does not return ends the wait at the bound. -func waitDeleted(parent context.Context, w *databricks.WorkspaceClient, service, profileName, pinnedFile string) error { - ctx, cancel := context.WithTimeout(parent, restartWaitTimeout) - defer cancel() - for { - state, err := mcp.CredentialState(ctx, w, service) - switch { - case err == nil && state != catalog.ProvisioningInfoStateActive: - return nil - case parent.Err() != nil: - return parent.Err() - case ctx.Err() != nil: - return fmt.Errorf("the removed sign-in to %s on profile %s still reads as signed in; run: %s", - service, profileName, mcpLoginHint(service, profileName, pinnedFile)) - case err != nil: - return describeRequestError(profileName, pinnedFile, err) - } - select { - case <-ctx.Done(): - case <-time.After(signInPollInterval): - } - } -} - // 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. @@ -283,19 +253,6 @@ func serveSignIn(t serveTarget) proxy.SignInFunc { // 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 req.Restart { - // The credential is deleted first, so that the sign-in below starts from no credential. - deleted, err := mcp.SignOut(ctx, w, service) - if err != nil { - return false, false, describeRequestError(profileName, pinnedFile, err) - } - if deleted { - fmt.Fprintf(req.Stderr, "%s did not accept the sign-in on profile %s, so it was removed; sign in again.\n", service, profileName) - } - if err := waitDeleted(ctx, w, service, profileName, pinnedFile); err != nil { - return false, false, err - } - } if workspaceID == "" { id, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 9ee08a86303..86a36483c4e 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -820,71 +820,7 @@ func TestServeChallengeHintNamesPinnedFileAndQuotesProfile(t *testing.T) { 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) -} - -// TestServeSignInRestartDeletesCredentialFirst checks the sign-in of the authenticate tool. A restart deletes the caller's -// credential before the sign-in reads it, so the sign-in waits for a new one rather than answering that the caller is -// already signed in. Without a restart a credential that reads ACTIVE is already signed in and is kept. -func TestServeSignInRestartDeletesCredentialFirst(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" - const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` - for _, tc := range []struct { - name string - req proxy.SignInRequest - wantDeleted bool - wantAlready bool - }{ - {name: "without restart the credential is kept", req: proxy.SignInRequest{Open: true}, wantAlready: true}, - {name: "restart deletes it first", req: proxy.SignInRequest{Open: true, Restart: true}, wantDeleted: true}, - } { - t.Run(tc.name, func(t *testing.T) { - var mu sync.Mutex - deleted, gets := false, 0 - server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - mu.Lock() - defer mu.Unlock() - if r.URL.Path != credentialPath { - rw.WriteHeader(http.StatusNotFound) - return - } - rw.Header().Set("Content-Type", "application/json") - switch { - case r.Method == http.MethodDelete: - deleted = true - _, _ = io.WriteString(rw, `{}`) - case !deleted: - _, _ = io.WriteString(rw, active) - default: - // The caller has not signed in yet after the delete, which the wait and the sign-in both read, and then does. - gets++ - if gets <= 2 { - rw.WriteHeader(http.StatusNotFound) - _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) - return - } - _, _ = io.WriteString(rw, active) - } - })) - t.Cleanup(server.Close) - w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token", WorkspaceID: "1"}) - require.NoError(t, err) - - signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) - tc.req.Stderr, tc.req.Progress = io.Discard, func(string) {} - alreadySignedIn, _, err := signIn(t.Context(), tc.req) - - require.NoError(t, err) - assert.Equal(t, tc.wantAlready, alreadySignedIn) - mu.Lock() - defer mu.Unlock() - assert.Equal(t, tc.wantDeleted, deleted) - }) - } + 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 @@ -1172,32 +1108,6 @@ func TestServeSignInTransientFailureKeepsServing(t *testing.T) { assert.NoError(t, guard.failure()) } -// TestServeSignInRestartDeleteRefreshFailureStopsProxy checks that the DELETE of a restart is authenticated by the guard -// too, so a token that cannot be refreshed for it stops the proxy with the login hint. -func TestServeSignInRestartDeleteRefreshFailureStopsProxy(t *testing.T) { - t.Setenv("BROWSER", "none") - server := serviceServer(t, http.StatusOK, `{}`) - var stopped bool - guard := &refreshGuard{ - authenticate: func(r *http.Request) error { - if r.Method == http.MethodDelete { - return refreshRejected - } - return nil - }, - 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, Restart: true, Stderr: io.Discard}) - - require.Error(t, err) - assert.True(t, stopped, "the proxy is stopped") - assert.Contains(t, err.Error(), "databricks auth login --profile logfood, then restart the MCP server in your agent") -} - // 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() @@ -1206,96 +1116,6 @@ func guardedTestClient(t *testing.T, host string, guard *refreshGuard) *databric return w } -// TestServeSignInRestartWaitsForDeletedCredential checks that after a restart's delete the sign-in does not read the -// credential as ACTIVE. A delete can take a moment to show, so the first reads may still say ACTIVE; the sign-in waits -// until they do not, and then opens the page rather than answering that the caller is already signed in. -func TestServeSignInRestartWaitsForDeletedCredential(t *testing.T) { - prevPoll, prevWait := signInPollInterval, signInWaitTimeout - prevRestart := restartWaitTimeout - t.Cleanup(func() { - signInPollInterval, signInWaitTimeout, restartWaitTimeout = prevPoll, prevWait, prevRestart - }) - signInPollInterval, signInWaitTimeout, restartWaitTimeout = time.Millisecond, 10*time.Second, 10*time.Second - t.Setenv("BROWSER", "none") - - const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" - const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` - var mu sync.Mutex - deleted := false - var reads int - server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - mu.Lock() - defer mu.Unlock() - if r.URL.Path != credentialPath { - rw.WriteHeader(http.StatusNotFound) - return - } - rw.Header().Set("Content-Type", "application/json") - if r.Method == http.MethodDelete { - deleted = true - _, _ = io.WriteString(rw, `{}`) - return - } - // After the delete, the first two reads still say ACTIVE, two read NOT_FOUND, and the caller then signs in. - reads++ - switch { - case !deleted: - _, _ = io.WriteString(rw, active) - case reads <= 2: - _, _ = io.WriteString(rw, active) - case reads <= 4: - rw.WriteHeader(http.StatusNotFound) - _, _ = io.WriteString(rw, `{"error_code":"NOT_FOUND","message":"Credential is not found"}`) - default: - _, _ = io.WriteString(rw, active) - } - })) - t.Cleanup(server.Close) - w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) - - polls := 0 - signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) - alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) { polls++ }, Stderr: io.Discard}) - - require.NoError(t, err) - assert.False(t, alreadySignedIn, "a deleted credential is not reported as signed in") - assert.True(t, urlShown) - assert.Equal(t, 1, polls, "the sign-in waits for the page's sign-in, one poll") -} - -// TestServeSignInRestartGivesUpOnStaleCredential checks that a credential that still reads ACTIVE after a restart's delete -// ends the sign-in with an error, and that no page is shown for it. -func TestServeSignInRestartGivesUpOnStaleCredential(t *testing.T) { - prevPoll, prevRestart := signInPollInterval, restartWaitTimeout - t.Cleanup(func() { signInPollInterval, restartWaitTimeout = prevPoll, prevRestart }) - signInPollInterval, restartWaitTimeout = time.Millisecond, 20*time.Millisecond - t.Setenv("BROWSER", "none") - - const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" - const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` - 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 r.Method == http.MethodDelete { - _, _ = io.WriteString(rw, `{}`) - return - } - _, _ = io.WriteString(rw, active) - })) - t.Cleanup(server.Close) - w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) - - signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) - alreadySignedIn, urlShown, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Stderr: io.Discard}) - - require.Error(t, err) - assert.False(t, alreadySignedIn) - assert.False(t, urlShown, "no page is shown while the credential still reads as signed in") -} - // 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) { @@ -1343,149 +1163,3 @@ func TestServeSignInProgressNamesThePage(t *testing.T) { }) } } - -// TestServeSignInRestartSaysTheCredentialWasRemoved checks that a restart that removes the caller's sign-in says so on the -// sign-in's stderr before the page, since the agent's user did not ask for the sign-in to be removed. -func TestServeSignInRestartSaysTheCredentialWasRemoved(t *testing.T) { - prevPoll, prevWait := signInPollInterval, signInWaitTimeout - prevRestart := restartWaitTimeout - t.Cleanup(func() { - signInPollInterval, signInWaitTimeout, restartWaitTimeout = prevPoll, prevWait, prevRestart - }) - signInPollInterval, signInWaitTimeout, restartWaitTimeout = time.Millisecond, 10*time.Second, 10*time.Second - t.Setenv("BROWSER", "none") - - const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" - var mu sync.Mutex - deleted := false - var reads int - server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - mu.Lock() - defer mu.Unlock() - if r.URL.Path != credentialPath { - rw.WriteHeader(http.StatusNotFound) - return - } - rw.Header().Set("Content-Type", "application/json") - if r.Method == http.MethodDelete { - deleted = true - _, _ = io.WriteString(rw, `{}`) - return - } - // After the delete, the first two reads say NOT_FOUND, and the third, the first poll, says ACTIVE. - if !deleted { - _, _ = io.WriteString(rw, `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}`) - return - } - reads++ - if reads <= 2 { - 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 stderr bytes.Buffer - signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) {}, Stderr: &stderr}) - - require.NoError(t, err) - notice := testService + " did not accept the sign-in on profile " + testProfile + ", so it was removed; sign in again." - require.Contains(t, stderr.String(), notice) - assert.Less(t, strings.Index(stderr.String(), notice), strings.Index(stderr.String(), "Open this URL"), "the notice comes before the page") -} - -// TestServeSignInRestartWaitIsBounded checks that the wait for a restart's delete ends at restartWaitTimeout even when a -// read of the credential does not return, so the wait is not held up by the SDK's own retry budget. -func TestServeSignInRestartWaitIsBounded(t *testing.T) { - prevRestart := restartWaitTimeout - t.Cleanup(func() { restartWaitTimeout = prevRestart }) - restartWaitTimeout = 100 * time.Millisecond - t.Setenv("BROWSER", "none") - - const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" - server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - if r.URL.Path != credentialPath { - rw.WriteHeader(http.StatusNotFound) - return - } - if r.Method == http.MethodDelete { - _, _ = io.WriteString(rw, `{}`) - return - } - // A read that hangs until the client gives up. - select { - case <-r.Context().Done(): - case <-time.After(5 * time.Second): - } - })) - t.Cleanup(server.Close) - w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) - - start := time.Now() - signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) {}, Stderr: io.Discard}) - - require.Error(t, err) - assert.Less(t, time.Since(start), 3*time.Second, "the wait ends at its bound, not when the hung read returns") -} - -// TestServeSignInErrorsNameTheProfileAndFix checks that the errors of a sign-in that the authenticate tool answers name -// the profile and the fix, as the other sign-in errors do: a delete the profile may not make, and a wait that ends while -// the removed sign-in still reads as signed in. -func TestServeSignInErrorsNameTheProfileAndFix(t *testing.T) { - prevRestart := restartWaitTimeout - t.Cleanup(func() { restartWaitTimeout = prevRestart }) - restartWaitTimeout = 50 * time.Millisecond - t.Setenv("BROWSER", "none") - - const credentialPath = "/api/2.1/unity-catalog/mcp-services/system.ai.github/user-credentials" - const active = `{"name":"mcp-services/system.ai.github","provisioning_info":{"state":"ACTIVE"}}` - cases := map[string]struct { - deleteStatus int - deleteBody string - read string - want string - }{ - "delete refused": { - deleteStatus: http.StatusUnauthorized, - deleteBody: `{"error_code":"UNAUTHENTICATED","message":"Invalid access token."}`, - read: active, - want: "databricks auth login --profile " + testProfile, - }, - "credential still signed in": { - deleteStatus: http.StatusOK, - deleteBody: `{}`, - read: active, - want: "still reads as signed in; run: databricks aitools mcp login " + testService + " --profile " + testProfile, - }, - } - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - 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 r.Method == http.MethodDelete { - rw.WriteHeader(tc.deleteStatus) - _, _ = io.WriteString(rw, tc.deleteBody) - return - } - _, _ = io.WriteString(rw, tc.read) - })) - t.Cleanup(server.Close) - w := guardedTestClient(t, server.URL, &refreshGuard{authenticate: func(*http.Request) error { return nil }, profile: testProfile, cancel: func() {}}) - - signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Restart: true, Progress: func(string) {}, Stderr: io.Discard}) - - require.Error(t, err) - assert.Contains(t, err.Error(), tc.want) - }) - } -} diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index e84cff46eda..f67774e2119 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -38,9 +38,6 @@ var listChangedNotification = []byte(`{"jsonrpc":"` + jsonrpcVersion + `","metho type SignInRequest struct { // Open opens the sign-in page. It is false when the page must not open again in this session. Open bool - // Restart deletes the caller's sign-in before the sign-in runs, so that it starts from no credential. The proxy asks - // for it when the service challenged the session again after an "Already signed in" answer. - Restart 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) @@ -67,6 +64,9 @@ type Challenge struct { 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 @@ -93,9 +93,10 @@ type signInHooks struct { exposed bool // alreadySignedIn means the last authenticate answered "Already signed in". alreadySignedIn bool - // restart means the next sign-in starts from no credential. A challenge that follows an "Already signed in" answer - // sets it: the credential reads as signed in, but the service does not accept it. - restart 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 @@ -185,7 +186,7 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess } // 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 asks for a restart of the sign-in. +// 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() @@ -193,29 +194,29 @@ func (h *signInHooks) recordChallenge(method string) { h.callChallenged = method == methodToolsCall h.callPending = h.callChallenged if h.alreadySignedIn { - h.restart = true + 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, a pending restart, a call challenge. A tools/list answer ends the same state 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. +// 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(method string, message []byte) ([]byte, error) { h.stateMu.Lock() defer h.stateMu.Unlock() if method == methodToolsCall { h.alreadySignedIn = false - h.restart = false + h.stillChallenged = false h.callPending = false h.callChallenged = false return message, nil } if !h.callPending { h.alreadySignedIn = false - h.restart = false + h.stillChallenged = false } if !h.callChallenged { h.exposed = false @@ -339,7 +340,7 @@ func (h *signInHooks) authFailure() string { // 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{"tools": []any{h.authenticateToolDef()}}) + 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. @@ -380,11 +381,19 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R defer h.release() h.stateMu.Lock() - restart := h.restart - h.restart = false + 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, Restart: restart, Progress: report, Stderr: h.stderr}) + 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 @@ -392,10 +401,6 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R // The sign-in ended, so the next list is the service's own, unless it is challenged again. h.callChallenged = false } - if restart && !urlShown && err != nil { - // A restart that failed before its page was shown may not have deleted the credential, so it is asked for again. - h.restart = true - } h.stateMu.Unlock() if ctx.Err() != nil { // A cancelled request is not answered. @@ -419,6 +424,13 @@ func (h *signInHooks) authenticate(ctx context.Context, id, progressToken json.R } } +// 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 diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 083af50251a..f371d1f3e65 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -71,7 +71,7 @@ const loginCommand = `DATABRICKS_CONFIG_FILE='/work/cfg/my file.cfg' databricks // 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(&Challenge{Service: testService, Profile: "logfood", Host: testHost, Label: "GitHub", LoginCommand: "databricks aitools mcp login system.ai.github --profile logfood", SignIn: signIn}, io.Discard) + b.Hooks = newSignInHooks(testChallenge(signIn), io.Discard) return b } @@ -523,10 +523,10 @@ func TestChallengeTextAroundSignInURL(t *testing.T) { } } -// TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn checks that when the service challenges again after -// an "Already signed in" answer, the next authenticate starts a new sign-in from no credential. The credential reads -// ACTIVE, but the service does not accept it, so answering "Already signed in" again would loop. -func TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn(t *testing.T) { +// 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)) @@ -539,36 +539,17 @@ func TestAuthenticateStartsSignInAfterChallengeFollowingAlreadySignedIn(t *testi p.send(t, `{"jsonrpc":"2.0","id":2,"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)) - - reqs := signIn.requests() - require.Len(t, reqs, 2) - assert.False(t, reqs[0].Restart) - assert.True(t, reqs[1].Restart, "the sign-in after the challenge starts from no credential") - assert.Contains(t, lines[1], "Already signed in to system.ai.github") - assert.Equal(t, listChangedMsg, lines[2]) - assert.Contains(t, lines[4], "Signed in to system.ai.github") -} - -// TestAuthenticateStaleAnswerWithoutChallengeKeepsCredential checks that a second authenticate that is not preceded by a -// challenge does not delete the credential, so it answers "Already signed in" again. -func TestAuthenticateStaleAnswerWithoutChallengeKeepsCredential(t *testing.T) { - srv, _ := challengedService(t) - 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, 2) - p.send(t, fmt.Sprintf(authenticate, 6)) - p.waitLines(t, 3) + p.waitLines(t, 5) + p.send(t, fmt.Sprintf(authenticate, 8)) + lines := p.waitLines(t, 7) require.NoError(t, p.finish(t)) - for _, req := range signIn.requests() { - assert.False(t, req.Restart) - } + // 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 @@ -658,10 +639,10 @@ func listAnswer(challenged *atomic.Bool) http.HandlerFunc { } } -// TestChallengeAfterSuccessDoesNotRestartSignIn checks that a challenge long after an "Already signed in" answer does not -// start the next sign-in from no credential. A successful answer from the service ends that state, so only a challenge -// that follows the answer with no success in between does. -func TestChallengeAfterSuccessDoesNotRestartSignIn(t *testing.T) { +// 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)) @@ -679,66 +660,11 @@ func TestChallengeAfterSuccessDoesNotRestartSignIn(t *testing.T) { p.send(t, `{"jsonrpc":"2.0","id":7,"method":"tools/list"}`) p.waitLines(t, 5) p.send(t, fmt.Sprintf(authenticate, 8)) - p.waitLines(t, 6) + lines := p.waitLines(t, 6) require.NoError(t, p.finish(t)) - reqs := signIn.requests() - require.Len(t, reqs, 2) - assert.False(t, reqs[1].Restart, "a challenge after a success is not a challenge to the credential that was reported signed in") -} - -// TestRestartIsNotRepeatedAfterItsPageWasShown checks the restart that a challenge after "Already signed in" asks for. -// Once its page was shown the credential was deleted, and a sign-in cancelled after that does not delete again, since -// the user may finish the page that is still open. A restart whose page was never shown is asked for again. -func TestRestartIsNotRepeatedAfterItsPageWasShown(t *testing.T) { - for name, urlShown := range map[string]bool{"page shown": true, "page not shown": false} { - t.Run(name, func(t *testing.T) { - var challenged atomic.Bool - challenged.Store(true) - srv, _ := newFakeEndpoint(t, listAnswer(&challenged)) - entered, returned := make(chan struct{}), make(chan struct{}) - var mu sync.Mutex - var reqs []SignInRequest - signIn := func(ctx context.Context, req SignInRequest) (bool, bool, error) { - mu.Lock() - reqs = append(reqs, req) - n := len(reqs) - mu.Unlock() - switch n { - case 1: - return true, false, nil - case 2: - close(entered) - <-ctx.Done() - close(returned) - return false, urlShown, ctx.Err() - default: - return false, true, nil - } - } - p := startPiped(t, t.Context(), challengedBridge(srv.URL, 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)) - waitClosed(t, entered, "the restart to start") - p.send(t, `{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":7}}`) - waitClosed(t, returned, "the restart to return") - p.send(t, fmt.Sprintf(authenticate, 8)) - p.waitLines(t, 5) - require.NoError(t, p.finish(t)) - - mu.Lock() - defer mu.Unlock() - require.Len(t, reqs, 3) - assert.True(t, reqs[1].Restart, "the challenge after the answer asks for a restart") - assert.Equal(t, !urlShown, reqs[2].Restart) - }) - } + 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 @@ -939,10 +865,10 @@ func TestUnauthorizedIsAuthFailureUnlessItAsksForSignIn(t *testing.T) { } } -// TestRejectedCredentialDoesNotRestartSignIn checks the loop that a rejected Databricks token made: the credential reads as -// signed in, the service rejects the token with a 401, and a later authenticate used to delete the provider credential. -// The 401 is an auth failure, so no sign-in is restarted. -func TestRejectedCredentialDoesNotRestartSignIn(t *testing.T) { +// 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) @@ -965,12 +891,11 @@ func TestRejectedCredentialDoesNotRestartSignIn(t *testing.T) { p.send(t, `{"jsonrpc":"2.0","id":6,"method":"tools/list"}`) p.waitLines(t, 4) p.send(t, fmt.Sprintf(authenticate, 7)) - p.waitLines(t, 6) + lines := p.waitLines(t, 6) require.NoError(t, p.finish(t)) - reqs := signIn.requests() - require.Len(t, reqs, 2) - assert.False(t, reqs[1].Restart, "the rejected token does not restart the provider sign-in") + 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 @@ -1010,7 +935,7 @@ func TestOnlyTheResponseToTheRequestEndsTheSignInState(t *testing.T) { _, err = h.Result(t.Context(), []byte(tc.request), http.StatusOK, []byte(tc.challenge)) require.NoError(t, err) - assert.True(t, h.restart, "the challenge after the answer asks for a restart") + 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) @@ -1032,17 +957,25 @@ func TestOnlyTheResponseToTheRequestEndsTheSignInState(t *testing.T) { // 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", SignIn: signIn} + 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, + } } -// TestSuccessEndsAPendingRestart checks that a restart asked for by a challenge after "Already signed in" ends with the -// next success. The agent may not call authenticate before the service answers again, so a restart left pending would -// delete a working credential on a much later, unrelated challenge. -func TestSuccessEndsAPendingRestart(t *testing.T) { +// 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(int) (bool, error) { return true, nil }} + 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) @@ -1058,12 +991,12 @@ func TestSuccessEndsAPendingRestart(t *testing.T) { p.send(t, `{"jsonrpc":"2.0","id":8,"method":"tools/list"}`) p.waitLines(t, 6) p.send(t, fmt.Sprintf(authenticate, 9)) - p.waitLines(t, 8) + lines := p.waitLines(t, 8) require.NoError(t, p.finish(t)) - reqs := signIn.requests() - require.Len(t, reqs, 2) - assert.False(t, reqs[1].Restart, "a success after the challenge ends the restart it asked for") + 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 @@ -1171,7 +1104,7 @@ func TestUndecodableResponsePassesThrough(t *testing.T) { // 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 starts the sign-in again from no credential. +// the next challenge to a call gets the logout answer. func TestAlreadySignedInSurvivesListAfterCallChallenge(t *testing.T) { var challenged atomic.Bool challenged.Store(true) @@ -1191,29 +1124,28 @@ func TestAlreadySignedInSurvivesListAfterCallChallenge(t *testing.T) { 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)) - p.waitLines(t, 9) + lines = p.waitLines(t, 8) require.NoError(t, p.finish(t)) - reqs := signIn.requests() - require.Len(t, reqs, 2) - assert.True(t, reqs[1].Restart, "the challenge after the answer, with the list passing in between, asks for a restart") + 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", a pending restart and a call challenge all stay. +// 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.restart = 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, restart: true, callChallenged: true, callPending: true}, signInHooks{ - exposed: h.exposed, alreadySignedIn: h.alreadySignedIn, restart: h.restart, callChallenged: h.callChallenged, callPending: h.callPending, + 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, }) } From c48b65a9ca54327192c957034caaa0e152ef8421 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:25:41 +0000 Subject: [PATCH 51/55] List authenticate on the first page only, and keep a service's own authenticate tool A paginated list after a challenged tools/call gets the authenticate tool on its first page only. A list that already has a tool named authenticate keeps it alone, and calls to that tool go to the service. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 10 +-- libs/aitools/mcp/proxy/challenge.go | 56 ++++++++++++++--- libs/aitools/mcp/proxy/challenge_test.go | 78 ++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 12 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 6b59855fbb1..5b666cd00ec 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -221,14 +221,14 @@ func serveChallenge(ctx context.Context, t serveTarget, errOut io.Writer) (*prox return nil, nil } return &proxy.Challenge{ - Service: t.service, - Profile: t.profile, - Host: t.w.Config.Host, - Label: mcp.ProviderLabel(t.service), + 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), + SignIn: serveSignIn(t), }, nil } diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index f67774e2119..8f1f99dc6f6 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -91,6 +91,9 @@ type signInHooks struct { // 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 @@ -126,9 +129,9 @@ func (h *signInHooks) Local(ctx context.Context, request []byte, notify func([]b return nil, false } h.stateMu.Lock() - exposed := h.exposed + intercept := h.exposed && !h.ownAuthenticate h.stateMu.Unlock() - if !exposed { + if !intercept { return nil, false } var progressToken json.RawMessage @@ -176,7 +179,7 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess // 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(req.Method, message) + msg, err := h.succeeded(request, req.Method, message) if err != nil { return nil, err } @@ -193,6 +196,10 @@ func (h *signInHooks) recordChallenge(method string) { 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 @@ -204,7 +211,7 @@ func (h *signInHooks) recordChallenge(method string) { // 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(method string, message []byte) ([]byte, error) { +func (h *signInHooks) succeeded(request []byte, method string, message []byte) ([]byte, error) { h.stateMu.Lock() defer h.stateMu.Unlock() if method == methodToolsCall { @@ -222,11 +229,20 @@ func (h *signInHooks) succeeded(method string, message []byte) ([]byte, error) { h.exposed = false return message, nil } - return h.withAuthenticate(message) + return h.withAuthenticate(request, message) } -// withAuthenticate adds the authenticate tool to the tools of a tools/list result, which has a result. -func (h *signInHooks) withAuthenticate(msg []byte) ([]byte, error) { +// 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 @@ -239,6 +255,19 @@ func (h *signInHooks) withAuthenticate(msg []byte) ([]byte, error) { 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 @@ -252,6 +281,19 @@ func (h *signInHooks) withAuthenticate(msg []byte) ([]byte, error) { 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 +} + // upstreamReply is an upstream message as challenge detection reads it: its envelope, and its error or result. type upstreamReply struct { message diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index f371d1f3e65..65fe1253830 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1149,3 +1149,81 @@ func TestIsErrorResultChangesNoSignInState(t *testing.T) { 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"}`) + lines := 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]) +} From db50827822d3d5b1339832eceabb9a035c467901 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:39:18 +0000 Subject: [PATCH 52/55] Open the sign-in page with no stdin and a context the request cannot cancel The browser command of serve gets no stdin, since serve's stdin is the protocol stream, and runs with a context that the authenticate request's cancellation does not reach, so a cancelled call does not kill the browser. Open and OpenOutputToStderr keep their behavior. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 10 +++++++- cmd/aitools/mcp_serve_test.go | 48 +++++++++++++++++++++++++++++++++++ libs/browser/browser.go | 24 +++++++++++++----- libs/browser/browser_test.go | 40 +++++++++++++++++++++++++++++ libs/exec/exec.go | 12 ++++++++- 5 files changed, 125 insertions(+), 9 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 5b666cd00ec..454b834078a 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -245,6 +245,10 @@ func lookupAtStart(ctx context.Context, w *databricks.WorkspaceClient, profileNa 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. @@ -264,7 +268,11 @@ func serveSignIn(t serveTarget) proxy.SignInFunc { // The opener is called even when the page must not open, so a call that must not open it passes one that does nothing. openPage := func(context.Context, string) error { return nil } if req.Open { - openPage = browser.OpenOutputToStderr + // 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) }}) diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 86a36483c4e..21ee619d97b 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -1163,3 +1163,51 @@ func TestServeSignInProgressNamesThePage(t *testing.T) { }) } } + +// 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/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() From 92e04cd00ea81bb64233e40ee5882be38f36d4f6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:40:15 +0000 Subject: [PATCH 53/55] Wait for the last answers before the serve acceptance script closes stdin Co-authored-by: Isaac --- acceptance/cmd/aitools/mcp/serve-authenticate-tool/script | 3 +++ 1 file changed, 3 insertions(+) diff --git a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script index f881f8f4ba8..36e9cebedf5 100644 --- a/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script +++ b/acceptance/cmd/aitools/mcp/serve-authenticate-tool/script @@ -12,6 +12,7 @@ MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=sch # 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 @@ -21,6 +22,8 @@ MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=sch 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" From 185760c219594cd7f2712f0a113276556ea45562 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:41:22 +0000 Subject: [PATCH 54/55] Make the sign-in page opener optional, and tidy the serve helpers mcp.SignIn opens the page only when Open is set, as it already does for OnPoll, so serve no longer passes a no-op opener. The testserver's sign-in answer is a plain function again, the proxy's list uses the tools key constant, and the bridge's loop keeps its variable name. The sign-in test that sets no progress callback now sets one. Co-authored-by: Isaac --- cmd/aitools/mcp_serve.go | 4 ++-- cmd/aitools/mcp_serve_test.go | 2 +- libs/aitools/mcp/login.go | 10 +++++----- libs/aitools/mcp/login_test.go | 15 +++++++++++++++ libs/aitools/mcp/proxy/bridge.go | 4 ++-- libs/testserver/mcp_endpoint.go | 8 ++++---- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go index 454b834078a..f1531de44ca 100644 --- a/cmd/aitools/mcp_serve.go +++ b/cmd/aitools/mcp_serve.go @@ -265,8 +265,8 @@ func serveSignIn(t serveTarget) proxy.SignInFunc { workspaceID = id } url := mcp.SignInURL(w.Config.Host, service, workspaceID) - // The opener is called even when the page must not open, so a call that must not open it passes one that does nothing. - openPage := func(context.Context, string) error { return nil } + // 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. diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go index 21ee619d97b..17262c8ba2e 100644 --- a/cmd/aitools/mcp_serve_test.go +++ b/cmd/aitools/mcp_serve_test.go @@ -1099,7 +1099,7 @@ func TestServeSignInTransientFailureKeepsServing(t *testing.T) { w := guardedTestClient(t, server.URL, guard) signIn := serveSignIn(serveTarget{w: w, profile: testProfile, service: testService}) - _, _, err := signIn(t.Context(), proxy.SignInRequest{Open: true, Stderr: io.Discard}) + _, _, 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") diff --git a/libs/aitools/mcp/login.go b/libs/aitools/mcp/login.go index d68d30781ce..b3a397475db 100644 --- a/libs/aitools/mcp/login.go +++ b/libs/aitools/mcp/login.go @@ -21,8 +21,8 @@ const ( SignInWaitTimeout = 5 * time.Minute ) -// SignInTimeoutError means the caller's credential did not become ACTIVE before the wait ended. LastCheck is the failure -// of the last poll, when that poll failed; a poll that reads the credential clears it. +// SignInTimeoutError means the caller's credential did not become ACTIVE before the wait ended. LastCheck is the +// failure of the last poll, when that poll failed; a poll that reads the credential clears it. type SignInTimeoutError struct { LastCheck error } @@ -41,8 +41,8 @@ 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. OnPoll, when set, is called on each poll of the wait. +// 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 @@ -99,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 diff --git a/libs/aitools/mcp/login_test.go b/libs/aitools/mcp/login_test.go index 894e91d8030..7f287c4d3e9 100644 --- a/libs/aitools/mcp/login_test.go +++ b/libs/aitools/mcp/login_test.go @@ -580,3 +580,18 @@ func TestSignInCallsOnPollOnEachPoll(t *testing.T) { 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 6a03cf04099..4e90eddf0cb 100644 --- a/libs/aitools/mcp/proxy/bridge.go +++ b/libs/aitools/mcp/proxy/bridge.go @@ -880,8 +880,8 @@ func (x *exchange) lines(reqCtx context.Context, msg []byte) ([][]byte, error) { // no lines. func oneLines(msgs [][]byte) ([][]byte, error) { lines := make([][]byte, 0, len(msgs)) - for _, msg := range msgs { - line, err := oneLine(msg) + for _, out := range msgs { + line, err := oneLine(out) if err != nil { return nil, err } diff --git a/libs/testserver/mcp_endpoint.go b/libs/testserver/mcp_endpoint.go index e397c3de2e3..2b7323920d7 100644 --- a/libs/testserver/mcp_endpoint.go +++ b/libs/testserver/mcp_endpoint.go @@ -67,7 +67,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { }) case "tools/list": if challenged { - return s.signInRequired(msg.ID) + return signInRequired(msg.ID) } return rpcResult(msg.ID, map[string]any{"tools": []any{ map[string]any{ @@ -81,7 +81,7 @@ func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { }}) case "tools/call": if challenged { - return s.signInRequired(msg.ID) + return signInRequired(msg.ID) } if msg.Params.Name != fakeMcpEchoTool { return Response{Body: rpcError(msg.ID, rpcCodeInvalidParams, "Unknown tool: "+msg.Params.Name)} @@ -137,8 +137,8 @@ func rpcError(id json.RawMessage, code int, message string) map[string]any { } } -// signInRequired answers with the gateway's -32006 challenge. -func (s *FakeWorkspace) signInRequired(id json.RawMessage) Response { +// signInRequired is the answer to a request for an external service, which the gateway gives before sign-in. +func signInRequired(id json.RawMessage) Response { return Response{Body: rpcError(id, rpcCodeSignInRequired, "The MCP service request was rejected (HTTP 401). Sign-in is required.")} } From 6b415917f543d7765b9da0d41bab283dd726f0aa Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 09:45:09 +0000 Subject: [PATCH 55/55] Read upstream replies through one helper Co-authored-by: Isaac --- libs/aitools/mcp/proxy/challenge.go | 14 ++++++++++++-- libs/aitools/mcp/proxy/challenge_test.go | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/libs/aitools/mcp/proxy/challenge.go b/libs/aitools/mcp/proxy/challenge.go index 8f1f99dc6f6..a1959a18e49 100644 --- a/libs/aitools/mcp/proxy/challenge.go +++ b/libs/aitools/mcp/proxy/challenge.go @@ -155,10 +155,10 @@ func (h *signInHooks) Result(_ context.Context, request []byte, status int, mess if req.Method != methodToolsList && req.Method != methodToolsCall && status != http.StatusUnauthorized { return [][]byte{message}, nil } - var reply upstreamReply // 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. - if json.Unmarshal(message, &reply) != nil || !reply.isResponseTo(req) { + reply, ok := readReply(message, req) + if !ok { return [][]byte{message}, nil } challenged := h.challenged(&reply) @@ -294,6 +294,16 @@ func isFirstPage(request []byte) (bool, error) { 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 diff --git a/libs/aitools/mcp/proxy/challenge_test.go b/libs/aitools/mcp/proxy/challenge_test.go index 65fe1253830..0a399b0e1bb 100644 --- a/libs/aitools/mcp/proxy/challenge_test.go +++ b/libs/aitools/mcp/proxy/challenge_test.go @@ -1181,9 +1181,9 @@ func TestCallChallengeListsAuthenticateOnFirstPageOnly(t *testing.T) { 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) + p.waitLines(t, 3) p.send(t, `{"jsonrpc":"2.0","id":5,"method":"tools/list","params":{"cursor":"page-2"}}`) - lines = p.waitLines(t, 4) + lines := p.waitLines(t, 4) require.NoError(t, p.finish(t)) assert.Equal(t, []string{"echo", "authenticate"}, toolNames(t, lines[2]))