π The fast, Pythonic way to build MCP servers and clients.
OAuthProxy forward PKCE parameters (#1714)
Aarona3e847d07126
diff --git a/src/fastmcp/server/auth/oauth_proxy.py b/src/fastmcp/server/auth/oauth_proxy.pyindex 5c37fc661..113f90630 100644--- a/src/fastmcp/server/auth/oauth_proxy.py+++ b/src/fastmcp/server/auth/oauth_proxy.py@@ -455,6 +455,13 @@ class OAuthProxy(OAuthProvider):if scopes_to_use:query_params["scope"] = " ".join(scopes_to_use)+ # Add PKCE parameters if present (forward to upstream)+ if params.code_challenge:+ query_params["code_challenge"] = params.code_challenge+ query_params["code_challenge_method"] = getattr(+ params, "code_challenge_method", "S256"+ )+# Build the upstream authorization URLseparator = "&" if "?" in self._upstream_authorization_endpoint else "?"upstream_url = f"{self._upstream_authorization_endpoint}{separator}{urlencode(query_params)}"diff --git a/tests/server/auth/test_oauth_proxy.py b/tests/server/auth/test_oauth_proxy.pyindex c56164440..c7d341fad 100644--- a/tests/server/auth/test_oauth_proxy.py+++ b/tests/server/auth/test_oauth_proxy.py@@ -319,6 +319,82 @@ class TestOAuthProxyComprehensive:assert transaction["code_challenge_method"] == "S256"assert transaction["scopes"] == ["read", "write"]+ async def test_authorize_forwards_pkce_parameters(self, oauth_proxy):+ """Test that PKCE parameters are forwarded to upstream authorization server."""+ client = OAuthClientInformationFull(+ client_id="test-client",+ client_secret="test-secret",+ redirect_uris=[AnyUrl("http://localhost:54321/callback")],+ )++ # Test with both code_challenge and code_challenge_method+ params = AuthorizationParams(+ redirect_uri=AnyUrl("http://localhost:54321/callback"),+ redirect_uri_provided_explicitly=True,+ state="client-state-123",+ code_challenge="dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",+ code_challenge_method="S256",+ scopes=["read", "write"],+ )++ # Call authorize+ redirect_url = await oauth_proxy.authorize(client, params)++ # Parse the redirect URL+ parsed = urlparse(redirect_url)+ query_params = parse_qs(parsed.query)++ # Verify PKCE parameters are forwarded to upstream+ assert "code_challenge" in query_params+ assert query_params["code_challenge"] == [+ "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"+ ]+ assert "code_challenge_method" in query_params+ assert query_params["code_challenge_method"] == ["S256"]++ # Verify transaction still stores PKCE (for later verification)+ txn_id = query_params["state"][0]+ transaction = oauth_proxy._oauth_transactions.get(txn_id)+ assert transaction is not None+ assert (+ transaction["code_challenge"]+ == "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"+ )+ assert transaction["code_challenge_method"] == "S256"++ async def test_authorize_forwards_pkce_without_method(self, oauth_proxy):+ """Test that PKCE challenge is forwarded even without explicit method (defaults to S256)."""+ client = OAuthClientInformationFull(+ client_id="test-client",+ client_secret="test-secret",+ redirect_uris=[AnyUrl("http://localhost:54321/callback")],+ )++ # Test with only code_challenge (no explicit method)+ params = AuthorizationParams(+ redirect_uri=AnyUrl("http://localhost:54321/callback"),+ redirect_uri_provided_explicitly=True,+ state="client-state-123",+ code_challenge="dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",+ # No code_challenge_method - should default to S256+ scopes=["read"],+ )++ # Call authorize+ redirect_url = await oauth_proxy.authorize(client, params)++ # Parse the redirect URL+ parsed = urlparse(redirect_url)+ query_params = parse_qs(parsed.query)++ # Verify PKCE parameters are forwarded with default method+ assert "code_challenge" in query_params+ assert query_params["code_challenge"] == [+ "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"+ ]+ assert "code_challenge_method" in query_params+ assert query_params["code_challenge_method"] == ["S256"] # Default method+async def test_authorize_without_scopes(self, oauth_proxy):"""Test authorize without scopes uses required scopes from verifier."""client = OAuthClientInformationFull(