Title: fix(client/auth): discover AS metadata before cold-start token refresh, with issuer-binding check by claude[bot] · Pull Request #3263 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix(client/auth): discover AS metadata before cold-start token refresh, with issuer-binding check by claude[bot] · Pull Request #3263 · modelcontextprotocol/python-sdk
X Title: fix(client/auth): discover AS metadata before cold-start token refresh, with issuer-binding check by claude[bot] · Pull Request #3263 · modelcontextprotocol/python-sdk
Description: Requested by Felix Weinberger · Slack thread NoteAI disclosure: this PR was authored by Claude (an AI agent operated by the MCP maintainer team's triage workflow, at Felix Weinberger's request — see attribution above). Fixes #3240. Together with #3248 (which restores token expiry on load), resolves #3250. Problem On a cold start — a client restarting with a stored refresh token, before any 401 has triggered discovery — the eager pre-401 refresh in OAuthClientProvider.async_auth_flow builds its token URL from the urljoin(origin, "/token") fallback because context.oauth_metadata has not been discovered yet. Against servers whose token endpoint lives under a path (e.g. https://auth.example.com/oauth2/api/v1/token), the refresh POSTs to the wrong URL, gets a 404, _handle_refresh_response clears the stored tokens, and the client falls back to a full interactive re-auth on every process restart — which headless/gateway clients cannot perform (#3240, #3250). Fix Run OAuth discovery before the cold-start refresh, driven through the same httpx auth flow (no side-channel client): PRM discovery (SEP-985 well-known fallbacks; there is no 401 yet, so no WWW-Authenticate resource_metadata hint). ASM discovery (RFC 8414 fallbacks, SEP-2468 issuer validation) → the real token endpoint. Refresh, now targeting the discovered token endpoint. Because this discovery is unanchored (blind well-known probes — a co-hosted origin can legitimately serve some other resource's documents), its results are treated as best-effort and never destructive or fatal, unlike the 401 path's hint-anchored, authoritative discovery: A resource-mismatched PRM or an issuer-mismatched ASM counts as a failed discovery (skip, try the next URL) instead of raising out of the auth flow. On a SEP-2352 issuer-binding mismatch — the gap that led the author of #3241 to close it after cubic's P1 review comment — the eager refresh is skipped, so stored credentials are never presented to an unvalidated authorization server; the unanchored discovery results (including rejected ASM metadata) are discarded, and the credentials themselves are left for the anchored 401 path to judge — that path re-discovers with the server's hint and drops/re-registers only on a confirmed AS change. Servers publishing no metadata at all keep the exact previous behavior (refresh against {origin}/token), and the probes run only once per context (eager_discovery_attempted, recorded only when the probe sequence completes so an interrupted discovery is retried), so repeat in-process refreshes make no extra requests. When metadata is already known (normal in-process refresh), nothing changes — no extra requests. The inner refresh generator is driven under contextlib.aclosing, so it is finalized deterministically when httpx closes the auth flow mid-refresh. This follows the structure of #3241 (closed by its author) and addresses the issuer-binding concern it was missing, with strictly conservative semantics on the hint-less path. Tests Ten new anyio tests in tests/client/test_auth.py: cold-start refresh targets the discovered (pathful) token endpoint, then retries the original request with the refreshed bearer; legacy server with no metadata anywhere keeps the {origin}/token fallback, and a failed refresh still clears tokens; the discovery probes run only once per context — a second expiry refresh goes straight to the fallback; an interrupted probe sequence is not recorded as completed and is retried on the next refresh; a foreign (resource-mismatched) PRM from a blind probe is skipped as failed discovery; an issuer-mismatched ASM (SEP-2468) is skipped as failed discovery instead of failing the request; a non-4XX ASM discovery error stops the fallback chain (mirrors the 401 path); issuer-binding mismatch after PRM discovery skips the refresh while keeping credentials/tokens for the 401 path (cubic's P1 scenario, non-destructively); issuer-binding mismatch on the legacy no-PRM path discards the rejected ASM metadata (mirroring the 401 path's defensive clear); metadata already known → immediate refresh, no discovery requests. ruff format/ruff check clean, pyright strict 0 errors, full local test suite passes with 100% branch coverage on the changed files (tests/interaction/auth/test_lifecycle.py continues to cover the metadata-already-known refresh and failed-refresh paths).
Open Graph Description: Requested by Felix Weinberger · Slack thread NoteAI disclosure: this PR was authored by Claude (an AI agent operated by the MCP maintainer team's triage workflow, at Felix Weinberger's requ...
X Description: Requested by Felix Weinberger · Slack thread NoteAI disclosure: this PR was authored by Claude (an AI agent operated by the MCP maintainer team's triage workflow, at Felix Weinberger...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/3263
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:a56007a8-066f-a851-2c55-2af2d695dee9 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E7F0:842EC:15BC5E0:1D01A7C:6B013963 |
| html-safe-nonce | c6d67d714917b3bd3526533c3c68f4cb579b0aa8940496a8aab23193208fa636 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFN0YwOjg0MkVDOjE1QkM1RTA6MUQwMUE3Qzo2QjAxMzk2MyIsInZpc2l0b3JfaWQiOiIxODgxNDkxNzQ1Njk2MTM2NjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 012798b387c9af278f454ad3f55aa2e816b44c3346ada5ee5a74fcbb13d213a8 |
| hovercard-subject-tag | pull_request:4225906040 |
| 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/3263/files |
| twitter:image | https://avatars.githubusercontent.com/in/1236702?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/in/1236702?s=400&v=4 |
| og:image:alt | Requested by Felix Weinberger · Slack thread NoteAI disclosure: this PR was authored by Claude (an AI agent operated by the MCP maintainer team's triage workflow, at Felix Weinberger's requ... |
| 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