Skip to content

fix(server): gracefully handle EPIPE in StdioServerTransport#1615

Open
stakeswky wants to merge 3 commits intomodelcontextprotocol:mainfrom
stakeswky:fix/stdio-epipe-crash-1564
Open

fix(server): gracefully handle EPIPE in StdioServerTransport#1615
stakeswky wants to merge 3 commits intomodelcontextprotocol:mainfrom
stakeswky:fix/stdio-epipe-crash-1564

Conversation

@stakeswky
Copy link

Summary

Fixes #1564 by handling broken pipe (EPIPE) in StdioServerTransport so the process does not crash when the stdio client disconnects.

Changes

  • Add EPIPE-aware handling to stdout error listener (_onstdouterror)
    • On EPIPE, close transport gracefully and emit onclose
    • Keep non-EPIPE behavior routed to onerror
  • Harden send() write path
    • Wrap write in try/catch
    • Handle async write callback/stream errors with one-shot cleanup
    • On EPIPE, resolve send and close transport gracefully instead of rejecting/crashing
  • Make close() idempotent with _closed guard to avoid duplicate close events from concurrent EPIPE paths

Tests

Added tests in packages/server/test/server/stdio.test.ts:

  • should handle EPIPE from stdout error event and close gracefully
  • should resolve send and close on EPIPE write failure

Verified with:

  • pnpm --filter @modelcontextprotocol/server test -- test/server/stdio.test.ts

User added 3 commits February 22, 2026 16:12
Per MCP spec, calling a nonexistent tool should return a JSON-RPC Error
(code -32602), not a JSON-RPC Result with isError: true.

Previously, only UrlElicitationRequired errors were re-thrown; all other
ProtocolErrors (including tool/disabled checks) were swallowed and wrapped
in a CallToolResult. Now all ProtocolErrors propagate as JSON-RPC errors,
which is the correct behavior per the specification.

Fixes modelcontextprotocol#1510
Add error handling for stdout write failures to prevent uncaught EPIPE
exceptions when clients disconnect unexpectedly.

Changes:
- Add stdout error listener in start() to catch EPIPE and other errors
- Trigger graceful close when stdout errors occur
- Handle write errors in send() by rejecting the promise
- Clean up stdout error listener in close()

This prevents the Node.js process from crashing when a client disconnects
while the server is writing to stdout.

Fixes modelcontextprotocol#1564
@stakeswky stakeswky requested a review from a team as a code owner March 2, 2026 20:44
@changeset-bot
Copy link

changeset-bot bot commented Mar 2, 2026

⚠️ No Changeset found

Latest commit: 819da20

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 2, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1615

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1615

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1615

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1615

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1615

commit: 99830c7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled 'EPIPE' in 'StdioServerTransport' causes fatal process crash on client disconnect

1 participant