Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/backend/src/mcp-server/mcp-server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ export class McpServerService implements OnModuleInit {
): Promise<void> {
const { syncKg = true, notifySessions = true } = opts;
// Remove old tools from both registries
const oldTools = this.toolRegistry
.getAllTools()
.filter((t) => t.connectorId === connectorId);
const oldTools = this.toolRegistry.getToolsForConnectors([connectorId]);
this.toolRegistry.unregisterConnectorTools(connectorId);
for (const tool of oldTools) {
// Only drop from the upstream MCP registry if no other connector
Expand Down
61 changes: 61 additions & 0 deletions packages/backend/src/mcp-server/tool-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ describe('ToolRegistry', () => {
registry.registerTool(tool1);
registry.registerTool(tool2);
expect(registry.getToolCount()).toBe(1);
expect(registry.countByName('test_tool')).toBe(1);
expect(registry.getTool('test_tool')).toBe(tool2);
});

it('moves a tool re-registered under a new name out of the old name', () => {
registry.registerTool(makeTool({ id: 'tool-1', name: 'old_name' }));
registry.registerTool(makeTool({ id: 'tool-1', name: 'new_name' }));
expect(registry.countByName('old_name')).toBe(0);
expect(registry.getTool('old_name')).toBeUndefined();
expect(registry.getTool('new_name')?.id).toBe('tool-1');
});
});

Expand Down Expand Up @@ -183,6 +193,57 @@ describe('ToolRegistry', () => {
expect(registry.getToolsForOrganization('o2').map((x) => x.connectorId)).toEqual(['c2']);
});

it('keeps the other connectors under a shared name, in order, on unregister', () => {
const shared = (id: string, connectorId: string, organizationId: string) =>
makeTool({ id, connectorId, organizationId, name: 'shared' });
registry.registerTool(shared('a', 'c1', 'o1'));
registry.registerTool(shared('b', 'c2', 'o2'));
registry.registerTool(shared('c', 'c3', 'o3'));
registry.unregisterConnectorTools('c1');
expect(registry.countByName('shared')).toBe(2);
expect(registry.getTool('shared')?.id).toBe('b');
expect(registry.getToolForOrg('shared', 'o3')?.id).toBe('c');
expect(registry.getToolForOrg('shared', 'o1')).toBeUndefined();
registry.unregisterConnectorTools('c2');
registry.unregisterConnectorTools('c3');
expect(registry.countByName('shared')).toBe(0);
expect(registry.getTool('shared')).toBeUndefined();
});

it('counts tools per connector from the index', () => {
registry.registerTool(t('a', 'c1', 'o1'));
registry.registerTool(t('b', 'c1', 'o1'));
registry.registerTool(t('c', 'c2', 'o2'));
expect(registry.countByConnector()).toEqual(new Map([['c1', 2], ['c2', 1]]));
registry.unregisterConnectorTools('c1');
expect(registry.countByConnector()).toEqual(new Map([['c2', 1]]));
});

it('reloads one connector without walking the rest of the registry', () => {
// 200k tools in 20k connectors, about the Cloud registry's shape. A
// reload used to scan and re-index all of them: 100 reloads took
// seconds. Through the connector index they take milliseconds.
jest.spyOn((registry as any).logger, 'debug').mockImplementation(() => undefined);
for (let c = 0; c < 20_000; c++) {
for (let i = 0; i < 10; i++) {
registry.registerTool(
makeTool({ id: `t${c}-${i}`, connectorId: `c${c}`, organizationId: `o${c % 5000}`, name: `tool_${i}` }),
);
}
}
const mine = registry.getToolsForConnectors(['c42']);
const started = Date.now();
for (let n = 0; n < 100; n++) {
registry.unregisterConnectorTools('c42');
mine.forEach((tool) => registry.registerTool(tool));
}
const elapsed = Date.now() - started;
if (process.env.REGISTRY_BENCH) console.log(`100 reloads: ${elapsed} ms`);
expect(elapsed).toBeLessThan(250);
expect(registry.getToolCount()).toBe(200_000);
expect(registry.countByName('tool_3')).toBe(20_000);
});

it('drops a connector\'s tools from both indexes on unregister', () => {
registry.registerTool(t('a', 'c1', 'o1'));
registry.registerTool(t('b', 'c2', 'o1'));
Expand Down
67 changes: 39 additions & 28 deletions packages/backend/src/mcp-server/tool-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ export class ToolRegistry {
private readonly logger = new Logger(ToolRegistry.name);
// Keyed by tool.id (globally unique) to prevent cross-org name collisions
private readonly toolsById = new Map<string, RegisteredTool>();
// Secondary index by name for backward-compatible lookups (within a filtered set)
private readonly toolsByName = new Map<string, RegisteredTool[]>();
// Secondary index by name for backward-compatible lookups (within a filtered
// set). A Map per name, like the other indexes, so dropping one tool from a
// name shared by thousands of installs of a catalog adapter costs O(1).
private readonly toolsByName = new Map<string, Map<string, RegisteredTool>>();
// Per-organization and per-connector indexes, so a request reads its own
// tools instead of copying and filtering the whole registry (134k tools on
// Cloud in Nov 2026, once per /mcp request). Each keeps the registry's
Expand Down Expand Up @@ -107,33 +109,37 @@ export class ToolRegistry {
this.toolsById.set(tool.id, tool);
this.addToIndex(this.toolsByOrg, tool.organizationId, tool);
this.addToIndex(this.toolsByConnector, tool.connectorId, tool);
// Append to name index (multiple tools can share a name across orgs)
const existing = this.toolsByName.get(tool.name) || [];
existing.push(tool);
this.toolsByName.set(tool.name, existing);
// Name index (multiple tools can share a name across orgs). A tool
// registered again under the same id replaces its old entry in place, so
// the name keeps one entry per tool and its first-registered order.
if (previous && previous.name !== tool.name) {
this.removeFromIndex(this.toolsByName, previous.name, tool.id);
}
this.addToIndex(this.toolsByName, tool.name, tool);
this.logger.debug(`Registered MCP tool: ${tool.name} (${tool.id})`);
}

/**
* Unregister all tools belonging to a connector.
*
* Reads only that connector's tools through the per-connector index. It
* used to walk the whole registry and then rebuild the name index from
* scratch: two passes over every tool of every organization (316k on Cloud
* in Nov 2026) for each reload, and a dashboard tool toggle reloads its
* connector. A burst of toggles, or a registry catch-up after a busy day,
* then held the event loop long enough to slow every /mcp request.
*/
unregisterConnectorTools(connectorId: string): void {
for (const [id, tool] of this.toolsById.entries()) {
if (tool.connectorId === connectorId) {
this.toolsById.delete(id);
this.order.delete(id);
this.removeFromIndex(this.toolsByOrg, tool.organizationId, id);
this.logger.debug(`Unregistered MCP tool: ${tool.name} (${id})`);
}
const tools = this.toolsByConnector.get(connectorId);
if (!tools) return;
for (const [id, tool] of tools) {
this.toolsById.delete(id);
this.order.delete(id);
this.removeFromIndex(this.toolsByOrg, tool.organizationId, id);
this.removeFromIndex(this.toolsByName, tool.name, id);
this.logger.debug(`Unregistered MCP tool: ${tool.name} (${id})`);
}
this.toolsByConnector.delete(connectorId);
// Rebuild name index
this.toolsByName.clear();
for (const tool of this.toolsById.values()) {
const existing = this.toolsByName.get(tool.name) || [];
existing.push(tool);
this.toolsByName.set(tool.name, existing);
}
}

/**
Expand All @@ -142,9 +148,11 @@ export class ToolRegistry {
*/
getTool(name: string, connectorIds?: string[]): RegisteredTool | undefined {
const candidates = this.toolsByName.get(name);
if (!candidates || candidates.length === 0) return undefined;
if (!connectorIds) return candidates[0];
return candidates.find((t) => connectorIds.includes(t.connectorId));
if (!candidates) return undefined;
for (const t of candidates.values()) {
if (!connectorIds || connectorIds.includes(t.connectorId)) return t;
}
return undefined;
}

/**
Expand All @@ -159,8 +167,11 @@ export class ToolRegistry {
organizationId: string,
): RegisteredTool | undefined {
const candidates = this.toolsByName.get(name);
if (!candidates || candidates.length === 0) return undefined;
return candidates.find((t) => t.organizationId === organizationId);
if (!candidates) return undefined;
for (const t of candidates.values()) {
if (t.organizationId === organizationId) return t;
}
return undefined;
}

/**
Expand Down Expand Up @@ -227,7 +238,7 @@ export class ToolRegistry {
* exposed in the upstream MCP library's (single-tenant) registry.
*/
countByName(name: string): number {
return this.toolsByName.get(name)?.length ?? 0;
return this.toolsByName.get(name)?.size ?? 0;
}

/**
Expand All @@ -237,8 +248,8 @@ export class ToolRegistry {
*/
countByConnector(): Map<string, number> {
const counts = new Map<string, number>();
for (const tool of this.toolsById.values()) {
counts.set(tool.connectorId, (counts.get(tool.connectorId) ?? 0) + 1);
for (const [connectorId, tools] of this.toolsByConnector) {
counts.set(connectorId, tools.size);
}
return counts;
}
Expand Down
Loading