diff --git a/src/client/sse.ts b/src/client/sse.ts index 2ebf23e8..e7f0dc6b 100644 --- a/src/client/sse.ts +++ b/src/client/sse.ts @@ -22,6 +22,12 @@ export class SSEClientTransport implements Transport { } start(): Promise { + if (this._eventSource) { + throw new Error( + "SSEClientTransport already started! If using Client class, note that connect() calls start() automatically.", + ); + } + return new Promise((resolve, reject) => { this._eventSource = new EventSource(this._url.href); this._abortController = new AbortController(); diff --git a/src/client/stdio.ts b/src/client/stdio.ts index 36d4f206..98f5b77f 100644 --- a/src/client/stdio.ts +++ b/src/client/stdio.ts @@ -45,6 +45,12 @@ export class StdioClientTransport implements Transport { * Starts the server process and prepares to communicate with it. */ async start(): Promise { + if (this._process) { + throw new Error( + "StdioClientTransport already started! If using Client class, note that connect() calls start() automatically.", + ); + } + return new Promise((resolve, reject) => { this._process = spawn( this._serverParams.command, diff --git a/src/client/websocket.ts b/src/client/websocket.ts index 97dc5529..3ca76082 100644 --- a/src/client/websocket.ts +++ b/src/client/websocket.ts @@ -19,6 +19,12 @@ export class WebSocketClientTransport implements Transport { } start(): Promise { + if (this._socket) { + throw new Error( + "WebSocketClientTransport already started! If using Client class, note that connect() calls start() automatically.", + ); + } + return new Promise((resolve, reject) => { this._socket = new WebSocket(this._url, SUBPROTOCOL); diff --git a/src/server/sse.ts b/src/server/sse.ts index 9afbea79..1342fcc3 100644 --- a/src/server/sse.ts +++ b/src/server/sse.ts @@ -37,7 +37,9 @@ export class SSEServerTransport implements Transport { */ async start(): Promise { if (this._sseResponse) { - throw new Error("Already connected!"); + throw new Error( + "SSEServerTransport already started! If using Server class, note that connect() calls start() automatically.", + ); } this.res.writeHead(200, { diff --git a/src/server/stdio.ts b/src/server/stdio.ts index d5f3eee4..f0eff82c 100644 --- a/src/server/stdio.ts +++ b/src/server/stdio.ts @@ -11,6 +11,7 @@ import { Transport } from "../shared/transport.js"; */ export class StdioServerTransport implements Transport { private _readBuffer: ReadBuffer = new ReadBuffer(); + private _started = false; constructor( private _stdin: Readable = process.stdin, @@ -34,6 +35,13 @@ export class StdioServerTransport implements Transport { * Starts listening for messages on stdin. */ async start(): Promise { + if (this._started) { + throw new Error( + "StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.", + ); + } + + this._started = true; this._stdin.on("data", this._ondata); this._stdin.on("error", this._onerror); }