Conversation
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
dc1f2e6 to
7ac6d17
Compare
0399400 to
7552e9d
Compare
7552e9d to
b3a2417
Compare
|
This looks great! Any update on when you'll be able to merge this? |
gao-sun
approved these changes
Jul 2, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the mcp-auth library to support OAuth 2.0 Protected Resource Metadata (RFC 9728) while preserving backward compatibility with the legacy authorization server mode. It introduces strategy-based handlers, a TokenVerifier abstraction, and enhanced middleware and routing for protected resource metadata.
- Introduce
AuthorizationServerHandler(deprecated) andResourceServerHandlerfor dual-mode operation - Add
TokenVerifierto encapsulate validation logic per resource - Enhance
bearer_auth_middlewarewith aresourceparameter and addresource_metadata_router()
Reviewed Changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| mcpauth/utils/_transpile_resource_metadata.py | Convert MCPAuth config metadata to standard protected-resource metadata |
| mcpauth/utils/_create_resource_metadata_endpoint.py | Build RFC 9728 metadata endpoint URLs |
| mcpauth/utils/_bearer_www_authenticate_header.py | Implement chained header builder for WWW-Authenticate |
| mcpauth/middleware/create_bearer_auth.py | Extend error handling and include resource_metadata header |
| mcpauth/types.py | Add ResourceServerMetadata & ResourceServerConfig types |
| mcpauth/auth/token_verifier.py | Encapsulate JWT issuer extraction and verification per server |
| mcpauth/auth/resource_server_handler.py | Implement protected-resource mode routing & verifier lookup |
| mcpauth/auth/authorization_server_handler.py | Maintain legacy auth-server mode with deprecation warning |
| mcpauth/init.py | Initialize correct handler based on server vs protected_resources |
| tests/* | Add and update tests for new resource metadata, header, and middleware behavior |
| samples/server/* | Update example apps to use resource-server configuration |
Comments suppressed due to low confidence (2)
mcpauth/middleware/create_bearer_auth.py:94
- Update the
_handle_errordocstring to note that it now returns three values(status_code, response_body, headers)instead of two.
"""
mcpauth/middleware/create_bearer_auth.py:44
- [nitpick] The field name
resourceinBearerAuthConfigcan be ambiguous; consider renaming it toresource_idfor consistency with other APIs and to clarify that it’s an identifier rather than the metadata itself.
resource: Optional[str] = None
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a significant architectural enhancement to the
mcp-authPython library, refactoring it to support modern OAuth 2.0 Protected Resource Metadata (RFC 9728) while maintaining full backward compatibility with the existing Authorization Server mode. This change enables developers to configure multiple distinct resources, each with its own set of trusted authorization servers, within a singleMCPAuthinstance.Core Architectural Changes
The refactor is centered around a clearer, more scalable internal architecture, leveraging the Strategy design pattern:
MCPAuthconstructor now acts as a factory, instantiating a corresponding handler to manage mode-specific logic based on the provided configuration.AuthorizationServerHandler, this supports the legacy, single-AS configuration and is now formally deprecated.ResourceServerHandler, this is the new, recommended approach. It accepts a singleResourceServerConfigor a list of them, allowing the definition of multiple protected resources and their associated policies.TokenVerifier: An Authentication Policy Encapsulator: A new core internal class,TokenVerifier, has been introduced. This class encapsulates the complete authentication policy for a single logical resource.TokenVerifierinstance is bound to the specific list ofAuthServerConfigobjects that its associated resource trusts.jwks_uri.iss(issuer) claim is within its list of trusted issuers, providing precise and helpful error messages that list all expected issuers on failure.MCPAuthclass, which now acts as a high-level factory and registry that routes requests to the correctTokenVerifier.API Enhancements and Usage
The public-facing API has evolved to be more powerful and expressive without introducing breaking changes.
bearer_auth_middleware: Thebearer_auth_middlewaremethod is now significantly more powerful.resourceparameter. This property is required whenMCPAuthis initialized inprotected_resourcesmode and signals which resource's authentication policy to apply.MCPAuthuses the resource identifier to look up the correspondingTokenVerifierand uses it to validate the token. This makes protecting endpoints for different resources explicit and robust.serverconfiguration, the behavior ofbearer_auth_middlewareremains unchanged, ensuring zero breaking changes.resource_metadata_router(): A new method that creates aStarletterouter to automatically serve the Protected Resource Metadata for all configured resources./.well-known/oauth-protected-resource/...pathing logic as defined in RFC 9728.metadata_route()method is now marked as@deprecated.Example Usage
Here is a practical example of how to configure and use the new resource server mode to protect an API endpoint: