Skip to content

Clean logic #3

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

Merged
Changes from all commits
Commits
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
51 changes: 20 additions & 31 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,46 +821,29 @@ 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,
)]
)

# Always mount both /mcp and /mcp/ for full compatibility, regardless of default
_main_path = self.settings.streamable_http_path
if _main_path.endswith("/"):
_alt_path = _main_path.rstrip("/")
else:
_alt_path = _main_path + "/"
if _alt_path != _main_path:
if self._auth_server_provider:
routes.append(
Mount(
_alt_path,
app=RequireAuthMiddleware(
handle_streamable_http,
required_scopes,
),
)
)
else:
routes.append(
Mount(
_alt_path,
app=handle_streamable_http,
)
)

routes.extend(self._custom_starlette_routes)

return Starlette(
Expand Down