fix(web): break /mcp trailing-slash redirect loop that made the MCP endpoint unreachable - #353
Merged
Merged
Conversation
The remote MCP endpoint was unreachable (ConnectionRefused). The Starlette Mount at /mcp (streamable route at "/") 307-redirects the bare /mcp to /mcp/, and because the middleware rewrote to the raw upstream, that redirect leaked the internal Docker host (http://backend:8000/mcp/) to the client. Next's trailingSlash:false then 308-bounced /mcp/ back to /mcp, forming a loop. Proxy /mcp to the trailing-slash form the Mount expects so the backend never redirects. rewrite() keeps the client-facing URL as /mcp — no client-visible redirect, no loop, no leaked internal host. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #352.
Problem
https://wikis.onetest.ai/mcpwas unreachable from MCP clients —ConnectionRefused. It's a trailing-slash redirect loop, not network/auth:Mount("/mcp", …)(streamable route at/) 307-redirects bare/mcp→/mcp/. Becauseweb/src/middleware.tsrewrote to the raw upstream, the backend built that redirect from its own request URL and leaked the internal host:location: http://backend:8000/mcp/— unreachable by the client.trailingSlash: false) 308-redirects/mcp/→/mcp, so the other direction loops.Fix
Proxy the MCP path to the trailing-slash form the Mount expects, so the backend never issues the redirect.
NextResponse.rewritekeeps the client URL as/mcp(no client-visible redirect, no loop).Client config stays
https://wikis.onetest.ai/mcp(no trailing slash) — the bug was purely server-side.Verification
curl -i https://wikis.onetest.ai/mcp→307 location: http://backend:8000/mcp/;curl -i .../mcp/→308 location: /mcp./mcpshould return200with an SSE/JSON response and noLocationheader. (Requires a deploy to verify end-to-end.)🤖 Generated with Claude Code