Skip to content

fix(oauth): treat zero ExpiresAt as never-expires in HasPersistedToken - #453

Merged
Dumbris merged 1 commit into
smart-mcp-proxy:mainfrom
HaloCollar:fix/oauth-zero-expiry
May 12, 2026
Merged

Dumbris merged 1 commit into
smart-mcp-proxy:mainfrom
HaloCollar:fix/oauth-zero-expiry

Conversation

@electrolobzik

Copy link
Copy Markdown
Contributor

Summary

Fix an OAuth re-auth loop that breaks any upstream whose authorization server omits expires_in from its /token response (observed concretely against Atlassian's MCP endpoint at https://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:

core/connection_oauth.go | MCP initialize JSON-RPC call failed | error: transport error: failed to send request: no valid token available, authorization required
managed/client.go        | all authentication strategies failed, last error: OAuth authentication required for <server>

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) computed isExpired = time.Now().After(record.ExpiresAt). When the auth server doesn't return expires_in, the oauth2 library leaves ExpiresAt = time.Time{} (the Go zero value), and time.Now().After(time.Time{}) is always true. The persisted token therefore looked expired on every reconnect, skipBrowserFlow stayed false in all four HasPersistedToken callers in internal/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 own Token.IsExpired() (client/transport/oauth.go:82-86 in v0.44.0-beta.2) both already treat IsZero() as "never expires". HasPersistedToken was the lone outlier.

Changes

  • HasPersistedToken: short-circuit on record.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 existing adjustedExpiresAt := record.ExpiresAt fallback already kept the zero, so the token is returned unchanged for the underlying client.Token.
  • Three new unit tests against HasPersistedToken covering 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 green
  • go test ./internal/upstream/... — all green
  • New TestHasPersistedToken_ZeroExpiresAt reproduces the Atlassian scenario and passes
  • Existing TestTokenStoreManager_HasValidToken_PersistentStore_NoExpiration still passes (no regression in the symmetric helper)
  • Reproduced fix end-to-end against https://mcp.atlassian.com/v1/mcp with a patched binary — the upstream stays connected across restarts after a single OAuth login

🤖 Generated with Claude Code

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.
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.
@Dumbris

Dumbris commented May 12, 2026

Copy link
Copy Markdown
Member

Thanks Roman! 🙏 Great catch on the asymmetry between HasPersistedToken and HasValidToken / mcp-go's own IsZero() semantics — that was a sneaky one, and the Atlassian repro made it concrete. The fix is tight, the three regression tests (zero / future / past expiry) cover exactly the right triangle, and the log-message switch in PersistentTokenStore.GetToken is a nice cleanup.

Merging.

@Dumbris
Dumbris merged commit a11d002 into smart-mcp-proxy:main May 12, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants