Skip to content

Commit fe988f3

Browse files
authored
feat: stub some hooks for the MCP server (#1107) (#1110)
1 parent e8b3647 commit fe988f3

14 files changed

+739
-217
lines changed

package-lock.json

Lines changed: 475 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@
529529
{
530530
"command": "mdb.stopMCPServer",
531531
"title": "MongoDB: Stop MCP Server"
532+
},
533+
{
534+
"command": "mdb.getMCPServerConfig",
535+
"title": "MongoDB: Get MCP Server Config"
532536
}
533537
],
534538
"menus": {
@@ -1414,6 +1418,7 @@
14141418
"mongodb-connection-string-url": "^3.0.2",
14151419
"mongodb-data-service": "^22.30.1",
14161420
"mongodb-log-writer": "^2.4.1",
1421+
"mongodb-mcp-server": "^0.3.0",
14171422
"mongodb-query-parser": "^4.4.2",
14181423
"mongodb-schema": "^12.6.2",
14191424
"node-machine-id": "1.1.12",
@@ -1445,7 +1450,7 @@
14451450
"@types/lodash": "^4.17.14",
14461451
"@types/micromatch": "^4.0.9",
14471452
"@types/mocha": "^8.2.3",
1448-
"@types/node": "^14.18.63",
1453+
"@types/node": "^20.19.0",
14491454
"@types/prettier": "^2.7.3",
14501455
"@types/react": "^17.0.83",
14511456
"@types/react-dom": "^17.0.25",

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ enum EXTENSION_COMMANDS {
8888
// MCP Server commands.
8989
START_MCP_SERVER = 'mdb.startMCPServer',
9090
STOP_MCP_SERVER = 'mdb.stopMCPServer',
91+
GET_MCP_SERVER_CONFIG = 'mdb.getMCPServerConfig',
9192
}
9293

9394
export type ExtensionCommand = EXTENSION_COMMANDS;

src/connectionController.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ const log = createLogger('connection controller');
3939

4040
const MAX_CONNECTION_NAME_LENGTH = 512;
4141

42-
export enum DataServiceEventTypes {
43-
CONNECTIONS_DID_CHANGE = 'CONNECTIONS_DID_CHANGE',
44-
ACTIVE_CONNECTION_CHANGED = 'ACTIVE_CONNECTION_CHANGED',
42+
interface DataServiceEventTypes {
43+
CONNECTIONS_DID_CHANGE: any;
44+
ACTIVE_CONNECTION_CHANGED: any;
4545
}
4646

4747
export enum ConnectionTypes {
@@ -156,7 +156,8 @@ export default class ConnectionController {
156156
private _statusView: StatusView;
157157

158158
// Used by other parts of the extension that respond to changes in the connections.
159-
private eventEmitter: EventEmitter = new EventEmitter();
159+
private eventEmitter: EventEmitter<DataServiceEventTypes> =
160+
new EventEmitter();
160161

161162
constructor({
162163
statusView,
@@ -231,7 +232,7 @@ export default class ConnectionController {
231232
}
232233

233234
if (loadedConnections.length) {
234-
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
235+
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
235236
}
236237

237238
// TODO: re-enable with fewer 'Saved Connections Loaded' events
@@ -439,7 +440,7 @@ export default class ConnectionController {
439440
});
440441
this._connectionAttempt = connectionAttempt;
441442
this._connectingConnectionId = connectionId;
442-
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
443+
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
443444

444445
if (this._activeDataService) {
445446
log.info('Disconnecting from the previous connection...', {
@@ -531,7 +532,7 @@ export default class ConnectionController {
531532
this._connectingConnectionId = null;
532533
}
533534

534-
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
535+
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
535536
}
536537

537538
log.info('Successfully connected', { connectionId });
@@ -547,7 +548,7 @@ export default class ConnectionController {
547548
this._connectingConnectionId = null;
548549

549550
this._connections[connectionId].lastUsed = new Date();
550-
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);
551+
this.eventEmitter.emit('ACTIVE_CONNECTION_CHANGED');
551552
await this._connectionStorage.saveConnection(
552553
this._connections[connectionId],
553554
);
@@ -721,8 +722,8 @@ export default class ConnectionController {
721722
this._disconnecting = true;
722723
this._statusView.showMessage('Disconnecting from current connection...');
723724

724-
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
725-
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);
725+
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
726+
this.eventEmitter.emit('ACTIVE_CONNECTION_CHANGED');
726727

727728
if (!this._activeDataService) {
728729
log.error('Unable to disconnect: no active connection');
@@ -766,7 +767,7 @@ export default class ConnectionController {
766767

767768
delete this._connections[connectionId];
768769
await this._connectionStorage.removeConnection(connectionId);
769-
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
770+
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
770771
}
771772

772773
// Prompts the user to remove the connection then removes it on affirmation.
@@ -957,8 +958,8 @@ export default class ConnectionController {
957958
}
958959

959960
this._connections[connectionId].name = inputtedConnectionName;
960-
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
961-
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);
961+
this.eventEmitter.emit('CONNECTIONS_DID_CHANGE');
962+
this.eventEmitter.emit('ACTIVE_CONNECTION_CHANGED');
962963

963964
await this._connectionStorage.saveConnection(
964965
this._connections[connectionId],
@@ -969,14 +970,14 @@ export default class ConnectionController {
969970
}
970971

971972
addEventListener(
972-
eventType: DataServiceEventTypes,
973+
eventType: keyof DataServiceEventTypes,
973974
listener: () => void,
974975
): void {
975976
this.eventEmitter.addListener(eventType, listener);
976977
}
977978

978979
removeEventListener(
979-
eventType: DataServiceEventTypes,
980+
eventType: keyof DataServiceEventTypes,
980981
listener: () => void,
981982
): void {
982983
this.eventEmitter.removeListener(eventType, listener);

src/editors/activeConnectionCodeLensProvider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import EXTENSION_COMMANDS from '../commands';
44
import type ConnectionController from '../connectionController';
55
import { isPlayground } from '../utils/playground';
66
import { getDBFromConnectionString } from '../utils/connection-string-db';
7-
import { DataServiceEventTypes } from '../connectionController';
87

98
export default class ActiveConnectionCodeLensProvider
109
implements vscode.CodeLensProvider
@@ -28,7 +27,7 @@ export default class ActiveConnectionCodeLensProvider
2827
this._onDidChangeCodeLenses.fire();
2928
};
3029
this._connectionController.addEventListener(
31-
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
30+
'ACTIVE_CONNECTION_CHANGED',
3231
this._activeConnectionChangedHandler,
3332
);
3433
}
@@ -67,7 +66,7 @@ export default class ActiveConnectionCodeLensProvider
6766

6867
deactivate(): void {
6968
this._connectionController.removeEventListener(
70-
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
69+
'ACTIVE_CONNECTION_CHANGED',
7170
this._activeConnectionChangedHandler,
7271
);
7372
}

src/editors/memoryFileSystemProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class MemoryFileSystemProvider implements vscode.FileSystemProvider {
219219

220220
_emitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
221221
_bufferedEvents: vscode.FileChangeEvent[] = [];
222-
_fireSoonHandle?: NodeJS.Timer;
222+
_fireSoonHandle?: NodeJS.Timeout;
223223

224224
readonly onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> =
225225
this._emitter.event;

src/editors/playgroundController.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import os from 'os';
55

66
import type PlaygroundSelectionCodeActionProvider from './playgroundSelectionCodeActionProvider';
77
import type ConnectionController from '../connectionController';
8-
import { DataServiceEventTypes } from '../connectionController';
98
import { createLogger } from '../logging';
109
import type { ConnectionTreeItem } from '../explorer';
1110
import { CollectionTreeItem } from '../explorer';
@@ -103,7 +102,7 @@ export default class PlaygroundController {
103102
void this._activeConnectionChanged();
104103
};
105104
this._connectionController.addEventListener(
106-
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
105+
'ACTIVE_CONNECTION_CHANGED',
107106
this._activeConnectionChangedHandler,
108107
);
109108

@@ -684,7 +683,7 @@ export default class PlaygroundController {
684683

685684
deactivate(): void {
686685
this._connectionController.removeEventListener(
687-
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
686+
'ACTIVE_CONNECTION_CHANGED',
688687
this._activeConnectionChangedHandler,
689688
);
690689
}

src/explorer/explorerController.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type * as vscode from 'vscode';
22

33
import type ConnectionController from '../connectionController';
4-
import { DataServiceEventTypes } from '../connectionController';
54
import ExplorerTreeController from './explorerTreeController';
65
import { createTrackedTreeView } from '../utils/treeViewHelper';
76
import type { TelemetryService } from '../telemetry';
@@ -23,7 +22,7 @@ export default class ExplorerController {
2322
private createTreeView = (): void => {
2423
// Remove the listener that called this function.
2524
this._connectionController.removeEventListener(
26-
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
25+
'CONNECTIONS_DID_CHANGE',
2726
this.createTreeView,
2827
);
2928

@@ -41,7 +40,7 @@ export default class ExplorerController {
4140
// Listen for a change in connections to occur before we create the tree
4241
// so that we show the `viewsWelcome` before any connections are added.
4342
this._connectionController.addEventListener(
44-
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
43+
'CONNECTIONS_DID_CHANGE',
4544
this.createTreeView,
4645
);
4746
}

src/explorer/explorerTreeController.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from 'vscode';
22

33
import type ConnectionController from '../connectionController';
4-
import { DataServiceEventTypes } from '../connectionController';
54
import ConnectionTreeItem from './connectionTreeItem';
65
import { createLogger } from '../logging';
76
import { DOCUMENT_ITEM } from './documentTreeItem';
@@ -31,7 +30,7 @@ export default class ExplorerTreeController
3130

3231
// Subscribe to changes in the connections.
3332
this._connectionController.addEventListener(
34-
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
33+
'CONNECTIONS_DID_CHANGE',
3534
() => {
3635
this.refresh();
3736
},
@@ -42,7 +41,7 @@ export default class ExplorerTreeController
4241

4342
removeListeners(): void {
4443
this._connectionController.removeEventListener(
45-
DataServiceEventTypes.CONNECTIONS_DID_CHANGE,
44+
'CONNECTIONS_DID_CHANGE',
4645
() => {
4746
this.refresh();
4847
},

src/logging.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,33 @@ class Logger implements ILogger {
1515
this.name = name;
1616
}
1717

18+
private formatMessage(message?: any, ...optionalParams: any[]): string {
19+
return `[${this.name}] ${message} ${optionalParams.length ? util.inspect(optionalParams) : ''}`;
20+
}
21+
1822
public trace(message?: any, ...optionalParams: any[]): void {
19-
this.append(
20-
'TRACE',
21-
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
22-
);
23+
Logger.channel.trace(this.formatMessage(message, ...optionalParams));
2324
}
2425

2526
public debug(message?: any, ...optionalParams: any[]): void {
26-
this.append(
27-
'DEBUG',
28-
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
29-
);
27+
Logger.channel.debug(this.formatMessage(message, ...optionalParams));
3028
}
3129

3230
public info(message?: any, ...optionalParams: any[]): void {
33-
this.append(
34-
'INFO ',
35-
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
36-
);
31+
Logger.channel.info(this.formatMessage(message, ...optionalParams));
3732
}
3833

3934
public warn(message?: any, ...optionalParams: any[]): void {
40-
this.append(
41-
'WARN ',
42-
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
43-
);
35+
Logger.channel.warn(this.formatMessage(message, ...optionalParams));
4436
}
4537

4638
public error(message?: any, ...optionalParams: any[]): void {
47-
this.append(
48-
'ERROR',
49-
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
50-
);
39+
Logger.channel.error(this.formatMessage(message, ...optionalParams));
5140
}
5241

5342
public fatal(message?: any, ...optionalParams: any[]): void {
54-
this.append(
55-
'FATAL',
56-
`${message} ${optionalParams ? util.inspect(optionalParams) : ''}`,
57-
);
58-
}
59-
60-
private append(type: string, message: string): void {
61-
// https://code.visualstudio.com/api/references/vscode-api#window.createOutputChannel
62-
63-
Logger.channel.appendLine(
64-
`${new Date().toISOString()} ${this.name} ${type} ${message}\n`,
43+
Logger.channel.error(
44+
`FATAL: ${this.formatMessage(message, ...optionalParams)}`,
6545
);
6646
}
6747
}

0 commit comments

Comments
 (0)