disconnect

This commit is contained in:
Nate 2025-04-18 12:43:13 -07:00
parent bbc5002a57
commit 1ec10a3cbb
2 changed files with 14 additions and 4 deletions

View file

@ -207,10 +207,13 @@ describe("MCPConnection", () => {
},
});
const abortController = new AbortController();
await conn.connectClient(false, abortController.signal);
expect(conn.status).toBe("connected");
await conn.client.close();
try {
const abortController = new AbortController();
await conn.connectClient(false, abortController.signal);
expect(conn.status).toBe("connected");
} finally {
await conn.disconnect();
}
});
});
});

View file

@ -46,6 +46,12 @@ class MCPConnection {
this.abortController = new AbortController();
}
async disconnect() {
this.abortController.abort();
await this.client.close();
await this.transport.close();
}
private constructTransport(options: MCPOptions): Transport {
switch (options.transport.type) {
case "stdio":
@ -133,6 +139,7 @@ class MCPConnection {
this.transport = this.constructTransport(this.options);
try {
await this.client.connect(this.transport);
await this.client.close();
} catch (error) {
// Allow the case where for whatever reason is already connected
if (