|
| 1 | +// npx vitest run core/tools/__tests__/mcpServerRestriction.spec.ts |
| 2 | + |
| 3 | +import type { Task } from "../../task/Task" |
| 4 | +import { isMcpServerAllowed, getAllowedMcpServersForTask, ensureMcpServerAllowed } from "../mcpServerRestriction" |
| 5 | + |
| 6 | +vi.mock("../../../shared/modes", async (importOriginal) => { |
| 7 | + const actual = await importOriginal<typeof import("../../../shared/modes")>() |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + defaultModeSlug: "code", |
| 11 | + getModeBySlug: vi.fn(), |
| 12 | + } |
| 13 | +}) |
| 14 | + |
| 15 | +import { getModeBySlug } from "../../../shared/modes" |
| 16 | + |
| 17 | +const toolError = (error: string) => `ERR:${error}` |
| 18 | + |
| 19 | +function makeTask(state: any): Task { |
| 20 | + return { |
| 21 | + providerRef: { |
| 22 | + deref: () => ({ |
| 23 | + getState: vi.fn().mockResolvedValue(state), |
| 24 | + }), |
| 25 | + }, |
| 26 | + consecutiveMistakeCount: 0, |
| 27 | + didToolFailInCurrentTurn: false, |
| 28 | + recordToolError: vi.fn(), |
| 29 | + } as unknown as Task |
| 30 | +} |
| 31 | + |
| 32 | +describe("isMcpServerAllowed", () => { |
| 33 | + it("allows all servers when allowlist is undefined (backward compatible)", () => { |
| 34 | + expect(isMcpServerAllowed("any-server", undefined)).toBe(true) |
| 35 | + }) |
| 36 | + |
| 37 | + it("rejects all servers when allowlist is empty", () => { |
| 38 | + expect(isMcpServerAllowed("any-server", [])).toBe(false) |
| 39 | + }) |
| 40 | + |
| 41 | + it("allows a server present in a populated allowlist", () => { |
| 42 | + expect(isMcpServerAllowed("allowed", ["allowed", "other"])).toBe(true) |
| 43 | + }) |
| 44 | + |
| 45 | + it("rejects a server absent from a populated allowlist", () => { |
| 46 | + expect(isMcpServerAllowed("disallowed", ["allowed", "other"])).toBe(false) |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe("getAllowedMcpServersForTask", () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.mocked(getModeBySlug).mockReset() |
| 53 | + }) |
| 54 | + |
| 55 | + it("returns the mode's allowedMcpServers when defined", async () => { |
| 56 | + vi.mocked(getModeBySlug).mockReturnValue({ |
| 57 | + slug: "code", |
| 58 | + name: "Code", |
| 59 | + roleDefinition: "", |
| 60 | + groups: ["mcp"], |
| 61 | + allowedMcpServers: ["srv-a"], |
| 62 | + } as any) |
| 63 | + const task = makeTask({ mode: "code", customModes: [] }) |
| 64 | + await expect(getAllowedMcpServersForTask(task)).resolves.toEqual(["srv-a"]) |
| 65 | + }) |
| 66 | + |
| 67 | + it("returns undefined when the mode does not restrict servers", async () => { |
| 68 | + vi.mocked(getModeBySlug).mockReturnValue({ |
| 69 | + slug: "code", |
| 70 | + name: "Code", |
| 71 | + roleDefinition: "", |
| 72 | + groups: ["mcp"], |
| 73 | + } as any) |
| 74 | + const task = makeTask({ mode: "code", customModes: [] }) |
| 75 | + await expect(getAllowedMcpServersForTask(task)).resolves.toBeUndefined() |
| 76 | + }) |
| 77 | + |
| 78 | + it("returns undefined when the mode cannot be resolved", async () => { |
| 79 | + vi.mocked(getModeBySlug).mockReturnValue(undefined as any) |
| 80 | + const task = makeTask({ mode: "missing", customModes: [] }) |
| 81 | + await expect(getAllowedMcpServersForTask(task)).resolves.toBeUndefined() |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +describe("ensureMcpServerAllowed", () => { |
| 86 | + beforeEach(() => { |
| 87 | + vi.mocked(getModeBySlug).mockReset() |
| 88 | + }) |
| 89 | + |
| 90 | + function mockModeAllowlist(allowedMcpServers?: string[]) { |
| 91 | + vi.mocked(getModeBySlug).mockReturnValue({ |
| 92 | + slug: "code", |
| 93 | + name: "Code", |
| 94 | + roleDefinition: "", |
| 95 | + groups: ["mcp"], |
| 96 | + ...(allowedMcpServers !== undefined ? { allowedMcpServers } : {}), |
| 97 | + } as any) |
| 98 | + } |
| 99 | + |
| 100 | + it("allows invocation when allowlist is undefined (allows all)", async () => { |
| 101 | + mockModeAllowlist(undefined) |
| 102 | + const task = makeTask({ mode: "code", customModes: [] }) |
| 103 | + const pushToolResult = vi.fn() |
| 104 | + |
| 105 | + const result = await ensureMcpServerAllowed(task, "use_mcp_tool", "anything", pushToolResult, toolError) |
| 106 | + |
| 107 | + expect(result).toBe(true) |
| 108 | + expect(pushToolResult).not.toHaveBeenCalled() |
| 109 | + expect(task.recordToolError).not.toHaveBeenCalled() |
| 110 | + }) |
| 111 | + |
| 112 | + it("allows invocation when server is in the populated allowlist", async () => { |
| 113 | + mockModeAllowlist(["allowed-server"]) |
| 114 | + const task = makeTask({ mode: "code", customModes: [] }) |
| 115 | + const pushToolResult = vi.fn() |
| 116 | + |
| 117 | + const result = await ensureMcpServerAllowed(task, "use_mcp_tool", "allowed-server", pushToolResult, toolError) |
| 118 | + |
| 119 | + expect(result).toBe(true) |
| 120 | + expect(pushToolResult).not.toHaveBeenCalled() |
| 121 | + }) |
| 122 | + |
| 123 | + it("rejects invocation when server is NOT in the populated allowlist", async () => { |
| 124 | + mockModeAllowlist(["allowed-server"]) |
| 125 | + const task = makeTask({ mode: "code", customModes: [] }) |
| 126 | + const pushToolResult = vi.fn() |
| 127 | + |
| 128 | + const result = await ensureMcpServerAllowed( |
| 129 | + task, |
| 130 | + "use_mcp_tool", |
| 131 | + "disallowed-server", |
| 132 | + pushToolResult, |
| 133 | + toolError, |
| 134 | + ) |
| 135 | + |
| 136 | + expect(result).toBe(false) |
| 137 | + expect(task.consecutiveMistakeCount).toBe(1) |
| 138 | + expect(task.didToolFailInCurrentTurn).toBe(true) |
| 139 | + expect(task.recordToolError).toHaveBeenCalledWith("use_mcp_tool") |
| 140 | + expect(pushToolResult).toHaveBeenCalledTimes(1) |
| 141 | + const message = (pushToolResult as any).mock.calls[0][0] as string |
| 142 | + expect(message).toContain("disallowed-server") |
| 143 | + expect(message).toContain("not allowed") |
| 144 | + expect(message).toContain("allowed-server") |
| 145 | + }) |
| 146 | + |
| 147 | + it("rejects all invocations when allowlist is empty", async () => { |
| 148 | + mockModeAllowlist([]) |
| 149 | + const task = makeTask({ mode: "code", customModes: [] }) |
| 150 | + const pushToolResult = vi.fn() |
| 151 | + |
| 152 | + const result = await ensureMcpServerAllowed( |
| 153 | + task, |
| 154 | + "access_mcp_resource", |
| 155 | + "any-server", |
| 156 | + pushToolResult, |
| 157 | + toolError, |
| 158 | + ) |
| 159 | + |
| 160 | + expect(result).toBe(false) |
| 161 | + expect(task.recordToolError).toHaveBeenCalledWith("access_mcp_resource") |
| 162 | + const message = (pushToolResult as any).mock.calls[0][0] as string |
| 163 | + expect(message).toContain("No MCP servers are allowed") |
| 164 | + }) |
| 165 | +}) |
0 commit comments