fastmcp 2.12.5__py3-none-any.whl → 2.13.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. fastmcp/cli/cli.py +7 -6
  2. fastmcp/cli/install/claude_code.py +6 -6
  3. fastmcp/cli/install/claude_desktop.py +3 -3
  4. fastmcp/cli/install/cursor.py +7 -7
  5. fastmcp/cli/install/gemini_cli.py +3 -3
  6. fastmcp/cli/install/mcp_json.py +3 -3
  7. fastmcp/cli/run.py +13 -8
  8. fastmcp/client/auth/oauth.py +100 -208
  9. fastmcp/client/client.py +11 -11
  10. fastmcp/client/logging.py +18 -14
  11. fastmcp/client/oauth_callback.py +85 -171
  12. fastmcp/client/transports.py +77 -22
  13. fastmcp/contrib/component_manager/component_service.py +6 -6
  14. fastmcp/contrib/mcp_mixin/README.md +32 -1
  15. fastmcp/contrib/mcp_mixin/mcp_mixin.py +14 -2
  16. fastmcp/experimental/utilities/openapi/json_schema_converter.py +4 -0
  17. fastmcp/experimental/utilities/openapi/parser.py +23 -3
  18. fastmcp/prompts/prompt.py +13 -6
  19. fastmcp/prompts/prompt_manager.py +16 -101
  20. fastmcp/resources/resource.py +13 -6
  21. fastmcp/resources/resource_manager.py +5 -164
  22. fastmcp/resources/template.py +107 -17
  23. fastmcp/resources/types.py +30 -24
  24. fastmcp/server/auth/auth.py +40 -32
  25. fastmcp/server/auth/handlers/authorize.py +324 -0
  26. fastmcp/server/auth/jwt_issuer.py +236 -0
  27. fastmcp/server/auth/middleware.py +96 -0
  28. fastmcp/server/auth/oauth_proxy.py +1256 -242
  29. fastmcp/server/auth/oidc_proxy.py +23 -6
  30. fastmcp/server/auth/providers/auth0.py +40 -21
  31. fastmcp/server/auth/providers/aws.py +29 -3
  32. fastmcp/server/auth/providers/azure.py +178 -127
  33. fastmcp/server/auth/providers/descope.py +4 -6
  34. fastmcp/server/auth/providers/github.py +29 -8
  35. fastmcp/server/auth/providers/google.py +30 -9
  36. fastmcp/server/auth/providers/introspection.py +281 -0
  37. fastmcp/server/auth/providers/jwt.py +8 -2
  38. fastmcp/server/auth/providers/scalekit.py +179 -0
  39. fastmcp/server/auth/providers/supabase.py +172 -0
  40. fastmcp/server/auth/providers/workos.py +32 -14
  41. fastmcp/server/context.py +122 -36
  42. fastmcp/server/http.py +58 -18
  43. fastmcp/server/low_level.py +121 -2
  44. fastmcp/server/middleware/caching.py +469 -0
  45. fastmcp/server/middleware/error_handling.py +6 -2
  46. fastmcp/server/middleware/logging.py +48 -37
  47. fastmcp/server/middleware/middleware.py +28 -15
  48. fastmcp/server/middleware/rate_limiting.py +3 -3
  49. fastmcp/server/middleware/tool_injection.py +116 -0
  50. fastmcp/server/proxy.py +6 -6
  51. fastmcp/server/server.py +683 -207
  52. fastmcp/settings.py +24 -10
  53. fastmcp/tools/tool.py +7 -3
  54. fastmcp/tools/tool_manager.py +30 -112
  55. fastmcp/tools/tool_transform.py +3 -3
  56. fastmcp/utilities/cli.py +62 -22
  57. fastmcp/utilities/components.py +5 -0
  58. fastmcp/utilities/inspect.py +77 -21
  59. fastmcp/utilities/logging.py +118 -8
  60. fastmcp/utilities/mcp_server_config/v1/environments/uv.py +6 -6
  61. fastmcp/utilities/mcp_server_config/v1/mcp_server_config.py +3 -3
  62. fastmcp/utilities/mcp_server_config/v1/schema.json +3 -0
  63. fastmcp/utilities/tests.py +87 -4
  64. fastmcp/utilities/types.py +1 -1
  65. fastmcp/utilities/ui.py +617 -0
  66. {fastmcp-2.12.5.dist-info → fastmcp-2.13.0.dist-info}/METADATA +10 -6
  67. {fastmcp-2.12.5.dist-info → fastmcp-2.13.0.dist-info}/RECORD +70 -63
  68. fastmcp/cli/claude.py +0 -135
  69. fastmcp/utilities/storage.py +0 -204
  70. {fastmcp-2.12.5.dist-info → fastmcp-2.13.0.dist-info}/WHEEL +0 -0
  71. {fastmcp-2.12.5.dist-info → fastmcp-2.13.0.dist-info}/entry_points.txt +0 -0
  72. {fastmcp-2.12.5.dist-info → fastmcp-2.13.0.dist-info}/licenses/LICENSE +0 -0
@@ -18,16 +18,28 @@ production use with enterprise identity providers.
18
18
 
19
19
  from __future__ import annotations
20
20
 
21
+ import base64
21
22
  import hashlib
23
+ import hmac
24
+ import json
22
25
  import secrets
23
26
  import time
24
27
  from base64 import urlsafe_b64encode
25
28
  from typing import TYPE_CHECKING, Any, Final
26
- from urllib.parse import urlencode
29
+ from urllib.parse import urlencode, urlparse
27
30
 
28
31
  import httpx
29
32
  from authlib.common.security import generate_token
30
33
  from authlib.integrations.httpx_client import AsyncOAuth2Client
34
+ from cryptography.fernet import Fernet
35
+ from key_value.aio.adapters.pydantic import PydanticAdapter
36
+ from key_value.aio.protocols import AsyncKeyValue
37
+ from key_value.aio.stores.disk import DiskStore
38
+ from key_value.aio.wrappers.encryption import FernetEncryptionWrapper
39
+ from mcp.server.auth.handlers.token import TokenErrorResponse, TokenSuccessResponse
40
+ from mcp.server.auth.handlers.token import TokenHandler as _SDKTokenHandler
41
+ from mcp.server.auth.json_response import PydanticJSONResponse
42
+ from mcp.server.auth.middleware.client_auth import ClientAuthenticator
31
43
  from mcp.server.auth.provider import (
32
44
  AccessToken,
33
45
  AuthorizationCode,
@@ -35,21 +47,40 @@ from mcp.server.auth.provider import (
35
47
  RefreshToken,
36
48
  TokenError,
37
49
  )
50
+ from mcp.server.auth.routes import cors_middleware
38
51
  from mcp.server.auth.settings import (
39
52
  ClientRegistrationOptions,
40
53
  RevocationOptions,
41
54
  )
42
55
  from mcp.shared.auth import OAuthClientInformationFull, OAuthToken
43
- from pydantic import AnyHttpUrl, AnyUrl, SecretStr
56
+ from pydantic import AnyHttpUrl, AnyUrl, BaseModel, Field, SecretStr
44
57
  from starlette.requests import Request
45
- from starlette.responses import RedirectResponse
58
+ from starlette.responses import HTMLResponse, RedirectResponse
46
59
  from starlette.routing import Route
60
+ from typing_extensions import override
47
61
 
48
- import fastmcp
62
+ from fastmcp import settings
49
63
  from fastmcp.server.auth.auth import OAuthProvider, TokenVerifier
50
- from fastmcp.server.auth.redirect_validation import validate_redirect_uri
64
+ from fastmcp.server.auth.handlers.authorize import AuthorizationHandler
65
+ from fastmcp.server.auth.jwt_issuer import (
66
+ JWTIssuer,
67
+ derive_jwt_key,
68
+ )
69
+ from fastmcp.server.auth.redirect_validation import (
70
+ validate_redirect_uri,
71
+ )
51
72
  from fastmcp.utilities.logging import get_logger
52
- from fastmcp.utilities.storage import JSONFileStorage, KVStorage
73
+ from fastmcp.utilities.ui import (
74
+ BUTTON_STYLES,
75
+ DETAIL_BOX_STYLES,
76
+ DETAILS_STYLES,
77
+ INFO_BOX_STYLES,
78
+ REDIRECT_SECTION_STYLES,
79
+ TOOLTIP_STYLES,
80
+ create_logo,
81
+ create_page,
82
+ create_secure_html_response,
83
+ )
53
84
 
54
85
  if TYPE_CHECKING:
55
86
  pass
@@ -57,6 +88,96 @@ if TYPE_CHECKING:
57
88
  logger = get_logger(__name__)
58
89
 
59
90
 
91
+ # -------------------------------------------------------------------------
92
+ # Constants
93
+ # -------------------------------------------------------------------------
94
+
95
+ # Default token expiration times
96
+ DEFAULT_ACCESS_TOKEN_EXPIRY_SECONDS: Final[int] = 60 * 60 # 1 hour
97
+ DEFAULT_AUTH_CODE_EXPIRY_SECONDS: Final[int] = 5 * 60 # 5 minutes
98
+
99
+ # HTTP client timeout
100
+ HTTP_TIMEOUT_SECONDS: Final[int] = 30
101
+
102
+
103
+ # -------------------------------------------------------------------------
104
+ # Pydantic Models
105
+ # -------------------------------------------------------------------------
106
+
107
+
108
+ class OAuthTransaction(BaseModel):
109
+ """OAuth transaction state for consent flow.
110
+
111
+ Stored server-side to track active authorization flows with client context.
112
+ Includes CSRF tokens for consent protection per MCP security best practices.
113
+ """
114
+
115
+ txn_id: str
116
+ client_id: str
117
+ client_redirect_uri: str
118
+ client_state: str
119
+ code_challenge: str | None
120
+ code_challenge_method: str
121
+ scopes: list[str]
122
+ created_at: float
123
+ resource: str | None = None
124
+ proxy_code_verifier: str | None = None
125
+ csrf_token: str | None = None
126
+ csrf_expires_at: float | None = None
127
+
128
+
129
+ class ClientCode(BaseModel):
130
+ """Client authorization code with PKCE and upstream tokens.
131
+
132
+ Stored server-side after upstream IdP callback. Contains the upstream
133
+ tokens bound to the client's PKCE challenge for secure token exchange.
134
+ """
135
+
136
+ code: str
137
+ client_id: str
138
+ redirect_uri: str
139
+ code_challenge: str | None
140
+ code_challenge_method: str
141
+ scopes: list[str]
142
+ idp_tokens: dict[str, Any]
143
+ expires_at: float
144
+ created_at: float
145
+
146
+
147
+ class UpstreamTokenSet(BaseModel):
148
+ """Stored upstream OAuth tokens from identity provider.
149
+
150
+ These tokens are obtained from the upstream provider (Google, GitHub, etc.)
151
+ and stored in plaintext within this model. Encryption is handled transparently
152
+ at the storage layer via FernetEncryptionWrapper. Tokens are never exposed to MCP clients.
153
+ """
154
+
155
+ upstream_token_id: str # Unique ID for this token set
156
+ access_token: str # Upstream access token
157
+ refresh_token: str | None # Upstream refresh token
158
+ refresh_token_expires_at: (
159
+ float | None
160
+ ) # Unix timestamp when refresh token expires (if known)
161
+ expires_at: float # Unix timestamp when access token expires
162
+ token_type: str # Usually "Bearer"
163
+ scope: str # Space-separated scopes
164
+ client_id: str # MCP client this is bound to
165
+ created_at: float # Unix timestamp
166
+ raw_token_data: dict[str, Any] = Field(default_factory=dict) # Full token response
167
+
168
+
169
+ class JTIMapping(BaseModel):
170
+ """Maps FastMCP token JTI to upstream token ID.
171
+
172
+ This allows stateless JWT validation while still being able to look up
173
+ the corresponding upstream token when tools need to access upstream APIs.
174
+ """
175
+
176
+ jti: str # JWT ID from FastMCP-issued token
177
+ upstream_token_id: str # References UpstreamTokenSet
178
+ created_at: float # Unix timestamp
179
+
180
+
60
181
  class ProxyDCRClient(OAuthClientInformationFull):
61
182
  """Client for DCR proxy with configurable redirect URI validation.
62
183
 
@@ -83,18 +204,8 @@ class ProxyDCRClient(OAuthClientInformationFull):
83
204
  arise from accepting arbitrary redirect URIs.
84
205
  """
85
206
 
86
- def __init__(
87
- self, *args, allowed_redirect_uri_patterns: list[str] | None = None, **kwargs
88
- ):
89
- """Initialize with allowed redirect URI patterns.
90
-
91
- Args:
92
- allowed_redirect_uri_patterns: List of allowed redirect URI patterns with wildcard support.
93
- If None, defaults to localhost-only patterns.
94
- If empty list, allows all redirect URIs.
95
- """
96
- super().__init__(*args, **kwargs)
97
- self._allowed_redirect_uri_patterns = allowed_redirect_uri_patterns
207
+ allowed_redirect_uri_patterns: list[str] | None = Field(default=None)
208
+ client_name: str | None = Field(default=None)
98
209
 
99
210
  def validate_redirect_uri(self, redirect_uri: AnyUrl | None) -> AnyUrl:
100
211
  """Validate redirect URI against allowed patterns.
@@ -106,7 +217,10 @@ class ProxyDCRClient(OAuthClientInformationFull):
106
217
  """
107
218
  if redirect_uri is not None:
108
219
  # Validate against allowed patterns
109
- if validate_redirect_uri(redirect_uri, self._allowed_redirect_uri_patterns):
220
+ if validate_redirect_uri(
221
+ redirect_uri=redirect_uri,
222
+ allowed_patterns=self.allowed_redirect_uri_patterns,
223
+ ):
110
224
  return redirect_uri
111
225
  # Fall back to normal validation if not in allowed patterns
112
226
  return super().validate_redirect_uri(redirect_uri)
@@ -114,12 +228,205 @@ class ProxyDCRClient(OAuthClientInformationFull):
114
228
  return super().validate_redirect_uri(redirect_uri)
115
229
 
116
230
 
117
- # Default token expiration times
118
- DEFAULT_ACCESS_TOKEN_EXPIRY_SECONDS: Final[int] = 60 * 60 # 1 hour
119
- DEFAULT_AUTH_CODE_EXPIRY_SECONDS: Final[int] = 5 * 60 # 5 minutes
231
+ # -------------------------------------------------------------------------
232
+ # Helper Functions
233
+ # -------------------------------------------------------------------------
234
+
235
+
236
+ def create_consent_html(
237
+ client_id: str,
238
+ redirect_uri: str,
239
+ scopes: list[str],
240
+ txn_id: str,
241
+ csrf_token: str,
242
+ client_name: str | None = None,
243
+ title: str = "Application Access Request",
244
+ server_name: str | None = None,
245
+ server_icon_url: str | None = None,
246
+ server_website_url: str | None = None,
247
+ client_website_url: str | None = None,
248
+ ) -> str:
249
+ """Create a styled HTML consent page for OAuth authorization requests."""
250
+ import html as html_module
251
+
252
+ client_display = html_module.escape(client_name or client_id)
253
+ server_name_escaped = html_module.escape(server_name or "FastMCP")
254
+
255
+ # Make server name a hyperlink if website URL is available
256
+ if server_website_url:
257
+ website_url_escaped = html_module.escape(server_website_url)
258
+ server_display = f'<a href="{website_url_escaped}" target="_blank" rel="noopener noreferrer" class="server-name-link">{server_name_escaped}</a>'
259
+ else:
260
+ server_display = server_name_escaped
261
+
262
+ # Build intro box with call-to-action
263
+ intro_box = f"""
264
+ <div class="info-box">
265
+ <p>The application <strong>{client_display}</strong> wants to access the MCP server <strong>{server_display}</strong>. Please ensure you recognize the callback address below.</p>
266
+ </div>
267
+ """
120
268
 
121
- # HTTP client timeout
122
- HTTP_TIMEOUT_SECONDS: Final[int] = 30
269
+ # Build redirect URI section (yellow box, centered)
270
+ redirect_uri_escaped = html_module.escape(redirect_uri)
271
+ redirect_section = f"""
272
+ <div class="redirect-section">
273
+ <span class="label">Credentials will be sent to:</span>
274
+ <div class="value">{redirect_uri_escaped}</div>
275
+ </div>
276
+ """
277
+
278
+ # Build advanced details with collapsible section
279
+ detail_rows = [
280
+ ("Application Name", html_module.escape(client_name or client_id)),
281
+ ("Application Website", html_module.escape(client_website_url or "N/A")),
282
+ ("Application ID", client_id),
283
+ ("Redirect URI", redirect_uri_escaped),
284
+ (
285
+ "Requested Scopes",
286
+ ", ".join(html_module.escape(s) for s in scopes) if scopes else "None",
287
+ ),
288
+ ]
289
+
290
+ detail_rows_html = "\n".join(
291
+ [
292
+ f"""
293
+ <div class="detail-row">
294
+ <div class="detail-label">{label}:</div>
295
+ <div class="detail-value">{value}</div>
296
+ </div>
297
+ """
298
+ for label, value in detail_rows
299
+ ]
300
+ )
301
+
302
+ advanced_details = f"""
303
+ <details>
304
+ <summary>Advanced Details</summary>
305
+ <div class="detail-box">
306
+ {detail_rows_html}
307
+ </div>
308
+ </details>
309
+ """
310
+
311
+ # Build form with buttons
312
+ form = f"""
313
+ <form id="consentForm" method="POST" action="/consent/submit">
314
+ <input type="hidden" name="txn_id" value="{txn_id}" />
315
+ <input type="hidden" name="csrf_token" value="{csrf_token}" />
316
+ <div class="button-group">
317
+ <button type="submit" name="action" value="approve" class="btn-approve">Allow Access</button>
318
+ <button type="submit" name="action" value="deny" class="btn-deny">Deny</button>
319
+ </div>
320
+ </form>
321
+ """
322
+
323
+ # Build help link with tooltip (identical to current implementation)
324
+ help_link = """
325
+ <div class="help-link-container">
326
+ <span class="help-link">
327
+ Why am I seeing this?
328
+ <span class="tooltip">
329
+ This FastMCP server requires your consent to allow a new client
330
+ to connect. This protects you from <a
331
+ href="https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#confused-deputy-problem"
332
+ target="_blank" class="tooltip-link">confused deputy
333
+ attacks</a>, where malicious clients could impersonate you
334
+ and steal access.<br><br>
335
+ <a
336
+ href="https://gofastmcp.com/servers/auth/oauth-proxy#confused-deputy-attacks"
337
+ target="_blank" class="tooltip-link">Learn more about
338
+ FastMCP security →</a>
339
+ </span>
340
+ </span>
341
+ </div>
342
+ """
343
+
344
+ # Build the page content
345
+ content = f"""
346
+ <div class="container">
347
+ {create_logo(icon_url=server_icon_url, alt_text=server_name or "FastMCP")}
348
+ <h1>Application Access Request</h1>
349
+ {intro_box}
350
+ {redirect_section}
351
+ {advanced_details}
352
+ {form}
353
+ </div>
354
+ {help_link}
355
+ """
356
+
357
+ # Additional styles needed for this page
358
+ additional_styles = (
359
+ INFO_BOX_STYLES
360
+ + REDIRECT_SECTION_STYLES
361
+ + DETAILS_STYLES
362
+ + DETAIL_BOX_STYLES
363
+ + BUTTON_STYLES
364
+ + TOOLTIP_STYLES
365
+ )
366
+
367
+ # Need to allow form-action for form submission
368
+ csp_policy = "default-src 'none'; style-src 'unsafe-inline'; img-src https:; base-uri 'none'; form-action *"
369
+
370
+ return create_page(
371
+ content=content,
372
+ title=title,
373
+ additional_styles=additional_styles,
374
+ csp_policy=csp_policy,
375
+ )
376
+
377
+
378
+ # -------------------------------------------------------------------------
379
+ # Handler Classes
380
+ # -------------------------------------------------------------------------
381
+
382
+
383
+ class TokenHandler(_SDKTokenHandler):
384
+ """TokenHandler that returns OAuth 2.1 compliant error responses.
385
+
386
+ The MCP SDK always returns HTTP 400 for all client authentication issues.
387
+ However, OAuth 2.1 Section 5.3 and the MCP specification require that
388
+ invalid or expired tokens MUST receive a HTTP 401 response.
389
+
390
+ This handler extends the base MCP SDK TokenHandler to transform client
391
+ authentication failures into OAuth 2.1 compliant responses:
392
+ - Changes 'unauthorized_client' to 'invalid_client' error code
393
+ - Returns HTTP 401 status code instead of 400 for client auth failures
394
+
395
+ Per OAuth 2.1 Section 5.3: "The authorization server MAY return an HTTP 401
396
+ (Unauthorized) status code to indicate which HTTP authentication schemes
397
+ are supported."
398
+
399
+ Per MCP spec: "Invalid or expired tokens MUST receive a HTTP 401 response."
400
+ """
401
+
402
+ def response(self, obj: TokenSuccessResponse | TokenErrorResponse):
403
+ """Override response method to provide OAuth 2.1 compliant error handling."""
404
+ # Check if this is a client authentication failure (not just unauthorized for grant type)
405
+ # unauthorized_client can mean two things:
406
+ # 1. Client authentication failed (client_id not found or wrong credentials) -> invalid_client 401
407
+ # 2. Client not authorized for this grant type -> unauthorized_client 400 (correct per spec)
408
+ if (
409
+ isinstance(obj, TokenErrorResponse)
410
+ and obj.error == "unauthorized_client"
411
+ and obj.error_description
412
+ and "Invalid client_id" in obj.error_description
413
+ ):
414
+ # Transform client auth failure to OAuth 2.1 compliant response
415
+ return PydanticJSONResponse(
416
+ content=TokenErrorResponse(
417
+ error="invalid_client",
418
+ error_description=obj.error_description,
419
+ error_uri=obj.error_uri,
420
+ ),
421
+ status_code=401,
422
+ headers={
423
+ "Cache-Control": "no-store",
424
+ "Pragma": "no-cache",
425
+ },
426
+ )
427
+
428
+ # Otherwise use default behavior from parent class
429
+ return super().response(obj)
123
430
 
124
431
 
125
432
  class OAuthProxy(OAuthProvider):
@@ -201,7 +508,6 @@ class OAuthProxy(OAuthProvider):
201
508
  State Management
202
509
  ---------------
203
510
  The proxy maintains minimal but crucial state:
204
- - _clients: DCR registrations (all use ProxyDCRClient for flexibility)
205
511
  - _oauth_transactions: Active authorization flows with client context
206
512
  - _client_codes: Authorization codes with PKCE challenges and upstream tokens
207
513
  - _access_tokens, _refresh_tokens: Token storage for revocation
@@ -257,7 +563,11 @@ class OAuthProxy(OAuthProvider):
257
563
  # Extra parameters to forward to token endpoint
258
564
  extra_token_params: dict[str, str] | None = None,
259
565
  # Client storage
260
- client_storage: KVStorage | None = None,
566
+ client_storage: AsyncKeyValue | None = None,
567
+ # JWT signing key
568
+ jwt_signing_key: str | bytes | None = None,
569
+ # Consent screen configuration
570
+ require_authorization_consent: bool = True,
261
571
  ):
262
572
  """Initialize the OAuth proxy provider.
263
573
 
@@ -291,10 +601,18 @@ class OAuthProxy(OAuthProvider):
291
601
  Example: {"audience": "https://api.example.com"}
292
602
  extra_token_params: Additional parameters to forward to the upstream token endpoint.
293
603
  Useful for provider-specific parameters during token exchange.
294
- client_storage: Storage implementation for OAuth client registrations.
295
- Defaults to file-based storage in ~/.fastmcp/oauth-proxy-clients/ if not specified.
296
- Pass any KVStorage implementation for custom storage backends.
604
+ client_storage: Storage backend for OAuth state (client registrations, tokens).
605
+ If None, an encrypted DiskStore will be created in the data directory.
606
+ jwt_signing_key: Secret for signing FastMCP JWT tokens (any string or bytes).
607
+ If bytes are provided, they will be used as-is.
608
+ If a string is provided, it will be derived into a 32-byte key using PBKDF2 (1.2M iterations).
609
+ If not provided, it will be derived from the upstream client secret using HKDF.
610
+ require_authorization_consent: Whether to require user consent before authorizing clients (default True).
611
+ When True, users see a consent screen before being redirected to the upstream IdP.
612
+ When False, authorization proceeds directly without user confirmation.
613
+ SECURITY WARNING: Only disable for local development or testing environments.
297
614
  """
615
+
298
616
  # Always enable DCR since we implement it locally for MCP clients
299
617
  client_registration_options = ClientRegistrationOptions(
300
618
  enabled=True,
@@ -316,12 +634,14 @@ class OAuthProxy(OAuthProvider):
316
634
  )
317
635
 
318
636
  # Store upstream configuration
319
- self._upstream_authorization_endpoint = upstream_authorization_endpoint
320
- self._upstream_token_endpoint = upstream_token_endpoint
321
- self._upstream_client_id = upstream_client_id
322
- self._upstream_client_secret = SecretStr(upstream_client_secret)
323
- self._upstream_revocation_endpoint = upstream_revocation_endpoint
324
- self._default_scope_str = " ".join(self.required_scopes or [])
637
+ self._upstream_authorization_endpoint: str = upstream_authorization_endpoint
638
+ self._upstream_token_endpoint: str = upstream_token_endpoint
639
+ self._upstream_client_id: str = upstream_client_id
640
+ self._upstream_client_secret: SecretStr = SecretStr(
641
+ secret_value=upstream_client_secret
642
+ )
643
+ self._upstream_revocation_endpoint: str | None = upstream_revocation_endpoint
644
+ self._default_scope_str: str = " ".join(self.required_scopes or [])
325
645
 
326
646
  # Store redirect configuration
327
647
  if not redirect_path:
@@ -330,23 +650,125 @@ class OAuthProxy(OAuthProvider):
330
650
  self._redirect_path = (
331
651
  redirect_path if redirect_path.startswith("/") else f"/{redirect_path}"
332
652
  )
333
- self._allowed_client_redirect_uris = allowed_client_redirect_uris
653
+
654
+ if (
655
+ isinstance(allowed_client_redirect_uris, list)
656
+ and not allowed_client_redirect_uris
657
+ ):
658
+ logger.warning(
659
+ "allowed_client_redirect_uris is empty list; no redirect URIs will be accepted. "
660
+ + "This will block all OAuth clients."
661
+ )
662
+ self._allowed_client_redirect_uris: list[str] | None = (
663
+ allowed_client_redirect_uris
664
+ )
334
665
 
335
666
  # PKCE configuration
336
- self._forward_pkce = forward_pkce
667
+ self._forward_pkce: bool = forward_pkce
337
668
 
338
669
  # Token endpoint authentication
339
- self._token_endpoint_auth_method = token_endpoint_auth_method
670
+ self._token_endpoint_auth_method: str | None = token_endpoint_auth_method
671
+
672
+ # Consent screen configuration
673
+ self._require_authorization_consent: bool = require_authorization_consent
674
+ if not require_authorization_consent:
675
+ logger.warning(
676
+ "Authorization consent screen disabled - only use for local development or testing. "
677
+ + "In production, this screen protects against confused deputy attacks."
678
+ )
340
679
 
341
680
  # Extra parameters for authorization and token endpoints
342
- self._extra_authorize_params = extra_authorize_params or {}
343
- self._extra_token_params = extra_token_params or {}
681
+ self._extra_authorize_params: dict[str, str] = extra_authorize_params or {}
682
+ self._extra_token_params: dict[str, str] = extra_token_params or {}
683
+
684
+ if jwt_signing_key is None:
685
+ jwt_signing_key = derive_jwt_key(
686
+ high_entropy_material=upstream_client_secret,
687
+ salt="fastmcp-jwt-signing-key",
688
+ )
689
+
690
+ if isinstance(jwt_signing_key, str):
691
+ if len(jwt_signing_key) < 12:
692
+ logger.warning(
693
+ "jwt_signing_key is less than 12 characters; it is recommended to use a longer. "
694
+ + "string for the key derivation."
695
+ )
696
+ jwt_signing_key = derive_jwt_key(
697
+ low_entropy_material=jwt_signing_key,
698
+ salt="fastmcp-jwt-signing-key",
699
+ )
344
700
 
345
- # Initialize client storage (default to file-based if not provided)
701
+ self._jwt_issuer: JWTIssuer = JWTIssuer(
702
+ issuer=str(self.base_url),
703
+ audience=f"{str(self.base_url).rstrip('/')}/mcp",
704
+ signing_key=jwt_signing_key,
705
+ )
706
+
707
+ # If the user does not provide a store, we will provide an encrypted disk store
346
708
  if client_storage is None:
347
- cache_dir = fastmcp.settings.home / "oauth-proxy-clients"
348
- client_storage = JSONFileStorage(cache_dir)
349
- self._client_storage = client_storage
709
+ storage_encryption_key = derive_jwt_key(
710
+ high_entropy_material=jwt_signing_key.decode(),
711
+ salt="fastmcp-storage-encryption-key",
712
+ )
713
+ client_storage = FernetEncryptionWrapper(
714
+ key_value=DiskStore(directory=settings.home / "oauth-proxy"),
715
+ fernet=Fernet(key=storage_encryption_key),
716
+ )
717
+
718
+ self._client_storage: AsyncKeyValue = client_storage
719
+
720
+ # Cache HTTPS check to avoid repeated logging
721
+ self._is_https: bool = str(self.base_url).startswith("https://")
722
+ if not self._is_https:
723
+ logger.warning(
724
+ "Using non-secure cookies for development; deploy with HTTPS for production."
725
+ )
726
+
727
+ self._upstream_token_store: PydanticAdapter[UpstreamTokenSet] = PydanticAdapter[
728
+ UpstreamTokenSet
729
+ ](
730
+ key_value=self._client_storage,
731
+ pydantic_model=UpstreamTokenSet,
732
+ default_collection="mcp-upstream-tokens",
733
+ raise_on_validation_error=True,
734
+ )
735
+
736
+ self._client_store: PydanticAdapter[ProxyDCRClient] = PydanticAdapter[
737
+ ProxyDCRClient
738
+ ](
739
+ key_value=self._client_storage,
740
+ pydantic_model=ProxyDCRClient,
741
+ default_collection="mcp-oauth-proxy-clients",
742
+ raise_on_validation_error=True,
743
+ )
744
+
745
+ # OAuth transaction storage for IdP callback forwarding
746
+ # Reuse client_storage with different collections for state management
747
+ self._transaction_store: PydanticAdapter[OAuthTransaction] = PydanticAdapter[
748
+ OAuthTransaction
749
+ ](
750
+ key_value=self._client_storage,
751
+ pydantic_model=OAuthTransaction,
752
+ default_collection="mcp-oauth-transactions",
753
+ raise_on_validation_error=True,
754
+ )
755
+
756
+ self._code_store: PydanticAdapter[ClientCode] = PydanticAdapter[ClientCode](
757
+ key_value=self._client_storage,
758
+ pydantic_model=ClientCode,
759
+ default_collection="mcp-authorization-codes",
760
+ raise_on_validation_error=True,
761
+ )
762
+
763
+ # Storage for JTI mappings (FastMCP token -> upstream token)
764
+ self._jti_mapping_store: PydanticAdapter[JTIMapping] = PydanticAdapter[
765
+ JTIMapping
766
+ ](
767
+ key_value=self._client_storage,
768
+ pydantic_model=JTIMapping,
769
+ default_collection="mcp-jti-mappings",
770
+ raise_on_validation_error=True,
771
+ )
350
772
 
351
773
  # Local state for token bookkeeping only (no client caching)
352
774
  self._access_tokens: dict[str, AccessToken] = {}
@@ -356,14 +778,8 @@ class OAuthProxy(OAuthProvider):
356
778
  self._access_to_refresh: dict[str, str] = {}
357
779
  self._refresh_to_access: dict[str, str] = {}
358
780
 
359
- # OAuth transaction storage for IdP callback forwarding
360
- self._oauth_transactions: dict[
361
- str, dict[str, Any]
362
- ] = {} # txn_id -> transaction_data
363
- self._client_codes: dict[str, dict[str, Any]] = {} # client_code -> code_data
364
-
365
781
  # Use the provided token validator
366
- self._token_validator = token_verifier
782
+ self._token_validator: TokenVerifier = token_verifier
367
783
 
368
784
  logger.debug(
369
785
  "Initialized OAuth proxy provider with upstream server %s",
@@ -393,6 +809,7 @@ class OAuthProxy(OAuthProvider):
393
809
  # Client Registration (Local Implementation)
394
810
  # -------------------------------------------------------------------------
395
811
 
812
+ @override
396
813
  async def get_client(self, client_id: str) -> OAuthClientInformationFull | None:
397
814
  """Get client information by ID. This is generally the random ID
398
815
  provided to the DCR client during registration, not the upstream client ID.
@@ -400,20 +817,15 @@ class OAuthProxy(OAuthProvider):
400
817
  For unregistered clients, returns None (which will raise an error in the SDK).
401
818
  """
402
819
  # Load from storage
403
- data = await self._client_storage.get(client_id)
404
- if not data:
820
+ if not (client := await self._client_store.get(key=client_id)):
405
821
  return None
406
822
 
407
- if client_data := data.get("client", None):
408
- return ProxyDCRClient(
409
- allowed_redirect_uri_patterns=data.get(
410
- "allowed_redirect_uri_patterns", self._allowed_client_redirect_uris
411
- ),
412
- **client_data,
413
- )
823
+ if client.allowed_redirect_uri_patterns is None:
824
+ client.allowed_redirect_uri_patterns = self._allowed_client_redirect_uris
414
825
 
415
- return None
826
+ return client
416
827
 
828
+ @override
417
829
  async def register_client(self, client_info: OAuthClientInformationFull) -> None:
418
830
  """Register a client locally
419
831
 
@@ -424,7 +836,7 @@ class OAuthProxy(OAuthProvider):
424
836
  """
425
837
 
426
838
  # Create a ProxyDCRClient with configured redirect URI validation
427
- proxy_client = ProxyDCRClient(
839
+ proxy_client: ProxyDCRClient = ProxyDCRClient(
428
840
  client_id=client_info.client_id,
429
841
  client_secret=client_info.client_secret,
430
842
  redirect_uris=client_info.redirect_uris or [AnyUrl("http://localhost")],
@@ -433,14 +845,13 @@ class OAuthProxy(OAuthProvider):
433
845
  scope=client_info.scope or self._default_scope_str,
434
846
  token_endpoint_auth_method="none",
435
847
  allowed_redirect_uri_patterns=self._allowed_client_redirect_uris,
848
+ client_name=getattr(client_info, "client_name", None),
436
849
  )
437
850
 
438
- # Store as structured dict with all needed metadata
439
- storage_data = {
440
- "client": proxy_client.model_dump(mode="json"),
441
- "allowed_redirect_uri_patterns": self._allowed_client_redirect_uris,
442
- }
443
- await self._client_storage.set(client_info.client_id, storage_data)
851
+ await self._client_store.put(
852
+ key=client_info.client_id,
853
+ value=proxy_client,
854
+ )
444
855
 
445
856
  # Log redirect URIs to help users discover what patterns they might need
446
857
  if client_info.redirect_uris:
@@ -461,18 +872,21 @@ class OAuthProxy(OAuthProvider):
461
872
  # Authorization Flow (Proxy to Upstream)
462
873
  # -------------------------------------------------------------------------
463
874
 
875
+ @override
464
876
  async def authorize(
465
877
  self,
466
878
  client: OAuthClientInformationFull,
467
879
  params: AuthorizationParams,
468
880
  ) -> str:
469
- """Start OAuth transaction and redirect to upstream IdP.
881
+ """Start OAuth transaction and route through consent interstitial.
882
+
883
+ Flow:
884
+ 1. Store transaction with client details and PKCE (if forwarding)
885
+ 2. Return local /consent URL; browser visits consent first
886
+ 3. Consent handler redirects to upstream IdP if approved/already approved
470
887
 
471
- This implements the DCR-compliant proxy pattern:
472
- 1. Store transaction with client details and PKCE challenge
473
- 2. Generate proxy's own PKCE parameters if forwarding is enabled
474
- 3. Use transaction ID as state for IdP
475
- 4. Redirect to IdP with our fixed callback URL and proxy's PKCE
888
+ If consent is disabled (require_authorization_consent=False), skip the consent screen
889
+ and redirect directly to the upstream IdP.
476
890
  """
477
891
  # Generate transaction ID for this authorization request
478
892
  txn_id = secrets.token_urlsafe(32)
@@ -488,80 +902,52 @@ class OAuthProxy(OAuthProvider):
488
902
  )
489
903
 
490
904
  # Store transaction data for IdP callback processing
491
- transaction_data = {
492
- "client_id": client.client_id,
493
- "client_redirect_uri": str(params.redirect_uri),
494
- "client_state": params.state,
495
- "code_challenge": params.code_challenge,
496
- "code_challenge_method": getattr(params, "code_challenge_method", "S256"),
497
- "scopes": params.scopes or [],
498
- "created_at": time.time(),
499
- }
500
-
501
- # Store proxy's PKCE verifier if we're forwarding
502
- if proxy_code_verifier:
503
- transaction_data["proxy_code_verifier"] = proxy_code_verifier
504
-
505
- self._oauth_transactions[txn_id] = transaction_data
506
-
507
- # Build query parameters for upstream IdP authorization request
508
- # Use our fixed IdP callback and transaction ID as state
509
- query_params: dict[str, Any] = {
510
- "response_type": "code",
511
- "client_id": self._upstream_client_id,
512
- "redirect_uri": f"{str(self.base_url).rstrip('/')}{self._redirect_path}",
513
- "state": txn_id, # Use txn_id as IdP state
514
- }
515
-
516
- # Add scopes - use client scopes or fallback to required scopes
517
- scopes_to_use = params.scopes or self.required_scopes or []
518
-
519
- if scopes_to_use:
520
- query_params["scope"] = " ".join(scopes_to_use)
521
-
522
- # Forward proxy's PKCE challenge to upstream if enabled
523
- if proxy_code_challenge:
524
- query_params["code_challenge"] = proxy_code_challenge
525
- query_params["code_challenge_method"] = "S256"
526
- logger.debug(
527
- "Forwarding proxy PKCE challenge to upstream for transaction %s",
528
- txn_id,
529
- )
905
+ transaction = OAuthTransaction(
906
+ txn_id=txn_id,
907
+ client_id=client.client_id,
908
+ client_redirect_uri=str(params.redirect_uri),
909
+ client_state=params.state or "",
910
+ code_challenge=params.code_challenge,
911
+ code_challenge_method=getattr(params, "code_challenge_method", "S256"),
912
+ scopes=params.scopes or [],
913
+ created_at=time.time(),
914
+ resource=getattr(params, "resource", None),
915
+ proxy_code_verifier=proxy_code_verifier,
916
+ )
917
+ await self._transaction_store.put(
918
+ key=txn_id,
919
+ value=transaction,
920
+ ttl=15 * 60, # Auto-expire after 15 minutes
921
+ )
530
922
 
531
- # Forward resource parameter if provided (RFC 8707)
532
- if params.resource:
533
- query_params["resource"] = params.resource
534
- logger.debug(
535
- "Forwarding resource indicator '%s' to upstream for transaction %s",
536
- params.resource,
537
- txn_id,
923
+ # If consent is disabled, skip consent screen and go directly to upstream IdP
924
+ if not self._require_authorization_consent:
925
+ upstream_url = self._build_upstream_authorize_url(
926
+ txn_id, transaction.model_dump()
538
927
  )
539
-
540
- # Add any extra authorization parameters configured for this proxy
541
- if self._extra_authorize_params:
542
- query_params.update(self._extra_authorize_params)
543
928
  logger.debug(
544
- "Adding extra authorization parameters for transaction %s: %s",
929
+ "Starting OAuth transaction %s for client %s, redirecting directly to upstream IdP (consent disabled, PKCE forwarding: %s)",
545
930
  txn_id,
546
- list(self._extra_authorize_params.keys()),
931
+ client.client_id,
932
+ "enabled" if proxy_code_challenge else "disabled",
547
933
  )
934
+ return upstream_url
548
935
 
549
- # Build the upstream authorization URL
550
- separator = "&" if "?" in self._upstream_authorization_endpoint else "?"
551
- upstream_url = f"{self._upstream_authorization_endpoint}{separator}{urlencode(query_params)}"
936
+ consent_url = f"{str(self.base_url).rstrip('/')}/consent?txn_id={txn_id}"
552
937
 
553
938
  logger.debug(
554
- "Starting OAuth transaction %s for client %s, redirecting to IdP (PKCE forwarding: %s)",
939
+ "Starting OAuth transaction %s for client %s, redirecting to consent page (PKCE forwarding: %s)",
555
940
  txn_id,
556
941
  client.client_id,
557
942
  "enabled" if proxy_code_challenge else "disabled",
558
943
  )
559
- return upstream_url
944
+ return consent_url
560
945
 
561
946
  # -------------------------------------------------------------------------
562
947
  # Authorization Code Handling
563
948
  # -------------------------------------------------------------------------
564
949
 
950
+ @override
565
951
  async def load_authorization_code(
566
952
  self,
567
953
  client: OAuthClientInformationFull,
@@ -573,22 +959,22 @@ class OAuthProxy(OAuthProvider):
573
959
  with PKCE challenge for validation.
574
960
  """
575
961
  # Look up client code data
576
- code_data = self._client_codes.get(authorization_code)
577
- if not code_data:
962
+ code_model = await self._code_store.get(key=authorization_code)
963
+ if not code_model:
578
964
  logger.debug("Authorization code not found: %s", authorization_code)
579
965
  return None
580
966
 
581
967
  # Check if code expired
582
- if time.time() > code_data["expires_at"]:
968
+ if time.time() > code_model.expires_at:
583
969
  logger.debug("Authorization code expired: %s", authorization_code)
584
- self._client_codes.pop(authorization_code, None)
970
+ _ = await self._code_store.delete(key=authorization_code)
585
971
  return None
586
972
 
587
973
  # Verify client ID matches
588
- if code_data["client_id"] != client.client_id:
974
+ if code_model.client_id != client.client_id:
589
975
  logger.debug(
590
976
  "Authorization code client ID mismatch: %s vs %s",
591
- code_data["client_id"],
977
+ code_model.client_id,
592
978
  client.client_id,
593
979
  )
594
980
  return None
@@ -597,75 +983,174 @@ class OAuthProxy(OAuthProvider):
597
983
  return AuthorizationCode(
598
984
  code=authorization_code,
599
985
  client_id=client.client_id,
600
- redirect_uri=code_data["redirect_uri"],
986
+ redirect_uri=AnyUrl(url=code_model.redirect_uri),
601
987
  redirect_uri_provided_explicitly=True,
602
- scopes=code_data["scopes"],
603
- expires_at=code_data["expires_at"],
604
- code_challenge=code_data.get("code_challenge", ""),
988
+ scopes=code_model.scopes,
989
+ expires_at=code_model.expires_at,
990
+ code_challenge=code_model.code_challenge or "",
605
991
  )
606
992
 
993
+ @override
607
994
  async def exchange_authorization_code(
608
995
  self,
609
996
  client: OAuthClientInformationFull,
610
997
  authorization_code: AuthorizationCode,
611
998
  ) -> OAuthToken:
612
- """Exchange authorization code for stored IdP tokens.
999
+ """Exchange authorization code for FastMCP-issued tokens.
1000
+
1001
+ Implements the token factory pattern:
1002
+ 1. Retrieves upstream tokens from stored authorization code
1003
+ 2. Extracts user identity from upstream token
1004
+ 3. Encrypts and stores upstream tokens
1005
+ 4. Issues FastMCP-signed JWT tokens
1006
+ 5. Returns FastMCP tokens (NOT upstream tokens)
613
1007
 
614
- For the DCR-compliant proxy flow, we return the IdP tokens that were obtained
615
- during the IdP callback exchange. PKCE validation is handled by the MCP framework.
1008
+ PKCE validation is handled by the MCP framework before this method is called.
616
1009
  """
617
1010
  # Look up stored code data
618
- code_data = self._client_codes.get(authorization_code.code)
619
- if not code_data:
1011
+ code_model = await self._code_store.get(key=authorization_code.code)
1012
+ if not code_model:
620
1013
  logger.error(
621
1014
  "Authorization code not found in client codes: %s",
622
1015
  authorization_code.code,
623
1016
  )
624
1017
  raise TokenError("invalid_grant", "Authorization code not found")
625
1018
 
626
- # Get stored IdP tokens
627
- idp_tokens = code_data["idp_tokens"]
1019
+ # Get stored upstream tokens
1020
+ idp_tokens = code_model.idp_tokens
628
1021
 
629
1022
  # Clean up client code (one-time use)
630
- self._client_codes.pop(authorization_code.code, None)
1023
+ await self._code_store.delete(key=authorization_code.code)
1024
+
1025
+ # Generate IDs for token storage
1026
+ upstream_token_id = secrets.token_urlsafe(32)
1027
+ access_jti = secrets.token_urlsafe(32)
1028
+ refresh_jti = (
1029
+ secrets.token_urlsafe(32) if idp_tokens.get("refresh_token") else None
1030
+ )
631
1031
 
632
- # Extract token information for local tracking
633
- access_token_value = idp_tokens["access_token"]
634
- refresh_token_value = idp_tokens.get("refresh_token")
1032
+ # Calculate token expiry times
635
1033
  expires_in = int(
636
1034
  idp_tokens.get("expires_in", DEFAULT_ACCESS_TOKEN_EXPIRY_SECONDS)
637
1035
  )
638
- expires_at = int(time.time() + expires_in)
639
1036
 
640
- # Store access token locally for tracking
641
- access_token = AccessToken(
642
- token=access_token_value,
1037
+ # Calculate refresh token expiry if provided by upstream
1038
+ # Some providers include refresh_expires_in, some don't
1039
+ refresh_expires_in = None
1040
+ refresh_token_expires_at = None
1041
+ if idp_tokens.get("refresh_token"):
1042
+ if "refresh_expires_in" in idp_tokens:
1043
+ refresh_expires_in = int(idp_tokens["refresh_expires_in"])
1044
+ refresh_token_expires_at = time.time() + refresh_expires_in
1045
+ logger.debug(
1046
+ "Upstream refresh token expires in %d seconds", refresh_expires_in
1047
+ )
1048
+ else:
1049
+ # Default to 30 days if upstream doesn't specify
1050
+ # This is conservative - most providers use longer expiry
1051
+ refresh_expires_in = 60 * 60 * 24 * 30 # 30 days
1052
+ refresh_token_expires_at = time.time() + refresh_expires_in
1053
+ logger.debug(
1054
+ "Upstream refresh token expiry unknown, using 30-day default"
1055
+ )
1056
+
1057
+ # Encrypt and store upstream tokens
1058
+ upstream_token_set = UpstreamTokenSet(
1059
+ upstream_token_id=upstream_token_id,
1060
+ access_token=idp_tokens["access_token"],
1061
+ refresh_token=idp_tokens["refresh_token"]
1062
+ if idp_tokens.get("refresh_token")
1063
+ else None,
1064
+ refresh_token_expires_at=refresh_token_expires_at,
1065
+ expires_at=time.time() + expires_in,
1066
+ token_type=idp_tokens.get("token_type", "Bearer"),
1067
+ scope=" ".join(authorization_code.scopes),
1068
+ client_id=client.client_id,
1069
+ created_at=time.time(),
1070
+ raw_token_data=idp_tokens,
1071
+ )
1072
+ await self._upstream_token_store.put(
1073
+ key=upstream_token_id,
1074
+ value=upstream_token_set,
1075
+ ttl=expires_in, # Auto-expire when access token expires
1076
+ )
1077
+ logger.debug("Stored encrypted upstream tokens (jti=%s)", access_jti[:8])
1078
+
1079
+ # Issue minimal FastMCP access token (just a reference via JTI)
1080
+ fastmcp_access_token = self._jwt_issuer.issue_access_token(
643
1081
  client_id=client.client_id,
644
1082
  scopes=authorization_code.scopes,
645
- expires_at=expires_at,
1083
+ jti=access_jti,
1084
+ expires_in=expires_in,
646
1085
  )
647
- self._access_tokens[access_token_value] = access_token
648
1086
 
649
- # Store refresh token if provided
650
- if refresh_token_value:
651
- refresh_token = RefreshToken(
652
- token=refresh_token_value,
1087
+ # Issue minimal FastMCP refresh token if upstream provided one
1088
+ # Use upstream refresh token expiry to align lifetimes
1089
+ fastmcp_refresh_token = None
1090
+ if refresh_jti and refresh_expires_in:
1091
+ fastmcp_refresh_token = self._jwt_issuer.issue_refresh_token(
653
1092
  client_id=client.client_id,
654
1093
  scopes=authorization_code.scopes,
655
- expires_at=None, # Refresh tokens typically don't expire
1094
+ jti=refresh_jti,
1095
+ expires_in=refresh_expires_in,
1096
+ )
1097
+
1098
+ # Store JTI mappings
1099
+ await self._jti_mapping_store.put(
1100
+ key=access_jti,
1101
+ value=JTIMapping(
1102
+ jti=access_jti,
1103
+ upstream_token_id=upstream_token_id,
1104
+ created_at=time.time(),
1105
+ ),
1106
+ ttl=expires_in, # Auto-expire with access token
1107
+ )
1108
+ if refresh_jti:
1109
+ await self._jti_mapping_store.put(
1110
+ key=refresh_jti,
1111
+ value=JTIMapping(
1112
+ jti=refresh_jti,
1113
+ upstream_token_id=upstream_token_id,
1114
+ created_at=time.time(),
1115
+ ),
1116
+ ttl=60 * 60 * 24 * 30, # Auto-expire with refresh token (30 days)
656
1117
  )
657
- self._refresh_tokens[refresh_token_value] = refresh_token
658
1118
 
1119
+ # Store FastMCP access token for MCP framework validation
1120
+ self._access_tokens[fastmcp_access_token] = AccessToken(
1121
+ token=fastmcp_access_token,
1122
+ client_id=client.client_id,
1123
+ scopes=authorization_code.scopes,
1124
+ expires_at=int(time.time() + expires_in),
1125
+ )
1126
+
1127
+ # Store FastMCP refresh token if provided
1128
+ if fastmcp_refresh_token:
1129
+ self._refresh_tokens[fastmcp_refresh_token] = RefreshToken(
1130
+ token=fastmcp_refresh_token,
1131
+ client_id=client.client_id,
1132
+ scopes=authorization_code.scopes,
1133
+ expires_at=None,
1134
+ )
659
1135
  # Maintain token relationships for cleanup
660
- self._access_to_refresh[access_token_value] = refresh_token_value
661
- self._refresh_to_access[refresh_token_value] = access_token_value
1136
+ self._access_to_refresh[fastmcp_access_token] = fastmcp_refresh_token
1137
+ self._refresh_to_access[fastmcp_refresh_token] = fastmcp_access_token
662
1138
 
663
1139
  logger.debug(
664
- "Successfully exchanged client code for stored IdP tokens (client: %s)",
1140
+ "Issued FastMCP tokens for client=%s (access_jti=%s, refresh_jti=%s)",
665
1141
  client.client_id,
1142
+ access_jti[:8],
1143
+ refresh_jti[:8] if refresh_jti else "none",
666
1144
  )
667
1145
 
668
- return OAuthToken(**idp_tokens) # type: ignore[arg-type]
1146
+ # Return FastMCP-issued tokens (NOT upstream tokens!)
1147
+ return OAuthToken(
1148
+ access_token=fastmcp_access_token,
1149
+ token_type="Bearer",
1150
+ expires_in=expires_in,
1151
+ refresh_token=fastmcp_refresh_token,
1152
+ scope=" ".join(authorization_code.scopes),
1153
+ )
669
1154
 
670
1155
  # -------------------------------------------------------------------------
671
1156
  # Refresh Token Flow
@@ -685,9 +1170,45 @@ class OAuthProxy(OAuthProvider):
685
1170
  refresh_token: RefreshToken,
686
1171
  scopes: list[str],
687
1172
  ) -> OAuthToken:
688
- """Exchange refresh token for new access token using authlib."""
1173
+ """Exchange FastMCP refresh token for new FastMCP access token.
1174
+
1175
+ Implements two-tier refresh:
1176
+ 1. Verify FastMCP refresh token
1177
+ 2. Look up upstream token via JTI mapping
1178
+ 3. Refresh upstream token with upstream provider
1179
+ 4. Update stored upstream token
1180
+ 5. Issue new FastMCP access token
1181
+ 6. Keep same FastMCP refresh token (unless upstream rotates)
1182
+ """
1183
+ # Verify FastMCP refresh token
1184
+ try:
1185
+ refresh_payload = self._jwt_issuer.verify_token(refresh_token.token)
1186
+ refresh_jti = refresh_payload["jti"]
1187
+ except Exception as e:
1188
+ logger.debug("FastMCP refresh token validation failed: %s", e)
1189
+ raise TokenError("invalid_grant", "Invalid refresh token") from e
689
1190
 
690
- # Use authlib's AsyncOAuth2Client for refresh token exchange
1191
+ # Look up upstream token via JTI mapping
1192
+ jti_mapping = await self._jti_mapping_store.get(key=refresh_jti)
1193
+ if not jti_mapping:
1194
+ logger.error("JTI mapping not found for refresh token: %s", refresh_jti[:8])
1195
+ raise TokenError("invalid_grant", "Refresh token mapping not found")
1196
+
1197
+ upstream_token_set = await self._upstream_token_store.get(
1198
+ key=jti_mapping.upstream_token_id
1199
+ )
1200
+ if not upstream_token_set:
1201
+ logger.error(
1202
+ "Upstream token set not found: %s", jti_mapping.upstream_token_id[:8]
1203
+ )
1204
+ raise TokenError("invalid_grant", "Upstream token not found")
1205
+
1206
+ # Decrypt upstream refresh token
1207
+ if not upstream_token_set.refresh_token:
1208
+ logger.error("No upstream refresh token available")
1209
+ raise TokenError("invalid_grant", "Refresh not supported for this token")
1210
+
1211
+ # Refresh upstream token using authlib
691
1212
  oauth_client = AsyncOAuth2Client(
692
1213
  client_id=self._upstream_client_id,
693
1214
  client_secret=self._upstream_client_secret.get_secret_value(),
@@ -696,77 +1217,205 @@ class OAuthProxy(OAuthProvider):
696
1217
  )
697
1218
 
698
1219
  try:
699
- logger.debug("Using authlib to refresh token from upstream")
700
-
701
- # Let authlib handle the refresh token exchange
1220
+ logger.debug("Refreshing upstream token (jti=%s)", refresh_jti[:8])
702
1221
  token_response: dict[str, Any] = await oauth_client.refresh_token( # type: ignore[misc]
703
1222
  url=self._upstream_token_endpoint,
704
- refresh_token=refresh_token.token,
1223
+ refresh_token=upstream_token_set.refresh_token,
705
1224
  scope=" ".join(scopes) if scopes else None,
706
1225
  )
707
-
708
- logger.debug(
709
- "Successfully refreshed access token via authlib (client: %s)",
710
- client.client_id,
711
- )
712
-
1226
+ logger.debug("Successfully refreshed upstream token")
713
1227
  except Exception as e:
714
- logger.error("Authlib refresh token exchange failed: %s", e)
715
- raise TokenError(
716
- "invalid_grant", f"Upstream refresh token exchange failed: {e}"
717
- ) from e
1228
+ logger.error("Upstream token refresh failed: %s", e)
1229
+ raise TokenError("invalid_grant", f"Upstream refresh failed: {e}") from e
718
1230
 
719
- # Update local token storage
720
- new_access_token = token_response["access_token"]
721
- expires_in = int(
1231
+ # Update stored upstream token
1232
+ new_expires_in = int(
722
1233
  token_response.get("expires_in", DEFAULT_ACCESS_TOKEN_EXPIRY_SECONDS)
723
1234
  )
1235
+ upstream_token_set.access_token = token_response["access_token"]
1236
+ upstream_token_set.expires_at = time.time() + new_expires_in
1237
+
1238
+ # Handle upstream refresh token rotation and expiry
1239
+ new_refresh_expires_in = None
1240
+ if new_upstream_refresh := token_response.get("refresh_token"):
1241
+ if new_upstream_refresh != upstream_token_set.refresh_token:
1242
+ upstream_token_set.refresh_token = new_upstream_refresh
1243
+ logger.debug("Upstream refresh token rotated")
1244
+
1245
+ # Update refresh token expiry if provided
1246
+ if "refresh_expires_in" in token_response:
1247
+ new_refresh_expires_in = int(token_response["refresh_expires_in"])
1248
+ upstream_token_set.refresh_token_expires_at = (
1249
+ time.time() + new_refresh_expires_in
1250
+ )
1251
+ logger.debug(
1252
+ "Upstream refresh token expires in %d seconds",
1253
+ new_refresh_expires_in,
1254
+ )
1255
+ elif upstream_token_set.refresh_token_expires_at:
1256
+ # Keep existing expiry if upstream doesn't provide new one
1257
+ new_refresh_expires_in = int(
1258
+ upstream_token_set.refresh_token_expires_at - time.time()
1259
+ )
1260
+ else:
1261
+ # Default to 30 days if unknown
1262
+ new_refresh_expires_in = 60 * 60 * 24 * 30
1263
+ upstream_token_set.refresh_token_expires_at = (
1264
+ time.time() + new_refresh_expires_in
1265
+ )
1266
+
1267
+ upstream_token_set.raw_token_data = token_response
1268
+ await self._upstream_token_store.put(
1269
+ key=upstream_token_set.upstream_token_id,
1270
+ value=upstream_token_set,
1271
+ ttl=new_expires_in, # Auto-expire when refreshed access token expires
1272
+ )
724
1273
 
725
- self._access_tokens[new_access_token] = AccessToken(
726
- token=new_access_token,
1274
+ # Issue new minimal FastMCP access token (just a reference via JTI)
1275
+ new_access_jti = secrets.token_urlsafe(32)
1276
+ new_fastmcp_access = self._jwt_issuer.issue_access_token(
727
1277
  client_id=client.client_id,
728
1278
  scopes=scopes,
729
- expires_at=int(time.time() + expires_in),
1279
+ jti=new_access_jti,
1280
+ expires_in=new_expires_in,
730
1281
  )
731
1282
 
732
- # Handle refresh token rotation if new one provided
733
- if "refresh_token" in token_response:
734
- new_refresh_token = token_response["refresh_token"]
735
- if new_refresh_token != refresh_token.token:
736
- # Remove old refresh token
737
- self._refresh_tokens.pop(refresh_token.token, None)
738
- old_access = self._refresh_to_access.pop(refresh_token.token, None)
739
- if old_access:
740
- self._access_to_refresh.pop(old_access, None)
741
-
742
- # Store new refresh token
743
- self._refresh_tokens[new_refresh_token] = RefreshToken(
744
- token=new_refresh_token,
745
- client_id=client.client_id,
746
- scopes=scopes,
747
- expires_at=None,
748
- )
749
- self._access_to_refresh[new_access_token] = new_refresh_token
750
- self._refresh_to_access[new_refresh_token] = new_access_token
1283
+ # Store new access token JTI mapping
1284
+ await self._jti_mapping_store.put(
1285
+ key=new_access_jti,
1286
+ value=JTIMapping(
1287
+ jti=new_access_jti,
1288
+ upstream_token_id=upstream_token_set.upstream_token_id,
1289
+ created_at=time.time(),
1290
+ ),
1291
+ ttl=new_expires_in, # Auto-expire with refreshed access token
1292
+ )
1293
+
1294
+ # Issue NEW minimal FastMCP refresh token (rotation for security)
1295
+ # Use upstream refresh token expiry to align lifetimes
1296
+ new_refresh_jti = secrets.token_urlsafe(32)
1297
+ new_fastmcp_refresh = self._jwt_issuer.issue_refresh_token(
1298
+ client_id=client.client_id,
1299
+ scopes=scopes,
1300
+ jti=new_refresh_jti,
1301
+ expires_in=new_refresh_expires_in
1302
+ or 60 * 60 * 24 * 30, # Fallback to 30 days
1303
+ )
1304
+
1305
+ # Store new refresh token JTI mapping with aligned expiry
1306
+ refresh_ttl = new_refresh_expires_in or 60 * 60 * 24 * 30
1307
+ await self._jti_mapping_store.put(
1308
+ key=new_refresh_jti,
1309
+ value=JTIMapping(
1310
+ jti=new_refresh_jti,
1311
+ upstream_token_id=upstream_token_set.upstream_token_id,
1312
+ created_at=time.time(),
1313
+ ),
1314
+ ttl=refresh_ttl, # Align with upstream refresh token expiry
1315
+ )
1316
+
1317
+ # Invalidate old refresh token (refresh token rotation - enforces one-time use)
1318
+ await self._jti_mapping_store.delete(key=refresh_jti)
1319
+ logger.debug(
1320
+ "Rotated refresh token (old JTI invalidated - one-time use enforced)"
1321
+ )
1322
+
1323
+ # Update local token tracking
1324
+ self._access_tokens[new_fastmcp_access] = AccessToken(
1325
+ token=new_fastmcp_access,
1326
+ client_id=client.client_id,
1327
+ scopes=scopes,
1328
+ expires_at=int(time.time() + new_expires_in),
1329
+ )
1330
+ self._refresh_tokens[new_fastmcp_refresh] = RefreshToken(
1331
+ token=new_fastmcp_refresh,
1332
+ client_id=client.client_id,
1333
+ scopes=scopes,
1334
+ expires_at=None,
1335
+ )
1336
+
1337
+ # Update token relationship mappings
1338
+ self._access_to_refresh[new_fastmcp_access] = new_fastmcp_refresh
1339
+ self._refresh_to_access[new_fastmcp_refresh] = new_fastmcp_access
751
1340
 
752
- return OAuthToken(**token_response) # type: ignore[arg-type]
1341
+ # Clean up old token from in-memory tracking
1342
+ self._refresh_tokens.pop(refresh_token.token, None)
1343
+ old_access = self._refresh_to_access.pop(refresh_token.token, None)
1344
+ if old_access:
1345
+ self._access_tokens.pop(old_access, None)
1346
+ self._access_to_refresh.pop(old_access, None)
1347
+
1348
+ logger.info(
1349
+ "Issued new FastMCP tokens (rotated refresh) for client=%s (access_jti=%s, refresh_jti=%s)",
1350
+ client.client_id,
1351
+ new_access_jti[:8],
1352
+ new_refresh_jti[:8],
1353
+ )
1354
+
1355
+ # Return new FastMCP tokens (both access AND refresh are new)
1356
+ return OAuthToken(
1357
+ access_token=new_fastmcp_access,
1358
+ token_type="Bearer",
1359
+ expires_in=new_expires_in,
1360
+ refresh_token=new_fastmcp_refresh, # NEW refresh token (rotated)
1361
+ scope=" ".join(scopes),
1362
+ )
753
1363
 
754
1364
  # -------------------------------------------------------------------------
755
1365
  # Token Validation
756
1366
  # -------------------------------------------------------------------------
757
1367
 
758
1368
  async def load_access_token(self, token: str) -> AccessToken | None:
759
- """Validate access token using upstream JWKS.
1369
+ """Validate FastMCP JWT by swapping for upstream token.
760
1370
 
761
- Delegates to the JWT verifier which handles signature validation,
762
- expiration checking, and claims validation using the upstream JWKS.
1371
+ This implements the token swap pattern:
1372
+ 1. Verify FastMCP JWT signature (proves it's our token)
1373
+ 2. Look up upstream token via JTI mapping
1374
+ 3. Decrypt upstream token
1375
+ 4. Validate upstream token with provider (GitHub API, JWT validation, etc.)
1376
+ 5. Return upstream validation result
1377
+
1378
+ The FastMCP JWT is a reference token - all authorization data comes
1379
+ from validating the upstream token via the TokenVerifier.
763
1380
  """
764
- result = await self._token_validator.verify_token(token)
765
- if result:
766
- logger.debug("Token validated successfully")
767
- else:
768
- logger.debug("Token validation failed")
769
- return result
1381
+ try:
1382
+ # 1. Verify FastMCP JWT signature and claims
1383
+ payload = self._jwt_issuer.verify_token(token)
1384
+ jti = payload["jti"]
1385
+
1386
+ # 2. Look up upstream token via JTI mapping
1387
+ jti_mapping = await self._jti_mapping_store.get(key=jti)
1388
+ if not jti_mapping:
1389
+ logger.debug("JTI mapping not found: %s", jti)
1390
+ return None
1391
+
1392
+ upstream_token_set = await self._upstream_token_store.get(
1393
+ key=jti_mapping.upstream_token_id
1394
+ )
1395
+ if not upstream_token_set:
1396
+ logger.debug(
1397
+ "Upstream token not found: %s", jti_mapping.upstream_token_id
1398
+ )
1399
+ return None
1400
+
1401
+ # 3. Validate with upstream provider (delegated to TokenVerifier)
1402
+ # This calls the real token validator (GitHub API, JWKS, etc.)
1403
+ validated = await self._token_validator.verify_token(
1404
+ upstream_token_set.access_token
1405
+ )
1406
+
1407
+ if not validated:
1408
+ logger.debug("Upstream token validation failed")
1409
+ return None
1410
+
1411
+ logger.debug(
1412
+ "Token swap successful for JTI=%s (upstream validated)", jti[:8]
1413
+ )
1414
+ return validated
1415
+
1416
+ except Exception as e:
1417
+ logger.debug("Token swap validation failed: %s", e)
1418
+ return None
770
1419
 
771
1420
  # -------------------------------------------------------------------------
772
1421
  # Token Revocation
@@ -819,21 +1468,22 @@ class OAuthProxy(OAuthProvider):
819
1468
  def get_routes(
820
1469
  self,
821
1470
  mcp_path: str | None = None,
822
- mcp_endpoint: Any | None = None,
823
1471
  ) -> list[Route]:
824
- """Get OAuth routes with custom proxy token handler.
1472
+ """Get OAuth routes with custom handlers for better error UX.
825
1473
 
826
- This method creates standard OAuth routes and replaces the token endpoint
827
- with our proxy handler that forwards requests to the upstream OAuth server.
1474
+ This method creates standard OAuth routes and replaces:
1475
+ - /authorize endpoint: Enhanced error responses for unregistered clients
1476
+ - /token endpoint: OAuth 2.1 compliant error codes
828
1477
 
829
1478
  Args:
830
1479
  mcp_path: The path where the MCP endpoint is mounted (e.g., "/mcp")
831
- mcp_endpoint: The MCP endpoint handler to protect with auth
1480
+ This is used to advertise the resource URL in metadata.
832
1481
  """
833
1482
  # Get standard OAuth routes from parent class
834
- routes = super().get_routes(mcp_path, mcp_endpoint)
1483
+ routes = super().get_routes(mcp_path)
835
1484
  custom_routes = []
836
1485
  token_route_found = False
1486
+ authorize_route_found = False
837
1487
 
838
1488
  logger.debug(
839
1489
  f"get_routes called - configuring OAuth routes in {len(routes)} routes"
@@ -844,16 +1494,52 @@ class OAuthProxy(OAuthProvider):
844
1494
  f"Route {i}: {route} - path: {getattr(route, 'path', 'N/A')}, methods: {getattr(route, 'methods', 'N/A')}"
845
1495
  )
846
1496
 
847
- # Keep all standard OAuth routes unchanged - our DCR-compliant flow handles everything
848
- custom_routes.append(route)
849
-
1497
+ # Replace the authorize endpoint with our enhanced handler for better error UX
850
1498
  if (
1499
+ isinstance(route, Route)
1500
+ and route.path == "/authorize"
1501
+ and route.methods is not None
1502
+ and ("GET" in route.methods or "POST" in route.methods)
1503
+ ):
1504
+ authorize_route_found = True
1505
+ # Replace with our enhanced authorization handler
1506
+ authorize_handler = AuthorizationHandler(
1507
+ provider=self,
1508
+ base_url=self.base_url,
1509
+ server_name=None, # Could be extended to pass server metadata
1510
+ server_icon_url=None,
1511
+ )
1512
+ custom_routes.append(
1513
+ Route(
1514
+ path="/authorize",
1515
+ endpoint=authorize_handler.handle,
1516
+ methods=["GET", "POST"],
1517
+ )
1518
+ )
1519
+ # Replace the token endpoint with our custom handler that returns proper OAuth 2.1 error codes
1520
+ elif (
851
1521
  isinstance(route, Route)
852
1522
  and route.path == "/token"
853
1523
  and route.methods is not None
854
1524
  and "POST" in route.methods
855
1525
  ):
856
1526
  token_route_found = True
1527
+ # Replace with our OAuth 2.1 compliant token handler
1528
+ token_handler = TokenHandler(
1529
+ provider=self, client_authenticator=ClientAuthenticator(self)
1530
+ )
1531
+ custom_routes.append(
1532
+ Route(
1533
+ path="/token",
1534
+ endpoint=cors_middleware(
1535
+ token_handler.handle, ["POST", "OPTIONS"]
1536
+ ),
1537
+ methods=["POST", "OPTIONS"],
1538
+ )
1539
+ )
1540
+ else:
1541
+ # Keep all other standard OAuth routes unchanged
1542
+ custom_routes.append(route)
857
1543
 
858
1544
  # Add OAuth callback endpoint for forwarding to client callbacks
859
1545
  custom_routes.append(
@@ -864,8 +1550,18 @@ class OAuthProxy(OAuthProvider):
864
1550
  )
865
1551
  )
866
1552
 
1553
+ # Add consent endpoints
1554
+ custom_routes.append(
1555
+ Route(path="/consent", endpoint=self._show_consent_page, methods=["GET"])
1556
+ )
1557
+ custom_routes.append(
1558
+ Route(
1559
+ path="/consent/submit", endpoint=self._submit_consent, methods=["POST"]
1560
+ )
1561
+ )
1562
+
867
1563
  logger.debug(
868
- f"✅ OAuth routes configured: token_endpoint={token_route_found}, total routes={len(custom_routes)} (includes OAuth callback)"
1564
+ f"✅ OAuth routes configured: authorize_endpoint={authorize_route_found}, token_endpoint={token_route_found}, total routes={len(custom_routes)} (includes OAuth callback + consent)"
869
1565
  )
870
1566
  return custom_routes
871
1567
 
@@ -907,13 +1603,14 @@ class OAuthProxy(OAuthProvider):
907
1603
  )
908
1604
 
909
1605
  # Look up transaction data
910
- transaction = self._oauth_transactions.get(txn_id)
911
- if not transaction:
1606
+ transaction_model = await self._transaction_store.get(key=txn_id)
1607
+ if not transaction_model:
912
1608
  logger.error("IdP callback with invalid transaction ID: %s", txn_id)
913
1609
  return RedirectResponse(
914
1610
  url="data:text/html,<h1>OAuth Error</h1><p>Invalid or expired transaction</p>",
915
1611
  status_code=302,
916
1612
  )
1613
+ transaction = transaction_model.model_dump()
917
1614
 
918
1615
  # Exchange IdP code for tokens (server-side)
919
1616
  oauth_client = AsyncOAuth2Client(
@@ -977,19 +1674,24 @@ class OAuthProxy(OAuthProvider):
977
1674
  code_expires_at = int(time.time() + DEFAULT_AUTH_CODE_EXPIRY_SECONDS)
978
1675
 
979
1676
  # Store client code with PKCE challenge and IdP tokens
980
- self._client_codes[client_code] = {
981
- "client_id": transaction["client_id"],
982
- "redirect_uri": transaction["client_redirect_uri"],
983
- "code_challenge": transaction["code_challenge"],
984
- "code_challenge_method": transaction["code_challenge_method"],
985
- "scopes": transaction["scopes"],
986
- "idp_tokens": idp_tokens,
987
- "expires_at": code_expires_at,
988
- "created_at": time.time(),
989
- }
1677
+ await self._code_store.put(
1678
+ key=client_code,
1679
+ value=ClientCode(
1680
+ code=client_code,
1681
+ client_id=transaction["client_id"],
1682
+ redirect_uri=transaction["client_redirect_uri"],
1683
+ code_challenge=transaction["code_challenge"],
1684
+ code_challenge_method=transaction["code_challenge_method"],
1685
+ scopes=transaction["scopes"],
1686
+ idp_tokens=idp_tokens,
1687
+ expires_at=code_expires_at,
1688
+ created_at=time.time(),
1689
+ ),
1690
+ ttl=DEFAULT_AUTH_CODE_EXPIRY_SECONDS, # Auto-expire after 5 minutes
1691
+ )
990
1692
 
991
1693
  # Clean up transaction
992
- self._oauth_transactions.pop(txn_id, None)
1694
+ await self._transaction_store.delete(key=txn_id)
993
1695
 
994
1696
  # Build client callback URL with our code and original state
995
1697
  client_redirect_uri = transaction["client_redirect_uri"]
@@ -1016,3 +1718,315 @@ class OAuthProxy(OAuthProvider):
1016
1718
  url="data:text/html,<h1>OAuth Error</h1><p>Internal server error during IdP callback</p>",
1017
1719
  status_code=302,
1018
1720
  )
1721
+
1722
+ # -------------------------------------------------------------------------
1723
+ # Consent Interstitial
1724
+ # -------------------------------------------------------------------------
1725
+
1726
+ def _normalize_uri(self, uri: str) -> str:
1727
+ """Normalize a URI to a canonical form for consent tracking."""
1728
+ parsed = urlparse(uri)
1729
+ path = parsed.path or ""
1730
+ normalized = f"{parsed.scheme.lower()}://{parsed.netloc.lower()}{path}"
1731
+ if normalized.endswith("/") and len(path) > 1:
1732
+ normalized = normalized[:-1]
1733
+ return normalized
1734
+
1735
+ def _make_client_key(self, client_id: str, redirect_uri: str | AnyUrl) -> str:
1736
+ """Create a stable key for consent tracking from client_id and redirect_uri."""
1737
+ normalized = self._normalize_uri(str(redirect_uri))
1738
+ return f"{client_id}:{normalized}"
1739
+
1740
+ def _cookie_name(self, base_name: str) -> str:
1741
+ """Return secure cookie name for HTTPS, fallback for HTTP development."""
1742
+ if self._is_https:
1743
+ return f"__Host-{base_name}"
1744
+ return f"__{base_name}"
1745
+
1746
+ def _sign_cookie(self, payload: str) -> str:
1747
+ """Sign a cookie payload with HMAC-SHA256.
1748
+
1749
+ Returns: base64(payload).base64(signature)
1750
+ """
1751
+ # Use upstream client secret as signing key
1752
+ key = self._upstream_client_secret.get_secret_value().encode()
1753
+ signature = hmac.new(key, payload.encode(), hashlib.sha256).digest()
1754
+ signature_b64 = base64.b64encode(signature).decode()
1755
+ return f"{payload}.{signature_b64}"
1756
+
1757
+ def _verify_cookie(self, signed_value: str) -> str | None:
1758
+ """Verify and extract payload from signed cookie.
1759
+
1760
+ Returns: payload if signature valid, None otherwise
1761
+ """
1762
+ try:
1763
+ if "." not in signed_value:
1764
+ return None
1765
+ payload, signature_b64 = signed_value.rsplit(".", 1)
1766
+
1767
+ # Verify signature
1768
+ key = self._upstream_client_secret.get_secret_value().encode()
1769
+ expected_sig = hmac.new(key, payload.encode(), hashlib.sha256).digest()
1770
+ provided_sig = base64.b64decode(signature_b64.encode())
1771
+
1772
+ # Constant-time comparison
1773
+ if not hmac.compare_digest(expected_sig, provided_sig):
1774
+ return None
1775
+
1776
+ return payload
1777
+ except Exception:
1778
+ return None
1779
+
1780
+ def _decode_list_cookie(self, request: Request, base_name: str) -> list[str]:
1781
+ """Decode and verify a signed base64-encoded JSON list from cookie. Returns [] if missing/invalid."""
1782
+ # Prefer secure name, but also check non-secure variant for dev
1783
+ secure_name = self._cookie_name(base_name)
1784
+ raw = request.cookies.get(secure_name) or request.cookies.get(f"__{base_name}")
1785
+ if not raw:
1786
+ return []
1787
+ try:
1788
+ # Verify signature
1789
+ payload = self._verify_cookie(raw)
1790
+ if not payload:
1791
+ logger.debug("Cookie signature verification failed for %s", secure_name)
1792
+ return []
1793
+
1794
+ # Decode payload
1795
+ data = base64.b64decode(payload.encode())
1796
+ value = json.loads(data.decode())
1797
+ if isinstance(value, list):
1798
+ return [str(x) for x in value]
1799
+ except Exception:
1800
+ logger.debug("Failed to decode cookie %s; treating as empty", secure_name)
1801
+ return []
1802
+
1803
+ def _encode_list_cookie(self, values: list[str]) -> str:
1804
+ """Encode values to base64 and sign with HMAC.
1805
+
1806
+ Returns: signed cookie value (payload.signature)
1807
+ """
1808
+ payload = json.dumps(values, separators=(",", ":")).encode()
1809
+ payload_b64 = base64.b64encode(payload).decode()
1810
+ return self._sign_cookie(payload_b64)
1811
+
1812
+ def _set_list_cookie(
1813
+ self,
1814
+ response: HTMLResponse | RedirectResponse,
1815
+ base_name: str,
1816
+ value_b64: str,
1817
+ max_age: int,
1818
+ ) -> None:
1819
+ name = self._cookie_name(base_name)
1820
+ response.set_cookie(
1821
+ name,
1822
+ value_b64,
1823
+ max_age=max_age,
1824
+ secure=self._is_https,
1825
+ httponly=True,
1826
+ samesite="lax",
1827
+ path="/",
1828
+ )
1829
+
1830
+ def _build_upstream_authorize_url(
1831
+ self, txn_id: str, transaction: dict[str, Any]
1832
+ ) -> str:
1833
+ """Construct the upstream IdP authorization URL using stored transaction data."""
1834
+ query_params: dict[str, Any] = {
1835
+ "response_type": "code",
1836
+ "client_id": self._upstream_client_id,
1837
+ "redirect_uri": f"{str(self.base_url).rstrip('/')}{self._redirect_path}",
1838
+ "state": txn_id,
1839
+ }
1840
+
1841
+ scopes_to_use = transaction.get("scopes") or self.required_scopes or []
1842
+ if scopes_to_use:
1843
+ query_params["scope"] = " ".join(scopes_to_use)
1844
+
1845
+ # If PKCE forwarding was enabled, include the proxy challenge
1846
+ proxy_code_verifier = transaction.get("proxy_code_verifier")
1847
+ if proxy_code_verifier:
1848
+ challenge_bytes = hashlib.sha256(proxy_code_verifier.encode()).digest()
1849
+ proxy_code_challenge = (
1850
+ urlsafe_b64encode(challenge_bytes).decode().rstrip("=")
1851
+ )
1852
+ query_params["code_challenge"] = proxy_code_challenge
1853
+ query_params["code_challenge_method"] = "S256"
1854
+
1855
+ # Forward resource indicator if present in transaction
1856
+ if resource := transaction.get("resource"):
1857
+ query_params["resource"] = resource
1858
+
1859
+ # Extra configured parameters
1860
+ if self._extra_authorize_params:
1861
+ query_params.update(self._extra_authorize_params)
1862
+
1863
+ separator = "&" if "?" in self._upstream_authorization_endpoint else "?"
1864
+ return f"{self._upstream_authorization_endpoint}{separator}{urlencode(query_params)}"
1865
+
1866
+ async def _show_consent_page(
1867
+ self, request: Request
1868
+ ) -> HTMLResponse | RedirectResponse:
1869
+ """Display consent page or auto-approve/deny based on cookies."""
1870
+ from fastmcp.server.server import FastMCP
1871
+
1872
+ txn_id = request.query_params.get("txn_id")
1873
+ if not txn_id:
1874
+ return create_secure_html_response(
1875
+ "<h1>Error</h1><p>Invalid or expired transaction</p>", status_code=400
1876
+ )
1877
+
1878
+ txn_model = await self._transaction_store.get(key=txn_id)
1879
+ if not txn_model:
1880
+ return create_secure_html_response(
1881
+ "<h1>Error</h1><p>Invalid or expired transaction</p>", status_code=400
1882
+ )
1883
+
1884
+ txn = txn_model.model_dump()
1885
+ client_key = self._make_client_key(txn["client_id"], txn["client_redirect_uri"])
1886
+
1887
+ approved = set(self._decode_list_cookie(request, "MCP_APPROVED_CLIENTS"))
1888
+ denied = set(self._decode_list_cookie(request, "MCP_DENIED_CLIENTS"))
1889
+
1890
+ if client_key in approved:
1891
+ upstream_url = self._build_upstream_authorize_url(txn_id, txn)
1892
+ return RedirectResponse(url=upstream_url, status_code=302)
1893
+
1894
+ if client_key in denied:
1895
+ callback_params = {
1896
+ "error": "access_denied",
1897
+ "state": txn.get("client_state") or "",
1898
+ }
1899
+ sep = "&" if "?" in txn["client_redirect_uri"] else "?"
1900
+ return RedirectResponse(
1901
+ url=f"{txn['client_redirect_uri']}{sep}{urlencode(callback_params)}",
1902
+ status_code=302,
1903
+ )
1904
+
1905
+ # Need consent: issue CSRF token and show HTML
1906
+ csrf_token = secrets.token_urlsafe(32)
1907
+ csrf_expires_at = time.time() + 15 * 60
1908
+
1909
+ # Update transaction with CSRF token
1910
+ txn_model.csrf_token = csrf_token
1911
+ txn_model.csrf_expires_at = csrf_expires_at
1912
+ await self._transaction_store.put(
1913
+ key=txn_id, value=txn_model, ttl=15 * 60
1914
+ ) # Auto-expire after 15 minutes
1915
+
1916
+ # Update dict for use in HTML generation
1917
+ txn["csrf_token"] = csrf_token
1918
+ txn["csrf_expires_at"] = csrf_expires_at
1919
+
1920
+ # Load client to get client_name if available
1921
+ client = await self.get_client(txn["client_id"])
1922
+ client_name = getattr(client, "client_name", None) if client else None
1923
+
1924
+ # Extract server metadata from app state
1925
+ fastmcp = getattr(request.app.state, "fastmcp_server", None)
1926
+
1927
+ if isinstance(fastmcp, FastMCP):
1928
+ server_name = fastmcp.name
1929
+ icons = fastmcp.icons
1930
+ server_icon_url = icons[0].src if icons else None
1931
+ server_website_url = fastmcp.website_url
1932
+ else:
1933
+ server_name = None
1934
+ server_icon_url = None
1935
+ server_website_url = None
1936
+
1937
+ html = create_consent_html(
1938
+ client_id=txn["client_id"],
1939
+ redirect_uri=txn["client_redirect_uri"],
1940
+ scopes=txn.get("scopes") or [],
1941
+ txn_id=txn_id,
1942
+ csrf_token=csrf_token,
1943
+ client_name=client_name,
1944
+ server_name=server_name,
1945
+ server_icon_url=server_icon_url,
1946
+ server_website_url=server_website_url,
1947
+ )
1948
+ response = create_secure_html_response(html)
1949
+ # Store CSRF in cookie with short lifetime
1950
+ self._set_list_cookie(
1951
+ response,
1952
+ "MCP_CONSENT_STATE",
1953
+ self._encode_list_cookie([csrf_token]),
1954
+ max_age=15 * 60,
1955
+ )
1956
+ return response
1957
+
1958
+ async def _submit_consent(
1959
+ self, request: Request
1960
+ ) -> RedirectResponse | HTMLResponse:
1961
+ """Handle consent approval/denial, set cookies, and redirect appropriately."""
1962
+ form = await request.form()
1963
+ txn_id = str(form.get("txn_id", ""))
1964
+ action = str(form.get("action", ""))
1965
+ csrf_token = str(form.get("csrf_token", ""))
1966
+
1967
+ if not txn_id:
1968
+ return create_secure_html_response(
1969
+ "<h1>Error</h1><p>Invalid or expired transaction</p>", status_code=400
1970
+ )
1971
+
1972
+ txn_model = await self._transaction_store.get(key=txn_id)
1973
+ if not txn_model:
1974
+ return create_secure_html_response(
1975
+ "<h1>Error</h1><p>Invalid or expired transaction</p>", status_code=400
1976
+ )
1977
+
1978
+ txn = txn_model.model_dump()
1979
+ expected_csrf = txn.get("csrf_token")
1980
+ expires_at = float(txn.get("csrf_expires_at") or 0)
1981
+
1982
+ if not expected_csrf or csrf_token != expected_csrf or time.time() > expires_at:
1983
+ return create_secure_html_response(
1984
+ "<h1>Error</h1><p>Invalid or expired consent token</p>", status_code=400
1985
+ )
1986
+
1987
+ client_key = self._make_client_key(txn["client_id"], txn["client_redirect_uri"])
1988
+
1989
+ if action == "approve":
1990
+ approved = set(self._decode_list_cookie(request, "MCP_APPROVED_CLIENTS"))
1991
+ if client_key not in approved:
1992
+ approved.add(client_key)
1993
+ approved_b64 = self._encode_list_cookie(sorted(approved))
1994
+
1995
+ upstream_url = self._build_upstream_authorize_url(txn_id, txn)
1996
+ response = RedirectResponse(url=upstream_url, status_code=302)
1997
+ self._set_list_cookie(
1998
+ response, "MCP_APPROVED_CLIENTS", approved_b64, max_age=365 * 24 * 3600
1999
+ )
2000
+ # Clear CSRF cookie by setting empty short-lived value
2001
+ self._set_list_cookie(
2002
+ response, "MCP_CONSENT_STATE", self._encode_list_cookie([]), max_age=60
2003
+ )
2004
+ return response
2005
+
2006
+ elif action == "deny":
2007
+ denied = set(self._decode_list_cookie(request, "MCP_DENIED_CLIENTS"))
2008
+ if client_key not in denied:
2009
+ denied.add(client_key)
2010
+ denied_b64 = self._encode_list_cookie(sorted(denied))
2011
+
2012
+ callback_params = {
2013
+ "error": "access_denied",
2014
+ "state": txn.get("client_state") or "",
2015
+ }
2016
+ sep = "&" if "?" in txn["client_redirect_uri"] else "?"
2017
+ client_callback_url = (
2018
+ f"{txn['client_redirect_uri']}{sep}{urlencode(callback_params)}"
2019
+ )
2020
+ response = RedirectResponse(url=client_callback_url, status_code=302)
2021
+ self._set_list_cookie(
2022
+ response, "MCP_DENIED_CLIENTS", denied_b64, max_age=365 * 24 * 3600
2023
+ )
2024
+ self._set_list_cookie(
2025
+ response, "MCP_CONSENT_STATE", self._encode_list_cookie([]), max_age=60
2026
+ )
2027
+ return response
2028
+
2029
+ else:
2030
+ return create_secure_html_response(
2031
+ "<h1>Error</h1><p>Invalid action</p>", status_code=400
2032
+ )