Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Comments

Close side panel

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

Draft
Ashutosh0x wants to merge 5 commits intomodelcontextprotocol:mainmodelcontextprotocol/python-sdk:mainfrom
Ashutosh0x:fix/883-sse-middleware-assertionAshutosh0x/python-sdk:fix/883-sse-middleware-assertionCopy head branch name to clipboard
Draft

fix: use ASGI callable for SSE endpoint to avoid BaseHTTPMiddleware AssertionError (#883)#2029
Ashutosh0x wants to merge 5 commits intomodelcontextprotocol:mainmodelcontextprotocol/python-sdk:mainfrom
Ashutosh0x:fix/883-sse-middleware-assertionAshutosh0x/python-sdk:fix/883-sse-middleware-assertionCopy head branch name to clipboard

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
@maxisbey maxisbey added the bug Something isn't working label Feb 11, 2026
@maxisbey maxisbey marked this pull request as draft February 11, 2026 14:20
@maxisbey
Copy link
Contributor

Marking as draft until CI is fixed

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.