modelcontextprotocol / modelcontextprotocol/typescript-sdk
Session Close Notification for Stateful Connections (MCP Clients)
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 13.4k
- Forks
- 2.2k
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 4
Description
Describe the bug
- Ref - https://github.com/modelcontextprotocol/typescript-sdk/blob/v1.x/src/client/streamableHttp.ts#L623
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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