fix: handle EPIPE errors in StdioServerTransport to prevent process crash#1568
Open
stakeswky wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix: handle EPIPE errors in StdioServerTransport to prevent process crash#1568stakeswky wants to merge 2 commits intomodelcontextprotocol:mainfrom
stakeswky wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
added 2 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
|
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
Fix uncaught EPIPE exceptions in
StdioServerTransportthat crash the Node.js process when clients disconnect unexpectedly.Problem
When a client disconnects while the server is writing to stdout, the
stdout.write()call throws an EPIPE error. Since there's no error handler, this becomes an uncaught exception that crashes the entire Node.js process.Root Cause
send()method callsthis._stdout.write()without any error handlingstart()method doesn't register an error listener on_stdoutFix
start(): Catches EPIPE and other stdout errors, triggers graceful closesend(): Wrap write operations with error handling, reject the promise on failureclose(): Remove stdout error listener to prevent leaksBehavior
onerrorcallback with the errorclose()to clean up resourcessend()promises instead of crashingThis allows servers to handle client disconnections gracefully without process crashes.
Fixes #1564