Skip to content

Commit 3d86e61

Browse files
author
chiliu
committed
fix
1 parent 86da08d commit 3d86e61

File tree

1 file changed

+17
-41
lines changed

1 file changed

+17
-41
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ async def run_streamable_http_async(self) -> None:
618618
import uvicorn
619619

620620
starlette_app = self.streamable_http_app()
621-
starlette_app.router.redirect_slashes = False
621+
622622
config = uvicorn.Config(
623623
starlette_app,
624624
host=self.settings.host,
@@ -782,21 +782,16 @@ def streamable_http_app(self) -> Starlette:
782782
)
783783

784784
# Create the ASGI handler
785-
async def handle_streamable_http(request: Request) -> Response:
786-
await self.session_manager.handle_request(
787-
request.scope, request.receive, request._send
788-
)
789-
return Response()
785+
async def handle_streamable_http(
786+
scope: Scope, receive: Receive, send: Send
787+
) -> None:
788+
await self.session_manager.handle_request(scope, receive, send)
790789

791790
# Create routes
792-
routes: list[Route] = []
791+
routes: list[Route | Mount] = []
793792
middleware: list[Middleware] = []
794793
required_scopes = []
795794

796-
# Always register both /mcp and /mcp/ for full compatibility
797-
_main_path = self.settings.streamable_http_path.removesuffix("/")
798-
_alt_path = _main_path + "/"
799-
800795
# Add auth endpoints if auth provider is configured
801796
if self._auth_server_provider:
802797
assert self.settings.auth
@@ -822,39 +817,19 @@ async def handle_streamable_http(request: Request) -> Response:
822817
revocation_options=self.settings.auth.revocation_options,
823818
)
824819
)
825-
routes.extend(
826-
[
827-
Route(
828-
_main_path,
829-
endpoint=RequireAuthMiddleware(
830-
handle_streamable_http, required_scopes
831-
),
832-
methods=["GET", "POST", "OPTIONS"],
833-
),
834-
Route(
835-
_alt_path,
836-
endpoint=RequireAuthMiddleware(
837-
handle_streamable_http, required_scopes
838-
),
839-
methods=["GET", "POST", "OPTIONS"],
840-
),
841-
]
820+
routes.append(
821+
Mount(
822+
self.settings.streamable_http_path,
823+
app=RequireAuthMiddleware(handle_streamable_http, required_scopes),
824+
)
842825
)
843826
else:
844827
# Auth is disabled, no wrapper needed
845-
routes.extend(
846-
[
847-
Route(
848-
_main_path,
849-
endpoint=handle_streamable_http,
850-
methods=["GET", "POST", "OPTIONS"],
851-
),
852-
Route(
853-
_alt_path,
854-
endpoint=handle_streamable_http,
855-
methods=["GET", "POST", "OPTIONS"],
856-
),
857-
]
828+
routes.append(
829+
Mount(
830+
self.settings.streamable_http_path,
831+
app=handle_streamable_http,
832+
)
858833
)
859834

860835
routes.extend(self._custom_starlette_routes)
@@ -864,6 +839,7 @@ async def handle_streamable_http(request: Request) -> Response:
864839
routes=routes,
865840
middleware=middleware,
866841
lifespan=lambda app: self.session_manager.run(),
842+
redirect_slashes=False,
867843
)
868844

869845
async def list_prompts(self) -> list[MCPPrompt]:

0 commit comments

Comments
 (0)