Repository navigation
Conversation
Coalesce refreshes for the current token source and let each OAuth caller wait with its own context. Wake waiters when a newer authorization supplies a token and keep bearer lookup cancellable across the stdio GitHub clients.
There was a problem hiding this comment.
🟡 Changes recommended
The tool guard lacks a cancellation regression test, leaving ignored HasTokenContext errors or use of a non-cancelable context undetected.
1 open issue
What changed in this PR
Makes stdio OAuth token-refresh waits cancellable while allowing the shared refresh to finish and preserve rotated credentials for the session.
Changes:
- Shares in-flight refreshes and wakes waiters when a newer authorization installs a token.
- Passes request contexts through the tool guard and REST, GraphQL, and raw-content token lookups.
- Adds cancellation and transport regression tests, and documents refresh ownership.
| File | Description |
|---|---|
internal/oauth/refresh_cancellation_test.go |
Tests cancellation, shared refreshes, and authorization replacement. |
internal/oauth/manager.go |
Adds context-aware token waits and shared-refresh coordination. |
internal/ghmcp/server.go |
Connects request contexts to outbound OAuth bearer lookups. |
internal/ghmcp/server_test.go |
Updates client setup for the OAuth provider parameter. |
internal/ghmcp/oauth.go |
Makes the tool guard’s token check cancellable. |
internal/ghmcp/oauth_test.go |
Updates authentication fakes and client setup. |
internal/ghmcp/oauth_refresh_transport_test.go |
Tests request cancellation, body closure, and host-scoped bearer handling. |
docs/oauth-login.md |
Explains shared refreshes and canceled request waits. |
Review effort: Max (adaptive)
| hasToken, err := mgr.HasTokenContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } |
There was a problem hiding this comment.
Addressed in 1419bf09. I added a regression test for the OAuth tool guard that passes an already-canceled request context and verifies that the guard returns context.Canceled with a nil result, skips authentication, and never calls the tool handler.
I verified that the test fails if the guard drops the token-check error or substitutes context.Background() for the request context. The full Windows Go suite, Linux race suite, and lint passed.

Summary
Let canceled stdio MCP requests stop waiting for an OAuth token refresh. Concurrent requests can still receive the result of the same refresh and reuse the rotated credentials.
Why
When the session token expired, the stdio tool guard called
HasToken, which synchronously calledoauth2.TokenSource.Token. Authorization resume and the outbound bearer provider could enter the same path. The source uses an independent HTTP context for refresh. That is appropriate for the shared exchange, but it also meant a canceled caller could not return until the exchange released it.A gated token source reproduced the problem on
df32d7ec5786. BothAuthenticateandAwaitTokenremained blocked after cancellation while the refresh was held. An already-canceled call entered the source and returned success instead ofcontext.Canceled. Installing a newer authorization also left waiters blocked on the old source.What changed
MCP impact
Prompts tested (tool changes only)
The authorization tests use synthetic tokens and a gated refresh source. The outbound tests use the configured clients against a local HTTP fixture for REST, GraphQL, and raw content.
Security / limits
Tool renaming
Lint & tests
./script/linton Linux with golangci-lint v2.14.0../script/test(go test -race ./...) on Linux.go test -v ./..., the snapshot suite withUPDATE_TOOLSNAPS=true, and a CLI build. The verbose suite passed 32 packages, 1,225 top-level tests, and 7,997 subtests. The new OAuth manager and stdio transport tests each passed 100 consecutive runs. The tagged end-to-end package compiled with execution disabled.go mod tidy -diff, a CLI build, and regenerated documentation validation.Docs
Skills
Related work includes the stdio OAuth introduction, multi-round-trip OAuth migration, and GitHub's refresh token rotation documentation.
AI assistance was used to investigate and reproduce the defect, author the fix and tests, review the diff, and draft this description.