feat(oauth): make proactive token refresh window configurable (--oauth-refresh-leeway)#57
Merged
Merged
Conversation
The 60 s leeway in ensure_token() was hard-coded; tune via the new --oauth-refresh-leeway SECONDS flag (or MCP_OAUTH_REFRESH_LEEWAY env var). Default 60 s preserves existing behaviour. Negative values are rejected at parse time. Use cases (cf. mcp-remote#252): - ASes whose refresh token lifetime is barely longer than the access token's — the default 60 s margin can race expiry; raise leeway for clock-skew tolerance. - Deployments with extremely short access tokens — drop leeway towards 0 to keep cache hits useful. Cross-linked in WORKAROUNDS.md alongside typescript-sdk#1954, which documents the upstream side of the bug (no leeway check at all). Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- Replace ineffective patch.dict('os.environ', {}, clear=False) with
monkeypatch.delenv for proper teardown
- Pass env var value as string to argparse so invalid values surface as
argparse errors instead of a raw Python ValueError on startup
- Add test for invalid env var value (covers the new error path)
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
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.
Fixes #56
Summary
--oauth-refresh-leeway SECONDS(default60, env varMCP_OAUTH_REFRESH_LEEWAY)ensure_token()cache-validity checkWhy
mcp-stdio already does proactive token refresh —
ensure_token()treats acached access token as expired when its real expiry is within 60 s. This is
fine for most ASes, but a real-world deployment (mcp-remote#252)
issues refresh tokens that outlive the access token by only ~1 s. Under those
conditions, even mcp-stdio's 60 s window is too generous: the refresh races
the AS revocation. Make the window tunable.
A related upstream bug is typescript-sdk#1954
—
adaptOAuthProvider.token()returns expired tokens with no leeway check atall. mcp-stdio is structurally immune to that side; this PR is purely about
exposing configurability of the leeway we already have.
Scope
src/mcp_stdio/oauth.py—ensure_token()gainsrefresh_leeway: float = 60.0src/mcp_stdio/cli.py— new flag + env var +_non_negative_floatargparse typerelay.py(the 401-reactive refresh path) — untouched, leeway is proactive onlyTest plan
pytest tests/ -v— 367 tests pass (+7 new)TestEnsureToken: leeway=0 / leeway=300 / default leeway=60TestMain: default / flag / env var / negative-rejectionmcp-stdio --helpshows the new flagmcp-stdio --oauth-refresh-leeway -1 ...exits with argparse error🤖 Generated with Claude Code