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
This commit is contained in:
Roo Code 2025-07-18 14:58:37 +00:00
parent 1b57b878dd
commit cebbafa37b
4 changed files with 4 additions and 4 deletions

View file

@ -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.
`

View file

@ -6,7 +6,7 @@ export async function getMcpServersSection(
diffStrategy?: DiffStrategy,
enableMcpServerCreation?: boolean,
): Promise<string> {
if (!mcpHub || mcpHub.getAllServers().length === 0) {
if (!mcpHub || (mcpHub.getAllServers()?.length ?? 0) === 0) {
return ""
}

View file

@ -1608,7 +1608,7 @@ export class Task extends EventEmitter<ClineEvents> {
})
}
const hasMcpServers = (mcpHub?.getAllServers().length ?? 0) > 0
const hasMcpServers = mcpHub && (mcpHub.getAllServers().length ?? 0) > 0
const rooIgnoreInstructions = this.rooIgnoreController?.getInstructions()

View file

@ -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,