diff --git a/docs/architecture/mcp-dynamic-tool-loading.md b/docs/architecture/mcp-dynamic-tool-loading.md new file mode 100644 index 0000000000..9b48eeddd9 --- /dev/null +++ b/docs/architecture/mcp-dynamic-tool-loading.md @@ -0,0 +1,33 @@ +# MCP dynamic tool loading + +Issue #578 adds a task-scoped gate to API tool declarations. When the tools +allowed by the current mode and tool policy exceed `mcp.dynamicLoad.threshold` +(default 50), the task initially attaches up to `mcp.dynamicLoad.initialK` +(default 30) ranked tools. Images-only tasks use the first distinct scoped +tools because there is no initial text to rank. + +The selection is stored in task history. Missing or invalid selections are +initialized on demand; a valid empty selection remains initialized. Restored +nonempty selections are checked against scoped definitions before use; selections +with no available names are ranked again. MCP names +already used in conversation history are retained for provider compatibility, +so resumed tasks can require more than `initialK` declarations. This is an +initial selection limit, not a lifetime cap. Historical names accumulate during +a task and are not trimmed, since removing their definitions can cause providers +to reject conversation history. Current server, mode, and tool-policy +restrictions still apply. + +If saving fails, the task retains its in-memory selection and continues. Later +normal history saves retry persistence. History writes union attached names under +the disk lock, preserving newer historical names when an older save arrives late. +Storage errors do not trigger API +stream retries. + +The gate limits advertised definitions; it does not add an execution permission +boundary. Existing tool authorization still applies to calls by name. + +The settings schema and extension-state round trip are implemented. SettingsView +controls and replacement of the legacy catalog-wide warning are deferred to +#581. The legacy warning uses its own catalog advisory threshold; it does not +report the scoped tool count for a running task. The `mcp_load` escape hatch is +deferred to #579. diff --git a/packages/types/src/__tests__/global-settings.test.ts b/packages/types/src/__tests__/global-settings.test.ts index f2b2e7e8e5..890f9dcabd 100644 --- a/packages/types/src/__tests__/global-settings.test.ts +++ b/packages/types/src/__tests__/global-settings.test.ts @@ -4,6 +4,7 @@ import { GLOBAL_SETTINGS_KEYS, globalSettingsSchema, } from "../global-settings.js" +import { resolveMcpDynamicLoadSettings } from "../mcp.js" describe("destructive command guard global setting", () => { it("is opt-in by default", () => { @@ -51,3 +52,26 @@ describe("collapseCommandOutputByDefault global setting", () => { expect(globalSettingsSchema.safeParse({ collapseCommandOutputByDefault: "true" }).success).toBe(false) }) }) + +describe.each(["mcp.dynamicLoad.threshold", "mcp.dynamicLoad.initialK"] as const)("%s", (key) => { + it.each([undefined, 1, 30, 50, 100])("round-trips %s through the settings schema", (value) => { + const settings = globalSettingsSchema.parse({ [key]: value }) + expect(globalSettingsSchema.parse(JSON.parse(JSON.stringify(settings)))[key]).toBe(value) + expect(GLOBAL_SETTINGS_KEYS).toContain(key) + }) + it.each([-1, 1.5, NaN, Infinity, "30", null])("rejects %s", (value) => { + expect(globalSettingsSchema.safeParse({ [key]: value }).success).toBe(false) + }) + it("allows zero only for the threshold", () => { + expect(globalSettingsSchema.safeParse({ [key]: 0 }).success).toBe(key === "mcp.dynamicLoad.threshold") + }) +}) + +describe("runtime MCP routing defaults", () => { + it.each([undefined, null, -1, 1.5, NaN, Infinity, "30", {}])("replaces invalid input %j", (value) => { + expect(resolveMcpDynamicLoadSettings(value, value)).toEqual({ threshold: 50, initialK: 30 }) + }) + it("preserves a zero threshold and a positive limit", () => { + expect(resolveMcpDynamicLoadSettings(0, 1)).toEqual({ threshold: 0, initialK: 1 }) + }) +}) diff --git a/packages/types/src/__tests__/history.test.ts b/packages/types/src/__tests__/history.test.ts index 883bb138d2..bd5c27a5e9 100644 --- a/packages/types/src/__tests__/history.test.ts +++ b/packages/types/src/__tests__/history.test.ts @@ -63,4 +63,25 @@ describe("historyItemSchema", () => { it("leaves title unset when absent", () => { expect(historyItemSchema.parse(baseItem)).not.toHaveProperty("title") }) + + it.each([ + { value: undefined, expected: undefined }, + { value: null, expected: undefined }, + { value: "abc", expected: undefined }, + { value: 42, expected: undefined }, + { value: {}, expected: undefined }, + { value: [42], expected: undefined }, + { value: [], expected: [] }, + { value: ["mcp--" + "s".repeat(57) + "--"], expected: ["mcp--" + "s".repeat(57) + "--"] }, + { value: ["mcp--" + "s".repeat(59)], expected: ["mcp--" + "s".repeat(59)] }, + { value: ["mcp--server--"], expected: undefined }, + { value: ["mcp--" + "s".repeat(56) + "--"], expected: undefined }, + { value: ["invalid"], expected: undefined }, + { value: ["mcp__a__b"], expected: undefined }, + { value: ["mcp--a--b", null, 42], expected: ["mcp--a--b"] }, + ])("normalizes persisted attachments $value without losing the task", ({ value, expected }) => { + const parsed = historyItemSchema.parse({ ...baseItem, attachedMcpToolNames: value }) + expect(parsed.id).toBe(baseItem.id) + expect(parsed.attachedMcpToolNames).toEqual(expected) + }) }) diff --git a/packages/types/src/global-settings.ts b/packages/types/src/global-settings.ts index eb51c1d67d..f585547fd1 100644 --- a/packages/types/src/global-settings.ts +++ b/packages/types/src/global-settings.ts @@ -15,6 +15,7 @@ import { toolNamesSchema } from "./tool.js" import { type Keys } from "./type-fu.js" import { languagesSchema } from "./vscode.js" import { providerIdentifiers } from "./provider-identifiers.js" +import { mcpDynamicLoadThresholdSchema, mcpDynamicLoadInitialKSchema } from "./mcp.js" /** * Default delay in milliseconds after writes to allow diagnostics to detect potential problems. @@ -265,6 +266,8 @@ export const globalSettingsSchema = z.object({ telemetrySetting: telemetrySettingsSchema.optional(), mcpEnabled: z.boolean().optional(), + "mcp.dynamicLoad.threshold": mcpDynamicLoadThresholdSchema.optional(), + "mcp.dynamicLoad.initialK": mcpDynamicLoadInitialKSchema.optional(), mode: z.string().optional(), modeApiConfigs: z.record(z.string(), z.string()).optional(), diff --git a/packages/types/src/history.ts b/packages/types/src/history.ts index 131194212e..aaa59095be 100644 --- a/packages/types/src/history.ts +++ b/packages/types/src/history.ts @@ -1,5 +1,7 @@ import { z } from "zod" +import { isCanonicalMcpToolName } from "./mcp.js" + import { todoItemSchema } from "./todo.js" /** @@ -26,6 +28,14 @@ export const pendingTaskActionSchema = z.discriminatedUnion("kind", [ export type PendingTaskAction = z.infer +// Older or hand-edited history can contain an invalid attachment field. Do not +// discard the task record or mark a non-array value as an initialized selection. +export const attachedMcpToolNamesSchema = z.preprocess((value) => { + if (!Array.isArray(value)) return undefined + const names = value.filter((name): name is string => typeof name === "string" && isCanonicalMcpToolName(name)) + return value.length > 0 && names.length === 0 ? undefined : names +}, z.array(z.string()).optional()) + export const historyItemSchema = z.object({ id: z.string(), rootTaskId: z.string().optional(), @@ -50,6 +60,8 @@ export const historyItemSchema = z.object({ completedByChildId: z.string().optional(), // Child that completed and resumed this parent completionResultSummary: z.string().optional(), // Summary from completed child pendingAction: pendingTaskActionSchema.optional(), + // Absent means routing has not run; an empty array is an initialized selection. + attachedMcpToolNames: attachedMcpToolNamesSchema, }) export type HistoryItem = z.infer diff --git a/packages/types/src/mcp.ts b/packages/types/src/mcp.ts index ce23cadd32..951279d062 100644 --- a/packages/types/src/mcp.ts +++ b/packages/types/src/mcp.ts @@ -1,11 +1,30 @@ import { z } from "zod" -/** - * Maximum number of MCP tools that can be enabled before showing a warning. - * LLMs tend to perform poorly when given too many tools to choose from. - */ +/** Canonical generated names, including prefixes cut off at the provider's 64-character limit. */ +export function isCanonicalMcpToolName(name: string): boolean { + return ( + /^mcp--[A-Za-z_][\w-]*--[A-Za-z_][\w-]*$/.test(name) || + (name.length === 64 && /^mcp--[A-Za-z_][\w-]*$/.test(name)) + ) +} + +export const DEFAULT_MCP_DYNAMIC_LOAD_THRESHOLD = 50 +export const DEFAULT_MCP_DYNAMIC_LOAD_INITIAL_K = 30 + +/** @deprecated Legacy catalog advisory. Runtime routing uses the dynamic-load settings. */ export const MAX_MCP_TOOLS_THRESHOLD = 60 +export const mcpDynamicLoadThresholdSchema = z.number().int().min(0) +export const mcpDynamicLoadInitialKSchema = z.number().int().min(1) + +/** Apply the same validation to persisted values and direct runtime callers. */ +export function resolveMcpDynamicLoadSettings(threshold: unknown, initialK: unknown) { + return { + threshold: mcpDynamicLoadThresholdSchema.safeParse(threshold).data ?? DEFAULT_MCP_DYNAMIC_LOAD_THRESHOLD, + initialK: mcpDynamicLoadInitialKSchema.safeParse(initialK).data ?? DEFAULT_MCP_DYNAMIC_LOAD_INITIAL_K, + } +} + /** * McpServerUse */ @@ -155,7 +174,7 @@ export interface EnabledMcpToolsCount { * @example * const { enabledToolCount, enabledServerCount } = countEnabledMcpTools(mcpServers) * if (enabledToolCount > MAX_MCP_TOOLS_THRESHOLD) { - * // Show warning + * // Show the legacy catalog advisory, separate from task-scoped routing. * } */ export function countEnabledMcpTools(servers: McpServer[]): EnabledMcpToolsCount { diff --git a/packages/types/src/message.ts b/packages/types/src/message.ts index 6f5162544d..5405c91a92 100644 --- a/packages/types/src/message.ts +++ b/packages/types/src/message.ts @@ -139,7 +139,7 @@ export function isNonBlockingAsk(ask: ClineAsk): ask is NonBlockingAsk { * - `condense_context`: Context condensation/summarization has started * - `condense_context_error`: Error occurred during context condensation * - `codebase_search_result`: Results from searching the codebase - * - `too_many_tools_warning`: Warning that too many MCP tools are enabled, which may confuse the LLM + * - `too_many_tools_warning`: Legacy warning retained for history rendering; no longer emitted by Task. * - `ignored_disabled_tools_warning`: Warning that a `disabledTools` entry names a protocol tool, which user settings cannot disable */ export const clineSays = [ diff --git a/packages/types/src/vscode-extension-host.ts b/packages/types/src/vscode-extension-host.ts index 40f83fbaa2..89810b0a48 100644 --- a/packages/types/src/vscode-extension-host.ts +++ b/packages/types/src/vscode-extension-host.ts @@ -377,6 +377,8 @@ export type ExtensionState = Pick< experiments: Experiments // Map of experiment IDs to their enabled state mcpEnabled: boolean + "mcp.dynamicLoad.threshold"?: number + "mcp.dynamicLoad.initialK"?: number mode: string customModes: ModeConfig[] diff --git a/src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts b/src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts index 0aee40ef81..d29fdfddd3 100644 --- a/src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts +++ b/src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts @@ -1,5 +1,7 @@ // npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts +import { buildMcpToolName } from "../../../utils/mcp-name" +import { NativeToolCallParser } from "../NativeToolCallParser" import { DispatchState, type PendingWork } from "../../task/dispatchState" import type { Anthropic } from "@anthropic-ai/sdk" import { TelemetryEventName } from "@roo-code/types" @@ -322,6 +324,40 @@ describe("presentAssistantMessage - tool usage attribution", () => { }) describe("native mcp_tool_use block", () => { + it.each([55, 57, 58, 59, 70])( + "resolves generated names truncated within a %s-character server", + async (length) => { + const serverName = "s".repeat(length) + const toolName = "original_tool" + const name = buildMcpToolName(serverName, toolName) + mockTask.providerRef = { + deref: () => ({ + getState: vi.fn().mockResolvedValue({ mode: "code", customModes: [] }), + getMcpHub: () => ({ + findServerNameBySanitizedName: () => undefined, + getAllServers: () => [ + { name: serverName, tools: [{ name: toolName, enabledForPrompt: true }] }, + ], + }), + }), + } + const parsed = NativeToolCallParser.parseDynamicMcpTool({ id: "truncated", name, arguments: "{}" }) + expect(parsed).not.toBeNull() + mockTask.assistantMessageContent = [parsed] + const handle = vi.spyOn(useMcpToolTool, "handle").mockResolvedValue(undefined) + // The structural task double provides the dispatch state, provider, API and callbacks used here. + await presentAssistantMessage(mockTask as unknown as Task, baseSnapshot) + expect(handle).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + params: expect.objectContaining({ server_name: serverName, tool_name: toolName }), + }), + expect.anything(), + ) + handle.mockRestore() + }, + ) + it("records exactly one attempt once the MCP tool's own validation passes", async () => { mockTask.providerRef = { deref: () => ({ diff --git a/src/core/assistant-message/presentAssistantMessage.ts b/src/core/assistant-message/presentAssistantMessage.ts index 73d48b2e7c..1af39a0417 100644 --- a/src/core/assistant-message/presentAssistantMessage.ts +++ b/src/core/assistant-message/presentAssistantMessage.ts @@ -1,3 +1,4 @@ +import { buildMcpToolName, toolNamesMatch } from "../../utils/mcp-name" import { markToolRejected } from "../task/toolExecutionContext" import { DispatchState, transitionDispatch } from "../task/dispatchState" import { serializeError } from "serialize-error" @@ -323,6 +324,7 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request // We need the original name to find the actual MCP connection const mcpHub = cline.providerRef.deref()?.getMcpHub() let resolvedServerName = mcpBlock.serverName + let resolvedToolName = mcpBlock.toolName if (mcpHub) { const originalName = mcpHub.findServerNameBySanitizedName(mcpBlock.serverName) if (originalName) { @@ -330,6 +332,22 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request } } + // Truncated generated names cannot be reversed by splitting separators. + // Recover the original pair from the catalog; normal policy checks still apply. + if (mcpHub && mcpBlock.name.length === 64) { + const matches = mcpHub + .getAllServers() + .flatMap((server) => + (server.tools ?? []) + .filter((tool) => toolNamesMatch(buildMcpToolName(server.name, tool.name), mcpBlock.name)) + .map((tool) => ({ serverName: server.name, toolName: tool.name })), + ) + if (matches.length === 1 && matches[0]) { + resolvedServerName = matches[0].serverName + resolvedToolName = matches[0].toolName + } + } + // Execute the MCP tool using the same handler as use_mcp_tool // Create a synthetic ToolUse block that the useMcpToolTool can handle const syntheticToolUse: ToolUse<"use_mcp_tool"> = { @@ -338,13 +356,13 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request name: "use_mcp_tool", params: { server_name: resolvedServerName, - tool_name: mcpBlock.toolName, + tool_name: resolvedToolName, arguments: JSON.stringify(mcpBlock.arguments), }, partial: mcpBlock.partial, nativeArgs: { server_name: resolvedServerName, - tool_name: mcpBlock.toolName, + tool_name: resolvedToolName, arguments: mcpBlock.arguments, }, } diff --git a/src/core/task-persistence/__tests__/mcpAttachments.integration.spec.ts b/src/core/task-persistence/__tests__/mcpAttachments.integration.spec.ts new file mode 100644 index 0000000000..a4fde8ad87 --- /dev/null +++ b/src/core/task-persistence/__tests__/mcpAttachments.integration.spec.ts @@ -0,0 +1,84 @@ +import * as fs from "fs/promises" +import * as os from "os" +import * as path from "path" +import { TaskHistoryStore } from "../TaskHistoryStore" +import { taskMetadata } from "../taskMetadata" + +describe("MCP attachment history round trip", () => { + it.each([false, true])( + "retains historical names after a delayed stale save (separate store: %s)", + async (separateStore) => { + const directory = await fs.mkdtemp(path.join(os.tmpdir(), "mcp-attachments-race-")) + const store = new TaskHistoryStore(directory) + const peer = new TaskHistoryStore(directory) + const restored = new TaskHistoryStore(directory) + try { + const { historyItem } = await taskMetadata({ + taskId: "attachment-task", + taskNumber: 1, + messages: [], + workspace: directory, + globalStoragePath: directory, + attachedMcpToolNames: ["mcp--catalog--initial"], + }) + await store.initialize() + await store.upsert(historyItem) + await peer.initialize() + let release = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + const olderSave = gate.then(() => + (separateStore ? peer : store).upsert({ + ...historyItem, + attachedMcpToolNames: separateStore ? [] : historyItem.attachedMcpToolNames, + }), + ) + await store.upsert({ + ...historyItem, + attachedMcpToolNames: ["mcp--catalog--initial", "mcp--catalog--historical"], + }) + release() + await olderSave + await restored.initialize() + expect(restored.get(historyItem.id)?.attachedMcpToolNames).toEqual([ + "mcp--catalog--initial", + "mcp--catalog--historical", + ]) + } finally { + store.dispose() + peer.dispose() + restored.dispose() + await fs.rm(directory, { recursive: true, force: true }) + } + }, + ) + + it.each([{ names: undefined }, { names: [] }, { names: ["mcp--github--issues"] }])( + "preserves $names across a store restart", + async ({ names }) => { + const directory = await fs.mkdtemp(path.join(os.tmpdir(), "mcp-attachments-")) + const store = new TaskHistoryStore(directory) + const restored = new TaskHistoryStore(directory) + try { + const { historyItem } = await taskMetadata({ + taskId: "attachment-task", + taskNumber: 1, + messages: [], + workspace: directory, + globalStoragePath: directory, + attachedMcpToolNames: names, + }) + await store.initialize() + await store.upsert(historyItem) + store.dispose() + await restored.initialize() + expect(restored.get(historyItem.id)?.attachedMcpToolNames).toEqual(names) + } finally { + store.dispose() + restored.dispose() + await fs.rm(directory, { recursive: true, force: true }) + } + }, + ) +}) diff --git a/src/core/task-persistence/__tests__/taskStoreConcurrency.spec.ts b/src/core/task-persistence/__tests__/taskStoreConcurrency.spec.ts index 419ba03cb4..e57670265b 100644 --- a/src/core/task-persistence/__tests__/taskStoreConcurrency.spec.ts +++ b/src/core/task-persistence/__tests__/taskStoreConcurrency.spec.ts @@ -34,3 +34,18 @@ describe("mergeHistoryDelta", () => { expect(mergeHistoryDelta(disk, incoming, { id: disk.id, status: undefined }).status).toBe("active") }) }) + +describe("MCP attachment delta normalization", () => { + it.each([null, 42, {}, "abc", [42], ["invalid"], ["mcp--catalog--old", 42]])( + "normalizes malformed on-disk attachments %j before unioning", + (value) => { + const disk = { ...item("active"), attachedMcpToolNames: value } + const incoming = { ...item("active"), attachedMcpToolNames: ["mcp--catalog--new"] } + expect(mergeHistoryDelta(disk, incoming, incoming).attachedMcpToolNames).toEqual( + Array.isArray(value) && value.includes("mcp--catalog--old") + ? ["mcp--catalog--old", "mcp--catalog--new"] + : ["mcp--catalog--new"], + ) + }, + ) +}) diff --git a/src/core/task-persistence/taskMetadata.ts b/src/core/task-persistence/taskMetadata.ts index c52b0bdc33..90112cffec 100644 --- a/src/core/task-persistence/taskMetadata.ts +++ b/src/core/task-persistence/taskMetadata.ts @@ -23,6 +23,7 @@ export type TaskMetadataOptions = { mode?: string /** Provider profile name for the task (sticky profile feature) */ apiConfigName?: string + attachedMcpToolNames?: string[] /** Initial status for the task (e.g., "active" for child tasks) */ initialStatus?: "active" | "delegated" | "completed" | "interrupted" } @@ -38,6 +39,7 @@ export async function taskMetadata({ mode, apiConfigName, initialStatus, + attachedMcpToolNames, }: TaskMetadataOptions) { const taskDir = await getTaskDirectoryPath(globalStoragePath, id) @@ -112,6 +114,7 @@ export async function taskMetadata({ mode, ...(typeof apiConfigName === "string" && apiConfigName.length > 0 ? { apiConfigName } : {}), ...(initialStatus && { status: initialStatus }), + ...(attachedMcpToolNames !== undefined && { attachedMcpToolNames }), } return { historyItem, tokenUsage } diff --git a/src/core/task-persistence/taskStoreConcurrency.ts b/src/core/task-persistence/taskStoreConcurrency.ts index 0f747c4cb2..ec6f34a7ea 100644 --- a/src/core/task-persistence/taskStoreConcurrency.ts +++ b/src/core/task-persistence/taskStoreConcurrency.ts @@ -1,5 +1,5 @@ import deepEqual from "fast-deep-equal" -import type { HistoryItem } from "@roo-code/types" +import { attachedMcpToolNamesSchema, type HistoryItem } from "@roo-code/types" import { type HistoryItemStatus, VALID_TASK_STATUS_TRANSITIONS } from "./taskLifecycle" @@ -38,6 +38,12 @@ export function mergeHistoryDelta(existing: unknown, incoming: HistoryItem, delt normalizedDelta.status = attemptedStatus } const merged = { ...disk, ...normalizedDelta } + // Attachments accumulate for history compatibility. Merge under the disk lock + // so a delayed metadata snapshot cannot remove a newer historical name. + const diskNames = attachedMcpToolNamesSchema.parse(disk.attachedMcpToolNames) + if (normalizedDelta.attachedMcpToolNames && diskNames) { + merged.attachedMcpToolNames = [...new Set([...diskNames, ...normalizedDelta.attachedMcpToolNames])] + } if (normalizedDelta.childIds && disk.childIds) { merged.childIds = [...new Set([...disk.childIds, ...normalizedDelta.childIds])] } diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index 90cf7bf7fb..720937e92d 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -54,9 +54,9 @@ import { DEFAULT_CHECKPOINT_TIMEOUT_SECONDS, MAX_CHECKPOINT_TIMEOUT_SECONDS, MIN_CHECKPOINT_TIMEOUT_SECONDS, - MAX_MCP_TOOLS_THRESHOLD, - countEnabledMcpTools, providerIdentifiers, + attachedMcpToolNamesSchema, + resolveMcpDynamicLoadSettings, } from "@roo-code/types" import { TelemetryService } from "@roo-code/telemetry" import { CloudService } from "@roo-code/cloud" @@ -101,6 +101,9 @@ import { formatResponse } from "../prompts/responses" import { SYSTEM_PROMPT } from "../prompts/system" import { partitionDisabledToolsForProtocol } from "../prompts/tools/effective-tool-policy" import { buildNativeToolsArrayWithRestrictions } from "./build-tools" +import { ToolRouter } from "../../services/tools/ToolRouter" +import { buildMcpToolName, isMcpTool, normalizeMcpToolName } from "../../utils/mcp-name" +import type { ToolDoc } from "../../services/tools/types" // core modules import { ToolRepetitionDetector } from "../tools/ToolRepetitionDetector" @@ -292,6 +295,10 @@ export class Task extends EventEmitter implements TaskLike { readonly instanceId: string readonly metadata: TaskMetadata + readonly attachedMcpToolNames = new Set() + private mcpToolsInitialized = false + private restoredMcpToolsNeedValidation = false + private mcpToolsInitialization?: Promise todoList?: TodoItem[] @@ -747,6 +754,12 @@ export class Task extends EventEmitter implements TaskLike { images: historyItem ? [] : images, } this._isHistoryTask = !!historyItem && !task && !images + const savedMcpToolNames = attachedMcpToolNamesSchema.parse(historyItem?.attachedMcpToolNames) + if (savedMcpToolNames !== undefined) { + for (const name of savedMcpToolNames) this.attachedMcpToolNames.add(name) + this.mcpToolsInitialized = true + this.restoredMcpToolsNeedValidation = savedMcpToolNames.length > 0 + } // Normal use-case is usually retry similar history task with new workspace. this.workspacePath = parentTask @@ -1783,8 +1796,10 @@ export class Task extends EventEmitter implements TaskLike { * `merge` (default `true`) is passed through to `saveTaskMessages`: the in-memory * snapshot is merged with the on-disk record. `overwriteClineMessages` passes `false` * to replace the stored messages outright. + * MCP initialization uses strict mode to detect metadata failures, retain the + * in-memory selection, and let later normal saves retry persistence. */ - private async saveClineMessages(merge = true): Promise { + private async saveClineMessages(merge = true, requireMetadataPersistence = false): Promise { try { await saveTaskMessages({ messages: structuredClone(this.clineMessages), @@ -1813,6 +1828,7 @@ export class Task extends EventEmitter implements TaskLike { mode: this._taskMode || defaultModeSlug, // Use the task's own mode, not the current provider mode. apiConfigName: this._taskApiConfigName, // Use the task's own provider profile, not the current provider profile. initialStatus: this.initialStatus, + attachedMcpToolNames: this.mcpToolsInitialized ? [...this.attachedMcpToolNames] : undefined, }) // Emit token/tool usage updates using debounced function @@ -1823,12 +1839,15 @@ export class Task extends EventEmitter implements TaskLike { this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage) const provider = this.providerRef.deref() + if (requireMetadataPersistence && !provider) + throw new Error("Provider lost while saving MCP tool selection") const existingStatus = provider?.taskHistoryStore.get(this.taskId)?.status await provider?.updateTaskHistory(existingStatus ? { ...historyItem, status: existingStatus } : historyItem) } catch (error) { // The message array was persisted above; a metadata or task-history failure must // not mask that write (see the method docs). The next saveClineMessages() call // recomputes and re-emits the metadata update. + if (requireMetadataPersistence) throw error console.error("Failed to save task metadata:", error) } @@ -2665,6 +2684,9 @@ export class Task extends EventEmitter implements TaskLike { let allTools: import("openai").default.Chat.ChatCompletionTool[] = [] if (provider) { const toolsResult = await buildNativeToolsArrayWithRestrictions({ + task: this, + mcpDynamicLoadThreshold: state?.["mcp.dynamicLoad.threshold"], + mcpDynamicLoadInitialK: state?.["mcp.dynamicLoad.initialK"], provider, cwd: this.cwd, mode, @@ -2678,6 +2700,9 @@ export class Task extends EventEmitter implements TaskLike { allTools = toolsResult.tools } + if (this.abort || this.abandoned) return + this.currentRequestAbortController ??= new AbortController() + // Build metadata with tools and taskId for the condensing API call const metadata: ApiHandlerCreateMessageMetadata = { mode, @@ -2958,37 +2983,6 @@ export class Task extends EventEmitter implements TaskLike { // Lifecycle // Start / Resume / Abort / Dispose - /** - * Get enabled MCP tools count for this task. - * Returns the count along with the number of servers contributing. - * - * @returns Object with enabledToolCount and enabledServerCount - */ - private async getEnabledMcpToolsCount(): Promise<{ enabledToolCount: number; enabledServerCount: number }> { - try { - const provider = this.providerRef.deref() - if (!provider) { - return { enabledToolCount: 0, enabledServerCount: 0 } - } - - const { mcpEnabled } = (await provider.getState()) ?? {} - if (!(mcpEnabled ?? true)) { - return { enabledToolCount: 0, enabledServerCount: 0 } - } - - const mcpHub = await McpServerManager.getInstance(provider.context, provider) - if (!mcpHub) { - return { enabledToolCount: 0, enabledServerCount: 0 } - } - - const servers = mcpHub.getServers() - return countEnabledMcpTools(servers) - } catch (error) { - console.error("[Task#getEnabledMcpToolsCount] Error counting MCP tools:", error) - return { enabledToolCount: 0, enabledServerCount: 0 } - } - } - /** * Manually start a **new** task when it was created with `startTask: false`. * @@ -3039,6 +3033,64 @@ export class Task extends EventEmitter implements TaskLike { return this._runPromise } + /** Select once, preserve historical calls, and attempt to persist before use. */ + public async initializeAttachedMcpTools(scopedTools: ToolDoc[], initialK: number): Promise { + if (this.mcpToolsInitialization) return this.mcpToolsInitialization + + this.mcpToolsInitialization = (async () => { + if (this.restoredMcpToolsNeedValidation) { + const available = new Set(scopedTools.map((tool) => buildMcpToolName(tool.serverName, tool.toolName))) + // Scope can change when a server reconnects or the task changes mode. + // Keep saved names, and rerank only when none is currently available. + this.mcpToolsInitialized = [...this.attachedMcpToolNames].some((name) => available.has(name)) + this.restoredMcpToolsNeedValidation = false + } + + const historicalNames = new Set() + for (const message of this.apiConversationHistory) { + if (message.role !== "assistant" || !Array.isArray(message.content)) continue + for (const block of message.content) { + if (block.type === "tool_use" && typeof block.name === "string" && isMcpTool(block.name)) { + historicalNames.add(normalizeMcpToolName(block.name)) + } + } + } + + if (this.mcpToolsInitialized) { + const missing = [...historicalNames].filter((name) => !this.attachedMcpToolNames.has(name)) + if (missing.length === 0) return + for (const name of missing) this.attachedMcpToolNames.add(name) + } else { + const taskText = this.metadata.task?.trim() ?? "" + const { initialK: limit } = resolveMcpDynamicLoadSettings(undefined, initialK) + // Images-only tasks have no searchable text. Use a bounded, stable + // selection instead of permanently attaching no tools. + const names = taskText + ? await ToolRouter.getInstance().attachInitial(taskText, scopedTools, limit) + : [...new Set(scopedTools.map((tool) => buildMcpToolName(tool.serverName, tool.toolName)))].slice( + 0, + limit, + ) + if (!taskText && names.length === 0 && historicalNames.size === 0) return + // History compatibility may require more than initialK definitions. + for (const name of [...historicalNames, ...names]) this.attachedMcpToolNames.add(name) + this.mcpToolsInitialized = true + } + try { + if (!(await this.saveClineMessages(true, true))) throw new Error("Failed to save MCP tool selection") + } catch (error) { + // Storage failure must not enter the API stream's retry loop. Keep + // routing stable; every subsequent normal history save includes it. + console.error("[Task] Failed to persist MCP tool selection; keeping in-memory selection:", error) + } + })() + try { + await this.mcpToolsInitialization + } finally { + this.mcpToolsInitialization = undefined + } + } + private async startTask(task?: string, images?: string[]): Promise { try { // `conversationHistory` (for API) and `clineMessages` (for webview) @@ -3057,31 +3109,13 @@ export class Task extends EventEmitter implements TaskLike { await this.say("text", task, images) - // Check for too many MCP tools and warn the user - const { enabledToolCount, enabledServerCount } = await this.getEnabledMcpToolsCount() - if (enabledToolCount > MAX_MCP_TOOLS_THRESHOLD) { - await this.say( - "too_many_tools_warning", - JSON.stringify({ - toolCount: enabledToolCount, - serverCount: enabledServerCount, - threshold: MAX_MCP_TOOLS_THRESHOLD, - }), - undefined, - undefined, - undefined, - undefined, - { isNonInteractive: true }, - ) - } - // Surface once per new task which `disabledTools` entries carry no // disabling weight (protocol tools are exempt from user disabling). // Detection reads the raw list, so the notice also fires when a // model-profile `excludedTools` entry suppresses the tool through a // separate route. `startTask` runs exactly once per new task, which // is what bounds this to a single notice. The block is guarded like - // `getEnabledMcpToolsCount` above: this is a telemetry-class notice, + // other startup notices: this is a telemetry-class notice, // so a rejection from the state read or the say must never keep the // task loop from starting. try { @@ -5467,46 +5501,55 @@ export class Task extends EventEmitter implements TaskLike { // Send condenseTaskContextStarted to show in-progress indicator await this.providerRef.deref()?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId }) - // Build tools for condensing metadata (same tools used for normal API calls) - const provider = this.providerRef.deref() - let allTools: import("openai").default.Chat.ChatCompletionTool[] = [] - if (provider) { - const toolsResult = await buildNativeToolsArrayWithRestrictions({ - provider, - cwd: this.cwd, - mode, - customModes: state?.customModes, - experiments: state?.experiments, - apiConfiguration, - disabledTools: state?.disabledTools, - modelInfo, - includeAllToolsWithRestrictions: false, - }) - allTools = toolsResult.tools - } + try { + // Build tools for condensing metadata (same tools used for normal API calls) + const provider = this.providerRef.deref() + let allTools: import("openai").default.Chat.ChatCompletionTool[] = [] + if (provider) { + const toolsResult = await buildNativeToolsArrayWithRestrictions({ + task: this, + mcpDynamicLoadThreshold: state?.["mcp.dynamicLoad.threshold"], + mcpDynamicLoadInitialK: state?.["mcp.dynamicLoad.initialK"], + provider, + cwd: this.cwd, + mode, + customModes: state?.customModes, + experiments: state?.experiments, + apiConfiguration, + disabledTools: state?.disabledTools, + modelInfo, + includeAllToolsWithRestrictions: false, + }) + allTools = toolsResult.tools + } - // Build metadata with tools and taskId for the condensing API call - const metadata: ApiHandlerCreateMessageMetadata = { - mode, - taskId: this.taskId, - ...(this.currentRequestAbortController?.signal - ? { - abortSignal: this.currentRequestAbortController.signal, - } - : {}), - ...(allTools.length > 0 - ? { - tools: allTools, - tool_choice: "auto", - parallelToolCalls: true, - } - : {}), - } + if (this.abort || this.abandoned) return + this.currentRequestAbortController ??= new AbortController() + + // Build metadata with tools and taskId for the condensing API call + const metadata: ApiHandlerCreateMessageMetadata = { + mode, + taskId: this.taskId, + ...(this.currentRequestAbortController?.signal + ? { + abortSignal: this.currentRequestAbortController.signal, + } + : {}), + ...(allTools.length > 0 + ? { + tools: allTools, + tool_choice: "auto", + parallelToolCalls: true, + } + : {}), + } - try { // Generate environment details to include in the condensed summary const environmentDetails = await getEnvironmentDetails(this, true) + const systemPrompt = await this.getSystemPrompt(state, modelInfo) + if (this.abort || this.abandoned) return + // Force aggressive truncation by keeping only 75% of the conversation history const truncateResult = await manageContext({ messages: this.apiConversationHistory, @@ -5516,7 +5559,7 @@ export class Task extends EventEmitter implements TaskLike { apiHandler: this.api, autoCondenseContext: true, autoCondenseContextPercent: FORCED_CONTEXT_REDUCTION_PERCENT, - systemPrompt: await this.getSystemPrompt(state, modelInfo), + systemPrompt, taskId: this.taskId, profileThresholds, currentProfileId, @@ -5525,6 +5568,8 @@ export class Task extends EventEmitter implements TaskLike { useAvailableInputForContextPercent, }) + if (this.abort || this.abandoned) return + if (truncateResult.messages !== this.apiConversationHistory) { await this.overwriteApiConversationHistory(truncateResult.messages) } @@ -5742,59 +5787,67 @@ export class Task extends EventEmitter implements TaskLike { ?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId }) } - // Build tools for condensing metadata (same tools used for normal API calls) - // This ensures the condensing API call includes tool definitions for providers that need them - let contextMgmtTools: import("openai").default.Chat.ChatCompletionTool[] = [] - { - const provider = this.providerRef.deref() - if (provider) { - const toolsResult = await buildNativeToolsArrayWithRestrictions({ - provider, - cwd: this.cwd, - mode, - customModes: state?.customModes, - experiments: state?.experiments, - apiConfiguration, - disabledTools: state?.disabledTools, - modelInfo: requestModelInfo, - includeAllToolsWithRestrictions: false, - }) - contextMgmtTools = toolsResult.tools + try { + // Build tools for condensing metadata (same tools used for normal API calls) + // This ensures the condensing API call includes tool definitions for providers that need them + let contextMgmtTools: import("openai").default.Chat.ChatCompletionTool[] = [] + { + const provider = this.providerRef.deref() + if (provider) { + const toolsResult = await buildNativeToolsArrayWithRestrictions({ + task: this, + mcpDynamicLoadThreshold: state?.["mcp.dynamicLoad.threshold"], + mcpDynamicLoadInitialK: state?.["mcp.dynamicLoad.initialK"], + provider, + cwd: this.cwd, + mode, + customModes: state?.customModes, + experiments: state?.experiments, + apiConfiguration, + disabledTools: state?.disabledTools, + modelInfo: requestModelInfo, + includeAllToolsWithRestrictions: false, + }) + contextMgmtTools = toolsResult.tools + } } - } - // Build metadata with tools and taskId for the condensing API call - const contextMgmtMetadata: ApiHandlerCreateMessageMetadata = { - mode, - taskId: this.taskId, - ...(this.currentRequestAbortController?.signal - ? { - abortSignal: this.currentRequestAbortController.signal, - } - : {}), - ...(contextMgmtTools.length > 0 - ? { - tools: contextMgmtTools, - tool_choice: "auto", - parallelToolCalls: true, - } - : {}), - } + if (this.abort || this.abandoned) return + this.currentRequestAbortController ??= new AbortController() - // Only generate environment details when context management will actually run. - // getEnvironmentDetails(this, true) triggers a recursive workspace listing which - // adds overhead - avoid this for the common case where context is below threshold. - const contextMgmtEnvironmentDetails = contextManagementWillRun - ? await getEnvironmentDetails(this, true) - : undefined - - // Get files read by Roo for code folding - only when context management will run - const contextMgmtFilesReadByRoo = - contextManagementWillRun && autoCondenseContext - ? await this.getFilesReadByRooSafely("attemptApiRequest") + // Build metadata with tools and taskId for the condensing API call + const contextMgmtMetadata: ApiHandlerCreateMessageMetadata = { + mode, + taskId: this.taskId, + ...(this.currentRequestAbortController?.signal + ? { + abortSignal: this.currentRequestAbortController.signal, + } + : {}), + ...(contextMgmtTools.length > 0 + ? { + tools: contextMgmtTools, + tool_choice: "auto", + parallelToolCalls: true, + } + : {}), + } + + // Only generate environment details when context management will actually run. + // getEnvironmentDetails(this, true) triggers a recursive workspace listing which + // adds overhead - avoid this for the common case where context is below threshold. + const contextMgmtEnvironmentDetails = contextManagementWillRun + ? await getEnvironmentDetails(this, true) : undefined - try { + // Get files read by Roo for code folding - only when context management will run + const contextMgmtFilesReadByRoo = + contextManagementWillRun && autoCondenseContext + ? await this.getFilesReadByRooSafely("attemptApiRequest") + : undefined + + if (this.abort || this.abandoned) return + const truncateResult = await manageContext({ messages: this.apiConversationHistory, totalTokens: contextTokens, @@ -5815,6 +5868,8 @@ export class Task extends EventEmitter implements TaskLike { rooIgnoreController: this.rooIgnoreController, useAvailableInputForContextPercent, }) + if (this.abort || this.abandoned) return + if (truncateResult.messages !== this.apiConversationHistory) { await this.overwriteApiConversationHistory(truncateResult.messages) } @@ -5923,6 +5978,9 @@ export class Task extends EventEmitter implements TaskLike { } const toolsResult = await buildNativeToolsArrayWithRestrictions({ + task: this, + mcpDynamicLoadThreshold: state?.["mcp.dynamicLoad.threshold"], + mcpDynamicLoadInitialK: state?.["mcp.dynamicLoad.initialK"], provider, cwd: this.cwd, mode, @@ -5937,6 +5995,12 @@ export class Task extends EventEmitter implements TaskLike { allowedFunctionNames = toolsResult.allowedFunctionNames } + if (this.abort || this.abandoned) { + throw new Error( + `[Task#attemptApiRequest] task ${this.taskId}.${this.instanceId} aborted during request construction`, + ) + } + const shouldIncludeTools = allTools.length > 0 // Create an AbortController to allow cancelling the request mid-stream diff --git a/src/core/task/__tests__/Task.ignored-disabled-tools-notice.spec.ts b/src/core/task/__tests__/Task.ignored-disabled-tools-notice.spec.ts index a83586122a..2bce811b74 100644 --- a/src/core/task/__tests__/Task.ignored-disabled-tools-notice.spec.ts +++ b/src/core/task/__tests__/Task.ignored-disabled-tools-notice.spec.ts @@ -105,7 +105,6 @@ vi.mock("delay", () => ({ type StartTaskAccess = { startTask: (task?: string, images?: string[]) => Promise resumeTaskFromHistory: () => Promise - getEnabledMcpToolsCount: () => Promise<{ enabledToolCount: number; enabledServerCount: number }> getSavedClineMessages: () => Promise getSavedApiConversationHistory: () => Promise initiateTaskLoop: (userContent: unknown[]) => Promise @@ -169,10 +168,6 @@ async function startTaskWithDisabledTools(disabledTools: string[] | undefined, p info: modelInfo, }) const taskAccess = getStartTaskAccess(task) - vi.spyOn(taskAccess, "getEnabledMcpToolsCount").mockResolvedValue({ - enabledToolCount: 0, - enabledServerCount: 0, - }) vi.spyOn(taskAccess, "initiateTaskLoop").mockResolvedValue(undefined) await taskAccess.startTask("example task") @@ -202,10 +197,6 @@ async function startTaskWithFailingStateRead() { const saySpy = vi.spyOn(task, "say").mockResolvedValue(undefined) const taskAccess = getStartTaskAccess(task) - vi.spyOn(taskAccess, "getEnabledMcpToolsCount").mockResolvedValue({ - enabledToolCount: 0, - enabledServerCount: 0, - }) const initiateLoopSpy = vi.spyOn(taskAccess, "initiateTaskLoop").mockResolvedValue(undefined) await taskAccess.startTask("example task") diff --git a/src/core/task/__tests__/Task.persistence.spec.ts b/src/core/task/__tests__/Task.persistence.spec.ts index fb7a4e8cc6..e6f4408afc 100644 --- a/src/core/task/__tests__/Task.persistence.spec.ts +++ b/src/core/task/__tests__/Task.persistence.spec.ts @@ -9,6 +9,7 @@ import { RooCodeEventName, type ClineMessage, type GlobalState, + type HistoryItem, type PendingTaskAction, type ProviderSettings, } from "@roo-code/types" @@ -21,6 +22,7 @@ import { ContextProxy } from "../../config/ContextProxy" import { providerIdentifiers } from "@roo-code/types/provider-identifiers" import { attemptCompletionTool, type AttemptCompletionCallbacks } from "../../tools/AttemptCompletionTool" import type { ToolUse } from "../../../shared/tools" +import { ToolRouter } from "../../../services/tools/ToolRouter" type TaskPersistenceAccess = { addToApiConversationHistory: (message: Anthropic.MessageParam) => Promise @@ -302,6 +304,241 @@ describe("Task persistence", () => { mockProvider.log = vi.fn() }) + describe("MCP tool attachments", () => { + afterEach(() => vi.restoreAllMocks()) + it.each([{ names: [] }, { names: ["mcp--github--issues"] }])( + "persists a single initial selection: $names", + async ({ names }) => { + const rank = vi.spyOn(ToolRouter.getInstance(), "attachInitial").mockResolvedValue(names) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "find tickets", + startTask: false, + }) + await Promise.all([task.initializeAttachedMcpTools([], 30), task.initializeAttachedMcpTools([], 30)]) + expect(rank).toHaveBeenCalledExactlyOnceWith("find tickets", [], 30) + expect([...task.attachedMcpToolNames]).toEqual(names) + expect(mockTaskMetadata).toHaveBeenCalledWith(expect.objectContaining({ attachedMcpToolNames: names })) + await task.initializeAttachedMcpTools([], 10) + expect(rank).toHaveBeenCalledTimes(1) + rank.mockRestore() + }, + ) + + it.each([ + { names: [], savedNames: [] }, + { names: ["mcp--github--issues"], savedNames: ["mcp--github--issues"] }, + { + names: ["mcp--github--issues", "mcp--missing--tool"], + savedNames: ["mcp--github--issues", "mcp--missing--tool"], + }, + ])("restores initialized selections without reranking: $names", async ({ names, savedNames }) => { + const rank = vi.spyOn(ToolRouter.getInstance(), "attachInitial") + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + startTask: false, + historyItem: { + id: "restored", + number: 1, + ts: 1, + task: "find tickets", + tokensIn: 0, + tokensOut: 0, + totalCost: 0, + attachedMcpToolNames: savedNames, + }, + }) + await task.initializeAttachedMcpTools([{ serverName: "github", toolName: "issues", description: "" }], 30) + expect([...task.attachedMcpToolNames]).toEqual(names) + expect(rank).not.toHaveBeenCalled() + rank.mockRestore() + }) + + it("retains unavailable restored names while reranking and when their scope returns", async () => { + const rank = vi.spyOn(ToolRouter.getInstance(), "attachInitial").mockResolvedValue(["mcp--github--issues"]) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + startTask: false, + historyItem: { ...legacyHistory(), attachedMcpToolNames: ["mcp--catalog--returning"] }, + }) + await task.initializeAttachedMcpTools([{ serverName: "github", toolName: "issues", description: "" }], 30) + expect([...task.attachedMcpToolNames]).toEqual(["mcp--catalog--returning", "mcp--github--issues"]) + await task.initializeAttachedMcpTools( + [{ serverName: "catalog", toolName: "returning", description: "" }], + 30, + ) + expect([...task.attachedMcpToolNames]).toEqual(["mcp--catalog--returning", "mcp--github--issues"]) + expect(rank).toHaveBeenCalledTimes(1) + }) + + it.each(["history", "messages", "provider"] as const)( + "keeps selection without retrying on a %s save failure", + async (stage) => { + const rank = vi + .spyOn(ToolRouter.getInstance(), "attachInitial") + .mockResolvedValue(["mcp--github--issues"]) + const log = vi.spyOn(console, "error").mockImplementation(() => {}) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "find tickets", + startTask: false, + }) + await task["taskApiConfigReady"] + const metadataResult = await mockTaskMetadata() + mockTaskMetadata.mockImplementation(async (options: { attachedMcpToolNames?: string[] }) => ({ + ...metadataResult, + historyItem: { ...metadataResult.historyItem, attachedMcpToolNames: options.attachedMcpToolNames }, + })) + const lostProvider = + stage === "provider" ? vi.spyOn(task["providerRef"], "deref").mockReturnValue(undefined) : undefined + if (stage === "history") + vi.mocked(mockProvider.updateTaskHistory).mockRejectedValueOnce(new Error("disk full")) + if (stage === "messages") mockSaveTaskMessages.mockRejectedValueOnce(new Error("disk full")) + await expect(task.initializeAttachedMcpTools([], 30)).resolves.toBeUndefined() + expect([...task.attachedMcpToolNames]).toEqual(["mcp--github--issues"]) + expect(log).toHaveBeenCalledWith( + expect.stringContaining("keeping in-memory selection"), + expect.any(Error), + ) + const persistenceFailures = () => + log.mock.calls.filter( + ([message]) => + typeof message === "string" && + message.startsWith("[Task] Failed to persist MCP tool selection"), + ).length + expect(persistenceFailures()).toBe(1) + await task.initializeAttachedMcpTools([], 30) + expect(rank).toHaveBeenCalledTimes(1) + expect(persistenceFailures()).toBe(1) + lostProvider?.mockRestore() + await task["saveClineMessages"]() + expect(mockTaskMetadata).toHaveBeenLastCalledWith( + expect.objectContaining({ attachedMcpToolNames: ["mcp--github--issues"] }), + ) + expect(mockProvider.updateTaskHistory).toHaveBeenLastCalledWith( + expect.objectContaining({ attachedMcpToolNames: ["mcp--github--issues"] }), + ) + mockTaskMetadata.mockResolvedValue(metadataResult) + log.mockRestore() + rank.mockRestore() + }, + ) + + function legacyHistory(): HistoryItem { + return { + id: "legacy-task", + number: 1, + ts: 1, + task: "find tickets", + tokensIn: 0, + tokensOut: 0, + totalCost: 0, + } + } + + it.each([undefined, null, "abc", 12, {}, [12], ["invalid"], ["mcp__github__issues"]])( + "treats invalid or absent persisted selection %j as uninitialized", + async (value) => { + const historyItem = legacyHistory() + // Model unvalidated external history without weakening the production types. + Reflect.set(historyItem, "attachedMcpToolNames", value) + const rank = vi + .spyOn(ToolRouter.getInstance(), "attachInitial") + .mockResolvedValue(["mcp--github--issues"]) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + historyItem, + startTask: false, + }) + await task.initializeAttachedMcpTools([], 30) + expect([...task.attachedMcpToolNames]).toEqual(["mcp--github--issues"]) + expect(rank).toHaveBeenCalledExactlyOnceWith("find tickets", [], 30) + rank.mockRestore() + }, + ) + + it("does not persist an empty selection before initialization", async () => { + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + historyItem: legacyHistory(), + startTask: false, + }) + await task["saveClineMessages"]() + expect(mockTaskMetadata).toHaveBeenLastCalledWith( + expect.objectContaining({ attachedMcpToolNames: undefined }), + ) + }) + + it.each([undefined, "", " "])( + "attaches bounded tools for images-only or blank tasks (%j)", + async (taskText) => { + const rank = vi.spyOn(ToolRouter.getInstance(), "attachInitial") + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: taskText, + images: ["image-data"], + startTask: false, + }) + await task.initializeAttachedMcpTools([], 2) + expect(mockTaskMetadata).not.toHaveBeenCalled() + await task.initializeAttachedMcpTools( + [ + { serverName: "a b", toolName: "t", description: "" }, + { serverName: "a_b", toolName: "t", description: "" }, + { serverName: "other", toolName: "u", description: "" }, + { serverName: "extra", toolName: "v", description: "" }, + ], + 2, + ) + expect([...task.attachedMcpToolNames]).toEqual(["mcp--a_b--t", "mcp--other--u"]) + expect(mockTaskMetadata).toHaveBeenLastCalledWith( + expect.objectContaining({ attachedMcpToolNames: ["mcp--a_b--t", "mcp--other--u"] }), + ) + expect(rank).not.toHaveBeenCalled() + rank.mockRestore() + }, + ) + + it.each([false, true])("preserves historical MCP names (previously initialized: %s)", async (initialized) => { + const historyItem = { + ...legacyHistory(), + ...(initialized ? { attachedMcpToolNames: ["mcp--catalog--top"] } : {}), + } + const rank = vi.spyOn(ToolRouter.getInstance(), "attachInitial").mockResolvedValue(["mcp--catalog--top"]) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + historyItem, + startTask: false, + }) + task.apiConversationHistory = [ + { + role: "assistant", + content: [ + { type: "tool_use", id: "old", name: "mcp--catalog--previous", input: {} }, + { type: "tool_use", id: "native", name: "read_file", input: {} }, + ], + }, + ] + await task.initializeAttachedMcpTools([{ serverName: "catalog", toolName: "top", description: "" }], 1) + expect(task.attachedMcpToolNames).toEqual(new Set(["mcp--catalog--previous", "mcp--catalog--top"])) + expect(rank).toHaveBeenCalledTimes(initialized ? 0 : 1) + expect(mockTaskMetadata).toHaveBeenLastCalledWith( + expect.objectContaining({ + attachedMcpToolNames: expect.arrayContaining(["mcp--catalog--previous", "mcp--catalog--top"]), + }), + ) + rank.mockRestore() + }) + }) + // ── saveApiConversationHistory (via retrySaveApiConversationHistory) ── describe("saveApiConversationHistory", () => { diff --git a/src/core/task/__tests__/Task.spec.ts b/src/core/task/__tests__/Task.spec.ts index b73b9afea7..d0b154b1e0 100644 --- a/src/core/task/__tests__/Task.spec.ts +++ b/src/core/task/__tests__/Task.spec.ts @@ -41,11 +41,11 @@ import { McpHub } from "../../../services/mcp/McpHub" import { McpServerManager } from "../../../services/mcp/McpServerManager" import { readFileTool } from "../../tools/ReadFileTool" import { writeToFileTool } from "../../tools/WriteToFileTool" +import * as buildTools from "../build-tools" type TaskTestAccess = { getSystemPrompt: (requestState: ProviderState | undefined, requestModelInfo?: ModelInfo) => Promise handleContextWindowExceededError: (requestModelInfo: ModelInfo) => Promise - getEnabledMcpToolsCount: () => Promise<{ enabledToolCount: number; enabledServerCount: number }> initiateTaskLoop: (userContent: Anthropic.Messages.ContentBlockParam[]) => Promise startTask: (task?: string, images?: string[]) => Promise resumeTaskFromHistory: () => Promise @@ -4725,7 +4725,7 @@ describe("Cline", () => { expect(abortSpy).toHaveBeenCalled() // Verify the controller was cleared - expect(task.currentRequestAbortController).toBeUndefined() + expect(task["currentRequestAbortController"]).toBeUndefined() // Verify logging expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining("Aborting current HTTP request")) @@ -4790,7 +4790,7 @@ describe("Cline", () => { expect(options.metadata?.abortSignal).toBe(task.currentRequestAbortController!.signal) }) - it("should omit abortSignal from condenseContext metadata when no current request exists", async () => { + it("creates a cancellable condenseContext request when no current request exists", async () => { const task = new Task({ provider: mockProvider, apiConfiguration: mockApiConfig, @@ -4805,7 +4805,8 @@ describe("Cline", () => { expect(summarizeConversation).toHaveBeenCalled() const [options] = vi.mocked(summarizeConversation).mock.calls.at(-1)! expect(options.metadata).toBeDefined() - expect("abortSignal" in (options.metadata ?? {})).toBe(false) + expect(options.metadata?.abortSignal).toBe(task.currentRequestAbortController?.signal) + expect(options.metadata?.abortSignal).toBeDefined() }) it("should pass AbortController signal to createMessage metadata", async () => { @@ -4926,6 +4927,290 @@ describe("Cline", () => { expect(allowedFunctionNames.every((name) => toolNames.includes(name))).toBe(true) }) + it.each([ + { + provider: providerIdentifiers.anthropic, + threshold: 50, + initialK: 7, + expected: 7, + saveFailure: false, + historyCall: false, + }, + { + provider: providerIdentifiers.gemini, + threshold: 50, + initialK: 7, + expected: 7, + saveFailure: false, + historyCall: false, + }, + { + provider: providerIdentifiers.anthropic, + threshold: 100, + initialK: 7, + expected: 100, + saveFailure: false, + historyCall: false, + }, + { + provider: providerIdentifiers.anthropic, + threshold: 50, + initialK: 7, + expected: 7, + saveFailure: true, + historyCall: false, + }, + { + provider: providerIdentifiers.gemini, + threshold: 50, + initialK: 7, + expected: 8, + saveFailure: false, + historyCall: true, + }, + ])( + "sends $expected of 100 MCP tools in the initial $provider API request", + async ({ provider, threshold, initialK, expected, saveFailure, historyCall }) => { + const apiConfiguration: ProviderSettings = { ...mockApiConfig, apiProvider: provider } + const task = new Task({ + provider: mockProvider, + apiConfiguration, + task: "search tickets", + startTask: false, + }) + const access = getTaskTestAccess(task) + vi.spyOn(access, "getSystemPrompt").mockResolvedValue("mock system prompt") + // Message and history persistence have focused round-trip coverage. + const save = vi.spyOn(access, "saveClineMessages").mockResolvedValue(true) + if (saveFailure) save.mockRejectedValue(new Error("persistent storage failure")) + vi.spyOn(task.api, "getModel").mockReturnValue({ + id: "test-model", + info: { contextWindow: 200000, maxTokens: 4096, supportsPromptCache: false }, + }) + const state = await mockProvider.getState() + vi.spyOn(mockProvider, "getState").mockResolvedValue({ + ...state, + apiConfiguration, + requestDelaySeconds: 0, + "mcp.dynamicLoad.threshold": threshold, + "mcp.dynamicLoad.initialK": initialK, + }) + // Native tool generation only reads the hub's public server list. + const hub = { + getServers: () => [ + { + name: "catalog", + config: "{}", + status: "connected" as const, + tools: Array.from({ length: 100 }, (_, index) => ({ + name: `tool_${index}`, + description: "search tickets", + })), + }, + ], + } as McpHub + vi.spyOn(mockProvider, "getMcpHub").mockReturnValue(hub) + const createMessage = vi + .spyOn(task.api, "createMessage") + .mockReturnValue(asyncStreamFrom([{ type: "text", text: "ok" }])) + task.apiConversationHistory = [ + { role: "user", content: [{ type: "text", text: "search tickets" }], ts: Date.now() }, + ] + if (historyCall) { + task.apiConversationHistory.push( + { + role: "assistant", + content: [ + { type: "tool_use", id: "previous", name: "mcp--catalog--tool_99", input: {} }, + ], + }, + { + role: "user", + content: [{ type: "tool_result", tool_use_id: "previous", content: "done" }], + }, + ) + } + await task.attemptApiRequest(0).next() + expect(createMessage).toHaveBeenCalledTimes(1) + const metadata = requireDefined(createMessage.mock.calls[0])[2] + const names = requireDefined(metadata?.tools).flatMap((tool) => + "function" in tool ? [tool.function.name] : [], + ) + expect(names.filter((name) => name.startsWith("mcp--"))).toHaveLength(expected) + if (historyCall) expect(names).toContain("mcp--catalog--tool_99") + expect(names).toContain("read_file") + expect(names).not.toContain("mcp_load") + if (provider === providerIdentifiers.gemini) + expect( + metadata?.allowedFunctionNames?.filter((name) => name.startsWith("mcp--")), + ).toHaveLength(expected) + }, + ) + + it.each(["abort", "abandoned"] as const)( + "stops a request canceled during tool building: %s", + async (flag) => { + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "search tickets", + startTask: false, + }) + vi.spyOn(getTaskTestAccess(task), "getSystemPrompt").mockResolvedValue("mock prompt") + vi.spyOn(task.api, "getModel").mockReturnValue({ + id: "test-model", + info: { contextWindow: 200000, maxTokens: 4096, supportsPromptCache: false }, + }) + const createMessage = vi.spyOn(task.api, "createMessage") + let release = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + let entered = () => {} + const building = new Promise((resolve) => { + entered = resolve + }) + const build = vi + .spyOn(buildTools, "buildNativeToolsArrayWithRestrictions") + .mockImplementation(async () => { + entered() + await gate + return { tools: [] } + }) + try { + const request = task.attemptApiRequest(0).next() + await building + task[flag] = true + release() + await expect(request).rejects.toThrow("aborted during request construction") + expect(createMessage).not.toHaveBeenCalled() + expect(task["currentRequestAbortController"]).toBeUndefined() + } finally { + release() + build.mockRestore() + } + }, + ) + + it.each( + (["manual", "recovery", "automatic"] as const).flatMap((path) => + (["none", "build", "environment", "summary"] as const).map((cancelAt) => ({ path, cancelAt })), + ), + )( + "passes tools and handles cancellation in $path condensation ($cancelAt)", + async ({ path, cancelAt }) => { + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "search tickets", + startTask: false, + }) + const state = await mockProvider.getState() + vi.spyOn(mockProvider, "getState").mockResolvedValue({ + ...state, + requestDelaySeconds: 0, + autoCondenseContext: true, + "mcp.dynamicLoad.threshold": 2, + "mcp.dynamicLoad.initialK": 1, + }) + const access = getTaskTestAccess(task) + vi.spyOn(access, "getSystemPrompt").mockResolvedValue("mock prompt") + vi.spyOn(access, "saveClineMessages").mockResolvedValue(true) + vi.spyOn(task, "submitUserMessage").mockResolvedValue(undefined) + const model: ModelInfo = { contextWindow: 50000, maxTokens: 1024, supportsPromptCache: false } + vi.spyOn(task.api, "getModel").mockReturnValue({ id: "routing-test", info: model }) + vi.spyOn(task.api, "countTokens").mockResolvedValue(1000) + vi.spyOn(task, "getTokenUsage").mockReturnValue({ + totalCost: 0, + totalTokensIn: 0, + totalTokensOut: 0, + contextTokens: 100000, + }) + vi.spyOn(task.api, "createMessage").mockReturnValue( + asyncStreamFrom([{ type: "text", text: "ok" }]), + ) + task.apiConversationHistory = [ + { role: "user", content: [{ type: "text", text: "search tickets" }], ts: Date.now() }, + ] + const tools: Awaited< + ReturnType + >["tools"] = [ + { + type: "function", + function: { + name: "mcp--catalog--selected", + parameters: { type: "object", properties: {} }, + }, + }, + ] + const build = vi + .spyOn(buildTools, "buildNativeToolsArrayWithRestrictions") + .mockResolvedValue({ tools }) + const summariesBefore = vi.mocked(summarizeConversation).mock.calls.length + const overwrite = vi.spyOn(task, "overwriteApiConversationHistory").mockResolvedValue(undefined) + if (cancelAt === "build") + build.mockImplementationOnce(async () => { + task["abort"] = true + return { tools } + }) + if (cancelAt === "environment") + vi.mocked(getEnvironmentDetails).mockImplementationOnce(async () => { + task["abort"] = true + return "" + }) + if (cancelAt === "summary") + vi.mocked(summarizeConversation).mockImplementationOnce(async ({ metadata }) => { + expect(metadata?.abortSignal).toBeDefined() + task["abort"] = true + task["currentRequestAbortController"]?.abort() + expect(metadata?.abortSignal?.aborted).toBe(true) + return { messages: [], summary: "canceled", cost: 0, newContextTokens: 1 } + }) + try { + if (path === "manual") await task.condenseContext() + else if (path === "recovery") await access.handleContextWindowExceededError(model) + else await task.attemptApiRequest(0).next() + if (path !== "manual") { + const notifications = vi + .mocked(mockProvider.postMessageToWebview) + .mock.calls.map(([message]) => message) + .filter((message) => + ["condenseTaskContextStarted", "condenseTaskContextResponse"].includes( + message.type, + ), + ) + expect(notifications).toEqual([ + { type: "condenseTaskContextStarted", text: task.taskId }, + { type: "condenseTaskContextResponse", text: task.taskId }, + ]) + } + if (cancelAt !== "none") { + expect(vi.mocked(summarizeConversation).mock.calls.length).toBe( + summariesBefore + (cancelAt === "summary" ? 1 : 0), + ) + expect(overwrite).not.toHaveBeenCalled() + return + } + expect(vi.mocked(summarizeConversation).mock.calls.length).toBe(summariesBefore + 1) + expect(vi.mocked(summarizeConversation).mock.calls.at(-1)?.[0].metadata?.tools).toEqual( + tools, + ) + expect(build).toHaveBeenCalledTimes(path === "automatic" ? 2 : 1) + for (const [options] of build.mock.calls) { + expect(options).toEqual( + expect.objectContaining({ + task, + mcpDynamicLoadThreshold: 2, + mcpDynamicLoadInitialK: 1, + }), + ) + } + } finally { + build.mockRestore() + } + }, + ) + it("should invoke abort on currentRequestAbortController during first-chunk wait", async () => { const task = new Task({ provider: mockProvider, @@ -4943,7 +5228,7 @@ describe("Cline", () => { task.cancelCurrentRequest() expect(abortSpy).toHaveBeenCalledTimes(1) - expect(task.currentRequestAbortController).toBeUndefined() + expect(task["currentRequestAbortController"]).toBeUndefined() }) it("should reject streaming consumption when aborted between chunks", async () => { @@ -5152,7 +5437,7 @@ describe("Cline", () => { }, ] - expect(task.currentRequestAbortController).toBeUndefined() + expect(task["currentRequestAbortController"]).toBeUndefined() const iterator = task.attemptApiRequest(0) await iterator.next() @@ -5888,7 +6173,7 @@ describe("Cline", () => { expect(createMessageSpy).not.toHaveBeenCalled() // The per-request controller is only created once the request is // committed, so a cancelled construction never reaches it. - expect(task.currentRequestAbortController).toBeUndefined() + expect(task["currentRequestAbortController"]).toBeUndefined() await cancelling } finally { vi.useRealTimers() @@ -5948,7 +6233,7 @@ describe("Cline", () => { expect(createMessageSpy).not.toHaveBeenCalled() // The per-request controller is only created once the request is // committed, so a disposed construction never reaches it. - expect(task.currentRequestAbortController).toBeUndefined() + expect(task["currentRequestAbortController"]).toBeUndefined() await disposal } finally { vi.useRealTimers() @@ -6888,10 +7173,6 @@ describe("Cline", () => { await pendingPostState }) const saySpy = vi.spyOn(task, "say").mockResolvedValue(undefined) - vi.spyOn(taskAccess, "getEnabledMcpToolsCount").mockResolvedValue({ - enabledToolCount: 0, - enabledServerCount: 0, - }) const initiateTaskLoopSpy = vi.spyOn(taskAccess, "initiateTaskLoop").mockResolvedValue(undefined) const startPromise = taskAccess.startTask("new task") diff --git a/src/core/task/__tests__/build-tools-dynamic-load.integration.spec.ts b/src/core/task/__tests__/build-tools-dynamic-load.integration.spec.ts new file mode 100644 index 0000000000..4e51191620 --- /dev/null +++ b/src/core/task/__tests__/build-tools-dynamic-load.integration.spec.ts @@ -0,0 +1,147 @@ +import type OpenAI from "openai" +import type { McpServer } from "@roo-code/types" +import type { ClineProvider } from "../../webview/ClineProvider" +import { makeExtensionContext } from "../../../test-utils/vscode" +import { ToolRouter } from "../../../services/tools/ToolRouter" +import { buildNativeToolsArrayWithRestrictions } from "../build-tools" + +vi.mock("../../../services/code-index/code-index-manager-registry", () => ({ + CodeIndexManagerRegistry: { getOrCreate: () => undefined }, +})) + +type Options = Parameters[0] + +function names(tools: OpenAI.Chat.ChatCompletionTool[]) { + return tools.flatMap((tool) => ("function" in tool ? [tool.function.name] : [])) +} + +function setup(count: number) { + const server: McpServer = { + name: "catalog", + config: "{}", + status: "connected", + tools: Array.from({ length: count }, (_, index) => ({ name: `tool_${index}`, description: "search tickets" })), + } + const servers = [server] + // Tool construction only uses context and the hub's getServers method. + const provider = { + context: makeExtensionContext(), + getMcpHub: () => ({ getServers: () => servers }), + } as ClineProvider + let initialized = false + const task: NonNullable = { + attachedMcpToolNames: new Set(), + initializeAttachedMcpTools: vi.fn(async (scope, k) => { + if (initialized) return + const selected = await ToolRouter.getInstance().attachInitial("search tickets", scope, k) + for (const name of selected) task.attachedMcpToolNames.add(name) + initialized = true + }), + } + const options: Options = { + provider, + task, + cwd: "/test", + mode: "code", + customModes: [], + experiments: {}, + apiConfiguration: {}, + } + return { server, servers, task, options } +} + +describe.each([false, true])("MCP dynamic loading (all definitions: %s)", (includeAllToolsWithRestrictions) => { + it.each([0, 49, 50])("keeps %s tools at or below the threshold", async (count) => { + const { task, options } = setup(count) + const result = await buildNativeToolsArrayWithRestrictions({ ...options, includeAllToolsWithRestrictions }) + expect(names(result.tools).filter((name) => name.startsWith("mcp--"))).toHaveLength(count) + expect(task.initializeAttachedMcpTools).not.toHaveBeenCalled() + }) + + it.each([undefined, 7])("limits 100 tools to initialK=%s and retains native tools", async (initialK) => { + const { task, options } = setup(100) + const baseline = await buildNativeToolsArrayWithRestrictions({ + ...setup(0).options, + includeAllToolsWithRestrictions, + }) + const result = await buildNativeToolsArrayWithRestrictions({ + ...options, + mcpDynamicLoadInitialK: initialK, + includeAllToolsWithRestrictions, + }) + const declarations = names(result.tools) + const attached = declarations.filter((name) => name.startsWith("mcp--")) + expect(attached).toHaveLength(initialK ?? 30) + expect(new Set(attached)).toEqual(task.attachedMcpToolNames) + expect(declarations.filter((name) => !name.startsWith("mcp--"))).toEqual(names(baseline.tools)) + expect(declarations).not.toContain("mcp_load") + if (includeAllToolsWithRestrictions) { + expect(result.allowedFunctionNames?.filter((name) => name.startsWith("mcp--"))).toEqual(attached) + } + }) + + it("starts routing at exactly 51 tools", async () => { + const { task, options } = setup(51) + const result = await buildNativeToolsArrayWithRestrictions({ ...options, includeAllToolsWithRestrictions }) + expect(names(result.tools).filter((name) => name.startsWith("mcp--"))).toHaveLength(30) + expect(task.initializeAttachedMcpTools).toHaveBeenCalledTimes(1) + }) + + it("defaults polluted runtime settings instead of bypassing the gate", async () => { + const { options } = setup(100) + const result = await buildNativeToolsArrayWithRestrictions({ + ...options, + mcpDynamicLoadThreshold: NaN, + mcpDynamicLoadInitialK: Infinity, + includeAllToolsWithRestrictions, + }) + expect(names(result.tools).filter((name) => name.startsWith("mcp--"))).toHaveLength(30) + }) + + it("uses a configured threshold, including zero", async () => { + const { options, task } = setup(2) + const result = await buildNativeToolsArrayWithRestrictions({ + ...options, + mcpDynamicLoadThreshold: 0, + mcpDynamicLoadInitialK: 1, + includeAllToolsWithRestrictions, + }) + expect(names(result.tools).filter((name) => name.startsWith("mcp--"))).toHaveLength(1) + expect(task.initializeAttachedMcpTools).toHaveBeenCalledTimes(1) + }) + + it("ranks only the mode's allowed servers and prompt-enabled tools", async () => { + const { options, servers, server, task } = setup(100) + servers.push({ ...server, name: "forbidden" }) + server.tools?.push({ name: "hidden", description: "tickets", enabledForPrompt: false }) + await buildNativeToolsArrayWithRestrictions({ + ...options, + mode: "scoped", + includeAllToolsWithRestrictions, + customModes: [ + { + slug: "scoped", + name: "Scoped", + roleDefinition: "Scoped", + groups: ["mcp"], + allowedMcpServers: ["catalog"], + }, + ], + }) + const scope = vi.mocked(task.initializeAttachedMcpTools).mock.calls[0][0] + expect(scope).toHaveLength(100) + expect(scope.every((tool) => tool.serverName === "catalog" && tool.toolName !== "hidden")).toBe(true) + }) + + it("does not rank MCP tools when policy disables them", async () => { + const { options, task } = setup(100) + const result = await buildNativeToolsArrayWithRestrictions({ + ...options, + disabledTools: ["use_mcp_tool"], + includeAllToolsWithRestrictions, + }) + expect(task.initializeAttachedMcpTools).not.toHaveBeenCalled() + const callable = result.allowedFunctionNames ?? names(result.tools) + expect(callable.some((name) => name.startsWith("mcp--"))).toBe(false) + }) +}) diff --git a/src/core/task/build-tools.ts b/src/core/task/build-tools.ts index c5ee7a163c..a1ecb63b62 100644 --- a/src/core/task/build-tools.ts +++ b/src/core/task/build-tools.ts @@ -3,9 +3,12 @@ import path from "path" import type OpenAI from "openai" import type { ProviderSettings, ModeConfig, ModelInfo } from "@roo-code/types" +import { resolveMcpDynamicLoadSettings } from "@roo-code/types" import { customToolRegistry, formatNative } from "@roo-code/core" import type { ClineProvider } from "../webview/ClineProvider" +import type { Task } from "./Task" +import { buildMcpToolName } from "../../utils/mcp-name" import { getRooDirectoriesForCwd } from "../../services/roo-config/index.js" import { getModeBySlug, defaultModeSlug } from "../../shared/modes" @@ -17,6 +20,9 @@ import { } from "../prompts/tools/filter-tools-for-mode" interface BuildToolsOptions { + task?: Pick + mcpDynamicLoadThreshold?: number + mcpDynamicLoadInitialK?: number provider: ClineProvider cwd: string mode: string | undefined @@ -134,10 +140,40 @@ export async function buildNativeToolsArrayWithRestrictions(options: BuildToolsO // the same disabledTools/modelInfo the native filter consumes also gate the // dynamic mcp--* declarations, which all represent use_mcp_tool. const mcpTools = getMcpServerTools(mcpHub, allowedMcpServers) - const filteredMcpTools = filterMcpToolsForMode(mcpTools, mode, customModes, experiments, { + let filteredMcpTools = filterMcpToolsForMode(mcpTools, mode, customModes, experiments, { disabledTools, modelInfo, }) + let advertisedMcpTools = mcpTools + const { threshold, initialK } = resolveMcpDynamicLoadSettings( + options.mcpDynamicLoadThreshold, + options.mcpDynamicLoadInitialK, + ) + if (options.task && filteredMcpTools.length > threshold) { + const scopedNames = new Set(filteredMcpTools.map(getToolName)) + const scopedTools = (mcpHub?.getServers() ?? []) + .filter((server) => !allowedMcpServers || allowedMcpServers.includes(server.name)) + .flatMap((server) => + (server.tools ?? []) + .filter( + (tool) => + tool.enabledForPrompt !== false && + scopedNames.has(buildMcpToolName(server.name, tool.name)), + ) + .map((tool) => ({ + serverName: server.name, + toolName: tool.name, + description: tool.description ?? "", + })), + ) + await options.task.initializeAttachedMcpTools(scopedTools, initialK) + // This gate limits advertised definitions. Existing execution authorization + // still decides whether a requested tool call is permitted. + const attached = options.task.attachedMcpToolNames + filteredMcpTools = filteredMcpTools.filter((tool) => attached.has(getToolName(tool))) + // Gemini also receives only attached MCP definitions, not the full catalog. + advertisedMcpTools = mcpTools.filter((tool) => attached.has(getToolName(tool))) + } // Add custom tools if they are available and the experiment is enabled. let nativeCustomTools: OpenAI.Chat.ChatCompletionFunctionTool[] = [] @@ -159,7 +195,7 @@ export async function buildNativeToolsArrayWithRestrictions(options: BuildToolsO // allowed names based on mode filtering if (includeAllToolsWithRestrictions) { // Combine ALL tools (unfiltered native + all MCP + custom) - const allTools = [...nativeTools, ...mcpTools, ...nativeCustomTools] + const allTools = [...nativeTools, ...advertisedMcpTools, ...nativeCustomTools] // Extract names of tools that are allowed based on mode filtering. // Resolve any alias names to canonical names to ensure consistency with allTools diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index dc4ed27621..95e1791f73 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -59,6 +59,7 @@ import { RooCodeEventName, requestyDefaultModelId, openRouterDefaultModelId, + resolveMcpDynamicLoadSettings, DEFAULT_WRITE_DELAY_MS, DEFAULT_DIFF_FUZZY_THRESHOLD, DEFAULT_DESTRUCTIVE_COMMAND_GUARD_ENABLED, @@ -3147,6 +3148,8 @@ export class ClineProvider terminalZdotdir, terminalProfile, mcpEnabled, + "mcp.dynamicLoad.threshold": mcpDynamicLoadThreshold, + "mcp.dynamicLoad.initialK": mcpDynamicLoadInitialK, currentApiConfigName, listApiConfigMeta, pinnedApiConfigs, @@ -3267,6 +3270,8 @@ export class ClineProvider // Keep the default unauthenticated state if the optional Zoo Code auth service is unavailable. } + const mcpDynamicLoad = resolveMcpDynamicLoadSettings(mcpDynamicLoadThreshold, mcpDynamicLoadInitialK) + return { version: this.context.extension?.packageJSON?.version ?? "", apiConfiguration: currentTaskApiConfiguration ?? apiConfiguration, @@ -3321,6 +3326,8 @@ export class ClineProvider terminalZdotdir: terminalZdotdir ?? false, terminalProfile, mcpEnabled: mcpEnabled ?? true, + "mcp.dynamicLoad.threshold": mcpDynamicLoad.threshold, + "mcp.dynamicLoad.initialK": mcpDynamicLoad.initialK, currentApiConfigName: currentTask ? currentTaskApiConfigName : currentApiConfigName, listApiConfigMeta: listApiConfigMeta ?? [], pinnedApiConfigs: pinnedApiConfigs ?? {}, @@ -3507,6 +3514,11 @@ export class ClineProvider const taskSyncEnabled: boolean = false + const mcpDynamicLoad = resolveMcpDynamicLoadSettings( + stateValues["mcp.dynamicLoad.threshold"], + stateValues["mcp.dynamicLoad.initialK"], + ) + // Return the same structure as before. return { apiConfiguration: providerSettings, @@ -3561,6 +3573,8 @@ export class ClineProvider mode: stateValues.mode ?? defaultModeSlug, language: stateValues.language ?? formatLanguage(vscode.env.language), mcpEnabled: stateValues.mcpEnabled ?? true, + "mcp.dynamicLoad.threshold": mcpDynamicLoad.threshold, + "mcp.dynamicLoad.initialK": mcpDynamicLoad.initialK, mcpServers: this.mcpHub?.getAllServers() ?? [], currentApiConfigName: stateValues.currentApiConfigName ?? "default", listApiConfigMeta: stateValues.listApiConfigMeta ?? [], diff --git a/src/core/webview/__tests__/ClineProvider.spec.ts b/src/core/webview/__tests__/ClineProvider.spec.ts index 07bacb91f6..0cc513dfc4 100644 --- a/src/core/webview/__tests__/ClineProvider.spec.ts +++ b/src/core/webview/__tests__/ClineProvider.spec.ts @@ -4182,6 +4182,25 @@ describe("ClineProvider", () => { expect(state.allowedWriteFiles).toEqual([]) }) + test.each([ + { threshold: undefined, initialK: undefined, expectedThreshold: 50, expectedK: 30 }, + { threshold: 0, initialK: 7, expectedThreshold: 0, expectedK: 7 }, + { threshold: 100, initialK: 20, expectedThreshold: 100, expectedK: 20 }, + { threshold: -1, initialK: NaN, expectedThreshold: 50, expectedK: 30 }, + { threshold: Infinity, initialK: 0, expectedThreshold: 50, expectedK: 30 }, + ])( + "round-trips MCP routing settings ($threshold, $initialK)", + async ({ threshold, initialK, expectedThreshold, expectedK }) => { + await provider.resolveWebviewView(mockWebviewView) + await provider.contextProxy.setValue("mcp.dynamicLoad.threshold", threshold) + await provider.contextProxy.setValue("mcp.dynamicLoad.initialK", initialK) + for (const state of [await provider.getState(), await provider.getStateToPostToWebview()]) { + expect(state["mcp.dynamicLoad.threshold"]).toBe(expectedThreshold) + expect(state["mcp.dynamicLoad.initialK"]).toBe(expectedK) + } + }, + ) + test("getStateToPostToWebview returns the saved destructive command guard setting", async () => { await provider.resolveWebviewView(mockWebviewView) await provider.contextProxy.setValue("destructiveCommandGuardEnabled", true) diff --git a/src/core/webview/__tests__/webviewMessageHandler.spec.ts b/src/core/webview/__tests__/webviewMessageHandler.spec.ts index 685a7b11c1..804187a1ee 100644 --- a/src/core/webview/__tests__/webviewMessageHandler.spec.ts +++ b/src/core/webview/__tests__/webviewMessageHandler.spec.ts @@ -1905,6 +1905,33 @@ describe("webviewMessageHandler - mcpEnabled", () => { }) }) +describe.each(["mcp.dynamicLoad.threshold", "mcp.dynamicLoad.initialK"] as const)( + "webviewMessageHandler - %s", + (key) => { + beforeEach(() => vi.clearAllMocks()) + it.each([undefined, 1, 30, 100])("persists valid value %s", async (value) => { + await webviewMessageHandler(mockClineProvider, { + type: "updateSettings", + updatedSettings: { [key]: value }, + }) + expect(mockClineProvider.contextProxy.setValue).toHaveBeenCalledWith(key, value) + }) + it.each([-1, 1.5, NaN, Infinity])("rejects invalid value %s", async (value) => { + await webviewMessageHandler(mockClineProvider, { + type: "updateSettings", + updatedSettings: { [key]: value }, + }) + expect(mockClineProvider.contextProxy.setValue).not.toHaveBeenCalled() + }) + it("accepts zero only for the threshold", async () => { + await webviewMessageHandler(mockClineProvider, { type: "updateSettings", updatedSettings: { [key]: 0 } }) + if (key === "mcp.dynamicLoad.threshold") + expect(mockClineProvider.contextProxy.setValue).toHaveBeenCalledWith(key, 0) + else expect(mockClineProvider.contextProxy.setValue).not.toHaveBeenCalled() + }) + }, +) + describe("webviewMessageHandler - destructiveCommandGuardEnabled", () => { beforeEach(() => { vi.clearAllMocks() diff --git a/src/core/webview/webviewMessageHandler.ts b/src/core/webview/webviewMessageHandler.ts index 2a04aa413f..9b35682688 100644 --- a/src/core/webview/webviewMessageHandler.ts +++ b/src/core/webview/webviewMessageHandler.ts @@ -30,6 +30,7 @@ import { RouterModelsMessageType, VsCodeLmModelsMessageType, isTelemetryOptedIn, + globalSettingsSchema, } from "@roo-code/types" import { customToolRegistry } from "@roo-code/core" import { CloudService } from "@roo-code/cloud" @@ -850,6 +851,17 @@ export const webviewMessageHandler = async ( } } else if (key === "execaShellPath") { Terminal.setExecaShellPath(value as string | undefined) + } else if (key === "mcp.dynamicLoad.threshold" || key === "mcp.dynamicLoad.initialK") { + const parsed = globalSettingsSchema.shape[key].safeParse(value) + if (!parsed.success) { + console.warn( + "[settings] Ignoring invalid MCP dynamic-load setting:", + key, + parsed.error.message, + ) + continue + } + newValue = parsed.data } else if (key === "mcpEnabled") { newValue = value ?? true const mcpHub = provider.getMcpHub() diff --git a/src/utils/mcp-name.ts b/src/utils/mcp-name.ts index 7512fff80a..b9d75cf4d4 100644 --- a/src/utils/mcp-name.ts +++ b/src/utils/mcp-name.ts @@ -1,3 +1,5 @@ +import { isCanonicalMcpToolName } from "@roo-code/types" + /** * Utilities for sanitizing MCP server and tool names to conform to * API function name requirements across all providers. @@ -161,13 +163,18 @@ export function parseMcpToolName(mcpToolName: string): { serverName: string; too // Split on the separator to get server and tool names const separatorIndex = remainder.indexOf(MCP_TOOL_SEPARATOR) if (separatorIndex === -1) { + // A long server name can consume the entire generated name. Dispatch + // resolves these prefixes against the advertised catalog before execution. + if (normalizedName.length === 64 && isCanonicalMcpToolName(normalizedName)) { + return { serverName: remainder, toolName: "" } + } return null } const serverName = remainder.slice(0, separatorIndex) const toolName = remainder.slice(separatorIndex + MCP_TOOL_SEPARATOR.length) - if (!serverName || !toolName) { + if ((!serverName || !toolName) && !(normalizedName.length === 64 && isCanonicalMcpToolName(normalizedName))) { return null } diff --git a/webview-ui/src/components/chat/ChatRow.tsx b/webview-ui/src/components/chat/ChatRow.tsx index 3b6a3ca66b..2462ce09cd 100644 --- a/webview-ui/src/components/chat/ChatRow.tsx +++ b/webview-ui/src/components/chat/ChatRow.tsx @@ -1588,6 +1588,7 @@ export const ChatRowContent = ({ ) case "too_many_tools_warning": { + // Historical messages only; Task no longer emits this warning. const warningData = safeJsonParse<{ toolCount: number serverCount: number diff --git a/webview-ui/src/hooks/useTooManyTools.ts b/webview-ui/src/hooks/useTooManyTools.ts index 75a3d834c6..a4aef9a1f0 100644 --- a/webview-ui/src/hooks/useTooManyTools.ts +++ b/webview-ui/src/hooks/useTooManyTools.ts @@ -21,6 +21,8 @@ export interface TooManyToolsInfo { /** * Hook that calculates tool counts and provides localized warning messages. * Used by MCP settings views to summarize enabled tool counts. + * This legacy catalog advisory is separate from the task-scoped routing gate. + * Its replacement with routing status and controls is deferred to #581. * * @returns Tool count information and localized messages *