docs/python-sdk/fastmcp-server-auth-handlers-authorize.mdx
fastmcp.server.auth.handlers.authorizeEnhanced authorization handler with improved error responses.
This module provides an enhanced authorization handler that wraps the MCP SDK's AuthorizationHandler to provide better error messages when clients attempt to authorize with unregistered client IDs.
The enhancement adds:
create_unregistered_client_html <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/auth/handlers/authorize.py#L41" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>create_unregistered_client_html(client_id: str, registration_endpoint: str, discovery_endpoint: str, server_name: str | None = None, server_icon_url: str | None = None, title: str = 'Client Not Registered') -> str
Create styled HTML error page for unregistered client attempts.
Args:
client_id: The unregistered client ID that was providedregistration_endpoint: URL of the registration endpointdiscovery_endpoint: URL of the OAuth metadata discovery endpointserver_name: Optional server name for brandingserver_icon_url: Optional server icon URLtitle: Page titleReturns:
AuthorizationHandler <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/auth/handlers/authorize.py#L161" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Authorization handler with enhanced error responses for unregistered clients.
This handler extends the MCP SDK's AuthorizationHandler to provide better UX when clients attempt to authorize without being registered. It implements content negotiation to return:
This maintains OAuth 2.1 compliance (returns 400 for invalid client_id) while providing actionable guidance to fix the error.
Methods:
handle <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/auth/handlers/authorize.py#L196" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>handle(self, request: Request) -> Response
Handle authorization request with enhanced error responses.
This method extends the SDK's authorization handler and intercepts errors for unregistered clients to provide better error responses based on the client's Accept header.
Args:
request: The authorization requestReturns: