Conversation
(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.
carlesonielfa
previously approved these changes
Jul 24, 2026
carlesonielfa
approved these changes
Jul 24, 2026
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.
(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.