From cebbafa37ba1deb5914d5424bc296d95c0eaadb5 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Fri, 18 Jul 2025 14:58:37 +0000 Subject: [PATCH] fix: improve MCP section logic consistency and null handling - Enhanced null/undefined handling for mcpHub across all files - Made MCP server detection logic consistent between Task.ts and generateSystemPrompt.ts - Added defensive programming with optional chaining for getAllServers() calls - Ensures MCP sections are properly omitted when no servers are available --- src/core/prompts/sections/capabilities.ts | 2 +- src/core/prompts/sections/mcp-servers.ts | 2 +- src/core/task/Task.ts | 2 +- src/core/webview/generateSystemPrompt.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/prompts/sections/capabilities.ts b/src/core/prompts/sections/capabilities.ts index 47e86ab452..65aa925dc5 100644 --- a/src/core/prompts/sections/capabilities.ts +++ b/src/core/prompts/sections/capabilities.ts @@ -33,7 +33,7 @@ CAPABILITIES ? "\n- You can use the browser_action tool to interact with websites (including html files and locally running development servers) through a Puppeteer-controlled browser when you feel it is necessary in accomplishing the user's task. This tool is particularly useful for web development tasks as it allows you to launch a browser, navigate to pages, interact with elements through clicks and keyboard input, and capture the results through screenshots and console logs. This tool may be useful at key stages of web development tasks-such as after implementing new features, making substantial changes, when troubleshooting issues, or to verify the result of your work. You can analyze the provided screenshots to ensure correct rendering or identify errors, and review console logs for runtime issues.\n - For example, if asked to add a component to a react website, you might create the necessary files, use execute_command to run the site locally, then use browser_action to launch the browser, navigate to the local server, and verify the component renders & functions correctly before closing the browser." : "" }${ - mcpHub && mcpHub.getAllServers().length > 0 + mcpHub && (mcpHub.getAllServers()?.length ?? 0) > 0 ? ` - You have access to MCP servers that may provide additional tools and resources. Each server may provide different capabilities that you can use to accomplish tasks more effectively. ` diff --git a/src/core/prompts/sections/mcp-servers.ts b/src/core/prompts/sections/mcp-servers.ts index f56918b0ed..21072000a6 100644 --- a/src/core/prompts/sections/mcp-servers.ts +++ b/src/core/prompts/sections/mcp-servers.ts @@ -6,7 +6,7 @@ export async function getMcpServersSection( diffStrategy?: DiffStrategy, enableMcpServerCreation?: boolean, ): Promise { - if (!mcpHub || mcpHub.getAllServers().length === 0) { + if (!mcpHub || (mcpHub.getAllServers()?.length ?? 0) === 0) { return "" } diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index 3f9a9cdf0e..5a80d57c7a 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -1608,7 +1608,7 @@ export class Task extends EventEmitter { }) } - const hasMcpServers = (mcpHub?.getAllServers().length ?? 0) > 0 + const hasMcpServers = mcpHub && (mcpHub.getAllServers().length ?? 0) > 0 const rooIgnoreInstructions = this.rooIgnoreController?.getInstructions() diff --git a/src/core/webview/generateSystemPrompt.ts b/src/core/webview/generateSystemPrompt.ts index 3dd93e3d4f..afa047e8db 100644 --- a/src/core/webview/generateSystemPrompt.ts +++ b/src/core/webview/generateSystemPrompt.ts @@ -64,7 +64,7 @@ export const generateSystemPrompt = async (provider: ClineProvider, message: Web const canUseBrowserTool = modelSupportsComputerUse && modeSupportsBrowser && (browserToolEnabled ?? true) const mcpHub = provider.getMcpHub() - const hasMcpServers = (mcpHub?.getAllServers().length ?? 0) > 0 + const hasMcpServers = mcpHub && (mcpHub.getAllServers().length ?? 0) > 0 const systemPrompt = await SYSTEM_PROMPT( provider.context,