diff --git a/src/core/prompts/instructions/__tests__/instructions.test.ts b/src/core/prompts/instructions/__tests__/instructions.test.ts new file mode 100644 index 00000000000..0ecf2d8523e --- /dev/null +++ b/src/core/prompts/instructions/__tests__/instructions.test.ts @@ -0,0 +1,109 @@ +import { describe, it, expect, vi, beforeEach } from "vitest" +import { fetchInstructions } from "../instructions" +import { createMCPServerInstructions } from "../create-mcp-server" +import { createModeInstructions } from "../create-mode" +import { McpHub } from "../../../../services/mcp/McpHub" +import { DiffStrategy } from "../../../../shared/tools" +import * as vscode from "vscode" + +// Mock the imported modules +vi.mock("../create-mcp-server", () => ({ + createMCPServerInstructions: vi.fn(), +})) + +vi.mock("../create-mode", () => ({ + createModeInstructions: vi.fn(), +})) + +describe("fetchInstructions", () => { + const mockMcpHub = {} as McpHub + const mockDiffStrategy = {} as DiffStrategy + const mockContext = {} as vscode.ExtensionContext + + beforeEach(() => { + vi.clearAllMocks() + }) + + describe("create_mcp_server", () => { + it("should return MCP server instructions when enableMcpServerCreation is true", async () => { + const mockInstructions = "MCP server creation instructions" + vi.mocked(createMCPServerInstructions).mockResolvedValue(mockInstructions) + + const result = await fetchInstructions("create_mcp_server", { + mcpHub: mockMcpHub, + diffStrategy: mockDiffStrategy, + enableMcpServerCreation: true, + }) + + expect(result).toBe(mockInstructions) + expect(createMCPServerInstructions).toHaveBeenCalledWith(mockMcpHub, mockDiffStrategy) + }) + + it("should return MCP server instructions when enableMcpServerCreation is undefined (default true)", async () => { + const mockInstructions = "MCP server creation instructions" + vi.mocked(createMCPServerInstructions).mockResolvedValue(mockInstructions) + + const result = await fetchInstructions("create_mcp_server", { + mcpHub: mockMcpHub, + diffStrategy: mockDiffStrategy, + // enableMcpServerCreation is undefined + }) + + expect(result).toBe(mockInstructions) + expect(createMCPServerInstructions).toHaveBeenCalledWith(mockMcpHub, mockDiffStrategy) + }) + + it("should return disabled message when enableMcpServerCreation is false", async () => { + const result = await fetchInstructions("create_mcp_server", { + mcpHub: mockMcpHub, + diffStrategy: mockDiffStrategy, + enableMcpServerCreation: false, + }) + + expect(result).toBe( + "MCP server creation is currently disabled. This feature can be enabled in the settings.", + ) + expect(createMCPServerInstructions).not.toHaveBeenCalled() + }) + }) + + describe("create_mode", () => { + it("should return mode creation instructions", async () => { + const mockInstructions = "Mode creation instructions" + vi.mocked(createModeInstructions).mockResolvedValue(mockInstructions) + + const result = await fetchInstructions("create_mode", { + context: mockContext, + }) + + expect(result).toBe(mockInstructions) + expect(createModeInstructions).toHaveBeenCalledWith(mockContext) + }) + + it("should not be affected by enableMcpServerCreation setting", async () => { + const mockInstructions = "Mode creation instructions" + vi.mocked(createModeInstructions).mockResolvedValue(mockInstructions) + + const result = await fetchInstructions("create_mode", { + context: mockContext, + enableMcpServerCreation: false, + }) + + expect(result).toBe(mockInstructions) + expect(createModeInstructions).toHaveBeenCalledWith(mockContext) + }) + }) + + describe("unknown task", () => { + it("should return empty string for unknown task", async () => { + const result = await fetchInstructions("unknown_task", { + mcpHub: mockMcpHub, + diffStrategy: mockDiffStrategy, + }) + + expect(result).toBe("") + expect(createMCPServerInstructions).not.toHaveBeenCalled() + expect(createModeInstructions).not.toHaveBeenCalled() + }) + }) +}) diff --git a/src/core/prompts/instructions/instructions.ts b/src/core/prompts/instructions/instructions.ts index c1ff2a1899e..65df8cd9303 100644 --- a/src/core/prompts/instructions/instructions.ts +++ b/src/core/prompts/instructions/instructions.ts @@ -8,11 +8,16 @@ interface InstructionsDetail { mcpHub?: McpHub diffStrategy?: DiffStrategy context?: vscode.ExtensionContext + enableMcpServerCreation?: boolean } export async function fetchInstructions(text: string, detail: InstructionsDetail): Promise { switch (text) { case "create_mcp_server": { + // Check if MCP server creation is enabled + if (detail.enableMcpServerCreation === false) { + return "MCP server creation is currently disabled. This feature can be enabled in the settings." + } return await createMCPServerInstructions(detail.mcpHub, detail.diffStrategy) } case "create_mode": { diff --git a/src/core/tools/fetchInstructionsTool.ts b/src/core/tools/fetchInstructionsTool.ts index 5325f98fbf4..42ffb5b8ee8 100644 --- a/src/core/tools/fetchInstructionsTool.ts +++ b/src/core/tools/fetchInstructionsTool.ts @@ -36,7 +36,7 @@ export async function fetchInstructionsTool( return } - // Bow fetch the content and provide it to the agent. + // Now fetch the content and provide it to the agent. const provider = cline.providerRef.deref() const mcpHub = provider?.getMcpHub() @@ -46,7 +46,12 @@ export async function fetchInstructionsTool( const diffStrategy = cline.diffStrategy const context = provider?.context - const content = await fetchInstructions(task, { mcpHub, diffStrategy, context }) + + // Get the enableMcpServerCreation setting from provider state + const state = await provider?.getState() + const enableMcpServerCreation = state?.enableMcpServerCreation ?? true + + const content = await fetchInstructions(task, { mcpHub, diffStrategy, context, enableMcpServerCreation }) if (!content) { pushToolResult(formatResponse.toolError(`Invalid instructions request: ${task}`))