Skip to content

Commit 7ef5f8d

Browse files
authored
Clean logic (#3)
1 parent 3e9e33f commit 7ef5f8d

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,12 @@ async def handle_streamable_http(
790790
middleware: list[Middleware] = []
791791
required_scopes = []
792792

793+
# Always mount both /mcp and /mcp/ for full compatibility, regardless of default
794+
# Verify that _main_path has root format -> /mcp
795+
_main_path = self.settings.streamable_http_path.removesuffix("/")
796+
# Format _alt_path so it ends with '/' -> /mcp/
797+
_alt_path = _main_path + "/"
798+
793799
# Add auth endpoints if auth provider is configured
794800
if self._auth_server_provider:
795801
assert self.settings.auth
@@ -815,46 +821,29 @@ async def handle_streamable_http(
815821
revocation_options=self.settings.auth.revocation_options,
816822
)
817823
)
818-
routes.append(
824+
routes.extend([
819825
Mount(
820-
self.settings.streamable_http_path,
826+
_main_path,
821827
app=RequireAuthMiddleware(handle_streamable_http, required_scopes),
822-
)
828+
),
829+
Mount(
830+
_alt_path,
831+
app=RequireAuthMiddleware(handle_streamable_http, required_scopes),
832+
)]
823833
)
824834
else:
825835
# Auth is disabled, no wrapper needed
826-
routes.append(
836+
routes.extend([
827837
Mount(
828-
self.settings.streamable_http_path,
838+
_main_path,
829839
app=handle_streamable_http,
830-
)
840+
),
841+
Mount(
842+
_alt_path,
843+
app=handle_streamable_http,
844+
)]
831845
)
832846

833-
# Always mount both /mcp and /mcp/ for full compatibility, regardless of default
834-
_main_path = self.settings.streamable_http_path
835-
if _main_path.endswith("/"):
836-
_alt_path = _main_path.rstrip("/")
837-
else:
838-
_alt_path = _main_path + "/"
839-
if _alt_path != _main_path:
840-
if self._auth_server_provider:
841-
routes.append(
842-
Mount(
843-
_alt_path,
844-
app=RequireAuthMiddleware(
845-
handle_streamable_http,
846-
required_scopes,
847-
),
848-
)
849-
)
850-
else:
851-
routes.append(
852-
Mount(
853-
_alt_path,
854-
app=handle_streamable_http,
855-
)
856-
)
857-
858847
routes.extend(self._custom_starlette_routes)
859848

860849
return Starlette(

0 commit comments

Comments
 (0)