Title: fix(client/auth): use stored refresh_token on 401 instead of full re-authorization by beraterkanelcelik · Pull Request #2875 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix(client/auth): use stored refresh_token on 401 instead of full re-authorization by beraterkanelcelik · Pull Request #2875 · modelcontextprotocol/python-sdk
X Title: fix(client/auth): use stored refresh_token on 401 instead of full re-authorization by beraterkanelcelik · Pull Request #2875 · modelcontextprotocol/python-sdk
Description: Problem When an OAuthClientProvider is reconstructed and loads previously stored tokens via TokenStorage, a stored refresh_token is never used — every access-token expiry forces a full interactive re-authorization. Root cause, in async_auth_flow (src/mcp/client/auth/oauth2.py): _initialize() loads current_tokens from storage but does not restore token_expiry_time (it stays None). OAuthToken.expires_in is relative, so a reloaded provider has no way to know the token's absolute expiry. With token_expiry_time is None, is_token_valid() returns True for the loaded token regardless of its real state, so the proactive-refresh branch if not is_token_valid() and can_refresh_token() is skipped. The server then rejects the stale access token with 401, but the 401 branch proceeds directly to a full authorization-code grant. It never attempts a refresh_token grant — even when a valid refresh_token and client_info are present in storage. This is especially visible for clients that build a fresh provider per request and rely on persisted tokens: they re-authorize interactively on every access-token expiry instead of refreshing silently. Fix In the 401 branch of async_auth_flow, after metadata discovery and before client registration / authorization, attempt a refresh_token grant when can_refresh_token() is true. The discovery already performed in that branch resolves the token endpoint, so the stored refresh_token can be used directly. If the refresh fails, the flow falls back to the existing full re-authorization path. Tests Adds to tests/client/test_auth.py: test_initialize_does_not_restore_token_expiry — documents the precondition (is_token_valid() is True after _initialize() loads a stored token, because expiry isn't restored). test_reloaded_provider_uses_refresh_token_on_401 — drives the full reload → 401 → refresh path and asserts a refresh_token grant is issued (not a new authorization) and the original request is retried with the refreshed token. test_reloaded_provider_falls_back_to_reauth_when_refresh_fails — asserts the flow falls back to full re-authorization when the refresh grant fails. All existing auth tests continue to pass.
Open Graph Description: Problem When an OAuthClientProvider is reconstructed and loads previously stored tokens via TokenStorage, a stored refresh_token is never used — every access-token expiry forces a full interactive ...
X Description: Problem When an OAuthClientProvider is reconstructed and loads previously stored tokens via TokenStorage, a stored refresh_token is never used — every access-token expiry forces a full interactive ...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2875
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:1c3f91dc-edb2-9a92-faea-aad2705b6634 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D6CE:1C94F:BAEA87:EE68CD:6B013834 |
| html-safe-nonce | a2c45c837a7851eefb952f4b0d8a094aac54045bbc54af1cafa055092ba7e255 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENkNFOjFDOTRGOkJBRUE4NzpFRTY4Q0Q6NkIwMTM4MzQiLCJ2aXNpdG9yX2lkIjoiODU0ODk3ODQ4NTc0MDI1NTI4NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 00c0c70ab5b8bf9a0b08b8e885d2ca4aa4fee3b3d0461ea4fb1e0aa127e6093d |
| hovercard-subject-tag | pull_request:3867668998 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2875/files |
| twitter:image | https://avatars.githubusercontent.com/u/74668890?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/74668890?s=400&v=4 |
| og:image:alt | Problem When an OAuthClientProvider is reconstructed and loads previously stored tokens via TokenStorage, a stored refresh_token is never used — every access-token expiry forces a full interactive ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width