Skip to content

Conversation

@LucaButBoring
Copy link
Contributor

@LucaButBoring LucaButBoring commented Aug 7, 2025

Implements support for the RFC 7523 authentication flows. This PR is a trimmed-down version of #1020, but will likely be superceded by a future authlib-based implementation (see #1240).

Compared to #1020, this implements the flow via a separate httpx.Auth subclass, which is in-line with prior maintainer feedback on how to grow these auth implementations.

Motivation and Context

Implementation example for modelcontextprotocol/modelcontextprotocol#1046.

How Has This Been Tested?

Unit tests (TBD: unit tests for section 2.2 flow). Planning to spot test with Keycloak setup once that gets published.

Breaking Changes

None.

Types of changes

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

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

LucaButBoring and others added 30 commits June 23, 2025 10:52
No longer doing external integration examples as of modelcontextprotocol#1011. Will likely bring this back later as a standalone example.
Python default parameters reuse their references, so we can't use a collection like a dict as a default parameter value or we'll dirty our state.
@felixweinberger felixweinberger added the auth Issues and PRs related to Authentication / OAuth label Oct 3, 2025
@felixweinberger felixweinberger marked this pull request as draft October 13, 2025 15:36
@felixweinberger
Copy link
Contributor

Marking this as a draft for now to remove from our review queue while we wait for the SEP approval - modelcontextprotocol/modelcontextprotocol#1047

Please reopen once the SEP has been accepted or if you specifically need input beforehand!

@LucaButBoring
Copy link
Contributor Author

SEP-1046 is accepted; reopening this (will fix merge conflicts).

@LucaButBoring LucaButBoring marked this pull request as ready for review October 20, 2025 18:00
@felixweinberger felixweinberger added needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention and removed pending SEP approval When a PR is attached as an implementation detail to a SEP, we mark it as such for triage. needs sync Needs sync with latest main branch to ensure CI passes labels Oct 21, 2025
@LucaButBoring
Copy link
Contributor Author

Per discussion on Discord, RFC7523OAuthClientProvider has been moved into a new mcp.client.auth.extensions.client_credentials module. Doing this required also renaming src/mcp/client/auth.py to src/mcp/client/auth/oauth2.py and re-exporting it from a new src/mcp/client/auth/__init__.py.

token_data["client_assertion_type"] = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
# We need to set the audience to the token endpoint, the audience is difference from the one in claims
# it represents the resource server that will validate the token
token_data["audience"] = self.context.get_resource_url()
Copy link
Member

Choose a reason for hiding this comment

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

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 the comment here didn't match the required change; I've updated it in the latest commit - I believe the aud claim is supposed to be updated to the issuer, not this.

issuer = str(self.context.oauth_metadata.issuer)
assertion = self.jwt_parameters.to_assertion(with_audience_fallback=issuer)

This line is actually setting the audience parameter in the token exchange request:

token_data["audience"] = self.context.get_resource_url()

as described in RFC 8693.

Copy link
Member

Choose a reason for hiding this comment

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

ah yep i had the wrong line. thanks!

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

@pcarleton pcarleton merged commit f161149 into modelcontextprotocol:main Oct 29, 2025
18 checks passed
Implements authorization code flow with PKCE and automatic token refresh.
"""

from mcp.client.auth.oauth2 import * # noqa: F403
Copy link
Member

Choose a reason for hiding this comment

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

Please add the right imports here.

Copy link
Contributor

Choose a reason for hiding this comment

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

you mean all classes one by one?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor

Choose a reason for hiding this comment

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

fixed in #1532

maxisbey added a commit that referenced this pull request Oct 29, 2025
This addresses issues introduced in PR #1247:

1. Fixed pyright reportMissingTypeStubs error by adding __init__.py to the
   extensions directory, making it a proper Python package
2. Replaced wildcard import in auth/__init__.py with explicit imports as
   requested in code review

The changes ensure type checking passes and maintain backward compatibility
with existing code that imports from mcp.client.auth.
@LucaButBoring LucaButBoring mentioned this pull request Oct 29, 2025
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Issues and PRs related to Authentication / OAuth needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement SEP-1046: Support OAuth client credentials flow in authorization

6 participants