Port of #2239 (v1.x) to v2 (#2275)

This commit is contained in:
Konstantin Konstantinov 2026-06-10 20:14:36 +03:00 • committed by GitHub
commit 1b53a415ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,14 +17,25 @@ import { process } from '@modelcontextprotocol/server/_shims';
* ```
*/
export class StdioServerTransport implements Transport {
private _readBuffer: ReadBuffer = new ReadBuffer();
private _readBuffer: ReadBuffer;
private _started = false;
private _closed = false;
constructor(
private _stdin: Readable = process.stdin,
private _stdout: Writable = process.stdout
) {}
private _stdout: Writable = process.stdout,
options?: {
/**
* Maximum size of the read buffer in bytes. If a single message exceeds
* this size the transport will emit an error and close.
*
* Defaults to 10 MB.
*/
maxBufferSize?: number;
}
) {
this._readBuffer = new ReadBuffer({ maxBufferSize: options?.maxBufferSize });
}
onclose?: () => void;
onerror?: (error: Error) => void;
@ -32,8 +43,13 @@ export class StdioServerTransport implements Transport {
// Arrow functions to bind `this` properly, while maintaining function identity.
_ondata = (chunk: Buffer) => {
this._readBuffer.append(chunk);
this.processReadBuffer();
try {
this._readBuffer.append(chunk);
this.processReadBuffer();
} catch (error) {
this.onerror?.(error as Error);
this.close().catch(() => {});
}
};
_onerror = (error: Error) => {
this.onerror?.(error);