From 8bb00cc1d3369da6b1d1c841e494b377dd8689f0 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 21 Oct 2026 11:29:29 +0100 Subject: [PATCH 01/16] feat: create utility --- packages/compass-generative-ai/package.json | 6 +- .../src/tools/infrastructure/messages.ts | 68 ++++++ .../tools/infrastructure/rpc-client.spec.ts | 120 ++++++++++ .../src/tools/infrastructure/rpc-client.ts | 74 ++++++ .../src/tools/infrastructure/server.ts | 67 ++++++ .../tools/infrastructure/tools-host.spec.ts | 75 ++++++ .../src/tools/infrastructure/tools-host.ts | 226 ++++++++++++++++++ packages/compass/src/main/application.ts | 2 +- .../src/utilities/ai-assistant/index.mts | 41 ++++ .../src/utilities/ai-assistant/index.spec.mts | 99 ++++++++ 10 files changed, 775 insertions(+), 3 deletions(-) create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/messages.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/server.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts create mode 100644 packages/compass/src/utilities/ai-assistant/index.mts create mode 100644 packages/compass/src/utilities/ai-assistant/index.spec.mts diff --git a/packages/compass-generative-ai/package.json b/packages/compass-generative-ai/package.json index 463f615e621..2f6d6cbd2ba 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": { 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..ee7950f5642 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts @@ -0,0 +1,68 @@ +import type { EventSource } from '@mongodb-js/compass-utils'; + +export type ToolsRpcPort = { + postMessage(message: unknown): 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/rpc-client.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts new file mode 100644 index 00000000000..ad180142818 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts @@ -0,0 +1,120 @@ +import { expect } from 'chai'; +import { MessageChannel } from 'worker_threads'; +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; + serveToolsRequests(channel.port2, async () => { + called += 1; + return Promise.resolve(null); + }); + channel.port1.postMessage({ hello: 'stray' }); + channel.port1.postMessage(null); + channel.port1.postMessage({ kind: 'nonsense' }); + // let stray messages arrive + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(called).to.equal(0); + }); +}); 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..7a1960b6634 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts @@ -0,0 +1,74 @@ +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 } + >(); + + const stopListening = listen(port, 'message', (event: { data: unknown }) => { + const message = event.data; + if (!isResponse(message)) return; + + const entry = pending.get(message.id); + if (!entry) return; + + pending.delete(message.id); + + 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) { + listen(signal, 'abort', () => { + pending.delete(id); + port.postMessage({ kind: 'abort', id } satisfies ToolsRpcAbort); + reject(signal.reason ?? new Error('The operation was aborted')); + }); + } + port.postMessage({ + kind: 'request', + id, + method, + params, + } satisfies ToolsRpcRequest); + }); + }, + close() { + for (const { reject } of pending.values()) { + reject(new Error('Tools rpc client closed')); + } + pending.clear(); + stopListening[Symbol.dispose](); + }, + }; +} 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..b20ea88a844 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/server.ts @@ -0,0 +1,67 @@ +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(); + + return 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); + }); + }); +} 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..71f28961bb7 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -0,0 +1,75 @@ +import { expect } from 'chai'; +import { MessageChannel } from 'worker_threads'; +import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; +import type { Logger } from '@mongodb-js/compass-logging'; +import { createToolsRpcClient } from './rpc-client'; +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(function () { + 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: { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27019', + connectOptions: {}, + }, + })) 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); + }); +}); 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..6c72ce8a84a --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -0,0 +1,226 @@ +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 { serveToolsRequests } from './server'; +import type { ToolsRpcMethod, ToolsRpcPort } from './messages'; +import { createConnectionErrorHandler } from '../../tools-connection-error-handler'; +import { ToolsLogger } from '../../tools-logger'; +import { ToolsConnectionManager } from '../../tools-connection-manager'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; +import { removeZodTransforms } from '../../remove-zod-transforms'; +import { READ_ONLY_DATABASE_TOOLS } from '../../available-tools'; + +export type ToolDefinitionInfo = { + name: string; + description: string; + inputSchema: Record; +}; + +type ToolsHostConfig = { + logger: Logger; +}; + +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(); + } +} + +/** + * 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; + // The utility has no preferences of its own: the renderer delivers the + // telemetry anonymous id (and the rest of the runtime config) through + // setUserConfig. + private telemetryAnonymousId = ''; + + constructor(port: ToolsRpcPort, { logger }: ToolsHostConfig) { + this.logger = logger; + 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 the renderer sends setUserConfig whenever + // the context changes. + 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 } = params as { + toolName: string; + toolArgs: unknown; + connectParams: ToolsConnectParams; + }; + 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}`); + } + } + + stop(): void { + this.disposable[Symbol.dispose](); + } +} diff --git a/packages/compass/src/main/application.ts b/packages/compass/src/main/application.ts index 8835b9d46d5..e2e75c98cc4 100644 --- a/packages/compass/src/main/application.ts +++ b/packages/compass/src/main/application.ts @@ -42,7 +42,7 @@ import { utilityPortChannel } from '@mongodb-js/compass-utils'; 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..f7f1c49a5ad --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -0,0 +1,41 @@ +import process from 'node:process'; + +import type { ParentPort } from 'electron'; +import { listen } from '@mongodb-js/compass-utils'; +import { createUtilityLogger } from '../logging.mts'; +import { onPort } from '../ipc.mts'; +import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; + +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', () => { + host.stop(); + }); + }) + ); + + 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..d0ffdbf9dff --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.spec.mts @@ -0,0 +1,99 @@ +import { expect } from 'chai'; +import process from 'node:process'; +import { EventEmitter } from 'events'; +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', async function () { + this.timeout(10_000); + const port = createFakePort(); + parentPort.emit('message', { ports: [port] }); + // let the host attach its listeners + await new Promise((resolve) => setTimeout(resolve, 0)); + + port.emit('close'); + port.emit('message', { + data: { + kind: 'request', + id: 1, + method: 'listDatabaseTools', + params: null, + }, + }); + await new Promise((resolve) => setTimeout(resolve, 10)); + + expect(port.sent).to.have.lengthOf(0); + }); +}); From c7e083b4ecfa2772a5a125ac698bc209639508d6 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Tue, 27 Oct 2026 11:31:09 +0000 Subject: [PATCH 02/16] refactor(generative-ai): proxy assistant database tools to the utility process COMPASS-11269 --- .../src/tools-controller.spec.ts | 186 +++++----- .../src/tools-controller.ts | 319 ++++++------------ 2 files changed, 192 insertions(+), 313 deletions(-) diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index 7edff1ac4f8..dd8eacf99d6 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -1,12 +1,13 @@ import sinon from 'sinon'; import { expect } from 'chai'; +import { MessageChannel } from 'worker_threads'; import { ToolsController } from './tools-controller'; import type { ToolGroup } from './tools-controller'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider'; import type { Logger } from '@mongodb-js/compass-logging'; import type { ToolsConnectParams } from './tools-connection-manager'; -import { READ_ONLY_DATABASE_TOOLS } from './available-tools'; +import { ToolsHost } from './tools/infrastructure/tools-host'; import { createSandboxFromDefaultPreferences, type PreferencesAccess, @@ -14,6 +15,10 @@ import { import type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; 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,51 @@ 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[] = []; + + 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) { + host.stop(); + } + for (const channel of openChannels) { + channel.port1.close(); + channel.port2.close(); + } + openChannels.length = 0; + openHosts.length = 0; sandbox.restore(); }); @@ -54,10 +85,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 +271,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 +296,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 +308,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 +425,45 @@ describe('ToolsController', function () { expect(result.query).to.equal('new query'); }); - it('syncs telemetry setting with runner userConfig', function () { + 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: [], }); - expect((toolsController as any).runner.userConfig.telemetry).to.equal( - 'enabled' - ); + // give the rpc call a moment to land + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(runner.userConfig.telemetry).to.equal('enabled'); toolsController.setContext({ enableMCPTelemetry: false, connections: [], }); - expect((toolsController as any).runner.userConfig.telemetry).to.equal( - 'disabled' - ); + await new Promise((resolve) => setTimeout(resolve, 0)); + 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 new Promise((resolve) => setTimeout(resolve, 0)); + 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 new Promise((resolve) => setTimeout(resolve, 0)); + expect(runner.userConfig.maxTimeMS).to.equal(undefined); }); }); @@ -508,22 +500,21 @@ 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('logs when server starts successfully', async function () { @@ -540,21 +531,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 +553,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 +560,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 +579,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 +600,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 bbedc251b2f..8f48c788b21 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -1,25 +1,12 @@ -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 { createConnectionErrorHandler } from './tools-connection-error-handler'; -import { ToolsLogger } from './tools-logger'; -import { ToolsConnectionManager } from './tools-connection-manager'; +import { openUtilityPort } from '@mongodb-js/compass-utils'; +import { createToolsRpcClient } from './tools/infrastructure/rpc-client'; +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 type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; import type { PreferencesAccess } from 'compass-preferences-model'; import { getAtlasConfigForPreset } from '@mongodb-js/atlas-service/provider'; @@ -42,44 +29,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 +37,8 @@ type ToolsControllerConfig = { enableMCPTelemetry: boolean; maxTimeMS?: number; atlasAdminApi: AtlasAdminApiService; + // Injected for tests + openPort?: () => ToolsRpcPort; }; export class ToolsController { @@ -95,65 +46,36 @@ 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; + private initialConfig: { + enableMCPTelemetry: boolean; + maxTimeMS?: number; + }; + private portClient: ReturnType | 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.initialConfig = { enableMCPTelemetry, maxTimeMS }; } setActiveTools(toolGroups: Set): void { @@ -201,91 +123,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, + }, + 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 +190,14 @@ 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; + // Keep the MCP server's runtime config in the utility in sync with + // Compass preferences; it can no longer be mutated directly from here. + void this.portClient?.call('setUserConfig', { + enableMCPTelemetry: context.enableMCPTelemetry, + maxTimeMS: context.maxTimeMS, + telemetryAnonymousId: this.getTelemetryAnonymousId(), + }); } setConnectionIdForToolCall({ @@ -353,13 +210,8 @@ 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 async startServer(): Promise { - if (this.runner.server) { + if (this.portClient) { return; } @@ -367,20 +219,27 @@ export class ToolsController { 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()); + // Deliver the runtime config before anything else can run in the + // utility: telemetry is best-effort disabled from the start. + await this.portClient.call('setUserConfig', { + ...this.initialConfig, + telemetryAnonymousId: this.getTelemetryAnonymousId(), + }); + // Handshake doubles as the one-time schema fetch: the db tool + // definitions are static for the life of the server, so + // getActiveTools can stay synchronous for its callers. + 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 +250,26 @@ export class ToolsController { 'Error when attempting to start MCP server', { error: error.message } ); + this.portClient?.close(); + this.portClient = null; + this.dbToolDefinitions = []; } } 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.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'); this.logger.log.info( this.logger.mongoLogId(1_001_000_407), 'ToolsController', @@ -417,8 +282,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: { From 6b1dc6fd03134edae42fb799e11a3472795d83c9 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Tue, 27 Oct 2026 14:20:16 +0000 Subject: [PATCH 03/16] start main port --- .../compass-utils/src/utility-process.spec.ts | 56 +++++++++++++++++++ packages/compass-utils/src/utility-process.ts | 1 + 2 files changed, 57 insertions(+) create mode 100644 packages/compass-utils/src/utility-process.spec.ts 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; } From 90040d0bab8f8ace08ade9912d8a52e22ea80a6c Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Tue, 27 Oct 2026 14:34:09 +0000 Subject: [PATCH 04/16] fix import --- packages/compass-generative-ai/src/tools-controller.spec.ts | 2 +- .../src/tools/infrastructure/rpc-client.spec.ts | 2 +- .../src/tools/infrastructure/tools-host.spec.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index 599bf39beb1..b4accb4be13 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -1,6 +1,6 @@ import sinon from 'sinon'; import { expect } from 'chai'; -import { MessageChannel } from 'worker_threads'; +import { MessageChannel } from 'node:worker_threads'; import { ToolsController } from './tools-controller'; import type { ToolGroup } from './tools-controller'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; 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 index ad180142818..ba26475362f 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { MessageChannel } from 'worker_threads'; +import { MessageChannel } from 'node:worker_threads'; import { createToolsRpcClient } from './rpc-client'; import { serveToolsRequests } from './server'; 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 index 71f28961bb7..9e76cd2eb08 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { MessageChannel } from 'worker_threads'; +import { MessageChannel } from 'node:worker_threads'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import type { Logger } from '@mongodb-js/compass-logging'; import { createToolsRpcClient } from './rpc-client'; From 80914d4d62c399389553d21176799834144e76b8 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Tue, 27 Oct 2026 15:31:32 +0000 Subject: [PATCH 05/16] address comments and add more tests --- .../compass-assistant/test/tool-calls.eval.ts | 9 +++- .../src/tools-controller.spec.ts | 29 +++++++++- .../src/tools-controller.ts | 44 +++++++++------ .../src/tools/infrastructure/messages.ts | 1 + .../tools/infrastructure/rpc-client.spec.ts | 50 +++++++++++++++++ .../src/tools/infrastructure/rpc-client.ts | 54 +++++++++++++------ .../src/tools/infrastructure/server.ts | 12 ++++- .../tools/infrastructure/tools-host.spec.ts | 26 +++++++-- .../src/tools/infrastructure/tools-host.ts | 14 ++++- .../src/utilities/ai-assistant/index.mts | 9 +++- 10 files changed, 210 insertions(+), 38 deletions(-) diff --git a/packages/compass-assistant/test/tool-calls.eval.ts b/packages/compass-assistant/test/tool-calls.eval.ts index b5f02cf47dc..05924730e91 100644 --- a/packages/compass-assistant/test/tool-calls.eval.ts +++ b/packages/compass-assistant/test/tool-calls.eval.ts @@ -12,10 +12,12 @@ import { createOpenAI } from '@ai-sdk/openai'; import { streamText } from 'ai'; import type { ToolSet, ToolCallPart } from 'ai'; import { MongoClient } from 'mongodb'; +import { MessageChannel } from 'node:worker_threads'; 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,9 @@ const DEFAULT_EVAL_TASK_CONFIG: EvalTaskConfig = { const logger = createNoopLogger('EVAL-TOOLS-CONTROLLER'); +const evalChannel = new MessageChannel(); +new ToolsHost(evalChannel.port2, { logger }); + const toolsController = new ToolsController({ logger, getTelemetryAnonymousId: () => 'eval-anonymous-id', @@ -72,6 +77,7 @@ const toolsController = new ToolsController({ enableMCPTelemetry: false, preferences: defaultPreferencesInstance, atlasAdminApi: {} as AtlasAdminApiService, + openPort: () => evalChannel.port1, }); let toolsInitialized = false; @@ -83,7 +89,8 @@ let toolsInitialized = false; async function ensureToolsInitialized(): Promise { if (toolsInitialized) return; await toolsController.startServer(); - if (!toolsController.server) { + toolsController.setActiveTools(new Set(['db-read'])); + if (!toolsController.getActiveTools()['find']) { throw new Error('Failed to start ToolsController server for eval tools'); } toolsInitialized = true; diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index b4accb4be13..920dd4f2a4f 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -65,7 +65,7 @@ describe('ToolsController', function () { afterEach(async function () { await toolsController.stopServer(); for (const host of openHosts) { - host.stop(); + await host.stop(); } for (const channel of openChannels) { channel.port1.close(); @@ -517,6 +517,33 @@ describe('ToolsController', function () { 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 () { const logStub = sandbox.stub(logger.log, 'info'); diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 0e2677a33ca..660c74d6adc 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -18,6 +18,8 @@ import { export type ToolGroup = 'querybar' | 'aggregation-builder' | 'db-read'; +const ASSISTANT_TOOLS_STARTUP_TIMEOUT_MS = 10_000; + type CompassContext = { enableMCPTelemetry: boolean; maxTimeMS?: number; @@ -57,6 +59,7 @@ export class ToolsController { maxTimeMS?: number; }; private portClient: ReturnType | null = null; + private startPromise: Promise | null = null; private dbToolDefinitions: ToolDefinitionInfo[] = []; constructor({ @@ -210,11 +213,12 @@ export class ToolsController { this.connectionIdByToolCallId[toolCallId] = connectionId; } - public async startServer(): Promise { - if (this.portClient) { - return; - } + public startServer(): Promise { + this.startPromise ??= this.startServerOnce(); + return this.startPromise; + } + private async startServerOnce(): Promise { try { this.logger.log.info( this.logger.mongoLogId(1_001_000_402), @@ -223,18 +227,16 @@ export class ToolsController { ); this.portClient = createToolsRpcClient(this.openPort()); - // Deliver the runtime config before anything else can run in the - // utility: telemetry is best-effort disabled from the start. - await this.portClient.call('setUserConfig', { - ...this.initialConfig, - telemetryAnonymousId: this.getTelemetryAnonymousId(), + await this.withStartupTimeout(async () => { + await this.portClient!.call('setUserConfig', { + ...this.initialConfig, + telemetryAnonymousId: this.getTelemetryAnonymousId(), + }); + + this.dbToolDefinitions = (await this.portClient!.call( + 'listDatabaseTools' + )) as ToolDefinitionInfo[]; }); - // Handshake doubles as the one-time schema fetch: the db tool - // definitions are static for the life of the server, so - // getActiveTools can stay synchronous for its callers. - this.dbToolDefinitions = (await this.portClient.call( - 'listDatabaseTools' - )) as ToolDefinitionInfo[]; this.logger.log.info( this.logger.mongoLogId(1_001_000_403), @@ -253,9 +255,20 @@ export class ToolsController { 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( @@ -267,6 +280,7 @@ export class ToolsController { } const client = this.portClient; this.portClient = null; + this.startPromise = null; this.dbToolDefinitions = []; try { await client.call('stopServer'); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/messages.ts b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts index ee7950f5642..1ed8adfa5ed 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/messages.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts @@ -2,6 +2,7 @@ import type { EventSource } from '@mongodb-js/compass-utils'; export type ToolsRpcPort = { postMessage(message: unknown): void; + close(): void; } & EventSource; export type ToolsRpcMethod = 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 index ba26475362f..6ac32c0b8ff 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import sinon from 'sinon'; import { MessageChannel } from 'node:worker_threads'; import { createToolsRpcClient } from './rpc-client'; import { serveToolsRequests } from './server'; @@ -117,4 +118,53 @@ describe('tools rpc', function () { await new Promise((resolve) => setTimeout(resolve, 0)); expect(called).to.equal(0); }); + + 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(); + + controller.abort(new Error('too late')); + await new Promise((resolve) => setTimeout(resolve, 0)); + + 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; + const server = serveToolsRequests( + channel.port2, + (_method, _params, signal) => { + serverSignal = signal; + 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 new Promise((resolve) => setTimeout(resolve, 0)); + + 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 index 7a1960b6634..5ae9142c88b 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts @@ -22,20 +22,37 @@ export function createToolsRpcClient(port: ToolsRpcPort): { let nextId = 0; const pending = new Map< number, - { resolve: (value: unknown) => void; reject: (reason: unknown) => void } + { + 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; - const entry = pending.get(message.id); - if (!entry) return; - - pending.delete(message.id); - - if (message.ok) entry.resolve(message.result); - else entry.reject(new Error(message.error)); + settle(message.id, (entry) => { + if (message.ok) entry.resolve(message.result); + else entry.reject(new Error(message.error)); + }); }); return { @@ -49,11 +66,16 @@ export function createToolsRpcClient(port: ToolsRpcPort): { } pending.set(id, { resolve, reject }); if (signal) { - listen(signal, 'abort', () => { - pending.delete(id); - port.postMessage({ kind: 'abort', id } satisfies ToolsRpcAbort); - reject(signal.reason ?? new Error('The operation was aborted')); + 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', @@ -64,11 +86,13 @@ export function createToolsRpcClient(port: ToolsRpcPort): { }); }, close() { - for (const { reject } of pending.values()) { - reject(new Error('Tools rpc client closed')); + for (const id of [...pending.keys()]) { + settle(id, (entry) => + entry.reject(new Error('Tools rpc client closed')) + ); } - pending.clear(); 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 index b20ea88a844..ce2b2a253f3 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/server.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/server.ts @@ -21,7 +21,7 @@ export function serveToolsRequests( ): Disposable { const controllers = new Map(); - return listen(port, 'message', (event: { data: unknown }) => { + const stopListening = listen(port, 'message', (event: { data: unknown }) => { const message = event.data; if (!isToolsRpcMessage(message)) return; @@ -64,4 +64,14 @@ export function serveToolsRequests( 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 index 9e76cd2eb08..92dbac99fda 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import sinon from 'sinon'; import { MessageChannel } from 'node:worker_threads'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import type { Logger } from '@mongodb-js/compass-logging'; @@ -23,8 +24,8 @@ describe('ToolsHost', function () { }); }); - afterEach(function () { - host.stop(); + afterEach(async function () { + await host.stop(); channel.port1.close(); channel.port2.close(); }); @@ -55,7 +56,7 @@ describe('ToolsHost', function () { connectParams: { connectionId: 'c1', connectionString: 'mongodb://localhost:27019', - connectOptions: {}, + connectOptions: { serverSelectionTimeoutMS: 500 }, }, })) as { content: unknown[]; isError?: boolean }; @@ -72,4 +73,23 @@ describe('ToolsHost', function () { 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 index 6c72ce8a84a..0fc531b6b4a 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -220,7 +220,19 @@ export class ToolsHost { } } - stop(): void { + 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/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts index f7f1c49a5ad..509faa31f2e 100644 --- a/packages/compass/src/utilities/ai-assistant/index.mts +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -28,7 +28,14 @@ export function main(parentPort: ParentPort): Disposable { // stack moved out; the listener dies with its closed port. const host = new ToolsHost(port, { logger }); listen(port, 'close', () => { - host.stop(); + 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) } + ); + }); }); }) ); From 52eb18586e486b1257a2f59c7a33badbf370afbe Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Tue, 27 Oct 2026 16:30:52 +0000 Subject: [PATCH 06/16] copilot comments --- .../src/tools-controller.spec.ts | 17 ++++ .../src/tools-controller.ts | 18 +++- .../infrastructure/connect-params.spec.ts | 87 +++++++++++++++++++ .../tools/infrastructure/connect-params.ts | 17 ++++ .../tools/infrastructure/tools-host.spec.ts | 13 ++- .../src/tools/infrastructure/tools-host.ts | 11 ++- 6 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index 920dd4f2a4f..3f03f061504 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -425,6 +425,23 @@ describe('ToolsController', function () { expect(result.query).to.equal('new query'); }); + 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: [], + }); + + await toolsController.startServer(); + const runner = (openHosts[0] as any).runner; + await new Promise((resolve) => setTimeout(resolve, 0)); + + 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; diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 660c74d6adc..a370d99581e 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -4,6 +4,7 @@ import type { Logger } from '@mongodb-js/compass-logging'; import z from 'zod/v4'; import { openUtilityPort } from '@mongodb-js/compass-utils'; 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'; @@ -54,7 +55,8 @@ export class ToolsController { private readonly track: TrackFunction; private readonly getTelemetryAnonymousId: () => string; private readonly openPort: () => ToolsRpcPort; - private initialConfig: { + /** Latest config known to the renderer, sent when the utility starts */ + private currentConfig: { enableMCPTelemetry: boolean; maxTimeMS?: number; }; @@ -78,7 +80,7 @@ export class ToolsController { this.preferences = preferences; this.track = track; this.openPort = openPort ?? (() => openUtilityPort('ai-assistant')); - this.initialConfig = { enableMCPTelemetry, maxTimeMS }; + this.currentConfig = { enableMCPTelemetry, maxTimeMS }; } setActiveTools(toolGroups: Set): void { @@ -140,7 +142,9 @@ export class ToolsController { { toolName: definition.name, toolArgs: args, - connectParams, + // Encoded: connection options can hold BSON values that + // structured cloning would flatten (see connect-params.ts) + connectParams: encodeConnectParams(connectParams), }, options.abortSignal ); @@ -194,6 +198,12 @@ export class ToolsController { setContext(context: ToolsContext): void { this.context = context; + // The utility may not be started yet: remember the latest config so + // startup sends this rather than the values the controller was built with. + this.currentConfig = { + enableMCPTelemetry: context.enableMCPTelemetry, + maxTimeMS: context.maxTimeMS, + }; // Keep the MCP server's runtime config in the utility in sync with // Compass preferences; it can no longer be mutated directly from here. void this.portClient?.call('setUserConfig', { @@ -229,7 +239,7 @@ export class ToolsController { this.portClient = createToolsRpcClient(this.openPort()); await this.withStartupTimeout(async () => { await this.portClient!.call('setUserConfig', { - ...this.initialConfig, + ...this.currentConfig, telemetryAnonymousId: this.getTelemetryAnonymousId(), }); 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..d43ed9a4492 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts @@ -0,0 +1,87 @@ +import { expect } from 'chai'; +import { Binary, UUID } from 'bson'; +import { MessageChannel } from 'node:worker_threads'; +import { decodeConnectParams, encodeConnectParams } from './connect-params'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; + +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(); + }); + + const connectParamsWithBson = (): ToolsConnectParams => ({ + 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 + ), + bsonType: 'string', + }, + ], + }, + }, + kmsProviders: { local: { key: new UUID().toBinary() } }, + }, + }, + }); + + it('keeps BSON values intact when passed through a MessagePort', async function () { + // Structured cloning drops prototypes: this is the case the encoding + // exists for, so it is asserted through a real port, not just the helpers. + const received = new Promise((resolve) => { + // worker MessagePorts emit the value itself, unlike the EventTarget + // shape `listen` handles + channel.port2.on('message', (value: string) => resolve(value)); + }); + channel.port1.postMessage(encodeConnectParams(connectParamsWithBson())); + + const decoded = decodeConnectParams(await received); + const { autoEncryption } = decoded.connectOptions as { + autoEncryption?: { + encryptedFieldsMap?: Record; + }; + }; + const keyId = + autoEncryption?.encryptedFieldsMap?.['hr.employees'].fields[0].keyId; + + expect(keyId).to.be.instanceOf(Binary); + expect((keyId as Binary).buffer).to.deep.equal( + Buffer.from('a21ddc6a880643849fdf8ba02a767b5f', 'hex') + ); + }); + + it('round-trips plain options unchanged', function () { + const connectParams: ToolsConnectParams = { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27017', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + serverSelectionTimeoutMS: 500, + }, + }; + + expect( + decodeConnectParams(encodeConnectParams(connectParams)) + ).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..00d873857c0 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts @@ -0,0 +1,17 @@ +import { EJSON } from 'bson'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; + +/** + * Connection options can carry BSON values: CSFLE encrypted field maps hold + * `keyId` as Binary, for example. Structured cloning across the utility port + * drops prototypes, which would hand the driver plain objects instead of BSON + * values, so the connection parameters travel as EJSON, which round-trips + * every BSON type. + */ +export function encodeConnectParams(connectParams: ToolsConnectParams): string { + return EJSON.stringify(connectParams); +} + +export function decodeConnectParams(encoded: string): ToolsConnectParams { + return EJSON.parse(encoded) as ToolsConnectParams; +} 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 index 92dbac99fda..e98d532ab78 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -4,6 +4,7 @@ import { MessageChannel } from 'node:worker_threads'; 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'; @@ -53,11 +54,17 @@ describe('ToolsHost', function () { const result = (await client.call('performToolCall', { toolName: 'find', toolArgs: { database: 'test', collection: 'coll' }, - connectParams: { + connectParams: encodeConnectParams({ connectionId: 'c1', connectionString: 'mongodb://localhost:27019', - connectOptions: { serverSelectionTimeoutMS: 500 }, - }, + 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'); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts index 0fc531b6b4a..d715736d554 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -13,11 +13,11 @@ import type { UserConfig, } from 'mongodb-mcp-server'; import { serveToolsRequests } from './server'; +import { decodeConnectParams } from './connect-params'; import type { ToolsRpcMethod, ToolsRpcPort } from './messages'; import { createConnectionErrorHandler } from '../../tools-connection-error-handler'; import { ToolsLogger } from '../../tools-logger'; import { ToolsConnectionManager } from '../../tools-connection-manager'; -import type { ToolsConnectParams } from '../../tools-connection-manager'; import { removeZodTransforms } from '../../remove-zod-transforms'; import { READ_ONLY_DATABASE_TOOLS } from '../../available-tools'; @@ -179,11 +179,16 @@ export class ToolsHost { })); } case 'performToolCall': { - const { toolName, toolArgs, connectParams } = params as { + const { + toolName, + toolArgs, + connectParams: encodedConnectParams, + } = params as { toolName: string; toolArgs: unknown; - connectParams: ToolsConnectParams; + connectParams: string; }; + const connectParams = decodeConnectParams(encodedConnectParams); await this.ensureServerStarted(); const toolBase = this.server?.tools.find((t) => t.name === toolName); if (!toolBase) { From 7262280327517914aa9af2865b6a01be4ce255fe Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Tue, 27 Oct 2026 17:21:59 +0000 Subject: [PATCH 07/16] fix tests --- .../compass-assistant/test/tool-calls.eval.ts | 6 +- packages/compass-generative-ai/package.json | 2 +- .../src/tools-controller.ts | 6 - .../infrastructure/connect-params.spec.ts | 104 ++++++++++-------- .../tools/infrastructure/connect-params.ts | 63 +++++++++-- .../tools/infrastructure/in-memory-runner.ts | 28 +++++ .../src/tools/infrastructure/noop-metrics.ts | 17 +++ .../src/tools/infrastructure/tools-host.ts | 63 +---------- .../src/utilities/ai-assistant/index.spec.mts | 2 +- 9 files changed, 166 insertions(+), 125 deletions(-) create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts diff --git a/packages/compass-assistant/test/tool-calls.eval.ts b/packages/compass-assistant/test/tool-calls.eval.ts index 05924730e91..4b93feb62d5 100644 --- a/packages/compass-assistant/test/tool-calls.eval.ts +++ b/packages/compass-assistant/test/tool-calls.eval.ts @@ -89,10 +89,6 @@ let toolsInitialized = false; async function ensureToolsInitialized(): Promise { if (toolsInitialized) return; await toolsController.startServer(); - toolsController.setActiveTools(new Set(['db-read'])); - if (!toolsController.getActiveTools()['find']) { - throw new Error('Failed to start ToolsController server for eval tools'); - } toolsInitialized = true; } @@ -225,6 +221,8 @@ function createToolCallAssistantTask(config: EvalTaskConfig) { connectionConfig, }); + assert(tools['find'], 'ToolsController did not provide database tools'); + const contextPrompt = buildContextPromptText({ custom, connectionString: connectionConfig.connectionString, diff --git a/packages/compass-generative-ai/package.json b/packages/compass-generative-ai/package.json index 9f6a8f46298..ad4b7562303 100644 --- a/packages/compass-generative-ai/package.json +++ b/packages/compass-generative-ai/package.json @@ -72,6 +72,7 @@ "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.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", @@ -97,7 +98,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^2.2.2", - "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/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index a370d99581e..c195cd79f05 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -142,8 +142,6 @@ export class ToolsController { { toolName: definition.name, toolArgs: args, - // Encoded: connection options can hold BSON values that - // structured cloning would flatten (see connect-params.ts) connectParams: encodeConnectParams(connectParams), }, options.abortSignal @@ -198,14 +196,10 @@ export class ToolsController { setContext(context: ToolsContext): void { this.context = context; - // The utility may not be started yet: remember the latest config so - // startup sends this rather than the values the controller was built with. this.currentConfig = { enableMCPTelemetry: context.enableMCPTelemetry, maxTimeMS: context.maxTimeMS, }; - // Keep the MCP server's runtime config in the utility in sync with - // Compass preferences; it can no longer be mutated directly from here. void this.portClient?.call('setUserConfig', { enableMCPTelemetry: context.enableMCPTelemetry, maxTimeMS: context.maxTimeMS, 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 index d43ed9a4492..907f4f4f611 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts @@ -2,6 +2,7 @@ import { expect } from 'chai'; import { Binary, UUID } from 'bson'; import { MessageChannel } from 'node:worker_threads'; import { decodeConnectParams, encodeConnectParams } from './connect-params'; +import type { ConnectParamsWire } from './connect-params'; import type { ToolsConnectParams } from '../../tools-connection-manager'; describe('connect params over the port', function () { @@ -17,59 +18,68 @@ describe('connect params over the port', function () { channel.port2.close(); }); - const connectParamsWithBson = (): ToolsConnectParams => ({ - 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 - ), - bsonType: 'string', - }, - ], - }, - }, - kmsProviders: { local: { key: new UUID().toBinary() } }, - }, - }, - }); - - it('keeps BSON values intact when passed through a MessagePort', async function () { - // Structured cloning drops prototypes: this is the case the encoding - // exists for, so it is asserted through a real port, not just the helpers. - const received = new Promise((resolve) => { + 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: string) => resolve(value)); + channel.port2.on('message', (value: ConnectParamsWire) => resolve(value)); }); - channel.port1.postMessage(encodeConnectParams(connectParamsWithBson())); + channel.port1.postMessage(wire); + return await received; + } - const decoded = decodeConnectParams(await received); - const { autoEncryption } = decoded.connectOptions as { - autoEncryption?: { - encryptedFieldsMap?: Record; - }; - }; - const keyId = - autoEncryption?.encryptedFieldsMap?.['hr.employees'].fields[0].keyId; + 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 as Binary).buffer).to.deep.equal( + 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('round-trips plain options unchanged', function () { + it('leaves non-BSON values alone', async function () { const connectParams: ToolsConnectParams = { connectionId: 'c1', connectionString: 'mongodb://localhost:27017', @@ -80,8 +90,10 @@ describe('connect params over the port', function () { }, }; - expect( - decodeConnectParams(encodeConnectParams(connectParams)) - ).to.deep.equal(connectParams); + 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 index 00d873857c0..a528548fa34 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts @@ -1,17 +1,60 @@ -import { EJSON } from 'bson'; +import { Binary, UUID } from 'bson'; import type { ToolsConnectParams } from '../../tools-connection-manager'; +const BSON_TYPES = { UUID, Binary } as const; + /** - * Connection options can carry BSON values: CSFLE encrypted field maps hold - * `keyId` as Binary, for example. Structured cloning across the utility port - * drops prototypes, which would hand the driver plain objects instead of BSON - * values, so the connection parameters travel as EJSON, which round-trips - * every BSON type. + * 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 function encodeConnectParams(connectParams: ToolsConnectParams): string { - return EJSON.stringify(connectParams); +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; } -export function decodeConnectParams(encoded: string): ToolsConnectParams { - return EJSON.parse(encoded) as ToolsConnectParams; +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..9d1a5652a0e --- /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.setupServer(); + } + + async closeTransport(): Promise { + await this.server?.close(); + } +} 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/tools-host.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts index d715736d554..02e4b9e0b91 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -1,25 +1,18 @@ 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 { Keychain, UserConfigSchema } from 'mongodb-mcp-server'; +import type { Server } from 'mongodb-mcp-server'; import { serveToolsRequests } from './server'; import { decodeConnectParams } from './connect-params'; +import type { ConnectParamsWire } from './connect-params'; import type { ToolsRpcMethod, ToolsRpcPort } from './messages'; 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 { InMemoryRunner } from './in-memory-runner'; +import { NoopMetrics } from './noop-metrics'; export type ToolDefinitionInfo = { name: string; @@ -31,44 +24,6 @@ type ToolsHostConfig = { logger: Logger; }; -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(); - } -} - /** * Utility-process side of the assistant database tools. Owns the MCP server, * its connection manager, and the driver (moved here from ToolsController @@ -80,9 +35,6 @@ export class ToolsHost { private readonly runner: InMemoryRunner; private connectionManager: ToolsConnectionManager; private readonly disposable: Disposable; - // The utility has no preferences of its own: the renderer delivers the - // telemetry anonymous id (and the rest of the runtime config) through - // setUserConfig. private telemetryAnonymousId = ''; constructor(port: ToolsRpcPort, { logger }: ToolsHostConfig) { @@ -91,9 +43,6 @@ export class ToolsHost { disabledTools: ['connect'], loggers: ['mcp'], readOnly: true, - // NOTE: the preferences could change at runtime. As a best-effort way - // of keeping them in sync the renderer sends setUserConfig whenever - // the context changes. telemetry: 'disabled', }); @@ -186,7 +135,7 @@ export class ToolsHost { } = params as { toolName: string; toolArgs: unknown; - connectParams: string; + connectParams: ConnectParamsWire; }; const connectParams = decodeConnectParams(encodedConnectParams); await this.ensureServerStarted(); diff --git a/packages/compass/src/utilities/ai-assistant/index.spec.mts b/packages/compass/src/utilities/ai-assistant/index.spec.mts index d0ffdbf9dff..bddd1c971f8 100644 --- a/packages/compass/src/utilities/ai-assistant/index.spec.mts +++ b/packages/compass/src/utilities/ai-assistant/index.spec.mts @@ -1,6 +1,6 @@ import { expect } from 'chai'; import process from 'node:process'; -import { EventEmitter } from 'events'; +import { EventEmitter } from 'node:events'; import type { MessagePortMain, ParentPort } from 'electron'; import { main } from './index.mts'; From 6c156ff6efe1411c9cbb54007d81851d337b921d Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 11:55:25 +0000 Subject: [PATCH 08/16] fix e2e tests --- packages/compass-assistant/test/tool-calls.eval.ts | 6 +++++- packages/compass/webpack.config.js | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/compass-assistant/test/tool-calls.eval.ts b/packages/compass-assistant/test/tool-calls.eval.ts index 4b93feb62d5..2933b9e90b7 100644 --- a/packages/compass-assistant/test/tool-calls.eval.ts +++ b/packages/compass-assistant/test/tool-calls.eval.ts @@ -67,8 +67,11 @@ 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(); -new ToolsHost(evalChannel.port2, { logger }); +const evalHost = new ToolsHost(evalChannel.port2, { logger }); const toolsController = new ToolsController({ logger, @@ -282,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/webpack.config.js b/packages/compass/webpack.config.js index 9f1a46804de..371abeddb61 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', }) From fb4f807ab640b05b359251e2cac6841b9c406d23 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 12:00:31 +0000 Subject: [PATCH 09/16] remove deprecated --- .../src/tools/infrastructure/in-memory-runner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9d1a5652a0e..b192c4280a2 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts @@ -19,7 +19,7 @@ export class InMemoryRunner extends TransportRunnerBase { } async start(): Promise { - this.server = await this.setupServer(); + this.server = await this.createServer(); } async closeTransport(): Promise { From 386cb133c31d4a5ed87e81d22bdebeb61c295865 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 12:32:20 +0000 Subject: [PATCH 10/16] copilot review --- .../src/tools-controller.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index c195cd79f05..138e472f6c0 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -200,11 +200,18 @@ export class ToolsController { enableMCPTelemetry: context.enableMCPTelemetry, maxTimeMS: context.maxTimeMS, }; - void this.portClient?.call('setUserConfig', { - enableMCPTelemetry: context.enableMCPTelemetry, - maxTimeMS: context.maxTimeMS, - telemetryAnonymousId: this.getTelemetryAnonymousId(), - }); + 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({ @@ -287,7 +294,7 @@ export class ToolsController { this.startPromise = null; this.dbToolDefinitions = []; try { - await client.call('stopServer'); + await client.call('stopServer', undefined, AbortSignal.timeout(10_000)); this.logger.log.info( this.logger.mongoLogId(1_001_000_407), 'ToolsController', From 01c8db79b46a127fac001e590c432bb9074968eb Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 12:49:07 +0000 Subject: [PATCH 11/16] new import order rule --- .../src/tools-controller.spec.ts | 35 +++++++++++++------ .../src/tools-controller.ts | 8 ++--- .../infrastructure/connect-params.spec.ts | 4 +-- .../tools/infrastructure/rpc-client.spec.ts | 26 ++++++++++---- .../tools/infrastructure/tools-host.spec.ts | 2 +- .../src/tools/infrastructure/tools-host.ts | 8 ++--- .../src/utilities/ai-assistant/index.mts | 2 +- .../src/utilities/ai-assistant/index.spec.mts | 20 ++++------- 8 files changed, 61 insertions(+), 44 deletions(-) diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index 3f03f061504..6bb98167926 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -1,18 +1,18 @@ +import { MessageChannel } from 'node:worker_threads'; import sinon from 'sinon'; import { expect } from 'chai'; -import { MessageChannel } from 'node:worker_threads'; -import { ToolsController } from './tools-controller'; -import type { ToolGroup } from './tools-controller'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider'; import type { Logger } from '@mongodb-js/compass-logging'; -import type { ToolsConnectParams } from './tools-connection-manager'; -import { ToolsHost } from './tools/infrastructure/tools-host'; import { createSandboxFromDefaultPreferences, type PreferencesAccess, } from '@mongodb-js/compass-preferences-model'; import type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; +import type { ToolsConnectParams } from './tools-connection-manager'; +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; @@ -33,6 +33,20 @@ describe('ToolsController', function () { 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 { @@ -436,8 +450,8 @@ describe('ToolsController', function () { await toolsController.startServer(); const runner = (openHosts[0] as any).runner; - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitFor(() => runner.userConfig.maxTimeMS === 1234); expect(runner.userConfig.telemetry).to.equal('enabled'); expect(runner.userConfig.maxTimeMS).to.equal(1234); }); @@ -450,15 +464,14 @@ describe('ToolsController', function () { enableMCPTelemetry: true, connections: [], }); - // give the rpc call a moment to land - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitFor(() => runner.userConfig.telemetry === 'enabled'); expect(runner.userConfig.telemetry).to.equal('enabled'); toolsController.setContext({ enableMCPTelemetry: false, connections: [], }); - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitFor(() => runner.userConfig.telemetry === 'disabled'); expect(runner.userConfig.telemetry).to.equal('disabled'); }); @@ -471,7 +484,7 @@ describe('ToolsController', function () { maxTimeMS: 5000, connections: [], }); - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitFor(() => runner.userConfig.maxTimeMS === 5000); expect(runner.userConfig.maxTimeMS).to.equal(5000); toolsController.setContext({ @@ -479,7 +492,7 @@ describe('ToolsController', function () { maxTimeMS: undefined, connections: [], }); - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitFor(() => runner.userConfig.maxTimeMS === undefined); expect(runner.userConfig.maxTimeMS).to.equal(undefined); }); }); diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 138e472f6c0..1b4adb587e5 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -3,15 +3,15 @@ import type { ToolSet } from 'ai'; import type { Logger } from '@mongodb-js/compass-logging'; import z from 'zod/v4'; 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 { 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 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 { debugConnection, debugConnectionDescription, 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 index 907f4f4f611..91c9405faf8 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts @@ -1,9 +1,9 @@ +import { MessageChannel } from 'node:worker_threads'; import { expect } from 'chai'; import { Binary, UUID } from 'bson'; -import { MessageChannel } from 'node:worker_threads'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; import { decodeConnectParams, encodeConnectParams } from './connect-params'; import type { ConnectParamsWire } from './connect-params'; -import type { ToolsConnectParams } from '../../tools-connection-manager'; describe('connect params over the port', function () { let channel: MessageChannel; 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 index 6ac32c0b8ff..c87116fbfd9 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts @@ -1,6 +1,6 @@ +import { MessageChannel } from 'node:worker_threads'; import { expect } from 'chai'; import sinon from 'sinon'; -import { MessageChannel } from 'node:worker_threads'; import { createToolsRpcClient } from './rpc-client'; import { serveToolsRequests } from './server'; @@ -107,16 +107,20 @@ describe('tools rpc', function () { it('ignores messages that are not part of the protocol', async function () { let called = 0; - serveToolsRequests(channel.port2, async () => { + 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' }); - // let stray messages arrive - await new Promise((resolve) => setTimeout(resolve, 0)); - expect(called).to.equal(0); + + // 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 () { @@ -138,8 +142,9 @@ describe('tools rpc', function () { 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')); - await new Promise((resolve) => setTimeout(resolve, 0)); expect(postMessage).to.not.have.been.called; postMessage.restore(); @@ -148,10 +153,17 @@ describe('tools rpc', function () { 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)); }); @@ -161,7 +173,7 @@ describe('tools rpc', function () { void client.call('performToolCall').catch(() => { // the request is aborted below; the rejection is expected }); - await new Promise((resolve) => setTimeout(resolve, 0)); + await arrived; server[Symbol.dispose](); 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 index e98d532ab78..712e02e3f74 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -1,6 +1,6 @@ +import { MessageChannel } from 'node:worker_threads'; import { expect } from 'chai'; import sinon from 'sinon'; -import { MessageChannel } from 'node:worker_threads'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import type { Logger } from '@mongodb-js/compass-logging'; import { createToolsRpcClient } from './rpc-client'; diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts index 02e4b9e0b91..97a44d3c2d2 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -2,15 +2,15 @@ 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 { serveToolsRequests } from './server'; -import { decodeConnectParams } from './connect-params'; -import type { ConnectParamsWire } from './connect-params'; -import type { ToolsRpcMethod, ToolsRpcPort } from './messages'; 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'; diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts index 509faa31f2e..c8af71061c1 100644 --- a/packages/compass/src/utilities/ai-assistant/index.mts +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -2,9 +2,9 @@ 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'; -import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; export const name = 'ai-assistant'; diff --git a/packages/compass/src/utilities/ai-assistant/index.spec.mts b/packages/compass/src/utilities/ai-assistant/index.spec.mts index bddd1c971f8..3e72634e390 100644 --- a/packages/compass/src/utilities/ai-assistant/index.spec.mts +++ b/packages/compass/src/utilities/ai-assistant/index.spec.mts @@ -1,6 +1,6 @@ -import { expect } from 'chai'; 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'; @@ -76,24 +76,16 @@ describe('ai-assistant utility', function () { expect(response.result.map((tool) => tool.name)).to.include('find'); }); - it('stops serving requests when the port closes', async function () { - this.timeout(10_000); + it('stops serving requests when the port closes', function () { const port = createFakePort(); parentPort.emit('message', { ports: [port] }); - // let the host attach its listeners - await new Promise((resolve) => setTimeout(resolve, 0)); + expect(port.listenerCount('message')).to.be.greaterThan(0); port.emit('close'); - port.emit('message', { - data: { - kind: 'request', - id: 1, - method: 'listDatabaseTools', - params: null, - }, - }); - await new Promise((resolve) => setTimeout(resolve, 10)); + // 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); }); }); From 8ab4070640b4ddca6dad606a667d55db5d36e642 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 13:06:44 +0000 Subject: [PATCH 12/16] lint again --- packages/compass-assistant/test/tool-calls.eval.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compass-assistant/test/tool-calls.eval.ts b/packages/compass-assistant/test/tool-calls.eval.ts index 7f3c2052d55..fcab42ba971 100644 --- a/packages/compass-assistant/test/tool-calls.eval.ts +++ b/packages/compass-assistant/test/tool-calls.eval.ts @@ -9,11 +9,11 @@ */ 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'; import { MongoClient } from 'mongodb'; -import { MessageChannel } from 'node:worker_threads'; import { ToolsController, type ToolGroup, From d5aa2de2321659dcb4a538dade2ae573a00b4ca1 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 14:13:21 +0000 Subject: [PATCH 13/16] make it work for compass-web --- package-lock.json | 2 +- .../compass-generative-ai/src/provider.tsx | 22 ++++++++- packages/compass-web/src/entrypoint.tsx | 4 +- .../compass-web/src/tools-loopback.spec.ts | 47 +++++++++++++++++++ packages/compass-web/src/tools-loopback.ts | 47 +++++++++++++++++++ 5 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 packages/compass-web/src/tools-loopback.spec.ts create mode 100644 packages/compass-web/src/tools-loopback.ts diff --git a/package-lock.json b/package-lock.json index 299970519a3..b83d07e7263 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55057,6 +55057,7 @@ "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.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", @@ -55082,7 +55083,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^2.2.2", - "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-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; +} From e476de76f3139ec3ef1493802a4d6f423948bd0b Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 15:45:47 +0000 Subject: [PATCH 14/16] reduce packed bundle size --- packages/compass/webpack.config.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/compass/webpack.config.js b/packages/compass/webpack.config.js index 371abeddb61..601a3f1ab52 100644 --- a/packages/compass/webpack.config.js +++ b/packages/compass/webpack.config.js @@ -212,6 +212,13 @@ module.exports = (_env, args) => { externals: Object.fromEntries( sharedExternals.map((name) => [name, `node-commonjs ${name}`]) ), + 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, + }, + }, plugins: [ new webpack.EnvironmentPlugin(hadronEnvConfig), // Main forks one file per utility From 51e618f41d1eb6cd1859a252f3d7c4ff70697387 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 15:57:42 +0000 Subject: [PATCH 15/16] optimize bundle size for utilities --- packages/compass/webpack.config.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/compass/webpack.config.js b/packages/compass/webpack.config.js index 601a3f1ab52..f8206391d79 100644 --- a/packages/compass/webpack.config.js +++ b/packages/compass/webpack.config.js @@ -219,13 +219,27 @@ module.exports = (_env, args) => { '@mongodb-js/atlas-local': false, }, }, - plugins: [ - new webpack.EnvironmentPlugin(hadronEnvConfig), - // Main forks one file per utility - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: Object.keys(utilityEntries).length, - }), - ], + // 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', From 37ce50f20151ede9d0b07249e483971da1510804 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 18:13:28 +0000 Subject: [PATCH 16/16] fix e2e flakiness --- packages/compass-e2e-tests/tests/assistant.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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(