Handle SSE Disconnects Properly#612
Merged
praboud-ant merged 6 commits intomodelcontextprotocol:mainfrom May 2, 2025
Merged
Conversation
praboud-ant
reviewed
May 2, 2025
src/mcp/server/fastmcp/server.py
Outdated
| host: str = "0.0.0.0" | ||
| port: int = 8000 | ||
| sse_path: str = "/sse" | ||
| sse_path: str = "/sse/" |
Contributor
Author
There was a problem hiding this comment.
Apparently Mount makes it trailing by default.
Starlette adds a trailing slash to the mount path by default and redirects requests without a trailing slash to the path with a trailing slash.
Our test client doesn't follow redirects so didn't like it.
Contributor
Author
There was a problem hiding this comment.
Discussed offline; we don't want to break current users so switched back to Route but return an empty Response to get around this.
praboud-ant
reviewed
May 2, 2025
praboud-ant
approved these changes
May 2, 2025
9 tasks
9 tasks
2 tasks
9 tasks
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.
Motivation and Context
The
EventSourceResponsefrom https://github.com/sysid/sse-starlette returns on SSE session disconnect. However, since we start this in a sub task group, the current implementation does nothing when a disconnect happens. This causes the sessionRouteto stay open forever in these cases.This PR modifies this behavior to close the streams in the SSE server when EventSourceResponse wraps up, and updates
ServerSessionto properly wrap up and clean up its streams as well when the upstream streams close.This exposes a different error in that we have this set up as a
Routein most cases which shouldn't returnNone. By fixing the 'running forever' issue we end up seeing exceptions related to this whenever a client disconnects. I updatedmount_sseto return an empty response to fix this.How Has This Been Tested?
Tested manually and ensured that the route was closed when clients close the connection.
Breaking Changes
It isn't breaking, but users need to change the
handle_ssefunction to aMountto not get"TypeError: 'NoneType' object is not callable"errors when client disconnects.Types of changes
Checklist
Additional context