Repository navigation
Description
Describe the bug
I am using mcp.StreamableClientTransport with an OAuthHandler. As documented, this handler will automatically be invoked by go-sdk on e.g. a 401 from the MCP endpoint.
My client implement an interactive OAuth2 authorization flow using the newly exposed APIs in v1.5.0-pre.
The behaviour I am seeing that I believe to be a bug is this:
- I configure a new MCP server endpoint in my client config.
- My client tries to use the MCP server via the
StreamableClientTransport. - It receives a 401 and calls
Authorize()on myOAuthHandler. - The context passed to
Authorize()times out. - In the switch statement in
call(), the execution flow enters the case wherectx.Err() != nil. Here,conn.Notifyis called. ThisNotifycall, of course, receives a 401 which then callsAuthorize()on my handler again.
In the case of my client, this prompts the user for interactive authorization again, even though the timeout has already cancelled the authorization request.
Relevant code is here:
Lines 213 to 223 in 862d78a
| call := conn.Call(ctx, method, params) | |
| err := call.Await(ctx, result) | |
| switch { | |
| case errors.Is(err, jsonrpc2.ErrClientClosing), errors.Is(err, jsonrpc2.ErrServerClosing): | |
| return fmt.Errorf("%w: calling %q: %v", ErrConnectionClosed, method, err) | |
| case ctx.Err() != nil: | |
| // Notify the peer of cancellation. | |
| err := conn.Notify(xcontext.Detach(ctx), notificationCancelled, &CancelledParams{ | |
| Reason: ctx.Err().Error(), | |
| RequestID: call.ID().Raw(), | |
| }) |
To Reproduce
Steps to reproduce the behavior:
- Set up a
StreamableClientTransportwith anOAuthHandler. - Use the transport without setting up authorization.
- Pass a context with a short timeout to
Authorize(), and wait for it to expire before returning.
Expected behavior
I would expect cancelling the context passed to Authorize() to indicate that the connection should be abandoned and not to use it for any further communication.
Logs
n/a
Additional context
n/a
Activity
ravyg commentedon Apr 8, 2026
I tracked through this and the bug needs a fix in two layers. Posting here for visibility before I open a PR.
Root cause
The
Notify(notificationCancelled)inmcp/transport.go:call()is unconditional. When the originalAwaitfails because the user'sOAuthHandler.Authorize()returned a cancelled context, the SDK still tries to send a "cancelled" notification back to the peer — but that notification has to travel through the sameauth.HTTPTransport, which has noTokenSourcestored, gets a fresh 401, and re-invokesAuthorize(). The user sees a second prompt for the request they just abandoned.Proposed fix (two parts, defense-in-depth)
Part 1 —
mcp/transport.gocall()Bound the cancellation
Notifywith a freshcontext.Background()-derived context so it can never block the caller's return path on a degraded connection.Part 2 —
auth/client.goHTTPTransportTrack the last handler error. When
Authorize()returns a context-cancelled error, store it. SubsequentRoundTripcalls short-circuit instead of re-invoking the handler — directly enforcing the expectation in the bug report that "cancelling Authorize should indicate the connection should be abandoned."Why both parts
Part 1 prevents the hang, Part 2 prevents the re-prompt. They're independent.
Behavioral impact
Test plan
OAuthHandlerthat records invocation count and returnsctx.Err()Initializewith a tight deadline → assert handler invoked exactly once, caller returns within bounded timeHTTPTransportafter the failed one works normallyWill open a PR shortly.
mcp: do not re-prompt OAuth after cancelled Authorize
mcp: do not re-prompt OAuth after cancelled Authorize
mcp: do not re-prompt OAuth after cancelled Authorize
mcp: do not re-prompt OAuth after cancelled Authorize
mcp: do not re-prompt OAuth after cancelled Authorize
mcp: do not re-prompt OAuth after cancelled Authorize (modelcontextpr…