-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix building auth metadata paths #779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+85
−21
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5577323
fix auth metadata paths
Rodriguespn 521d3bc
fix lint
Rodriguespn 18f88ff
more robust solution
Rodriguespn 628b492
Merge branch 'modelcontextprotocol:main' into main
Rodriguespn c7068b3
refactor append_path
Rodriguespn 99ef0d4
delete modify_url_path function
Rodriguespn edd2e09
Update src/mcp/server/auth/routes.py
Kludex f85bf8d
Merge branch 'main' into main
Kludex 045da79
Add tests
Kludex 762d4e8
Merge branch 'main' into main
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,12 @@ | |
|
||
import httpx | ||
import pytest | ||
from inline_snapshot import snapshot | ||
from pydantic import AnyHttpUrl | ||
|
||
from mcp.client.auth import OAuthClientProvider | ||
from mcp.server.auth.routes import build_metadata | ||
from mcp.server.auth.settings import ClientRegistrationOptions, RevocationOptions | ||
from mcp.shared.auth import ( | ||
OAuthClientInformationFull, | ||
OAuthClientMetadata, | ||
|
@@ -905,3 +908,76 @@ async def test_token_exchange_error_basic(self, oauth_provider, oauth_client_inf | |
await oauth_provider._exchange_code_for_token( | ||
"invalid_auth_code", oauth_client_info | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
( | ||
"issuer_url", | ||
"service_documentation_url", | ||
"authorization_endpoint", | ||
"token_endpoint", | ||
"registration_endpoint", | ||
"revocation_endpoint", | ||
), | ||
( | ||
pytest.param( | ||
"https://auth.example.com", | ||
"https://auth.example.com/docs", | ||
"https://auth.example.com/authorize", | ||
"https://auth.example.com/token", | ||
"https://auth.example.com/register", | ||
"https://auth.example.com/revoke", | ||
id="simple-url", | ||
), | ||
pytest.param( | ||
"https://auth.example.com/", | ||
"https://auth.example.com/docs", | ||
"https://auth.example.com/authorize", | ||
"https://auth.example.com/token", | ||
"https://auth.example.com/register", | ||
"https://auth.example.com/revoke", | ||
id="with-trailing-slash", | ||
), | ||
pytest.param( | ||
"https://auth.example.com/v1/mcp", | ||
"https://auth.example.com/v1/mcp/docs", | ||
"https://auth.example.com/v1/mcp/authorize", | ||
"https://auth.example.com/v1/mcp/token", | ||
"https://auth.example.com/v1/mcp/register", | ||
"https://auth.example.com/v1/mcp/revoke", | ||
id="with-path-param", | ||
), | ||
), | ||
) | ||
def test_build_metadata( | ||
issuer_url: str, | ||
service_documentation_url: str, | ||
authorization_endpoint: str, | ||
token_endpoint: str, | ||
registration_endpoint: str, | ||
revocation_endpoint: str, | ||
): | ||
metadata = build_metadata( | ||
issuer_url=AnyHttpUrl(issuer_url), | ||
service_documentation_url=AnyHttpUrl(service_documentation_url), | ||
client_registration_options=ClientRegistrationOptions( | ||
enabled=True, valid_scopes=["read", "write", "admin"] | ||
), | ||
revocation_options=RevocationOptions(enabled=True), | ||
) | ||
|
||
assert metadata == snapshot( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added |
||
OAuthMetadata( | ||
issuer=AnyHttpUrl(issuer_url), | ||
authorization_endpoint=AnyHttpUrl(authorization_endpoint), | ||
token_endpoint=AnyHttpUrl(token_endpoint), | ||
registration_endpoint=AnyHttpUrl(registration_endpoint), | ||
scopes_supported=["read", "write", "admin"], | ||
grant_types_supported=["authorization_code", "refresh_token"], | ||
token_endpoint_auth_methods_supported=["client_secret_post"], | ||
service_documentation=AnyHttpUrl(service_documentation_url), | ||
revocation_endpoint=AnyHttpUrl(revocation_endpoint), | ||
revocation_endpoint_auth_methods_supported=["client_secret_post"], | ||
code_challenge_methods_supported=["S256"], | ||
) | ||
) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to strip in any of those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion, @Kludex!
I initially thought the same — that
urljoin(str(issuer_url), "/authorize")
would be enough. But it actually breaks in some cases where thebase_url
includes a path.For example, when the
base_url
ishttps://example.com/auth/oidc/op/Customer/
,urljoin
returnshttps://example.com/authorize
, which drops the intended path entirely. That’s becauseurljoin
treats the/authorize
as an absolute path and replaces everything after the domain.To illustrate this, I added a test suite comparing both approaches:
So for consistency across all cases, I believe we need to keep the rstrip("/") approach.
Let me know what you think!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunate, but it makes sense.