π The fast, Pythonic way to build MCP servers and clients.
Pass mcp_path through the remote OAuth custom routes example (#5925)
Asem ahmedeef43ec3b5c3
diff --git a/docs/servers/auth/remote-oauth.mdx b/docs/servers/auth/remote-oauth.mdxindex e2fd14b98..9a7dfea52 100644--- a/docs/servers/auth/remote-oauth.mdx+++ b/docs/servers/auth/remote-oauth.mdx@@ -146,9 +146,14 @@ You can extend `RemoteAuthProvider` to add additional endpoints beyond the stand```pythonimport httpx2+from pydantic import AnyHttpUrlfrom starlette.responses import JSONResponsefrom starlette.routing import Route+from fastmcp import FastMCP+from fastmcp.server.auth import RemoteAuthProvider+from fastmcp.server.auth.providers.jwt import JWTVerifier+class CompanyAuthProvider(RemoteAuthProvider):def __init__(self):token_verifier = JWTVerifier(@@ -163,11 +168,11 @@ class CompanyAuthProvider(RemoteAuthProvider):base_url="https://api.yourcompany.com" # Your server base URL)- def get_routes(self) -> list[Route]:+ def get_routes(self, mcp_path: str | None = None) -> list[Route]:"""Add custom endpoints to the standard protected resource routes."""# Get the standard OAuth protected resource routes- routes = super().get_routes()+ routes = super().get_routes(mcp_path)# Add authorization server metadata forwarding for client convenienceasync def authorization_server_metadata(request):@@ -187,7 +192,7 @@ class CompanyAuthProvider(RemoteAuthProvider):mcp = FastMCP(name="Company API", auth=CompanyAuthProvider())```-This pattern uses `super().get_routes()` to get the standard protected resource routes, then adds additional endpoints as needed. A common use case is providing authorization server metadata forwarding, which allows MCP clients to discover your identity provider's capabilities through your MCP server rather than contacting the identity provider directly.+This pattern uses `super().get_routes(mcp_path)` to get the standard protected resource routes, then adds additional endpoints as needed. `http_app()` passes the MCP path into `get_routes`, and the base implementation uses that path for the protected-resource metadata URL. A common use case is providing authorization server metadata forwarding, which allows MCP clients to discover your identity provider's capabilities through your MCP server rather than contacting the identity provider directly.## WorkOS AuthKit Integrationdiff --git a/tests/server/auth/test_remote_auth_provider.py b/tests/server/auth/test_remote_auth_provider.pyindex c7ef28fab..b096a7109 100644--- a/tests/server/auth/test_remote_auth_provider.py+++ b/tests/server/auth/test_remote_auth_provider.py@@ -1,6 +1,8 @@import httpx2import pytestfrom pydantic import AnyHttpUrl+from starlette.responses import JSONResponse+from starlette.routing import Routefrom fastmcp import FastMCPfrom fastmcp.server.auth import RemoteAuthProvider@@ -608,3 +610,41 @@ class TestRemoteAuthProviderIntegration:assert response.status_code == 200data = response.json()assert data["scopes_supported"] == ["read"]++ async def test_overridden_get_routes_receives_mcp_path(self):+ """A subclass that forwards mcp_path keeps path-aware metadata."""++ class CompanyAuthProvider(RemoteAuthProvider):+ def get_routes(self, mcp_path: str | None = None) -> list[Route]:+ routes = super().get_routes(mcp_path)++ async def authorization_server_metadata(request):+ return JSONResponse({"issuer": "https://auth.example.com"})++ routes.append(+ Route(+ "/.well-known/oauth-authorization-server",+ authorization_server_metadata,+ )+ )+ return routes++ provider = CompanyAuthProvider(+ token_verifier=StaticTokenVerifier(+ tokens={"test": {"client_id": "c", "scopes": ["read"]}}+ ),+ authorization_servers=[AnyHttpUrl("https://auth.example.com")],+ base_url="https://api.example.com",+ )+ app = FastMCP("Company API", auth=provider).http_app()++ async with httpx2.AsyncClient(+ transport=httpx2.ASGITransport(app=app),+ base_url="https://api.example.com",+ ) as client:+ metadata = await client.get("/.well-known/oauth-protected-resource/mcp")+ custom = await client.get("/.well-known/oauth-authorization-server")++ assert metadata.status_code == 200+ assert custom.status_code == 200+ assert custom.json()["issuer"] == "https://auth.example.com"