Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/mcp/server/auth/routes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import urllib.parse
from collections.abc import Awaitable, Callable
from typing import Any

Expand Down Expand Up @@ -165,13 +166,24 @@ def build_metadata(
service_documentation_url: AnyHttpUrl | None,
client_registration_options: ClientRegistrationOptions,
revocation_options: RevocationOptions,
) -> OAuthMetadata:
) -> OAuthMetadata:
def append_path(path: str, endpoint_path: str) -> str:
# Ensures the path ends with a slash
path = f"{path}/"

# Ensures the endpoint path does not start with a slash
endpoint_path_lstrip = endpoint_path.lstrip("/")

# Join the two paths and remove leading slashes This ensures that the final
# path doesn't have double slashes between the host and the endpoint
return urllib.parse.urljoin(path, endpoint_path_lstrip).lstrip("/")


authorization_url = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + AUTHORIZATION_PATH.lstrip("/")
)
token_url = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + TOKEN_PATH.lstrip("/")
issuer_url, lambda path: append_path(path, AUTHORIZATION_PATH)
)
token_url = modify_url_path(issuer_url, lambda path: append_path(path, TOKEN_PATH))
Copy link
Member

Choose a reason for hiding this comment

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

I think we can delete this modify_url_path tho. str(issuer_url) should work.


# Create metadata
metadata = OAuthMetadata(
issuer=issuer_url,
Expand All @@ -194,14 +206,14 @@ def build_metadata(
# Add registration endpoint if supported
if client_registration_options.enabled:
metadata.registration_endpoint = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + REGISTRATION_PATH.lstrip("/")
issuer_url, lambda path: append_path(path, REGISTRATION_PATH)
)

# Add revocation endpoint if supported
if revocation_options.enabled:
metadata.revocation_endpoint = modify_url_path(
issuer_url, lambda path: path.rstrip("/") + REVOCATION_PATH.lstrip("/")
issuer_url, lambda path: append_path(path, REVOCATION_PATH)
)
metadata.revocation_endpoint_auth_methods_supported = ["client_secret_post"]

return metadata
return metadata
Loading