π The fast, Pythonic way to build MCP servers and clients.
Add default scope handling in OAuth proxy (#1667)
romanusyk210e9cc5ca40
diff --git a/docs/servers/auth/oauth-proxy.mdx b/docs/servers/auth/oauth-proxy.mdxindex 378a3ac26..5f3a1c1ad 100644--- a/docs/servers/auth/oauth-proxy.mdx+++ b/docs/servers/auth/oauth-proxy.mdx@@ -172,6 +172,33 @@ The `OAuthProxy` class provides the complete proxy implementation:</ParamField></Card>+### Dynamic client scope++When `OAuthProxy` creates a dynamic client (`ProxyDCRClient`) during registration or for temporary/unregistered access, it sets the client's `scope` string from your `TokenVerifier.required_scopes` (joined with spaces). If `required_scopes` is empty or `None`, the client's `scope` will be an empty string.++```python+from fastmcp.server.auth.providers.jwt import JWTVerifier+from fastmcp.server.auth.oauth_proxy import OAuthProxy++token_verifier = JWTVerifier(+ jwks_uri="https://provider/.well-known/jwks.json",+ issuer="https://provider",+ audience="my-app",+)+token_verifier.required_scopes = ["read", "write"]++auth = OAuthProxy(+ upstream_authorization_endpoint="https://provider/authorize",+ upstream_token_endpoint="https://provider/token",+ upstream_client_id="cid",+ upstream_client_secret="secret",+ token_verifier=token_verifier,+ base_url="https://your-server.com",+)++# Any dynamic client created by the proxy will have scope "read write"+```+```pythonfrom fastmcp import FastMCPfrom fastmcp.server.auth import OAuthProxydiff --git a/src/fastmcp/server/auth/oauth_proxy.py b/src/fastmcp/server/auth/oauth_proxy.pyindex 2954f8baf..8f7418a83 100644--- a/src/fastmcp/server/auth/oauth_proxy.py+++ b/src/fastmcp/server/auth/oauth_proxy.py@@ -317,6 +317,7 @@ class OAuthProxy(OAuthProvider):self._upstream_client_id = upstream_client_idself._upstream_client_secret = SecretStr(upstream_client_secret)self._upstream_revocation_endpoint = upstream_revocation_endpoint+ self._default_scope_str = " ".join(self.required_scopes or [])# Store redirect configurationself._redirect_path = (@@ -376,6 +377,7 @@ class OAuthProxy(OAuthProvider):AnyUrl("http://localhost")], # Placeholder, validation uses allowed_patternsgrant_types=["authorization_code", "refresh_token"],+ scope=self._default_scope_str,token_endpoint_auth_method="none",allowed_redirect_uri_patterns=self._allowed_client_redirect_uris,)@@ -415,6 +417,7 @@ class OAuthProxy(OAuthProvider):redirect_uris=client_info.redirect_uris or [AnyUrl("http://localhost")],grant_types=client_info.grant_typesor ["authorization_code", "refresh_token"],+ scope=self._default_scope_str,token_endpoint_auth_method="none",allowed_redirect_uri_patterns=self._allowed_client_redirect_uris,)diff --git a/tests/server/auth/test_oauth_proxy.py b/tests/server/auth/test_oauth_proxy.pyindex 56b3e9729..28019f39a 100644--- a/tests/server/auth/test_oauth_proxy.py+++ b/tests/server/auth/test_oauth_proxy.py@@ -220,6 +220,7 @@ class TestOAuthProxyComprehensive:assert stored_client is not Noneassert stored_client.client_id == "test-client-id"assert stored_client.client_secret == "test-client-secret"+ assert stored_client.scope == "read write"async def test_register_client_empty_grant_types(self, oauth_proxy):"""Test client registration with empty grant types."""@@ -266,6 +267,7 @@ class TestOAuthProxyComprehensive:assert len(temp_client.redirect_uris) >= 1# ProxyDCRClient uses a placeholder URL but accepts any localhost URIassert str(temp_client.redirect_uris[0]) == "http://localhost/"+ assert temp_client.scope == "read write"# Test that it accepts any localhost redirect URIfrom pydantic import AnyUrl@@ -379,6 +381,20 @@ class TestOAuthProxyComprehensive:# Proxy should NOT add any scopes - providers handle their own defaultsassert "scope" not in query_params+ async def test_client_scope_empty_when_no_required_scopes(self):+ """When required_scopes is None/empty, client scope should be empty string."""+ proxy = OAuthProxy(+ upstream_authorization_endpoint="https://auth.example.com/authorize",+ upstream_token_endpoint="https://auth.example.com/token",+ upstream_client_id="client-123",+ upstream_client_secret="secret-456",+ token_verifier=Mock(required_scopes=None),+ base_url="https://api.example.com",+ )++ temp_client = await proxy.get_client("any-client")+ assert temp_client.scope == ""+async def test_load_authorization_code_valid(self, oauth_proxy):"""Test loading a valid authorization code."""# Store a client code