modelcontextprotocol / modelcontextprotocol/typescript-sdk

Session Close Notification for Stateful Connections (MCP Clients)

Open
#2,175 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug enhancement needs decision P2
Dominant language
TypeScript
Stars
13.4k
Forks
2.2k
Avg merge
3d 15h
Merged PRs (30d)
4

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/client/streamableHttp.ts around line 623 and trace how sessionTermination and the exposed terminateSession method are handled during client disconnects. Clarify the automatic cleanup versus reconnection-persistence behavior, including the proposed initialization metadata, then verify that stale sessions can be cleaned up without preventing hosts from reusing a session ID.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.