|
6 | 6 |
|
7 | 7 | import secrets |
8 | 8 | import time |
9 | | -from typing import Callable |
| 9 | +from typing import Callable, Literal |
10 | 10 | from uuid import uuid4 |
11 | 11 |
|
12 | | -from pydantic import ValidationError |
| 12 | +from pydantic import BaseModel, ValidationError |
13 | 13 | from starlette.requests import Request |
14 | 14 | from starlette.responses import JSONResponse, Response |
15 | 15 |
|
16 | 16 | from mcp.server.auth.errors import ( |
17 | 17 | InvalidRequestError, |
18 | 18 | OAuthError, |
19 | 19 | ServerError, |
| 20 | + stringify_pydantic_error, |
20 | 21 | ) |
21 | 22 | from mcp.server.auth.json_response import PydanticJSONResponse |
22 | 23 | from mcp.server.auth.provider import OAuthRegisteredClientsStore |
23 | 24 | from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata |
24 | 25 |
|
| 26 | +class ErrorResponse(BaseModel): |
| 27 | + error: Literal["invalid_redirect_uri", "invalid_client_metadata", "invalid_software_statement", "unapproved_software_statement"] |
| 28 | + error_description: str |
| 29 | + |
25 | 30 |
|
26 | 31 | def create_registration_handler( |
27 | 32 | clients_store: OAuthRegisteredClientsStore, client_secret_expiry_seconds: int | None |
28 | 33 | ) -> Callable: |
29 | | - """ |
30 | | - Create a handler for OAuth 2.0 Dynamic Client Registration. |
31 | | -
|
32 | | - Corresponds to clientRegistrationHandler in src/server/auth/handlers/register.ts |
33 | | -
|
34 | | - Args: |
35 | | - clients_store: The store for registered clients |
36 | | - client_secret_expiry_seconds: Optional expiry time for client secrets |
37 | | -
|
38 | | - Returns: |
39 | | - A Starlette endpoint handler function |
40 | | - """ |
41 | | - |
42 | 34 | async def registration_handler(request: Request) -> Response: |
43 | | - """ |
44 | | - Handler for the OAuth 2.0 Dynamic Client Registration endpoint. |
45 | | -
|
46 | | - Args: |
47 | | - request: The Starlette request |
48 | | -
|
49 | | - Returns: |
50 | | - JSON response with client information or error |
51 | | - """ |
| 35 | + # Implements dynamic client registration as defined in https://datatracker.ietf.org/doc/html/rfc7591#section-3.1 |
52 | 36 | try: |
53 | 37 | # Parse request body as JSON |
54 | | - try: |
55 | | - body = await request.json() |
56 | | - client_metadata = OAuthClientMetadata.model_validate(body) |
57 | | - except ValidationError as e: |
58 | | - raise InvalidRequestError(f"Invalid client metadata: {str(e)}") |
59 | | - |
60 | | - client_id = str(uuid4()) |
61 | | - client_secret = None |
62 | | - if client_metadata.token_endpoint_auth_method != "none": |
63 | | - # cryptographically secure random 32-byte hex string |
64 | | - client_secret = secrets.token_hex(32) |
65 | | - |
66 | | - client_id_issued_at = int(time.time()) |
67 | | - client_secret_expires_at = ( |
68 | | - client_id_issued_at + client_secret_expiry_seconds |
69 | | - if client_secret_expiry_seconds is not None |
70 | | - else None |
71 | | - ) |
72 | | - |
73 | | - client_info = OAuthClientInformationFull( |
74 | | - client_id=client_id, |
75 | | - client_id_issued_at=client_id_issued_at, |
76 | | - client_secret=client_secret, |
77 | | - client_secret_expires_at=client_secret_expires_at, |
78 | | - # passthrough information from the client request |
79 | | - redirect_uris=client_metadata.redirect_uris, |
80 | | - token_endpoint_auth_method=client_metadata.token_endpoint_auth_method, |
81 | | - grant_types=client_metadata.grant_types, |
82 | | - response_types=client_metadata.response_types, |
83 | | - client_name=client_metadata.client_name, |
84 | | - client_uri=client_metadata.client_uri, |
85 | | - logo_uri=client_metadata.logo_uri, |
86 | | - scope=client_metadata.scope, |
87 | | - contacts=client_metadata.contacts, |
88 | | - tos_uri=client_metadata.tos_uri, |
89 | | - policy_uri=client_metadata.policy_uri, |
90 | | - jwks_uri=client_metadata.jwks_uri, |
91 | | - jwks=client_metadata.jwks, |
92 | | - software_id=client_metadata.software_id, |
93 | | - software_version=client_metadata.software_version, |
94 | | - ) |
95 | | - # Register client |
96 | | - client = await clients_store.register_client(client_info) |
97 | | - if not client: |
98 | | - raise ServerError("Failed to register client") |
99 | | - |
100 | | - # Return client information |
101 | | - return PydanticJSONResponse(content=client, status_code=201) |
102 | | - |
103 | | - except OAuthError as e: |
104 | | - # Handle OAuth errors |
105 | | - status_code = 500 if isinstance(e, ServerError) else 400 |
106 | | - return JSONResponse(status_code=status_code, content=e.to_response_object()) |
| 38 | + body = await request.json() |
| 39 | + client_metadata = OAuthClientMetadata.model_validate(body) |
| 40 | + except ValidationError as validation_error: |
| 41 | + return PydanticJSONResponse(content=ErrorResponse( |
| 42 | + error="invalid_client_metadata", |
| 43 | + error_description=stringify_pydantic_error(validation_error) |
| 44 | + ), status_code=400) |
| 45 | + raise InvalidRequestError(f"Invalid client metadata: {str(e)}") |
| 46 | + |
| 47 | + client_id = str(uuid4()) |
| 48 | + client_secret = None |
| 49 | + if client_metadata.token_endpoint_auth_method != "none": |
| 50 | + # cryptographically secure random 32-byte hex string |
| 51 | + client_secret = secrets.token_hex(32) |
| 52 | + |
| 53 | + client_id_issued_at = int(time.time()) |
| 54 | + client_secret_expires_at = ( |
| 55 | + client_id_issued_at + client_secret_expiry_seconds |
| 56 | + if client_secret_expiry_seconds is not None |
| 57 | + else None |
| 58 | + ) |
| 59 | + |
| 60 | + client_info = OAuthClientInformationFull( |
| 61 | + client_id=client_id, |
| 62 | + client_id_issued_at=client_id_issued_at, |
| 63 | + client_secret=client_secret, |
| 64 | + client_secret_expires_at=client_secret_expires_at, |
| 65 | + # passthrough information from the client request |
| 66 | + redirect_uris=client_metadata.redirect_uris, |
| 67 | + token_endpoint_auth_method=client_metadata.token_endpoint_auth_method, |
| 68 | + grant_types=client_metadata.grant_types, |
| 69 | + response_types=client_metadata.response_types, |
| 70 | + client_name=client_metadata.client_name, |
| 71 | + client_uri=client_metadata.client_uri, |
| 72 | + logo_uri=client_metadata.logo_uri, |
| 73 | + scope=client_metadata.scope, |
| 74 | + contacts=client_metadata.contacts, |
| 75 | + tos_uri=client_metadata.tos_uri, |
| 76 | + policy_uri=client_metadata.policy_uri, |
| 77 | + jwks_uri=client_metadata.jwks_uri, |
| 78 | + jwks=client_metadata.jwks, |
| 79 | + software_id=client_metadata.software_id, |
| 80 | + software_version=client_metadata.software_version, |
| 81 | + ) |
| 82 | + # Register client |
| 83 | + client = await clients_store.register_client(client_info) |
| 84 | + |
| 85 | + # Return client information |
| 86 | + return PydanticJSONResponse(content=client, status_code=201) |
107 | 87 |
|
108 | 88 | return registration_handler |
0 commit comments