Skip to content

Commit f7ec158

Browse files
committed
chore: Fix typing issues and few PR suggestions
1 parent 28d8b69 commit f7ec158

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/common/connectionManager.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export interface ConnectionStateErrored extends ConnectionState {
5252
}
5353

5454
export type AnyConnectionState =
55-
| ConnectionState
5655
| ConnectionStateConnected
5756
| ConnectionStateConnecting
5857
| ConnectionStateDisconnected
@@ -77,7 +76,7 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
7776
async connect(settings: ConnectionSettings): Promise<AnyConnectionState> {
7877
this.emit("connection-requested", this.state);
7978

80-
if (this.state.tag == "connected" || this.state.tag == "connecting") {
79+
if (this.state.tag === "connected" || this.state.tag === "connecting") {
8180
await this.disconnect();
8281
}
8382

@@ -126,18 +125,13 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
126125
}
127126

128127
async disconnect(): Promise<ConnectionStateDisconnected | ConnectionStateErrored> {
129-
if (this.state.tag == "disconnected") {
130-
return this.state as ConnectionStateDisconnected;
131-
}
132-
133-
if (this.state.tag == "errored") {
134-
return this.state as ConnectionStateErrored;
128+
if (this.state.tag === "disconnected" || this.state.tag === "errored") {
129+
return this.state;
135130
}
136131

137132
if (this.state.tag == "connected" || this.state.tag == "connecting") {
138-
const state = this.state as ConnectionStateConnecting | ConnectionStateConnected;
139133
try {
140-
await state.serviceProvider?.close(true);
134+
await this.state.serviceProvider?.close(true);
141135
} finally {
142136
this.changeState("connection-closed", { tag: "disconnected" });
143137
}
@@ -150,9 +144,14 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
150144
return this.state;
151145
}
152146

153-
changeState<State extends AnyConnectionState>(event: keyof ConnectionManagerEvents, newState: State): State {
147+
changeState<Event extends keyof ConnectionManagerEvents, State extends ConnectionManagerEvents[Event][0]>(
148+
event: Event,
149+
newState: State
150+
): State {
154151
this.state = newState;
155-
this.emit(event, newState);
152+
// TypeScript doesn't seem to be happy with the spread operator and generics
153+
// eslint-disable-next-line
154+
this.emit(event, ...([newState] as any));
156155
return newState;
157156
}
158157

tests/integration/tools/mongodb/connect/connect.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import { beforeEach, describe, expect, it } from "vitest";
1212
describeWithMongoDB(
1313
"SwitchConnection tool",
1414
(integration) => {
15-
beforeEach(() => {
16-
integration.mcpServer().userConfig.connectionString = integration.connectionString();
17-
integration.mcpServer().session.connectionManager.changeState("connection-succeeded", {
18-
tag: "connected",
15+
beforeEach(async () => {
16+
await integration.mcpServer().session.connectToMongoDB({
17+
connectionString: integration.connectionString(),
18+
...config.connectOptions,
1919
});
2020
});
2121

tests/integration/transports/stdio.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, beforeEach, it, beforeAll, afterAll } from "vitest";
1+
import { describe, expect, it, beforeAll, afterAll } from "vitest";
22
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
33
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
44
import { describeWithMongoDB } from "../tools/mongodb/mongodbHelpers.js";

0 commit comments

Comments
 (0)