Skip to content

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

Add `valid_scopes` parameter to OIDC proxy valid scopes (#4660)

Eduardo Cruz Guedes794bfe956779
diff --git a/docs/servers/auth/oauth-proxy.mdx b/docs/servers/auth/oauth-proxy.mdx
index 0ca8b524c..5cc0f633a 100644
--- a/docs/servers/auth/oauth-proxy.mdx
+++ b/docs/servers/auth/oauth-proxy.mdx
@@ -206,9 +206,11 @@ mcp = FastMCP(name="My Server", auth=auth)
</ParamField>
<ParamField body="valid_scopes" type="list[str] | None">
- List of all possible valid scopes for the OAuth provider. These are advertised
- to clients through the `/.well-known` endpoints. Defaults to `required_scopes`
- from your TokenVerifier if not specified.
+ The complete set of scopes clients are allowed to request β€” the full set of
+ available scopes (a superset of `required_scopes`). These are advertised to
+ clients through the `/.well-known` endpoints and enforced at Dynamic Client
+ Registration. Defaults to `required_scopes` from your TokenVerifier if not
+ specified.
</ParamField>
<ParamField body="extra_authorize_params" type="dict[str, str] | None">
diff --git a/docs/servers/auth/oidc-proxy.mdx b/docs/servers/auth/oidc-proxy.mdx
index 8580c28fd..763963858 100644
--- a/docs/servers/auth/oidc-proxy.mdx
+++ b/docs/servers/auth/oidc-proxy.mdx
@@ -132,6 +132,15 @@ These patterns apply to MCP client loopback redirects. Configure the upstream OA
</ParamField>
+<ParamField body="valid_scopes" type="list[str] | None">
+ The complete set of scopes clients are allowed to request β€” the full set of
+ available scopes (a superset of `required_scopes`). These are advertised to
+ clients through the `/.well-known` endpoints (as `scopes_supported`) and
+ enforced at Dynamic Client Registration: a client registering with a scope
+ outside this set is rejected. Defaults to `required_scopes` from your token
+ verifier if not specified.
+</ParamField>
+
<ParamField body="token_endpoint_auth_method" type="str | None">
Token endpoint authentication method for the upstream OAuth server. Controls how the proxy authenticates when exchanging authorization codes and refresh tokens with the upstream provider.
- `"client_secret_basic"`: Send credentials in Authorization header (most common)
diff --git a/fastmcp_slim/fastmcp/server/auth/oidc_proxy.py b/fastmcp_slim/fastmcp/server/auth/oidc_proxy.py
index 8f17c75b8..e2bda58ab 100644
--- a/fastmcp_slim/fastmcp/server/auth/oidc_proxy.py
+++ b/fastmcp_slim/fastmcp/server/auth/oidc_proxy.py
@@ -226,6 +226,7 @@ class OIDCProxy(OAuthProxy):
redirect_path: str | None = None,
# Client configuration
allowed_client_redirect_uris: list[str] | None = None,
+ valid_scopes: list[str] | None = None,
client_storage: AsyncKeyValue | None = None,
# JWT and encryption keys
jwt_signing_key: str | bytes | None = None,
@@ -284,6 +285,15 @@ class OIDCProxy(OAuthProxy):
ports allowed to vary for MCP compatibility. Unsafe browser schemes are rejected.
If empty list, no redirect URIs are allowed.
These are for MCP clients performing loopback redirects, NOT for the upstream OAuth app.
+ valid_scopes: The complete set of scopes clients are allowed to request,
+ advertised to clients via the `/.well-known` endpoints (as
+ `scopes_supported`) and enforced at Dynamic Client Registration: a
+ client that registers requesting a scope outside this set is rejected.
+ This is a superset of `required_scopes`, which is only the floor
+ enforced during token validation. Defaults to `required_scopes` when
+ not provided, so permitting optional scopes beyond the required floor
+ means setting this explicitly. Valid whether or not a custom
+ `token_verifier` is supplied.
client_storage: Storage backend for OAuth state (client registrations, encrypted tokens).
If None, an encrypted file store will be created in the data directory
(derived from `platformdirs`).
@@ -414,6 +424,7 @@ class OIDCProxy(OAuthProxy):
"issuer_url": issuer_url or base_url,
"service_documentation_url": self.oidc_config.service_documentation,
"allowed_client_redirect_uris": allowed_client_redirect_uris,
+ "valid_scopes": valid_scopes,
"client_storage": client_storage,
"jwt_signing_key": jwt_signing_key,
"token_endpoint_auth_method": token_endpoint_auth_method,
@@ -454,14 +465,16 @@ class OIDCProxy(OAuthProxy):
self._verify_id_token = verify_id_token
- # When verify_id_token strips scopes from the verifier, restore
- # them on the provider so they're still advertised to clients
- # and enforced at the FastMCP token level. We also need to
- # recompute derived state that OAuthProxy.__init__ already built
- # from the (empty) verifier scopes.
- if verify_id_token and required_scopes:
- self.required_scopes = required_scopes
- self.update_default_scopes(required_scopes)
+ # When verify_id_token strips scopes from the verifier, restore the
+ # derived scope state OAuthProxy.__init__ built from the (empty) verifier
+ # scopes. required_scopes is the enforcement floor; the advertised and
+ # registerable set is the broader valid_scopes when one was given.
+ if verify_id_token:
+ if required_scopes:
+ self.required_scopes = required_scopes
+ advertised_scopes = valid_scopes or required_scopes
+ if advertised_scopes:
+ self.update_default_scopes(advertised_scopes)
def _get_verification_token(
self, upstream_token_set: UpstreamTokenSet
diff --git a/tests/server/auth/test_oidc_proxy.py b/tests/server/auth/test_oidc_proxy.py
index 2636a1c83..755049120 100644
--- a/tests/server/auth/test_oidc_proxy.py
+++ b/tests/server/auth/test_oidc_proxy.py
@@ -982,3 +982,139 @@ class TestDiscoveryTimeout:
),
)
assert timeout == 10
+
+
+class TestOIDCProxyValidScopes:
+ """Tests for the valid_scopes parameter on OIDCProxy."""
+
+ def test_valid_scopes_widens_advertised_set(self, valid_oidc_configuration_dict):
+ """valid_scopes broadens the advertised/registerable set beyond required_scopes."""
+ with patch(
+ "fastmcp.server.auth.oidc_proxy.OIDCConfiguration.get_oidc_configuration"
+ ) as mock_get:
+ oidc_config = OIDCConfiguration.model_validate(
+ valid_oidc_configuration_dict
+ )
+ mock_get.return_value = oidc_config
+
+ proxy = OIDCProxy(
+ config_url=TEST_CONFIG_URL,
+ client_id=TEST_CLIENT_ID,
+ client_secret=TEST_CLIENT_SECRET,
+ base_url=TEST_BASE_URL,
+ required_scopes=["openid"],
+ valid_scopes=["openid", "email", "calendar"],
+ jwt_signing_key="test-secret",
+ )
+
+ assert proxy.client_registration_options is not None
+ assert proxy.client_registration_options.valid_scopes == [
+ "openid",
+ "email",
+ "calendar",
+ ]
+ assert proxy.client_registration_options.default_scopes == [
+ "openid",
+ "email",
+ "calendar",
+ ]
+ assert proxy._default_scope_str == "openid email calendar"
+
+ def test_valid_scopes_defaults_to_required_scopes(
+ self, valid_oidc_configuration_dict
+ ):
+ """Omitting valid_scopes falls back to required_scopes (existing behavior)."""
+ with patch(
+ "fastmcp.server.auth.oidc_proxy.OIDCConfiguration.get_oidc_configuration"
+ ) as mock_get:
+ oidc_config = OIDCConfiguration.model_validate(
+ valid_oidc_configuration_dict
+ )
+ mock_get.return_value = oidc_config
+
+ proxy = OIDCProxy(
+ config_url=TEST_CONFIG_URL,
+ client_id=TEST_CLIENT_ID,
+ client_secret=TEST_CLIENT_SECRET,
+ base_url=TEST_BASE_URL,
+ required_scopes=["read", "write"],
+ jwt_signing_key="test-secret",
+ )
+
+ assert proxy.client_registration_options is not None
+ assert proxy.client_registration_options.valid_scopes == ["read", "write"]
+
+ def test_valid_scopes_with_custom_token_verifier(
+ self, valid_oidc_configuration_dict
+ ):
+ """valid_scopes is allowed alongside a custom token_verifier (no error)."""
+ with patch(
+ "fastmcp.server.auth.oidc_proxy.OIDCConfiguration.get_oidc_configuration"
+ ) as mock_get:
+ oidc_config = OIDCConfiguration.model_validate(
+ valid_oidc_configuration_dict
+ )
+ mock_get.return_value = oidc_config
+
+ custom_verifier = IntrospectionTokenVerifier(
+ introspection_url="https://example.com/oauth/introspect",
+ client_id="introspection-client",
+ client_secret="introspection-secret",
+ required_scopes=["read"],
+ )
+
+ proxy = OIDCProxy(
+ config_url=TEST_CONFIG_URL,
+ client_id=TEST_CLIENT_ID,
+ client_secret=TEST_CLIENT_SECRET,
+ base_url=TEST_BASE_URL,
+ token_verifier=custom_verifier,
+ valid_scopes=["read", "write", "admin"],
+ jwt_signing_key="test-secret",
+ )
+
+ assert proxy.client_registration_options is not None
+ assert proxy.client_registration_options.valid_scopes == [
+ "read",
+ "write",
+ "admin",
+ ]
+
+ def test_valid_scopes_preserved_with_verify_id_token(
+ self, valid_oidc_configuration_dict
+ ):
+ """verify_id_token restores required_scopes without clobbering valid_scopes."""
+ with patch(
+ "fastmcp.server.auth.oidc_proxy.OIDCConfiguration.get_oidc_configuration"
+ ) as mock_get:
+ oidc_config = OIDCConfiguration.model_validate(
+ valid_oidc_configuration_dict
+ )
+ mock_get.return_value = oidc_config
+
+ proxy = OIDCProxy(
+ config_url=TEST_CONFIG_URL,
+ client_id=TEST_CLIENT_ID,
+ client_secret=TEST_CLIENT_SECRET,
+ base_url=TEST_BASE_URL,
+ required_scopes=["read"],
+ valid_scopes=["read", "write", "admin"],
+ verify_id_token=True,
+ jwt_signing_key="test-secret",
+ )
+
+ # Enforcement floor is restored to required_scopes...
+ assert proxy.required_scopes == ["read"]
+ # ...but the advertised/registerable set keeps the full valid_scopes.
+ assert proxy.client_registration_options is not None
+ assert proxy.client_registration_options.valid_scopes == [
+ "read",
+ "write",
+ "admin",
+ ]
+ assert proxy.client_registration_options.default_scopes == [
+ "read",
+ "write",
+ "admin",
+ ]
+ assert proxy._default_scope_str == "read write admin"