Skip to content

Commit 837084b

Browse files
chore: PR feedback
1 parent 32e72f0 commit 837084b

File tree

4 files changed

+57
-81
lines changed

4 files changed

+57
-81
lines changed

src/mcp/mcpConnectionManager.ts

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ export interface MCPConnectParams {
1919
}
2020

2121
export class MCPConnectionManager extends ConnectionManager {
22-
private activeConnectionId: string | null = null;
23-
private activeConnectionProvider: ServiceProvider | null = null;
22+
private activeConnection: {
23+
id: string;
24+
provider: ServiceProvider;
25+
} | null = null;
2426

2527
constructor(private readonly logger: LoggerBase) {
2628
super();
@@ -46,17 +48,19 @@ export class MCPConnectionManager extends ConnectionManager {
4648
connectParams.connectOptions,
4749
);
4850
await serviceProvider.runCommand('admin', { hello: 1 });
49-
this.activeConnectionId = connectParams.connectionId;
50-
this.activeConnectionProvider = serviceProvider;
51+
this.activeConnection = {
52+
id: connectParams.connectionId,
53+
provider: serviceProvider,
54+
};
5155
return this.changeState('connection-success', {
5256
tag: 'connected',
5357
serviceProvider,
5458
});
5559
} catch (error) {
5660
this.logger.error({
5761
id: MCPLogIds.ConnectError,
58-
context: 'VSCodeMCPConnectionManager.connect',
59-
message: error instanceof Error ? error.message : String(error),
62+
context: 'vscode-mcp-connection-manager',
63+
message: `Error connecting to VSCode connection - ${error instanceof Error ? error.message : String(error)}`,
6064
});
6165
return this.changeState('connection-error', {
6266
tag: 'errored',
@@ -67,72 +71,48 @@ export class MCPConnectionManager extends ConnectionManager {
6771

6872
async disconnect(): Promise<ConnectionStateDisconnected> {
6973
try {
70-
await this.activeConnectionProvider?.close(true);
74+
await this.activeConnection?.provider?.close(true);
7175
} catch (error) {
7276
this.logger.error({
7377
id: MCPLogIds.DisconnectError,
74-
context: 'VSCodeMCPConnectionManager.disconnect',
75-
message: error instanceof Error ? error.message : String(error),
78+
context: 'vscode-mcp-connection-manager',
79+
message: `Error disconnecting from VSCode connection - ${error instanceof Error ? error.message : String(error)}`,
7680
});
7781
}
7882

79-
this.activeConnectionId = null;
80-
this.activeConnectionProvider = null;
83+
this.activeConnection = null;
8184
return this.changeState('connection-close', {
8285
tag: 'disconnected',
8386
});
8487
}
8588

86-
async updateConnection({
87-
connectionId,
88-
connectionString,
89-
connectOptions,
90-
}: {
91-
connectionId: string | undefined;
92-
connectionString: string | undefined;
93-
connectOptions: DevtoolsConnectOptions | undefined;
94-
}): Promise<void> {
95-
try {
96-
if (this.activeConnectionId && this.activeConnectionId !== connectionId) {
97-
await this.disconnect();
98-
}
99-
100-
const connectionWasDisconnected =
101-
!connectionId || !connectionString || !connectOptions;
89+
async updateConnection(
90+
connectParams: MCPConnectParams | undefined,
91+
): Promise<void> {
92+
if (connectParams?.connectionId === this.activeConnection?.id) {
93+
return;
94+
}
10295

103-
if (
104-
this.activeConnectionId === connectionId ||
105-
connectionWasDisconnected
106-
) {
107-
return;
108-
}
96+
await this.disconnect();
10997

110-
if (isAtlasStream(connectionString)) {
111-
this.logger.warning({
112-
id: MCPLogIds.UpdateConnectionError,
113-
context: 'VSCodeMCPConnectionManager.updateConnection',
114-
message: 'updateConnection called for an AtlasStreams connection',
115-
});
116-
this.changeState('connection-error', {
117-
tag: 'errored',
118-
errorReason:
119-
'MongoDB MCP server do not support connecting to Atlas Streams',
120-
});
121-
return;
122-
}
98+
if (!connectParams) {
99+
return;
100+
}
123101

124-
await this.connectToVSCodeConnection({
125-
connectionString,
126-
connectOptions,
127-
connectionId,
128-
});
129-
} catch (error) {
130-
this.logger.error({
102+
if (isAtlasStream(connectParams.connectionString)) {
103+
this.logger.warning({
131104
id: MCPLogIds.UpdateConnectionError,
132-
context: 'VSCodeMCPConnectionManager.updateConnection',
133-
message: error instanceof Error ? error.message : String(error),
105+
context: 'vscode-mcp-connection-manager',
106+
message: 'Attempting a connection to an AtlasStreams.',
134107
});
135-
throw error;
108+
this.changeState('connection-error', {
109+
tag: 'errored',
110+
errorReason:
111+
'MongoDB MCP server does not support connecting to Atlas Streams',
112+
});
113+
return;
136114
}
115+
116+
await this.connectToVSCodeConnection(connectParams);
137117
}
138118
}

src/mcp/mcpController.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '@himanshusinghs/mongodb-mcp-server';
1414
import type ConnectionController from '../connectionController';
1515
import { createLogger } from '../logging';
16+
import type { MCPConnectParams } from './mcpConnectionManager';
1617
import { MCPConnectionManager } from './mcpConnectionManager';
1718

1819
type mcpServerStartupConfig = 'ask' | 'enabled' | 'disabled';
@@ -238,10 +239,15 @@ ${jsonConfig}`,
238239
const connectionId = this.connectionController.getActiveConnectionId();
239240
const mongoClientOptions =
240241
this.connectionController.getMongoClientConnectionOptions();
241-
await this.mcpConnectionManager?.updateConnection({
242-
connectionId: connectionId ?? undefined,
243-
connectionString: mongoClientOptions?.url,
244-
connectOptions: mongoClientOptions?.options,
245-
});
242+
243+
const connectParams: MCPConnectParams | undefined =
244+
connectionId && mongoClientOptions
245+
? {
246+
connectionId: connectionId,
247+
connectionString: mongoClientOptions.url,
248+
connectOptions: mongoClientOptions.options,
249+
}
250+
: undefined;
251+
await this.mcpConnectionManager?.updateConnection(connectParams);
246252
}
247253
}

src/test/suite/mcp/mcpConnectionManager.test.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { MCPConnectionManager } from '../../../mcp/mcpConnectionManager';
1111
chai.use(chaiAsPromised);
1212

1313
const sandbox = sinon.createSandbox();
14-
suite('MCPConnectionManager Test Suite', function () {
14+
suite.only('MCPConnectionManager Test Suite', function () {
1515
let mcpConnectionManager: MCPConnectionManager;
1616
let fakeServiceProvider: NodeDriverServiceProvider;
1717

@@ -93,8 +93,7 @@ suite('MCPConnectionManager Test Suite', function () {
9393
expect(mcpConnectionManager.currentConnectionState.tag).to.equal(
9494
'disconnected',
9595
);
96-
expect((mcpConnectionManager as any).activeConnectionId).to.be.null;
97-
expect((mcpConnectionManager as any).activeConnectionProvider).to.be.null;
96+
expect((mcpConnectionManager as any).activeConnection).to.be.null;
9897
});
9998

10099
test('should attempt to disconnect and on failure clear out the state', async function () {
@@ -115,8 +114,7 @@ suite('MCPConnectionManager Test Suite', function () {
115114
expect(mcpConnectionManager.currentConnectionState.tag).to.equal(
116115
'disconnected',
117116
);
118-
expect((mcpConnectionManager as any).activeConnectionId).to.be.null;
119-
expect((mcpConnectionManager as any).activeConnectionProvider).to.be.null;
117+
expect((mcpConnectionManager as any).activeConnection).to.be.null;
120118
});
121119
});
122120

@@ -128,11 +126,7 @@ suite('MCPConnectionManager Test Suite', function () {
128126
'connectToVSCodeConnection',
129127
);
130128
const disconnectSpy = sandbox.spy(mcpConnectionManager, 'disconnect');
131-
await mcpConnectionManager.updateConnection({
132-
connectionId: undefined,
133-
connectionString: undefined,
134-
connectOptions: undefined,
135-
});
129+
await mcpConnectionManager.updateConnection(undefined);
136130

137131
expect(connectSpy).to.not.be.called;
138132
expect(disconnectSpy).to.not.be.called;
@@ -152,7 +146,7 @@ suite('MCPConnectionManager Test Suite', function () {
152146
});
153147

154148
expect(connectSpy).to.not.be.called;
155-
expect(disconnectSpy).to.not.be.called;
149+
expect(disconnectSpy).to.be.called;
156150
expect(mcpConnectionManager.currentConnectionState.tag).to.equal(
157151
'errored',
158152
);
@@ -161,7 +155,7 @@ suite('MCPConnectionManager Test Suite', function () {
161155
mcpConnectionManager.currentConnectionState as ConnectionStateErrored
162156
).errorReason,
163157
).to.equal(
164-
'MongoDB MCP server do not support connecting to Atlas Streams',
158+
'MongoDB MCP server does not support connecting to Atlas Streams',
165159
);
166160
});
167161

@@ -177,12 +171,12 @@ suite('MCPConnectionManager Test Suite', function () {
177171
connectOptions: {} as unknown as DevtoolsConnectOptions,
178172
});
179173

174+
expect(disconnectSpy).to.be.called;
180175
expect(connectSpy).to.be.calledWithExactly({
181176
connectionId: '1',
182177
connectionString: 'mongodb://localhost:27017',
183178
connectOptions: {} as unknown as DevtoolsConnectOptions,
184179
});
185-
expect(disconnectSpy).to.not.be.called;
186180
});
187181
});
188182

@@ -223,11 +217,7 @@ suite('MCPConnectionManager Test Suite', function () {
223217
'connectToVSCodeConnection',
224218
);
225219
const disconnectSpy = sandbox.spy(mcpConnectionManager, 'disconnect');
226-
await mcpConnectionManager.updateConnection({
227-
connectionId: undefined,
228-
connectionString: undefined,
229-
connectOptions: undefined,
230-
});
220+
await mcpConnectionManager.updateConnection(undefined);
231221

232222
expect(connectSpy).to.not.be.called;
233223
expect(disconnectSpy).to.be.called;
@@ -265,7 +255,7 @@ suite('MCPConnectionManager Test Suite', function () {
265255
mcpConnectionManager.currentConnectionState as ConnectionStateErrored
266256
).errorReason,
267257
).to.equal(
268-
'MongoDB MCP server do not support connecting to Atlas Streams',
258+
'MongoDB MCP server does not support connecting to Atlas Streams',
269259
);
270260
});
271261

src/test/suite/mcp/mcpController.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { TelemetryService } from '../../../telemetry';
1515
import { TEST_DATABASE_URI } from '../dbTestHelper';
1616

1717
const sandbox = sinon.createSandbox();
18-
suite('MCPController test suite', function () {
18+
suite.only('MCPController test suite', function () {
1919
let extensionContext: ExtensionContext;
2020
let connectionController: ConnectionController;
2121
let mcpController: MCPController;

0 commit comments

Comments
 (0)