Skip to content

Commit f92a858

Browse files
committed
feat(authorize): Add logic to check for metadata
1 parent eeeaeb3 commit f92a858

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/mcp/server/auth/handlers/authorize.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from dataclasses import dataclass
33
from typing import Any, Literal
44

5+
import httpx
56
from pydantic import AnyUrl, BaseModel, Field, RootModel, ValidationError
67
from starlette.datastructures import FormData, QueryParams
78
from starlette.requests import Request
@@ -16,7 +17,11 @@
1617
OAuthAuthorizationServerProvider,
1718
construct_redirect_uri,
1819
)
19-
from mcp.shared.auth import InvalidRedirectUriError, InvalidScopeError
20+
from mcp.shared.auth import (
21+
InvalidRedirectUriError,
22+
InvalidScopeError,
23+
OAuthClientInformationFull,
24+
)
2025

2126
logger = logging.getLogger(__name__)
2227

@@ -166,6 +171,29 @@ async def error_response(
166171
client = await self.provider.get_client(
167172
auth_request.client_id,
168173
)
174+
if not client:
175+
# Check if `client_id` is a valid URL for Metadata Document
176+
if auth_request.client_id.startswith("https://"):
177+
try:
178+
async with httpx.AsyncClient() as http_client:
179+
response = await http_client.get(auth_request.client_id)
180+
response.raise_for_status()
181+
metadata = response.json()
182+
183+
if metadata.get("client_id") != auth_request.client_id:
184+
return await error_response(
185+
error="invalid_request",
186+
error_description=f"Client ID '{auth_request.client_id}' \
187+
not found in metadata document",
188+
)
189+
190+
client = OAuthClientInformationFull(**metadata)
191+
192+
except Exception as e:
193+
return await error_response(
194+
error="invalid_request",
195+
error_description=f"Failed to fetch client metadata from {auth_request.client_id}: {e}",
196+
)
169197
if not client:
170198
# For client_id validation errors, return direct error (no redirect)
171199
return await error_response(

0 commit comments

Comments
 (0)