diff --git a/src/services/mcp/McpHub.ts b/src/services/mcp/McpHub.ts index c973f35a03..818729c46b 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,27 @@ const McpSettingsSchema = z.object({ }) export class McpHub { + private readonly serverListEvents = new EventEmitter() + private serverListKey?: string + + public onServerListChanged(listener: () => void): { dispose(): void } { + const safeListener = () => { + try { + 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) + } + } + 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 +206,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 +1879,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 +2576,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..7b87a31352 --- /dev/null +++ b/src/services/tools/ToolRouter.ts @@ -0,0 +1,58 @@ +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) { + // 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)) + 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..3611b2f45e --- /dev/null +++ b/src/services/tools/__tests__/ToolRouter.integration.spec.ts @@ -0,0 +1,164 @@ +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" +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 + // 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 { + 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-")) + post = vi.fn().mockResolvedValue(undefined) + provider = { + cwd: directory, + ensureSettingsDirectoryExists: async () => directory, + postMessageToWebview: post, + 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.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") + 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 new file mode 100644 index 0000000000..75efe0d00d --- /dev/null +++ b/src/services/tools/__tests__/ToolRouter.spec.ts @@ -0,0 +1,94 @@ +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 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]( + "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 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]) { + 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() +}