From 8f591ec82ee4fdde746360331f436bcbdbc9c314 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Fri, 26 Sep 2025 10:42:58 +0200 Subject: [PATCH] chore: emit close on ConnectionManager --- src/common/connectionManager.ts | 23 ++++++++++++++++++++--- src/common/session.ts | 11 +---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/common/connectionManager.ts b/src/common/connectionManager.ts index 9e0be5608..1094f8453 100644 --- a/src/common/connectionManager.ts +++ b/src/common/connectionManager.ts @@ -66,10 +66,11 @@ export interface ConnectionManagerEvents { "connection-time-out": [ConnectionStateErrored]; "connection-close": [ConnectionStateDisconnected]; "connection-error": [ConnectionStateErrored]; + close: [AnyConnectionState]; } export abstract class ConnectionManager { - protected clientName: string; + public clientName: string; protected readonly _events: EventEmitter; readonly events: Pick, "on" | "off" | "once">; private state: AnyConnectionState; @@ -101,6 +102,7 @@ export abstract class ConnectionManager { abstract connect(settings: ConnectionSettings): Promise; abstract disconnect(): Promise; + abstract close(): Promise; } export class MCPConnectionManager extends ConnectionManager { @@ -122,7 +124,7 @@ export class MCPConnectionManager extends ConnectionManager { this.deviceId = deviceId; } - async connect(settings: ConnectionSettings): Promise { + override async connect(settings: ConnectionSettings): Promise { this._events.emit("connection-request", this.currentConnectionState); if (this.currentConnectionState.tag === "connected" || this.currentConnectionState.tag === "connecting") { @@ -215,7 +217,7 @@ export class MCPConnectionManager extends ConnectionManager { } } - async disconnect(): Promise { + override async disconnect(): Promise { if (this.currentConnectionState.tag === "disconnected" || this.currentConnectionState.tag === "errored") { return this.currentConnectionState; } @@ -239,6 +241,21 @@ export class MCPConnectionManager extends ConnectionManager { return { tag: "disconnected" }; } + override async close(): Promise { + try { + await this.disconnect(); + } catch (err: unknown) { + const error = err instanceof Error ? err : new Error(String(err)); + this.logger.error({ + id: LogId.mongodbDisconnectFailure, + context: "ConnectionManager", + message: `Error when closing ConnectionManager: ${error.message}`, + }); + } finally { + this._events.emit("close", this.currentConnectionState); + } + } + private onOidcAuthFailed(error: unknown): void { if ( this.currentConnectionState.tag === "connecting" && diff --git a/src/common/session.ts b/src/common/session.ts index 24946b171..3c702a645 100644 --- a/src/common/session.ts +++ b/src/common/session.ts @@ -102,16 +102,7 @@ export class Session extends EventEmitter { async disconnect(): Promise { const atlasCluster = this.connectedAtlasCluster; - try { - await this.connectionManager.disconnect(); - } catch (err: unknown) { - const error = err instanceof Error ? err : new Error(String(err)); - this.logger.error({ - id: LogId.mongodbDisconnectFailure, - context: "session", - message: `Error closing service provider: ${error.message}`, - }); - } + await this.connectionManager.close(); if (atlasCluster?.username && atlasCluster?.projectId) { void this.apiClient