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
88 changes: 41 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@
"mongodb-connection-string-url": "^3.0.2",
"mongodb-data-service": "^22.30.1",
"mongodb-log-writer": "^2.4.1",
"mongodb-mcp-server": "^0.3.1-prerelease.1",
"mongodb-mcp-server": "^1.0.0",
"mongodb-query-parser": "^4.4.2",
"mongodb-schema": "^12.6.2",
"node-machine-id": "1.1.12",
Expand Down
72 changes: 72 additions & 0 deletions src/mcp/mcpConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
type UserConfig,
configurableProperties,
defaultUserConfig,
} from 'mongodb-mcp-server';
import * as vscode from 'vscode';
import { createLogger } from '../logging';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { contributes } = require('../../package.json');

const logger = createLogger('mcp-config');

export function getMCPConfigFromVSCodeSettings(
packageJsonConfiguredProperties: Record<string, unknown> = contributes
?.configuration?.properties ?? {},
retrieveMCPConfiguration: () => vscode.WorkspaceConfiguration = (): vscode.WorkspaceConfiguration =>
vscode.workspace.getConfiguration('mdb.mcp'),
): Partial<UserConfig> {
// We're attempting to:
// 1. Use only the config values for MCP server exposed by VSCode config
// 2. Use only the config values that are relevant for MCP server (all mcp
// config exposed by VSCode does contain some irrelevant config as well,
// such as `server`)
const vscConfiguredProperties = Object.keys(packageJsonConfiguredProperties)
.filter((key) => key.startsWith('mdb.mcp'))
.map((key) => key.replace(/^mdb\.mcp\./, ''))
.filter((property) => configurableProperties.has(property));

logger.debug('Will retrieve MCP config for the following properties', {
vscConfiguredProperties,
});

const mcpConfiguration = retrieveMCPConfiguration();
return Object.fromEntries(
vscConfiguredProperties.map((property) => {
const configuredValue = mcpConfiguration.get(property);
return [
property,
// Most of the MCP config, if not all, consists of non-null configs and it is
// possible for a VSCode config to have a null value edited directly in the
// settings file which is why to safeguard against incorrect values we map
// them at-least to the expected defaults.
mcpConfigValues(property, configuredValue),
];
}),
);
}

// eslint-disable-next-line complexity
function mcpConfigValues(property: string, configuredValue: unknown): unknown {
switch (property) {
case 'apiBaseUrl':
case 'apiClientId':
case 'apiClientSecret':
case 'exportsPath': {
const trimmedValue = String(configuredValue).trim();
return typeof configuredValue === 'string' && !!trimmedValue
? trimmedValue
: defaultUserConfig[property];
}
case 'disabledTools':
return Array.isArray(configuredValue)
? configuredValue
: defaultUserConfig.disabledTools;
case 'readOnly':
case 'indexCheck':
case 'exportTimeoutMs':
case 'exportCleanupIntervalMs':
default:
return configuredValue ?? defaultUserConfig[property];
}
}
28 changes: 24 additions & 4 deletions src/mcp/mcpController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createLogger } from '../logging';
import type { MCPConnectParams } from './mcpConnectionManager';
import { MCPConnectionManager } from './mcpConnectionManager';
import { createMCPConnectionErrorHandler } from './mcpConnectionErrorHandler';
import { getMCPConfigFromVSCodeSettings } from './mcpConfig';

export type McpServerStartupConfig = 'enabled' | 'disabled';

Expand Down Expand Up @@ -64,6 +65,9 @@ export class MCPController {
this.context = context;
this.connectionController = connectionController;
this.getTelemetryAnonymousId = getTelemetryAnonymousId;
}

public async activate(): Promise<void> {
this.context.subscriptions.push(
vscode.lm.registerMcpServerDefinitionProvider('mongodb', {
onDidChangeMcpServerDefinitions: this.didChangeEmitter.event,
Expand All @@ -75,9 +79,7 @@ export class MCPController {
},
}),
);
}

public async activate(): Promise<void> {
this.connectionController.addEventListener(
'ACTIVE_CONNECTION_CHANGED',
() => {
Expand All @@ -102,14 +104,32 @@ export class MCPController {
};
registerGlobalSecretToRedact(token, 'password');

const vscodeConfiguredMCPConfig = getMCPConfigFromVSCodeSettings();

const mcpConfig: UserConfig = {
...defaultUserConfig,
...vscodeConfiguredMCPConfig,
transport: 'http',
httpPort: 0,
httpHeaders: headers,
disabledTools: ['connect'],
loggers: ['mcp'],
disabledTools: Array.from(
new Set([
'connect',
...(vscodeConfiguredMCPConfig.disabledTools ?? []),
]),
),
loggers: Array.from(
new Set(['mcp', ...(vscodeConfiguredMCPConfig.loggers ?? [])]),
),
};

logger.info('Starting MCP server with config', {
...mcpConfig,
httpHeaders: '<redacted>',
apiClientId: '<redacted>',
apiClientSecret: '<redacted>',
});

const createConnectionManager: ConnectionManagerFactoryFn = async ({
logger,
}) => {
Expand Down
Loading
Loading