From 5c59d450ddea11dbd4c84b63b8ecf4b8a6b2b85d Mon Sep 17 00:00:00 2001 From: Elliott de Launay Date: Thu, 5 Nov 2026 01:16:41 +0000 Subject: [PATCH 1/3] feat(tools): add singleton router with lazy indexing and MCP invalidation --- src/services/mcp/McpHub.ts | 39 ++++++ src/services/tools/ToolRouter.ts | 49 +++++++ .../__tests__/ToolRouter.integration.spec.ts | 130 ++++++++++++++++++ .../tools/__tests__/ToolRouter.spec.ts | 65 +++++++++ src/services/tools/index.ts | 7 + 5 files changed, 290 insertions(+) create mode 100644 src/services/tools/ToolRouter.ts create mode 100644 src/services/tools/__tests__/ToolRouter.integration.spec.ts create mode 100644 src/services/tools/__tests__/ToolRouter.spec.ts create mode 100644 src/services/tools/index.ts diff --git a/src/services/mcp/McpHub.ts b/src/services/mcp/McpHub.ts index c973f35a03..45bf866fca 100644 --- a/src/services/mcp/McpHub.ts +++ b/src/services/mcp/McpHub.ts @@ -1,5 +1,6 @@ import * as fs from "fs/promises" import * as path from "path" +import { EventEmitter } from "node:events" import * as vscode from "vscode" import { Client } from "@modelcontextprotocol/sdk/client/index.js" @@ -43,6 +44,7 @@ import { arePathsEqual, getWorkspacePath } from "../../utils/path" import { injectVariables } from "../../utils/config" import { safeWriteJson } from "../../utils/safeWriteJson" import { sanitizeMcpName, toolNamesMatch } from "../../utils/mcp-name" +import { ToolRouter } from "../tools/ToolRouter" // Discriminated union for connection states export type ConnectedMcpConnection = { @@ -163,6 +165,25 @@ const McpSettingsSchema = z.object({ }) export class McpHub { + private readonly serverListEvents = new EventEmitter() + private serverListKey?: string + + public onServerListChanged(listener: () => void): { dispose(): void } { + const safeListener = () => { + try { + listener() + } catch (error) { + console.error("[McpHub] Server-list change listener failed:", error) + } + } + this.serverListEvents.on("changed", safeListener) + return { + dispose: () => { + this.serverListEvents.off("changed", safeListener) + }, + } + } + private providerRef: WeakRef private disposables: vscode.Disposable[] = [] private settingsWatcher?: vscode.FileSystemWatcher @@ -183,6 +204,7 @@ export class McpHub { constructor(provider: ClineProvider, secretStorage?: SecretStorageService) { this.providerRef = new WeakRef(provider) + this.disposables.push(ToolRouter.getInstance().registerHub(this)) if (secretStorage) { this.secretStorage = secretStorage } @@ -1855,6 +1877,21 @@ export class McpHub { } private async notifyWebviewOfServerChanges(): Promise { + // Notify internal consumers before filesystem or webview work can fail. + const key = JSON.stringify( + this.connections.map(({ server }) => ({ + name: server.name, + source: server.source, + config: server.config, + status: server.status, + disabled: server.disabled, + tools: server.tools, + })), + ) + if (key !== this.serverListKey) { + this.serverListKey = key + this.serverListEvents.emit("changed") + } // Get global server order from settings file const settingsPath = await this.getMcpSettingsFilePath() const content = await fs.readFile(settingsPath, "utf-8") @@ -2537,6 +2574,8 @@ export class McpHub { } this.connections = [] + this.serverListEvents.emit("changed") + this.serverListEvents.removeAllListeners() if (this.settingsWatcher) { this.settingsWatcher.dispose() diff --git a/src/services/tools/ToolRouter.ts b/src/services/tools/ToolRouter.ts new file mode 100644 index 0000000000..edbd6c7e77 --- /dev/null +++ b/src/services/tools/ToolRouter.ts @@ -0,0 +1,49 @@ +import { createHash } from "node:crypto" +import { buildMcpToolName } from "../../utils/mcp-name" +import type { McpHub } from "../mcp/McpHub" +import { createRanker } from "./index" +import type { Ranker, ToolDoc } from "./types" + +/** Process-wide routing cache. Only caller-scoped tools are ever ranked. */ +export class ToolRouter { + private static instance: ToolRouter | undefined + private ranker?: Ranker + private indexedTools?: { key: string; tools: ToolDoc[] } + + private constructor() {} + + static getInstance(): ToolRouter { + return (this.instance ??= new ToolRouter()) + } + + /** The hub owns this subscription and disposes it with its other resources. */ + registerHub(hub: Pick): { dispose(): void } { + return hub.onServerListChanged(() => this.invalidate()) + } + + private invalidate(): void { + this.indexedTools = undefined + this.ranker = undefined + } + + async attachInitial(taskText: string, scopedTools: ToolDoc[], k: number): Promise { + return this.search(taskText, scopedTools, k) + } + + async search(query: string, scopedTools: ToolDoc[], k: number): Promise { + // Top-K requires a finite positive limit; fractional limits round down. + if (scopedTools.length === 0 || !query.trim() || !Number.isFinite(k) || k < 1) { + return [] + } + + // Content keys allow equivalent caller arrays to share the index while + // keeping task scopes and changed tool descriptions isolated. + const key = createHash("sha256").update(JSON.stringify(scopedTools)).digest("hex") + if (this.indexedTools?.key !== key) { + this.indexedTools = { key, tools: scopedTools.map((tool) => ({ ...tool })) } + } + this.ranker ??= createRanker() + const ranked = await this.ranker.rank(query, this.indexedTools.tools, Math.floor(k)) + return ranked.map((tool) => buildMcpToolName(tool.serverName, tool.toolName)) + } +} diff --git a/src/services/tools/__tests__/ToolRouter.integration.spec.ts b/src/services/tools/__tests__/ToolRouter.integration.spec.ts new file mode 100644 index 0000000000..a1dc5589c1 --- /dev/null +++ b/src/services/tools/__tests__/ToolRouter.integration.spec.ts @@ -0,0 +1,130 @@ +import * as fs from "fs/promises" +import * as os from "os" +import * as path from "path" +import type { McpServer } from "@roo-code/types" +import type { ClineProvider } from "../../../core/webview/ClineProvider" +import { GlobalFileNames } from "../../../shared/globalFileNames" +import { McpHub } from "../../mcp/McpHub" +import { Bm25Ranker } from "../Bm25Ranker" +import { ToolRouter } from "../ToolRouter" + +vi.mock("../../../core/webview/ClineProvider") + +describe("ToolRouter with McpHub", () => { + let directory: string + let hub: McpHub + const router = ToolRouter.getInstance() + const tools = [{ serverName: "github", toolName: "issues", description: "search tickets" }] + function addServer(): McpServer { + const server: McpServer = { + name: "github", + config: JSON.stringify({ command: "node" }), + status: "connecting", + source: "global", + } + hub.connections = [{ type: "disconnected", client: null, transport: null, server }] + return server + } + + beforeEach(async () => { + directory = await fs.mkdtemp(path.join(os.tmpdir(), "tool-router-")) + const provider: Partial = { + cwd: directory, + ensureSettingsDirectoryExists: async () => directory, + postMessageToWebview: vi.fn().mockResolvedValue(undefined), + getState: vi.fn().mockResolvedValue({ mcpEnabled: false }), + } + // The hub uses only cwd, settings directory, state, and webview posting. + hub = new McpHub(provider as ClineProvider) + await hub.waitUntilReady() + }) + + afterEach(async () => { + await hub.dispose() + await fs.rm(directory, { recursive: true, force: true }) + vi.restoreAllMocks() + }) + + it("returns empty before any servers are configured", async () => { + expect(hub.getServers()).toEqual([]) + expect(await router.attachInitial("tickets", [], 5)).toEqual([]) + expect(await router.search("tickets", [], 5)).toEqual([]) + }) + + it("invalidates the lazy index when a server is disabled and enabled", async () => { + await fs.writeFile( + path.join(directory, GlobalFileNames.mcpSettings), + JSON.stringify({ + mcpServers: { github: { command: "node" } }, + }), + ) + // An in-flight connection allows toggling without starting an external MCP process. + addServer() + const changed = vi.fn() + const subscription = hub.onServerListChanged(changed) + const rank = vi.spyOn(Bm25Ranker.prototype, "rank") + expect(await router.search("tickets", tools, 5)).toEqual(["mcp--github--issues"]) + const firstIndex = rank.mock.calls[0][1] + + await hub.toggleServerDisabled("github", true) + expect(changed).toHaveBeenCalledTimes(1) + expect(hub.getServers()).toEqual([]) + expect(router["indexedTools"]).toBeUndefined() + expect(await router.search("tickets", [], 5)).toEqual([]) + + await hub.toggleServerDisabled("github", false) + expect(changed).toHaveBeenCalledTimes(2) + expect(hub.getServers()).toHaveLength(1) + expect(await router.attachInitial("tickets", tools, 5)).toEqual(["mcp--github--issues"]) + expect(rank.mock.calls[1][1]).not.toBe(firstIndex) + const enabledIndex = rank.mock.calls[1][1] + await hub["notifyWebviewOfServerChanges"]() + expect(changed).toHaveBeenCalledTimes(2) + await router.search("tickets", tools, 5) + expect(rank.mock.calls[2][1]).toBe(enabledIndex) + subscription.dispose() + await hub.dispose() + expect(changed).toHaveBeenCalledTimes(2) + expect(router["indexedTools"]).toBeUndefined() + }) + + it.each<[string, Partial]>([ + ["name", { name: "jira" }], + ["source", { source: "project" }], + ["config", { config: JSON.stringify({ command: "python" }) }], + ["status", { status: "connected" }], + ["disabled", { disabled: true }], + ["tools", { tools: [{ name: "issues", description: "tickets" }] }], + ])("invalidates once when server %s changes", async (_field, update) => { + const server = addServer() + await hub["notifyWebviewOfServerChanges"]() + const changed = vi.fn() + hub.onServerListChanged(changed) + await router.search("tickets", tools, 5) + Object.assign(server, update) + await hub["notifyWebviewOfServerChanges"]() + expect(changed).toHaveBeenCalledTimes(1) + expect(router["indexedTools"]).toBeUndefined() + await hub["notifyWebviewOfServerChanges"]() + expect(changed).toHaveBeenCalledTimes(1) + }) + + it("isolates throwing listeners during notifications and disposal", async () => { + const failure = new Error("listener failed") + const log = vi.spyOn(console, "error").mockImplementation(() => {}) + hub.onServerListChanged(() => { + throw failure + }) + const changed = vi.fn() + hub.onServerListChanged(changed) + addServer() + const post = vi.spyOn(hub["providerRef"].deref()!, "postMessageToWebview") + await expect(hub["notifyWebviewOfServerChanges"]()).resolves.toBeUndefined() + expect(post).toHaveBeenCalled() + expect(changed).toHaveBeenCalledTimes(1) + expect(log).toHaveBeenCalledWith("[McpHub] Server-list change listener failed:", failure) + await expect(hub.dispose()).resolves.toBeUndefined() + expect(changed).toHaveBeenCalledTimes(2) + expect(hub["serverListEvents"].listenerCount("changed")).toBe(0) + }) +}) diff --git a/src/services/tools/__tests__/ToolRouter.spec.ts b/src/services/tools/__tests__/ToolRouter.spec.ts new file mode 100644 index 0000000000..c380a79be3 --- /dev/null +++ b/src/services/tools/__tests__/ToolRouter.spec.ts @@ -0,0 +1,65 @@ +import { Bm25Ranker } from "../Bm25Ranker" +import { ToolRouter } from "../ToolRouter" +import type { ToolDoc } from "../types" + +const tools: ToolDoc[] = [ + { serverName: "github", toolName: "issues", description: "search tickets" }, + { serverName: "slack", toolName: "messages", description: "search messages" }, +] + +describe("ToolRouter", () => { + afterEach(() => vi.restoreAllMocks()) + + it("shares a process-wide singleton", () => { + expect(ToolRouter.getInstance()).toBe(ToolRouter.getInstance()) + }) + + it.each(["attachInitial", "search"] as const)("%s ranks only the supplied scope", async (method) => { + const router = ToolRouter.getInstance() + expect(await router[method]("tickets", tools, 1)).toEqual(["mcp--github--issues"]) + expect(await router[method]("tickets", [tools[1]], 1)).toEqual([]) + expect(await router[method]("search", tools, 1)).toHaveLength(1) + expect(await router[method]("search", [], 5)).toEqual([]) + expect(await router[method]("search", tools, 0)).toEqual([]) + }) + + it("reuses an index for equivalent arrays and snapshots mutable caller documents", async () => { + const rank = vi.spyOn(Bm25Ranker.prototype, "rank") + const router = ToolRouter.getInstance() + const scope = tools.map((tool) => ({ ...tool })) + await router.search("search", scope, 2) + await router.search( + "tickets", + scope.map((tool) => ({ ...tool })), + 2, + ) + const firstIndex = rank.mock.calls[0][1] + expect(rank.mock.calls[1][1]).toBe(firstIndex) + scope[0].description = "calendar" + expect(await router.search("tickets", scope, 2)).toEqual([]) + expect(rank.mock.calls[2][1]).not.toBe(firstIndex) + expect(firstIndex[0].description).toBe("search tickets") + }) + + it.each(["attachInitial", "search"] as const)("%s qualifies duplicate tool names by server", async (method) => { + expect( + await ToolRouter.getInstance()[method]( + "tickets", + [ + { serverName: "github", toolName: "issues", description: "tickets" }, + { serverName: "jira", toolName: "issues", description: "tickets" }, + ], + 2, + ), + ).toEqual(["mcp--github--issues", "mcp--jira--issues"]) + }) + + it.each(["attachInitial", "search"] as const)("%s validates and floors limits", async (method) => { + const router = ToolRouter.getInstance() + for (const limit of [NaN, Infinity, -Infinity, -1, 0, 0.5]) { + expect(await router[method]("search", tools, limit)).toEqual([]) + } + expect(await router[method]("search", tools, 1.9)).toHaveLength(1) + expect(await router[method]("search", tools, 2)).toHaveLength(2) + }) +}) diff --git a/src/services/tools/index.ts b/src/services/tools/index.ts new file mode 100644 index 0000000000..780cb08715 --- /dev/null +++ b/src/services/tools/index.ts @@ -0,0 +1,7 @@ +import { Bm25Ranker } from "./Bm25Ranker" +import type { Ranker } from "./types" + +// The embeddings implementation (#576) can select its provider here. +export function createRanker(): Ranker { + return new Bm25Ranker() +} From 802554a8eedee05affa73a81bb50d7d53fa6044f Mon Sep 17 00:00:00 2001 From: Elliott de Launay Date: Thu, 5 Nov 2026 01:18:50 +0000 Subject: [PATCH 2/3] fix(tools): deduplicate canonical names before ranking --- src/services/tools/ToolRouter.ts | 11 +++++- .../__tests__/ToolRouter.integration.spec.ts | 38 ++++++++++++++++--- .../tools/__tests__/ToolRouter.spec.ts | 14 +++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/services/tools/ToolRouter.ts b/src/services/tools/ToolRouter.ts index edbd6c7e77..7b87a31352 100644 --- a/src/services/tools/ToolRouter.ts +++ b/src/services/tools/ToolRouter.ts @@ -40,7 +40,16 @@ export class ToolRouter { // keeping task scopes and changed tool descriptions isolated. const key = createHash("sha256").update(JSON.stringify(scopedTools)).digest("hex") if (this.indexedTools?.key !== key) { - this.indexedTools = { key, tools: scopedTools.map((tool) => ({ ...tool })) } + // Canonical names can collide after sanitizing or truncating. Keep the + // first tool per name before ranking so collisions do not use Top-K slots. + const unique = new Map() + for (const tool of scopedTools) { + const name = buildMcpToolName(tool.serverName, tool.toolName) + if (!unique.has(name)) { + unique.set(name, { ...tool }) + } + } + this.indexedTools = { key, tools: [...unique.values()] } } this.ranker ??= createRanker() const ranked = await this.ranker.rank(query, this.indexedTools.tools, Math.floor(k)) diff --git a/src/services/tools/__tests__/ToolRouter.integration.spec.ts b/src/services/tools/__tests__/ToolRouter.integration.spec.ts index a1dc5589c1..6945d82b25 100644 --- a/src/services/tools/__tests__/ToolRouter.integration.spec.ts +++ b/src/services/tools/__tests__/ToolRouter.integration.spec.ts @@ -1,6 +1,7 @@ import * as fs from "fs/promises" import * as os from "os" import * as path from "path" +import type { Mock } from "vitest" import type { McpServer } from "@roo-code/types" import type { ClineProvider } from "../../../core/webview/ClineProvider" import { GlobalFileNames } from "../../../shared/globalFileNames" @@ -13,6 +14,9 @@ vi.mock("../../../core/webview/ClineProvider") describe("ToolRouter with McpHub", () => { let directory: string let hub: McpHub + // The hub holds its provider weakly, so the suite keeps the strong reference. + let provider: Partial + let post: Mock const router = ToolRouter.getInstance() const tools = [{ serverName: "github", toolName: "issues", description: "search tickets" }] function addServer(): McpServer { @@ -28,10 +32,11 @@ describe("ToolRouter with McpHub", () => { beforeEach(async () => { directory = await fs.mkdtemp(path.join(os.tmpdir(), "tool-router-")) - const provider: Partial = { + post = vi.fn().mockResolvedValue(undefined) + provider = { cwd: directory, ensureSettingsDirectoryExists: async () => directory, - postMessageToWebview: vi.fn().mockResolvedValue(undefined), + postMessageToWebview: post, getState: vi.fn().mockResolvedValue({ mcpEnabled: false }), } // The hub uses only cwd, settings directory, state, and webview posting. @@ -117,14 +122,37 @@ describe("ToolRouter with McpHub", () => { }) const changed = vi.fn() hub.onServerListChanged(changed) - addServer() - const post = vi.spyOn(hub["providerRef"].deref()!, "postMessageToWebview") + const server = addServer() + post.mockClear() await expect(hub["notifyWebviewOfServerChanges"]()).resolves.toBeUndefined() - expect(post).toHaveBeenCalled() + expect(post).toHaveBeenCalledWith({ type: "mcpServers", mcpServers: [server] }) expect(changed).toHaveBeenCalledTimes(1) expect(log).toHaveBeenCalledWith("[McpHub] Server-list change listener failed:", failure) await expect(hub.dispose()).resolves.toBeUndefined() expect(changed).toHaveBeenCalledTimes(2) expect(hub["serverListEvents"].listenerCount("changed")).toBe(0) }) + + it("fires the event before the settings read can fail", async () => { + await fs.writeFile(path.join(directory, GlobalFileNames.mcpSettings), "{ not json") + const changed = vi.fn() + hub.onServerListChanged(changed) + addServer() + await expect(hub["notifyWebviewOfServerChanges"]()).rejects.toThrow() + expect(changed).toHaveBeenCalledTimes(1) + }) + + it("fires the event and keeps going when the webview post rejects", async () => { + const failure = new Error("post failed") + const log = vi.spyOn(console, "error").mockImplementation(() => {}) + const changed = vi.fn() + hub.onServerListChanged(changed) + addServer() + post.mockClear() + post.mockRejectedValueOnce(failure) + await expect(hub["notifyWebviewOfServerChanges"]()).resolves.toBeUndefined() + expect(post).toHaveBeenCalledTimes(1) + expect(changed).toHaveBeenCalledTimes(1) + expect(log).toHaveBeenCalledWith("[McpHub] Error calling targetProvider.postMessageToWebview:", failure) + }) }) diff --git a/src/services/tools/__tests__/ToolRouter.spec.ts b/src/services/tools/__tests__/ToolRouter.spec.ts index c380a79be3..823fbda509 100644 --- a/src/services/tools/__tests__/ToolRouter.spec.ts +++ b/src/services/tools/__tests__/ToolRouter.spec.ts @@ -54,6 +54,20 @@ describe("ToolRouter", () => { ).toEqual(["mcp--github--issues", "mcp--jira--issues"]) }) + it.each(["attachInitial", "search"] as const)("%s drops colliding canonical names before Top-K", async (method) => { + const result = await ToolRouter.getInstance()[method]( + "tickets", + [ + { serverName: "a b", toolName: "t", description: "tickets" }, + { serverName: "a_b", toolName: "t", description: "tickets" }, + { serverName: "other", toolName: "u", description: "tickets" }, + ], + 2, + ) + expect(result).toHaveLength(2) + expect(result).toEqual(expect.arrayContaining(["mcp--a_b--t", "mcp--other--u"])) + }) + it.each(["attachInitial", "search"] as const)("%s validates and floors limits", async (method) => { const router = ToolRouter.getInstance() for (const limit of [NaN, Infinity, -Infinity, -1, 0, 0.5]) { From 0aefdf0941f8f1e8cf4f98318c4edf84ce10d757 Mon Sep 17 00:00:00 2001 From: Elliott de Launay Date: Thu, 5 Nov 2026 02:15:13 +0000 Subject: [PATCH 3/3] fix(tools): handle async listener failures and cover blank queries --- src/services/mcp/McpHub.ts | 4 +- .../__tests__/ToolRouter.integration.spec.ts | 42 +++++++++++-------- .../tools/__tests__/ToolRouter.spec.ts | 15 +++++++ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/services/mcp/McpHub.ts b/src/services/mcp/McpHub.ts index 45bf866fca..818729c46b 100644 --- a/src/services/mcp/McpHub.ts +++ b/src/services/mcp/McpHub.ts @@ -171,7 +171,9 @@ export class McpHub { public onServerListChanged(listener: () => void): { dispose(): void } { const safeListener = () => { try { - listener() + void Promise.resolve(listener()).catch((error: unknown) => { + console.error("[McpHub] Server-list change listener failed:", error) + }) } catch (error) { console.error("[McpHub] Server-list change listener failed:", error) } diff --git a/src/services/tools/__tests__/ToolRouter.integration.spec.ts b/src/services/tools/__tests__/ToolRouter.integration.spec.ts index 6945d82b25..3611b2f45e 100644 --- a/src/services/tools/__tests__/ToolRouter.integration.spec.ts +++ b/src/services/tools/__tests__/ToolRouter.integration.spec.ts @@ -114,24 +114,30 @@ describe("ToolRouter with McpHub", () => { expect(changed).toHaveBeenCalledTimes(1) }) - it("isolates throwing listeners during notifications and disposal", async () => { - const failure = new Error("listener failed") - const log = vi.spyOn(console, "error").mockImplementation(() => {}) - hub.onServerListChanged(() => { - throw failure - }) - const changed = vi.fn() - hub.onServerListChanged(changed) - const server = addServer() - post.mockClear() - await expect(hub["notifyWebviewOfServerChanges"]()).resolves.toBeUndefined() - expect(post).toHaveBeenCalledWith({ type: "mcpServers", mcpServers: [server] }) - expect(changed).toHaveBeenCalledTimes(1) - expect(log).toHaveBeenCalledWith("[McpHub] Server-list change listener failed:", failure) - await expect(hub.dispose()).resolves.toBeUndefined() - expect(changed).toHaveBeenCalledTimes(2) - expect(hub["serverListEvents"].listenerCount("changed")).toBe(0) - }) + it.each(["synchronous", "asynchronous"] as const)( + "isolates %s listener failures during notifications and disposal", + async (mode) => { + const failure = new Error("listener failed") + const log = vi.spyOn(console, "error").mockImplementation(() => {}) + hub.onServerListChanged(() => { + if (mode === "asynchronous") { + return Promise.reject(failure) + } + throw failure + }) + const changed = vi.fn() + hub.onServerListChanged(changed) + const server = addServer() + post.mockClear() + await expect(hub["notifyWebviewOfServerChanges"]()).resolves.toBeUndefined() + expect(post).toHaveBeenCalledWith({ type: "mcpServers", mcpServers: [server] }) + expect(changed).toHaveBeenCalledTimes(1) + expect(log).toHaveBeenCalledWith("[McpHub] Server-list change listener failed:", failure) + await expect(hub.dispose()).resolves.toBeUndefined() + expect(changed).toHaveBeenCalledTimes(2) + expect(hub["serverListEvents"].listenerCount("changed")).toBe(0) + }, + ) it("fires the event before the settings read can fail", async () => { await fs.writeFile(path.join(directory, GlobalFileNames.mcpSettings), "{ not json") diff --git a/src/services/tools/__tests__/ToolRouter.spec.ts b/src/services/tools/__tests__/ToolRouter.spec.ts index 823fbda509..75efe0d00d 100644 --- a/src/services/tools/__tests__/ToolRouter.spec.ts +++ b/src/services/tools/__tests__/ToolRouter.spec.ts @@ -41,6 +41,21 @@ describe("ToolRouter", () => { expect(firstIndex[0].description).toBe("search tickets") }) + it.each(["attachInitial", "search"] as const)( + "%s skips blank queries before indexing or ranking", + async (method) => { + const router = ToolRouter.getInstance() + expect(await router[method]("tickets", tools, 5)).toEqual(["mcp--github--issues"]) + const index = router["indexedTools"] + const rank = vi.spyOn(Bm25Ranker.prototype, "rank") + for (const query of ["", " ", "\t\n"]) { + expect(await router[method](query, [tools[1]], 5)).toEqual([]) + expect(router["indexedTools"]).toBe(index) + } + expect(rank).not.toHaveBeenCalled() + }, + ) + it.each(["attachInitial", "search"] as const)("%s qualifies duplicate tool names by server", async (method) => { expect( await ToolRouter.getInstance()[method](