Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

fix(auth): omit scopes from generic authorization code exchange (#5183)

Liang Hongwei9d43d6e10090
diff --git a/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py b/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
index fa6666de9..9be3c6342 100644
--- a/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
+++ b/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
@@ -1653,8 +1653,11 @@ class OAuthProxy(OAuthProvider, ConsentMixin):
def _prepare_scopes_for_token_exchange(self, scopes: list[str]) -> list[str]:
"""Prepare scopes for initial token exchange (auth code -> tokens).
- Override this method to provide scopes during the authorization
- code exchange. Some providers (like Azure) require scopes to be sent.
+ The default omits the `scope` parameter from the upstream code
+ exchange, because the scopes were already negotiated at the
+ authorization endpoint and some OIDC providers reject them on the
+ token request. Override this method to send scopes during the
+ exchange; providers that require them (like Azure) do so.
Args:
scopes: Scopes from the authorization request
@@ -1662,7 +1665,7 @@ class OAuthProxy(OAuthProvider, ConsentMixin):
Returns:
List of scopes to send, or empty list to omit scope parameter
"""
- return scopes
+ return []
def _translate_scopes_from_idp(self, scopes: list[str]) -> list[str]:
"""Translate IdP-returned scopes into the client-facing form.
diff --git a/tests/server/auth/oauth_proxy/test_tokens.py b/tests/server/auth/oauth_proxy/test_tokens.py
index 1b720eec2..24c09772e 100644
--- a/tests/server/auth/oauth_proxy/test_tokens.py
+++ b/tests/server/auth/oauth_proxy/test_tokens.py
@@ -248,7 +248,11 @@ class TestOAuthProxyTokenEndpointAuth:
response = await proxy._handle_idp_callback(mock_request)
assert response.status_code == 302
- mock_client.fetch_token.assert_awaited_once()
+ mock_client.fetch_token.assert_awaited_once_with(
+ url="https://oauth.example.com/token",
+ code="idp-code",
+ redirect_uri="https://proxy.example.com/auth/callback",
+ )
mock_client.aclose.assert_awaited_once()
async def test_callback_redirect_includes_proxy_issuer(self, jwt_verifier):