Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-stdio-epipe-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/server': patch
---

Handle EPIPE errors in StdioServerTransport gracefully instead of crashing. When a client disconnects abruptly, the transport now catches stdout write errors, forwards them to `onerror`, and closes cleanly.
20 changes: 15 additions & 5 deletions packages/server/src/server/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class StdioServerTransport implements Transport {
_onerror = (error: Error) => {
this.onerror?.(error);
};
_onstdouterror = (error: Error) => {
this.onerror?.(error);
void this.close();
};

/**
* Starts listening for messages on `stdin`.
Expand All @@ -51,6 +55,7 @@ export class StdioServerTransport implements Transport {
this._started = true;
this._stdin.on('data', this._ondata);
this._stdin.on('error', this._onerror);
this._stdout.on('error', this._onstdouterror);
}

private processReadBuffer() {
Expand All @@ -72,6 +77,7 @@ export class StdioServerTransport implements Transport {
// Remove our event listeners first
this._stdin.off('data', this._ondata);
this._stdin.off('error', this._onerror);
this._stdout.off('error', this._onstdouterror);

// Check if we were the only data listener
const remainingDataListeners = this._stdin.listenerCount('data');
Expand All @@ -87,12 +93,16 @@ export class StdioServerTransport implements Transport {
}

send(message: JSONRPCMessage): Promise<void> {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const json = serializeMessage(message);
if (this._stdout.write(json)) {
resolve();
} else {
this._stdout.once('drain', resolve);
try {
if (this._stdout.write(json)) {
resolve();
} else {
this._stdout.once('drain', resolve);
}
} catch (error) {
reject(error);
}
});
}
Expand Down
61 changes: 61 additions & 0 deletions packages/server/test/server/stdio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,64 @@ test('should read multiple messages', async () => {
await finished;
expect(readMessages).toEqual(messages);
});

test('should handle stdout write errors gracefully', async () => {
const brokenOutput = new Writable({
write(_chunk, _encoding, callback) {
callback(new Error('write EPIPE'));
}
});

const server = new StdioServerTransport(input, brokenOutput);

const errors: Error[] = [];
server.onerror = error => {
errors.push(error);
};

let didClose = false;
server.onclose = () => {
didClose = true;
};

await server.start();

const message: JSONRPCMessage = {
jsonrpc: '2.0',
id: 1,
result: {}
};

// The send itself should resolve (write returns true before async error),
// but the error handler on the stream should fire and trigger close.
await server.send(message);

// Allow the async error callback to fire
await new Promise(resolve => setTimeout(resolve, 50));

expect(errors.length).toBeGreaterThan(0);
expect(errors[0]!.message).toContain('EPIPE');
expect(didClose).toBe(true);
});

test('should handle synchronous stdout write throws gracefully', async () => {
const throwingOutput = new Writable({
write() {
throw new Error('write EPIPE');
}
});

const server = new StdioServerTransport(input, throwingOutput);
server.onerror = () => {};

await server.start();

const message: JSONRPCMessage = {
jsonrpc: '2.0',
id: 1,
result: {}
};

// send() should reject instead of crashing the process
await expect(server.send(message)).rejects.toThrow('EPIPE');
});
Loading