Skip to content

Conversation

@Nayana-Parameswarappa
Copy link
Contributor

This pull request implements support for upscoping when an API request fails with a 403 Forbidden response due to insufficient_scope.

Motivation and Context

Before: When a request failed due to missing scopes, the SDK halted with a hard error, forcing users to manually determine the correct permissions and re-authenticate.

Now: We've introduced graceful error handling for insufficient_scope 403 responses. This change supports upscoping, allowing the SDK to either automatically prompt for the required scopes or guide the user, significantly enhancing resilience and user experience.

What changed (high level)

Extract scope and resource_metadata_url from WWW-Authenticate on 403 responses and use them to trigger an upscoping auth flow via the configured OAuthClientProvider.
Add a guard that records the last WWW-Authenticate header used for an upscoping attempt and prevents retrying an identical directive repeatedly; the guard is reset after successful responses.

Fixes

This resolves the issue described in:
#1039

How Has This Been Tested?

From running npm run test

Test Suites: 40 passed, 40 total
Tests: 836 passed, 836 total
Snapshots: 0 total
Time: 16.599 s

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • [X ] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

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

Additional context

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 14, 2025

Open in StackBlitz

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

commit: 48f4470

@davidmcwherter
Copy link

@felixweinberger I think this is in-line with SEP-835. Can you take a look?

@pcarleton
Copy link
Member

this duplicates some of #1133

I think we just need to add the 403 handling to the transports (e.g. I don't think we need to change auth.ts)

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.

(see prev comment)

@Nayana-Parameswarappa
Copy link
Contributor Author

this duplicates some of #1133

I think we just need to add the 403 handling to the transports (e.g. I don't think we need to change auth.ts)

I have updated the code to only to handle 403 handling.

…private

Instead of exporting extractFieldFromWwwAuth as a separate function, keep it
private and extend extractWWWAuthenticateParams to also return the 'error'
field. This provides a cleaner API while maintaining all functionality.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
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! thanks, I tweaked a bit to avoid an extra function export.

@pcarleton pcarleton enabled auto-merge (squash) November 20, 2025 19:37
@pcarleton pcarleton merged commit 3485a06 into modelcontextprotocol:main Nov 20, 2025
5 checks passed
@pcarleton pcarleton linked an issue Nov 21, 2025 that may be closed by this pull request
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.

Support SEP-835

3 participants