-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: re-add lightweight MCP servers section to system prompt for OpenAI compatibility #11323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
roomote
wants to merge
1
commit into
main
Choose a base branch
from
fix/mcp-tools-openai-system-prompt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−1
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
src/core/prompts/sections/__tests__/mcp-servers.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import type { McpServer, McpTool } from "@roo-code/types" | ||
|
|
||
| import type { McpHub } from "../../../../services/mcp/McpHub" | ||
|
|
||
| import { getMcpServersSection } from "../mcp-servers" | ||
|
|
||
| describe("getMcpServersSection", () => { | ||
| const createMockTool = (name: string, description = "Test tool", enabledForPrompt?: boolean): McpTool => ({ | ||
| name, | ||
| description, | ||
| inputSchema: { | ||
| type: "object", | ||
| properties: {}, | ||
| }, | ||
| ...(enabledForPrompt !== undefined ? { enabledForPrompt } : {}), | ||
| }) | ||
|
|
||
| const createMockServer = ( | ||
| name: string, | ||
| tools: McpTool[], | ||
| options: { instructions?: string; source?: "global" | "project" } = {}, | ||
| ): McpServer => ({ | ||
| name, | ||
| config: JSON.stringify({ type: "stdio", command: "test" }), | ||
| status: "connected", | ||
| source: options.source ?? "global", | ||
| tools, | ||
| instructions: options.instructions, | ||
| }) | ||
|
|
||
| const createMockMcpHub = (servers: McpServer[]): Partial<McpHub> => ({ | ||
| getServers: vi.fn().mockReturnValue(servers), | ||
| }) | ||
|
|
||
| it("should return empty string when mcpHub is undefined", () => { | ||
| const result = getMcpServersSection(undefined) | ||
| expect(result).toBe("") | ||
| }) | ||
|
|
||
| it("should return empty string when no servers are available", () => { | ||
| const mockHub = createMockMcpHub([]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toBe("") | ||
| }) | ||
|
|
||
| it("should return empty string when servers have no enabled tools", () => { | ||
| const server = createMockServer("testServer", []) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toBe("") | ||
| }) | ||
|
|
||
| it("should return empty string when all tools are disabled", () => { | ||
| const server = createMockServer("testServer", [ | ||
| createMockTool("tool1", "Tool 1", false), | ||
| createMockTool("tool2", "Tool 2", false), | ||
| ]) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toBe("") | ||
| }) | ||
|
|
||
| it("should include MCP SERVERS header", () => { | ||
| const server = createMockServer("testServer", [createMockTool("testTool")]) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toContain("MCP SERVERS") | ||
| }) | ||
|
|
||
| it("should explain the naming convention", () => { | ||
| const server = createMockServer("testServer", [createMockTool("testTool")]) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toContain("mcp--serverName--toolName") | ||
| }) | ||
|
|
||
| it("should list server name as heading", () => { | ||
| const server = createMockServer("context7", [createMockTool("resolve-library-id")]) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toContain("## context7") | ||
| }) | ||
|
|
||
| it("should list tool names using the mcp-- naming convention", () => { | ||
| const server = createMockServer("context7", [ | ||
| createMockTool("resolve-library-id"), | ||
| createMockTool("get-library-docs"), | ||
| ]) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toContain("mcp--context7--resolve-library-id") | ||
| expect(result).toContain("mcp--context7--get-library-docs") | ||
| }) | ||
|
|
||
| it("should include server instructions when provided", () => { | ||
| const server = createMockServer("conport", [createMockTool("init-db")], { | ||
| instructions: "Always initialize the database before performing queries.", | ||
| }) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toContain("Server Instructions:") | ||
| expect(result).toContain("Always initialize the database before performing queries.") | ||
| }) | ||
|
|
||
| it("should not include server instructions section when not provided", () => { | ||
| const server = createMockServer("testServer", [createMockTool("testTool")]) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).not.toContain("Server Instructions:") | ||
| }) | ||
|
|
||
| it("should list multiple servers", () => { | ||
| const server1 = createMockServer("context7", [createMockTool("resolve-library-id")]) | ||
| const server2 = createMockServer("git", [createMockTool("git-status")]) | ||
| const mockHub = createMockMcpHub([server1, server2]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toContain("## context7") | ||
| expect(result).toContain("## git") | ||
| expect(result).toContain("mcp--context7--resolve-library-id") | ||
| expect(result).toContain("mcp--git--git-status") | ||
| }) | ||
|
|
||
| it("should filter out disabled tools", () => { | ||
| const server = createMockServer("testServer", [ | ||
| createMockTool("enabledTool", "Enabled tool"), | ||
| createMockTool("disabledTool", "Disabled tool", false), | ||
| ]) | ||
| const mockHub = createMockMcpHub([server]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).toContain("mcp--testServer--enabledTool") | ||
| expect(result).not.toContain("mcp--testServer--disabledTool") | ||
| }) | ||
|
|
||
| it("should skip servers with only disabled tools", () => { | ||
| const serverWithDisabledTools = createMockServer("disabledServer", [createMockTool("tool1", "Tool 1", false)]) | ||
| const serverWithEnabledTools = createMockServer("enabledServer", [createMockTool("tool1", "Tool 1")]) | ||
| const mockHub = createMockMcpHub([serverWithDisabledTools, serverWithEnabledTools]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).not.toContain("## disabledServer") | ||
| expect(result).toContain("## enabledServer") | ||
| }) | ||
|
|
||
| it("should skip servers with undefined tools", () => { | ||
| const serverWithUndefinedTools: McpServer = { | ||
| name: "noTools", | ||
| config: JSON.stringify({ type: "stdio", command: "test" }), | ||
| status: "connected", | ||
| tools: undefined, | ||
| } | ||
| const serverWithTools = createMockServer("withTools", [createMockTool("tool1")]) | ||
| const mockHub = createMockMcpHub([serverWithUndefinedTools, serverWithTools]) | ||
| const result = getMcpServersSection(mockHub as McpHub) | ||
| expect(result).not.toContain("## noTools") | ||
| expect(result).toContain("## withTools") | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { McpTool } from "@roo-code/types" | ||
|
|
||
| import { McpHub } from "../../../services/mcp/McpHub" | ||
| import { buildMcpToolName } from "../../../utils/mcp-name" | ||
|
|
||
| /** | ||
| * Generates a lightweight MCP servers section for the system prompt. | ||
| * | ||
| * This provides the model with context about connected MCP servers, including: | ||
| * - The naming convention for MCP tool calls (mcp--serverName--toolName) | ||
| * - A list of connected servers and their available tools | ||
| * - Server-specific instructions (from the MCP protocol's `instructions` field) | ||
| * | ||
| * This does NOT duplicate tool schemas or descriptions, since those are already | ||
| * provided via native tool definitions. The purpose is to give the model enough | ||
| * context to understand how to use these tools, which is especially important | ||
| * for models like OpenAI's GPT series that benefit from explicit system prompt | ||
| * guidance about available MCP tools. | ||
| */ | ||
| export function getMcpServersSection(mcpHub?: McpHub): string { | ||
| if (!mcpHub) { | ||
| return "" | ||
| } | ||
|
|
||
| const servers = mcpHub.getServers() | ||
|
|
||
| if (servers.length === 0) { | ||
| return "" | ||
| } | ||
|
|
||
| // Build per-server entries with tool names and optional instructions | ||
| const serverEntries: string[] = [] | ||
|
|
||
| for (const server of servers) { | ||
| const enabledTools = (server.tools || []).filter((tool: McpTool) => tool.enabledForPrompt !== false) | ||
|
|
||
| if (enabledTools.length === 0) { | ||
| continue | ||
| } | ||
|
|
||
| const toolNames = enabledTools.map((tool: McpTool) => ` - ${buildMcpToolName(server.name, tool.name)}`) | ||
|
|
||
| let entry = `## ${server.name}\n` | ||
| entry += `Tools:\n${toolNames.join("\n")}` | ||
|
|
||
| if (server.instructions) { | ||
| entry += `\n\nServer Instructions:\n${server.instructions}` | ||
| } | ||
|
|
||
| serverEntries.push(entry) | ||
| } | ||
|
|
||
| if (serverEntries.length === 0) { | ||
| return "" | ||
| } | ||
|
|
||
| return `==== | ||
|
|
||
| MCP SERVERS | ||
|
|
||
| The following MCP (Model Context Protocol) servers are connected and provide additional tools you can use. MCP tools are called as native tool calls using the naming convention \`mcp--serverName--toolName\`. When a task could benefit from an MCP tool, prefer using it. | ||
|
|
||
| ${serverEntries.join("\n\n")}` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing
createMockMcpHub(true)insystem-prompt.spec.tscreates a mock server without atoolsarray. SincegetMcpServersSectionskips servers with no enabled tools, this new section always evaluates to""in the integration test, and thewith-mcp-hub-provided.snapsnapshot was not updated. This means there is no integration-level test verifying the new MCP SERVERS section actually appears in the final system prompt output. Consider addingtools(with at least one enabled tool) to the mock so the snapshot captures and locks down this new behavior.Fix it with Roo Code or mention @roomote and request a fix.