Fix OAuth discovery fallback and URL ordering #1624
Merged
+319
−41
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.
Motivation and Context
This PR addresses two related OAuth discovery issues that affect backward compatibility and correctness:
Issue #1495: Legacy Server Compatibility
Version 1.18.0 broke compatibility with legacy MCP servers (like Linear and Atlassian) that don't yet support Protected Resource Metadata (PRM) endpoints. The implementation raised an exception when PRM discovery failed, preventing fallback to the March 2025 spec behavior.
Issue #1623: Incorrect OAuth Discovery URL Priority
The OAuth discovery URL ordering was incorrect, causing the system to try root-level URLs before path-based OIDC URLs. This broke authentication with services like Jira MCP where tenant-specific authorization servers should take precedence over root-level servers.
Changes
1. Enable Fallback to March 2025 Spec (Issue #1495)
/.well-known/oauth-authorization-serverat the roothttps://mcp.linear.app/sse) and Atlassian (https://mcp.atlassian.com/v1/sse)2. Fix OAuth Discovery URL Ordering (Issue #1623)
Implementation Details
Function Renaming:
build_protected_resource_discovery_urls→build_protected_resource_metadata_discovery_urlsget_discovery_urls→build_oauth_authorization_server_metadata_discovery_urlsNew Behavior:
build_oauth_authorization_server_metadata_discovery_urls(auth_server_url: str | None, server_url: str)auth_server_urlisNone): Only try{root}/.well-known/oauth-authorization-serverauth_server_urlhas path like/tenant1): Only try path-based URLs in priority orderauth_server_urlis root likehttps://auth.example.com): Only try root URLsExamples
Example 1: Legacy Server (No PRM)
Example 2: Path-Aware Discovery
Example 3: Root Authorization Server
Breaking Changes
Some invalid server configurations will no longer work (these violate RFC specifications):
These configurations are not expected to exist in practice.
Testing
Additional Context
Related Issues
Fixes #1495
Fixes #1623