Skip to content

Fix 307 Temporary Redirect when use streamable_http #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0bf3521
Update streamable_http_path to /mcp/ to avoid 307 Temporary Redirect
chi2liu May 22, 2025
dd00227
Merge branch 'main' into fix-307-Temporary-Redirect
chi2liu May 23, 2025
d89e737
Streamable HTTP Trailing Slash Compatibility
chi2liu May 26, 2025
7d6e14f
Streamable HTTP Trailing Slash Compatibility
chi2liu May 26, 2025
a84399b
Streamable HTTP Trailing Slash Compatibility
chi2liu May 26, 2025
900c2dd
Merge branch 'main' into fix-307-Temporary-Redirect
chi2liu May 26, 2025
1a638ad
Streamable HTTP Trailing Slash Compatibility
chi2liu May 26, 2025
b3bf4c9
Streamable HTTP Trailing Slash Compatibility
chi2liu May 26, 2025
645e1ab
Streamable HTTP Trailing Slash Compatibility
chi2liu May 26, 2025
3e9e33f
Merge branch 'main' into fix-307-Temporary-Redirect
chi2liu May 27, 2025
7ef5f8d
Clean logic (#3)
vectorstain May 27, 2025
303cac8
Merge branch 'main' into fix-307-Temporary-Redirect
chi2liu May 27, 2025
0d05075
Merge branch 'main' into fix-307-Temporary-Redirect
chi2liu May 28, 2025
898fc88
add unit test for Streamable HTTP Trailing Slash Compatibility
May 28, 2025
45a5cd8
add unit test for Streamable HTTP Trailing Slash Compatibility
May 28, 2025
04f637c
Update test_streamable_http.py
chi2liu May 28, 2025
2377b70
add unit test for Streamable HTTP Trailing Slash Compatibility
May 28, 2025
09e403c
Merge branch 'fix-307-Temporary-Redirect' of https://github.com/chi2l…
May 28, 2025
a03c62e
add unit test for Streamable HTTP Trailing Slash Compatibility
May 28, 2025
10ccbbf
add unit test for Streamable HTTP Trailing Slash Compatibility
May 28, 2025
e102e7d
add unit test for Streamable HTTP Trailing Slash Compatibility
May 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,12 @@ async def handle_streamable_http(
middleware: list[Middleware] = []
required_scopes = []

# Always mount both /mcp and /mcp/ for full compatibility, regardless of default
# Verify that _main_path has root format -> /mcp
_main_path = self.settings.streamable_http_path.removesuffix("/")
# Format _alt_path so it ends with '/' -> /mcp/
_alt_path = _main_path + "/"

# Add auth endpoints if auth provider is configured
if self._auth_server_provider:
assert self.settings.auth
Expand All @@ -815,19 +821,27 @@ async def handle_streamable_http(
revocation_options=self.settings.auth.revocation_options,
)
)
routes.append(
routes.extend([
Mount(
self.settings.streamable_http_path,
_main_path,
app=RequireAuthMiddleware(handle_streamable_http, required_scopes),
)
),
Mount(
_alt_path,
app=RequireAuthMiddleware(handle_streamable_http, required_scopes),
)]
)
else:
# Auth is disabled, no wrapper needed
routes.append(
routes.extend([
Mount(
self.settings.streamable_http_path,
_main_path,
app=handle_streamable_http,
)
),
Mount(
_alt_path,
app=handle_streamable_http,
)]
)

routes.extend(self._custom_starlette_routes)
Expand Down
16 changes: 16 additions & 0 deletions tests/server/fastmcp/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ async def test_starlette_routes_with_mount_path(self):
mount_routes[0].path == "/messages"
), "Mount route path should be /messages"

mcp = FastMCP()
app = mcp.streamable_http_app()

# Find routes by type
streamable_routes = [r for r in app.routes if isinstance(r, Route)]
mount_routes = [r for r in app.routes if isinstance(r, Mount)]

# Verify routes exist
assert len(streamable_routes) == 2, "Should have two streamable routes"

# Verify path values
assert streamable_routes[0].path == "/mcp", "Streamable route path should be /mcp"
assert streamable_routes[1].path == "/mcp/", "Streamable route path should be /mcp"



@pytest.mark.anyio
async def test_non_ascii_description(self):
"""Test that FastMCP handles non-ASCII characters in descriptions correctly"""
Expand Down
Loading