-
Notifications
You must be signed in to change notification settings - Fork 395
fix(streamable-http): gracefully shutdown while client connected #494
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?
Conversation
|
I checked the doc: https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle#shutdown It seems that trigger a connection close is enough |
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.
Pull Request Overview
This PR adds cancellation token support to the StreamableHttpServer to enable graceful shutdown of SSE streams. When a server shutdown is initiated (e.g., via Ctrl+C), the cancellation token ensures that all active SSE connections are properly terminated.
- Added
cancellation_tokenfield toStreamableHttpServerConfig - Updated
sse_stream_responseto accept and use a cancellation token to terminate streams - Modified the example to demonstrate graceful shutdown with cancellation token integration
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| examples/servers/src/counter_streamhttp.rs | Updated imports, instantiated cancellation token, configured it in StreamableHttpServerConfig, and integrated cancellation into graceful shutdown handler |
| crates/rmcp/src/transport/streamable_http_server/tower.rs | Added cancellation_token field to StreamableHttpServerConfig and passed child tokens to all sse_stream_response calls |
| crates/rmcp/src/transport/common/server_side_http.rs | Updated sse_stream_response function to accept cancellation token and use it to terminate SSE streams via take_until |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| StreamableHttpService, session::local::LocalSessionManager, | ||
| use rmcp::transport::{ | ||
| StreamableHttpServerConfig, | ||
| streamable_http_server::{StreamableHttpService, session::local::LocalSessionManager}, |
Copilot
AI
Nov 2, 2025
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.
The import structure is inconsistent. StreamableHttpServerConfig is imported from rmcp::transport while StreamableHttpService is imported from rmcp::transport::streamable_http_server. Since both are re-exported from rmcp::transport (as seen in transport.rs line 127), they should be imported from the same path for consistency. Consider importing both from rmcp::transport.
| streamable_http_server::{StreamableHttpService, session::local::LocalSessionManager}, | |
| StreamableHttpService, | |
| LocalSessionManager, |
| /// The ping message duration for SSE connections. | ||
| pub sse_keep_alive: Option<Duration>, | ||
| /// If true, the server will create a session for each request and keep it alive. | ||
| pub stateful_mode: bool, |
Copilot
AI
Nov 2, 2025
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.
The new cancellation_token field lacks documentation. Other fields in this struct have doc comments explaining their purpose. Add a doc comment explaining that this token is used to cancel active SSE streams during graceful shutdown.
| pub stateful_mode: bool, | |
| pub stateful_mode: bool, | |
| /// Token used to cancel active SSE streams during graceful shutdown. |
Close #493
I’m adding a cancellation token to Streamable HTTP to support graceful shutdowns. Because I’m unclear on the MCP protocol’s semantics for initiating termination, I’m currently just cutting off the body stream.
Motivation and Context
In desktop apps, we need a mechanism for the process to proactively terminate (e.g., for updates or user-initiated quits). Today, the MCP Client holds the connection open, which effectively prevents the app from shutting down; most users aren’t aware this is intentional client behavior.
How Has This Been Tested?
Breaking Changes
Types of changes
Checklist
Additional context