Skip to content

Commit 5c59d45

Browse files
committed
feat(tools): add singleton router with lazy indexing and MCP invalidation
1 parent 6bbc7a0 commit 5c59d45

5 files changed

Lines changed: 290 additions & 0 deletions

File tree

‎src/services/mcp/McpHub.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from "fs/promises"
22
import * as path from "path"
3+
import { EventEmitter } from "node:events"
34

45
import * as vscode from "vscode"
56
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
@@ -43,6 +44,7 @@ import { arePathsEqual, getWorkspacePath } from "../../utils/path"
4344
import { injectVariables } from "../../utils/config"
4445
import { safeWriteJson } from "../../utils/safeWriteJson"
4546
import { sanitizeMcpName, toolNamesMatch } from "../../utils/mcp-name"
47+
import { ToolRouter } from "../tools/ToolRouter"
4648

4749
// Discriminated union for connection states
4850
export type ConnectedMcpConnection = {
@@ -163,6 +165,25 @@ const McpSettingsSchema = z.object({
163165
})
164166

165167
export class McpHub {
168+
private readonly serverListEvents = new EventEmitter()
169+
private serverListKey?: string
170+
171+
public onServerListChanged(listener: () => void): { dispose(): void } {
172+
const safeListener = () => {
173+
try {
174+
listener()
175+
} catch (error) {
176+
console.error("[McpHub] Server-list change listener failed:", error)
177+
}
178+
}
179+
this.serverListEvents.on("changed", safeListener)
180+
return {
181+
dispose: () => {
182+
this.serverListEvents.off("changed", safeListener)
183+
},
184+
}
185+
}
186+
166187
private providerRef: WeakRef<ClineProvider>
167188
private disposables: vscode.Disposable[] = []
168189
private settingsWatcher?: vscode.FileSystemWatcher
@@ -183,6 +204,7 @@ export class McpHub {
183204

184205
constructor(provider: ClineProvider, secretStorage?: SecretStorageService) {
185206
this.providerRef = new WeakRef(provider)
207+
this.disposables.push(ToolRouter.getInstance().registerHub(this))
186208
if (secretStorage) {
187209
this.secretStorage = secretStorage
188210
}
@@ -1855,6 +1877,21 @@ export class McpHub {
18551877
}
18561878

18571879
private async notifyWebviewOfServerChanges(): Promise<void> {
1880+
// Notify internal consumers before filesystem or webview work can fail.
1881+
const key = JSON.stringify(
1882+
this.connections.map(({ server }) => ({
1883+
name: server.name,
1884+
source: server.source,
1885+
config: server.config,
1886+
status: server.status,
1887+
disabled: server.disabled,
1888+
tools: server.tools,
1889+
})),
1890+
)
1891+
if (key !== this.serverListKey) {
1892+
this.serverListKey = key
1893+
this.serverListEvents.emit("changed")
1894+
}
18581895
// Get global server order from settings file
18591896
const settingsPath = await this.getMcpSettingsFilePath()
18601897
const content = await fs.readFile(settingsPath, "utf-8")
@@ -2537,6 +2574,8 @@ export class McpHub {
25372574
}
25382575

25392576
this.connections = []
2577+
this.serverListEvents.emit("changed")
2578+
this.serverListEvents.removeAllListeners()
25402579

25412580
if (this.settingsWatcher) {
25422581
this.settingsWatcher.dispose()

‎src/services/tools/ToolRouter.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { createHash } from "node:crypto"
2+
import { buildMcpToolName } from "../../utils/mcp-name"
3+
import type { McpHub } from "../mcp/McpHub"
4+
import { createRanker } from "./index"
5+
import type { Ranker, ToolDoc } from "./types"
6+
7+
/** Process-wide routing cache. Only caller-scoped tools are ever ranked. */
8+
export class ToolRouter {
9+
private static instance: ToolRouter | undefined
10+
private ranker?: Ranker
11+
private indexedTools?: { key: string; tools: ToolDoc[] }
12+
13+
private constructor() {}
14+
15+
static getInstance(): ToolRouter {
16+
return (this.instance ??= new ToolRouter())
17+
}
18+
19+
/** The hub owns this subscription and disposes it with its other resources. */
20+
registerHub(hub: Pick<McpHub, "onServerListChanged">): { dispose(): void } {
21+
return hub.onServerListChanged(() => this.invalidate())
22+
}
23+
24+
private invalidate(): void {
25+
this.indexedTools = undefined
26+
this.ranker = undefined
27+
}
28+
29+
async attachInitial(taskText: string, scopedTools: ToolDoc[], k: number): Promise<string[]> {
30+
return this.search(taskText, scopedTools, k)
31+
}
32+
33+
async search(query: string, scopedTools: ToolDoc[], k: number): Promise<string[]> {
34+
// Top-K requires a finite positive limit; fractional limits round down.
35+
if (scopedTools.length === 0 || !query.trim() || !Number.isFinite(k) || k < 1) {
36+
return []
37+
}
38+
39+
// Content keys allow equivalent caller arrays to share the index while
40+
// keeping task scopes and changed tool descriptions isolated.
41+
const key = createHash("sha256").update(JSON.stringify(scopedTools)).digest("hex")
42+
if (this.indexedTools?.key !== key) {
43+
this.indexedTools = { key, tools: scopedTools.map((tool) => ({ ...tool })) }
44+
}
45+
this.ranker ??= createRanker()
46+
const ranked = await this.ranker.rank(query, this.indexedTools.tools, Math.floor(k))
47+
return ranked.map((tool) => buildMcpToolName(tool.serverName, tool.toolName))
48+
}
49+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import * as fs from "fs/promises"
2+
import * as os from "os"
3+
import * as path from "path"
4+
import type { McpServer } from "@roo-code/types"
5+
import type { ClineProvider } from "../../../core/webview/ClineProvider"
6+
import { GlobalFileNames } from "../../../shared/globalFileNames"
7+
import { McpHub } from "../../mcp/McpHub"
8+
import { Bm25Ranker } from "../Bm25Ranker"
9+
import { ToolRouter } from "../ToolRouter"
10+
11+
vi.mock("../../../core/webview/ClineProvider")
12+
13+
describe("ToolRouter with McpHub", () => {
14+
let directory: string
15+
let hub: McpHub
16+
const router = ToolRouter.getInstance()
17+
const tools = [{ serverName: "github", toolName: "issues", description: "search tickets" }]
18+
function addServer(): McpServer {
19+
const server: McpServer = {
20+
name: "github",
21+
config: JSON.stringify({ command: "node" }),
22+
status: "connecting",
23+
source: "global",
24+
}
25+
hub.connections = [{ type: "disconnected", client: null, transport: null, server }]
26+
return server
27+
}
28+
29+
beforeEach(async () => {
30+
directory = await fs.mkdtemp(path.join(os.tmpdir(), "tool-router-"))
31+
const provider: Partial<ClineProvider> = {
32+
cwd: directory,
33+
ensureSettingsDirectoryExists: async () => directory,
34+
postMessageToWebview: vi.fn().mockResolvedValue(undefined),
35+
getState: vi.fn().mockResolvedValue({ mcpEnabled: false }),
36+
}
37+
// The hub uses only cwd, settings directory, state, and webview posting.
38+
hub = new McpHub(provider as ClineProvider)
39+
await hub.waitUntilReady()
40+
})
41+
42+
afterEach(async () => {
43+
await hub.dispose()
44+
await fs.rm(directory, { recursive: true, force: true })
45+
vi.restoreAllMocks()
46+
})
47+
48+
it("returns empty before any servers are configured", async () => {
49+
expect(hub.getServers()).toEqual([])
50+
expect(await router.attachInitial("tickets", [], 5)).toEqual([])
51+
expect(await router.search("tickets", [], 5)).toEqual([])
52+
})
53+
54+
it("invalidates the lazy index when a server is disabled and enabled", async () => {
55+
await fs.writeFile(
56+
path.join(directory, GlobalFileNames.mcpSettings),
57+
JSON.stringify({
58+
mcpServers: { github: { command: "node" } },
59+
}),
60+
)
61+
// An in-flight connection allows toggling without starting an external MCP process.
62+
addServer()
63+
const changed = vi.fn()
64+
const subscription = hub.onServerListChanged(changed)
65+
const rank = vi.spyOn(Bm25Ranker.prototype, "rank")
66+
expect(await router.search("tickets", tools, 5)).toEqual(["mcp--github--issues"])
67+
const firstIndex = rank.mock.calls[0][1]
68+
69+
await hub.toggleServerDisabled("github", true)
70+
expect(changed).toHaveBeenCalledTimes(1)
71+
expect(hub.getServers()).toEqual([])
72+
expect(router["indexedTools"]).toBeUndefined()
73+
expect(await router.search("tickets", [], 5)).toEqual([])
74+
75+
await hub.toggleServerDisabled("github", false)
76+
expect(changed).toHaveBeenCalledTimes(2)
77+
expect(hub.getServers()).toHaveLength(1)
78+
expect(await router.attachInitial("tickets", tools, 5)).toEqual(["mcp--github--issues"])
79+
expect(rank.mock.calls[1][1]).not.toBe(firstIndex)
80+
const enabledIndex = rank.mock.calls[1][1]
81+
await hub["notifyWebviewOfServerChanges"]()
82+
expect(changed).toHaveBeenCalledTimes(2)
83+
await router.search("tickets", tools, 5)
84+
expect(rank.mock.calls[2][1]).toBe(enabledIndex)
85+
subscription.dispose()
86+
await hub.dispose()
87+
expect(changed).toHaveBeenCalledTimes(2)
88+
expect(router["indexedTools"]).toBeUndefined()
89+
})
90+
91+
it.each<[string, Partial<McpServer>]>([
92+
["name", { name: "jira" }],
93+
["source", { source: "project" }],
94+
["config", { config: JSON.stringify({ command: "python" }) }],
95+
["status", { status: "connected" }],
96+
["disabled", { disabled: true }],
97+
["tools", { tools: [{ name: "issues", description: "tickets" }] }],
98+
])("invalidates once when server %s changes", async (_field, update) => {
99+
const server = addServer()
100+
await hub["notifyWebviewOfServerChanges"]()
101+
const changed = vi.fn()
102+
hub.onServerListChanged(changed)
103+
await router.search("tickets", tools, 5)
104+
Object.assign(server, update)
105+
await hub["notifyWebviewOfServerChanges"]()
106+
expect(changed).toHaveBeenCalledTimes(1)
107+
expect(router["indexedTools"]).toBeUndefined()
108+
await hub["notifyWebviewOfServerChanges"]()
109+
expect(changed).toHaveBeenCalledTimes(1)
110+
})
111+
112+
it("isolates throwing listeners during notifications and disposal", async () => {
113+
const failure = new Error("listener failed")
114+
const log = vi.spyOn(console, "error").mockImplementation(() => {})
115+
hub.onServerListChanged(() => {
116+
throw failure
117+
})
118+
const changed = vi.fn()
119+
hub.onServerListChanged(changed)
120+
addServer()
121+
const post = vi.spyOn(hub["providerRef"].deref()!, "postMessageToWebview")
122+
await expect(hub["notifyWebviewOfServerChanges"]()).resolves.toBeUndefined()
123+
expect(post).toHaveBeenCalled()
124+
expect(changed).toHaveBeenCalledTimes(1)
125+
expect(log).toHaveBeenCalledWith("[McpHub] Server-list change listener failed:", failure)
126+
await expect(hub.dispose()).resolves.toBeUndefined()
127+
expect(changed).toHaveBeenCalledTimes(2)
128+
expect(hub["serverListEvents"].listenerCount("changed")).toBe(0)
129+
})
130+
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Bm25Ranker } from "../Bm25Ranker"
2+
import { ToolRouter } from "../ToolRouter"
3+
import type { ToolDoc } from "../types"
4+
5+
const tools: ToolDoc[] = [
6+
{ serverName: "github", toolName: "issues", description: "search tickets" },
7+
{ serverName: "slack", toolName: "messages", description: "search messages" },
8+
]
9+
10+
describe("ToolRouter", () => {
11+
afterEach(() => vi.restoreAllMocks())
12+
13+
it("shares a process-wide singleton", () => {
14+
expect(ToolRouter.getInstance()).toBe(ToolRouter.getInstance())
15+
})
16+
17+
it.each(["attachInitial", "search"] as const)("%s ranks only the supplied scope", async (method) => {
18+
const router = ToolRouter.getInstance()
19+
expect(await router[method]("tickets", tools, 1)).toEqual(["mcp--github--issues"])
20+
expect(await router[method]("tickets", [tools[1]], 1)).toEqual([])
21+
expect(await router[method]("search", tools, 1)).toHaveLength(1)
22+
expect(await router[method]("search", [], 5)).toEqual([])
23+
expect(await router[method]("search", tools, 0)).toEqual([])
24+
})
25+
26+
it("reuses an index for equivalent arrays and snapshots mutable caller documents", async () => {
27+
const rank = vi.spyOn(Bm25Ranker.prototype, "rank")
28+
const router = ToolRouter.getInstance()
29+
const scope = tools.map((tool) => ({ ...tool }))
30+
await router.search("search", scope, 2)
31+
await router.search(
32+
"tickets",
33+
scope.map((tool) => ({ ...tool })),
34+
2,
35+
)
36+
const firstIndex = rank.mock.calls[0][1]
37+
expect(rank.mock.calls[1][1]).toBe(firstIndex)
38+
scope[0].description = "calendar"
39+
expect(await router.search("tickets", scope, 2)).toEqual([])
40+
expect(rank.mock.calls[2][1]).not.toBe(firstIndex)
41+
expect(firstIndex[0].description).toBe("search tickets")
42+
})
43+
44+
it.each(["attachInitial", "search"] as const)("%s qualifies duplicate tool names by server", async (method) => {
45+
expect(
46+
await ToolRouter.getInstance()[method](
47+
"tickets",
48+
[
49+
{ serverName: "github", toolName: "issues", description: "tickets" },
50+
{ serverName: "jira", toolName: "issues", description: "tickets" },
51+
],
52+
2,
53+
),
54+
).toEqual(["mcp--github--issues", "mcp--jira--issues"])
55+
})
56+
57+
it.each(["attachInitial", "search"] as const)("%s validates and floors limits", async (method) => {
58+
const router = ToolRouter.getInstance()
59+
for (const limit of [NaN, Infinity, -Infinity, -1, 0, 0.5]) {
60+
expect(await router[method]("search", tools, limit)).toEqual([])
61+
}
62+
expect(await router[method]("search", tools, 1.9)).toHaveLength(1)
63+
expect(await router[method]("search", tools, 2)).toHaveLength(2)
64+
})
65+
})

‎src/services/tools/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Bm25Ranker } from "./Bm25Ranker"
2+
import type { Ranker } from "./types"
3+
4+
// The embeddings implementation (#576) can select its provider here.
5+
export function createRanker(): Ranker {
6+
return new Bm25Ranker()
7+
}

0 commit comments

Comments
 (0)