Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/common/connectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectionManagerEvents>;
readonly events: Pick<EventEmitter<ConnectionManagerEvents>, "on" | "off" | "once">;
private state: AnyConnectionState;
Expand Down Expand Up @@ -101,6 +102,7 @@ export abstract class ConnectionManager {

abstract connect(settings: ConnectionSettings): Promise<AnyConnectionState>;
abstract disconnect(): Promise<ConnectionStateDisconnected | ConnectionStateErrored>;
abstract close(): Promise<void>;
}

export class MCPConnectionManager extends ConnectionManager {
Expand All @@ -122,7 +124,7 @@ export class MCPConnectionManager extends ConnectionManager {
this.deviceId = deviceId;
}

async connect(settings: ConnectionSettings): Promise<AnyConnectionState> {
override async connect(settings: ConnectionSettings): Promise<AnyConnectionState> {
this._events.emit("connection-request", this.currentConnectionState);

if (this.currentConnectionState.tag === "connected" || this.currentConnectionState.tag === "connecting") {
Expand Down Expand Up @@ -215,7 +217,7 @@ export class MCPConnectionManager extends ConnectionManager {
}
}

async disconnect(): Promise<ConnectionStateDisconnected | ConnectionStateErrored> {
override async disconnect(): Promise<ConnectionStateDisconnected | ConnectionStateErrored> {
if (this.currentConnectionState.tag === "disconnected" || this.currentConnectionState.tag === "errored") {
return this.currentConnectionState;
}
Expand All @@ -239,6 +241,21 @@ export class MCPConnectionManager extends ConnectionManager {
return { tag: "disconnected" };
}

override async close(): Promise<void> {
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" &&
Expand Down
11 changes: 1 addition & 10 deletions src/common/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,7 @@ export class Session extends EventEmitter<SessionEvents> {
async disconnect(): Promise<void> {
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
Expand Down
Loading