Skip to content

Commit b726c86

Browse files
author
chiliu
committed
redirect_slashes to False
1 parent 2748a75 commit b726c86

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ async def run_sse_async(self, mount_path: str | None = None) -> None:
603603
import uvicorn
604604

605605
starlette_app = self.sse_app(mount_path)
606-
606+
607607
config = uvicorn.Config(
608608
starlette_app,
609609
host=self.settings.host,
@@ -618,7 +618,7 @@ async def run_streamable_http_async(self) -> None:
618618
import uvicorn
619619

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

784784
# Create the ASGI handler
785785
async def handle_streamable_http(request: Request) -> Response:
786-
await self.session_manager.handle_request(request.scope, request.receive, request._send)
786+
await self.session_manager.handle_request(
787+
request.scope, request.receive, request._send
788+
)
787789
return Response()
788790

789791
# Create routes
@@ -793,7 +795,7 @@ async def handle_streamable_http(request: Request) -> Response:
793795

794796
# Always register both /mcp and /mcp/ for full compatibility
795797
_main_path = self.settings.streamable_http_path.removesuffix("/")
796-
_alt_path = _main_path + "/"
798+
_alt_path = _main_path + "/"
797799

798800
# Add auth endpoints if auth provider is configured
799801
if self._auth_server_provider:
@@ -820,31 +822,39 @@ async def handle_streamable_http(request: Request) -> Response:
820822
revocation_options=self.settings.auth.revocation_options,
821823
)
822824
)
823-
routes.extend([
824-
Route(
825-
_main_path,
826-
endpoint=RequireAuthMiddleware(handle_streamable_http, required_scopes),
827-
methods=["GET", "POST", "OPTIONS"]
828-
),
829-
Route(
830-
_alt_path,
831-
endpoint=RequireAuthMiddleware(handle_streamable_http, required_scopes),
832-
methods=["GET", "POST", "OPTIONS"]
833-
)]
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+
]
834842
)
835843
else:
836844
# Auth is disabled, no wrapper needed
837-
routes.extend([
838-
Route(
839-
_main_path,
840-
endpoint=handle_streamable_http,
841-
methods=["GET", "POST", "OPTIONS"]
842-
),
843-
Route(
844-
_alt_path,
845-
endpoint=handle_streamable_http,
846-
methods=["GET", "POST", "OPTIONS"]
847-
)]
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+
]
848858
)
849859

850860
routes.extend(self._custom_starlette_routes)

tests/server/fastmcp/test_server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ async def test_starlette_routes_with_mount_path(self):
134134
assert len(streamable_routes) == 2, "Should have two streamable routes"
135135

136136
# Verify path values
137-
assert streamable_routes[0].path == "/mcp", "Streamable route path should be /mcp"
138-
assert streamable_routes[1].path == "/mcp/", "Streamable route path should be /mcp/"
137+
assert (
138+
streamable_routes[0].path == "/mcp"
139+
), "Streamable route path should be /mcp"
140+
assert (
141+
streamable_routes[1].path == "/mcp/"
142+
), "Streamable route path should be /mcp/"
139143

140144
@pytest.mark.anyio
141145
async def test_non_ascii_description(self):

0 commit comments

Comments
 (0)