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
41 changes: 41 additions & 0 deletions src/services/mcp/McpHub.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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<ClineProvider>
private disposables: vscode.Disposable[] = []
private settingsWatcher?: vscode.FileSystemWatcher
Expand All @@ -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
}
Expand Down Expand Up @@ -1855,6 +1879,21 @@ export class McpHub {
}

private async notifyWebviewOfServerChanges(): Promise<void> {
// 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")
Expand Down Expand Up @@ -2537,6 +2576,8 @@ export class McpHub {
}

this.connections = []
this.serverListEvents.emit("changed")
this.serverListEvents.removeAllListeners()

if (this.settingsWatcher) {
this.settingsWatcher.dispose()
Expand Down
58 changes: 58 additions & 0 deletions src/services/tools/ToolRouter.ts
Original file line number Diff line number Diff line change
@@ -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<McpHub, "onServerListChanged">): { 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<string[]> {
return this.search(taskText, scopedTools, k)
}

async search(query: string, scopedTools: ToolDoc[], k: number): Promise<string[]> {
// Top-K requires a finite positive limit; fractional limits round down.
if (scopedTools.length === 0 || !query.trim() || !Number.isFinite(k) || k < 1) {

Check warning on line 35 in src/services/tools/ToolRouter.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/services/tools/ToolRouter.ts:35: Survived ConditionalExpression mutant (replacement: false). See the job summary for the complete list and resolution guidance.
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<string, ToolDoc>()
for (const tool of scopedTools) {
const name = buildMcpToolName(tool.serverName, tool.toolName)
if (!unique.has(name)) {

Check warning on line 48 in src/services/tools/ToolRouter.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/services/tools/ToolRouter.ts:48: Survived ConditionalExpression mutant (replacement: true). See the job summary for the complete list and resolution guidance.
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))
}
}
164 changes: 164 additions & 0 deletions src/services/tools/__tests__/ToolRouter.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -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<ClineProvider>
let post: Mock<ClineProvider["postMessageToWebview"]>
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<ClineProvider["postMessageToWebview"]>().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<McpServer>]>([
["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)
})
})
Loading
Loading