Skip to content

Commit 73c4713

Browse files
chore: emit close on ConnectionManager (#595)
1 parent 45ea2aa commit 73c4713

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/common/connectionManager.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7172
export 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

106108
export 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" &&

src/common/session.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,7 @@ export class Session extends EventEmitter<SessionEvents> {
102102
async disconnect(): Promise<void> {
103103
const atlasCluster = this.connectedAtlasCluster;
104104

105-
try {
106-
await this.connectionManager.disconnect();
107-
} catch (err: unknown) {
108-
const error = err instanceof Error ? err : new Error(String(err));
109-
this.logger.error({
110-
id: LogId.mongodbDisconnectFailure,
111-
context: "session",
112-
message: `Error closing service provider: ${error.message}`,
113-
});
114-
}
105+
await this.connectionManager.close();
115106

116107
if (atlasCluster?.username && atlasCluster?.projectId) {
117108
void this.apiClient

0 commit comments

Comments
 (0)