Skip to content

feat: expose discoverOAuthServerInfo() and add provider caching for auth server URL#1527

Merged
felixweinberger merged 9 commits intomainfrom
fweinberger/expose-auth-server-discovery
Feb 13, 2026
Merged

feat: expose discoverOAuthServerInfo() and add provider caching for auth server URL#1527
felixweinberger merged 9 commits intomainfrom
fweinberger/expose-auth-server-discovery

Conversation

@felixweinberger
Copy link
Contributor

@felixweinberger felixweinberger commented Feb 12, 2026

Summary

Extracts the RFC 9728 + authorization server metadata discovery logic from authInternal() into a new public discoverOAuthServerInfo() function, and adds unified discovery state caching to OAuthClientProvider.

Motivation and Context

MCP clients need the authorization server URL for operations outside the auth() orchestrator — specifically token refresh and token revocation. Currently, the SDK discovers this URL via RFC 9728 inside authInternal() but never exposes it, forcing consumers to reimplement the discovery logic.

Additionally, browser-based OAuth flows lose discovery state (including the resourceMetadataUrl from WWW-Authenticate headers) during redirects, causing token exchange failures (#1234, #1350).

Key Changes

discoverOAuthServerInfo(serverUrl, opts?) (new export)
Combines RFC 9728 protected resource metadata discovery with authorization server metadata discovery into a single call. Returns OAuthServerInfo { authorizationServerUrl, authorizationServerMetadata?, resourceMetadata? }.

OAuthDiscoveryState (new type)
Unified type for persisting all discovery results: auth server URL, resource metadata URL, resource metadata, and auth server metadata.

OAuthClientProvider changes (non-breaking)

  • saveDiscoveryState?(state) — called by auth() after discovery so providers can persist all discovery results
  • discoveryState?() — returns cached state; when present, auth() restores discovery results instead of re-discovering
  • invalidateCredentials scope union extended with 'discovery' for clearing cached discovery state

authInternal() refactored to use discoverOAuthServerInfo() and the unified caching. Cached path restores full discovery state including resource metadata, preserving the resource parameter in token requests.

This subsumes the use case from #1350 (persisting resourceMetadataUrl across browser redirects) — the URL is now part of OAuthDiscoveryState.

How Has This Been Tested?

  • 8 new test cases covering discoverOAuthServerInfo() and discovery state caching
  • Tests verify resource parameter is preserved on cached path
  • All 253 client tests pass
  • Full typecheck and lint pass

Breaking Changes

None. All additions are optional interface methods, new exports, and a new union member for an optional method parameter.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Co-authored with @hassan123789 whose #1350 identified the resourceMetadataUrl persistence problem that this unified approach addresses.

@changeset-bot
Copy link

changeset-bot bot commented Feb 12, 2026

🦋 Changeset detected

Latest commit: cb7b55c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 12, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1527

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1527

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1527

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1527

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1527

commit: cb7b55c

@pcarleton
Copy link
Member

generally like the idea, but i'd prefer if we made it flexible to store more discovery state:
#1350

this would make it easier to squirrel away the PRM URL, OASM URL, scope, resource parameter. Also makes it easier to provide those things out of band, e.g. if you've discovered them previously and want to just load them, or if there's something wrong with discovery (e.g. a 503 is de-railing the discovery process).

Copy link
Contributor

@ochafik ochafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to @pcarleton for approval but added a few questions :-)

@felixweinberger
Copy link
Contributor Author

generally like the idea, but i'd prefer if we made it flexible to store more discovery state: #1350

this would make it easier to squirrel away the PRM URL, OASM URL, scope, resource parameter. Also makes it easier to provide those things out of band, e.g. if you've discovered them previously and want to just load them, or if there's something wrong with discovery (e.g. a 503 is de-railing the discovery process).

Sure! We can cajole #1350 in potentially? @ochafik thoughts?

felixweinberger and others added 4 commits February 13, 2026 13:29
…State

Address PR review feedback from pcarleton and ochafik:
- Replace saveAuthorizationServerUrl/authorizationServerUrl with unified
  saveDiscoveryState/discoveryState that stores all discovery results
- Add OAuthDiscoveryState type covering auth server URL, resource metadata
  URL, resource metadata, and auth server metadata
- Add 'discovery' scope to invalidateCredentials for cache clearing
- Subsumes the use case from PR #1350 (persisting resourceMetadataUrl
  across browser redirects)
- Cached path now restores full discovery state including resource metadata,
  fixing the resource parameter regression

Co-authored-by: hassan123789 <49031989+hassan123789@users.noreply.github.com>
@felixweinberger felixweinberger marked this pull request as ready for review February 13, 2026 15:15
@felixweinberger felixweinberger requested a review from a team as a code owner February 13, 2026 15:15
pcarleton
pcarleton previously approved these changes Feb 13, 2026
Copy link
Member

@pcarleton pcarleton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, couple non-blocking

/**
* Result of {@linkcode discoverOAuthServerInfo}.
*/
export interface OAuthServerInfo {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this type and OAuthDiscoveryState be the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they're a little bit different right, they just happen to look similar right now - but in future the OAuthDiscoveryState (what gets saved) could diverge meaningfully from OAuthServerInfo?

To avoid duplication though makes sense for OAuthDiscoveryState to extend OAuthServerInfo though, so changed that here - wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool that makes sense to me

Address PR feedback:
- OAuthDiscoveryState now extends OAuthServerInfo instead of
  redeclaring the same fields, keeping them linked while allowing
  divergence for cache-specific fields like resourceMetadataUrl.
- Add TODO for potential authorizationServerMetadataUrl field to
  capture the exact well-known URL where AS metadata was discovered.
- Add TODO noting resourceMetadataUrl is only populated when
  explicitly provided, not when derived internally.
@felixweinberger felixweinberger merged commit dc896e1 into main Feb 13, 2026
15 checks passed
@felixweinberger felixweinberger deleted the fweinberger/expose-auth-server-discovery branch February 13, 2026 17:55
felixweinberger added a commit that referenced this pull request Feb 13, 2026
Backport of PR #1527 from main to v1.x:
- Add discoverOAuthServerInfo() combining RFC 9728 + AS metadata discovery
- Add OAuthServerInfo and OAuthDiscoveryState interfaces
- Add saveDiscoveryState()/discoveryState() to OAuthClientProvider
- Add 'discovery' scope to invalidateCredentials()
- Update auth() orchestrator to use cached discovery state
- Add comprehensive tests for new functionality
felixweinberger added a commit that referenced this pull request Feb 13, 2026
Backport of PR #1527 from main to v1.x:
- Add discoverOAuthServerInfo() combining RFC 9728 + AS metadata discovery
- Add OAuthServerInfo and OAuthDiscoveryState interfaces
- Add saveDiscoveryState()/discoveryState() to OAuthClientProvider
- Add 'discovery' scope to invalidateCredentials()
- Update auth() orchestrator to use cached discovery state
- Add comprehensive tests for new functionality
felixweinberger added a commit that referenced this pull request Feb 13, 2026
Backport of PR #1527 from main to v1.x:
- Add discoverOAuthServerInfo() combining RFC 9728 + AS metadata discovery
- Add OAuthServerInfo and OAuthDiscoveryState interfaces
- Add saveDiscoveryState()/discoveryState() to OAuthClientProvider
- Add 'discovery' scope to invalidateCredentials()
- Update auth() orchestrator to use cached discovery state
- Add comprehensive tests for new functionality
felixweinberger added a commit that referenced this pull request Feb 13, 2026
Backport of PR #1527 from main to v1.x:
- Add discoverOAuthServerInfo() combining RFC 9728 + AS metadata discovery
- Add OAuthServerInfo and OAuthDiscoveryState interfaces
- Add saveDiscoveryState()/discoveryState() to OAuthClientProvider
- Add 'discovery' scope to invalidateCredentials()
- Update auth() orchestrator to use cached discovery state
- Add comprehensive tests for new functionality
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.

3 participants