-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: Handle SSE Disconnects Properly When use starlette middleware #1222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: Handle SSE Disconnects Properly When use starlette middleware #1222
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @wenxuwan !
Tested it w/ a variant of @llamantino's code snippet (just updated import to from mcp.server.fastmcp import FastMCP
) and it seems to fix the issue 🎉
Two questions:
- Could you try and add some test case?
- Would/should this also apply to sse streams from streamable http?
@ochafik Thank you for your comments! , For your questions:
|
I don't think this is the solution. Why are we sending 2 |
The operation for sending the response was introduced in #612 |
Ah... I see the issue. The problem is that we are mixing Starlette way with pure ASGI way. 😞 This PR is not the solution, it just hides the conceptual issue from above. I guess the solution is to decide what to do... We probably want to leverage Starlette instead of use the ASGI Interface directly... |
@Kludex Thank you for your suggestion. As you mentioned, the current legacy implementation of MCP's SSE mixes ASGI and Starlette, which poses challenges for the seamless integration of the handle_sse implementation with Starlette. I see one potential solution to address this issue.Maintain our existing implementation as is, but wrap the current handle_sse implementation in a Response, allowing handle_sse to directly return a McpResponse, For example:
In this way, our implementation will comply with the requirements of the Starlette framework, even though we internally use ASGI directly. At the same time, it does not require significant changes to the existing architecture.What do you think? |
Can't we just remove those Response for now? |
No,if we directly remove return Response, the Starlette framework will throw an exception.
Because the Starlette framework expects the handler to return a Response, as shown below:
Therefore, the handle_sse function needs to return a Response. |
Ah, I see the issue. We can't really mix things here. We need to remove the way we handle with pure ASGI. Which means refactoring quite a lot here, but it needs to be done. |
Motivation and Context
When using the Starlette middleware capabilities, if the SSE client disconnects, the current server will return starlette.responses.Response, and the Response will send a message of type "http.response.start", which will cause the following error:
There are several issues that should be related to this PR, such as:
#883
jlowin/fastmcp#858
How Has This Been Tested?
Tested manually and ensured that the route was closed when clients close the connection.
Breaking Changes
No
Types of changes
Checklist
Additional context