FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: use ASGI callable for SSE endpoint to avoid BaseHTTPMiddleware AssertionError (#883) by Ashutosh0x · Pull Request #2029 · modelcontextprotocol/python-sdk · GitHub

fix: use ASGI callable for SSE endpoint to avoid BaseHTTPMiddleware AssertionError (#883) - #2029

Closed
Ashutosh0x wants to merge 5 commits into
modelcontextprotocol:mainfrom
Ashutosh0x:fix/883-sse-middleware-assertion
Closed

fix: use ASGI callable for SSE endpoint to avoid BaseHTTPMiddleware AssertionError (#883)#2029
Ashutosh0x wants to merge 5 commits into
modelcontextprotocol:mainfrom
Ashutosh0x:fix/883-sse-middleware-assertion

Conversation

Copy link
Copy Markdown

When Starlette's BaseHTTPMiddleware wraps the SSE endpoint, it expects http.response.body messages. However, the SSE handler sends raw ASGI events, causing 'AssertionError: Unexpected message'.

This PR fixes the issue by replacing the Starlette endpoint wrapper (sse_endpoint function) with a callable ASGI class (HandleSseAsgi). This ensures the SSE handler bypasses middleware response body wrapping, resolving the AssertionError.

Also removes the unnecessary 'return Response()' from handle_sse since it's a raw ASGI callable.

Closes #883

…ssertionError (modelcontextprotocol#883)

When Starlette's BaseHTTPMiddleware wraps the SSE endpoint, it expects
http.response.body messages. However, the SSE handler sends raw ASGI
events, causing 'AssertionError: Unexpected message'.

Fix: Replace the Starlette endpoint wrapper (sse_endpoint function)
with a callable ASGI class (HandleSseAsgi) so the SSE handler bypasses
middleware response body wrapping. Also remove the unnecessary
'return Response()' from handle_sse since it's a raw ASGI callable.

Closes modelcontextprotocol#883
Ashutosh0x mentioned this pull request Feb 10, 2026
maxisbey added the bug Something isn't working label Feb 11, 2026
maxisbey marked this pull request as draft February 11, 2026 14:20

Copy link
Copy Markdown
Contributor

Marking as draft until CI is fixed

Copy link
Copy Markdown
Contributor

Thanks for the PR. Tracking this in #1222 instead. Closing this as part of a wider backlog cleanup following the v2 launch. Feel free to reopen if this is still relevant.

AI Disclaimer

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AssertionError: Unexpected message while using middlewares

2 participants


Back | FazBrowse Home | New Git URL