Skip to content

feat: Client transport support connection closed event#637

Open
taobaorun wants to merge 6 commits intomodelcontextprotocol:mainfrom
taobaorun:feat/clienttransport_support_connection_closed_event
Open

feat: Client transport support connection closed event#637
taobaorun wants to merge 6 commits intomodelcontextprotocol:mainfrom
taobaorun:feat/clienttransport_support_connection_closed_event

Conversation

@taobaorun
Copy link
Copy Markdown

Motivation and Context

In numerous real-world MCP service invocation cases, we have observed that when Server-Sent Events (SSE) connections are disrupted due to server-side maintenance operations or network instability, clients often fail to promptly detect these disconnection events. This delay in detection can lead to client instances becoming unavailable. There is a critical need for clients to reliably perceive connection status changes, enabling them to perform timely reinitialization actions such as establishing new connections and implementing effective connection monitoring strategies.

How Has This Been Tested?

This functionality has been validated in actual MCP service deployment environments. Testing scenarios included:

  • Simulated server maintenance events that intentionally terminated SSE connections

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

@taobaorun
Copy link
Copy Markdown
Author

Hey @tzolov @chemicL ,could you please take a look at this PR when you have a moment?

This change addresses an issue where MCP clients can become unresponsive due to undetected SSE connection drops. It ensures clients can reliably detect disconnections to enable timely reconnection and maintain service availability.

@taobaorun taobaorun changed the title client transport support connection closed event feat: Client transport support connection closed event Oct 27, 2025
@Kehrlann Kehrlann self-assigned this Nov 18, 2025
@taobaorun
Copy link
Copy Markdown
Author

@Kehrlann Please help review this pr

@Kehrlann Kehrlann added area/client P2 Moderate issues affecting some users, edge cases, potentially valuable feature labels Apr 1, 2026
Copy link
Copy Markdown
Contributor

@Kehrlann Kehrlann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @taobaorun sorry for the long, long delay. I'm getting back at it. Notable: #610 also introduces a close handler (for a different purpose), it might make sense to look at these two in parallel.

In any case, this has drifted by quite a lot, so if you want to carry forward, you'll have to fix a few conflicts.

A few questions:

  • Why do you need to update the handler with a setter, rather than a simple constructor argument?
  • Why is the handler a Consumer<Void> rather than Runnable ?

@Kehrlann Kehrlann added the waiting for user Waiting for user feedback or more details label Apr 1, 2026
AtomicReference<Boolean> closedHandlerCalled = new AtomicReference<>(false);
transport.setConnectionClosedHandler(v -> closedHandlerCalled.set(true));
// transport close simulate the behavior of disconnection
transport.closeGracefully().block();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a client-side close. You should consider closing the server instead, to simulate the case you were mentioning in the PR description. Same goes for the other transport.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/client P2 Moderate issues affecting some users, edge cases, potentially valuable feature waiting for user Waiting for user feedback or more details

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants