From 83a43fda1e0a51dda0eb5ca933d3bd8bd70f0436 Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Wed, 16 Oct 2024 13:10:31 +0100 Subject: [PATCH] Protocol.connect() can automatically call Transport.start() --- src/cli.ts | 5 ----- src/shared/protocol.ts | 4 +++- src/shared/transport.ts | 2 ++ 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 9d3fb221..8031fcb7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -32,16 +32,13 @@ async function runClient(url_or_command: string, args: string[]) { if (url?.protocol === "http:" || url?.protocol === "https:") { clientTransport = new SSEClientTransport(new URL(url_or_command)); - await clientTransport.start(); } else if (url?.protocol === "ws:" || url?.protocol === "wss:") { clientTransport = new WebSocketClientTransport(new URL(url_or_command)); - await clientTransport.start(); } else { clientTransport = new StdioClientTransport({ command: url_or_command, args, }); - await clientTransport.start(); } console.log("Connected to server."); @@ -75,7 +72,6 @@ async function runServer(port: number | null) { servers = servers.filter((s) => s !== server); }; - await transport.start(); await server.connect(transport); }); @@ -104,7 +100,6 @@ async function runServer(port: number | null) { }); const transport = new StdioServerTransport(); - await transport.start(); await server.connect(transport); console.log("Server running on stdio"); diff --git a/src/shared/protocol.ts b/src/shared/protocol.ts index 96c1ae95..9f9a8a89 100644 --- a/src/shared/protocol.ts +++ b/src/shared/protocol.ts @@ -83,7 +83,7 @@ export class Protocol< } /** - * Attaches to the given transport and starts listening for messages. + * Attaches to the given transport, starts it, and starts listening for messages. * * The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward. */ @@ -106,6 +106,8 @@ export class Protocol< this._onnotification(message); } }; + + await this._transport.start(); } private _onclose(): void { diff --git a/src/shared/transport.ts b/src/shared/transport.ts index 8c98b95a..5843cf00 100644 --- a/src/shared/transport.ts +++ b/src/shared/transport.ts @@ -8,6 +8,8 @@ export interface Transport { * Starts processing messages on the transport, including any connection steps that might need to be taken. * * This method should only be called after callbacks are installed, or else messages may be lost. + * + * NOTE: This method should not be called explicitly when using Client, Server, or Protocol classes, as they will implicitly call start(). */ start(): Promise;