Describe the bug
Current Issue:
When a client connects, a stateful session is established between the client and server to support features like elicitation, where the server needs to look up client metadata using session state for each incoming message. However, there is currently no mechanism to inform the server when a client disconnects. This results in stale sessions accumulating on the server side, causing unnecessary memory and resource bloat.
To Reproduce
Steps to reproduce the behavior:
- Connect to any MCP Clients, On client disconnect the session close is not being handled properly since sessionTermination is made optional.
Expected behavior
Session Cleanup:
The MCP Client SDK should automatically handle session cleanup when a client disconnects. If a host needs to persist a session for reconnection purposes, it should be able to store the sessionId and reinitialize the session with the same ID at a later point.
This would allow MCP Servers to handle client exits gracefully and keep stale sessions from accumulating over time.
Possible Solution:
Upon investigating the SDK, I found that a terminateSession method is exposed but never invoked automatically — leaving session cleanup entirely up to the caller. This creates ambiguity: it's unclear whether and when callers are expected to call it, and in practice it often goes unused.
Additional context
In addition, it would be useful to include a metadata key during client initialization that indicates whether automatic session cleanup is enabled. This allows the server to decide whether it should persist session state or rely on client-driven cleanup behavior.
Describe the bug
Current Issue:
When a client connects, a stateful session is established between the client and server to support features like elicitation, where the server needs to look up client metadata using session state for each incoming message. However, there is currently no mechanism to inform the server when a client disconnects. This results in stale sessions accumulating on the server side, causing unnecessary memory and resource bloat.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Session Cleanup:
The MCP Client SDK should automatically handle session cleanup when a client disconnects. If a host needs to persist a session for reconnection purposes, it should be able to store the sessionId and reinitialize the session with the same ID at a later point.
This would allow MCP Servers to handle client exits gracefully and keep stale sessions from accumulating over time.
Possible Solution:
Upon investigating the SDK, I found that a
terminateSessionmethod is exposed but never invoked automatically — leaving session cleanup entirely up to the caller. This creates ambiguity: it's unclear whether and when callers are expected to call it, and in practice it often goes unused.Additional context
In addition, it would be useful to include a metadata key during client initialization that indicates whether automatic session cleanup is enabled. This allows the server to decide whether it should persist session state or rely on client-driven cleanup behavior.