-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: restore MCP tool auto-approval behavior for issue #9214 #9215
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
Open
roomote
wants to merge
1
commit into
main
Choose a base branch
from
fix/mcp-tool-auto-approve-regression
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.
Open
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
288 changes: 288 additions & 0 deletions
288
src/core/auto-approval/__tests__/checkAutoApproval.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,288 @@ | ||
| import { describe, it, expect } from "vitest" | ||
| import { checkAutoApproval } from "../index" | ||
| import type { ExtensionState } from "../../../shared/ExtensionMessage" | ||
| import type { McpServer } from "../../../shared/mcp" | ||
|
|
||
| describe("checkAutoApproval", () => { | ||
| describe("MCP tool auto-approval", () => { | ||
| it("should auto-approve MCP tools when alwaysAllowMcp is true and tool has no explicit alwaysAllow", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [ | ||
| { | ||
| name: "test-server", | ||
| config: "test", | ||
| status: "connected", | ||
| tools: [ | ||
| { | ||
| name: "test-tool", | ||
| description: "Test tool", | ||
| // No alwaysAllow property - should default to auto-approve | ||
| }, | ||
| ], | ||
| } as McpServer, | ||
| ], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "use_mcp_tool", | ||
| serverName: "test-server", | ||
| toolName: "test-tool", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("approve") | ||
| }) | ||
|
|
||
| it("should auto-approve MCP tools when alwaysAllowMcp is true and tool has alwaysAllow=true", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [ | ||
| { | ||
| name: "test-server", | ||
| config: "test", | ||
| status: "connected", | ||
| tools: [ | ||
| { | ||
| name: "test-tool", | ||
| description: "Test tool", | ||
| alwaysAllow: true, | ||
| }, | ||
| ], | ||
| } as McpServer, | ||
| ], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "use_mcp_tool", | ||
| serverName: "test-server", | ||
| toolName: "test-tool", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("approve") | ||
| }) | ||
|
|
||
| it("should NOT auto-approve MCP tools when tool explicitly has alwaysAllow=false", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [ | ||
| { | ||
| name: "test-server", | ||
| config: "test", | ||
| status: "connected", | ||
| tools: [ | ||
| { | ||
| name: "test-tool", | ||
| description: "Test tool", | ||
| alwaysAllow: false, // Explicitly disabled | ||
| }, | ||
| ], | ||
| } as McpServer, | ||
| ], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "use_mcp_tool", | ||
| serverName: "test-server", | ||
| toolName: "test-tool", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("ask") | ||
| }) | ||
|
|
||
| it("should NOT auto-approve MCP tools when alwaysAllowMcp is false", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: false, // Global MCP auto-approval disabled | ||
| mcpServers: [ | ||
| { | ||
| name: "test-server", | ||
| config: "test", | ||
| status: "connected", | ||
| tools: [ | ||
| { | ||
| name: "test-tool", | ||
| description: "Test tool", | ||
| }, | ||
| ], | ||
| } as McpServer, | ||
| ], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "use_mcp_tool", | ||
| serverName: "test-server", | ||
| toolName: "test-tool", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("ask") | ||
| }) | ||
|
|
||
| it("should NOT auto-approve when autoApprovalEnabled is false", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: false, // Auto-approval completely disabled | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "use_mcp_tool", | ||
| serverName: "test-server", | ||
| toolName: "test-tool", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("ask") | ||
| }) | ||
|
|
||
| it("should auto-approve MCP resources when alwaysAllowMcp is true", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "access_mcp_resource", | ||
| serverName: "test-server", | ||
| resourceUri: "test://resource", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("approve") | ||
| }) | ||
|
|
||
| it("should handle missing or unknown servers gracefully", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [ | ||
| { | ||
| name: "different-server", | ||
| config: "test", | ||
| status: "connected", | ||
| tools: [], | ||
| } as McpServer, | ||
| ], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "use_mcp_tool", | ||
| serverName: "unknown-server", | ||
| toolName: "test-tool", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| // Should still auto-approve since alwaysAllowMcp is true and tool doesn't exist to have alwaysAllow=false | ||
| expect(result.decision).toBe("approve") | ||
| }) | ||
|
|
||
| it("should handle missing tools gracefully", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [ | ||
| { | ||
| name: "test-server", | ||
| config: "test", | ||
| status: "connected", | ||
| tools: [ | ||
| { | ||
| name: "different-tool", | ||
| description: "Different tool", | ||
| }, | ||
| ], | ||
| } as McpServer, | ||
| ], | ||
| } | ||
|
|
||
| const mcpServerUse = JSON.stringify({ | ||
| type: "use_mcp_tool", | ||
| serverName: "test-server", | ||
| toolName: "unknown-tool", | ||
| }) | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: mcpServerUse, | ||
| }) | ||
|
|
||
| // Should still auto-approve since alwaysAllowMcp is true and tool doesn't exist to have alwaysAllow=false | ||
| expect(result.decision).toBe("approve") | ||
| }) | ||
|
|
||
| it("should handle invalid JSON gracefully", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [], | ||
| } | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: "invalid json", | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("ask") | ||
| }) | ||
|
|
||
| it("should handle missing text gracefully", async () => { | ||
| const state: Partial<ExtensionState> = { | ||
| autoApprovalEnabled: true, | ||
| alwaysAllowMcp: true, | ||
| mcpServers: [], | ||
| } | ||
|
|
||
| const result = await checkAutoApproval({ | ||
| state: state as any, | ||
| ask: "use_mcp_server", | ||
| text: undefined, | ||
| }) | ||
|
|
||
| expect(result.decision).toBe("ask") | ||
| }) | ||
| }) | ||
| }) |
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
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
isMcpToolAlwaysAllowedimport is no longer used after the refactoring and should be removed to keep the code clean.Fix it with Roo Code or mention @roomote and request a fix.