@@ -66,10 +66,11 @@ export interface ConnectionManagerEvents {
6666 "connection-time-out" : [ ConnectionStateErrored ] ;
6767 "connection-close" : [ ConnectionStateDisconnected ] ;
6868 "connection-error" : [ ConnectionStateErrored ] ;
69+ close : [ AnyConnectionState ] ;
6970}
7071
7172export abstract class ConnectionManager {
72- protected clientName : string ;
73+ public clientName : string ;
7374 protected readonly _events : EventEmitter < ConnectionManagerEvents > ;
7475 readonly events : Pick < EventEmitter < ConnectionManagerEvents > , "on" | "off" | "once" > ;
7576 private state : AnyConnectionState ;
@@ -101,6 +102,7 @@ export abstract class ConnectionManager {
101102
102103 abstract connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > ;
103104 abstract disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > ;
105+ abstract close ( ) : Promise < void > ;
104106}
105107
106108export class MCPConnectionManager extends ConnectionManager {
@@ -122,7 +124,7 @@ export class MCPConnectionManager extends ConnectionManager {
122124 this . deviceId = deviceId ;
123125 }
124126
125- async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
127+ override async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
126128 this . _events . emit ( "connection-request" , this . currentConnectionState ) ;
127129
128130 if ( this . currentConnectionState . tag === "connected" || this . currentConnectionState . tag === "connecting" ) {
@@ -215,7 +217,7 @@ export class MCPConnectionManager extends ConnectionManager {
215217 }
216218 }
217219
218- async disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > {
220+ override async disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > {
219221 if ( this . currentConnectionState . tag === "disconnected" || this . currentConnectionState . tag === "errored" ) {
220222 return this . currentConnectionState ;
221223 }
@@ -239,6 +241,21 @@ export class MCPConnectionManager extends ConnectionManager {
239241 return { tag : "disconnected" } ;
240242 }
241243
244+ override async close ( ) : Promise < void > {
245+ try {
246+ await this . disconnect ( ) ;
247+ } catch ( err : unknown ) {
248+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
249+ this . logger . error ( {
250+ id : LogId . mongodbDisconnectFailure ,
251+ context : "ConnectionManager" ,
252+ message : `Error when closing ConnectionManager: ${ error . message } ` ,
253+ } ) ;
254+ } finally {
255+ this . _events . emit ( "close" , this . currentConnectionState ) ;
256+ }
257+ }
258+
242259 private onOidcAuthFailed ( error : unknown ) : void {
243260 if (
244261 this . currentConnectionState . tag === "connecting" &&
0 commit comments