Skip to content

Commit 83a43fd

Browse files
committed
Protocol.connect() can automatically call Transport.start()
1 parent 8d80cc5 commit 83a43fd

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

src/cli.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,13 @@ async function runClient(url_or_command: string, args: string[]) {
3232

3333
if (url?.protocol === "http:" || url?.protocol === "https:") {
3434
clientTransport = new SSEClientTransport(new URL(url_or_command));
35-
await clientTransport.start();
3635
} else if (url?.protocol === "ws:" || url?.protocol === "wss:") {
3736
clientTransport = new WebSocketClientTransport(new URL(url_or_command));
38-
await clientTransport.start();
3937
} else {
4038
clientTransport = new StdioClientTransport({
4139
command: url_or_command,
4240
args,
4341
});
44-
await clientTransport.start();
4542
}
4643

4744
console.log("Connected to server.");
@@ -75,7 +72,6 @@ async function runServer(port: number | null) {
7572
servers = servers.filter((s) => s !== server);
7673
};
7774

78-
await transport.start();
7975
await server.connect(transport);
8076
});
8177

@@ -104,7 +100,6 @@ async function runServer(port: number | null) {
104100
});
105101

106102
const transport = new StdioServerTransport();
107-
await transport.start();
108103
await server.connect(transport);
109104

110105
console.log("Server running on stdio");

src/shared/protocol.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Protocol<
8383
}
8484

8585
/**
86-
* Attaches to the given transport and starts listening for messages.
86+
* Attaches to the given transport, starts it, and starts listening for messages.
8787
*
8888
* 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.
8989
*/
@@ -106,6 +106,8 @@ export class Protocol<
106106
this._onnotification(message);
107107
}
108108
};
109+
110+
await this._transport.start();
109111
}
110112

111113
private _onclose(): void {

src/shared/transport.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface Transport {
88
* Starts processing messages on the transport, including any connection steps that might need to be taken.
99
*
1010
* This method should only be called after callbacks are installed, or else messages may be lost.
11+
*
12+
* NOTE: This method should not be called explicitly when using Client, Server, or Protocol classes, as they will implicitly call start().
1113
*/
1214
start(): Promise<void>;
1315

0 commit comments

Comments
 (0)