modelcontextprotocol / modelcontextprotocol/typescript-sdk

Memory leak in StreamableHTTP and SSE

Open
#771 6 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug fix proposed P2 ready for work v2
Dominant language
TypeScript
Stars
13.4k
Forks
2.2k
Avg merge
3d 15h
Merged PRs (30d)
4

Description

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

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 by running the linked mock-server and test-client reproduction with Node.js and --expose-gc, then inspect EventSourceParserStream from eventsource-parser/stream as used by the StreamableHTTP and SSE transports. Re-test 50MB and 100MB responses; done means memory stays proportional to the response and is released after processing without the timeout.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
api, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.