From f7fe2b1f15bbac3c7d9cb62b4cc9e915a8942a1a Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 18:31:32 +0000 Subject: [PATCH 01/16] feat: ai assistant utility --- package-lock.json | 2 +- packages/compass-generative-ai/package.json | 8 +- .../infrastructure/connect-params.spec.ts | 99 +++++++++ .../tools/infrastructure/connect-params.ts | 60 ++++++ .../tools/infrastructure/in-memory-runner.ts | 28 +++ .../src/tools/infrastructure/messages.ts | 69 +++++++ .../src/tools/infrastructure/noop-metrics.ts | 17 ++ .../tools/infrastructure/rpc-client.spec.ts | 182 +++++++++++++++++ .../src/tools/infrastructure/rpc-client.ts | 98 +++++++++ .../src/tools/infrastructure/server.ts | 77 +++++++ .../tools/infrastructure/tools-host.spec.ts | 102 ++++++++++ .../src/tools/infrastructure/tools-host.ts | 192 ++++++++++++++++++ .../compass-utils/src/utility-process.spec.ts | 56 +++++ packages/compass-utils/src/utility-process.ts | 1 + packages/compass/src/main/application.ts | 2 +- .../src/utilities/ai-assistant/index.mts | 48 +++++ .../src/utilities/ai-assistant/index.spec.mts | 91 +++++++++ packages/compass/webpack.config.js | 40 +++- 18 files changed, 1160 insertions(+), 12 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 create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/messages.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.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-utils/src/utility-process.spec.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/package-lock.json b/package-lock.json index 072b3a26f63..57ab14bf7c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55322,6 +55322,7 @@ "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.0", "ai": "^7.0.0", + "bson": "^7.3.2", "ipaddr.js": "^2.5.0", "mongodb": "^7.5.0", "mongodb-connection-string-url": "^7.0.2", @@ -55347,7 +55348,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^3.35.0", - "bson": "^7.3.2", "chai": "^4.3.6", "depcheck": "^1.4.1", "electron-mocha": "^12.2.0", diff --git a/packages/compass-generative-ai/package.json b/packages/compass-generative-ai/package.json index 2a1fb9dd999..926f4021b2c 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": { @@ -71,6 +73,7 @@ "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.0", "ai": "^7.0.0", + "bson": "^7.3.2", "ipaddr.js": "^2.5.0", "mongodb": "^7.5.0", "mongodb-connection-string-url": "^7.0.2", @@ -96,7 +99,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^3.35.0", - "bson": "^7.3.2", "chai": "^4.3.6", "depcheck": "^1.4.1", "electron-mocha": "^12.2.0", diff --git a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts new file mode 100644 index 00000000000..91c9405faf8 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts @@ -0,0 +1,99 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import { Binary, UUID } from 'bson'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; +import { decodeConnectParams, encodeConnectParams } from './connect-params'; +import type { ConnectParamsWire } from './connect-params'; + +describe('connect params over the port', function () { + let channel: MessageChannel; + + beforeEach(function () { + channel = new MessageChannel(); + }); + + afterEach(function () { + // Open ports keep the event loop alive and would stop the suite exiting + channel.port1.close(); + channel.port2.close(); + }); + + async function overThePort(wire: ConnectParamsWire) { + const received = new Promise((resolve) => { + // worker MessagePorts emit the value itself, unlike the EventTarget + // shape `listen` handles + channel.port2.on('message', (value: ConnectParamsWire) => resolve(value)); + }); + channel.port1.postMessage(wire); + return await received; + } + + it('restores BSON values passed through a MessagePort', async function () { + // Structured cloning keeps the values but drops their prototypes: this is + // the case the encoding exists for, so it is asserted through a real port. + const connectParams = { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27017', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + autoEncryption: { + keyVaultNamespace: 'encryption.__keyVault', + encryptedFieldsMap: { + 'hr.employees': { + fields: [ + { + path: 'taxid', + keyId: new Binary( + Buffer.from('a21ddc6a880643849fdf8ba02a767b5f', 'hex'), + Binary.SUBTYPE_UUID + ), + }, + ], + }, + }, + kmsProviders: { local: { key: new UUID() } }, + }, + }, + } as unknown as ToolsConnectParams; + + const decoded = decodeConnectParams( + await overThePort(encodeConnectParams(connectParams)) + ); + const autoEncryption = ( + decoded.connectOptions as { + autoEncryption: { + encryptedFieldsMap: Record; + kmsProviders: { local: { key: unknown } }; + }; + } + ).autoEncryption; + + const keyId = autoEncryption.encryptedFieldsMap['hr.employees'].fields[0] + .keyId as Binary; + expect(keyId).to.be.instanceOf(Binary); + expect(keyId.buffer).to.deep.equal( + Buffer.from('a21ddc6a880643849fdf8ba02a767b5f', 'hex') + ); + // UUID extends Binary, so the more specific type has to win + expect(autoEncryption.kmsProviders.local.key).to.be.instanceOf(UUID); + }); + + it('leaves non-BSON values alone', async function () { + const connectParams: ToolsConnectParams = { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27017', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + serverSelectionTimeoutMS: 500, + }, + }; + + const decoded = decodeConnectParams( + await overThePort(encodeConnectParams(connectParams)) + ); + + expect(decoded).to.deep.equal(connectParams); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts new file mode 100644 index 00000000000..a528548fa34 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts @@ -0,0 +1,60 @@ +import { Binary, UUID } from 'bson'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; + +const BSON_TYPES = { UUID, Binary } as const; + +/** + * Structured cloning across the utility port keeps BSON values but drops their + * prototypes, so the driver would receive plain objects for e.g. the `keyId` + * of an encrypted field. + */ +export type ConnectParamsWire = { + connectParams: ToolsConnectParams; + bsonValues: Map; +}; + +export function encodeConnectParams( + connectParams: ToolsConnectParams +): ConnectParamsWire { + const bsonValues = new Map(); + collectBsonValues(connectParams, bsonValues, new Set()); + return { connectParams, bsonValues }; +} + +export function decodeConnectParams({ + connectParams, + bsonValues, +}: ConnectParamsWire): ToolsConnectParams { + for (const [value, typeName] of bsonValues) { + const bsonType = BSON_TYPES[typeName as keyof typeof BSON_TYPES]; + if (bsonType && typeof value === 'object' && value !== null) { + Object.setPrototypeOf(value, bsonType.prototype); + } + } + return connectParams; +} + +function collectBsonValues( + value: unknown, + found: Map, + seen: Set +): void { + if (typeof value !== 'object' || value === null || seen.has(value)) { + return; + } + + for (const [name, bsonType] of Object.entries(BSON_TYPES)) { + if (value instanceof bsonType) { + found.set(value, name); + return; + } + } + + seen.add(value); + + for (const entryValue of Array.isArray(value) + ? value + : Object.values(value)) { + collectBsonValues(entryValue, found, seen); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts b/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts new file mode 100644 index 00000000000..b192c4280a2 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts @@ -0,0 +1,28 @@ +import { TransportRunnerBase } from 'mongodb-mcp-server'; +import type { + Server, + TransportRunnerConfig, + UserConfig, +} from 'mongodb-mcp-server'; + +/** + * In-memory MCP runner that doesn't bind to any external transport. + * The server is kept in memory for direct programmatic access. + */ +export class InMemoryRunner extends TransportRunnerBase { + public server?: Server; + public readonly userConfig: UserConfig; + + constructor(config: TransportRunnerConfig) { + super(config); + this.userConfig = config.userConfig; + } + + async start(): Promise { + this.server = await this.createServer(); + } + + async closeTransport(): Promise { + await this.server?.close(); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/messages.ts b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts new file mode 100644 index 00000000000..1ed8adfa5ed --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts @@ -0,0 +1,69 @@ +import type { EventSource } from '@mongodb-js/compass-utils'; + +export type ToolsRpcPort = { + postMessage(message: unknown): void; + close(): void; +} & EventSource; + +export type ToolsRpcMethod = + /** + * Keeps the MCP server's runtime config in sync with Compass preferences. + * Sent by the renderer every time the assistant context changes. It replaces + * the direct `runner.userConfig` mutation `ToolsController.setContext` used + * to do when the server lived in the renderer. + */ + | 'setUserConfig' + /** + * Fetches the read-only database tool definitions: name, description and + * input schema as JSON Schema. Sent once when the connection is established, + * doubling as the handshake; the result is cached renderer-side so + * `getActiveTools()` can stay synchronous. + */ + | 'listDatabaseTools' + /** + * Executes one database tool. Sent by the renderer per AI tool call. + * The utility connects the driver to the given connection, invokes the tool + * with the request's abort signal (identified via the abort-for-id pattern, + * see `rpc-client.ts`), disconnects in `finally`, and resolves with the + * tool result, which is plain data. + */ + | 'performToolCall' + /** + * Closes the MCP server. Sent on provider unmount / window close. The + * utility additionally tears down a host when its port closes, which covers + * a renderer crash that can never deliver this message. + */ + | 'stopServer'; + +export type ToolsRpcRequest = { + kind: 'request'; + id: number; + method: ToolsRpcMethod; + params: unknown; +}; + +export type ToolsRpcResponse = { kind: 'response'; id: number } & ( + { ok: true; result: unknown } | { ok: false; error: string } +); + +export type ToolsRpcAbort = { kind: 'abort'; id: number }; + +export type ToolsRpcMessage = + ToolsRpcRequest | ToolsRpcResponse | ToolsRpcAbort; + +export function isToolsRpcMessage(value: unknown): value is ToolsRpcMessage { + return ( + typeof value === 'object' && + value !== null && + 'kind' in value && + ['request', 'response', 'abort'].includes(value.kind as string) + ); +} + +export function isResponse(value: unknown): value is ToolsRpcResponse { + return ( + typeof value === 'object' && + value !== null && + (value as { kind: unknown }).kind === 'response' + ); +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts b/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts new file mode 100644 index 00000000000..e7e43da0297 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts @@ -0,0 +1,17 @@ +import type { DefaultMetrics, Metrics } from 'mongodb-mcp-server'; + +class NoopDefaultMetrics { + observe(): void { + // no-op + } +} + +export class NoopMetrics implements Metrics { + constructor() {} + get(_key: K): DefaultMetrics[K] { + return new NoopDefaultMetrics() as unknown as DefaultMetrics[K]; + } + getMetrics(): Promise { + return Promise.resolve(''); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts new file mode 100644 index 00000000000..c87116fbfd9 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts @@ -0,0 +1,182 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import sinon from 'sinon'; +import { createToolsRpcClient } from './rpc-client'; +import { serveToolsRequests } from './server'; + +describe('tools rpc', function () { + let channel: MessageChannel; + + beforeEach(function () { + channel = new MessageChannel(); + }); + + afterEach(function () { + channel.port1.close(); + channel.port2.close(); + }); + + it('round-trips a request to a response', async function () { + const client = createToolsRpcClient(channel.port1); + const params = { enableMCPTelemetry: true, maxTimeMS: 5 }; + serveToolsRequests(channel.port2, async (method, sentParams, _signal) => { + expect(method).to.equal('setUserConfig'); + return Promise.resolve(sentParams); + }); + + expect(await client.call('setUserConfig', params)).to.deep.equal(params); + }); + + it('rejects with the error message from the other side', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, () => { + throw new Error('boom'); + }); + + const error = await client + .call('performToolCall', { toolName: 'find' }) + .catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'boom'); + }); + + it('propagates abort to the server-side signal', async function () { + const client = createToolsRpcClient(channel.port1); + const controller = new AbortController(); + let serverSignal: AbortSignal | undefined; + + // resolves only once the server side processed the abort message + const serverAborted = new Promise((resolve) => { + serveToolsRequests(channel.port2, (_method, _params, signal) => { + serverSignal = signal; + return new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => { + reject(signal.reason); + resolve(); + }); + }); + }); + }); + + const pending = client.call( + 'performToolCall', + undefined, + controller.signal + ); + controller.abort(new Error('stop')); + + const error = await pending.catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'stop'); + await serverAborted; + expect(serverSignal?.aborted).to.equal(true); + }); + + it('rejects immediately for an already-aborted signal', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, async () => Promise.resolve('unused')); + + const error = await client + .call('performToolCall', undefined, AbortSignal.abort(new Error('nope'))) + .catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'nope'); + }); + + it('resolves in request order regardless of completion order', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, async (_method, params, _signal) => { + const { delay, name } = params as { delay: number; name: string }; + await new Promise((resolve) => setTimeout(resolve, delay)); + return name; + }); + + const slow = client.call('performToolCall', { delay: 50, name: 'slow' }); + const fast = client.call('performToolCall', { delay: 1, name: 'fast' }); + expect(await Promise.all([slow, fast])).to.deep.equal(['slow', 'fast']); + }); + + it('rejects pending calls when the client is closed', async function () { + const client = createToolsRpcClient(channel.port1); + // no server on the other end: the call stays pending until close() + const pending = client.call('listDatabaseTools'); + client.close(); + + const error = await pending.catch((err: Error) => err); + expect(error) + .to.be.instanceOf(Error) + .with.property('message', 'Tools rpc client closed'); + }); + + it('ignores messages that are not part of the protocol', async function () { + let called = 0; + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, () => { + called += 1; + return Promise.resolve(null); + }); + channel.port1.postMessage({ hello: 'stray' }); + channel.port1.postMessage(null); + channel.port1.postMessage({ kind: 'nonsense' }); + + // Messages arrive in order, so a request that gets answered proves the + // strays were seen and ignored rather than queued behind it + await client.call('listDatabaseTools'); + + expect(called).to.equal(1); + }); + + it('closes the port when the client is closed', function () { + const close = sinon.spy(channel.port1, 'close'); + const client = createToolsRpcClient(channel.port1); + + client.close(); + + expect(close).to.have.been.calledOnce; + close.restore(); + }); + + it('does not send an abort message for a request that already settled', async function () { + const client = createToolsRpcClient(channel.port1); + const controller = new AbortController(); + const postMessage = sinon.spy(channel.port1, 'postMessage'); + serveToolsRequests(channel.port2, () => Promise.resolve('ok')); + + await client.call('performToolCall', undefined, controller.signal); + postMessage.resetHistory(); + + // Abort listeners run synchronously, so no waiting is needed to know + // whether the client still had one attached + controller.abort(new Error('too late')); + + expect(postMessage).to.not.have.been.called; + postMessage.restore(); + }); + + it('aborts in-flight requests when the server is disposed', async function () { + const client = createToolsRpcClient(channel.port1); + let serverSignal: AbortSignal | undefined; + // Resolved by the handler, so the test never has to guess how long a + // message takes to arrive + let requestArrived: () => void; + const arrived = new Promise((resolve) => { + requestArrived = resolve; + }); + const server = serveToolsRequests( + channel.port2, + (_method, _params, signal) => { + serverSignal = signal; + requestArrived(); + return new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => reject(signal.reason)); + }); + } + ); + + void client.call('performToolCall').catch(() => { + // the request is aborted below; the rejection is expected + }); + await arrived; + + server[Symbol.dispose](); + + expect(serverSignal?.aborted).to.equal(true); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts new file mode 100644 index 00000000000..5ae9142c88b --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts @@ -0,0 +1,98 @@ +import { listen } from '@mongodb-js/compass-utils'; +import { isResponse } from './messages'; +import type { + ToolsRpcAbort, + ToolsRpcMethod, + ToolsRpcPort, + ToolsRpcRequest, +} from './messages'; + +/** + * Renderer-side client. `port` is the end of the channel returned by + * `openUtilityPort`. + */ +export function createToolsRpcClient(port: ToolsRpcPort): { + call( + method: ToolsRpcMethod, + params?: unknown, + signal?: AbortSignal + ): Promise; + close(): void; +} { + let nextId = 0; + const pending = new Map< + number, + { + resolve: (value: unknown) => void; + reject: (reason: unknown) => void; + /** Removes the abort listener the call installed, if any */ + stopListeningForAbort?: () => void; + } + >(); + + const settle = ( + id: number, + settleEntry: (entry: { + resolve: (value: unknown) => void; + reject: (reason: unknown) => void; + stopListeningForAbort?: () => void; + }) => void + ): void => { + const entry = pending.get(id); + if (!entry) return; + pending.delete(id); + entry.stopListeningForAbort?.(); + settleEntry(entry); + }; + + const stopListening = listen(port, 'message', (event: { data: unknown }) => { + const message = event.data; + if (!isResponse(message)) return; + + settle(message.id, (entry) => { + if (message.ok) entry.resolve(message.result); + else entry.reject(new Error(message.error)); + }); + }); + + return { + call(method, params, signal) { + const id = nextId++; + return new Promise((resolve, reject) => { + if (signal?.aborted) { + return reject( + signal.reason ?? new Error('The operation was aborted') + ); + } + pending.set(id, { resolve, reject }); + if (signal) { + const stopListeningForAbort = listen(signal, 'abort', () => { + settle(id, (entry) => { + port.postMessage({ kind: 'abort', id } satisfies ToolsRpcAbort); + entry.reject( + signal.reason ?? new Error('The operation was aborted') + ); + }); + }); + pending.get(id)!.stopListeningForAbort = () => + stopListeningForAbort[Symbol.dispose](); + } + port.postMessage({ + kind: 'request', + id, + method, + params, + } satisfies ToolsRpcRequest); + }); + }, + close() { + for (const id of [...pending.keys()]) { + settle(id, (entry) => + entry.reject(new Error('Tools rpc client closed')) + ); + } + stopListening[Symbol.dispose](); + port.close(); + }, + }; +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/server.ts b/packages/compass-generative-ai/src/tools/infrastructure/server.ts new file mode 100644 index 00000000000..ce2b2a253f3 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/server.ts @@ -0,0 +1,77 @@ +import { listen } from '@mongodb-js/compass-utils'; +import { isToolsRpcMessage } from './messages'; +import type { + ToolsRpcMethod, + ToolsRpcPort, + ToolsRpcResponse, +} from './messages'; + +/** + * Utility-side server. `port` is a `MessagePortMain` received through + * `onPort` (already started). Each request gets its own AbortController; + * abort messages for that id abort it. + */ +export function serveToolsRequests( + port: ToolsRpcPort, + handler: ( + method: ToolsRpcMethod, + params: unknown, + signal: AbortSignal + ) => Promise +): Disposable { + const controllers = new Map(); + + const stopListening = listen(port, 'message', (event: { data: unknown }) => { + const message = event.data; + if (!isToolsRpcMessage(message)) return; + + if (message.kind === 'abort') { + controllers.get(message.id)?.abort(); + return; + } + + if (message.kind !== 'request') return; + + const controller = new AbortController(); + + controllers.set(message.id, controller); + + // The handler may throw synchronously; route it through a promise chain + // so a throw becomes a rejection instead of an uncaught error that would + // take the utility process down. + Promise.resolve() + .then(() => handler(message.method, message.params, controller.signal)) + .then((result) => { + port.postMessage({ + kind: 'response', + id: message.id, + ok: true, + result, + } satisfies ToolsRpcResponse); + }) + .catch((error: unknown) => { + port.postMessage({ + kind: 'response', + id: message.id, + ok: false, + error: + error instanceof Error + ? error.message + : String((error as any)?.message ?? error), + } satisfies ToolsRpcResponse); + }) + .finally(() => { + controllers.delete(message.id); + }); + }); + + return { + [Symbol.dispose]() { + stopListening[Symbol.dispose](); + for (const controller of controllers.values()) { + controller.abort(); + } + controllers.clear(); + }, + }; +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts new file mode 100644 index 00000000000..712e02e3f74 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -0,0 +1,102 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import sinon from 'sinon'; +import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; +import type { Logger } from '@mongodb-js/compass-logging'; +import { createToolsRpcClient } from './rpc-client'; +import { encodeConnectParams } from './connect-params'; +import { ToolsHost } from './tools-host'; +import type { ToolDefinitionInfo } from './tools-host'; + +describe('ToolsHost', function () { + // The MCP server starts synchronously enough for the in-memory runner, but + // registering the tools and the JSON Schema conversion give it some slack. + this.timeout(10_000); + + let channel: MessageChannel; + let host: ToolsHost; + let logger: Logger; + + beforeEach(function () { + channel = new MessageChannel(); + logger = createNoopLogger(); + host = new ToolsHost(channel.port2, { + logger, + }); + }); + + afterEach(async function () { + await host.stop(); + channel.port1.close(); + channel.port2.close(); + }); + + it('lists read-only database tools as JSON Schema', async function () { + const client = createToolsRpcClient(channel.port1); + const tools = (await client.call( + 'listDatabaseTools' + )) as ToolDefinitionInfo[]; + + const names = tools.map((t) => t.name).sort(); + expect(names).to.include('find'); + expect(names).to.include('aggregate'); + expect(names).to.not.include('connect'); + for (const tool of tools) { + expect(tool.description).to.be.a('string'); + expect(tool.inputSchema).to.have.property('type', 'object'); + } + }); + + it('returns an error result when the connection fails', async function () { + const client = createToolsRpcClient(channel.port1); + // nothing is listening on this port, so connecting fails and the MCP + // server surfaces that as an error result, not a rejection + const result = (await client.call('performToolCall', { + toolName: 'find', + toolArgs: { database: 'test', collection: 'coll' }, + connectParams: encodeConnectParams({ + connectionId: 'c1', + connectionString: 'mongodb://localhost:27019', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + // Fail fast when nothing is listening: the driver's default server + // selection timeout (30s) would outlive this suite's timeout. + serverSelectionTimeoutMS: 500, + }, + }), + })) as { content: unknown[]; isError?: boolean }; + + expect(result).to.be.an('object'); + expect(result.isError).to.equal(true); + }); + + it('applies setUserConfig to the runner config', async function () { + const client = createToolsRpcClient(channel.port1); + await client.call('setUserConfig', { + enableMCPTelemetry: true, + maxTimeMS: 1000, + }); + expect((host as any).runner.userConfig.telemetry).to.equal('enabled'); + expect((host as any).runner.userConfig.maxTimeMS).to.equal(1000); + }); + + it('releases the server and the connection when stopped', async function () { + const client = createToolsRpcClient(channel.port1); + await client.call('listDatabaseTools'); + const connectionManager = (host as any).connectionManager; + const disconnect = sinon.spy(connectionManager, 'disconnect'); + + await host.stop(); + + expect(disconnect).to.have.been.called; + const settled = await Promise.race([ + client + .call('listDatabaseTools') + .then(() => 'answered') + .catch(() => 'rejected'), + new Promise((resolve) => setTimeout(() => resolve('pending'), 100)), + ]); + expect(settled).to.equal('pending'); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts new file mode 100644 index 00000000000..97a44d3c2d2 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -0,0 +1,192 @@ +import type { Logger } from '@mongodb-js/compass-logging'; +import { z } from 'zod/v4'; +import { Keychain, UserConfigSchema } from 'mongodb-mcp-server'; +import type { Server } from 'mongodb-mcp-server'; +import { createConnectionErrorHandler } from '../../tools-connection-error-handler'; +import { ToolsLogger } from '../../tools-logger'; +import { ToolsConnectionManager } from '../../tools-connection-manager'; +import { removeZodTransforms } from '../../remove-zod-transforms'; +import { READ_ONLY_DATABASE_TOOLS } from '../../available-tools'; +import type { ToolsRpcMethod, ToolsRpcPort } from './messages'; +import type { ConnectParamsWire } from './connect-params'; +import { decodeConnectParams } from './connect-params'; +import { serveToolsRequests } from './server'; +import { InMemoryRunner } from './in-memory-runner'; +import { NoopMetrics } from './noop-metrics'; + +export type ToolDefinitionInfo = { + name: string; + description: string; + inputSchema: Record; +}; + +type ToolsHostConfig = { + logger: Logger; +}; + +/** + * Utility-process side of the assistant database tools. Owns the MCP server, + * its connection manager, and the driver (moved here from ToolsController + * when the server moved out of the renderer). Exposes the protocol methods + * from `./messages` over the port the utility receives for each renderer. + */ +export class ToolsHost { + private logger: Logger; + private readonly runner: InMemoryRunner; + private connectionManager: ToolsConnectionManager; + private readonly disposable: Disposable; + private telemetryAnonymousId = ''; + + constructor(port: ToolsRpcPort, { logger }: ToolsHostConfig) { + this.logger = logger; + const mcpConfig = UserConfigSchema.parse({ + disabledTools: ['connect'], + loggers: ['mcp'], + readOnly: true, + telemetry: 'disabled', + }); + + this.runner = new InMemoryRunner({ + userConfig: mcpConfig, + createConnectionManager: () => Promise.resolve(this.connectionManager), + connectionErrorHandler: createConnectionErrorHandler(), + additionalLoggers: [new ToolsLogger(Keychain.root, logger)], + telemetryProperties: { + hosting_mode: 'compass', + }, + metrics: new NoopMetrics(), + }); + + this.connectionManager = new ToolsConnectionManager({ + logger: this.runner.logger, + getTelemetryAnonymousId: () => this.telemetryAnonymousId, + }); + + this.disposable = serveToolsRequests(port, (method, params, signal) => + this.handleRequest(method, params, signal) + ); + } + + private get server(): Server | undefined { + return this.runner.server; + } + + private async ensureServerStarted(): Promise { + if (!this.runner.server) { + await this.runner.start(); + } + const server = this.runner.server!; + // The MCP server registers its tools lazily, on first use + if (server.tools.length === 0) { + server.registerTools(); + } + return server; + } + + private async handleRequest( + method: ToolsRpcMethod, + params: unknown, + signal: AbortSignal + ): Promise { + switch (method) { + case 'setUserConfig': { + const { enableMCPTelemetry, maxTimeMS, telemetryAnonymousId } = + params as { + enableMCPTelemetry: boolean; + maxTimeMS?: number; + telemetryAnonymousId?: string; + }; + this.runner.userConfig.telemetry = enableMCPTelemetry + ? 'enabled' + : 'disabled'; + this.runner.userConfig.maxTimeMS = maxTimeMS; + if (telemetryAnonymousId !== undefined) { + this.telemetryAnonymousId = telemetryAnonymousId; + } + return; + } + case 'listDatabaseTools': { + await this.ensureServerStarted(); + const readonlyNames = new Set( + READ_ONLY_DATABASE_TOOLS.map((tool) => tool.name) + ); + return (this.server?.tools ?? []) + .filter((toolBase) => readonlyNames.has(toolBase.name)) + .map((toolBase) => ({ + name: toolBase.name, + description: toolBase.description, + inputSchema: z.toJSONSchema( + z.object( + Object.fromEntries( + Object.entries(toolBase.argsShape).map(([key, value]) => [ + key, + removeZodTransforms(value), + ]) + ) + ) + ), + })); + } + case 'performToolCall': { + const { + toolName, + toolArgs, + connectParams: encodedConnectParams, + } = params as { + toolName: string; + toolArgs: unknown; + connectParams: ConnectParamsWire; + }; + const connectParams = decodeConnectParams(encodedConnectParams); + await this.ensureServerStarted(); + const toolBase = this.server?.tools.find((t) => t.name === toolName); + if (!toolBase) { + throw new Error(`Unknown tool: ${toolName}`); + } + try { + await this.connectionManager.connectToCompassConnection( + connectParams + ); + } catch (error: any) { + this.logger.log.error( + this.logger.mongoLogId(1_001_000_410), + 'ToolsHost', + 'Error when attempting to connect to Compass connection before executing tool', + { error: error.message } + ); + throw error; + } + try { + return await toolBase.invoke( + toolArgs as Parameters[0], + { signal } + ); + } finally { + await this.connectionManager.disconnect(); + } + } + case 'stopServer': { + await this.runner.server?.close(); + return; + } + default: + throw new Error(`Unknown method: ${method}`); + } + } + + async stop(): Promise { + // Aborts requests still in flight and stops answering new ones + this.disposable[Symbol.dispose](); + try { + await this.connectionManager.disconnect(); + await this.runner.server?.close(); + } catch (error: any) { + this.logger.log.error( + this.logger.mongoLogId(1_001_000_446), + 'ToolsHost', + 'Error when attempting to stop the MCP server', + { error: error.message } + ); + } + } +} diff --git a/packages/compass-utils/src/utility-process.spec.ts b/packages/compass-utils/src/utility-process.spec.ts new file mode 100644 index 00000000000..28c809cf798 --- /dev/null +++ b/packages/compass-utils/src/utility-process.spec.ts @@ -0,0 +1,56 @@ +import { expect } from 'chai'; +import sinon from 'sinon'; +import { openUtilityPort, utilityPortChannel } from './utility-process'; + +describe('openUtilityPort', function () { + let sandbox: sinon.SinonSandbox; + let originalWindow: unknown; + + beforeEach(function () { + sandbox = sinon.createSandbox(); + originalWindow = (globalThis as any).window; + }); + + afterEach(function () { + if (originalWindow === undefined) { + delete (globalThis as any).window; + } else { + (globalThis as any).window = originalWindow; + } + sandbox.restore(); + }); + + function stubMessageChannel() { + const port1 = { start: sinon.stub(), postMessage: sinon.stub() }; + const port2 = {}; + sandbox.replace( + globalThis, + 'MessageChannel', + sinon.stub().returns({ port1, port2 }) as unknown as typeof MessageChannel + ); + const postMessage = sinon.stub(); + (globalThis as any).window = { postMessage }; + return { port1, port2, postMessage }; + } + + it('hands the other end to the preload on the utility channel', function () { + const { port2, postMessage } = stubMessageChannel(); + + openUtilityPort('ai-assistant'); + + expect(postMessage).to.have.been.calledOnceWith( + { type: utilityPortChannel('ai-assistant') }, + '*', + [port2] + ); + }); + + it('starts the port it returns', function () { + const { port1 } = stubMessageChannel(); + + const port = openUtilityPort('ai-assistant'); + + expect(port).to.equal(port1); + expect(port1.start).to.have.been.calledOnce; + }); +}); diff --git a/packages/compass-utils/src/utility-process.ts b/packages/compass-utils/src/utility-process.ts index 2d6b29f79a4..8398a083a7e 100644 --- a/packages/compass-utils/src/utility-process.ts +++ b/packages/compass-utils/src/utility-process.ts @@ -26,5 +26,6 @@ export function isUtilityPortChannel( export function openUtilityPort(name: string): MessagePort { const { port1, port2 } = new MessageChannel(); window.postMessage({ type: utilityPortChannel(name) }, '*', [port2]); + port1.start(); return port1; } diff --git a/packages/compass/src/main/application.ts b/packages/compass/src/main/application.ts index 6281b22aced..81d087da11c 100644 --- a/packages/compass/src/main/application.ts +++ b/packages/compass/src/main/application.ts @@ -42,7 +42,7 @@ import { handleSquirrelWindowsStartup } from './squirrel-startup'; const { debug, log, mongoLogId } = createLogger('COMPASS-MAIN'); const track = createIpcTrack(); -const UTILITIES = ['embedded-shell']; +const UTILITIES = ['embedded-shell', 'ai-assistant']; type ExitHandler = () => Promise; type CompassApplicationMode = 'CLI' | 'GUI'; diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts new file mode 100644 index 00000000000..c8af71061c1 --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -0,0 +1,48 @@ +import process from 'node:process'; + +import type { ParentPort } from 'electron'; +import { listen } from '@mongodb-js/compass-utils'; +import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; +import { createUtilityLogger } from '../logging.mts'; +import { onPort } from '../ipc.mts'; + +export const name = 'ai-assistant'; + +export function main(parentPort: ParentPort): Disposable { + using ds = new DisposableStack(); + + ds.use( + listen(process, 'unhandledRejection', (reason) => { + throw reason; + }) + ); + + const logger = createUtilityLogger(parentPort, name); + + ds.use( + onPort(parentPort, (port) => { + // One host per renderer: it owns the MCP server and the driver + // connections for that renderer's tool calls. When the renderer goes + // away (crash included), its port closes and the host is torn down. + // Not tracked in `ds`: ports arrive after main() returned and the + // stack moved out; the listener dies with its closed port. + const host = new ToolsHost(port, { logger }); + listen(port, 'close', () => { + void host.stop().catch((error) => { + logger.log.error( + logger.mongoLogId(1_001_000_447), + 'ai-assistant', + 'Error when stopping the tools host', + { error: error instanceof Error ? error.message : String(error) } + ); + }); + }); + }) + ); + + return ds.move(); +} + +if (import.meta.main) { + main(process.parentPort); +} diff --git a/packages/compass/src/utilities/ai-assistant/index.spec.mts b/packages/compass/src/utilities/ai-assistant/index.spec.mts new file mode 100644 index 00000000000..3e72634e390 --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.spec.mts @@ -0,0 +1,91 @@ +import process from 'node:process'; +import { EventEmitter } from 'node:events'; +import { expect } from 'chai'; +import type { MessagePortMain, ParentPort } from 'electron'; +import { main } from './index.mts'; + +function createFakePort(): MessagePortMain & { sent: unknown[] } { + const sent: unknown[] = []; + return Object.assign(new EventEmitter(), { + sent, + postMessage: (message: unknown) => sent.push(message), + start: () => {}, + }) as unknown as MessagePortMain & { sent: unknown[] }; +} + +describe('ai-assistant utility', function () { + let parentPort: ParentPort; + let utility: Disposable; + let sentToMain: { channel: string; data: { line: string } }[]; + + beforeEach(function () { + sentToMain = []; + parentPort = Object.assign(new EventEmitter(), { + postMessage: (message: (typeof sentToMain)[number]) => + sentToMain.push(message), + }); + utility = main(parentPort); + }); + + afterEach(function () { + utility[Symbol.dispose](); + }); + + it('logs to main through parentPort', function () { + expect(sentToMain).to.have.lengthOf(1); + expect(sentToMain[0].channel).to.equal('compass:log'); + const entry = JSON.parse(sentToMain[0].data.line); + expect(entry).to.include({ + s: 'I', + c: 'AI-ASSISTANT', + id: 1_001_000_442, + ctx: 'Utility', + msg: 'Started', + }); + expect(entry.attr).to.include({ pid: process.pid, ppid: process.ppid }); + expect(entry.attr.startupMs).to.be.a('number'); + }); + + it('serves rpc requests arriving on a handed-over port', async function () { + this.timeout(10_000); + const port = createFakePort(); + parentPort.emit('message', { ports: [port] }); + + port.emit('message', { + data: { + kind: 'request', + id: 1, + method: 'listDatabaseTools', + params: null, + }, + }); + + const deadline = Date.now() + 9_000; + while (port.sent.length === 0 && Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + expect(port.sent).to.have.lengthOf(1); + const response = port.sent[0] as { + kind: string; + id: number; + ok: boolean; + result: { name: string }[]; + }; + expect(response).to.include({ kind: 'response', id: 1, ok: true }); + expect(response.result.map((tool) => tool.name)).to.include('find'); + }); + + it('stops serving requests when the port closes', function () { + const port = createFakePort(); + parentPort.emit('message', { ports: [port] }); + expect(port.listenerCount('message')).to.be.greaterThan(0); + + port.emit('close'); + + // The host releases its listener as the port closes, so nothing can + // answer requests for this renderer any more + expect(port.listenerCount('message')).to.equal(0); + expect(port.sent).to.have.lengthOf(0); + }); +}); diff --git a/packages/compass/webpack.config.js b/packages/compass/webpack.config.js index 74ade315c44..8edcbb12c81 100644 --- a/packages/compass/webpack.config.js +++ b/packages/compass/webpack.config.js @@ -63,6 +63,11 @@ module.exports = (_env, args) => { const utilitiesConfig = withoutStartElectron( createElectronMainConfig({ ...opts, + // Utilities are never coverage instrumented: istanbul's preamble calls + // `new Function('return this')`, and utility processes are forked with + // NODE_OPTIONS forbidding code generation from strings (application.ts), + // so an instrumented utility crashes on load. + coverage: '', entry: utilityEntries, outputFilename: '[name].mjs', }) @@ -200,13 +205,34 @@ module.exports = (_env, args) => { 'web-worker', ].map((name) => [name, `node-commonjs ${name}`]) ), - plugins: [ - new webpack.EnvironmentPlugin(hadronEnvConfig), - // Main forks one file per utility - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: Object.keys(utilityEntries).length, - }), - ], + resolve: { + alias: { + // Same as the renderer: Atlas Local is a native dependency the app + // does not ship for this flow, and bundling it only adds megabytes + '@mongodb-js/atlas-local': false, + }, + }, + // Utilities share the heavy Node-side dependencies (the driver, the MCP + // server, their transitive graph) instead of every utility bundling its + // own copy. + optimization: { + splitChunks: { + chunks: 'all', + cacheGroups: { + // Only the sharing rule: everything else stays in the utility that + // uses it, so this does not fragment the output + default: false, + defaultVendors: false, + utilitiesVendor: { + name: 'utilities-vendor', + chunks: 'all', + minChunks: 2, + reuseExistingChunk: true, + }, + }, + }, + }, + plugins: [new webpack.EnvironmentPlugin(hadronEnvConfig)], }), merge(preloadConfig, { name: 'preload', From ea7e12f3e75708f89cb5dd33caa27cfd4df89e6d Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 18:48:16 +0000 Subject: [PATCH 02/16] fix log ids --- .../src/tools/infrastructure/tools-host.ts | 2 +- packages/compass/src/utilities/ai-assistant/index.mts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 97a44d3c2d2..8764bf15424 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -149,7 +149,7 @@ export class ToolsHost { ); } catch (error: any) { this.logger.log.error( - this.logger.mongoLogId(1_001_000_410), + this.logger.mongoLogId(1_001_000_453), 'ToolsHost', 'Error when attempting to connect to Compass connection before executing tool', { error: error.message } diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts index c8af71061c1..c55454a63de 100644 --- a/packages/compass/src/utilities/ai-assistant/index.mts +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -30,7 +30,7 @@ export function main(parentPort: ParentPort): Disposable { listen(port, 'close', () => { void host.stop().catch((error) => { logger.log.error( - logger.mongoLogId(1_001_000_447), + logger.mongoLogId(1_001_000_454), 'ai-assistant', 'Error when stopping the tools host', { error: error instanceof Error ? error.message : String(error) } From ec7d9d6e331eb5bf30dafee5cd0fa85054eb9892 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 19:17:50 +0000 Subject: [PATCH 03/16] copilot comment --- .../src/utilities/ai-assistant/index.mts | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts index c55454a63de..d2c37625d67 100644 --- a/packages/compass/src/utilities/ai-assistant/index.mts +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -19,24 +19,35 @@ export function main(parentPort: ParentPort): Disposable { const logger = createUtilityLogger(parentPort, name); + const hosts = new Set(); + ds.defer(() => { + for (const host of hosts) { + void host.stop(); + } + }); + ds.use( onPort(parentPort, (port) => { - // One host per renderer: it owns the MCP server and the driver - // connections for that renderer's tool calls. When the renderer goes - // away (crash included), its port closes and the host is torn down. - // Not tracked in `ds`: ports arrive after main() returned and the - // stack moved out; the listener dies with its closed port. const host = new ToolsHost(port, { logger }); - listen(port, 'close', () => { - void host.stop().catch((error) => { - logger.log.error( - logger.mongoLogId(1_001_000_454), - 'ai-assistant', - 'Error when stopping the tools host', - { error: error instanceof Error ? error.message : String(error) } - ); - }); - }); + hosts.add(host); + listen( + port, + 'close', + () => { + hosts.delete(host); + void host.stop().catch((error) => { + logger.log.error( + logger.mongoLogId(1_001_000_454), + 'ai-assistant', + 'Error when stopping the tools host', + { + error: error instanceof Error ? error.message : String(error), + } + ); + }); + }, + { once: true } + ); }) ); From 386dbdd510c8d569938cc80f1e13303a1970569e Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Thu, 29 Oct 2026 12:05:35 +0000 Subject: [PATCH 04/16] fix app bundle size --- packages/compass-generative-ai/.eslintrc.js | 30 +++++++++++++++++++ .../src/tools-connection-manager.spec.ts | 1 + .../src/tools-connection-manager.ts | 11 ++++--- .../src/tools-controller.ts | 1 + .../src/tools/infrastructure/tools-host.ts | 1 + 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/compass-generative-ai/.eslintrc.js b/packages/compass-generative-ai/.eslintrc.js index 0b379d7a576..f3a05b276e3 100644 --- a/packages/compass-generative-ai/.eslintrc.js +++ b/packages/compass-generative-ai/.eslintrc.js @@ -6,4 +6,34 @@ module.exports = { tsconfigRootDir: __dirname, project: ['./tsconfig.json'], }, + overrides: [ + { + // Files in this directory run outside the renderer: in the AI assistant + // utility process on desktop, and in the page for compass-web. Renderer + // entries (the provider subpaths, the design system) would drag the UI + // stack into the utility bundle, so utility-side code takes its + // cross-world dependencies via config instead. + files: ['src/tools/infrastructure/**'], + excludedFiles: ['**/*.spec.ts'], + rules: { + '@typescript-eslint/no-restricted-imports': [ + 'error', + { + patterns: [ + { + group: [ + '@mongodb-js/compass-logging/provider', + '@mongodb-js/compass-telemetry/provider', + '@mongodb-js/compass-components', + '@mongodb-js/compass-app-registry', + ], + message: + 'Utility-side code must not import renderer entries; take what it needs via config.', + }, + ], + }, + ], + }, + }, + ], }; diff --git a/packages/compass-generative-ai/src/tools-connection-manager.spec.ts b/packages/compass-generative-ai/src/tools-connection-manager.spec.ts index 336c5785d71..e362daf9616 100644 --- a/packages/compass-generative-ai/src/tools-connection-manager.spec.ts +++ b/packages/compass-generative-ai/src/tools-connection-manager.spec.ts @@ -35,6 +35,7 @@ describe('ToolsConnectionManager', function () { connectionManager = new ToolsConnectionManager({ logger, getTelemetryAnonymousId, + mongoLogId: (id) => ({ __value: id }), }); }); diff --git a/packages/compass-generative-ai/src/tools-connection-manager.ts b/packages/compass-generative-ai/src/tools-connection-manager.ts index 44466163b0a..9b63236066c 100644 --- a/packages/compass-generative-ai/src/tools-connection-manager.ts +++ b/packages/compass-generative-ai/src/tools-connection-manager.ts @@ -11,7 +11,6 @@ import { import type { ServiceProvider } from '@mongosh/service-provider-core'; import { isAtlas, isAtlasStream } from '@mongodb-js/mongodb-build-info'; import ConnectionString from 'mongodb-connection-string-url'; -import { mongoLogId } from '@mongodb-js/compass-logging/provider'; export interface ToolsConnectParams { connectionId: string; @@ -25,11 +24,13 @@ const DEFAULT_TELEMETRY_APP_NAME = 'MongoDB Compass'; type ToolsConnectionManagerConfig = { logger: LoggerBase; getTelemetryAnonymousId: () => string; + mongoLogId: (id: number) => { __value: number }; }; export class ToolsConnectionManager extends ConnectionManager { private logger: LoggerBase; private getTelemetryAnonymousId: () => string; + private mongoLogId: ToolsConnectionManagerConfig['mongoLogId']; private activeConnection: { id: string; provider: ServiceProvider; @@ -38,10 +39,12 @@ export class ToolsConnectionManager extends ConnectionManager { constructor({ logger, getTelemetryAnonymousId, + mongoLogId, }: ToolsConnectionManagerConfig) { super(); this.logger = logger; this.getTelemetryAnonymousId = getTelemetryAnonymousId; + this.mongoLogId = mongoLogId; } override connect(): Promise { @@ -62,7 +65,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www. await this.activeConnection?.provider?.close(); } catch (error) { this.logger.error({ - id: mongoLogId(1_001_000_411), + id: this.mongoLogId(1_001_000_411), context: 'compass-tools-connection-manager', message: `Error disconnecting from Compass connection - ${ error instanceof Error ? error.message : String(error) @@ -93,7 +96,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www. if (isAtlasStream(connectParams.connectionString)) { this.logger.warning({ - id: mongoLogId(1_001_000_413), + id: this.mongoLogId(1_001_000_413), context: 'compass-tools-connection-manager', message: 'Attempting a connection to an Atlas Stream.', }); @@ -129,7 +132,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www. ); } catch (error) { this.logger.error({ - id: mongoLogId(1_001_000_412), + id: this.mongoLogId(1_001_000_412), context: 'compass-tools-connection-manager', message: `Error connecting to Compass connection - ${ error instanceof Error ? error.message : String(error) diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 29403be0fd5..bc8e0529481 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -139,6 +139,7 @@ export class ToolsController { this.connectionManager = new ToolsConnectionManager({ logger: this.runner.logger, getTelemetryAnonymousId, + mongoLogId: this.logger.mongoLogId.bind(this.logger), }); // Set up listener on close event to perform a cleanup when the 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 8764bf15424..9852a500b20 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -60,6 +60,7 @@ export class ToolsHost { this.connectionManager = new ToolsConnectionManager({ logger: this.runner.logger, getTelemetryAnonymousId: () => this.telemetryAnonymousId, + mongoLogId: this.logger.mongoLogId.bind(this.logger), }); this.disposable = serveToolsRequests(port, (method, params, signal) => From c7dee30e9960f3f6603da90b30e06a189e2cfd04 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 18:31:32 +0000 Subject: [PATCH 05/16] feat: ai assistant utility --- package-lock.json | 2 +- packages/compass-generative-ai/package.json | 8 +- .../infrastructure/connect-params.spec.ts | 99 +++++++++ .../tools/infrastructure/connect-params.ts | 60 ++++++ .../tools/infrastructure/in-memory-runner.ts | 28 +++ .../src/tools/infrastructure/messages.ts | 69 +++++++ .../src/tools/infrastructure/noop-metrics.ts | 17 ++ .../tools/infrastructure/rpc-client.spec.ts | 182 +++++++++++++++++ .../src/tools/infrastructure/rpc-client.ts | 98 +++++++++ .../src/tools/infrastructure/server.ts | 77 +++++++ .../tools/infrastructure/tools-host.spec.ts | 102 ++++++++++ .../src/tools/infrastructure/tools-host.ts | 192 ++++++++++++++++++ .../compass-utils/src/utility-process.spec.ts | 56 +++++ packages/compass-utils/src/utility-process.ts | 1 + packages/compass/src/main/application.ts | 2 +- .../src/utilities/ai-assistant/index.mts | 48 +++++ .../src/utilities/ai-assistant/index.spec.mts | 91 +++++++++ packages/compass/webpack.config.js | 40 +++- 18 files changed, 1160 insertions(+), 12 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 create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/messages.ts create mode 100644 packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.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-utils/src/utility-process.spec.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/package-lock.json b/package-lock.json index 072b3a26f63..57ab14bf7c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55322,6 +55322,7 @@ "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.0", "ai": "^7.0.0", + "bson": "^7.3.2", "ipaddr.js": "^2.5.0", "mongodb": "^7.5.0", "mongodb-connection-string-url": "^7.0.2", @@ -55347,7 +55348,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^3.35.0", - "bson": "^7.3.2", "chai": "^4.3.6", "depcheck": "^1.4.1", "electron-mocha": "^12.2.0", diff --git a/packages/compass-generative-ai/package.json b/packages/compass-generative-ai/package.json index 2a1fb9dd999..926f4021b2c 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": { @@ -71,6 +73,7 @@ "@mongosh/service-provider-core": "^5.2.0", "@mongosh/service-provider-node-driver": "^5.2.0", "ai": "^7.0.0", + "bson": "^7.3.2", "ipaddr.js": "^2.5.0", "mongodb": "^7.5.0", "mongodb-connection-string-url": "^7.0.2", @@ -96,7 +99,6 @@ "@types/sinon-chai": "^3.2.5", "autoevals": "^0.4.0", "braintrust": "^3.35.0", - "bson": "^7.3.2", "chai": "^4.3.6", "depcheck": "^1.4.1", "electron-mocha": "^12.2.0", diff --git a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts new file mode 100644 index 00000000000..91c9405faf8 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.spec.ts @@ -0,0 +1,99 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import { Binary, UUID } from 'bson'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; +import { decodeConnectParams, encodeConnectParams } from './connect-params'; +import type { ConnectParamsWire } from './connect-params'; + +describe('connect params over the port', function () { + let channel: MessageChannel; + + beforeEach(function () { + channel = new MessageChannel(); + }); + + afterEach(function () { + // Open ports keep the event loop alive and would stop the suite exiting + channel.port1.close(); + channel.port2.close(); + }); + + async function overThePort(wire: ConnectParamsWire) { + const received = new Promise((resolve) => { + // worker MessagePorts emit the value itself, unlike the EventTarget + // shape `listen` handles + channel.port2.on('message', (value: ConnectParamsWire) => resolve(value)); + }); + channel.port1.postMessage(wire); + return await received; + } + + it('restores BSON values passed through a MessagePort', async function () { + // Structured cloning keeps the values but drops their prototypes: this is + // the case the encoding exists for, so it is asserted through a real port. + const connectParams = { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27017', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + autoEncryption: { + keyVaultNamespace: 'encryption.__keyVault', + encryptedFieldsMap: { + 'hr.employees': { + fields: [ + { + path: 'taxid', + keyId: new Binary( + Buffer.from('a21ddc6a880643849fdf8ba02a767b5f', 'hex'), + Binary.SUBTYPE_UUID + ), + }, + ], + }, + }, + kmsProviders: { local: { key: new UUID() } }, + }, + }, + } as unknown as ToolsConnectParams; + + const decoded = decodeConnectParams( + await overThePort(encodeConnectParams(connectParams)) + ); + const autoEncryption = ( + decoded.connectOptions as { + autoEncryption: { + encryptedFieldsMap: Record; + kmsProviders: { local: { key: unknown } }; + }; + } + ).autoEncryption; + + const keyId = autoEncryption.encryptedFieldsMap['hr.employees'].fields[0] + .keyId as Binary; + expect(keyId).to.be.instanceOf(Binary); + expect(keyId.buffer).to.deep.equal( + Buffer.from('a21ddc6a880643849fdf8ba02a767b5f', 'hex') + ); + // UUID extends Binary, so the more specific type has to win + expect(autoEncryption.kmsProviders.local.key).to.be.instanceOf(UUID); + }); + + it('leaves non-BSON values alone', async function () { + const connectParams: ToolsConnectParams = { + connectionId: 'c1', + connectionString: 'mongodb://localhost:27017', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + serverSelectionTimeoutMS: 500, + }, + }; + + const decoded = decodeConnectParams( + await overThePort(encodeConnectParams(connectParams)) + ); + + expect(decoded).to.deep.equal(connectParams); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts new file mode 100644 index 00000000000..a528548fa34 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/connect-params.ts @@ -0,0 +1,60 @@ +import { Binary, UUID } from 'bson'; +import type { ToolsConnectParams } from '../../tools-connection-manager'; + +const BSON_TYPES = { UUID, Binary } as const; + +/** + * Structured cloning across the utility port keeps BSON values but drops their + * prototypes, so the driver would receive plain objects for e.g. the `keyId` + * of an encrypted field. + */ +export type ConnectParamsWire = { + connectParams: ToolsConnectParams; + bsonValues: Map; +}; + +export function encodeConnectParams( + connectParams: ToolsConnectParams +): ConnectParamsWire { + const bsonValues = new Map(); + collectBsonValues(connectParams, bsonValues, new Set()); + return { connectParams, bsonValues }; +} + +export function decodeConnectParams({ + connectParams, + bsonValues, +}: ConnectParamsWire): ToolsConnectParams { + for (const [value, typeName] of bsonValues) { + const bsonType = BSON_TYPES[typeName as keyof typeof BSON_TYPES]; + if (bsonType && typeof value === 'object' && value !== null) { + Object.setPrototypeOf(value, bsonType.prototype); + } + } + return connectParams; +} + +function collectBsonValues( + value: unknown, + found: Map, + seen: Set +): void { + if (typeof value !== 'object' || value === null || seen.has(value)) { + return; + } + + for (const [name, bsonType] of Object.entries(BSON_TYPES)) { + if (value instanceof bsonType) { + found.set(value, name); + return; + } + } + + seen.add(value); + + for (const entryValue of Array.isArray(value) + ? value + : Object.values(value)) { + collectBsonValues(entryValue, found, seen); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts b/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts new file mode 100644 index 00000000000..b192c4280a2 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/in-memory-runner.ts @@ -0,0 +1,28 @@ +import { TransportRunnerBase } from 'mongodb-mcp-server'; +import type { + Server, + TransportRunnerConfig, + UserConfig, +} from 'mongodb-mcp-server'; + +/** + * In-memory MCP runner that doesn't bind to any external transport. + * The server is kept in memory for direct programmatic access. + */ +export class InMemoryRunner extends TransportRunnerBase { + public server?: Server; + public readonly userConfig: UserConfig; + + constructor(config: TransportRunnerConfig) { + super(config); + this.userConfig = config.userConfig; + } + + async start(): Promise { + this.server = await this.createServer(); + } + + async closeTransport(): Promise { + await this.server?.close(); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/messages.ts b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts new file mode 100644 index 00000000000..1ed8adfa5ed --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/messages.ts @@ -0,0 +1,69 @@ +import type { EventSource } from '@mongodb-js/compass-utils'; + +export type ToolsRpcPort = { + postMessage(message: unknown): void; + close(): void; +} & EventSource; + +export type ToolsRpcMethod = + /** + * Keeps the MCP server's runtime config in sync with Compass preferences. + * Sent by the renderer every time the assistant context changes. It replaces + * the direct `runner.userConfig` mutation `ToolsController.setContext` used + * to do when the server lived in the renderer. + */ + | 'setUserConfig' + /** + * Fetches the read-only database tool definitions: name, description and + * input schema as JSON Schema. Sent once when the connection is established, + * doubling as the handshake; the result is cached renderer-side so + * `getActiveTools()` can stay synchronous. + */ + | 'listDatabaseTools' + /** + * Executes one database tool. Sent by the renderer per AI tool call. + * The utility connects the driver to the given connection, invokes the tool + * with the request's abort signal (identified via the abort-for-id pattern, + * see `rpc-client.ts`), disconnects in `finally`, and resolves with the + * tool result, which is plain data. + */ + | 'performToolCall' + /** + * Closes the MCP server. Sent on provider unmount / window close. The + * utility additionally tears down a host when its port closes, which covers + * a renderer crash that can never deliver this message. + */ + | 'stopServer'; + +export type ToolsRpcRequest = { + kind: 'request'; + id: number; + method: ToolsRpcMethod; + params: unknown; +}; + +export type ToolsRpcResponse = { kind: 'response'; id: number } & ( + { ok: true; result: unknown } | { ok: false; error: string } +); + +export type ToolsRpcAbort = { kind: 'abort'; id: number }; + +export type ToolsRpcMessage = + ToolsRpcRequest | ToolsRpcResponse | ToolsRpcAbort; + +export function isToolsRpcMessage(value: unknown): value is ToolsRpcMessage { + return ( + typeof value === 'object' && + value !== null && + 'kind' in value && + ['request', 'response', 'abort'].includes(value.kind as string) + ); +} + +export function isResponse(value: unknown): value is ToolsRpcResponse { + return ( + typeof value === 'object' && + value !== null && + (value as { kind: unknown }).kind === 'response' + ); +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts b/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts new file mode 100644 index 00000000000..e7e43da0297 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/noop-metrics.ts @@ -0,0 +1,17 @@ +import type { DefaultMetrics, Metrics } from 'mongodb-mcp-server'; + +class NoopDefaultMetrics { + observe(): void { + // no-op + } +} + +export class NoopMetrics implements Metrics { + constructor() {} + get(_key: K): DefaultMetrics[K] { + return new NoopDefaultMetrics() as unknown as DefaultMetrics[K]; + } + getMetrics(): Promise { + return Promise.resolve(''); + } +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts new file mode 100644 index 00000000000..c87116fbfd9 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts @@ -0,0 +1,182 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import sinon from 'sinon'; +import { createToolsRpcClient } from './rpc-client'; +import { serveToolsRequests } from './server'; + +describe('tools rpc', function () { + let channel: MessageChannel; + + beforeEach(function () { + channel = new MessageChannel(); + }); + + afterEach(function () { + channel.port1.close(); + channel.port2.close(); + }); + + it('round-trips a request to a response', async function () { + const client = createToolsRpcClient(channel.port1); + const params = { enableMCPTelemetry: true, maxTimeMS: 5 }; + serveToolsRequests(channel.port2, async (method, sentParams, _signal) => { + expect(method).to.equal('setUserConfig'); + return Promise.resolve(sentParams); + }); + + expect(await client.call('setUserConfig', params)).to.deep.equal(params); + }); + + it('rejects with the error message from the other side', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, () => { + throw new Error('boom'); + }); + + const error = await client + .call('performToolCall', { toolName: 'find' }) + .catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'boom'); + }); + + it('propagates abort to the server-side signal', async function () { + const client = createToolsRpcClient(channel.port1); + const controller = new AbortController(); + let serverSignal: AbortSignal | undefined; + + // resolves only once the server side processed the abort message + const serverAborted = new Promise((resolve) => { + serveToolsRequests(channel.port2, (_method, _params, signal) => { + serverSignal = signal; + return new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => { + reject(signal.reason); + resolve(); + }); + }); + }); + }); + + const pending = client.call( + 'performToolCall', + undefined, + controller.signal + ); + controller.abort(new Error('stop')); + + const error = await pending.catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'stop'); + await serverAborted; + expect(serverSignal?.aborted).to.equal(true); + }); + + it('rejects immediately for an already-aborted signal', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, async () => Promise.resolve('unused')); + + const error = await client + .call('performToolCall', undefined, AbortSignal.abort(new Error('nope'))) + .catch((err: Error) => err); + expect(error).to.be.instanceOf(Error).with.property('message', 'nope'); + }); + + it('resolves in request order regardless of completion order', async function () { + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, async (_method, params, _signal) => { + const { delay, name } = params as { delay: number; name: string }; + await new Promise((resolve) => setTimeout(resolve, delay)); + return name; + }); + + const slow = client.call('performToolCall', { delay: 50, name: 'slow' }); + const fast = client.call('performToolCall', { delay: 1, name: 'fast' }); + expect(await Promise.all([slow, fast])).to.deep.equal(['slow', 'fast']); + }); + + it('rejects pending calls when the client is closed', async function () { + const client = createToolsRpcClient(channel.port1); + // no server on the other end: the call stays pending until close() + const pending = client.call('listDatabaseTools'); + client.close(); + + const error = await pending.catch((err: Error) => err); + expect(error) + .to.be.instanceOf(Error) + .with.property('message', 'Tools rpc client closed'); + }); + + it('ignores messages that are not part of the protocol', async function () { + let called = 0; + const client = createToolsRpcClient(channel.port1); + serveToolsRequests(channel.port2, () => { + called += 1; + return Promise.resolve(null); + }); + channel.port1.postMessage({ hello: 'stray' }); + channel.port1.postMessage(null); + channel.port1.postMessage({ kind: 'nonsense' }); + + // Messages arrive in order, so a request that gets answered proves the + // strays were seen and ignored rather than queued behind it + await client.call('listDatabaseTools'); + + expect(called).to.equal(1); + }); + + it('closes the port when the client is closed', function () { + const close = sinon.spy(channel.port1, 'close'); + const client = createToolsRpcClient(channel.port1); + + client.close(); + + expect(close).to.have.been.calledOnce; + close.restore(); + }); + + it('does not send an abort message for a request that already settled', async function () { + const client = createToolsRpcClient(channel.port1); + const controller = new AbortController(); + const postMessage = sinon.spy(channel.port1, 'postMessage'); + serveToolsRequests(channel.port2, () => Promise.resolve('ok')); + + await client.call('performToolCall', undefined, controller.signal); + postMessage.resetHistory(); + + // Abort listeners run synchronously, so no waiting is needed to know + // whether the client still had one attached + controller.abort(new Error('too late')); + + expect(postMessage).to.not.have.been.called; + postMessage.restore(); + }); + + it('aborts in-flight requests when the server is disposed', async function () { + const client = createToolsRpcClient(channel.port1); + let serverSignal: AbortSignal | undefined; + // Resolved by the handler, so the test never has to guess how long a + // message takes to arrive + let requestArrived: () => void; + const arrived = new Promise((resolve) => { + requestArrived = resolve; + }); + const server = serveToolsRequests( + channel.port2, + (_method, _params, signal) => { + serverSignal = signal; + requestArrived(); + return new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => reject(signal.reason)); + }); + } + ); + + void client.call('performToolCall').catch(() => { + // the request is aborted below; the rejection is expected + }); + await arrived; + + server[Symbol.dispose](); + + expect(serverSignal?.aborted).to.equal(true); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts new file mode 100644 index 00000000000..5ae9142c88b --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts @@ -0,0 +1,98 @@ +import { listen } from '@mongodb-js/compass-utils'; +import { isResponse } from './messages'; +import type { + ToolsRpcAbort, + ToolsRpcMethod, + ToolsRpcPort, + ToolsRpcRequest, +} from './messages'; + +/** + * Renderer-side client. `port` is the end of the channel returned by + * `openUtilityPort`. + */ +export function createToolsRpcClient(port: ToolsRpcPort): { + call( + method: ToolsRpcMethod, + params?: unknown, + signal?: AbortSignal + ): Promise; + close(): void; +} { + let nextId = 0; + const pending = new Map< + number, + { + resolve: (value: unknown) => void; + reject: (reason: unknown) => void; + /** Removes the abort listener the call installed, if any */ + stopListeningForAbort?: () => void; + } + >(); + + const settle = ( + id: number, + settleEntry: (entry: { + resolve: (value: unknown) => void; + reject: (reason: unknown) => void; + stopListeningForAbort?: () => void; + }) => void + ): void => { + const entry = pending.get(id); + if (!entry) return; + pending.delete(id); + entry.stopListeningForAbort?.(); + settleEntry(entry); + }; + + const stopListening = listen(port, 'message', (event: { data: unknown }) => { + const message = event.data; + if (!isResponse(message)) return; + + settle(message.id, (entry) => { + if (message.ok) entry.resolve(message.result); + else entry.reject(new Error(message.error)); + }); + }); + + return { + call(method, params, signal) { + const id = nextId++; + return new Promise((resolve, reject) => { + if (signal?.aborted) { + return reject( + signal.reason ?? new Error('The operation was aborted') + ); + } + pending.set(id, { resolve, reject }); + if (signal) { + const stopListeningForAbort = listen(signal, 'abort', () => { + settle(id, (entry) => { + port.postMessage({ kind: 'abort', id } satisfies ToolsRpcAbort); + entry.reject( + signal.reason ?? new Error('The operation was aborted') + ); + }); + }); + pending.get(id)!.stopListeningForAbort = () => + stopListeningForAbort[Symbol.dispose](); + } + port.postMessage({ + kind: 'request', + id, + method, + params, + } satisfies ToolsRpcRequest); + }); + }, + close() { + for (const id of [...pending.keys()]) { + settle(id, (entry) => + entry.reject(new Error('Tools rpc client closed')) + ); + } + stopListening[Symbol.dispose](); + port.close(); + }, + }; +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/server.ts b/packages/compass-generative-ai/src/tools/infrastructure/server.ts new file mode 100644 index 00000000000..ce2b2a253f3 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/server.ts @@ -0,0 +1,77 @@ +import { listen } from '@mongodb-js/compass-utils'; +import { isToolsRpcMessage } from './messages'; +import type { + ToolsRpcMethod, + ToolsRpcPort, + ToolsRpcResponse, +} from './messages'; + +/** + * Utility-side server. `port` is a `MessagePortMain` received through + * `onPort` (already started). Each request gets its own AbortController; + * abort messages for that id abort it. + */ +export function serveToolsRequests( + port: ToolsRpcPort, + handler: ( + method: ToolsRpcMethod, + params: unknown, + signal: AbortSignal + ) => Promise +): Disposable { + const controllers = new Map(); + + const stopListening = listen(port, 'message', (event: { data: unknown }) => { + const message = event.data; + if (!isToolsRpcMessage(message)) return; + + if (message.kind === 'abort') { + controllers.get(message.id)?.abort(); + return; + } + + if (message.kind !== 'request') return; + + const controller = new AbortController(); + + controllers.set(message.id, controller); + + // The handler may throw synchronously; route it through a promise chain + // so a throw becomes a rejection instead of an uncaught error that would + // take the utility process down. + Promise.resolve() + .then(() => handler(message.method, message.params, controller.signal)) + .then((result) => { + port.postMessage({ + kind: 'response', + id: message.id, + ok: true, + result, + } satisfies ToolsRpcResponse); + }) + .catch((error: unknown) => { + port.postMessage({ + kind: 'response', + id: message.id, + ok: false, + error: + error instanceof Error + ? error.message + : String((error as any)?.message ?? error), + } satisfies ToolsRpcResponse); + }) + .finally(() => { + controllers.delete(message.id); + }); + }); + + return { + [Symbol.dispose]() { + stopListening[Symbol.dispose](); + for (const controller of controllers.values()) { + controller.abort(); + } + controllers.clear(); + }, + }; +} diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts new file mode 100644 index 00000000000..712e02e3f74 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.spec.ts @@ -0,0 +1,102 @@ +import { MessageChannel } from 'node:worker_threads'; +import { expect } from 'chai'; +import sinon from 'sinon'; +import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; +import type { Logger } from '@mongodb-js/compass-logging'; +import { createToolsRpcClient } from './rpc-client'; +import { encodeConnectParams } from './connect-params'; +import { ToolsHost } from './tools-host'; +import type { ToolDefinitionInfo } from './tools-host'; + +describe('ToolsHost', function () { + // The MCP server starts synchronously enough for the in-memory runner, but + // registering the tools and the JSON Schema conversion give it some slack. + this.timeout(10_000); + + let channel: MessageChannel; + let host: ToolsHost; + let logger: Logger; + + beforeEach(function () { + channel = new MessageChannel(); + logger = createNoopLogger(); + host = new ToolsHost(channel.port2, { + logger, + }); + }); + + afterEach(async function () { + await host.stop(); + channel.port1.close(); + channel.port2.close(); + }); + + it('lists read-only database tools as JSON Schema', async function () { + const client = createToolsRpcClient(channel.port1); + const tools = (await client.call( + 'listDatabaseTools' + )) as ToolDefinitionInfo[]; + + const names = tools.map((t) => t.name).sort(); + expect(names).to.include('find'); + expect(names).to.include('aggregate'); + expect(names).to.not.include('connect'); + for (const tool of tools) { + expect(tool.description).to.be.a('string'); + expect(tool.inputSchema).to.have.property('type', 'object'); + } + }); + + it('returns an error result when the connection fails', async function () { + const client = createToolsRpcClient(channel.port1); + // nothing is listening on this port, so connecting fails and the MCP + // server surfaces that as an error result, not a rejection + const result = (await client.call('performToolCall', { + toolName: 'find', + toolArgs: { database: 'test', collection: 'coll' }, + connectParams: encodeConnectParams({ + connectionId: 'c1', + connectionString: 'mongodb://localhost:27019', + connectOptions: { + productName: 'test', + productDocsLink: 'http://example.com/docs', + // Fail fast when nothing is listening: the driver's default server + // selection timeout (30s) would outlive this suite's timeout. + serverSelectionTimeoutMS: 500, + }, + }), + })) as { content: unknown[]; isError?: boolean }; + + expect(result).to.be.an('object'); + expect(result.isError).to.equal(true); + }); + + it('applies setUserConfig to the runner config', async function () { + const client = createToolsRpcClient(channel.port1); + await client.call('setUserConfig', { + enableMCPTelemetry: true, + maxTimeMS: 1000, + }); + expect((host as any).runner.userConfig.telemetry).to.equal('enabled'); + expect((host as any).runner.userConfig.maxTimeMS).to.equal(1000); + }); + + it('releases the server and the connection when stopped', async function () { + const client = createToolsRpcClient(channel.port1); + await client.call('listDatabaseTools'); + const connectionManager = (host as any).connectionManager; + const disconnect = sinon.spy(connectionManager, 'disconnect'); + + await host.stop(); + + expect(disconnect).to.have.been.called; + const settled = await Promise.race([ + client + .call('listDatabaseTools') + .then(() => 'answered') + .catch(() => 'rejected'), + new Promise((resolve) => setTimeout(() => resolve('pending'), 100)), + ]); + expect(settled).to.equal('pending'); + }); +}); diff --git a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts new file mode 100644 index 00000000000..97a44d3c2d2 --- /dev/null +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -0,0 +1,192 @@ +import type { Logger } from '@mongodb-js/compass-logging'; +import { z } from 'zod/v4'; +import { Keychain, UserConfigSchema } from 'mongodb-mcp-server'; +import type { Server } from 'mongodb-mcp-server'; +import { createConnectionErrorHandler } from '../../tools-connection-error-handler'; +import { ToolsLogger } from '../../tools-logger'; +import { ToolsConnectionManager } from '../../tools-connection-manager'; +import { removeZodTransforms } from '../../remove-zod-transforms'; +import { READ_ONLY_DATABASE_TOOLS } from '../../available-tools'; +import type { ToolsRpcMethod, ToolsRpcPort } from './messages'; +import type { ConnectParamsWire } from './connect-params'; +import { decodeConnectParams } from './connect-params'; +import { serveToolsRequests } from './server'; +import { InMemoryRunner } from './in-memory-runner'; +import { NoopMetrics } from './noop-metrics'; + +export type ToolDefinitionInfo = { + name: string; + description: string; + inputSchema: Record; +}; + +type ToolsHostConfig = { + logger: Logger; +}; + +/** + * Utility-process side of the assistant database tools. Owns the MCP server, + * its connection manager, and the driver (moved here from ToolsController + * when the server moved out of the renderer). Exposes the protocol methods + * from `./messages` over the port the utility receives for each renderer. + */ +export class ToolsHost { + private logger: Logger; + private readonly runner: InMemoryRunner; + private connectionManager: ToolsConnectionManager; + private readonly disposable: Disposable; + private telemetryAnonymousId = ''; + + constructor(port: ToolsRpcPort, { logger }: ToolsHostConfig) { + this.logger = logger; + const mcpConfig = UserConfigSchema.parse({ + disabledTools: ['connect'], + loggers: ['mcp'], + readOnly: true, + telemetry: 'disabled', + }); + + this.runner = new InMemoryRunner({ + userConfig: mcpConfig, + createConnectionManager: () => Promise.resolve(this.connectionManager), + connectionErrorHandler: createConnectionErrorHandler(), + additionalLoggers: [new ToolsLogger(Keychain.root, logger)], + telemetryProperties: { + hosting_mode: 'compass', + }, + metrics: new NoopMetrics(), + }); + + this.connectionManager = new ToolsConnectionManager({ + logger: this.runner.logger, + getTelemetryAnonymousId: () => this.telemetryAnonymousId, + }); + + this.disposable = serveToolsRequests(port, (method, params, signal) => + this.handleRequest(method, params, signal) + ); + } + + private get server(): Server | undefined { + return this.runner.server; + } + + private async ensureServerStarted(): Promise { + if (!this.runner.server) { + await this.runner.start(); + } + const server = this.runner.server!; + // The MCP server registers its tools lazily, on first use + if (server.tools.length === 0) { + server.registerTools(); + } + return server; + } + + private async handleRequest( + method: ToolsRpcMethod, + params: unknown, + signal: AbortSignal + ): Promise { + switch (method) { + case 'setUserConfig': { + const { enableMCPTelemetry, maxTimeMS, telemetryAnonymousId } = + params as { + enableMCPTelemetry: boolean; + maxTimeMS?: number; + telemetryAnonymousId?: string; + }; + this.runner.userConfig.telemetry = enableMCPTelemetry + ? 'enabled' + : 'disabled'; + this.runner.userConfig.maxTimeMS = maxTimeMS; + if (telemetryAnonymousId !== undefined) { + this.telemetryAnonymousId = telemetryAnonymousId; + } + return; + } + case 'listDatabaseTools': { + await this.ensureServerStarted(); + const readonlyNames = new Set( + READ_ONLY_DATABASE_TOOLS.map((tool) => tool.name) + ); + return (this.server?.tools ?? []) + .filter((toolBase) => readonlyNames.has(toolBase.name)) + .map((toolBase) => ({ + name: toolBase.name, + description: toolBase.description, + inputSchema: z.toJSONSchema( + z.object( + Object.fromEntries( + Object.entries(toolBase.argsShape).map(([key, value]) => [ + key, + removeZodTransforms(value), + ]) + ) + ) + ), + })); + } + case 'performToolCall': { + const { + toolName, + toolArgs, + connectParams: encodedConnectParams, + } = params as { + toolName: string; + toolArgs: unknown; + connectParams: ConnectParamsWire; + }; + const connectParams = decodeConnectParams(encodedConnectParams); + await this.ensureServerStarted(); + const toolBase = this.server?.tools.find((t) => t.name === toolName); + if (!toolBase) { + throw new Error(`Unknown tool: ${toolName}`); + } + try { + await this.connectionManager.connectToCompassConnection( + connectParams + ); + } catch (error: any) { + this.logger.log.error( + this.logger.mongoLogId(1_001_000_410), + 'ToolsHost', + 'Error when attempting to connect to Compass connection before executing tool', + { error: error.message } + ); + throw error; + } + try { + return await toolBase.invoke( + toolArgs as Parameters[0], + { signal } + ); + } finally { + await this.connectionManager.disconnect(); + } + } + case 'stopServer': { + await this.runner.server?.close(); + return; + } + default: + throw new Error(`Unknown method: ${method}`); + } + } + + async stop(): Promise { + // Aborts requests still in flight and stops answering new ones + this.disposable[Symbol.dispose](); + try { + await this.connectionManager.disconnect(); + await this.runner.server?.close(); + } catch (error: any) { + this.logger.log.error( + this.logger.mongoLogId(1_001_000_446), + 'ToolsHost', + 'Error when attempting to stop the MCP server', + { error: error.message } + ); + } + } +} diff --git a/packages/compass-utils/src/utility-process.spec.ts b/packages/compass-utils/src/utility-process.spec.ts new file mode 100644 index 00000000000..28c809cf798 --- /dev/null +++ b/packages/compass-utils/src/utility-process.spec.ts @@ -0,0 +1,56 @@ +import { expect } from 'chai'; +import sinon from 'sinon'; +import { openUtilityPort, utilityPortChannel } from './utility-process'; + +describe('openUtilityPort', function () { + let sandbox: sinon.SinonSandbox; + let originalWindow: unknown; + + beforeEach(function () { + sandbox = sinon.createSandbox(); + originalWindow = (globalThis as any).window; + }); + + afterEach(function () { + if (originalWindow === undefined) { + delete (globalThis as any).window; + } else { + (globalThis as any).window = originalWindow; + } + sandbox.restore(); + }); + + function stubMessageChannel() { + const port1 = { start: sinon.stub(), postMessage: sinon.stub() }; + const port2 = {}; + sandbox.replace( + globalThis, + 'MessageChannel', + sinon.stub().returns({ port1, port2 }) as unknown as typeof MessageChannel + ); + const postMessage = sinon.stub(); + (globalThis as any).window = { postMessage }; + return { port1, port2, postMessage }; + } + + it('hands the other end to the preload on the utility channel', function () { + const { port2, postMessage } = stubMessageChannel(); + + openUtilityPort('ai-assistant'); + + expect(postMessage).to.have.been.calledOnceWith( + { type: utilityPortChannel('ai-assistant') }, + '*', + [port2] + ); + }); + + it('starts the port it returns', function () { + const { port1 } = stubMessageChannel(); + + const port = openUtilityPort('ai-assistant'); + + expect(port).to.equal(port1); + expect(port1.start).to.have.been.calledOnce; + }); +}); diff --git a/packages/compass-utils/src/utility-process.ts b/packages/compass-utils/src/utility-process.ts index 2d6b29f79a4..8398a083a7e 100644 --- a/packages/compass-utils/src/utility-process.ts +++ b/packages/compass-utils/src/utility-process.ts @@ -26,5 +26,6 @@ export function isUtilityPortChannel( export function openUtilityPort(name: string): MessagePort { const { port1, port2 } = new MessageChannel(); window.postMessage({ type: utilityPortChannel(name) }, '*', [port2]); + port1.start(); return port1; } diff --git a/packages/compass/src/main/application.ts b/packages/compass/src/main/application.ts index 6281b22aced..81d087da11c 100644 --- a/packages/compass/src/main/application.ts +++ b/packages/compass/src/main/application.ts @@ -42,7 +42,7 @@ import { handleSquirrelWindowsStartup } from './squirrel-startup'; const { debug, log, mongoLogId } = createLogger('COMPASS-MAIN'); const track = createIpcTrack(); -const UTILITIES = ['embedded-shell']; +const UTILITIES = ['embedded-shell', 'ai-assistant']; type ExitHandler = () => Promise; type CompassApplicationMode = 'CLI' | 'GUI'; diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts new file mode 100644 index 00000000000..c8af71061c1 --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -0,0 +1,48 @@ +import process from 'node:process'; + +import type { ParentPort } from 'electron'; +import { listen } from '@mongodb-js/compass-utils'; +import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; +import { createUtilityLogger } from '../logging.mts'; +import { onPort } from '../ipc.mts'; + +export const name = 'ai-assistant'; + +export function main(parentPort: ParentPort): Disposable { + using ds = new DisposableStack(); + + ds.use( + listen(process, 'unhandledRejection', (reason) => { + throw reason; + }) + ); + + const logger = createUtilityLogger(parentPort, name); + + ds.use( + onPort(parentPort, (port) => { + // One host per renderer: it owns the MCP server and the driver + // connections for that renderer's tool calls. When the renderer goes + // away (crash included), its port closes and the host is torn down. + // Not tracked in `ds`: ports arrive after main() returned and the + // stack moved out; the listener dies with its closed port. + const host = new ToolsHost(port, { logger }); + listen(port, 'close', () => { + void host.stop().catch((error) => { + logger.log.error( + logger.mongoLogId(1_001_000_447), + 'ai-assistant', + 'Error when stopping the tools host', + { error: error instanceof Error ? error.message : String(error) } + ); + }); + }); + }) + ); + + return ds.move(); +} + +if (import.meta.main) { + main(process.parentPort); +} diff --git a/packages/compass/src/utilities/ai-assistant/index.spec.mts b/packages/compass/src/utilities/ai-assistant/index.spec.mts new file mode 100644 index 00000000000..3e72634e390 --- /dev/null +++ b/packages/compass/src/utilities/ai-assistant/index.spec.mts @@ -0,0 +1,91 @@ +import process from 'node:process'; +import { EventEmitter } from 'node:events'; +import { expect } from 'chai'; +import type { MessagePortMain, ParentPort } from 'electron'; +import { main } from './index.mts'; + +function createFakePort(): MessagePortMain & { sent: unknown[] } { + const sent: unknown[] = []; + return Object.assign(new EventEmitter(), { + sent, + postMessage: (message: unknown) => sent.push(message), + start: () => {}, + }) as unknown as MessagePortMain & { sent: unknown[] }; +} + +describe('ai-assistant utility', function () { + let parentPort: ParentPort; + let utility: Disposable; + let sentToMain: { channel: string; data: { line: string } }[]; + + beforeEach(function () { + sentToMain = []; + parentPort = Object.assign(new EventEmitter(), { + postMessage: (message: (typeof sentToMain)[number]) => + sentToMain.push(message), + }); + utility = main(parentPort); + }); + + afterEach(function () { + utility[Symbol.dispose](); + }); + + it('logs to main through parentPort', function () { + expect(sentToMain).to.have.lengthOf(1); + expect(sentToMain[0].channel).to.equal('compass:log'); + const entry = JSON.parse(sentToMain[0].data.line); + expect(entry).to.include({ + s: 'I', + c: 'AI-ASSISTANT', + id: 1_001_000_442, + ctx: 'Utility', + msg: 'Started', + }); + expect(entry.attr).to.include({ pid: process.pid, ppid: process.ppid }); + expect(entry.attr.startupMs).to.be.a('number'); + }); + + it('serves rpc requests arriving on a handed-over port', async function () { + this.timeout(10_000); + const port = createFakePort(); + parentPort.emit('message', { ports: [port] }); + + port.emit('message', { + data: { + kind: 'request', + id: 1, + method: 'listDatabaseTools', + params: null, + }, + }); + + const deadline = Date.now() + 9_000; + while (port.sent.length === 0 && Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + expect(port.sent).to.have.lengthOf(1); + const response = port.sent[0] as { + kind: string; + id: number; + ok: boolean; + result: { name: string }[]; + }; + expect(response).to.include({ kind: 'response', id: 1, ok: true }); + expect(response.result.map((tool) => tool.name)).to.include('find'); + }); + + it('stops serving requests when the port closes', function () { + const port = createFakePort(); + parentPort.emit('message', { ports: [port] }); + expect(port.listenerCount('message')).to.be.greaterThan(0); + + port.emit('close'); + + // The host releases its listener as the port closes, so nothing can + // answer requests for this renderer any more + expect(port.listenerCount('message')).to.equal(0); + expect(port.sent).to.have.lengthOf(0); + }); +}); diff --git a/packages/compass/webpack.config.js b/packages/compass/webpack.config.js index 74ade315c44..8edcbb12c81 100644 --- a/packages/compass/webpack.config.js +++ b/packages/compass/webpack.config.js @@ -63,6 +63,11 @@ module.exports = (_env, args) => { const utilitiesConfig = withoutStartElectron( createElectronMainConfig({ ...opts, + // Utilities are never coverage instrumented: istanbul's preamble calls + // `new Function('return this')`, and utility processes are forked with + // NODE_OPTIONS forbidding code generation from strings (application.ts), + // so an instrumented utility crashes on load. + coverage: '', entry: utilityEntries, outputFilename: '[name].mjs', }) @@ -200,13 +205,34 @@ module.exports = (_env, args) => { 'web-worker', ].map((name) => [name, `node-commonjs ${name}`]) ), - plugins: [ - new webpack.EnvironmentPlugin(hadronEnvConfig), - // Main forks one file per utility - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: Object.keys(utilityEntries).length, - }), - ], + resolve: { + alias: { + // Same as the renderer: Atlas Local is a native dependency the app + // does not ship for this flow, and bundling it only adds megabytes + '@mongodb-js/atlas-local': false, + }, + }, + // Utilities share the heavy Node-side dependencies (the driver, the MCP + // server, their transitive graph) instead of every utility bundling its + // own copy. + optimization: { + splitChunks: { + chunks: 'all', + cacheGroups: { + // Only the sharing rule: everything else stays in the utility that + // uses it, so this does not fragment the output + default: false, + defaultVendors: false, + utilitiesVendor: { + name: 'utilities-vendor', + chunks: 'all', + minChunks: 2, + reuseExistingChunk: true, + }, + }, + }, + }, + plugins: [new webpack.EnvironmentPlugin(hadronEnvConfig)], }), merge(preloadConfig, { name: 'preload', From 28a8fb1261eba1e578c524282faf1541c285aec9 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 18:48:16 +0000 Subject: [PATCH 06/16] fix log ids --- .../src/tools/infrastructure/tools-host.ts | 2 +- packages/compass/src/utilities/ai-assistant/index.mts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 97a44d3c2d2..8764bf15424 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -149,7 +149,7 @@ export class ToolsHost { ); } catch (error: any) { this.logger.log.error( - this.logger.mongoLogId(1_001_000_410), + this.logger.mongoLogId(1_001_000_453), 'ToolsHost', 'Error when attempting to connect to Compass connection before executing tool', { error: error.message } diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts index c8af71061c1..c55454a63de 100644 --- a/packages/compass/src/utilities/ai-assistant/index.mts +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -30,7 +30,7 @@ export function main(parentPort: ParentPort): Disposable { listen(port, 'close', () => { void host.stop().catch((error) => { logger.log.error( - logger.mongoLogId(1_001_000_447), + logger.mongoLogId(1_001_000_454), 'ai-assistant', 'Error when stopping the tools host', { error: error instanceof Error ? error.message : String(error) } From 6a2887524317035c38db8d1b0d1054f15e37170d Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Wed, 28 Oct 2026 19:17:50 +0000 Subject: [PATCH 07/16] copilot comment --- .../src/utilities/ai-assistant/index.mts | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass/src/utilities/ai-assistant/index.mts index c55454a63de..d2c37625d67 100644 --- a/packages/compass/src/utilities/ai-assistant/index.mts +++ b/packages/compass/src/utilities/ai-assistant/index.mts @@ -19,24 +19,35 @@ export function main(parentPort: ParentPort): Disposable { const logger = createUtilityLogger(parentPort, name); + const hosts = new Set(); + ds.defer(() => { + for (const host of hosts) { + void host.stop(); + } + }); + ds.use( onPort(parentPort, (port) => { - // One host per renderer: it owns the MCP server and the driver - // connections for that renderer's tool calls. When the renderer goes - // away (crash included), its port closes and the host is torn down. - // Not tracked in `ds`: ports arrive after main() returned and the - // stack moved out; the listener dies with its closed port. const host = new ToolsHost(port, { logger }); - listen(port, 'close', () => { - void host.stop().catch((error) => { - logger.log.error( - logger.mongoLogId(1_001_000_454), - 'ai-assistant', - 'Error when stopping the tools host', - { error: error instanceof Error ? error.message : String(error) } - ); - }); - }); + hosts.add(host); + listen( + port, + 'close', + () => { + hosts.delete(host); + void host.stop().catch((error) => { + logger.log.error( + logger.mongoLogId(1_001_000_454), + 'ai-assistant', + 'Error when stopping the tools host', + { + error: error instanceof Error ? error.message : String(error), + } + ); + }); + }, + { once: true } + ); }) ); From 313b59bcd68b413f072d220bb18889cd0414dcce Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Thu, 29 Oct 2026 12:05:35 +0000 Subject: [PATCH 08/16] fix app bundle size --- packages/compass-generative-ai/.eslintrc.js | 30 +++++++++++++++++++ .../src/tools-connection-manager.spec.ts | 1 + .../src/tools-connection-manager.ts | 11 ++++--- .../src/tools-controller.ts | 1 + .../src/tools/infrastructure/tools-host.ts | 1 + 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/compass-generative-ai/.eslintrc.js b/packages/compass-generative-ai/.eslintrc.js index 0b379d7a576..f3a05b276e3 100644 --- a/packages/compass-generative-ai/.eslintrc.js +++ b/packages/compass-generative-ai/.eslintrc.js @@ -6,4 +6,34 @@ module.exports = { tsconfigRootDir: __dirname, project: ['./tsconfig.json'], }, + overrides: [ + { + // Files in this directory run outside the renderer: in the AI assistant + // utility process on desktop, and in the page for compass-web. Renderer + // entries (the provider subpaths, the design system) would drag the UI + // stack into the utility bundle, so utility-side code takes its + // cross-world dependencies via config instead. + files: ['src/tools/infrastructure/**'], + excludedFiles: ['**/*.spec.ts'], + rules: { + '@typescript-eslint/no-restricted-imports': [ + 'error', + { + patterns: [ + { + group: [ + '@mongodb-js/compass-logging/provider', + '@mongodb-js/compass-telemetry/provider', + '@mongodb-js/compass-components', + '@mongodb-js/compass-app-registry', + ], + message: + 'Utility-side code must not import renderer entries; take what it needs via config.', + }, + ], + }, + ], + }, + }, + ], }; diff --git a/packages/compass-generative-ai/src/tools-connection-manager.spec.ts b/packages/compass-generative-ai/src/tools-connection-manager.spec.ts index 336c5785d71..e362daf9616 100644 --- a/packages/compass-generative-ai/src/tools-connection-manager.spec.ts +++ b/packages/compass-generative-ai/src/tools-connection-manager.spec.ts @@ -35,6 +35,7 @@ describe('ToolsConnectionManager', function () { connectionManager = new ToolsConnectionManager({ logger, getTelemetryAnonymousId, + mongoLogId: (id) => ({ __value: id }), }); }); diff --git a/packages/compass-generative-ai/src/tools-connection-manager.ts b/packages/compass-generative-ai/src/tools-connection-manager.ts index 44466163b0a..9b63236066c 100644 --- a/packages/compass-generative-ai/src/tools-connection-manager.ts +++ b/packages/compass-generative-ai/src/tools-connection-manager.ts @@ -11,7 +11,6 @@ import { import type { ServiceProvider } from '@mongosh/service-provider-core'; import { isAtlas, isAtlasStream } from '@mongodb-js/mongodb-build-info'; import ConnectionString from 'mongodb-connection-string-url'; -import { mongoLogId } from '@mongodb-js/compass-logging/provider'; export interface ToolsConnectParams { connectionId: string; @@ -25,11 +24,13 @@ const DEFAULT_TELEMETRY_APP_NAME = 'MongoDB Compass'; type ToolsConnectionManagerConfig = { logger: LoggerBase; getTelemetryAnonymousId: () => string; + mongoLogId: (id: number) => { __value: number }; }; export class ToolsConnectionManager extends ConnectionManager { private logger: LoggerBase; private getTelemetryAnonymousId: () => string; + private mongoLogId: ToolsConnectionManagerConfig['mongoLogId']; private activeConnection: { id: string; provider: ServiceProvider; @@ -38,10 +39,12 @@ export class ToolsConnectionManager extends ConnectionManager { constructor({ logger, getTelemetryAnonymousId, + mongoLogId, }: ToolsConnectionManagerConfig) { super(); this.logger = logger; this.getTelemetryAnonymousId = getTelemetryAnonymousId; + this.mongoLogId = mongoLogId; } override connect(): Promise { @@ -62,7 +65,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www. await this.activeConnection?.provider?.close(); } catch (error) { this.logger.error({ - id: mongoLogId(1_001_000_411), + id: this.mongoLogId(1_001_000_411), context: 'compass-tools-connection-manager', message: `Error disconnecting from Compass connection - ${ error instanceof Error ? error.message : String(error) @@ -93,7 +96,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www. if (isAtlasStream(connectParams.connectionString)) { this.logger.warning({ - id: mongoLogId(1_001_000_413), + id: this.mongoLogId(1_001_000_413), context: 'compass-tools-connection-manager', message: 'Attempting a connection to an Atlas Stream.', }); @@ -129,7 +132,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www. ); } catch (error) { this.logger.error({ - id: mongoLogId(1_001_000_412), + id: this.mongoLogId(1_001_000_412), context: 'compass-tools-connection-manager', message: `Error connecting to Compass connection - ${ error instanceof Error ? error.message : String(error) diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 29403be0fd5..bc8e0529481 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -139,6 +139,7 @@ export class ToolsController { this.connectionManager = new ToolsConnectionManager({ logger: this.runner.logger, getTelemetryAnonymousId, + mongoLogId: this.logger.mongoLogId.bind(this.logger), }); // Set up listener on close event to perform a cleanup when the 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 8764bf15424..9852a500b20 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -60,6 +60,7 @@ export class ToolsHost { this.connectionManager = new ToolsConnectionManager({ logger: this.runner.logger, getTelemetryAnonymousId: () => this.telemetryAnonymousId, + mongoLogId: this.logger.mongoLogId.bind(this.logger), }); this.disposable = serveToolsRequests(port, (method, params, signal) => From 02b5ace9c6aa12ca68bdad038b1feb03b602b6d6 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Fri, 30 Oct 2026 13:16:23 +0000 Subject: [PATCH 09/16] feat: integrate new utility with assistant and generative ai COMPASS-11269 (#8637) * feat: ai assistant utility * fix log ids * copilot comment * fix app bundle size * feat: integrate new utility with assistant and generative ai * copilot review --- .../compass-assistant/test/tool-calls.eval.ts | 15 +- .../compass-generative-ai/src/provider.tsx | 22 +- .../src/tools-controller.spec.ts | 243 ++++++------ .../src/tools-controller.ts | 349 +++++++----------- packages/compass-web/src/entrypoint.tsx | 4 +- .../compass-web/src/tools-loopback.spec.ts | 63 ++++ packages/compass-web/src/tools-loopback.ts | 51 +++ 7 files changed, 425 insertions(+), 322 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/packages/compass-assistant/test/tool-calls.eval.ts b/packages/compass-assistant/test/tool-calls.eval.ts index bf193e3b753..fcab42ba971 100644 --- a/packages/compass-assistant/test/tool-calls.eval.ts +++ b/packages/compass-assistant/test/tool-calls.eval.ts @@ -9,6 +9,7 @@ */ import { strict as assert } from 'node:assert'; +import { MessageChannel } from 'node:worker_threads'; import { createOpenAI } from '@ai-sdk/openai'; import { streamText } from 'ai'; import type { ToolSet, ToolCallPart } from 'ai'; @@ -17,6 +18,7 @@ import { ToolsController, type ToolGroup, } from '@mongodb-js/compass-generative-ai/provider'; +import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider'; import { defaultPreferencesInstance } from '@mongodb-js/compass-preferences-model'; @@ -65,6 +67,12 @@ const DEFAULT_EVAL_TASK_CONFIG: EvalTaskConfig = { const logger = createNoopLogger('EVAL-TOOLS-CONTROLLER'); +// There is no utility process in Node, so the host runs in this one, on the +// other end of a plain MessageChannel. Held in a variable so the run can +// release its server and driver connection when it finishes. +const evalChannel = new MessageChannel(); +const evalHost = new ToolsHost(evalChannel.port2, { logger }); + const toolsController = new ToolsController({ logger, getTelemetryAnonymousId: () => 'eval-anonymous-id', @@ -72,6 +80,7 @@ const toolsController = new ToolsController({ enableMCPTelemetry: false, preferences: defaultPreferencesInstance, atlasAdminApi: {} as AtlasAdminApiService, + openPort: () => evalChannel.port1, }); let toolsInitialized = false; @@ -83,9 +92,6 @@ let toolsInitialized = false; async function ensureToolsInitialized(): Promise { if (toolsInitialized) return; await toolsController.startServer(); - if (!toolsController.server) { - throw new Error('Failed to start ToolsController server for eval tools'); - } toolsInitialized = true; } @@ -218,6 +224,8 @@ function createToolCallAssistantTask(config: EvalTaskConfig) { connectionConfig, }); + assert(tools['find'], 'ToolsController did not provide database tools'); + const contextPrompt = buildContextPromptText({ custom, connectionString: connectionConfig.connectionString, @@ -277,6 +285,7 @@ async function main() { }); } finally { await toolsController.stopServer(); + await evalHost.stop(); await client.close(); await cluster.close(); // FIXME(COMPASS-10486): We need to have this otherwise the process hangs. diff --git a/packages/compass-generative-ai/src/provider.tsx b/packages/compass-generative-ai/src/provider.tsx index 5991c106002..7acb9bb935b 100644 --- a/packages/compass-generative-ai/src/provider.tsx +++ b/packages/compass-generative-ai/src/provider.tsx @@ -12,6 +12,7 @@ import { import { atlasAdminApiServiceLocator } from '@mongodb-js/atlas-admin-api/provider'; import { telemetryLocator } from '@mongodb-js/compass-telemetry/provider'; import { ToolsController } from './tools-controller'; +import type { ToolsRpcPort } from './tools/infrastructure/messages'; import { AtlasAiService } from './atlas-ai-service'; const AtlasAiServiceContext = createContext(null); @@ -60,8 +61,17 @@ export { AtlasAiService } from './atlas-ai-service'; const ToolsControllerContext = createContext(null); export const ToolsControllerProvider: React.FC<{ + /** + * How the controller reaches its tools host. Defaults to the desktop + * utility process; the browser has none, so compass-web provides a loopback + * that runs the same host in the page. + */ + openPort?: () => ToolsRpcPort; children?: React.ReactNode; -}> = createServiceProvider(function ToolsControllerProvider({ children }) { +}> = createServiceProvider(function ToolsControllerProvider({ + openPort, + children, +}) { const logger = useLogger('TOOLS-CONTROLLER'); const preferences = preferencesLocator(); const atlasAdminApi = atlasAdminApiServiceLocator(); @@ -78,8 +88,16 @@ export const ToolsControllerProvider: React.FC<{ enableMCPTelemetry: false, preferences, atlasAdminApi, + openPort, }); - }, [logger, telemetryAnonymousId, track, preferences, atlasAdminApi]); + }, [ + logger, + telemetryAnonymousId, + track, + preferences, + atlasAdminApi, + openPort, + ]); useEffect(() => { return () => { diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index b471023d6bf..6bb98167926 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -1,3 +1,4 @@ +import { MessageChannel } from 'node:worker_threads'; import sinon from 'sinon'; import { expect } from 'chai'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; @@ -9,11 +10,15 @@ import { } from '@mongodb-js/compass-preferences-model'; import type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; import type { ToolsConnectParams } from './tools-connection-manager'; -import { READ_ONLY_DATABASE_TOOLS } from './available-tools'; +import { ToolsHost } from './tools/infrastructure/tools-host'; import type { ToolGroup } from './tools-controller'; import { ToolsController } from './tools-controller'; describe('ToolsController', function () { + // The MCP server starts in the utility host on the other end of the port; + // starting it and registering tools is not instant. + this.timeout(10_000); + let sandbox: sinon.SinonSandbox; let logger: Logger; let toolsController: ToolsController; @@ -23,25 +28,65 @@ describe('ToolsController', function () { const baseTools = ['atlas-connection-error-debugger']; - beforeEach(async function () { - sandbox = sinon.createSandbox(); - logger = createNoopLogger(); - getTelemetryAnonymousId = sandbox.stub().returns('test-anonymous-id'); - preferences = await createSandboxFromDefaultPreferences(); - atlasAdminApi = {} as AtlasAdminApiService; - - toolsController = new ToolsController({ + // Every controller gets its own channel with a real ToolsHost on the other + // end, so the tests exercise the whole rpc boundary. + const openChannels: MessageChannel[] = []; + const openHosts: ToolsHost[] = []; + + /** + * setContext delivers the utility's config without awaiting the rpc call, so + * tests wait for the effect instead of guessing how long a message takes. + */ + async function waitFor( + condition: () => boolean, + timeoutMs = 2000 + ): Promise { + const deadline = Date.now() + timeoutMs; + while (!condition() && Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, 5)); + } + } + + function createController( + overrides: Partial[0]> = {} + ): ToolsController { + const channel = new MessageChannel(); + const host = new ToolsHost(channel.port2, { logger }); + openChannels.push(channel); + openHosts.push(host); + return new ToolsController({ enableMCPTelemetry: false, logger, getTelemetryAnonymousId, track: createNoopTrack(), preferences, atlasAdminApi, + openPort: () => channel.port1, + ...overrides, }); + } + + beforeEach(async function () { + sandbox = sinon.createSandbox(); + logger = createNoopLogger(); + getTelemetryAnonymousId = sandbox.stub().returns('test-anonymous-id'); + preferences = await createSandboxFromDefaultPreferences(); + atlasAdminApi = {} as AtlasAdminApiService; + + toolsController = createController(); }); afterEach(async function () { await toolsController.stopServer(); + for (const host of openHosts) { + await host.stop(); + } + for (const channel of openChannels) { + channel.port1.close(); + channel.port2.close(); + } + openChannels.length = 0; + openHosts.length = 0; sandbox.restore(); }); @@ -54,10 +99,6 @@ describe('ToolsController', function () { const tools = toolsController.getActiveTools(); expect(Object.keys(tools)).to.have.lengthOf(baseTools.length); }); - - it('server is initially undefined', function () { - expect(toolsController.server).to.be.undefined; - }); }); describe('setActiveTools', function () { @@ -244,14 +285,7 @@ describe('ToolsController', function () { }); it('ignores db tools if the server is not started', function () { - const newController = new ToolsController({ - enableMCPTelemetry: false, - logger, - getTelemetryAnonymousId, - track: createNoopTrack(), - preferences, - atlasAdminApi, - }); + const newController = createController(); newController.setActiveTools(new Set(['db-read'])); expect(Object.keys(newController.getActiveTools())).to.deep.equal( @@ -276,40 +310,10 @@ describe('ToolsController', function () { ]; for (const toolName of readonlyToolNames) { - expect(tools).to.have.property(toolName); + expect(tools, toolName).to.have.property(toolName); } }); - it('ensures all read only database tools are retrievable from the server after initialization', function () { - expect(toolsController.server).to.not.be.undefined; - expect(toolsController.server?.tools).to.not.be.undefined; - - if ( - toolsController.server && - toolsController.server.tools.length === 0 - ) { - toolsController.server.registerTools(); - } - - const serverTools = toolsController.server?.tools ?? []; - const serverToolNames = serverTools.map((tool) => tool.name); - - for (const tool of READ_ONLY_DATABASE_TOOLS) { - expect( - serverToolNames, - `Expected server to have tool: ${tool.name}` - ).to.include(tool.name); - } - - // Verify the count matches - expect( - serverToolNames.filter((name) => - READ_ONLY_DATABASE_TOOLS.map((t) => t.name).includes(name) - ).length, - 'Expected all READ_ONLY_DATABASE_TOOLS to be in server tools' - ).to.equal(READ_ONLY_DATABASE_TOOLS.length); - }); - it('does not include non-readonly tools', function () { const tools = toolsController.getActiveTools(); // These tools should not be included even if they exist in the MCP server @@ -318,12 +322,11 @@ describe('ToolsController', function () { expect(tools).to.not.have.property('update-many'); }); - it('database tools have needsApproval and strict properties', function () { + it('database tools have needsApproval property', function () { const tools = toolsController.getActiveTools(); const listDatabasesTool = tools['list-databases']; expect(listDatabasesTool).to.have.property('needsApproval', true); - expect(listDatabasesTool).to.have.property('strict', true); }); it('collection-schema tool has a custom description that instructs the model to use it before queries and aggregations', function () { @@ -436,42 +439,61 @@ describe('ToolsController', function () { expect(result.query).to.equal('new query'); }); - it('syncs telemetry setting with runner userConfig', function () { + it('uses the context set before startup when the server starts', async function () { + // Context often arrives before the first message starts the utility; + // those values must not be replaced by the constructor defaults. toolsController.setContext({ enableMCPTelemetry: true, + maxTimeMS: 1234, connections: [], }); - expect((toolsController as any).runner.userConfig.telemetry).to.equal( - 'enabled' - ); + + await toolsController.startServer(); + const runner = (openHosts[0] as any).runner; + + await waitFor(() => runner.userConfig.maxTimeMS === 1234); + expect(runner.userConfig.telemetry).to.equal('enabled'); + expect(runner.userConfig.maxTimeMS).to.equal(1234); + }); + + it('syncs telemetry setting with the utility host runner', async function () { + await toolsController.startServer(); + const runner = (openHosts[0] as any).runner; + + toolsController.setContext({ + enableMCPTelemetry: true, + connections: [], + }); + await waitFor(() => runner.userConfig.telemetry === 'enabled'); + expect(runner.userConfig.telemetry).to.equal('enabled'); toolsController.setContext({ enableMCPTelemetry: false, connections: [], }); - expect((toolsController as any).runner.userConfig.telemetry).to.equal( - 'disabled' - ); + await waitFor(() => runner.userConfig.telemetry === 'disabled'); + expect(runner.userConfig.telemetry).to.equal('disabled'); }); - it('syncs maxTimeMS setting with runner userConfig', function () { + it('syncs maxTimeMS setting with the utility host runner', async function () { + await toolsController.startServer(); + const runner = (openHosts[0] as any).runner; + toolsController.setContext({ enableMCPTelemetry: false, maxTimeMS: 5000, connections: [], }); - expect((toolsController as any).runner.userConfig.maxTimeMS).to.equal( - 5000 - ); + await waitFor(() => runner.userConfig.maxTimeMS === 5000); + expect(runner.userConfig.maxTimeMS).to.equal(5000); toolsController.setContext({ enableMCPTelemetry: false, maxTimeMS: undefined, connections: [], }); - expect((toolsController as any).runner.userConfig.maxTimeMS).to.equal( - undefined - ); + await waitFor(() => runner.userConfig.maxTimeMS === undefined); + expect(runner.userConfig.maxTimeMS).to.equal(undefined); }); }); @@ -508,22 +530,48 @@ describe('ToolsController', function () { describe('server lifecycle', function () { describe('startServer', function () { - it('starts the MCP server', async function () { - expect(toolsController.server).to.be.undefined; - + it('fetches the db tool definitions from the utility host', async function () { await toolsController.startServer(); + toolsController.setActiveTools(new Set(['db-read'])); - expect(toolsController.server).to.not.be.undefined; + expect(toolsController.getActiveTools()).to.have.property('find'); }); - it('does not restart server if already started', async function () { - await toolsController.startServer(); - const firstServer = toolsController.server; + it('does not open a second port if already started', async function () { + const openPort = sandbox.stub().returns(openChannels[0].port1); + const controller = createController({ openPort }); - await toolsController.startServer(); - const secondServer = toolsController.server; + await controller.startServer(); + await controller.startServer(); - expect(firstServer).to.equal(secondServer); + expect(openPort.calledOnce).to.be.true; + }); + + it('shares one startup between concurrent calls', async function () { + const openPort = sandbox.stub().returns(openChannels[0].port1); + const controller = createController({ openPort }); + controller.setActiveTools(new Set(['db-read'])); + + await Promise.all([controller.startServer(), controller.startServer()]); + + expect(openPort.calledOnce).to.be.true; + expect(controller.getActiveTools()).to.have.property('find'); + }); + + it('gives up on a utility that never answers', async function () { + this.timeout(20_000); + const silentChannel = new MessageChannel(); + openChannels.push(silentChannel); + const controller = createController({ + openPort: () => silentChannel.port1, + }); + controller.setActiveTools(new Set(['db-read'])); + + await controller.startServer(); + + expect(Object.keys(controller.getActiveTools())).to.deep.equal( + baseTools + ); }); it('logs when server starts successfully', async function () { @@ -540,21 +588,16 @@ describe('ToolsController', function () { logStub.calledWith( sinon.match.number, 'ToolsController', - 'In-memory MCP server started successfully' + 'Assistant tools utility process client started successfully' ) ).to.be.true; }); it('handles errors during server startup gracefully', async function () { - const errorController = new ToolsController({ - enableMCPTelemetry: false, - logger, + const errorController = createController({ getTelemetryAnonymousId: () => { throw new Error('Telemetry error'); }, - track: createNoopTrack(), - preferences, - atlasAdminApi, }); // Should not throw even if there's an error @@ -567,7 +610,6 @@ describe('ToolsController', function () { describe('stopServer', function () { it('stops the MCP server', async function () { await toolsController.startServer(); - expect(toolsController.server).to.not.be.undefined; await toolsController.stopServer(); @@ -575,6 +617,17 @@ describe('ToolsController', function () { expect(() => toolsController.stopServer()).to.not.throw(); }); + it('drops the db tools after stopping', async function () { + await toolsController.startServer(); + toolsController.setActiveTools(new Set(['db-read'])); + expect(toolsController.getActiveTools()).to.have.property('find'); + + await toolsController.stopServer(); + expect(Object.keys(toolsController.getActiveTools())).to.deep.equal( + baseTools + ); + }); + it('handles stop when server is not running', async function () { await toolsController.stopServer(); @@ -583,11 +636,6 @@ describe('ToolsController', function () { }); it('logs when server stops successfully', async function () { - // TODO: flushTimeout.unref is not a function - if ((process as any).type === 'renderer') { - return this.skip(); - } - await toolsController.startServer(); const logStub = sandbox.stub(logger.log, 'info'); @@ -609,19 +657,6 @@ describe('ToolsController', function () { }); }); - describe('server property', function () { - it('returns undefined when server is not started', function () { - expect(toolsController.server).to.be.undefined; - }); - - it('returns server instance when started', async function () { - await toolsController.startServer(); - - expect(toolsController.server).to.not.be.undefined; - expect(toolsController.server).to.have.property('tools'); - }); - }); - describe('integration scenarios', function () { it('can use multiple tool groups together', async function () { await toolsController.startServer(); diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index bc8e0529481..1b4adb587e5 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -1,29 +1,17 @@ -import { tool } from 'ai'; +import { tool, jsonSchema } from 'ai'; import type { ToolSet } from 'ai'; import type { Logger } from '@mongodb-js/compass-logging'; import z from 'zod/v4'; -import { - Keychain, - TransportRunnerBase, - UserConfigSchema, -} from 'mongodb-mcp-server'; -import type { - DefaultMetrics, - Metrics, - Server, - TransportRunnerConfig, - UserConfig, -} from 'mongodb-mcp-server'; +import { openUtilityPort } from '@mongodb-js/compass-utils'; import type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; import type { PreferencesAccess } from '@mongodb-js/compass-preferences-model'; import { getAtlasConfigForPreset } from '@mongodb-js/atlas-service/provider'; import type { TrackFunction } from '@mongodb-js/compass-telemetry'; -import { createConnectionErrorHandler } from './tools-connection-error-handler'; -import { ToolsLogger } from './tools-logger'; -import { ToolsConnectionManager } from './tools-connection-manager'; +import { createToolsRpcClient } from './tools/infrastructure/rpc-client'; +import { encodeConnectParams } from './tools/infrastructure/connect-params'; +import type { ToolsRpcPort } from './tools/infrastructure/messages'; +import type { ToolDefinitionInfo } from './tools/infrastructure/tools-host'; import type { ToolsConnectParams } from './tools-connection-manager'; -import { removeZodTransforms } from './remove-zod-transforms'; -import { READ_ONLY_DATABASE_TOOLS } from './available-tools'; import { debugConnection, debugConnectionDescription, @@ -31,6 +19,8 @@ import { export type ToolGroup = 'querybar' | 'aggregation-builder' | 'db-read'; +const ASSISTANT_TOOLS_STARTUP_TIMEOUT_MS = 10_000; + type CompassContext = { enableMCPTelemetry: boolean; maxTimeMS?: number; @@ -42,44 +32,6 @@ type ToolsContext = CompassContext & { connections: ToolsConnectParams[]; }; -class NoopDefaultMetrics { - observe(): void { - // no-op - } -} - -class NoopMetrics implements Metrics { - constructor() {} - get(_key: K): DefaultMetrics[K] { - return new NoopDefaultMetrics() as unknown as DefaultMetrics[K]; - } - getMetrics(): Promise { - return Promise.resolve(''); - } -} - -/** - * In-memory MCP runner that doesn't bind to any external transport. - * The server is kept in memory for direct programmatic access. - */ -class InMemoryRunner extends TransportRunnerBase { - public server?: Server; - public readonly userConfig: UserConfig; - - constructor(config: TransportRunnerConfig) { - super(config); - this.userConfig = config.userConfig; - } - - async start(): Promise { - this.server = await this.setupServer(); - } - - async closeTransport(): Promise { - await this.server?.close(); - } -} - type ToolsControllerConfig = { logger: Logger; preferences: PreferencesAccess; @@ -88,6 +40,8 @@ type ToolsControllerConfig = { enableMCPTelemetry: boolean; maxTimeMS?: number; atlasAdminApi: AtlasAdminApiService; + // Injected for tests + openPort?: () => ToolsRpcPort; }; export class ToolsController { @@ -95,66 +49,38 @@ export class ToolsController { private preferences: PreferencesAccess; private toolGroups: Set = new Set(); private context: ToolsContext = Object.create(null); - private readonly runner: InMemoryRunner; - private connectionManager: ToolsConnectionManager; private connectionIdByToolCallId: Record = Object.create(null); private readonly atlasAdminApi: AtlasAdminApiService; private readonly track: TrackFunction; + private readonly getTelemetryAnonymousId: () => string; + private readonly openPort: () => ToolsRpcPort; + /** Latest config known to the renderer, sent when the utility starts */ + private currentConfig: { + enableMCPTelemetry: boolean; + maxTimeMS?: number; + }; + private portClient: ReturnType | null = null; + private startPromise: Promise | null = null; + private dbToolDefinitions: ToolDefinitionInfo[] = []; constructor({ logger, getTelemetryAnonymousId, - track, enableMCPTelemetry, maxTimeMS, atlasAdminApi, preferences, + track, + openPort, }: ToolsControllerConfig) { this.logger = logger; + this.getTelemetryAnonymousId = getTelemetryAnonymousId; this.atlasAdminApi = atlasAdminApi; this.preferences = preferences; this.track = track; - const mcpConfig = UserConfigSchema.parse({ - disabledTools: ['connect'], - loggers: ['mcp'], - readOnly: true, - // NOTE: the preferences could change at runtime. As a best-effort way of - // keeping them in sync we'll change them every time we set the tools' context - telemetry: enableMCPTelemetry ? 'enabled' : 'disabled', - maxTimeMS, - }); - - this.runner = new InMemoryRunner({ - userConfig: mcpConfig, - createConnectionManager: () => Promise.resolve(this.connectionManager), - connectionErrorHandler: createConnectionErrorHandler(), - additionalLoggers: [new ToolsLogger(Keychain.root, this.logger)], - telemetryProperties: { - hosting_mode: 'compass', - }, - metrics: new NoopMetrics(), - }); - - this.connectionManager = new ToolsConnectionManager({ - logger: this.runner.logger, - getTelemetryAnonymousId, - mongoLogId: this.logger.mongoLogId.bind(this.logger), - }); - - // Set up listener on close event to perform a cleanup when the Client - // closes connection to MCP server and eventually ConnectionManager shuts down. - this.connectionManager.events.on('close', (): void => { - this.logger.log.info( - this.logger.mongoLogId(1_001_000_405), - 'Tools Controller', - 'MCPConnectionManager closed. Performing cleanup', - { - connectionManagerClientName: this.connectionManager.clientName, - } - ); - // Nothing to do because this will only happen once when Compass is closed - }); + this.openPort = openPort ?? (() => openUtilityPort('ai-assistant')); + this.currentConfig = { enableMCPTelemetry, maxTimeMS }; } setActiveTools(toolGroups: Set): void { @@ -202,91 +128,24 @@ export class ToolsController { }; } - if (this.toolGroups.has('db-read') && this.runner.server) { - const readonlyDatabaseToolNames = READ_ONLY_DATABASE_TOOLS.map( - (tool) => tool.name - ); - if (this.runner.server.tools.length === 0) { - this.runner.server.registerTools(); - } - - const availableTools = this.runner.server.tools ?? []; - for (const toolBase of availableTools) { - if (!readonlyDatabaseToolNames.includes(toolBase.name)) { - continue; - } - - tools[toolBase.name] = tool({ - description: getCustomToolDescription(toolBase), - inputSchema: z.object( - Object.fromEntries( - Object.entries(toolBase.argsShape).map(([key, value]) => { - // TODO: MCP server applies transformations like toEJSON. - // We should come up with a better solution for this but for now we recursively remove the transforms. - // AI SDK applies transformations defined in the schema *before* sending the request to the model. - return [key, removeZodTransforms(value)]; - }) - ) - ), + if (this.toolGroups.has('db-read') && this.dbToolDefinitions.length) { + for (const definition of this.dbToolDefinitions) { + tools[definition.name] = tool({ + description: getCustomToolDescription(definition), + inputSchema: jsonSchema(definition.inputSchema), needsApproval: true, - strict: true, - execute: async (args, options) => { - const connectionId = - this.connectionIdByToolCallId[options.toolCallId]; - if (!connectionId) { - // sanity check in case we ever get multiple tool calls in parallel - const error = new Error("Can't find connection for tool call"); - this.logger.log.error( - this.logger.mongoLogId(1_001_000_415), - 'ToolsController', - error.message, - { - toolCallId: options.toolCallId, - } - ); - throw error; - } - - const connectParams = this.context.connections.find( - (connection) => connection.connectionId === connectionId + strict: false, + execute: async (args: unknown, options) => { + const connectParams = this.resolveConnectParams(options.toolCallId); + return await this.portClient!.call( + 'performToolCall', + { + toolName: definition.name, + toolArgs: args, + connectParams: encodeConnectParams(connectParams), + }, + options.abortSignal ); - if (!connectParams) { - // the context could have changed between when the tool was - // created and when it gets executed - const error = new Error('No active connection to execute tool'); - this.logger.log.error( - this.logger.mongoLogId(1_001_000_414), - 'ToolsController', - error.message - ); - throw error; - } - - // connect - try { - await this.connectionManager.connectToCompassConnection( - connectParams - ); - } catch (error: any) { - this.logger.log.error( - this.logger.mongoLogId(1_001_000_410), - 'ToolsController', - 'Error when attempting to connect to Compass connection before executing tool', - { error: error.message } - ); - throw error; - } - - let result: Awaited>; - try { - result = await toolBase.invoke(args, { - signal: options.abortSignal ?? new AbortSignal(), - }); - } finally { - // disconnect - await this.connectionManager.disconnect(); - } - return result; }, }); } @@ -336,12 +195,23 @@ export class ToolsController { } setContext(context: ToolsContext): void { - // make sure these properties are always in sync with the tools' config - this.runner.userConfig.telemetry = context.enableMCPTelemetry - ? 'enabled' - : 'disabled'; - this.runner.userConfig.maxTimeMS = context.maxTimeMS; this.context = context; + this.currentConfig = { + enableMCPTelemetry: context.enableMCPTelemetry, + maxTimeMS: context.maxTimeMS, + }; + void this.portClient + ?.call('setUserConfig', { + enableMCPTelemetry: context.enableMCPTelemetry, + maxTimeMS: context.maxTimeMS, + telemetryAnonymousId: this.getTelemetryAnonymousId(), + }) + .catch((error: unknown) => { + this.logger.debug( + 'Error when updating assistant tools configuration', + error + ); + }); } setConnectionIdForToolCall({ @@ -354,34 +224,35 @@ export class ToolsController { this.connectionIdByToolCallId[toolCallId] = connectionId; } - // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents - get server(): Server | undefined { - return this.runner?.server; + public startServer(): Promise { + this.startPromise ??= this.startServerOnce(); + return this.startPromise; } - public async startServer(): Promise { - if (this.runner.server) { - return; - } - + private async startServerOnce(): Promise { try { this.logger.log.info( this.logger.mongoLogId(1_001_000_402), 'ToolsController', - 'Starting in-memory MCP server with config', - { - ...this.runner.userConfig, - apiClientId: '', - apiClientSecret: '', - } + 'Starting assistant tools utility process client' ); - await this.runner.start(); + this.portClient = createToolsRpcClient(this.openPort()); + await this.withStartupTimeout(async () => { + await this.portClient!.call('setUserConfig', { + ...this.currentConfig, + telemetryAnonymousId: this.getTelemetryAnonymousId(), + }); + + this.dbToolDefinitions = (await this.portClient!.call( + 'listDatabaseTools' + )) as ToolDefinitionInfo[]; + }); this.logger.log.info( this.logger.mongoLogId(1_001_000_403), 'ToolsController', - 'In-memory MCP server started successfully' + 'Assistant tools utility process client started successfully' ); } catch (error: any) { // In case of errors we don't want Compass to crash so we @@ -392,20 +263,38 @@ export class ToolsController { 'Error when attempting to start MCP server', { error: error.message } ); + this.portClient?.close(); + this.portClient = null; + this.dbToolDefinitions = []; + this.startPromise = null; } } + private async withStartupTimeout(start: () => Promise): Promise { + const timeout = new Promise((_resolve, reject) => { + const timer = setTimeout(() => { + reject(new Error('Timed out waiting for the assistant tools utility')); + }, ASSISTANT_TOOLS_STARTUP_TIMEOUT_MS); + timer.unref?.(); + }); + return await Promise.race([start(), timeout]); + } + public async stopServer(): Promise { + if (!this.portClient) { + this.logger.log.info( + this.logger.mongoLogId(1_001_000_406), + 'ToolsController', + 'MCP server stop requested. No MCP server running, nothing to stop.' + ); + return; + } + const client = this.portClient; + this.portClient = null; + this.startPromise = null; + this.dbToolDefinitions = []; try { - if (!this.runner.server) { - this.logger.log.info( - this.logger.mongoLogId(1_001_000_406), - 'ToolsController', - 'MCP server stop requested. No MCP server running, nothing to stop.' - ); - return; - } - await this.runner.server.close(); + await client.call('stopServer', undefined, AbortSignal.timeout(10_000)); this.logger.log.info( this.logger.mongoLogId(1_001_000_407), 'ToolsController', @@ -418,8 +307,44 @@ export class ToolsController { 'Error when attempting to close the MCP server', { error: error.message } ); + } finally { + client.close(); } } + + private resolveConnectParams(toolCallId: string): ToolsConnectParams { + const connectionId = this.connectionIdByToolCallId[toolCallId]; + if (!connectionId) { + // sanity check in case we ever get multiple tool calls in parallel + const error = new Error("Can't find connection for tool call"); + this.logger.log.error( + this.logger.mongoLogId(1_001_000_415), + 'ToolsController', + error.message, + { + toolCallId, + } + ); + throw error; + } + + const connectParams = this.context.connections.find( + (connection) => connection.connectionId === connectionId + ); + if (!connectParams) { + // the context could have changed between when the tool was + // created and when it gets executed + const error = new Error('No active connection to execute tool'); + this.logger.log.error( + this.logger.mongoLogId(1_001_000_414), + 'ToolsController', + error.message + ); + throw error; + } + + return connectParams; + } } function getCustomToolDescription(toolBase: { diff --git a/packages/compass-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} > - + (); + const logger = createNoopLogger(); + + const first = openToolsLoopback(hosts, logger); + const second = openToolsLoopback(hosts, logger); + + expect(first).to.not.equal(second); + expect(hosts.size).to.equal(2); + }); +}); diff --git a/packages/compass-web/src/tools-loopback.ts b/packages/compass-web/src/tools-loopback.ts new file mode 100644 index 00000000000..9ff70bdad8b --- /dev/null +++ b/packages/compass-web/src/tools-loopback.ts @@ -0,0 +1,51 @@ +import { useCallback, useEffect, useRef } from 'react'; +import { useLogger } from '@mongodb-js/compass-logging/provider'; +import type { Logger } from '@mongodb-js/compass-logging'; +import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; + +/** + * The channel between the renderer and the in-page host. Both ends listen + * through `addEventListener`, and a MessagePort only dispatches queued messages + * once started, so both have to be started here: the desktop equivalents do it + * in `openUtilityPort` and `onPort`. + */ +export function createToolsLoopbackChannel(): { + openPort: () => MessagePort; + hostPort: MessagePort; +} { + const channel = new MessageChannel(); + channel.port1.start(); + channel.port2.start(); + return { openPort: () => channel.port1, hostPort: channel.port2 }; +} + +export function openToolsLoopback( + hosts: Set, + logger: Logger +): MessagePort { + const { openPort, hostPort } = createToolsLoopbackChannel(); + hosts.add(new ToolsHost(hostPort, { logger })); + return openPort(); +} + +export function useToolsLoopback(): () => MessagePort { + const logger = useLogger('TOOLS-CONTROLLER'); + const hosts = useRef(new Set()); + + const openPort = useCallback( + () => openToolsLoopback(hosts.current, logger), + [logger] + ); + + useEffect(() => { + const live = hosts.current; + return () => { + for (const host of live) { + void host.stop(); + } + live.clear(); + }; + }, []); + + return openPort; +} From ba8e81efa396763c2175d2856a40b662001edfa2 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Fri, 30 Oct 2026 14:25:46 +0000 Subject: [PATCH 10/16] refactor to use new utilities structure --- package-lock.json | 1 + .../compass-generative-ai/src/provider.tsx | 22 +------ .../src/tools-controller.spec.ts | 9 ++- .../src/tools-controller.ts | 13 ++-- .../{messages.ts => protocol.ts} | 7 --- ...-client.spec.ts => runtime-client.spec.ts} | 63 ++++++++++--------- .../{rpc-client.ts => runtime-client.ts} | 7 +-- .../infrastructure/{server.ts => session.ts} | 12 ++-- .../tools/infrastructure/tools-host.spec.ts | 17 +++-- .../src/tools/infrastructure/tools-host.ts | 6 +- packages/compass-utilities/package.json | 1 + .../src}/ai-assistant/index.mts | 11 ++-- .../src}/ai-assistant/index.spec.mts | 0 packages/compass-web/polyfills/net/index.ts | 19 +++++- packages/compass-web/polyfills/tr46/index.ts | 4 +- packages/compass-web/src/entrypoint.tsx | 4 +- .../compass-web/src/tools-loopback.spec.ts | 63 ------------------- packages/compass-web/src/tools-loopback.ts | 51 --------------- packages/compass-web/src/utilities.mts | 30 +++++++-- 19 files changed, 123 insertions(+), 217 deletions(-) rename packages/compass-generative-ai/src/tools/infrastructure/{messages.ts => protocol.ts} (92%) rename packages/compass-generative-ai/src/tools/infrastructure/{rpc-client.spec.ts => runtime-client.spec.ts} (75%) rename packages/compass-generative-ai/src/tools/infrastructure/{rpc-client.ts => runtime-client.ts} (94%) rename packages/compass-generative-ai/src/tools/infrastructure/{server.ts => session.ts} (89%) rename packages/{compass/src/utilities => compass-utilities/src}/ai-assistant/index.mts (82%) rename packages/{compass/src/utilities => compass-utilities/src}/ai-assistant/index.spec.mts (100%) delete mode 100644 packages/compass-web/src/tools-loopback.spec.ts delete mode 100644 packages/compass-web/src/tools-loopback.ts diff --git a/package-lock.json b/package-lock.json index b04d9114253..67d4e337e17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57447,6 +57447,7 @@ "version": "1.0.0", "license": "SSPL", "dependencies": { + "@mongodb-js/compass-generative-ai": "^1.7.0", "@mongodb-js/compass-logging": "^1.18.2", "@mongodb-js/compass-shell": "^4.6.0", "@mongodb-js/compass-utils": "^0.10.6", diff --git a/packages/compass-generative-ai/src/provider.tsx b/packages/compass-generative-ai/src/provider.tsx index 7acb9bb935b..5991c106002 100644 --- a/packages/compass-generative-ai/src/provider.tsx +++ b/packages/compass-generative-ai/src/provider.tsx @@ -12,7 +12,6 @@ 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); @@ -61,17 +60,8 @@ 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({ - openPort, - children, -}) { +}> = createServiceProvider(function ToolsControllerProvider({ children }) { const logger = useLogger('TOOLS-CONTROLLER'); const preferences = preferencesLocator(); const atlasAdminApi = atlasAdminApiServiceLocator(); @@ -88,16 +78,8 @@ export const ToolsControllerProvider: React.FC<{ enableMCPTelemetry: false, preferences, atlasAdminApi, - openPort, }); - }, [ - logger, - telemetryAnonymousId, - track, - preferences, - atlasAdminApi, - openPort, - ]); + }, [logger, telemetryAnonymousId, track, preferences, atlasAdminApi]); useEffect(() => { return () => { diff --git a/packages/compass-generative-ai/src/tools-controller.spec.ts b/packages/compass-generative-ai/src/tools-controller.spec.ts index 6bb98167926..b1505efc262 100644 --- a/packages/compass-generative-ai/src/tools-controller.spec.ts +++ b/packages/compass-generative-ai/src/tools-controller.spec.ts @@ -47,11 +47,14 @@ describe('ToolsController', function () { } } + const asRendererPort = (port: MessageChannel['port1']): MessagePort => + port as unknown as MessagePort; + function createController( overrides: Partial[0]> = {} ): ToolsController { const channel = new MessageChannel(); - const host = new ToolsHost(channel.port2, { logger }); + const host = new ToolsHost(asRendererPort(channel.port2), { logger }); openChannels.push(channel); openHosts.push(host); return new ToolsController({ @@ -61,7 +64,7 @@ describe('ToolsController', function () { track: createNoopTrack(), preferences, atlasAdminApi, - openPort: () => channel.port1, + openPort: () => asRendererPort(channel.port1), ...overrides, }); } @@ -563,7 +566,7 @@ describe('ToolsController', function () { const silentChannel = new MessageChannel(); openChannels.push(silentChannel); const controller = createController({ - openPort: () => silentChannel.port1, + openPort: () => asRendererPort(silentChannel.port1), }); controller.setActiveTools(new Set(['db-read'])); diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 1b4adb587e5..3ab140fbf29 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -7,9 +7,8 @@ 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 { createRuntimeClient } from './tools/infrastructure/runtime-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 { @@ -41,7 +40,7 @@ type ToolsControllerConfig = { maxTimeMS?: number; atlasAdminApi: AtlasAdminApiService; // Injected for tests - openPort?: () => ToolsRpcPort; + openPort?: () => MessagePort; }; export class ToolsController { @@ -54,13 +53,13 @@ export class ToolsController { private readonly atlasAdminApi: AtlasAdminApiService; private readonly track: TrackFunction; private readonly getTelemetryAnonymousId: () => string; - private readonly openPort: () => ToolsRpcPort; + private readonly openPort: () => MessagePort; /** Latest config known to the renderer, sent when the utility starts */ private currentConfig: { enableMCPTelemetry: boolean; maxTimeMS?: number; }; - private portClient: ReturnType | null = null; + private portClient: ReturnType | null = null; private startPromise: Promise | null = null; private dbToolDefinitions: ToolDefinitionInfo[] = []; @@ -237,7 +236,7 @@ export class ToolsController { 'Starting assistant tools utility process client' ); - this.portClient = createToolsRpcClient(this.openPort()); + this.portClient = createRuntimeClient(this.openPort()); await this.withStartupTimeout(async () => { await this.portClient!.call('setUserConfig', { ...this.currentConfig, @@ -255,8 +254,6 @@ export class ToolsController { 'Assistant tools utility process client started successfully' ); } catch (error: any) { - // In case of errors we don't want Compass to crash so we - // silence MCP start errors and instead log them for debugging. this.logger.log.error( this.logger.mongoLogId(1_001_000_404), 'ToolsController', diff --git a/packages/compass-generative-ai/src/tools/infrastructure/messages.ts b/packages/compass-generative-ai/src/tools/infrastructure/protocol.ts similarity index 92% rename from packages/compass-generative-ai/src/tools/infrastructure/messages.ts rename to packages/compass-generative-ai/src/tools/infrastructure/protocol.ts index 1ed8adfa5ed..b3e4d9a714b 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/messages.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/protocol.ts @@ -1,10 +1,3 @@ -import type { EventSource } from '@mongodb-js/compass-utils'; - -export type ToolsRpcPort = { - postMessage(message: unknown): void; - close(): void; -} & EventSource; - export type ToolsRpcMethod = /** * Keeps the MCP server's runtime config in sync with Compass preferences. diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.spec.ts similarity index 75% rename from packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts rename to packages/compass-generative-ai/src/tools/infrastructure/runtime-client.spec.ts index c87116fbfd9..661fd51a0e8 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.spec.ts @@ -1,14 +1,22 @@ import { MessageChannel } from 'node:worker_threads'; import { expect } from 'chai'; import sinon from 'sinon'; -import { createToolsRpcClient } from './rpc-client'; -import { serveToolsRequests } from './server'; +import { createRuntimeClient } from './runtime-client'; +import { serveToolsRequests } from './session'; describe('tools rpc', function () { let channel: MessageChannel; + // worker_threads ports are structurally identical to DOM MessagePorts but + // not nominally, so tests bridge them with this cast. + const asRendererPort = (port: MessageChannel['port1']): MessagePort => + port as unknown as MessagePort; + let port1: MessagePort; + let port2: MessagePort; beforeEach(function () { channel = new MessageChannel(); + port1 = asRendererPort(channel.port1); + port2 = asRendererPort(channel.port2); }); afterEach(function () { @@ -17,9 +25,9 @@ describe('tools rpc', function () { }); it('round-trips a request to a response', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(port1); const params = { enableMCPTelemetry: true, maxTimeMS: 5 }; - serveToolsRequests(channel.port2, async (method, sentParams, _signal) => { + serveToolsRequests(port2, async (method, sentParams, _signal) => { expect(method).to.equal('setUserConfig'); return Promise.resolve(sentParams); }); @@ -28,8 +36,8 @@ describe('tools rpc', function () { }); it('rejects with the error message from the other side', async function () { - const client = createToolsRpcClient(channel.port1); - serveToolsRequests(channel.port2, () => { + const client = createRuntimeClient(port1); + serveToolsRequests(port2, () => { throw new Error('boom'); }); @@ -40,13 +48,13 @@ describe('tools rpc', function () { }); it('propagates abort to the server-side signal', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(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) => { + serveToolsRequests(port2, (_method, _params, signal) => { serverSignal = signal; return new Promise((_resolve, reject) => { signal.addEventListener('abort', () => { @@ -71,8 +79,8 @@ describe('tools rpc', function () { }); it('rejects immediately for an already-aborted signal', async function () { - const client = createToolsRpcClient(channel.port1); - serveToolsRequests(channel.port2, async () => Promise.resolve('unused')); + const client = createRuntimeClient(port1); + serveToolsRequests(port2, async () => Promise.resolve('unused')); const error = await client .call('performToolCall', undefined, AbortSignal.abort(new Error('nope'))) @@ -81,8 +89,8 @@ describe('tools rpc', function () { }); it('resolves in request order regardless of completion order', async function () { - const client = createToolsRpcClient(channel.port1); - serveToolsRequests(channel.port2, async (_method, params, _signal) => { + const client = createRuntimeClient(port1); + serveToolsRequests(port2, async (_method, params, _signal) => { const { delay, name } = params as { delay: number; name: string }; await new Promise((resolve) => setTimeout(resolve, delay)); return name; @@ -94,7 +102,7 @@ describe('tools rpc', function () { }); it('rejects pending calls when the client is closed', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(port1); // no server on the other end: the call stays pending until close() const pending = client.call('listDatabaseTools'); client.close(); @@ -107,8 +115,8 @@ describe('tools rpc', function () { it('ignores messages that are not part of the protocol', async function () { let called = 0; - const client = createToolsRpcClient(channel.port1); - serveToolsRequests(channel.port2, () => { + const client = createRuntimeClient(port1); + serveToolsRequests(port2, () => { called += 1; return Promise.resolve(null); }); @@ -125,7 +133,7 @@ describe('tools rpc', function () { it('closes the port when the client is closed', function () { const close = sinon.spy(channel.port1, 'close'); - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(port1); client.close(); @@ -134,10 +142,10 @@ describe('tools rpc', function () { }); it('does not send an abort message for a request that already settled', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(port1); const controller = new AbortController(); const postMessage = sinon.spy(channel.port1, 'postMessage'); - serveToolsRequests(channel.port2, () => Promise.resolve('ok')); + serveToolsRequests(port2, () => Promise.resolve('ok')); await client.call('performToolCall', undefined, controller.signal); postMessage.resetHistory(); @@ -151,7 +159,7 @@ describe('tools rpc', function () { }); it('aborts in-flight requests when the server is disposed', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(port1); let serverSignal: AbortSignal | undefined; // Resolved by the handler, so the test never has to guess how long a // message takes to arrive @@ -159,16 +167,13 @@ describe('tools rpc', function () { 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)); - }); - } - ); + const server = serveToolsRequests(port2, (_method, _params, signal) => { + serverSignal = signal; + requestArrived(); + return new Promise((_resolve, reject) => { + signal.addEventListener('abort', () => reject(signal.reason)); + }); + }); void client.call('performToolCall').catch(() => { // the request is aborted below; the rejection is expected diff --git a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts b/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.ts similarity index 94% rename from packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts rename to packages/compass-generative-ai/src/tools/infrastructure/runtime-client.ts index 5ae9142c88b..4b51f39c4ab 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/rpc-client.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.ts @@ -1,17 +1,16 @@ import { listen } from '@mongodb-js/compass-utils'; -import { isResponse } from './messages'; +import { isResponse } from './protocol'; import type { ToolsRpcAbort, ToolsRpcMethod, - ToolsRpcPort, ToolsRpcRequest, -} from './messages'; +} from './protocol'; /** * Renderer-side client. `port` is the end of the channel returned by * `openUtilityPort`. */ -export function createToolsRpcClient(port: ToolsRpcPort): { +export function createRuntimeClient(port: MessagePort): { call( method: ToolsRpcMethod, params?: unknown, diff --git a/packages/compass-generative-ai/src/tools/infrastructure/server.ts b/packages/compass-generative-ai/src/tools/infrastructure/session.ts similarity index 89% rename from packages/compass-generative-ai/src/tools/infrastructure/server.ts rename to packages/compass-generative-ai/src/tools/infrastructure/session.ts index ce2b2a253f3..6170c5779fe 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/server.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/session.ts @@ -1,18 +1,14 @@ import { listen } from '@mongodb-js/compass-utils'; -import { isToolsRpcMessage } from './messages'; -import type { - ToolsRpcMethod, - ToolsRpcPort, - ToolsRpcResponse, -} from './messages'; +import { isToolsRpcMessage } from './protocol'; +import type { ToolsRpcMethod, ToolsRpcResponse } from './protocol'; /** - * Utility-side server. `port` is a `MessagePortMain` received through + * Utility-side server. `port` is a `MessagePort` received through * `onPort` (already started). Each request gets its own AbortController; * abort messages for that id abort it. */ export function serveToolsRequests( - port: ToolsRpcPort, + port: MessagePort, handler: ( method: ToolsRpcMethod, params: unknown, 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 712e02e3f74..b6617eaf08f 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 @@ -3,7 +3,7 @@ import { expect } from 'chai'; import sinon from 'sinon'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import type { Logger } from '@mongodb-js/compass-logging'; -import { createToolsRpcClient } from './rpc-client'; +import { createRuntimeClient } from './runtime-client'; import { encodeConnectParams } from './connect-params'; import { ToolsHost } from './tools-host'; import type { ToolDefinitionInfo } from './tools-host'; @@ -17,10 +17,15 @@ describe('ToolsHost', function () { let host: ToolsHost; let logger: Logger; + // worker_threads ports are structurally identical to DOM MessagePorts but + // not nominally, so tests bridge them with this cast. + const asRendererPort = (port: MessageChannel['port1']): MessagePort => + port as unknown as MessagePort; + beforeEach(function () { channel = new MessageChannel(); logger = createNoopLogger(); - host = new ToolsHost(channel.port2, { + host = new ToolsHost(asRendererPort(channel.port2), { logger, }); }); @@ -32,7 +37,7 @@ describe('ToolsHost', function () { }); it('lists read-only database tools as JSON Schema', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(asRendererPort(channel.port1)); const tools = (await client.call( 'listDatabaseTools' )) as ToolDefinitionInfo[]; @@ -48,7 +53,7 @@ describe('ToolsHost', function () { }); it('returns an error result when the connection fails', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(asRendererPort(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', { @@ -72,7 +77,7 @@ describe('ToolsHost', function () { }); it('applies setUserConfig to the runner config', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(asRendererPort(channel.port1)); await client.call('setUserConfig', { enableMCPTelemetry: true, maxTimeMS: 1000, @@ -82,7 +87,7 @@ describe('ToolsHost', function () { }); it('releases the server and the connection when stopped', async function () { - const client = createToolsRpcClient(channel.port1); + const client = createRuntimeClient(asRendererPort(channel.port1)); await client.call('listDatabaseTools'); const connectionManager = (host as any).connectionManager; const disconnect = sinon.spy(connectionManager, 'disconnect'); 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 9852a500b20..1e38c999256 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -7,10 +7,10 @@ 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 { ToolsRpcMethod } from './protocol'; import type { ConnectParamsWire } from './connect-params'; import { decodeConnectParams } from './connect-params'; -import { serveToolsRequests } from './server'; +import { serveToolsRequests } from './session'; import { InMemoryRunner } from './in-memory-runner'; import { NoopMetrics } from './noop-metrics'; @@ -37,7 +37,7 @@ export class ToolsHost { private readonly disposable: Disposable; private telemetryAnonymousId = ''; - constructor(port: ToolsRpcPort, { logger }: ToolsHostConfig) { + constructor(port: MessagePort, { logger }: ToolsHostConfig) { this.logger = logger; const mcpConfig = UserConfigSchema.parse({ disabledTools: ['connect'], diff --git a/packages/compass-utilities/package.json b/packages/compass-utilities/package.json index 638c886ef1e..49bd84a04e2 100644 --- a/packages/compass-utilities/package.json +++ b/packages/compass-utilities/package.json @@ -46,6 +46,7 @@ "typescript": "^6.0.3" }, "dependencies": { + "@mongodb-js/compass-generative-ai": "^1.7.0", "@mongodb-js/compass-logging": "^1.18.2", "@mongodb-js/compass-shell": "^4.6.0", "@mongodb-js/compass-utils": "^0.10.6", diff --git a/packages/compass/src/utilities/ai-assistant/index.mts b/packages/compass-utilities/src/ai-assistant/index.mts similarity index 82% rename from packages/compass/src/utilities/ai-assistant/index.mts rename to packages/compass-utilities/src/ai-assistant/index.mts index d2c37625d67..846a09888db 100644 --- a/packages/compass/src/utilities/ai-assistant/index.mts +++ b/packages/compass-utilities/src/ai-assistant/index.mts @@ -1,14 +1,14 @@ 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 type { UtilityParentPort } from '../ipc.mts'; +import { currentParentPort, onPort } from '../ipc.mts'; export const name = 'ai-assistant'; -export function main(parentPort: ParentPort): Disposable { +export function main(parentPort: UtilityParentPort): Disposable { using ds = new DisposableStack(); ds.use( @@ -54,6 +54,7 @@ export function main(parentPort: ParentPort): Disposable { return ds.move(); } -if (import.meta.main) { - main(process.parentPort); +const parentPort = currentParentPort(); +if (parentPort) { + main(parentPort); } diff --git a/packages/compass/src/utilities/ai-assistant/index.spec.mts b/packages/compass-utilities/src/ai-assistant/index.spec.mts similarity index 100% rename from packages/compass/src/utilities/ai-assistant/index.spec.mts rename to packages/compass-utilities/src/ai-assistant/index.spec.mts diff --git a/packages/compass-web/polyfills/net/index.ts b/packages/compass-web/polyfills/net/index.ts index 52c9954dd23..878604749ae 100644 --- a/packages/compass-web/polyfills/net/index.ts +++ b/packages/compass-web/polyfills/net/index.ts @@ -1,6 +1,23 @@ import { Duplex } from 'node:stream'; import { ipVersion } from 'is-ip'; -import { getMultiplexLink } from '../../src/multiplex-link'; +import { + getMultiplexLink, + setMultiplexLink, + Link, +} from '../../src/multiplex-link'; + +// Utilities run as Web Workers with their own module realm, so the page's +// multiplex link is invisible here and nothing in the worker registers one. +// The host enriches every port handoff with the WebSocket candidates the page +// link uses; create our own link from them before the first driver connect. +if ('WorkerGlobalScope' in globalThis) { + globalThis.addEventListener('message', (event) => { + const { baseUrls } = (event as MessageEvent).data ?? {}; + if (Array.isArray(baseUrls) && baseUrls.length > 0 && !getMultiplexLink()) { + setMultiplexLink(new Link({ baseUrls })); + } + }); +} type ConnectOptions = { host: string; diff --git a/packages/compass-web/polyfills/tr46/index.ts b/packages/compass-web/polyfills/tr46/index.ts index 97a39f947d4..5dee56b9808 100644 --- a/packages/compass-web/polyfills/tr46/index.ts +++ b/packages/compass-web/polyfills/tr46/index.ts @@ -6,7 +6,9 @@ */ export function toASCII(domain: string) { try { - return new window.URL(`http://${domain}`).hostname; + // globalThis, not window: utilities run as Web Workers, where `window` + // doesn't exist and this would fail every domain parse + return new globalThis.URL(`http://${domain}`).hostname; } catch { return null; } diff --git a/packages/compass-web/src/entrypoint.tsx b/packages/compass-web/src/entrypoint.tsx index a45a84f7115..b3729c54529 100644 --- a/packages/compass-web/src/entrypoint.tsx +++ b/packages/compass-web/src/entrypoint.tsx @@ -104,7 +104,6 @@ import type { } from './logger-and-telemetry'; import { startUtilities } from './utilities.mts'; 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'; @@ -563,7 +562,6 @@ const CompassWebWithPreferences = ({ onTrack, preferences, }); - const openToolsPort = useToolsLoopback(); useEffect(() => { const utilities = startUtilities(window, logger); @@ -594,7 +592,7 @@ const CompassWebWithPreferences = ({ orgId={orgId} projectId={projectId} > - + (); - const logger = createNoopLogger(); - - const first = openToolsLoopback(hosts, logger); - const second = openToolsLoopback(hosts, logger); - - expect(first).to.not.equal(second); - expect(hosts.size).to.equal(2); - }); -}); diff --git a/packages/compass-web/src/tools-loopback.ts b/packages/compass-web/src/tools-loopback.ts deleted file mode 100644 index 9ff70bdad8b..00000000000 --- a/packages/compass-web/src/tools-loopback.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { useCallback, useEffect, useRef } from 'react'; -import { useLogger } from '@mongodb-js/compass-logging/provider'; -import type { Logger } from '@mongodb-js/compass-logging'; -import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility'; - -/** - * The channel between the renderer and the in-page host. Both ends listen - * through `addEventListener`, and a MessagePort only dispatches queued messages - * once started, so both have to be started here: the desktop equivalents do it - * in `openUtilityPort` and `onPort`. - */ -export function createToolsLoopbackChannel(): { - openPort: () => MessagePort; - hostPort: MessagePort; -} { - const channel = new MessageChannel(); - channel.port1.start(); - channel.port2.start(); - return { openPort: () => channel.port1, hostPort: channel.port2 }; -} - -export function openToolsLoopback( - hosts: Set, - logger: Logger -): MessagePort { - const { openPort, hostPort } = createToolsLoopbackChannel(); - hosts.add(new ToolsHost(hostPort, { logger })); - return openPort(); -} - -export function useToolsLoopback(): () => MessagePort { - const logger = useLogger('TOOLS-CONTROLLER'); - const hosts = useRef(new Set()); - - const openPort = useCallback( - () => openToolsLoopback(hosts.current, logger), - [logger] - ); - - useEffect(() => { - const live = hosts.current; - return () => { - for (const host of live) { - void host.stop(); - } - live.clear(); - }; - }, []); - - return openPort; -} diff --git a/packages/compass-web/src/utilities.mts b/packages/compass-web/src/utilities.mts index 339187e1f26..c468adc2abc 100644 --- a/packages/compass-web/src/utilities.mts +++ b/packages/compass-web/src/utilities.mts @@ -3,13 +3,22 @@ import { listen, utilityPortChannel, } from '@mongodb-js/compass-utils'; +import { getMultiplexLink } from './multiplex-link.ts'; import { CrossOriginWorker } from './cross-origin-worker.mts'; import type { CompassWebLogger } from './logger-and-telemetry.tsx'; -const UTILITIES = new Map CrossOriginWorker>(); - -// Keeps the import until the first utility is registered above -void CrossOriginWorker; +const UTILITIES: Map CrossOriginWorker> = new Map([ + [ + 'ai-assistant', + () => + new CrossOriginWorker( + new URL( + '../../compass-utilities/src/ai-assistant/index.mts', + import.meta.url + ) + ), + ], +]); export function startUtilities( target: Window, @@ -30,6 +39,10 @@ export function startUtilities( ); ds.use( listen(worker, 'error', (event: ErrorEvent) => { + // The hosted logger is invisible in the browser console, and this is + // the only place a worker boot failure surfaces, so mirror it. + // eslint-disable-next-line no-console + console.error(`Utility worker ${name} failed:`, errorFromEvent(event)); logger.log.error( logger.mongoLogId(1_001_000_453), 'Utilities', @@ -44,7 +57,14 @@ export function startUtilities( listen(target, 'message', (event: MessageEvent) => { if (event.source !== target) return; const worker = workers.get(event.data?.type); - worker?.postMessage(event.data, [...event.ports]); + // Workers run in their own module realm and can't see the page's + // multiplex link, so every port handoff also carries the WebSocket + // candidates the link uses; the worker's net polyfill builds its own + // link from them. + worker?.postMessage( + { ...event.data, baseUrls: getMultiplexLink()?.urls }, + [...event.ports] + ); }) ); From 3441e512ae9074c9bc6b1df1ee8b91e0b1508ebd Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Mon, 2 Nov 2026 09:35:49 +0000 Subject: [PATCH 11/16] fix refactor --- packages/compass-web/src/utilities.mts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/compass-web/src/utilities.mts b/packages/compass-web/src/utilities.mts index 1c4fa59acb6..ac1dfc537f4 100644 --- a/packages/compass-web/src/utilities.mts +++ b/packages/compass-web/src/utilities.mts @@ -3,7 +3,6 @@ import { listen, utilityPortChannel, } from '@mongodb-js/compass-utils'; -import { getMultiplexLink } from './multiplex-link.ts'; import { CrossOriginWorker } from './cross-origin-worker.mts'; import type { CompassWebLogger } from './logger-and-telemetry.tsx'; import { getLink } from './multiplex-link.ts'; @@ -67,10 +66,7 @@ export function startUtilities( // multiplex link, so every port handoff also carries the WebSocket // candidates the link uses; the worker's net polyfill builds its own // link from them. - worker?.postMessage( - { ...event.data, baseUrls: getMultiplexLink()?.urls }, - [...event.ports] - ); + worker?.postMessage({ ...event.data }, [...event.ports]); }) ); From 14b5c8892224260dd00b2947c369a5084b8ea789 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Mon, 2 Nov 2026 09:37:49 +0000 Subject: [PATCH 12/16] nit --- packages/compass-web/src/utilities.mts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/compass-web/src/utilities.mts b/packages/compass-web/src/utilities.mts index ac1dfc537f4..6b666e94f55 100644 --- a/packages/compass-web/src/utilities.mts +++ b/packages/compass-web/src/utilities.mts @@ -44,10 +44,6 @@ export function startUtilities( ); ds.use( listen(worker, 'error', (event: ErrorEvent) => { - // The hosted logger is invisible in the browser console, and this is - // the only place a worker boot failure surfaces, so mirror it. - // eslint-disable-next-line no-console - console.error(`Utility worker ${name} failed:`, errorFromEvent(event)); logger.log.error( logger.mongoLogId(1_001_000_453), 'Utilities', @@ -62,11 +58,7 @@ export function startUtilities( listen(target, 'message', (event: MessageEvent) => { if (event.source !== target) return; const worker = workers.get(event.data?.type); - // Workers run in their own module realm and can't see the page's - // multiplex link, so every port handoff also carries the WebSocket - // candidates the link uses; the worker's net polyfill builds its own - // link from them. - worker?.postMessage({ ...event.data }, [...event.ports]); + worker?.postMessage(event.data, [...event.ports]); }) ); From 8c9a9dbb27b4e730823e911c33a43e6460599622 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Mon, 2 Nov 2026 10:08:50 +0000 Subject: [PATCH 13/16] fix log number --- .../src/tools/infrastructure/tools-host.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1e38c999256..8daf584619a 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/tools-host.ts @@ -150,7 +150,7 @@ export class ToolsHost { ); } catch (error: any) { this.logger.log.error( - this.logger.mongoLogId(1_001_000_453), + this.logger.mongoLogId(1_001_000_455), 'ToolsHost', 'Error when attempting to connect to Compass connection before executing tool', { error: error.message } From 3ed710f33ef3b803469bbafa7da6ac1d409c4a03 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Mon, 2 Nov 2026 10:38:22 +0000 Subject: [PATCH 14/16] lint --- packages/compass-assistant/test/tool-calls.eval.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/compass-assistant/test/tool-calls.eval.ts b/packages/compass-assistant/test/tool-calls.eval.ts index fcab42ba971..fb7460b963d 100644 --- a/packages/compass-assistant/test/tool-calls.eval.ts +++ b/packages/compass-assistant/test/tool-calls.eval.ts @@ -71,7 +71,13 @@ const logger = createNoopLogger('EVAL-TOOLS-CONTROLLER'); // other end of a plain MessageChannel. Held in a variable so the run can // release its server and driver connection when it finishes. const evalChannel = new MessageChannel(); -const evalHost = new ToolsHost(evalChannel.port2, { logger }); + +// worker_threads ports are structurally identical to DOM MessagePorts but not +// nominally, so tests bridge them with this cast. +const asRendererPort = (port: MessageChannel['port1']): MessagePort => + port as unknown as MessagePort; + +const evalHost = new ToolsHost(asRendererPort(evalChannel.port2), { logger }); const toolsController = new ToolsController({ logger, @@ -80,7 +86,7 @@ const toolsController = new ToolsController({ enableMCPTelemetry: false, preferences: defaultPreferencesInstance, atlasAdminApi: {} as AtlasAdminApiService, - openPort: () => evalChannel.port1, + openPort: () => asRendererPort(evalChannel.port1), }); let toolsInitialized = false; From ce867f0caa5b3d18450d09950c3fbfaace3f249c Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Mon, 2 Nov 2026 15:16:11 +0000 Subject: [PATCH 15/16] address comments --- .../src/tools-connection-manager.ts | 3 +- .../src/tools-controller.ts | 29 ++++++++----------- .../src/tools/infrastructure/session.ts | 6 +--- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/packages/compass-generative-ai/src/tools-connection-manager.ts b/packages/compass-generative-ai/src/tools-connection-manager.ts index 9b63236066c..a7339c3b468 100644 --- a/packages/compass-generative-ai/src/tools-connection-manager.ts +++ b/packages/compass-generative-ai/src/tools-connection-manager.ts @@ -10,6 +10,7 @@ import { } from '@mongosh/service-provider-node-driver'; import type { ServiceProvider } from '@mongosh/service-provider-core'; import { isAtlas, isAtlasStream } from '@mongodb-js/mongodb-build-info'; +import type { Logger } from '@mongodb-js/compass-logging'; import ConnectionString from 'mongodb-connection-string-url'; export interface ToolsConnectParams { @@ -24,7 +25,7 @@ const DEFAULT_TELEMETRY_APP_NAME = 'MongoDB Compass'; type ToolsConnectionManagerConfig = { logger: LoggerBase; getTelemetryAnonymousId: () => string; - mongoLogId: (id: number) => { __value: number }; + mongoLogId: Logger['mongoLogId']; }; export class ToolsConnectionManager extends ConnectionManager { diff --git a/packages/compass-generative-ai/src/tools-controller.ts b/packages/compass-generative-ai/src/tools-controller.ts index 3ab140fbf29..4317e374a91 100644 --- a/packages/compass-generative-ai/src/tools-controller.ts +++ b/packages/compass-generative-ai/src/tools-controller.ts @@ -237,16 +237,21 @@ export class ToolsController { ); this.portClient = createRuntimeClient(this.openPort()); - await this.withStartupTimeout(async () => { - await this.portClient!.call('setUserConfig', { + const signal = AbortSignal.timeout(ASSISTANT_TOOLS_STARTUP_TIMEOUT_MS); + await this.portClient.call( + 'setUserConfig', + { ...this.currentConfig, telemetryAnonymousId: this.getTelemetryAnonymousId(), - }); + }, + signal + ); - this.dbToolDefinitions = (await this.portClient!.call( - 'listDatabaseTools' - )) as ToolDefinitionInfo[]; - }); + this.dbToolDefinitions = (await this.portClient.call( + 'listDatabaseTools', + undefined, + signal + )) as ToolDefinitionInfo[]; this.logger.log.info( this.logger.mongoLogId(1_001_000_403), @@ -267,16 +272,6 @@ export class ToolsController { } } - 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( diff --git a/packages/compass-generative-ai/src/tools/infrastructure/session.ts b/packages/compass-generative-ai/src/tools/infrastructure/session.ts index 6170c5779fe..8491a724ff3 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/session.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/session.ts @@ -32,11 +32,7 @@ export function serveToolsRequests( 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)) + handler(message.method, message.params, controller.signal) .then((result) => { port.postMessage({ kind: 'response', From de1763f516b472a33790982f891993e87f5b1978 Mon Sep 17 00:00:00 2001 From: Edjan Michiles Date: Mon, 2 Nov 2026 15:31:42 +0000 Subject: [PATCH 16/16] woops --- .../src/tools/infrastructure/runtime-client.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.spec.ts b/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.spec.ts index 661fd51a0e8..879ccba6417 100644 --- a/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.spec.ts +++ b/packages/compass-generative-ai/src/tools/infrastructure/runtime-client.spec.ts @@ -37,8 +37,8 @@ describe('tools rpc', function () { it('rejects with the error message from the other side', async function () { const client = createRuntimeClient(port1); - serveToolsRequests(port2, () => { - throw new Error('boom'); + serveToolsRequests(port2, async () => { + return Promise.reject(new Error('boom')); }); const error = await client