Skip to content

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

fix(auth): unprefix Azure scopes echoed back to MCP clients (#4130)

rgillinlz81a16ed27f77
diff --git a/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py b/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
index 9f549c185..2b1e44f20 100644
--- a/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
+++ b/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
@@ -1056,6 +1056,11 @@ class OAuthProxy(OAuthProvider, ConsentMixin):
if "scope" in idp_tokens
else list(authorization_code.scopes)
)
+ # Translate IdP-wire scopes into the client-facing form before they
+ # propagate to storage, the FastMCP JWT, and the response body. Default
+ # implementation is identity; AzureProvider overrides this to strip the
+ # identifier_uri prefix Azure echoes back on custom API scopes.
+ granted_scopes = self._translate_scopes_from_idp(granted_scopes)
# Clean up client code (one-time use)
await self._code_store.delete(key=authorization_code.code)
@@ -1236,6 +1241,35 @@ class OAuthProxy(OAuthProvider, ConsentMixin):
"""
return scopes
+ def _translate_scopes_from_idp(self, scopes: list[str]) -> list[str]:
+ """Translate IdP-returned scopes into the client-facing form.
+
+ Some IdPs (notably Azure) require scopes on the wire in a form that
+ differs from the form MCP clients use β€” e.g. Azure requires custom API
+ scopes to be prefixed with the application's identifier URI
+ (``api://{client_id}/read``) on outbound requests and echoes that same
+ prefixed form back in the token response. MCP clients, however, request
+ and recognize the short form (``read``) advertised via
+ ``/.well-known/oauth-authorization-server``.
+
+ This hook is the inverse of provider-specific outbound translation
+ (e.g. :meth:`AzureProvider._prefix_scopes_for_azure`). The default
+ implementation returns the scopes unchanged, which is correct for any
+ provider where IdP-wire scopes already match the client-facing form.
+
+ It is called once on each ``scope`` value parsed out of an IdP token
+ response, before that value is stored, embedded in the FastMCP JWT
+ scope claim, or echoed to the client. Applying the translation here
+ keeps storage, the issued JWT, and the response body consistent.
+
+ Args:
+ scopes: Scopes parsed from the IdP token response's ``scope`` field.
+
+ Returns:
+ List of scopes in the form clients expect.
+ """
+ return scopes
+
def _prepare_scopes_for_upstream_refresh(self, scopes: list[str]) -> list[str]:
"""Prepare scopes for upstream token refresh request.
@@ -1405,6 +1439,10 @@ class OAuthProxy(OAuthProvider, ConsentMixin):
if "scope" in token_response
else scopes
)
+ # Same translation as in exchange_authorization_code β€” keep storage,
+ # the rotated FastMCP JWT, and the response body all in client-facing
+ # scope form.
+ refreshed_scopes = self._translate_scopes_from_idp(refreshed_scopes)
upstream_token_set.scope = " ".join(refreshed_scopes)
# Handle upstream refresh token rotation and expiry
@@ -1619,11 +1657,14 @@ class OAuthProxy(OAuthProvider, ConsentMixin):
upstream_token_set.access_token = token_response["access_token"]
upstream_token_set.expires_at = time.time() + new_expires_in
- upstream_token_set.scope = " ".join(
+ refreshed_scopes = (
parse_scopes(token_response["scope"]) or []
if "scope" in token_response
else scopes
)
+ upstream_token_set.scope = " ".join(
+ self._translate_scopes_from_idp(refreshed_scopes)
+ )
# Handle upstream refresh token rotation
new_refresh_expires_in = None
diff --git a/fastmcp_slim/fastmcp/server/auth/providers/azure.py b/fastmcp_slim/fastmcp/server/auth/providers/azure.py
index fd0930db3..95eb4a288 100644
--- a/fastmcp_slim/fastmcp/server/auth/providers/azure.py
+++ b/fastmcp_slim/fastmcp/server/auth/providers/azure.py
@@ -430,6 +430,26 @@ class AzureProvider(OAuthProxy):
prefixed.append(f"{self.identifier_uri}/{scope}")
return prefixed
+ def _translate_scopes_from_idp(self, scopes: list[str]) -> list[str]:
+ """Strip ``{identifier_uri}/`` from custom API scopes Azure echoes back.
+
+ Inverse of :meth:`_prefix_scopes_for_azure`. Azure echoes the prefixed
+ form (``api://{client_id}/read``) in its token response's ``scope``
+ field, while MCP clients request and recognize the short form
+ (``read``) β€” the same form advertised on
+ ``/.well-known/oauth-authorization-server`` via ``valid_scopes``. Without
+ this translation, strict clients compare requested vs. granted scopes
+ and surface a "permissions not granted" warning (e.g. ChatGPT) even
+ when nothing is actually wrong.
+
+ OIDC scopes (``openid``, ``profile``, ``email``, ``offline_access``) and
+ external resource URIs (Microsoft Graph, etc.) never carry the prefix,
+ so :meth:`str.removeprefix` is a no-op on them and they pass through
+ unchanged.
+ """
+ prefix = f"{self.identifier_uri}/"
+ return [s.removeprefix(prefix) for s in scopes]
+
def _build_upstream_authorize_url(
self, txn_id: str, transaction: dict[str, Any]
) -> str:
diff --git a/tests/server/auth/providers/test_azure.py b/tests/server/auth/providers/test_azure.py
index 880e72558..959d9c369 100644
--- a/tests/server/auth/providers/test_azure.py
+++ b/tests/server/auth/providers/test_azure.py
@@ -1,13 +1,20 @@
"""Tests for Azure (Microsoft Entra) OAuth provider."""
+import time
+from unittest.mock import AsyncMock, patch
from urllib.parse import parse_qs, urlparse
import pytest
from key_value.aio.stores.memory import MemoryStore
-from mcp.server.auth.provider import AuthorizationParams
+from mcp.server.auth.provider import (
+ AuthorizationCode,
+ AuthorizationParams,
+ RefreshToken,
+)
from mcp.shared.auth import OAuthClientInformationFull
from pydantic import AnyUrl
+from fastmcp.server.auth.oauth_proxy.models import ClientCode, UpstreamTokenSet
from fastmcp.server.auth.providers.azure import AzureProvider
from fastmcp.server.auth.providers.jwt import JWTVerifier, RSAKeyPair
@@ -1183,3 +1190,284 @@ class TestAzureProviderFromB2CInputValidation:
jwt_signing_key="test-secret",
client_storage=memory_storage,
)
+
+
+class TestAzureScopeRoundTrip:
+ """Azure echoes ``api://{client_id}/<scope>`` back in its token response's
+ ``scope`` field, but MCP clients request and recognize the unprefixed form
+ advertised on ``/.well-known/oauth-authorization-server``. Without
+ translation, strict clients (e.g. ChatGPT) compare requested vs. granted
+ and surface a "permissions not granted" warning even though the token is
+ fully valid.
+ """
+
+ CLIENT_ID = "dummy"
+ REDIRECT_URI = "http://localhost:12345/callback"
+
+ async def _register_and_store_code(
+ self,
+ provider: AzureProvider,
+ *,
+ code: str,
+ idp_tokens: dict,
+ ) -> tuple[OAuthClientInformationFull, AuthorizationCode]:
+ # set_mcp_path() is what initializes the JWT issuer used during the
+ # token exchange. Normally get_routes() does this; for a unit test we
+ # call it directly.
+ provider.set_mcp_path("/mcp")
+
+ client = OAuthClientInformationFull(
+ client_id=self.CLIENT_ID,
+ client_secret="secret",
+ redirect_uris=[AnyUrl(self.REDIRECT_URI)],
+ )
+ await provider.register_client(client)
+
+ await provider._code_store.put(
+ key=code,
+ value=ClientCode(
+ code=code,
+ client_id=self.CLIENT_ID,
+ redirect_uri=self.REDIRECT_URI,
+ code_challenge="",
+ code_challenge_method="S256",
+ scopes=["read", "write"],
+ idp_tokens=idp_tokens,
+ expires_at=time.time() + 300,
+ created_at=time.time(),
+ ),
+ )
+
+ auth_code = AuthorizationCode(
+ code=code,
+ scopes=["read", "write"],
+ expires_at=time.time() + 300,
+ client_id=self.CLIENT_ID,
+ code_challenge="",
+ redirect_uri=AnyUrl(self.REDIRECT_URI),
+ redirect_uri_provided_explicitly=True,
+ )
+ return client, auth_code
+
+ async def test_exchange_authorization_code_unprefixes_echoed_scopes(
+ self, memory_storage: MemoryStore
+ ):
+ """OAuthToken.scope returned to the client must use the unprefixed form
+ advertised on /.well-known, not the prefixed form Azure echoes back.
+ """
+ provider = AzureProvider(
+ client_id="test_client",
+ client_secret="test_secret",
+ tenant_id="common",
+ identifier_uri="api://my-api",
+ required_scopes=["read", "write"],
+ base_url="https://srv.example",
+ jwt_signing_key="test-secret",
+ client_storage=memory_storage,
+ )
+
+ client, auth_code = await self._register_and_store_code(
+ provider,
+ code="test-auth-code",
+ idp_tokens={
+ "access_token": "upstream-access-token",
+ "refresh_token": "upstream-refresh-token",
+ "expires_in": 3600,
+ "token_type": "Bearer",
+ # This is what Azure actually returns β€” the prefixed form.
+ "scope": "api://my-api/read api://my-api/write",
+ },
+ )
+
+ result = await provider.exchange_authorization_code(client, auth_code)
+
+ assert result.scope is not None
+ granted = set(result.scope.split())
+ assert granted == {"read", "write"}, (
+ f"Expected unprefixed scopes echoed to client, got {result.scope!r}"
+ )
+
+ async def test_exchange_authorization_code_preserves_external_scopes(
+ self, memory_storage: MemoryStore
+ ):
+ """External resource URIs (e.g. Microsoft Graph) and OIDC scopes don't
+ carry the identifier_uri prefix, so the translation must leave them
+ alone."""
+ provider = AzureProvider(
+ client_id="test_client",
+ client_secret="test_secret",
+ tenant_id="common",
+ identifier_uri="api://my-api",
+ required_scopes=["read"],
+ base_url="https://srv.example",
+ jwt_signing_key="test-secret",
+ client_storage=memory_storage,
+ )
+
+ client, auth_code = await self._register_and_store_code(
+ provider,
+ code="test-auth-code-mixed",
+ idp_tokens={
+ "access_token": "upstream-access-token",
+ "refresh_token": "upstream-refresh-token",
+ "expires_in": 3600,
+ "token_type": "Bearer",
+ "scope": (
+ "api://my-api/read https://graph.microsoft.com/User.Read openid"
+ ),
+ },
+ )
+
+ result = await provider.exchange_authorization_code(client, auth_code)
+
+ assert result.scope is not None
+ granted = set(result.scope.split())
+ assert granted == {
+ "read",
+ "https://graph.microsoft.com/User.Read",
+ "openid",
+ }
+
+ async def test_exchange_refresh_token_unprefixes_echoed_scopes(
+ self, memory_storage: MemoryStore
+ ):
+ """The same translation must apply on refresh β€” otherwise the cosmetic
+ scope mismatch reappears after the first refresh."""
+ provider = AzureProvider(
+ client_id="test_client",
+ client_secret="test_secret",
+ tenant_id="common",
+ identifier_uri="api://my-api",
+ required_scopes=["read", "write"],
+ base_url="https://srv.example",
+ jwt_signing_key="test-secret",
+ client_storage=memory_storage,
+ )
+
+ with patch(
+ "fastmcp.server.auth.oauth_proxy.proxy.AsyncOAuth2Client"
+ ) as MockClient:
+ mock_client = AsyncMock()
+ mock_client.fetch_token = AsyncMock(
+ return_value={
+ "access_token": "upstream-access-token",
+ "refresh_token": "upstream-refresh-token",
+ "expires_in": 3600,
+ "token_type": "Bearer",
+ "scope": "api://my-api/read api://my-api/write",
+ }
+ )
+ MockClient.return_value = mock_client
+
+ client, auth_code = await self._register_and_store_code(
+ provider,
+ code="test-auth-code-refresh",
+ idp_tokens={
+ "access_token": "upstream-access-token",
+ "refresh_token": "upstream-refresh-token",
+ "expires_in": 3600,
+ "token_type": "Bearer",
+ "scope": "api://my-api/read api://my-api/write",
+ },
+ )
+ initial = await provider.exchange_authorization_code(client, auth_code)
+
+ assert initial.refresh_token is not None
+ fastmcp_refresh = RefreshToken(
+ token=initial.refresh_token,
+ client_id=self.CLIENT_ID,
+ scopes=["read", "write"],
+ expires_at=None,
+ )
+
+ # Simulate Azure echoing prefixed scopes on the refresh response too.
+ mock_client.refresh_token = AsyncMock(
+ return_value={
+ "access_token": "rotated-upstream-token",
+ "refresh_token": "rotated-upstream-refresh",
+ "expires_in": 3600,
+ "token_type": "Bearer",
+ "scope": "api://my-api/read api://my-api/write",
+ }
+ )
+
+ refreshed = await provider.exchange_refresh_token(
+ client, fastmcp_refresh, ["read", "write"]
+ )
+
+ assert refreshed.scope is not None
+ granted = set(refreshed.scope.split())
+ assert granted == {"read", "write"}, (
+ f"Expected unprefixed scopes after refresh, got {refreshed.scope!r}"
+ )
+
+ async def test_transparent_refresh_unprefixes_echoed_scopes(
+ self, memory_storage: MemoryStore
+ ):
+ """Transparent refresh stores client-facing scopes too.
+
+ Otherwise the scope mismatch can reappear when an expired upstream token
+ is refreshed during access-token validation.
+ """
+ provider = AzureProvider(
+ client_id="test_client",
+ client_secret="test_secret",
+ tenant_id="common",
+ identifier_uri="api://my-api",
+ required_scopes=["read", "write"],
+ base_url="https://srv.example",
+ jwt_signing_key="test-secret",
+ client_storage=memory_storage,
+ )
+
+ upstream_token_set = UpstreamTokenSet(
+ upstream_token_id="upstream-token-id",
+ access_token="expired-upstream-token",
+ refresh_token="refresh-token",
+ refresh_token_expires_at=time.time() + 3600,
+ expires_at=time.time() - 60,
+ token_type="Bearer",
+ scope="read write",
+ client_id=self.CLIENT_ID,
+ created_at=time.time() - 3600,
+ )
+
+ mock_oauth_client = AsyncMock()
+ mock_oauth_client.refresh_token = AsyncMock(
+ return_value={
+ "access_token": "rotated-upstream-token",
+ "refresh_token": "rotated-refresh-token",
+ "expires_in": 3600,
+ "token_type": "Bearer",
+ "scope": "api://my-api/read api://my-api/write",
+ }
+ )
+
+ with patch.object(
+ provider, "_create_upstream_oauth_client", return_value=mock_oauth_client
+ ):
+ refreshed = await provider._try_transparent_refresh(upstream_token_set)
+
+ assert refreshed.scope == "read write"
+
+ def test_translate_scopes_from_idp_is_inverse_of_prefix(
+ self, memory_storage: MemoryStore
+ ):
+ """The new hook should be the exact inverse of _prefix_scopes_for_azure
+ for any input the provider would have sent upstream."""
+ provider = AzureProvider(
+ client_id="test_client",
+ client_secret="test_secret",
+ tenant_id="common",
+ identifier_uri="api://my-api",
+ required_scopes=["read", "write"],
+ base_url="https://srv.example",
+ jwt_signing_key="test-secret",
+ client_storage=memory_storage,
+ )
+
+ original = ["read", "write", "openid", "https://graph.microsoft.com/User.Read"]
+ round_tripped = provider._translate_scopes_from_idp(
+ provider._prefix_scopes_for_azure(original)
+ )
+ assert round_tripped == original