Skip to content

Commit 2c0f2c3

Browse files
authored
fix: strip trailing slashes from OAuth metadata URLs
Pydantic's AnyHttpUrl automatically appends a trailing slash to bare hostnames (e.g., http://localhost:8000 becomes http://localhost:8000/). This causes OAuth discovery to fail in clients like Google's ADK and IBM's MCP Context Forge because RFC 8414 §3.3 and RFC 9728 §3 require that the issuer/resource URL in the metadata response must be identical to the URL used for discovery. This fix ensures all URLs in OAuth metadata (issuer, resource, authorization_servers) have trailing slashes stripped, following the same pattern already used for authorization_endpoint and token_endpoint. Github-Issue: #1919 Reported-by: joar
1 parent 5301298 commit 2c0f2c3

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/mcp/server/auth/routes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def build_metadata(
157157

158158
# Create metadata
159159
metadata = OAuthMetadata(
160-
issuer=issuer_url,
160+
issuer=AnyHttpUrl(str(issuer_url).rstrip("/")),
161161
authorization_endpoint=authorization_url,
162162
token_endpoint=token_url,
163163
scopes_supported=client_registration_options.valid_scopes,
@@ -222,8 +222,8 @@ def create_protected_resource_routes(
222222
List of Starlette routes for protected resource metadata
223223
"""
224224
metadata = ProtectedResourceMetadata(
225-
resource=resource_url,
226-
authorization_servers=authorization_servers,
225+
resource=AnyHttpUrl(str(resource_url).rstrip("/")),
226+
authorization_servers=[AnyHttpUrl(str(server).rstrip("/")) for server in authorization_servers],
227227
scopes_supported=scopes_supported,
228228
resource_name=resource_name,
229229
resource_documentation=resource_documentation,

0 commit comments

Comments
 (0)