@@ -66,10 +66,11 @@ export interface ConnectionManagerEvents {
66
66
"connection-time-out" : [ ConnectionStateErrored ] ;
67
67
"connection-close" : [ ConnectionStateDisconnected ] ;
68
68
"connection-error" : [ ConnectionStateErrored ] ;
69
+ close : [ AnyConnectionState ] ;
69
70
}
70
71
71
72
export abstract class ConnectionManager {
72
- protected clientName : string ;
73
+ public clientName : string ;
73
74
protected readonly _events : EventEmitter < ConnectionManagerEvents > ;
74
75
readonly events : Pick < EventEmitter < ConnectionManagerEvents > , "on" | "off" | "once" > ;
75
76
private state : AnyConnectionState ;
@@ -101,6 +102,7 @@ export abstract class ConnectionManager {
101
102
102
103
abstract connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > ;
103
104
abstract disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > ;
105
+ abstract close ( ) : Promise < void > ;
104
106
}
105
107
106
108
export class MCPConnectionManager extends ConnectionManager {
@@ -122,7 +124,7 @@ export class MCPConnectionManager extends ConnectionManager {
122
124
this . deviceId = deviceId ;
123
125
}
124
126
125
- async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
127
+ override async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
126
128
this . _events . emit ( "connection-request" , this . currentConnectionState ) ;
127
129
128
130
if ( this . currentConnectionState . tag === "connected" || this . currentConnectionState . tag === "connecting" ) {
@@ -215,7 +217,7 @@ export class MCPConnectionManager extends ConnectionManager {
215
217
}
216
218
}
217
219
218
- async disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > {
220
+ override async disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > {
219
221
if ( this . currentConnectionState . tag === "disconnected" || this . currentConnectionState . tag === "errored" ) {
220
222
return this . currentConnectionState ;
221
223
}
@@ -239,6 +241,21 @@ export class MCPConnectionManager extends ConnectionManager {
239
241
return { tag : "disconnected" } ;
240
242
}
241
243
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
+
242
259
private onOidcAuthFailed ( error : unknown ) : void {
243
260
if (
244
261
this . currentConnectionState . tag === "connecting" &&
0 commit comments