fix(server): gracefully handle EPIPE in StdioServerTransport#1615
Open
stakeswky wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
fix(server): gracefully handle EPIPE in StdioServerTransport#1615stakeswky wants to merge 3 commits intomodelcontextprotocol:mainfrom
stakeswky wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
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
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1564 by handling broken pipe (
EPIPE) inStdioServerTransportso the process does not crash when the stdio client disconnects.Changes
errorlistener (_onstdouterror)EPIPE, close transport gracefully and emitoncloseonerrorsend()write pathtry/catchEPIPE, resolve send and close transport gracefully instead of rejecting/crashingclose()idempotent with_closedguard to avoid duplicate close events from concurrent EPIPE pathsTests
Added tests in
packages/server/test/server/stdio.test.ts:should handle EPIPE from stdout error event and close gracefullyshould resolve send and close on EPIPE write failureVerified with:
pnpm --filter @modelcontextprotocol/server test -- test/server/stdio.test.ts