Skip to content

disconnect method for MCP client #794

@ho3einmolavi

Description

@ho3einmolavi

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions