Skip to content

Conversation

@greenhat616
Copy link

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@github-actions github-actions bot added T-core Core library changes T-examples Example code changes T-transport Transport layer changes labels Oct 21, 2025
@greenhat616
Copy link
Author

I checked the doc: https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle#shutdown

It seems that trigger a connection close is enough

@4t145 4t145 requested a review from Copilot November 2, 2025 18:40
Copy link
Contributor

Copilot AI left a 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_token field to StreamableHttpServerConfig
  • Updated sse_stream_response to 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},
Copy link

Copilot AI Nov 2, 2025

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.

Suggested change
streamable_http_server::{StreamableHttpService, session::local::LocalSessionManager},
StreamableHttpService,
LocalSessionManager,

Copilot uses AI. Check for mistakes.
/// 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,
Copy link

Copilot AI Nov 2, 2025

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.

Suggested change
pub stateful_mode: bool,
pub stateful_mode: bool,
/// Token used to cancel active SSE streams during graceful shutdown.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-core Core library changes T-examples Example code changes T-transport Transport layer changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gracefully shutdown is hang while a SSE connection is established

1 participant