Repository navigation
fix(oauth): treat zero ExpiresAt as never-expires in HasPersistedToken - #453
Merged
Merged
Conversation
Authorization servers that omit `expires_in` from the /token response (e.g. Atlassian's MCP endpoint at https://mcp.atlassian.com/v1/mcp) land in BBolt with ExpiresAt = time.Time{}. HasValidToken and mcp-go's own Token.IsExpired() already treat that as "never expires", but HasPersistedToken was computing `time.Now().After(time.Time{})` which is always true — so the persisted token was reported as expired on every reconnect, skipBrowserFlow stayed false, and the upstream looped forever asking for re-auth even though a valid access token sat in storage. Fix HasPersistedToken to short-circuit on IsZero(), matching HasValidToken. Also clean up PersistentTokenStore.GetToken so it no longer logs a spurious "token has expired" warning for those records. Add three unit tests covering the zero / future / past ExpiresAt cases against HasPersistedToken so the IsZero branch can't regress.
Merged
3 tasks done
electrolobzik
added a commit
to HaloCollar/mcpproxy-go
that referenced
this pull request
May 12, 2026
#2) Fixes OAuth re-auth loop against authorization servers that omit `expires_in` from /token responses (e.g. Atlassian's MCP endpoint). HasPersistedToken was computing `time.Now().After(time.Time{})` which is always true, so a zero ExpiresAt was reported as expired and the upstream looped asking for re-auth on every reconnect even with a valid bearer token persisted. Fix: short-circuit on IsZero() to match HasValidToken's existing semantics (and mcp-go's own Token.IsExpired). Clean up PersistentTokenStore.GetToken logging for the same case. +3 unit tests covering zero/future/past expiries. Mirror of upstream PR smart-mcp-proxy#453.
Member
|
Thanks Roman! 🙏 Great catch on the asymmetry between Merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix an OAuth re-auth loop that breaks any upstream whose authorization server omits
expires_infrom its/tokenresponse (observed concretely against Atlassian's MCP endpoint athttps://mcp.atlassian.com/v1/mcp).The authorization succeeds end-to-end — the callback is received, the token is exchanged, the record lands in BBolt — but on every subsequent reconnect the upstream emits:
and drops back to the same defer-OAuth-for-tray path it would take if no token existed.
Root cause
HasPersistedToken(internal/oauth/config.go) computedisExpired = time.Now().After(record.ExpiresAt). When the auth server doesn't returnexpires_in, the oauth2 library leavesExpiresAt = time.Time{}(the Go zero value), andtime.Now().After(time.Time{})is always true. The persisted token therefore looked expired on every reconnect,skipBrowserFlowstayed false in all fourHasPersistedTokencallers ininternal/upstream/core/connection_oauth.go(HTTP precheck/post-check, SSE precheck/post-check), and the upstream looped forever asking for re-auth even though a valid bearer token was sitting in storage.The asymmetry was the bug:
HasValidToken(config.go:299-304) and mcp-go's ownToken.IsExpired()(client/transport/oauth.go:82-86in v0.44.0-beta.2) both already treatIsZero()as "never expires".HasPersistedTokenwas the lone outlier.Changes
HasPersistedToken: short-circuit onrecord.ExpiresAt.IsZero()to match the rest of the stack.PersistentTokenStore.GetToken: stop logging "OAuth token has expired and needs refresh" for zero-expiry tokens, and stop computing the negative grace-period adjustment that path produced — the existingadjustedExpiresAt := record.ExpiresAtfallback already kept the zero, so the token is returned unchanged for the underlyingclient.Token.HasPersistedTokencovering zero / future / past expiries, so the IsZero branch can't regress.No migration needed — existing records that were silently broken just start working on the next reconnect.
Test plan
go test ./internal/oauth/— 14.9 s, all greengo test ./internal/upstream/...— all greenTestHasPersistedToken_ZeroExpiresAtreproduces the Atlassian scenario and passesTestTokenStoreManager_HasValidToken_PersistentStore_NoExpirationstill passes (no regression in the symmetric helper)https://mcp.atlassian.com/v1/mcpwith a patched binary — the upstream stays connected across restarts after a single OAuth login🤖 Generated with Claude Code