diff --git a/package-lock.json b/package-lock.json index 072b3a26f63..3dad1dccc3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19241,6 +19241,15 @@ } } }, + "node_modules/@vercel/oidc": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@vercel/oidc/-/oidc-3.2.0.tgz", + "integrity": "sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug==", + "license": "Apache-2.0", + "engines": { + "node": ">= 20" + } + }, "node_modules/@vscode/l10n": { "version": "0.0.18", "resolved": "https://registry.npmjs.org/@vscode/l10n/-/l10n-0.0.18.tgz", @@ -49962,9 +49971,9 @@ } }, "node_modules/undici": { - "version": "6.25.0", - "resolved": "https://registry.npmjs.org/undici/-/undici-6.25.0.tgz", - "integrity": "sha512-ZgpWDC5gmNiuY9CnLVXEH8rl50xhRCuLNA97fAUnKi8RRuV4E6KG31pDTsLVUKnohJE0I3XDrTeEydAXRw47xg==", + "version": "6.29.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.29.1.tgz", + "integrity": "sha512-EvRlUt/QqAEvXdQeRMYUfwOQsfzpDztykrG2owGEE/rBOBoYv9uF39ZzsNDh4lTFHhTr2XOZjKkYgueD12ZmGA==", "dev": true, "license": "MIT", "engines": { @@ -53204,15 +53213,6 @@ "node": ">=4" } }, - "packages/compass-assistant/node_modules/@vercel/oidc": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@vercel/oidc/-/oidc-3.2.0.tgz", - "integrity": "sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug==", - "license": "Apache-2.0", - "engines": { - "node": ">= 20" - } - }, "packages/compass-assistant/node_modules/ai": { "version": "7.0.154", "resolved": "https://registry.npmjs.org/ai/-/ai-7.0.154.tgz", @@ -55321,7 +55321,8 @@ "@mongodb-js/shell-bson-parser": "^1.6.3", "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.0", - "ai": "^7.0.0", + "ai": "^6.0.3", + "bson": "^7.3.2", "ipaddr.js": "^2.5.0", "mongodb": "^7.5.0", "mongodb-connection-string-url": "^7.0.2", @@ -55347,7 +55348,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^3.35.0", - "bson": "^7.3.2", "chai": "^4.3.6", "depcheck": "^1.4.1", "electron-mocha": "^12.2.0", @@ -55453,15 +55453,6 @@ "@types/node": "*" } }, - "packages/compass-generative-ai/node_modules/@vercel/oidc": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@vercel/oidc/-/oidc-3.2.0.tgz", - "integrity": "sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug==", - "license": "Apache-2.0", - "engines": { - "node": ">= 20" - } - }, "packages/compass-generative-ai/node_modules/accepts": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", diff --git a/packages/compass-assistant/test/tool-calls.eval.ts b/packages/compass-assistant/test/tool-calls.eval.ts index bf193e3b753..fcab42ba971 100644 --- a/packages/compass-assistant/test/tool-calls.eval.ts +++ b/packages/compass-assistant/test/tool-calls.eval.ts @@ -9,6 +9,7 @@ */ import { strict as assert } from 'node:assert'; +import { MessageChannel } from 'node:worker_threads'; import { createOpenAI } from '@ai-sdk/openai'; import { streamText } from 'ai'; import type { ToolSet, ToolCallPart } from 'ai'; @@ -17,6 +18,7 @@ import { ToolsController, type ToolGroup, } from '@mongodb-js/compass-generative-ai/provider'; +import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider'; import { defaultPreferencesInstance } from '@mongodb-js/compass-preferences-model'; @@ -65,6 +67,12 @@ const DEFAULT_EVAL_TASK_CONFIG: EvalTaskConfig = { const logger = createNoopLogger('EVAL-TOOLS-CONTROLLER'); +// There is no utility process in Node, so the host runs in this one, on the +// other end of a plain MessageChannel. Held in a variable so the run can +// release its server and driver connection when it finishes. +const evalChannel = new MessageChannel(); +const evalHost = new ToolsHost(evalChannel.port2, { logger }); + const toolsController = new ToolsController({ logger, getTelemetryAnonymousId: () => 'eval-anonymous-id', @@ -72,6 +80,7 @@ const toolsController = new ToolsController({ enableMCPTelemetry: false, preferences: defaultPreferencesInstance, atlasAdminApi: {} as AtlasAdminApiService, + openPort: () => evalChannel.port1, }); let toolsInitialized = false; @@ -83,9 +92,6 @@ let toolsInitialized = false; async function ensureToolsInitialized(): Promise { if (toolsInitialized) return; await toolsController.startServer(); - if (!toolsController.server) { - throw new Error('Failed to start ToolsController server for eval tools'); - } toolsInitialized = true; } @@ -218,6 +224,8 @@ function createToolCallAssistantTask(config: EvalTaskConfig) { connectionConfig, }); + assert(tools['find'], 'ToolsController did not provide database tools'); + const contextPrompt = buildContextPromptText({ custom, connectionString: connectionConfig.connectionString, @@ -277,6 +285,7 @@ async function main() { }); } finally { await toolsController.stopServer(); + await evalHost.stop(); await client.close(); await cluster.close(); // FIXME(COMPASS-10486): We need to have this otherwise the process hangs. diff --git a/packages/compass-e2e-tests/tests/assistant.test.ts b/packages/compass-e2e-tests/tests/assistant.test.ts index 8bd1ead7c77..7ee07017f5c 100644 --- a/packages/compass-e2e-tests/tests/assistant.test.ts +++ b/packages/compass-e2e-tests/tests/assistant.test.ts @@ -182,12 +182,12 @@ describe('MongoDB Assistant (with real backend)', function () { { timeout: 30_000 } ); - // Expand the tool call card to see the response const expandButton = chatMessages.$( 'button[aria-label="Expand additional content"]' ); - await expandButton.waitForDisplayed(); - await browser.clickVisible(expandButton); + if (await expandButton.isExisting()) { + await browser.clickVisible(expandButton); + } // Wait for the expanded content to include the response with structuredContent.databases await browser.waitUntil( diff --git a/packages/compass-generative-ai/package.json b/packages/compass-generative-ai/package.json index 2a1fb9dd999..e048aa1c5bd 100644 --- a/packages/compass-generative-ai/package.json +++ b/packages/compass-generative-ai/package.json @@ -25,11 +25,13 @@ "compass:main": "src/index.ts", "exports": { ".": "./dist/index.js", - "./provider": "./dist/provider.js" + "./provider": "./dist/provider.js", + "./utility": "./dist/tools/infrastructure/tools-host.js" }, "compass:exports": { ".": "./src/index.ts", - "./provider": "./src/provider.tsx" + "./provider": "./src/provider.tsx", + "./utility": "./src/tools/infrastructure/tools-host.ts" }, "types": "./dist/index.d.ts", "scripts": { @@ -70,7 +72,8 @@ "@mongodb-js/shell-bson-parser": "^1.6.3", "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.0", - "ai": "^7.0.0", + "ai": "^6.0.3", + "bson": "^7.3.2", "ipaddr.js": "^2.5.0", "mongodb": "^7.5.0", "mongodb-connection-string-url": "^7.0.2", @@ -96,7 +99,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^3.35.0", - "bson": "^7.3.2", "chai": "^4.3.6", "depcheck": "^1.4.1", "electron-mocha": "^12.2.0", diff --git a/packages/compass-generative-ai/src/provider.tsx b/packages/compass-generative-ai/src/provider.tsx index 5991c106002..7acb9bb935b 100644 --- a/packages/compass-generative-ai/src/provider.tsx +++ b/packages/compass-generative-ai/src/provider.tsx @@ -12,6 +12,7 @@ import { import { atlasAdminApiServiceLocator } from '@mongodb-js/atlas-admin-api/provider'; import { telemetryLocator } from '@mongodb-js/compass-telemetry/provider'; import { ToolsController } from './tools-controller'; +import type { ToolsRpcPort } from './tools/infrastructure/messages'; import { AtlasAiService } from './atlas-ai-service'; const AtlasAiServiceContext = createContext(null); @@ -60,8 +61,17 @@ export { AtlasAiService } from './atlas-ai-service'; const ToolsControllerContext = createContext(null); export const ToolsControllerProvider: React.FC<{ + /** + * How the controller reaches its tools host. Defaults to the desktop + * utility process; the browser has none, so compass-web provides a loopback + * that runs the same host in the page. + */ + openPort?: () => ToolsRpcPort; children?: React.ReactNode; -}> = createServiceProvider(function ToolsControllerProvider({ children }) { +}> = createServiceProvider(function ToolsControllerProvider({ + openPort, + children, +}) { const logger = useLogger('TOOLS-CONTROLLER'); const preferences = preferencesLocator(); const atlasAdminApi = atlasAdminApiServiceLocator(); @@ -78,8 +88,16 @@ export const ToolsControllerProvider: React.FC<{ enableMCPTelemetry: false, preferences, atlasAdminApi, + openPort, }); - }, [logger, telemetryAnonymousId, track, preferences, atlasAdminApi]); + }, [ + logger, + telemetryAnonymousId, + track, + preferences, + atlasAdminApi, + openPort, + ]); useEffect(() => { return () => { diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index b471023d6bf..6bb98167926 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -1,3 +1,4 @@ +import { MessageChannel } from 'node:worker_threads'; import sinon from 'sinon'; import { expect } from 'chai'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; @@ -9,11 +10,15 @@ import { } from '@mongodb-js/compass-preferences-model'; import type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; import type { ToolsConnectParams } from './tools-connection-manager'; -import { READ_ONLY_DATABASE_TOOLS } from './available-tools'; +import { ToolsHost } from './tools/infrastructure/tools-host'; import type { ToolGroup } from './tools-controller'; import { ToolsController } from './tools-controller'; describe('ToolsController', function () { + // The MCP server starts in the utility host on the other end of the port; + // starting it and registering tools is not instant. + this.timeout(10_000); + let sandbox: sinon.SinonSandbox; let logger: Logger; let toolsController: ToolsController; @@ -23,25 +28,65 @@ describe('ToolsController', function () { const baseTools = ['atlas-connection-error-debugger']; - beforeEach(async function () { - sandbox = sinon.createSandbox(); - logger = createNoopLogger(); - getTelemetryAnonymousId = sandbox.stub().returns('test-anonymous-id'); - preferences = await createSandboxFromDefaultPreferences(); - atlasAdminApi = {} as AtlasAdminApiService; - - toolsController = new ToolsController({ + // Every controller gets its own channel with a real ToolsHost on the other + // end, so the tests exercise the whole rpc boundary. + const openChannels: MessageChannel[] = []; + const openHosts: ToolsHost[] = []; + + /** + * setContext delivers the utility's config without awaiting the rpc call, so + * tests wait for the effect instead of guessing how long a message takes. + */ + async function waitFor( + condition: () => boolean, + timeoutMs = 2000 + ): Promise { + const deadline = Date.now() + timeoutMs; + while (!condition() && Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, 5)); + } + } + + function createController( + overrides: Partial[0]> = {} + ): ToolsController { + const channel = new MessageChannel(); + const host = new ToolsHost(channel.port2, { logger }); + openChannels.push(channel); + openHosts.push(host); + return new ToolsController({ enableMCPTelemetry: false, logger, getTelemetryAnonymousId, track: createNoopTrack(), preferences, atlasAdminApi, + openPort: () => channel.port1, + ...overrides, }); + } + + beforeEach(async function () { + sandbox = sinon.createSandbox(); + logger = createNoopLogger(); + getTelemetryAnonymousId = sandbox.stub().returns('test-anonymous-id'); + preferences = await createSandboxFromDefaultPreferences(); + atlasAdminApi = {} as AtlasAdminApiService; + + toolsController = createController(); }); afterEach(async function () { await toolsController.stopServer(); + for (const host of openHosts) { + await host.stop(); + } + for (const channel of openChannels) { + channel.port1.close(); + channel.port2.close(); + } + openChannels.length = 0; + openHosts.length = 0; sandbox.restore(); }); @@ -54,10 +99,6 @@ describe('ToolsController', function () { const tools = toolsController.getActiveTools(); expect(Object.keys(tools)).to.have.lengthOf(baseTools.length); }); - - it('server is initially undefined', function () { - expect(toolsController.server).to.be.undefined; - }); }); describe('setActiveTools', function () { @@ -244,14 +285,7 @@ describe('ToolsController', function () { }); it('ignores db tools if the server is not started', function () { - const newController = new ToolsController({ - enableMCPTelemetry: false, - logger, - getTelemetryAnonymousId, - track: createNoopTrack(), - preferences, - atlasAdminApi, - }); + const newController = createController(); newController.setActiveTools(new Set(['db-read'])); expect(Object.keys(newController.getActiveTools())).to.deep.equal( @@ -276,40 +310,10 @@ describe('ToolsController', function () { ]; for (const toolName of readonlyToolNames) { - expect(tools).to.have.property(toolName); + expect(tools, toolName).to.have.property(toolName); } }); - it('ensures all read only database tools are retrievable from the server after initialization', function () { - expect(toolsController.server).to.not.be.undefined; - expect(toolsController.server?.tools).to.not.be.undefined; - - if ( - toolsController.server && - toolsController.server.tools.length === 0 - ) { - toolsController.server.registerTools(); - } - - const serverTools = toolsController.server?.tools ?? []; - const serverToolNames = serverTools.map((tool) => tool.name); - - for (const tool of READ_ONLY_DATABASE_TOOLS) { - expect( - serverToolNames, - `Expected server to have tool: ${tool.name}` - ).to.include(tool.name); - } - - // Verify the count matches - expect( - serverToolNames.filter((name) => - READ_ONLY_DATABASE_TOOLS.map((t) => t.name).includes(name) - ).length, - 'Expected all READ_ONLY_DATABASE_TOOLS to be in server tools' - ).to.equal(READ_ONLY_DATABASE_TOOLS.length); - }); - it('does not include non-readonly tools', function () { const tools = toolsController.getActiveTools(); // These tools should not be included even if they exist in the MCP server @@ -318,12 +322,11 @@ describe('ToolsController', function () { expect(tools).to.not.have.property('update-many'); }); - it('database tools have needsApproval and strict properties', function () { + it('database tools have needsApproval property', function () { const tools = toolsController.getActiveTools(); const listDatabasesTool = tools['list-databases']; expect(listDatabasesTool).to.have.property('needsApproval', true); - expect(listDatabasesTool).to.have.property('strict', true); }); it('collection-schema tool has a custom description that instructs the model to use it before queries and aggregations', function () { @@ -436,42 +439,61 @@ describe('ToolsController', function () { expect(result.query).to.equal('new query'); }); - it('syncs telemetry setting with runner userConfig', function () { + it('uses the context set before startup when the server starts', async function () { + // Context often arrives before the first message starts the utility; + // those values must not be replaced by the constructor defaults. toolsController.setContext({ enableMCPTelemetry: true, + maxTimeMS: 1234, connections: [], }); - expect((toolsController as any).runner.userConfig.telemetry).to.equal( - 'enabled' - ); + + await toolsController.startServer(); + const runner = (openHosts[0] as any).runner; + + await waitFor(() => runner.userConfig.maxTimeMS === 1234); + expect(runner.userConfig.telemetry).to.equal('enabled'); + expect(runner.userConfig.maxTimeMS).to.equal(1234); + }); + + it('syncs telemetry setting with the utility host runner', async function () { + await toolsController.startServer(); + const runner = (openHosts[0] as any).runner; + + toolsController.setContext({ + enableMCPTelemetry: true, + connections: [], + }); + await waitFor(() => runner.userConfig.telemetry === 'enabled'); + expect(runner.userConfig.telemetry).to.equal('enabled'); toolsController.setContext({ enableMCPTelemetry: false, connections: [], }); - expect((toolsController as any).runner.userConfig.telemetry).to.equal( - 'disabled' - ); + await waitFor(() => runner.userConfig.telemetry === 'disabled'); + expect(runner.userConfig.telemetry).to.equal('disabled'); }); - it('syncs maxTimeMS setting with runner userConfig', function () { + it('syncs maxTimeMS setting with the utility host runner', async function () { + await toolsController.startServer(); + const runner = (openHosts[0] as any).runner; + toolsController.setContext({ enableMCPTelemetry: false, maxTimeMS: 5000, connections: [], }); - expect((toolsController as any).runner.userConfig.maxTimeMS).to.equal( - 5000 - ); + await waitFor(() => runner.userConfig.maxTimeMS === 5000); + expect(runner.userConfig.maxTimeMS).to.equal(5000); toolsController.setContext({ enableMCPTelemetry: false, maxTimeMS: undefined, connections: [], }); - expect((toolsController as any).runner.userConfig.maxTimeMS).to.equal( - undefined - ); + await waitFor(() => runner.userConfig.maxTimeMS === undefined); + expect(runner.userConfig.maxTimeMS).to.equal(undefined); }); }); @@ -508,22 +530,48 @@ describe('ToolsController', function () { describe('server lifecycle', function () { describe('startServer', function () { - it('starts the MCP server', async function () { - expect(toolsController.server).to.be.undefined; - + it('fetches the db tool definitions from the utility host', async function () { await toolsController.startServer(); + toolsController.setActiveTools(new Set(['db-read'])); - expect(toolsController.server).to.not.be.undefined; + expect(toolsController.getActiveTools()).to.have.property('find'); }); - it('does not restart server if already started', async function () { - await toolsController.startServer(); - const firstServer = toolsController.server; + it('does not open a second port if already started', async function () { + const openPort = sandbox.stub().returns(openChannels[0].port1); + const controller = createController({ openPort }); - await toolsController.startServer(); - const secondServer = toolsController.server; + await controller.startServer(); + await controller.startServer(); - expect(firstServer).to.equal(secondServer); + expect(openPort.calledOnce).to.be.true; + }); + + it('shares one startup between concurrent calls', async function () { + const openPort = sandbox.stub().returns(openChannels[0].port1); + const controller = createController({ openPort }); + controller.setActiveTools(new Set(['db-read'])); + + await Promise.all([controller.startServer(), controller.startServer()]); + + expect(openPort.calledOnce).to.be.true; + expect(controller.getActiveTools()).to.have.property('find'); + }); + + it('gives up on a utility that never answers', async function () { + this.timeout(20_000); + const silentChannel = new MessageChannel(); + openChannels.push(silentChannel); + const controller = createController({ + openPort: () => silentChannel.port1, + }); + controller.setActiveTools(new Set(['db-read'])); + + await controller.startServer(); + + expect(Object.keys(controller.getActiveTools())).to.deep.equal( + baseTools + ); }); it('logs when server starts successfully', async function () { @@ -540,21 +588,16 @@ describe('ToolsController', function () { logStub.calledWith( sinon.match.number, 'ToolsController', - 'In-memory MCP server started successfully' + 'Assistant tools utility process client started successfully' ) ).to.be.true; }); it('handles errors during server startup gracefully', async function () { - const errorController = new ToolsController({ - enableMCPTelemetry: false, - logger, + const errorController = createController({ getTelemetryAnonymousId: () => { throw new Error('Telemetry error'); }, - track: createNoopTrack(), - preferences, - atlasAdminApi, }); // Should not throw even if there's an error @@ -567,7 +610,6 @@ describe('ToolsController', function () { describe('stopServer', function () { it('stops the MCP server', async function () { await toolsController.startServer(); - expect(toolsController.server).to.not.be.undefined; await toolsController.stopServer(); @@ -575,6 +617,17 @@ describe('ToolsController', function () { expect(() => toolsController.stopServer()).to.not.throw(); }); + it('drops the db tools after stopping', async function () { + await toolsController.startServer(); + toolsController.setActiveTools(new Set(['db-read'])); + expect(toolsController.getActiveTools()).to.have.property('find'); + + await toolsController.stopServer(); + expect(Object.keys(toolsController.getActiveTools())).to.deep.equal( + baseTools + ); + }); + it('handles stop when server is not running', async function () { await toolsController.stopServer(); @@ -583,11 +636,6 @@ describe('ToolsController', function () { }); it('logs when server stops successfully', async function () { - // TODO: flushTimeout.unref is not a function - if ((process as any).type === 'renderer') { - return this.skip(); - } - await toolsController.startServer(); const logStub = sandbox.stub(logger.log, 'info'); @@ -609,19 +657,6 @@ describe('ToolsController', function () { }); }); - describe('server property', function () { - it('returns undefined when server is not started', function () { - expect(toolsController.server).to.be.undefined; - }); - - it('returns server instance when started', async function () { - await toolsController.startServer(); - - expect(toolsController.server).to.not.be.undefined; - expect(toolsController.server).to.have.property('tools'); - }); - }); - describe('integration scenarios', function () { it('can use multiple tool groups together', async function () { await toolsController.startServer(); diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 29403be0fd5..1b4adb587e5 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -1,29 +1,17 @@ -import { tool } from 'ai'; +import { tool, jsonSchema } from 'ai'; import type { ToolSet } from 'ai'; import type { Logger } from '@mongodb-js/compass-logging'; import z from 'zod/v4'; -import { - Keychain, - TransportRunnerBase, - UserConfigSchema, -} from 'mongodb-mcp-server'; -import type { - DefaultMetrics, - Metrics, - Server, - TransportRunnerConfig, - UserConfig, -} from 'mongodb-mcp-server'; +import { openUtilityPort } from '@mongodb-js/compass-utils'; import type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; import type { PreferencesAccess } from '@mongodb-js/compass-preferences-model'; import { getAtlasConfigForPreset } from '@mongodb-js/atlas-service/provider'; import type { TrackFunction } from '@mongodb-js/compass-telemetry'; -import { createConnectionErrorHandler } from './tools-connection-error-handler'; -import { ToolsLogger } from './tools-logger'; -import { ToolsConnectionManager } from './tools-connection-manager'; +import { createToolsRpcClient } from './tools/infrastructure/rpc-client'; +import { encodeConnectParams } from './tools/infrastructure/connect-params'; +import type { ToolsRpcPort } from './tools/infrastructure/messages'; +import type { ToolDefinitionInfo } from './tools/infrastructure/tools-host'; import type { ToolsConnectParams } from './tools-connection-manager'; -import { removeZodTransforms } from './remove-zod-transforms'; -import { READ_ONLY_DATABASE_TOOLS } from './available-tools'; import { debugConnection, debugConnectionDescription, @@ -31,6 +19,8 @@ import { export type ToolGroup = 'querybar' | 'aggregation-builder' | 'db-read'; +const ASSISTANT_TOOLS_STARTUP_TIMEOUT_MS = 10_000; + type CompassContext = { enableMCPTelemetry: boolean; maxTimeMS?: number; @@ -42,44 +32,6 @@ type ToolsContext = CompassContext & { connections: ToolsConnectParams[]; }; -class NoopDefaultMetrics { - observe(): void { - // no-op - } -} - -class NoopMetrics implements Metrics { - constructor() {} - get(_key: K): DefaultMetrics[K] { - return new NoopDefaultMetrics() as unknown as DefaultMetrics[K]; - } - getMetrics(): Promise { - return Promise.resolve(''); - } -} - -/** - * In-memory MCP runner that doesn't bind to any external transport. - * The server is kept in memory for direct programmatic access. - */ -class InMemoryRunner extends TransportRunnerBase { - public server?: Server; - public readonly userConfig: UserConfig; - - constructor(config: TransportRunnerConfig) { - super(config); - this.userConfig = config.userConfig; - } - - async start(): Promise { - this.server = await this.setupServer(); - } - - async closeTransport(): Promise { - await this.server?.close(); - } -} - type ToolsControllerConfig = { logger: Logger; preferences: PreferencesAccess; @@ -88,6 +40,8 @@ type ToolsControllerConfig = { enableMCPTelemetry: boolean; maxTimeMS?: number; atlasAdminApi: AtlasAdminApiService; + // Injected for tests + openPort?: () => ToolsRpcPort; }; export class ToolsController { @@ -95,65 +49,38 @@ export class ToolsController { private preferences: PreferencesAccess; private toolGroups: Set = new Set(); private context: ToolsContext = Object.create(null); - private readonly runner: InMemoryRunner; - private connectionManager: ToolsConnectionManager; private connectionIdByToolCallId: Record = Object.create(null); private readonly atlasAdminApi: AtlasAdminApiService; private readonly track: TrackFunction; + private readonly getTelemetryAnonymousId: () => string; + private readonly openPort: () => ToolsRpcPort; + /** Latest config known to the renderer, sent when the utility starts */ + private currentConfig: { + enableMCPTelemetry: boolean; + maxTimeMS?: number; + }; + private portClient: ReturnType | null = null; + private startPromise: Promise | null = null; + private dbToolDefinitions: ToolDefinitionInfo[] = []; constructor({ logger, getTelemetryAnonymousId, - track, enableMCPTelemetry, maxTimeMS, atlasAdminApi, preferences, + track, + openPort, }: ToolsControllerConfig) { this.logger = logger; + this.getTelemetryAnonymousId = getTelemetryAnonymousId; this.atlasAdminApi = atlasAdminApi; this.preferences = preferences; this.track = track; - const mcpConfig = UserConfigSchema.parse({ - disabledTools: ['connect'], - loggers: ['mcp'], - readOnly: true, - // NOTE: the preferences could change at runtime. As a best-effort way of - // keeping them in sync we'll change them every time we set the tools' context - telemetry: enableMCPTelemetry ? 'enabled' : 'disabled', - maxTimeMS, - }); - - this.runner = new InMemoryRunner({ - userConfig: mcpConfig, - createConnectionManager: () => Promise.resolve(this.connectionManager), - connectionErrorHandler: createConnectionErrorHandler(), - additionalLoggers: [new ToolsLogger(Keychain.root, this.logger)], - telemetryProperties: { - hosting_mode: 'compass', - }, - metrics: new NoopMetrics(), - }); - - this.connectionManager = new ToolsConnectionManager({ - logger: this.runner.logger, - getTelemetryAnonymousId, - }); - - // Set up listener on close event to perform a cleanup when the Client - // closes connection to MCP server and eventually ConnectionManager shuts down. - this.connectionManager.events.on('close', (): void => { - this.logger.log.info( - this.logger.mongoLogId(1_001_000_405), - 'Tools Controller', - 'MCPConnectionManager closed. Performing cleanup', - { - connectionManagerClientName: this.connectionManager.clientName, - } - ); - // Nothing to do because this will only happen once when Compass is closed - }); + this.openPort = openPort ?? (() => openUtilityPort('ai-assistant')); + this.currentConfig = { enableMCPTelemetry, maxTimeMS }; } setActiveTools(toolGroups: Set): void { @@ -201,91 +128,24 @@ export class ToolsController { }; } - if (this.toolGroups.has('db-read') && this.runner.server) { - const readonlyDatabaseToolNames = READ_ONLY_DATABASE_TOOLS.map( - (tool) => tool.name - ); - if (this.runner.server.tools.length === 0) { - this.runner.server.registerTools(); - } - - const availableTools = this.runner.server.tools ?? []; - for (const toolBase of availableTools) { - if (!readonlyDatabaseToolNames.includes(toolBase.name)) { - continue; - } - - tools[toolBase.name] = tool({ - description: getCustomToolDescription(toolBase), - inputSchema: z.object( - Object.fromEntries( - Object.entries(toolBase.argsShape).map(([key, value]) => { - // TODO: MCP server applies transformations like toEJSON. - // We should come up with a better solution for this but for now we recursively remove the transforms. - // AI SDK applies transformations defined in the schema *before* sending the request to the model. - return [key, removeZodTransforms(value)]; - }) - ) - ), + if (this.toolGroups.has('db-read') && this.dbToolDefinitions.length) { + for (const definition of this.dbToolDefinitions) { + tools[definition.name] = tool({ + description: getCustomToolDescription(definition), + inputSchema: jsonSchema(definition.inputSchema), needsApproval: true, - strict: true, - execute: async (args, options) => { - const connectionId = - this.connectionIdByToolCallId[options.toolCallId]; - if (!connectionId) { - // sanity check in case we ever get multiple tool calls in parallel - const error = new Error("Can't find connection for tool call"); - this.logger.log.error( - this.logger.mongoLogId(1_001_000_415), - 'ToolsController', - error.message, - { - toolCallId: options.toolCallId, - } - ); - throw error; - } - - const connectParams = this.context.connections.find( - (connection) => connection.connectionId === connectionId + strict: false, + execute: async (args: unknown, options) => { + const connectParams = this.resolveConnectParams(options.toolCallId); + return await this.portClient!.call( + 'performToolCall', + { + toolName: definition.name, + toolArgs: args, + connectParams: encodeConnectParams(connectParams), + }, + options.abortSignal ); - if (!connectParams) { - // the context could have changed between when the tool was - // created and when it gets executed - const error = new Error('No active connection to execute tool'); - this.logger.log.error( - this.logger.mongoLogId(1_001_000_414), - 'ToolsController', - error.message - ); - throw error; - } - - // connect - try { - await this.connectionManager.connectToCompassConnection( - connectParams - ); - } catch (error: any) { - this.logger.log.error( - this.logger.mongoLogId(1_001_000_410), - 'ToolsController', - 'Error when attempting to connect to Compass connection before executing tool', - { error: error.message } - ); - throw error; - } - - let result: Awaited>; - try { - result = await toolBase.invoke(args, { - signal: options.abortSignal ?? new AbortSignal(), - }); - } finally { - // disconnect - await this.connectionManager.disconnect(); - } - return result; }, }); } @@ -335,12 +195,23 @@ export class ToolsController { } setContext(context: ToolsContext): void { - // make sure these properties are always in sync with the tools' config - this.runner.userConfig.telemetry = context.enableMCPTelemetry - ? 'enabled' - : 'disabled'; - this.runner.userConfig.maxTimeMS = context.maxTimeMS; this.context = context; + this.currentConfig = { + enableMCPTelemetry: context.enableMCPTelemetry, + maxTimeMS: context.maxTimeMS, + }; + void this.portClient + ?.call('setUserConfig', { + enableMCPTelemetry: context.enableMCPTelemetry, + maxTimeMS: context.maxTimeMS, + telemetryAnonymousId: this.getTelemetryAnonymousId(), + }) + .catch((error: unknown) => { + this.logger.debug( + 'Error when updating assistant tools configuration', + error + ); + }); } setConnectionIdForToolCall({ @@ -353,34 +224,35 @@ export class ToolsController { this.connectionIdByToolCallId[toolCallId] = connectionId; } - // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents - get server(): Server | undefined { - return this.runner?.server; + public startServer(): Promise { + this.startPromise ??= this.startServerOnce(); + return this.startPromise; } - public async startServer(): Promise { - if (this.runner.server) { - return; - } - + private async startServerOnce(): Promise { try { this.logger.log.info( this.logger.mongoLogId(1_001_000_402), 'ToolsController', - 'Starting in-memory MCP server with config', - { - ...this.runner.userConfig, - apiClientId: '', - apiClientSecret: '', - } + 'Starting assistant tools utility process client' ); - await this.runner.start(); + this.portClient = createToolsRpcClient(this.openPort()); + await this.withStartupTimeout(async () => { + await this.portClient!.call('setUserConfig', { + ...this.currentConfig, + telemetryAnonymousId: this.getTelemetryAnonymousId(), + }); + + this.dbToolDefinitions = (await this.portClient!.call( + 'listDatabaseTools' + )) as ToolDefinitionInfo[]; + }); this.logger.log.info( this.logger.mongoLogId(1_001_000_403), 'ToolsController', - 'In-memory MCP server started successfully' + 'Assistant tools utility process client started successfully' ); } catch (error: any) { // In case of errors we don't want Compass to crash so we @@ -391,20 +263,38 @@ export class ToolsController { 'Error when attempting to start MCP server', { error: error.message } ); + this.portClient?.close(); + this.portClient = null; + this.dbToolDefinitions = []; + this.startPromise = null; } } + private async withStartupTimeout(start: () => Promise): Promise { + const timeout = new Promise((_resolve, reject) => { + const timer = setTimeout(() => { + reject(new Error('Timed out waiting for the assistant tools utility')); + }, ASSISTANT_TOOLS_STARTUP_TIMEOUT_MS); + timer.unref?.(); + }); + return await Promise.race([start(), timeout]); + } + public async stopServer(): Promise { + if (!this.portClient) { + this.logger.log.info( + this.logger.mongoLogId(1_001_000_406), + 'ToolsController', + 'MCP server stop requested. No MCP server running, nothing to stop.' + ); + return; + } + const client = this.portClient; + this.portClient = null; + this.startPromise = null; + this.dbToolDefinitions = []; try { - if (!this.runner.server) { - this.logger.log.info( - this.logger.mongoLogId(1_001_000_406), - 'ToolsController', - 'MCP server stop requested. No MCP server running, nothing to stop.' - ); - return; - } - await this.runner.server.close(); + await client.call('stopServer', undefined, AbortSignal.timeout(10_000)); this.logger.log.info( this.logger.mongoLogId(1_001_000_407), 'ToolsController', @@ -417,8 +307,44 @@ export class ToolsController { 'Error when attempting to close the MCP server', { error: error.message } ); + } finally { + client.close(); } } + + private resolveConnectParams(toolCallId: string): ToolsConnectParams { + const connectionId = this.connectionIdByToolCallId[toolCallId]; + if (!connectionId) { + // sanity check in case we ever get multiple tool calls in parallel + const error = new Error("Can't find connection for tool call"); + this.logger.log.error( + this.logger.mongoLogId(1_001_000_415), + 'ToolsController', + error.message, + { + toolCallId, + } + ); + throw error; + } + + const connectParams = this.context.connections.find( + (connection) => connection.connectionId === connectionId + ); + if (!connectParams) { + // the context could have changed between when the tool was + // created and when it gets executed + const error = new Error('No active connection to execute tool'); + this.logger.log.error( + this.logger.mongoLogId(1_001_000_414), + 'ToolsController', + error.message + ); + throw error; + } + + return connectParams; + } } function getCustomToolDescription(toolBase: { diff --git a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts new file mode 100644 index 00000000000..91c9405faf8 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts @@ -0,0 +1,99 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import { Binary, UUID } from 'bson'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; +import { decodeConnectParams, encodeConnectParams } from './connect-params'; +import type { ConnectParamsWire } from './connect-params'; + +describe('connect params over the port', function () { + let channel: MessageChannel; + + beforeEach(function () { + channel = new MessageChannel(); + }); + + afterEach(function () { + // Open ports keep the event loop alive and would stop the suite exiting + channel.port1.close(); + channel.port2.close(); + }); + + async function overThePort(wire: ConnectParamsWire) { + const received = new Promise((resolve) => { + // worker MessagePorts emit the value itself, unlike the EventTarget + // shape `listen` handles + channel.port2.on('message', (value: ConnectParamsWire) => resolve(value)); + }); + channel.port1.postMessage(wire); + return await received; + } + + it('restores BSON values passed through a MessagePort', async function () { + // Structured cloning keeps the values but drops their prototypes: this is + // the case the encoding exists for, so it is asserted through a real port. + const connectParams = { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27017', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + autoEncryption: { + keyVaultNamespace: 'encryption.__keyVault', + encryptedFieldsMap: { + 'hr.employees': { + fields: [ + { + path: 'taxid', + keyId: new Binary( + Buffer.from('a21ddc6a880643849fdf8ba02a767b5f', 'hex'), + Binary.SUBTYPE_UUID + ), + }, + ], + }, + }, + kmsProviders: { local: { key: new UUID() } }, + }, + }, + } as unknown as ToolsConnectParams; + + const decoded = decodeConnectParams( + await overThePort(encodeConnectParams(connectParams)) + ); + const autoEncryption = ( + decoded.connectOptions as { + autoEncryption: { + encryptedFieldsMap: Record; + kmsProviders: { local: { key: unknown } }; + }; + } + ).autoEncryption; + + const keyId = autoEncryption.encryptedFieldsMap['hr.employees'].fields[0] + .keyId as Binary; + expect(keyId).to.be.instanceOf(Binary); + expect(keyId.buffer).to.deep.equal( + Buffer.from('a21ddc6a880643849fdf8ba02a767b5f', 'hex') + ); + // UUID extends Binary, so the more specific type has to win + expect(autoEncryption.kmsProviders.local.key).to.be.instanceOf(UUID); + }); + + it('leaves non-BSON values alone', async function () { + const connectParams: ToolsConnectParams = { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27017', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + serverSelectionTimeoutMS: 500, + }, + }; + + const decoded = decodeConnectParams( + await overThePort(encodeConnectParams(connectParams)) + ); + + expect(decoded).to.deep.equal(connectParams); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts new file mode 100644 index 00000000000..a528548fa34 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts @@ -0,0 +1,60 @@ +import { Binary, UUID } from 'bson'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; + +const BSON_TYPES = { UUID, Binary } as const; + +/** + * Structured cloning across the utility port keeps BSON values but drops their + * prototypes, so the driver would receive plain objects for e.g. the `keyId` + * of an encrypted field. + */ +export type ConnectParamsWire = { + connectParams: ToolsConnectParams; + bsonValues: Map; +}; + +export function encodeConnectParams( + connectParams: ToolsConnectParams +): ConnectParamsWire { + const bsonValues = new Map(); + collectBsonValues(connectParams, bsonValues, new Set()); + return { connectParams, bsonValues }; +} + +export function decodeConnectParams({ + connectParams, + bsonValues, +}: ConnectParamsWire): ToolsConnectParams { + for (const [value, typeName] of bsonValues) { + const bsonType = BSON_TYPES[typeName as keyof typeof BSON_TYPES]; + if (bsonType && typeof value === 'object' && value !== null) { + Object.setPrototypeOf(value, bsonType.prototype); + } + } + return connectParams; +} + +function collectBsonValues( + value: unknown, + found: Map, + seen: Set +): void { + if (typeof value !== 'object' || value === null || seen.has(value)) { + return; + } + + for (const [name, bsonType] of Object.entries(BSON_TYPES)) { + if (value instanceof bsonType) { + found.set(value, name); + return; + } + } + + seen.add(value); + + for (const entryValue of Array.isArray(value) + ? value + : Object.values(value)) { + collectBsonValues(entryValue, found, seen); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts b/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts new file mode 100644 index 00000000000..b192c4280a2 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts @@ -0,0 +1,28 @@ +import { TransportRunnerBase } from 'mongodb-mcp-server'; +import type { + Server, + TransportRunnerConfig, + UserConfig, +} from 'mongodb-mcp-server'; + +/** + * In-memory MCP runner that doesn't bind to any external transport. + * The server is kept in memory for direct programmatic access. + */ +export class InMemoryRunner extends TransportRunnerBase { + public server?: Server; + public readonly userConfig: UserConfig; + + constructor(config: TransportRunnerConfig) { + super(config); + this.userConfig = config.userConfig; + } + + async start(): Promise { + this.server = await this.createServer(); + } + + async closeTransport(): Promise { + await this.server?.close(); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/messages.ts b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts new file mode 100644 index 00000000000..1ed8adfa5ed --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts @@ -0,0 +1,69 @@ +import type { EventSource } from '@mongodb-js/compass-utils'; + +export type ToolsRpcPort = { + postMessage(message: unknown): void; + close(): void; +} & EventSource; + +export type ToolsRpcMethod = + /** + * Keeps the MCP server's runtime config in sync with Compass preferences. + * Sent by the renderer every time the assistant context changes. It replaces + * the direct `runner.userConfig` mutation `ToolsController.setContext` used + * to do when the server lived in the renderer. + */ + | 'setUserConfig' + /** + * Fetches the read-only database tool definitions: name, description and + * input schema as JSON Schema. Sent once when the connection is established, + * doubling as the handshake; the result is cached renderer-side so + * `getActiveTools()` can stay synchronous. + */ + | 'listDatabaseTools' + /** + * Executes one database tool. Sent by the renderer per AI tool call. + * The utility connects the driver to the given connection, invokes the tool + * with the request's abort signal (identified via the abort-for-id pattern, + * see `rpc-client.ts`), disconnects in `finally`, and resolves with the + * tool result, which is plain data. + */ + | 'performToolCall' + /** + * Closes the MCP server. Sent on provider unmount / window close. The + * utility additionally tears down a host when its port closes, which covers + * a renderer crash that can never deliver this message. + */ + | 'stopServer'; + +export type ToolsRpcRequest = { + kind: 'request'; + id: number; + method: ToolsRpcMethod; + params: unknown; +}; + +export type ToolsRpcResponse = { kind: 'response'; id: number } & ( + { ok: true; result: unknown } | { ok: false; error: string } +); + +export type ToolsRpcAbort = { kind: 'abort'; id: number }; + +export type ToolsRpcMessage = + ToolsRpcRequest | ToolsRpcResponse | ToolsRpcAbort; + +export function isToolsRpcMessage(value: unknown): value is ToolsRpcMessage { + return ( + typeof value === 'object' && + value !== null && + 'kind' in value && + ['request', 'response', 'abort'].includes(value.kind as string) + ); +} + +export function isResponse(value: unknown): value is ToolsRpcResponse { + return ( + typeof value === 'object' && + value !== null && + (value as { kind: unknown }).kind === 'response' + ); +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts b/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts new file mode 100644 index 00000000000..e7e43da0297 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts @@ -0,0 +1,17 @@ +import type { DefaultMetrics, Metrics } from 'mongodb-mcp-server'; + +class NoopDefaultMetrics { + observe(): void { + // no-op + } +} + +export class NoopMetrics implements Metrics { + constructor() {} + get(_key: K): DefaultMetrics[K] { + return new NoopDefaultMetrics() as unknown as DefaultMetrics[K]; + } + getMetrics(): Promise { + return Promise.resolve(''); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts new file mode 100644 index 00000000000..c87116fbfd9 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts @@ -0,0 +1,182 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import sinon from 'sinon'; +import { createToolsRpcClient } from './rpc-client'; +import { serveToolsRequests } from './server'; + +describe('tools rpc', function () { + let channel: MessageChannel; + + beforeEach(function () { + channel = new MessageChannel(); + }); + + afterEach(function () { + channel.port1.close(); + channel.port2.close(); + }); + + it('round-trips a request to a response', async function () { + const client = createToolsRpcClient(channel.port1); + const params = { enableMCPTelemetry: true, maxTimeMS: 5 }; + serveToolsRequests(channel.port2, async (method, sentParams, _signal) => { + expect(method).to.equal('setUserConfig'); + return Promise.resolve(sentParams); + }); + + expect(await client.call('setUserConfig', params)).to.deep.equal(params); + }); + + it('rejects with the error message from the other side', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, () => { + throw new Error('boom'); + }); + + const error = await client + .call('performToolCall', { toolName: 'find' }) + .catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'boom'); + }); + + it('propagates abort to the server-side signal', async function () { + const client = createToolsRpcClient(channel.port1); + const controller = new AbortController(); + let serverSignal: AbortSignal | undefined; + + // resolves only once the server side processed the abort message + const serverAborted = new Promise((resolve) => { + serveToolsRequests(channel.port2, (_method, _params, signal) => { + serverSignal = signal; + return new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => { + reject(signal.reason); + resolve(); + }); + }); + }); + }); + + const pending = client.call( + 'performToolCall', + undefined, + controller.signal + ); + controller.abort(new Error('stop')); + + const error = await pending.catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'stop'); + await serverAborted; + expect(serverSignal?.aborted).to.equal(true); + }); + + it('rejects immediately for an already-aborted signal', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, async () => Promise.resolve('unused')); + + const error = await client + .call('performToolCall', undefined, AbortSignal.abort(new Error('nope'))) + .catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'nope'); + }); + + it('resolves in request order regardless of completion order', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, async (_method, params, _signal) => { + const { delay, name } = params as { delay: number; name: string }; + await new Promise((resolve) => setTimeout(resolve, delay)); + return name; + }); + + const slow = client.call('performToolCall', { delay: 50, name: 'slow' }); + const fast = client.call('performToolCall', { delay: 1, name: 'fast' }); + expect(await Promise.all([slow, fast])).to.deep.equal(['slow', 'fast']); + }); + + it('rejects pending calls when the client is closed', async function () { + const client = createToolsRpcClient(channel.port1); + // no server on the other end: the call stays pending until close() + const pending = client.call('listDatabaseTools'); + client.close(); + + const error = await pending.catch((err: Error) => err); + expect(error) + .to.be.instanceOf(Error) + .with.property('message', 'Tools rpc client closed'); + }); + + it('ignores messages that are not part of the protocol', async function () { + let called = 0; + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, () => { + called += 1; + return Promise.resolve(null); + }); + channel.port1.postMessage({ hello: 'stray' }); + channel.port1.postMessage(null); + channel.port1.postMessage({ kind: 'nonsense' }); + + // Messages arrive in order, so a request that gets answered proves the + // strays were seen and ignored rather than queued behind it + await client.call('listDatabaseTools'); + + expect(called).to.equal(1); + }); + + it('closes the port when the client is closed', function () { + const close = sinon.spy(channel.port1, 'close'); + const client = createToolsRpcClient(channel.port1); + + client.close(); + + expect(close).to.have.been.calledOnce; + close.restore(); + }); + + it('does not send an abort message for a request that already settled', async function () { + const client = createToolsRpcClient(channel.port1); + const controller = new AbortController(); + const postMessage = sinon.spy(channel.port1, 'postMessage'); + serveToolsRequests(channel.port2, () => Promise.resolve('ok')); + + await client.call('performToolCall', undefined, controller.signal); + postMessage.resetHistory(); + + // Abort listeners run synchronously, so no waiting is needed to know + // whether the client still had one attached + controller.abort(new Error('too late')); + + expect(postMessage).to.not.have.been.called; + postMessage.restore(); + }); + + it('aborts in-flight requests when the server is disposed', async function () { + const client = createToolsRpcClient(channel.port1); + let serverSignal: AbortSignal | undefined; + // Resolved by the handler, so the test never has to guess how long a + // message takes to arrive + let requestArrived: () => void; + const arrived = new Promise((resolve) => { + requestArrived = resolve; + }); + const server = serveToolsRequests( + channel.port2, + (_method, _params, signal) => { + serverSignal = signal; + requestArrived(); + return new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => reject(signal.reason)); + }); + } + ); + + void client.call('performToolCall').catch(() => { + // the request is aborted below; the rejection is expected + }); + await arrived; + + server[Symbol.dispose](); + + expect(serverSignal?.aborted).to.equal(true); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts new file mode 100644 index 00000000000..5ae9142c88b --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts @@ -0,0 +1,98 @@ +import { listen } from '@mongodb-js/compass-utils'; +import { isResponse } from './messages'; +import type { + ToolsRpcAbort, + ToolsRpcMethod, + ToolsRpcPort, + ToolsRpcRequest, +} from './messages'; + +/** + * Renderer-side client. `port` is the end of the channel returned by + * `openUtilityPort`. + */ +export function createToolsRpcClient(port: ToolsRpcPort): { + call( + method: ToolsRpcMethod, + params?: unknown, + signal?: AbortSignal + ): Promise; + close(): void; +} { + let nextId = 0; + const pending = new Map< + number, + { + resolve: (value: unknown) => void; + reject: (reason: unknown) => void; + /** Removes the abort listener the call installed, if any */ + stopListeningForAbort?: () => void; + } + >(); + + const settle = ( + id: number, + settleEntry: (entry: { + resolve: (value: unknown) => void; + reject: (reason: unknown) => void; + stopListeningForAbort?: () => void; + }) => void + ): void => { + const entry = pending.get(id); + if (!entry) return; + pending.delete(id); + entry.stopListeningForAbort?.(); + settleEntry(entry); + }; + + const stopListening = listen(port, 'message', (event: { data: unknown }) => { + const message = event.data; + if (!isResponse(message)) return; + + settle(message.id, (entry) => { + if (message.ok) entry.resolve(message.result); + else entry.reject(new Error(message.error)); + }); + }); + + return { + call(method, params, signal) { + const id = nextId++; + return new Promise((resolve, reject) => { + if (signal?.aborted) { + return reject( + signal.reason ?? new Error('The operation was aborted') + ); + } + pending.set(id, { resolve, reject }); + if (signal) { + const stopListeningForAbort = listen(signal, 'abort', () => { + settle(id, (entry) => { + port.postMessage({ kind: 'abort', id } satisfies ToolsRpcAbort); + entry.reject( + signal.reason ?? new Error('The operation was aborted') + ); + }); + }); + pending.get(id)!.stopListeningForAbort = () => + stopListeningForAbort[Symbol.dispose](); + } + port.postMessage({ + kind: 'request', + id, + method, + params, + } satisfies ToolsRpcRequest); + }); + }, + close() { + for (const id of [...pending.keys()]) { + settle(id, (entry) => + entry.reject(new Error('Tools rpc client closed')) + ); + } + stopListening[Symbol.dispose](); + port.close(); + }, + }; +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/server.ts b/packages/compass-generative-ai/src/tools/infrastructure/server.ts new file mode 100644 index 00000000000..ce2b2a253f3 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/server.ts @@ -0,0 +1,77 @@ +import { listen } from '@mongodb-js/compass-utils'; +import { isToolsRpcMessage } from './messages'; +import type { + ToolsRpcMethod, + ToolsRpcPort, + ToolsRpcResponse, +} from './messages'; + +/** + * Utility-side server. `port` is a `MessagePortMain` received through + * `onPort` (already started). Each request gets its own AbortController; + * abort messages for that id abort it. + */ +export function serveToolsRequests( + port: ToolsRpcPort, + handler: ( + method: ToolsRpcMethod, + params: unknown, + signal: AbortSignal + ) => Promise +): Disposable { + const controllers = new Map(); + + const stopListening = listen(port, 'message', (event: { data: unknown }) => { + const message = event.data; + if (!isToolsRpcMessage(message)) return; + + if (message.kind === 'abort') { + controllers.get(message.id)?.abort(); + return; + } + + if (message.kind !== 'request') return; + + const controller = new AbortController(); + + controllers.set(message.id, controller); + + // The handler may throw synchronously; route it through a promise chain + // so a throw becomes a rejection instead of an uncaught error that would + // take the utility process down. + Promise.resolve() + .then(() => handler(message.method, message.params, controller.signal)) + .then((result) => { + port.postMessage({ + kind: 'response', + id: message.id, + ok: true, + result, + } satisfies ToolsRpcResponse); + }) + .catch((error: unknown) => { + port.postMessage({ + kind: 'response', + id: message.id, + ok: false, + error: + error instanceof Error + ? error.message + : String((error as any)?.message ?? error), + } satisfies ToolsRpcResponse); + }) + .finally(() => { + controllers.delete(message.id); + }); + }); + + return { + [Symbol.dispose]() { + stopListening[Symbol.dispose](); + for (const controller of controllers.values()) { + controller.abort(); + } + controllers.clear(); + }, + }; +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts new file mode 100644 index 00000000000..712e02e3f74 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -0,0 +1,102 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import sinon from 'sinon'; +import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; +import type { Logger } from '@mongodb-js/compass-logging'; +import { createToolsRpcClient } from './rpc-client'; +import { encodeConnectParams } from './connect-params'; +import { ToolsHost } from './tools-host'; +import type { ToolDefinitionInfo } from './tools-host'; + +describe('ToolsHost', function () { + // The MCP server starts synchronously enough for the in-memory runner, but + // registering the tools and the JSON Schema conversion give it some slack. + this.timeout(10_000); + + let channel: MessageChannel; + let host: ToolsHost; + let logger: Logger; + + beforeEach(function () { + channel = new MessageChannel(); + logger = createNoopLogger(); + host = new ToolsHost(channel.port2, { + logger, + }); + }); + + afterEach(async function () { + await host.stop(); + channel.port1.close(); + channel.port2.close(); + }); + + it('lists read-only database tools as JSON Schema', async function () { + const client = createToolsRpcClient(channel.port1); + const tools = (await client.call( + 'listDatabaseTools' + )) as ToolDefinitionInfo[]; + + const names = tools.map((t) => t.name).sort(); + expect(names).to.include('find'); + expect(names).to.include('aggregate'); + expect(names).to.not.include('connect'); + for (const tool of tools) { + expect(tool.description).to.be.a('string'); + expect(tool.inputSchema).to.have.property('type', 'object'); + } + }); + + it('returns an error result when the connection fails', async function () { + const client = createToolsRpcClient(channel.port1); + // nothing is listening on this port, so connecting fails and the MCP + // server surfaces that as an error result, not a rejection + const result = (await client.call('performToolCall', { + toolName: 'find', + toolArgs: { database: 'test', collection: 'coll' }, + connectParams: encodeConnectParams({ + connectionId: 'c1', + connectionString: 'mongodb://localhost:27019', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + // Fail fast when nothing is listening: the driver's default server + // selection timeout (30s) would outlive this suite's timeout. + serverSelectionTimeoutMS: 500, + }, + }), + })) as { content: unknown[]; isError?: boolean }; + + expect(result).to.be.an('object'); + expect(result.isError).to.equal(true); + }); + + it('applies setUserConfig to the runner config', async function () { + const client = createToolsRpcClient(channel.port1); + await client.call('setUserConfig', { + enableMCPTelemetry: true, + maxTimeMS: 1000, + }); + expect((host as any).runner.userConfig.telemetry).to.equal('enabled'); + expect((host as any).runner.userConfig.maxTimeMS).to.equal(1000); + }); + + it('releases the server and the connection when stopped', async function () { + const client = createToolsRpcClient(channel.port1); + await client.call('listDatabaseTools'); + const connectionManager = (host as any).connectionManager; + const disconnect = sinon.spy(connectionManager, 'disconnect'); + + await host.stop(); + + expect(disconnect).to.have.been.called; + const settled = await Promise.race([ + client + .call('listDatabaseTools') + .then(() => 'answered') + .catch(() => 'rejected'), + new Promise((resolve) => setTimeout(() => resolve('pending'), 100)), + ]); + expect(settled).to.equal('pending'); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts new file mode 100644 index 00000000000..97a44d3c2d2 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -0,0 +1,192 @@ +import type { Logger } from '@mongodb-js/compass-logging'; +import { z } from 'zod/v4'; +import { Keychain, UserConfigSchema } from 'mongodb-mcp-server'; +import type { Server } from 'mongodb-mcp-server'; +import { createConnectionErrorHandler } from '../../tools-connection-error-handler'; +import { ToolsLogger } from '../../tools-logger'; +import { ToolsConnectionManager } from '../../tools-connection-manager'; +import { removeZodTransforms } from '../../remove-zod-transforms'; +import { READ_ONLY_DATABASE_TOOLS } from '../../available-tools'; +import type { ToolsRpcMethod, ToolsRpcPort } from './messages'; +import type { ConnectParamsWire } from './connect-params'; +import { decodeConnectParams } from './connect-params'; +import { serveToolsRequests } from './server'; +import { InMemoryRunner } from './in-memory-runner'; +import { NoopMetrics } from './noop-metrics'; + +export type ToolDefinitionInfo = { + name: string; + description: string; + inputSchema: Record; +}; + +type ToolsHostConfig = { + logger: Logger; +}; + +/** + * Utility-process side of the assistant database tools. Owns the MCP server, + * its connection manager, and the driver (moved here from ToolsController + * when the server moved out of the renderer). Exposes the protocol methods + * from `./messages` over the port the utility receives for each renderer. + */ +export class ToolsHost { + private logger: Logger; + private readonly runner: InMemoryRunner; + private connectionManager: ToolsConnectionManager; + private readonly disposable: Disposable; + private telemetryAnonymousId = ''; + + constructor(port: ToolsRpcPort, { logger }: ToolsHostConfig) { + this.logger = logger; + const mcpConfig = UserConfigSchema.parse({ + disabledTools: ['connect'], + loggers: ['mcp'], + readOnly: true, + telemetry: 'disabled', + }); + + this.runner = new InMemoryRunner({ + userConfig: mcpConfig, + createConnectionManager: () => Promise.resolve(this.connectionManager), + connectionErrorHandler: createConnectionErrorHandler(), + additionalLoggers: [new ToolsLogger(Keychain.root, logger)], + telemetryProperties: { + hosting_mode: 'compass', + }, + metrics: new NoopMetrics(), + }); + + this.connectionManager = new ToolsConnectionManager({ + logger: this.runner.logger, + getTelemetryAnonymousId: () => this.telemetryAnonymousId, + }); + + this.disposable = serveToolsRequests(port, (method, params, signal) => + this.handleRequest(method, params, signal) + ); + } + + private get server(): Server | undefined { + return this.runner.server; + } + + private async ensureServerStarted(): Promise { + if (!this.runner.server) { + await this.runner.start(); + } + const server = this.runner.server!; + // The MCP server registers its tools lazily, on first use + if (server.tools.length === 0) { + server.registerTools(); + } + return server; + } + + private async handleRequest( + method: ToolsRpcMethod, + params: unknown, + signal: AbortSignal + ): Promise { + switch (method) { + case 'setUserConfig': { + const { enableMCPTelemetry, maxTimeMS, telemetryAnonymousId } = + params as { + enableMCPTelemetry: boolean; + maxTimeMS?: number; + telemetryAnonymousId?: string; + }; + this.runner.userConfig.telemetry = enableMCPTelemetry + ? 'enabled' + : 'disabled'; + this.runner.userConfig.maxTimeMS = maxTimeMS; + if (telemetryAnonymousId !== undefined) { + this.telemetryAnonymousId = telemetryAnonymousId; + } + return; + } + case 'listDatabaseTools': { + await this.ensureServerStarted(); + const readonlyNames = new Set( + READ_ONLY_DATABASE_TOOLS.map((tool) => tool.name) + ); + return (this.server?.tools ?? []) + .filter((toolBase) => readonlyNames.has(toolBase.name)) + .map((toolBase) => ({ + name: toolBase.name, + description: toolBase.description, + inputSchema: z.toJSONSchema( + z.object( + Object.fromEntries( + Object.entries(toolBase.argsShape).map(([key, value]) => [ + key, + removeZodTransforms(value), + ]) + ) + ) + ), + })); + } + case 'performToolCall': { + const { + toolName, + toolArgs, + connectParams: encodedConnectParams, + } = params as { + toolName: string; + toolArgs: unknown; + connectParams: ConnectParamsWire; + }; + const connectParams = decodeConnectParams(encodedConnectParams); + await this.ensureServerStarted(); + const toolBase = this.server?.tools.find((t) => t.name === toolName); + if (!toolBase) { + throw new Error(`Unknown tool: ${toolName}`); + } + try { + await this.connectionManager.connectToCompassConnection( + connectParams + ); + } catch (error: any) { + this.logger.log.error( + this.logger.mongoLogId(1_001_000_410), + 'ToolsHost', + 'Error when attempting to connect to Compass connection before executing tool', + { error: error.message } + ); + throw error; + } + try { + return await toolBase.invoke( + toolArgs as Parameters[0], + { signal } + ); + } finally { + await this.connectionManager.disconnect(); + } + } + case 'stopServer': { + await this.runner.server?.close(); + return; + } + default: + throw new Error(`Unknown method: ${method}`); + } + } + + async stop(): Promise { + // Aborts requests still in flight and stops answering new ones + this.disposable[Symbol.dispose](); + try { + await this.connectionManager.disconnect(); + await this.runner.server?.close(); + } catch (error: any) { + this.logger.log.error( + this.logger.mongoLogId(1_001_000_446), + 'ToolsHost', + 'Error when attempting to stop the MCP server', + { error: error.message } + ); + } + } +} diff --git a/packages/compass-utils/src/utility-process.spec.ts b/packages/compass-utils/src/utility-process.spec.ts new file mode 100644 index 00000000000..28c809cf798 --- /dev/null +++ b/packages/compass-utils/src/utility-process.spec.ts @@ -0,0 +1,56 @@ +import { expect } from 'chai'; +import sinon from 'sinon'; +import { openUtilityPort, utilityPortChannel } from './utility-process'; + +describe('openUtilityPort', function () { + let sandbox: sinon.SinonSandbox; + let originalWindow: unknown; + + beforeEach(function () { + sandbox = sinon.createSandbox(); + originalWindow = (globalThis as any).window; + }); + + afterEach(function () { + if (originalWindow === undefined) { + delete (globalThis as any).window; + } else { + (globalThis as any).window = originalWindow; + } + sandbox.restore(); + }); + + function stubMessageChannel() { + const port1 = { start: sinon.stub(), postMessage: sinon.stub() }; + const port2 = {}; + sandbox.replace( + globalThis, + 'MessageChannel', + sinon.stub().returns({ port1, port2 }) as unknown as typeof MessageChannel + ); + const postMessage = sinon.stub(); + (globalThis as any).window = { postMessage }; + return { port1, port2, postMessage }; + } + + it('hands the other end to the preload on the utility channel', function () { + const { port2, postMessage } = stubMessageChannel(); + + openUtilityPort('ai-assistant'); + + expect(postMessage).to.have.been.calledOnceWith( + { type: utilityPortChannel('ai-assistant') }, + '*', + [port2] + ); + }); + + it('starts the port it returns', function () { + const { port1 } = stubMessageChannel(); + + const port = openUtilityPort('ai-assistant'); + + expect(port).to.equal(port1); + expect(port1.start).to.have.been.calledOnce; + }); +}); diff --git a/packages/compass-utils/src/utility-process.ts b/packages/compass-utils/src/utility-process.ts index 2d6b29f79a4..8398a083a7e 100644 --- a/packages/compass-utils/src/utility-process.ts +++ b/packages/compass-utils/src/utility-process.ts @@ -26,5 +26,6 @@ export function isUtilityPortChannel( export function openUtilityPort(name: string): MessagePort { const { port1, port2 } = new MessageChannel(); window.postMessage({ type: utilityPortChannel(name) }, '*', [port2]); + port1.start(); return port1; } diff --git a/packages/compass-web/src/entrypoint.tsx b/packages/compass-web/src/entrypoint.tsx index 6b536fc58a0..e7e1601e30c 100644 --- a/packages/compass-web/src/entrypoint.tsx +++ b/packages/compass-web/src/entrypoint.tsx @@ -103,6 +103,7 @@ import type { LogFunction, } from './logger-and-telemetry'; import { AtlasCloudAuthServiceProvider } from './atlas-auth-service'; +import { useToolsLoopback } from './tools-loopback'; import { AtlasCloudConnectionStorageProvider } from './connection-storage'; import { CompassAssistantDrawerWithConnections } from './compass-assistant-drawer'; import { Link, setMultiplexLink } from './multiplex-link'; @@ -561,6 +562,7 @@ const CompassWebWithPreferences = ({ onTrack, preferences, }); + const openToolsPort = useToolsLoopback(); const { initialWorkspaceTabs, @@ -586,7 +588,7 @@ const CompassWebWithPreferences = ({ orgId={orgId} projectId={projectId} > - + MessagePort; + hostPort: MessagePort; +} { + const channel = new MessageChannel(); + channel.port1.start(); + channel.port2.start(); + return { openPort: () => channel.port1, hostPort: channel.port2 }; +} + +/** + * The browser has no utility process, so the assistant's tools host runs in + * the page instead: the same host and the same protocol as the desktop build, + * with the driver polyfills compass-web already provides, on the other end of + * a plain MessageChannel. + */ +export function useToolsLoopback(): () => MessagePort { + const logger = useLogger('TOOLS-CONTROLLER'); + + const { openPort, host } = useMemo(() => { + const { openPort, hostPort } = createToolsLoopbackChannel(); + return { + openPort, + host: new ToolsHost(hostPort, { logger }), + }; + }, [logger]); + + useEffect(() => { + return () => { + // Nothing else releases the in-page host: on desktop the utility does + // this when the port closes. + void host.stop(); + }; + }, [host]); + + return openPort; +} diff --git a/packages/compass/src/main/application.ts b/packages/compass/src/main/application.ts index 6281b22aced..81d087da11c 100644 --- a/packages/compass/src/main/application.ts +++ b/packages/compass/src/main/application.ts @@ -42,7 +42,7 @@ import { handleSquirrelWindowsStartup } from './squirrel-startup'; const { debug, log, mongoLogId } = createLogger('COMPASS-MAIN'); const track = createIpcTrack(); -const UTILITIES = ['embedded-shell']; +const UTILITIES = ['embedded-shell', 'ai-assistant']; type ExitHandler = () => Promise; type CompassApplicationMode = 'CLI' | 'GUI'; diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts new file mode 100644 index 00000000000..c8af71061c1 --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -0,0 +1,48 @@ +import process from 'node:process'; + +import type { ParentPort } from 'electron'; +import { listen } from '@mongodb-js/compass-utils'; +import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; +import { createUtilityLogger } from '../logging.mts'; +import { onPort } from '../ipc.mts'; + +export const name = 'ai-assistant'; + +export function main(parentPort: ParentPort): Disposable { + using ds = new DisposableStack(); + + ds.use( + listen(process, 'unhandledRejection', (reason) => { + throw reason; + }) + ); + + const logger = createUtilityLogger(parentPort, name); + + ds.use( + onPort(parentPort, (port) => { + // One host per renderer: it owns the MCP server and the driver + // connections for that renderer's tool calls. When the renderer goes + // away (crash included), its port closes and the host is torn down. + // Not tracked in `ds`: ports arrive after main() returned and the + // stack moved out; the listener dies with its closed port. + const host = new ToolsHost(port, { logger }); + listen(port, 'close', () => { + void host.stop().catch((error) => { + logger.log.error( + logger.mongoLogId(1_001_000_447), + 'ai-assistant', + 'Error when stopping the tools host', + { error: error instanceof Error ? error.message : String(error) } + ); + }); + }); + }) + ); + + return ds.move(); +} + +if (import.meta.main) { + main(process.parentPort); +} diff --git a/packages/compass/src/utilities/ai-assistant/index.spec.mts b/packages/compass/src/utilities/ai-assistant/index.spec.mts new file mode 100644 index 00000000000..3e72634e390 --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.spec.mts @@ -0,0 +1,91 @@ +import process from 'node:process'; +import { EventEmitter } from 'node:events'; +import { expect } from 'chai'; +import type { MessagePortMain, ParentPort } from 'electron'; +import { main } from './index.mts'; + +function createFakePort(): MessagePortMain & { sent: unknown[] } { + const sent: unknown[] = []; + return Object.assign(new EventEmitter(), { + sent, + postMessage: (message: unknown) => sent.push(message), + start: () => {}, + }) as unknown as MessagePortMain & { sent: unknown[] }; +} + +describe('ai-assistant utility', function () { + let parentPort: ParentPort; + let utility: Disposable; + let sentToMain: { channel: string; data: { line: string } }[]; + + beforeEach(function () { + sentToMain = []; + parentPort = Object.assign(new EventEmitter(), { + postMessage: (message: (typeof sentToMain)[number]) => + sentToMain.push(message), + }); + utility = main(parentPort); + }); + + afterEach(function () { + utility[Symbol.dispose](); + }); + + it('logs to main through parentPort', function () { + expect(sentToMain).to.have.lengthOf(1); + expect(sentToMain[0].channel).to.equal('compass:log'); + const entry = JSON.parse(sentToMain[0].data.line); + expect(entry).to.include({ + s: 'I', + c: 'AI-ASSISTANT', + id: 1_001_000_442, + ctx: 'Utility', + msg: 'Started', + }); + expect(entry.attr).to.include({ pid: process.pid, ppid: process.ppid }); + expect(entry.attr.startupMs).to.be.a('number'); + }); + + it('serves rpc requests arriving on a handed-over port', async function () { + this.timeout(10_000); + const port = createFakePort(); + parentPort.emit('message', { ports: [port] }); + + port.emit('message', { + data: { + kind: 'request', + id: 1, + method: 'listDatabaseTools', + params: null, + }, + }); + + const deadline = Date.now() + 9_000; + while (port.sent.length === 0 && Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + expect(port.sent).to.have.lengthOf(1); + const response = port.sent[0] as { + kind: string; + id: number; + ok: boolean; + result: { name: string }[]; + }; + expect(response).to.include({ kind: 'response', id: 1, ok: true }); + expect(response.result.map((tool) => tool.name)).to.include('find'); + }); + + it('stops serving requests when the port closes', function () { + const port = createFakePort(); + parentPort.emit('message', { ports: [port] }); + expect(port.listenerCount('message')).to.be.greaterThan(0); + + port.emit('close'); + + // The host releases its listener as the port closes, so nothing can + // answer requests for this renderer any more + expect(port.listenerCount('message')).to.equal(0); + expect(port.sent).to.have.lengthOf(0); + }); +}); diff --git a/packages/compass/webpack.config.js b/packages/compass/webpack.config.js index 74ade315c44..8edcbb12c81 100644 --- a/packages/compass/webpack.config.js +++ b/packages/compass/webpack.config.js @@ -63,6 +63,11 @@ module.exports = (_env, args) => { const utilitiesConfig = withoutStartElectron( createElectronMainConfig({ ...opts, + // Utilities are never coverage instrumented: istanbul's preamble calls + // `new Function('return this')`, and utility processes are forked with + // NODE_OPTIONS forbidding code generation from strings (application.ts), + // so an instrumented utility crashes on load. + coverage: '', entry: utilityEntries, outputFilename: '[name].mjs', }) @@ -200,13 +205,34 @@ module.exports = (_env, args) => { 'web-worker', ].map((name) => [name, `node-commonjs ${name}`]) ), - plugins: [ - new webpack.EnvironmentPlugin(hadronEnvConfig), - // Main forks one file per utility - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: Object.keys(utilityEntries).length, - }), - ], + resolve: { + alias: { + // Same as the renderer: Atlas Local is a native dependency the app + // does not ship for this flow, and bundling it only adds megabytes + '@mongodb-js/atlas-local': false, + }, + }, + // Utilities share the heavy Node-side dependencies (the driver, the MCP + // server, their transitive graph) instead of every utility bundling its + // own copy. + optimization: { + splitChunks: { + chunks: 'all', + cacheGroups: { + // Only the sharing rule: everything else stays in the utility that + // uses it, so this does not fragment the output + default: false, + defaultVendors: false, + utilitiesVendor: { + name: 'utilities-vendor', + chunks: 'all', + minChunks: 2, + reuseExistingChunk: true, + }, + }, + }, + }, + plugins: [new webpack.EnvironmentPlugin(hadronEnvConfig)], }), merge(preloadConfig, { name: 'preload',