Protocol.connect() can automatically call Transport.start()

This commit is contained in:
Justin Spahr-Summers 2024-10-16 13:10:31 +01:00
commit 83a43fda1e
No known key found for this signature in database
3 changed files with 5 additions and 6 deletions

View file

@ -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");

View file

@ -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 {

View file

@ -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<void>;