Skip to content

Memory leak in StreamableHTTP and SSE #771

@alexhermida

Description

@alexhermida

Describe the bug
Memory leak in StreamableHTTP transport when processing large SSE responses. Memory usage grows 10-40x the response size and is never released, causing timeouts at ~100MB responses.

We have an MCP that sometimes return above 50MB responses which when is close to 100MB usually triggers a problem in TypeScript clients, is also testable with the last version of the inspector.

  • Python clients don't have any issue with tool responses of this size.
  • The main issue is that the client side gets completely blocked.

Please, I'm not familiar with the TypeScript MCP library so any insights are welcome..

To Reproduce

With the help of Claude I created the following tests to avoid any external dependencies.

Steps to reproduce the behavior:

  1. Copy Gist with mock server, dependencies and test client.
  2. npm install
  3. node mcp-mock-server.js
  4. In another terminal: node --expose-gc test-memory-leak.js

You can also test it directly connecting with the Inspector to the server.

Expected behavior
Memory usage should be proportional to response size and released after processing.

Logs

--- Testing 50MB Response ---
[50MB_START] Heap: 120MB | Total: 154MB | External: 3MB | RSS: 299MB
⏱️  [6s] Memory: 529MB heap (4MB external)
⏱️  [9s] Memory: 886MB heap (4MB external)
     ⚠️  Memory usage growing rapidly!
⏱️  [27s] Memory: 1158MB heap (4MB external)
     ⚠️  Memory usage growing rapidly!
✅ 50MB response completed in 29898ms
[50MB_COMPLETE] Heap: 1577MB | Total: 1610MB | External: 3MB | RSS: 1578MB
📊 Response size: 118MB
📊 Memory amplification: 13.4x

--- Testing 100MB Response ---
❌ 100MB response failed after 60658ms: MCP error -32001: Request timed out
[100MB_FAILED] Heap: 1246MB | Total: 1280MB | External: 4MB | RSS: 1195MB

📊 MEMORY LEAK ANALYSIS SUMMARY:
   Initial Memory: 10MB
   Final Memory: 1246MB
   Total Leaked: 1236MB

Additional context

  • Issue is reproducible with controlled mock server (no external dependencies)
  • Memory leak occurs regardless of response content - purely size-dependent
  • Garbage collection does not recover the leaked memory
  • The leak appears to be in EventSourceParserStream from eventsource-parser/stream dependency
  • Both StreamableHTTP and SSE transports are affected (same underlying parser)

Environment

  • Node.js: v23.11.1
  • @modelcontextprotocol/sdk: 1.15.1
  • OS: macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions