Skip to content

Conversation

@Ashutosh0x
Copy link

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 Ashutosh0x mentioned this pull request Feb 10, 2026
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.

AssertionError: Unexpected message while using middlewares

1 participant