Skip to content

Commit 58133f8

Browse files
committed
fix: respect enableMcpServerCreation setting in fetch_instructions tool
- Modified fetchInstructions to check enableMcpServerCreation setting before returning MCP server creation instructions - Updated fetchInstructionsTool to pass the setting from provider state - Added comprehensive tests to verify the fix - When setting is disabled, fetch_instructions with 'create_mcp_server' now returns a message indicating the feature is disabled Fixes #6607
1 parent b2d2a2c commit 58133f8

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { describe, it, expect, vi, beforeEach } from "vitest"
2+
import { fetchInstructions } from "../instructions"
3+
import { createMCPServerInstructions } from "../create-mcp-server"
4+
import { createModeInstructions } from "../create-mode"
5+
import { McpHub } from "../../../../services/mcp/McpHub"
6+
import { DiffStrategy } from "../../../../shared/tools"
7+
import * as vscode from "vscode"
8+
9+
// Mock the imported modules
10+
vi.mock("../create-mcp-server", () => ({
11+
createMCPServerInstructions: vi.fn(),
12+
}))
13+
14+
vi.mock("../create-mode", () => ({
15+
createModeInstructions: vi.fn(),
16+
}))
17+
18+
describe("fetchInstructions", () => {
19+
const mockMcpHub = {} as McpHub
20+
const mockDiffStrategy = {} as DiffStrategy
21+
const mockContext = {} as vscode.ExtensionContext
22+
23+
beforeEach(() => {
24+
vi.clearAllMocks()
25+
})
26+
27+
describe("create_mcp_server", () => {
28+
it("should return MCP server instructions when enableMcpServerCreation is true", async () => {
29+
const mockInstructions = "MCP server creation instructions"
30+
vi.mocked(createMCPServerInstructions).mockResolvedValue(mockInstructions)
31+
32+
const result = await fetchInstructions("create_mcp_server", {
33+
mcpHub: mockMcpHub,
34+
diffStrategy: mockDiffStrategy,
35+
enableMcpServerCreation: true,
36+
})
37+
38+
expect(result).toBe(mockInstructions)
39+
expect(createMCPServerInstructions).toHaveBeenCalledWith(mockMcpHub, mockDiffStrategy)
40+
})
41+
42+
it("should return MCP server instructions when enableMcpServerCreation is undefined (default true)", async () => {
43+
const mockInstructions = "MCP server creation instructions"
44+
vi.mocked(createMCPServerInstructions).mockResolvedValue(mockInstructions)
45+
46+
const result = await fetchInstructions("create_mcp_server", {
47+
mcpHub: mockMcpHub,
48+
diffStrategy: mockDiffStrategy,
49+
// enableMcpServerCreation is undefined
50+
})
51+
52+
expect(result).toBe(mockInstructions)
53+
expect(createMCPServerInstructions).toHaveBeenCalledWith(mockMcpHub, mockDiffStrategy)
54+
})
55+
56+
it("should return disabled message when enableMcpServerCreation is false", async () => {
57+
const result = await fetchInstructions("create_mcp_server", {
58+
mcpHub: mockMcpHub,
59+
diffStrategy: mockDiffStrategy,
60+
enableMcpServerCreation: false,
61+
})
62+
63+
expect(result).toBe(
64+
"MCP server creation is currently disabled. This feature can be enabled in the settings.",
65+
)
66+
expect(createMCPServerInstructions).not.toHaveBeenCalled()
67+
})
68+
})
69+
70+
describe("create_mode", () => {
71+
it("should return mode creation instructions", async () => {
72+
const mockInstructions = "Mode creation instructions"
73+
vi.mocked(createModeInstructions).mockResolvedValue(mockInstructions)
74+
75+
const result = await fetchInstructions("create_mode", {
76+
context: mockContext,
77+
})
78+
79+
expect(result).toBe(mockInstructions)
80+
expect(createModeInstructions).toHaveBeenCalledWith(mockContext)
81+
})
82+
83+
it("should not be affected by enableMcpServerCreation setting", async () => {
84+
const mockInstructions = "Mode creation instructions"
85+
vi.mocked(createModeInstructions).mockResolvedValue(mockInstructions)
86+
87+
const result = await fetchInstructions("create_mode", {
88+
context: mockContext,
89+
enableMcpServerCreation: false,
90+
})
91+
92+
expect(result).toBe(mockInstructions)
93+
expect(createModeInstructions).toHaveBeenCalledWith(mockContext)
94+
})
95+
})
96+
97+
describe("unknown task", () => {
98+
it("should return empty string for unknown task", async () => {
99+
const result = await fetchInstructions("unknown_task", {
100+
mcpHub: mockMcpHub,
101+
diffStrategy: mockDiffStrategy,
102+
})
103+
104+
expect(result).toBe("")
105+
expect(createMCPServerInstructions).not.toHaveBeenCalled()
106+
expect(createModeInstructions).not.toHaveBeenCalled()
107+
})
108+
})
109+
})

src/core/prompts/instructions/instructions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ interface InstructionsDetail {
88
mcpHub?: McpHub
99
diffStrategy?: DiffStrategy
1010
context?: vscode.ExtensionContext
11+
enableMcpServerCreation?: boolean
1112
}
1213

1314
export async function fetchInstructions(text: string, detail: InstructionsDetail): Promise<string> {
1415
switch (text) {
1516
case "create_mcp_server": {
17+
// Check if MCP server creation is enabled
18+
if (detail.enableMcpServerCreation === false) {
19+
return "MCP server creation is currently disabled. This feature can be enabled in the settings."
20+
}
1621
return await createMCPServerInstructions(detail.mcpHub, detail.diffStrategy)
1722
}
1823
case "create_mode": {

src/core/tools/fetchInstructionsTool.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function fetchInstructionsTool(
3636
return
3737
}
3838

39-
// Bow fetch the content and provide it to the agent.
39+
// Now fetch the content and provide it to the agent.
4040
const provider = cline.providerRef.deref()
4141
const mcpHub = provider?.getMcpHub()
4242

@@ -46,7 +46,12 @@ export async function fetchInstructionsTool(
4646

4747
const diffStrategy = cline.diffStrategy
4848
const context = provider?.context
49-
const content = await fetchInstructions(task, { mcpHub, diffStrategy, context })
49+
50+
// Get the enableMcpServerCreation setting from provider state
51+
const state = await provider?.getState()
52+
const enableMcpServerCreation = state?.enableMcpServerCreation ?? true
53+
54+
const content = await fetchInstructions(task, { mcpHub, diffStrategy, context, enableMcpServerCreation })
5055

5156
if (!content) {
5257
pushToolResult(formatResponse.toolError(`Invalid instructions request: ${task}`))

0 commit comments

Comments
 (0)