FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(auth): keep root PRM resource URL canonical by Epochex · Pull Request #2888 · modelcontextprotocol/python-sdk · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
18 changes: 17 additions & 1 deletion src/mcp/shared/auth.py
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
@@ -1,6 +1,7 @@
from typing import Any, Literal
from urllib.parse import urlsplit, urlunsplit

from pydantic import AnyHttpUrl, AnyUrl, BaseModel, ConfigDict, Field, field_validator
from pydantic import AnyHttpUrl, AnyUrl, BaseModel, ConfigDict, Field, field_serializer, field_validator


class OAuthToken(BaseModel):
Expand Down Expand Up @@ -193,3 +194,18 @@ class ProtectedResourceMetadata(BaseModel):
dpop_signing_alg_values_supported: list[str] | None = None
# dpop_bound_access_tokens_required default is False, but omitted here for clarity
dpop_bound_access_tokens_required: bool | None = None

@field_serializer("resource")
def _serialize_resource(self, value: AnyHttpUrl) -> str:
"""Preserve canonical root resources without a trailing slash.

Pydantic normalizes `https://example.com` to `https://example.com/`.
RFC 9728 resource metadata is compared as a canonical resource URL, so
when the resource path is the origin root we serialize it back without
that synthetic slash.
"""
url = str(value)
parsed = urlsplit(url)
if parsed.path != "/":
return url
return urlunsplit((parsed.scheme, parsed.netloc, "", parsed.query, parsed.fragment))
12 changes: 11 additions & 1 deletion tests/server/auth/test_protected_resource.py
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
Expand Up @@ -9,6 +9,7 @@
from starlette.applications import Starlette

from mcp.server.auth.routes import build_resource_metadata_url, create_protected_resource_routes
from mcp.shared.auth import ProtectedResourceMetadata


@pytest.fixture
Expand Down Expand Up @@ -96,7 +97,7 @@ async def test_metadata_endpoint_without_path(root_resource_client: httpx.AsyncC
assert response.status_code == 200
assert response.json() == snapshot(
{
"resource": "https://example.com/",
"resource": "https://example.com",
"authorization_servers": ["https://auth.example.com/"],
"scopes_supported": ["read"],
"resource_name": "Root Resource",
Expand All @@ -105,6 +106,15 @@ async def test_metadata_endpoint_without_path(root_resource_client: httpx.AsyncC
)


def test_root_resource_serializes_without_trailing_slash():
metadata = ProtectedResourceMetadata(
resource=AnyHttpUrl("https://example.com"),
authorization_servers=[AnyHttpUrl("https://auth.example.com")],
)

assert metadata.model_dump(mode="json")["resource"] == "https://example.com"


# Tests for URL construction utility function


Expand Down
Loading

Back | FazBrowse Home | New Git URL