Skip to content

Session Close Notification for Stateful Connections (MCP Clients) #2175

Description

@lokendra-ss-18283

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:

  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingenhancementRequest for a new feature that's not currently supportedneeds decisionIssue is actionable, needs maintainer decision on whether to implement

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions