|
| 1 | +import type { McpServer, McpTool } from "@roo-code/types" |
| 2 | + |
| 3 | +import type { McpHub } from "../../../../services/mcp/McpHub" |
| 4 | + |
| 5 | +import { getMcpServersSection } from "../mcp-servers" |
| 6 | + |
| 7 | +describe("getMcpServersSection", () => { |
| 8 | + const createMockTool = (name: string, description = "Test tool", enabledForPrompt?: boolean): McpTool => ({ |
| 9 | + name, |
| 10 | + description, |
| 11 | + inputSchema: { |
| 12 | + type: "object", |
| 13 | + properties: {}, |
| 14 | + }, |
| 15 | + ...(enabledForPrompt !== undefined ? { enabledForPrompt } : {}), |
| 16 | + }) |
| 17 | + |
| 18 | + const createMockServer = ( |
| 19 | + name: string, |
| 20 | + tools: McpTool[], |
| 21 | + options: { instructions?: string; source?: "global" | "project" } = {}, |
| 22 | + ): McpServer => ({ |
| 23 | + name, |
| 24 | + config: JSON.stringify({ type: "stdio", command: "test" }), |
| 25 | + status: "connected", |
| 26 | + source: options.source ?? "global", |
| 27 | + tools, |
| 28 | + instructions: options.instructions, |
| 29 | + }) |
| 30 | + |
| 31 | + const createMockMcpHub = (servers: McpServer[]): Partial<McpHub> => ({ |
| 32 | + getServers: vi.fn().mockReturnValue(servers), |
| 33 | + }) |
| 34 | + |
| 35 | + it("should return empty string when mcpHub is undefined", () => { |
| 36 | + const result = getMcpServersSection(undefined) |
| 37 | + expect(result).toBe("") |
| 38 | + }) |
| 39 | + |
| 40 | + it("should return empty string when no servers are available", () => { |
| 41 | + const mockHub = createMockMcpHub([]) |
| 42 | + const result = getMcpServersSection(mockHub as McpHub) |
| 43 | + expect(result).toBe("") |
| 44 | + }) |
| 45 | + |
| 46 | + it("should return empty string when servers have no enabled tools", () => { |
| 47 | + const server = createMockServer("testServer", []) |
| 48 | + const mockHub = createMockMcpHub([server]) |
| 49 | + const result = getMcpServersSection(mockHub as McpHub) |
| 50 | + expect(result).toBe("") |
| 51 | + }) |
| 52 | + |
| 53 | + it("should return empty string when all tools are disabled", () => { |
| 54 | + const server = createMockServer("testServer", [ |
| 55 | + createMockTool("tool1", "Tool 1", false), |
| 56 | + createMockTool("tool2", "Tool 2", false), |
| 57 | + ]) |
| 58 | + const mockHub = createMockMcpHub([server]) |
| 59 | + const result = getMcpServersSection(mockHub as McpHub) |
| 60 | + expect(result).toBe("") |
| 61 | + }) |
| 62 | + |
| 63 | + it("should include MCP SERVERS header", () => { |
| 64 | + const server = createMockServer("testServer", [createMockTool("testTool")]) |
| 65 | + const mockHub = createMockMcpHub([server]) |
| 66 | + const result = getMcpServersSection(mockHub as McpHub) |
| 67 | + expect(result).toContain("MCP SERVERS") |
| 68 | + }) |
| 69 | + |
| 70 | + it("should explain the naming convention", () => { |
| 71 | + const server = createMockServer("testServer", [createMockTool("testTool")]) |
| 72 | + const mockHub = createMockMcpHub([server]) |
| 73 | + const result = getMcpServersSection(mockHub as McpHub) |
| 74 | + expect(result).toContain("mcp--serverName--toolName") |
| 75 | + }) |
| 76 | + |
| 77 | + it("should list server name as heading", () => { |
| 78 | + const server = createMockServer("context7", [createMockTool("resolve-library-id")]) |
| 79 | + const mockHub = createMockMcpHub([server]) |
| 80 | + const result = getMcpServersSection(mockHub as McpHub) |
| 81 | + expect(result).toContain("## context7") |
| 82 | + }) |
| 83 | + |
| 84 | + it("should list tool names using the mcp-- naming convention", () => { |
| 85 | + const server = createMockServer("context7", [ |
| 86 | + createMockTool("resolve-library-id"), |
| 87 | + createMockTool("get-library-docs"), |
| 88 | + ]) |
| 89 | + const mockHub = createMockMcpHub([server]) |
| 90 | + const result = getMcpServersSection(mockHub as McpHub) |
| 91 | + expect(result).toContain("mcp--context7--resolve-library-id") |
| 92 | + expect(result).toContain("mcp--context7--get-library-docs") |
| 93 | + }) |
| 94 | + |
| 95 | + it("should include server instructions when provided", () => { |
| 96 | + const server = createMockServer("conport", [createMockTool("init-db")], { |
| 97 | + instructions: "Always initialize the database before performing queries.", |
| 98 | + }) |
| 99 | + const mockHub = createMockMcpHub([server]) |
| 100 | + const result = getMcpServersSection(mockHub as McpHub) |
| 101 | + expect(result).toContain("Server Instructions:") |
| 102 | + expect(result).toContain("Always initialize the database before performing queries.") |
| 103 | + }) |
| 104 | + |
| 105 | + it("should not include server instructions section when not provided", () => { |
| 106 | + const server = createMockServer("testServer", [createMockTool("testTool")]) |
| 107 | + const mockHub = createMockMcpHub([server]) |
| 108 | + const result = getMcpServersSection(mockHub as McpHub) |
| 109 | + expect(result).not.toContain("Server Instructions:") |
| 110 | + }) |
| 111 | + |
| 112 | + it("should list multiple servers", () => { |
| 113 | + const server1 = createMockServer("context7", [createMockTool("resolve-library-id")]) |
| 114 | + const server2 = createMockServer("git", [createMockTool("git-status")]) |
| 115 | + const mockHub = createMockMcpHub([server1, server2]) |
| 116 | + const result = getMcpServersSection(mockHub as McpHub) |
| 117 | + expect(result).toContain("## context7") |
| 118 | + expect(result).toContain("## git") |
| 119 | + expect(result).toContain("mcp--context7--resolve-library-id") |
| 120 | + expect(result).toContain("mcp--git--git-status") |
| 121 | + }) |
| 122 | + |
| 123 | + it("should filter out disabled tools", () => { |
| 124 | + const server = createMockServer("testServer", [ |
| 125 | + createMockTool("enabledTool", "Enabled tool"), |
| 126 | + createMockTool("disabledTool", "Disabled tool", false), |
| 127 | + ]) |
| 128 | + const mockHub = createMockMcpHub([server]) |
| 129 | + const result = getMcpServersSection(mockHub as McpHub) |
| 130 | + expect(result).toContain("mcp--testServer--enabledTool") |
| 131 | + expect(result).not.toContain("mcp--testServer--disabledTool") |
| 132 | + }) |
| 133 | + |
| 134 | + it("should skip servers with only disabled tools", () => { |
| 135 | + const serverWithDisabledTools = createMockServer("disabledServer", [createMockTool("tool1", "Tool 1", false)]) |
| 136 | + const serverWithEnabledTools = createMockServer("enabledServer", [createMockTool("tool1", "Tool 1")]) |
| 137 | + const mockHub = createMockMcpHub([serverWithDisabledTools, serverWithEnabledTools]) |
| 138 | + const result = getMcpServersSection(mockHub as McpHub) |
| 139 | + expect(result).not.toContain("## disabledServer") |
| 140 | + expect(result).toContain("## enabledServer") |
| 141 | + }) |
| 142 | + |
| 143 | + it("should skip servers with undefined tools", () => { |
| 144 | + const serverWithUndefinedTools: McpServer = { |
| 145 | + name: "noTools", |
| 146 | + config: JSON.stringify({ type: "stdio", command: "test" }), |
| 147 | + status: "connected", |
| 148 | + tools: undefined, |
| 149 | + } |
| 150 | + const serverWithTools = createMockServer("withTools", [createMockTool("tool1")]) |
| 151 | + const mockHub = createMockMcpHub([serverWithUndefinedTools, serverWithTools]) |
| 152 | + const result = getMcpServersSection(mockHub as McpHub) |
| 153 | + expect(result).not.toContain("## noTools") |
| 154 | + expect(result).toContain("## withTools") |
| 155 | + }) |
| 156 | +}) |
0 commit comments