modelcontextprotocol / modelcontextprotocol/typescript-sdk

disconnect method for MCP client

Open
#794 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation enhancement needs confirmation P3
Dominant language
TypeScript
Stars
13.4k
Forks
2.2k
Avg merge
3d 15h
Merged PRs (30d)
4

Description

Feature Request: Add disconnect() method to MCP SDK Client

Summary

Add a disconnect() method to the MCP SDK Client class to enable proper connection cleanup and resource management.

Problem

Currently, the MCP SDK Client only has a connect() method but lacks a corresponding disconnect() method. This creates several issues:

  1. Resource Leaks: Connections remain open until process termination
  2. Poor Resource Management: No way to explicitly close connections
  3. Connection Pooling Issues: Difficult to implement proper connection lifecycle management
  4. Memory Leaks: Long-running applications accumulate open connections

Current Workaround

Developers currently have to rely on process termination for connection cleanup:

// Current approach - no explicit disconnect
const client = new Client({ name: "my-client", version: "1.0.0" });
await client.connect(transport);
// Connection remains open until process ends

Proposed Solution

Add a disconnect() method to the Client class:

interface Client {
  // ... existing methods
  disconnect(): Promise<void>;
}

Implementation Details

  • Close the underlying transport connection
  • Clean up any pending requests
  • Release associated resources
  • Mark the client as disconnected
  • Allow reconnection after disconnect

Benefits

  1. Explicit Resource Management: Developers can control connection lifecycle
  2. Better Connection Pooling: Enable proper connection reuse and cleanup
  3. Memory Efficiency: Prevent connection accumulation in long-running apps
  4. Testing: Easier to test connection scenarios
  5. Graceful Shutdown: Proper cleanup during application shutdown

Use Cases

  • Connection pooling implementations
  • Long-running applications
  • Testing frameworks
  • Graceful application shutdown
  • Resource management in multi-tenant environments

Priority

Medium - This would improve the developer experience and enable better resource management patterns.

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 at the MCP SDK Client class and its existing connect() method, then trace how the underlying transport and pending requests are managed. Define done as an awaitable disconnect() that closes the transport, cleans up pending requests, marks the client disconnected, and permits reconnection; add coverage for these lifecycle cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.