feat: Client transport support connection closed event#637
feat: Client transport support connection closed event#637taobaorun wants to merge 6 commits intomodelcontextprotocol:mainfrom
Conversation
|
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. |
…t_support_connection_closed_event
|
@Kehrlann Please help review this pr |
Kehrlann
left a comment
There was a problem hiding this comment.
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 thanRunnable?
| AtomicReference<Boolean> closedHandlerCalled = new AtomicReference<>(false); | ||
| transport.setConnectionClosedHandler(v -> closedHandlerCalled.set(true)); | ||
| // transport close simulate the behavior of disconnection | ||
| transport.closeGracefully().block(); |
There was a problem hiding this comment.
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.
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:
Breaking Changes
Types of changes
Checklist
Additional context