Skip to content

Fix MCP endpoints hanging on Starlette 1.x#89

Merged
Ciniesta merged 2 commits into
mainfrom
mcp-fix
Jul 24, 2026
Merged

Fix MCP endpoints hanging on Starlette 1.x#89
Ciniesta merged 2 commits into
mainfrom
mcp-fix

Conversation

@Ciniesta

@Ciniesta Ciniesta commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

(twin fix to agentic api)

Issue

In Starlette 1.x, by the time your FastAPI route handler runs, the framework has already internally consumed the ASGI receive stream (to build the Request object, parse params, etc.). The MCP SDK then does Request(scope, receive).body() internally, but receive is already drained. That second read hangs forever waiting for bytes that will never arrive.

Fix

Call await request.body() first (which returns Starlette's cached copy of the body), then build a fake receive that replays those bytes. The MCP transport reads from the fake receive successfully.

(twin fix to agentic api)

 MCP endpoints hang indefinitely, returning zero bytes. Server logs show ClientDisconnect when reading the request body.

Cause: Starlette 1.x changed how BaseHTTPMiddleware wraps the ASGI receive callable. The MCP transport passes request._receive to a new internal Request object, which gets a broken receive from the middleware wrapper. Other endpoints (/ask) are unaffected because they read the body through FastAPI's normal parsing. arag is unaffected because it's still on Starlette 0.50.

Fix: Pre-read the body via request.body() before passing to the MCP transport, bypassing the broken receive wrapper.
@Ciniesta
Ciniesta requested a review from a team July 23, 2026 20:25
carlesonielfa
carlesonielfa previously approved these changes Jul 24, 2026
@Ciniesta
Ciniesta added this pull request to the merge queue Jul 24, 2026
Merged via the queue into main with commit 910e743 Jul 24, 2026
6 checks passed
@Ciniesta
Ciniesta deleted the mcp-fix branch July 24, 2026 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants