π The fast, Pythonic way to build MCP servers and clients.
fix(auth): preserve AuthKit issuer identity (#5452)
mika443a6c98fc19
diff --git a/fastmcp_slim/fastmcp/server/auth/providers/workos.py b/fastmcp_slim/fastmcp/server/auth/providers/workos.pyindex 18c0a763a..2e1e41433 100644--- a/fastmcp_slim/fastmcp/server/auth/providers/workos.py+++ b/fastmcp_slim/fastmcp/server/auth/providers/workos.py@@ -11,11 +11,11 @@ Choose based on your WorkOS setup and authentication requirements.from __future__ import annotationsimport contextlib-from typing import Literal+from typing import Annotated, Literalimport httpx2from key_value.aio.protocols import AsyncKeyValue-from pydantic import AnyHttpUrl+from pydantic import AnyHttpUrl, TypeAdapter, UrlConstraintsfrom starlette.responses import JSONResponsefrom starlette.routing import Route@@ -27,6 +27,10 @@ from fastmcp.utilities.logging import get_loggerlogger = get_logger(__name__)+_AUTHKIT_AUTHORIZATION_SERVER_URL_ADAPTER = TypeAdapter(+ Annotated[AnyHttpUrl, UrlConstraints(preserve_empty_path=True)]+)+class WorkOSTokenVerifier(TokenVerifier):"""Token verifier for WorkOS OAuth tokens.@@ -385,7 +389,11 @@ class AuthKitProvider(RemoteAuthProvider):# Initialize RemoteAuthProvider with AuthKit as the authorization serversuper().__init__(token_verifier=token_verifier,- authorization_servers=[AnyHttpUrl(self.authkit_domain)],+ authorization_servers=[+ _AUTHKIT_AUTHORIZATION_SERVER_URL_ADAPTER.validate_python(+ self.authkit_domain+ )+ ],base_url=self.base_url,resource_base_url=resource_base_url,scopes_supported=scopes_supported,diff --git a/tests/server/auth/providers/test_workos.py b/tests/server/auth/providers/test_workos.pyindex 66c19d769..56f5c740f 100644--- a/tests/server/auth/providers/test_workos.py+++ b/tests/server/auth/providers/test_workos.py@@ -5,6 +5,7 @@ from urllib.parse import urlparseimport pytestfrom key_value.aio.stores.memory import MemoryStorefrom mcp import MCPError+from starlette.testclient import TestClientfrom fastmcp import Client, FastMCPfrom fastmcp.client.transports import StreamableHttpTransport@@ -338,6 +339,26 @@ class TestAuthKitAudienceBinding:assert isinstance(verifier, JWTVerifier)assert verifier.audience == "http://127.0.0.1:8000/mcp"+ @pytest.mark.parametrize(+ "authkit_domain",+ ["https://test.authkit.app", "https://test.authkit.app/"],+ )+ def test_protected_resource_metadata_preserves_authkit_issuer(+ self, authkit_domain: str+ ):+ issuer = "https://test.authkit.app"+ auth = AuthKitProvider(+ authkit_domain=authkit_domain,+ base_url="http://127.0.0.1:8000",+ )+ app = FastMCP("test", auth=auth).http_app(path="/mcp")++ with TestClient(app) as client:+ response = client.get("/.well-known/oauth-protected-resource/mcp")++ assert response.status_code == 200+ assert response.json()["authorization_servers"] == [issuer]+class TestWorkOSTokenVerifierScopes:async def test_verify_token_rejects_missing_required_scopes(