go-sdk/internal
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Thomas Bechtold d9728a88f3
auth: bind refreshing token source to a background context, not the request ctx (#988)
## Problem

`AuthorizationCodeHandler.exchangeAuthorizationCode` builds the
long-lived token source from the **request context** passed to
`Authorize()`:

```go
clientCtx := context.WithValue(ctx, oauth2.HTTPClient, h.config.Client)
...
h.tokenSource = cfg.TokenSource(clientCtx, token)
```

`golang.org/x/oauth2` captures that context and reuses it for **every**
future refresh:
- `oauth2.go`: `Config.TokenSource(ctx, …)` stores `ctx` in
`tokenRefresher{ctx}`
- `tokenRefresher.Token()` (no ctx arg) calls `retrieveToken(tf.ctx, …)`
- `internal/token.go`: `ContextClient(ctx).Do(req.WithContext(ctx))`

`Authorize()` is normally invoked from a request- or connect-scoped
context — e.g. the streamable client transport calls it during
`setMCPHeaders`/`initialize`, and that context is cancelled once the
operation completes. The cached access token works until expiry; the
**first refresh after expiry** then fails instantly with `context
canceled`, before any HTTP request is sent. The connection can no longer
refresh.

`setMCPHeaders` only re-invokes `Authorize()` for an `invalid_grant`
`oauth2.RetrieveError` (#917). A `context canceled` is not a
`RetrieveError`, so it is never recovered — the connection is wedged
until the client reconnects.

### Reproduce

1. Connect a `StreamableClientTransport` with an
`AuthorizationCodeHandler` to a server whose `initialize` returns 401
(so `Authorize()` runs during connect, under the connect context).
2. Let the connect context be cancelled after connect returns (the
common pattern: a `context.WithTimeout(parent, …)` + `defer cancel()`
bounding the interactive flow).
3. Wait for the access token to expire and issue a request.
4. Every request fails with `Post "<token endpoint>": context canceled`
and `dur≈0` — the refresh never hits the network.

## Fix

A token source outlives the request that created it, so bind its
refreshes to `context.Background()` (still carrying the configured HTTP
client via `oauth2.HTTPClient`). The one-shot token exchange keeps using
the request context.

```go
refreshCtx := context.WithValue(context.Background(), oauth2.HTTPClient, h.config.Client)
h.tokenSource = cfg.TokenSource(refreshCtx, token)
```

`auth` package tests pass. Behaviour is otherwise unchanged: the same
HTTP client value is carried; only the cancellation parent differs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Guglielmo Colombo <guglielmoc@google.com>
2026-06-16 16:42:53 +02:00
..
authutil mcp: add optional issuer validator for pre-registered client validation (SEP-2352) (#946) 2026-05-29 12:46:34 +00:00
docs mcp: tool.InputSchema and tool.OutputSchema validation (SEP-2106) (#1009) 2026-06-16 13:55:47 +02:00
json oauthex: use internal JSON library for decoding. (#866) 2026-03-30 14:52:15 +02:00
jsonrpc2 fix(jsonrpc2): decode requests when method key is present (#1000) 2026-06-16 09:57:18 +02:00
mcpgodebug mcp: update JSON marshaling to not HTML-escape messages (#769) 2026-01-30 17:13:30 +01:00
oauthtest auth: bind refreshing token source to a background context, not the request ctx (#988) 2026-06-16 16:42:53 +02:00
readme Add documentation backed by mkdocs (#883) 2026-04-09 14:25:43 +02:00
util internal: remove unused util functions (#871) 2026-03-31 15:40:06 +02:00
xcontext x/tools: modernize interface{} -> any 2025-02-19 15:14:27 -08:00