Skip to content

fix: restore eager OAuth discovery to avoid slow unauthenticated roundtrip#2078

Open
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
BabyChrist666:fix/oauth-performance-regression-1274
Open

fix: restore eager OAuth discovery to avoid slow unauthenticated roundtrip#2078
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
BabyChrist666:fix/oauth-performance-regression-1274

Conversation

@BabyChrist666
Copy link
Contributor

@BabyChrist666 BabyChrist666 commented Feb 17, 2026

Summary

Fixes #1274

  • When the client has no valid tokens but has existing client_info (i.e., has been through the OAuth flow at least once), performs OAuth discovery and authorization before sending the MCP request (eager flow), eliminating the unnecessary unauthenticated roundtrip that servers like Notion handle slowly (~10s latency per operation)
  • Extracts _perform_oauth_discovery_and_auth() helper shared by both the eager (pre-request) and reactive (post-401) paths, keeping code DRY while preserving RFC 9728 WWW-Authenticate header support on the 401 path
  • Adds dedicated tests for the eager OAuth flow behavior and updates existing tests that validate the reactive 401 path

Root Cause

Between v1.11.0 and v1.12.0, the client OAuth flow changed from eager discovery (discover → authenticate → send MCP request) to lazy discovery (send MCP request → get 401 → discover → authenticate → retry). This causes a slow unauthenticated roundtrip to the MCP server before OAuth even begins, which some servers (e.g. Notion) handle very slowly.

Approach

The fix introduces conditional eager OAuth:

  • First connection (no client_info): Uses the reactive 401 path as before, since the server's WWW-Authenticate header may carry routing information (e.g. resource_metadata URL) that pure well-known discovery lacks
  • Subsequent connections (has client_info but expired/missing tokens): Runs the full discovery/registration/authorization flow eagerly BEFORE sending the MCP request, avoiding the slow unauthenticated roundtrip
  • If the eager flow fails (e.g. resource mismatch, registration error), it falls through gracefully to the reactive 401 path

Both paths share a single _perform_oauth_discovery_and_auth() helper method.

Test plan

🤖 Generated with Claude Code

…dtrip (modelcontextprotocol#1274)

When the client has no valid tokens, perform OAuth discovery and
authorization BEFORE sending the MCP request. This restores the eager
behavior from v1.11.0 that was removed in v1.12.0, eliminating the
unnecessary unauthenticated roundtrip that servers like Notion handle
slowly (~10s latency per operation).

Both the eager (pre-request) and reactive (post-401) paths now share
a single `_perform_oauth_discovery_and_auth()` helper, keeping the
code DRY while preserving RFC 9728 WWW-Authenticate header support
on the 401 path.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@BabyChrist666 BabyChrist666 force-pushed the fix/oauth-performance-regression-1274 branch from 2f8377f to 794feea Compare February 18, 2026 00:08
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.

Streamable HTTP client performance regression starting with v1.12.0

1 participant

Comments