Skip to content

Commit 91c578e

Browse files
committed
Refactor FastMCP server to introduce _build_provider_auth_routes helper
Signed-off-by: Jesse Sanford <108698+jessesanford@users.noreply.github.com>
1 parent 649c7a6 commit 91c578e

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -750,17 +750,7 @@ async def handle_sse(scope: Scope, receive: Receive, send: Send):
750750

751751
# Add auth endpoints if auth server provider is configured
752752
if self._auth_server_provider:
753-
from mcp.server.auth.routes import create_auth_routes
754-
755-
routes.extend(
756-
create_auth_routes(
757-
provider=self._auth_server_provider,
758-
issuer_url=self.settings.auth.issuer_url,
759-
service_documentation_url=self.settings.auth.service_documentation_url,
760-
client_registration_options=self.settings.auth.client_registration_options,
761-
revocation_options=self.settings.auth.revocation_options,
762-
)
763-
)
753+
routes.extend(_build_provider_auth_routes(self._auth_server_provider, self.settings.auth))
764754

765755
# When auth is configured, require authentication
766756
if self._token_verifier:
@@ -865,17 +855,7 @@ async def handle_streamable_http(scope: Scope, receive: Receive, send: Send) ->
865855

866856
# Add auth endpoints if auth server provider is configured
867857
if self._auth_server_provider:
868-
from mcp.server.auth.routes import create_auth_routes
869-
870-
routes.extend(
871-
create_auth_routes(
872-
provider=self._auth_server_provider,
873-
issuer_url=self.settings.auth.issuer_url,
874-
service_documentation_url=self.settings.auth.service_documentation_url,
875-
client_registration_options=self.settings.auth.client_registration_options,
876-
revocation_options=self.settings.auth.revocation_options,
877-
)
878-
)
858+
routes.extend(_build_provider_auth_routes(self._auth_server_provider, self.settings.auth))
879859

880860
# Set up routes with or without auth
881861
if self._token_verifier:
@@ -1145,3 +1125,39 @@ async def warning(self, message: str, **extra: Any) -> None:
11451125
async def error(self, message: str, **extra: Any) -> None:
11461126
"""Send an error log message."""
11471127
await self.log("error", message, **extra)
1128+
1129+
1130+
# ---------------------------------------------------------------------------
1131+
# Internal helpers
1132+
# ---------------------------------------------------------------------------
1133+
1134+
1135+
# pyright: reportUnknownArgumentType=false, reportUnknownParameterType=false
1136+
def _build_provider_auth_routes(provider: OAuthAuthorizationServerProvider[Any, Any, Any], auth_settings: AuthSettings):
1137+
"""Return the list of Starlette routes for the given provider.
1138+
1139+
This consolidates the custom-route fallback logic that previously appeared
1140+
twice in ``sse_app`` and ``streamable_http_app``.
1141+
"""
1142+
1143+
from mcp.server.auth.routes import create_auth_routes # local import to avoid cycles
1144+
1145+
# Allow provider to supply its own custom route list (e.g. proxy mode)
1146+
get_auth_routes = getattr(provider, "get_auth_routes", None)
1147+
if callable(get_auth_routes):
1148+
try:
1149+
custom = get_auth_routes()
1150+
if custom and hasattr(custom, "__iter__"):
1151+
return list(custom) # type: ignore[return-value]
1152+
except Exception:
1153+
# Fall back to default factory on any error
1154+
pass
1155+
1156+
# Default behaviour – use shared route factory
1157+
return create_auth_routes(
1158+
provider=provider,
1159+
issuer_url=auth_settings.issuer_url,
1160+
service_documentation_url=auth_settings.service_documentation_url,
1161+
client_registration_options=auth_settings.client_registration_options,
1162+
revocation_options=auth_settings.revocation_options,
1163+
)

0 commit comments

Comments
 (0)