| 1 | # Tool Permissions System |
| 2 | |
| 3 | The tool permissions system allows you to control which tools the AI can use and how it can use them. There are three permission levels: |
| 4 | |
| 5 | - **allow**: The tool will be executed automatically without asking for permission |
| 6 | - **ask**: The tool will prompt the user for permission before execution |
| 7 | - **exclude**: The tool will be filtered out entirely and won't be available to the AI |
| 8 | |
| 9 | ## Default Policies |
| 10 | |
| 11 | The system comes with sensible default policies: |
| 12 | |
| 13 | - Read-only tools (`readFile`, `listFiles`, `searchCode`, `fetch`) are **allowed** by default |
| 14 | - Write operations (`writeFile`) require **confirmation** (ask) |
| 15 | - Terminal commands (`runTerminalCommand`) require **confirmation** (ask) |
| 16 | - MCP tools with IDE prefix are **allowed** by default |
| 17 | - Other MCP tools require **confirmation** (ask) |
| 18 | - Any unmatched tools default to **ask** |
| 19 | |
| 20 | ## How It Works |
| 21 | |
| 22 | ### 1. Tool Filtering (Exclude Policy) |
| 23 | |
| 24 | Tools with "exclude" permission are filtered out before being sent to the AI model. This means the AI won't even know these tools exist. |
| 25 | |
| 26 | ### 2. Permission Checking (Ask Policy) |
| 27 | |
| 28 | When the AI tries to use a tool with "ask" permission, the system will: |
| 29 | |
| 30 | 1. Display a permission request in the UI |
| 31 | 2. Wait for user approval (y/n) |
| 32 | 3. Execute the tool if approved, or return an error if denied |
| 33 | |
| 34 | ### 3. Automatic Execution (Allow Policy) |
| 35 | |
| 36 | Tools with "allow" permission are executed immediately without user intervention. |
| 37 | |
| 38 | ## Architecture |
| 39 | |
| 40 | ### Core Components |
| 41 | |
| 42 | - **`types.ts`**: Defines the permission policy types and interfaces |
| 43 | - **`defaultPolicies.ts`**: Contains the hardcoded default permission policies |
| 44 | - **`permissionChecker.ts`**: Implements the permission checking logic |
| 45 | - **`permissionManager.ts`**: Manages permission requests and user responses |
| 46 | - **UI Components**: Handle displaying permission requests and collecting user input |
| 47 | |
| 48 | ### Data Flow |
| 49 | |
| 50 | 1. **Tool Loading**: `getAllTools()` in `streamChatResponse.ts` filters out excluded tools |
| 51 | 2. **Tool Execution**: Before executing each tool call, permissions are checked |
| 52 | 3. **User Interaction**: For "ask" policies, UI displays permission request |
| 53 | 4. **Execution**: Tool is executed or denied based on permission result |
| 54 | |
| 55 | ## UI Integration |
| 56 | |
| 57 | The permission system integrates with the existing chat UI: |
| 58 | |
| 59 | - Permission requests appear as special message types |
| 60 | - Users can approve/deny with y/n keys |
| 61 | - The decision is shown in the chat history |
| 62 | - The system handles the async nature of permission requests |
| 63 | |
| 64 | ## Future Enhancements |
| 65 | |
| 66 | The system is designed to be extensible for future features like: |
| 67 | |
| 68 | - Custom permission policies loaded from configuration |
| 69 | - "Remember this decision" functionality |
| 70 | - Argument-based permission matching (partially implemented) |
| 71 | - Per-session or per-project permission overrides |
| 72 | |
| 73 | ## Example Usage |
| 74 | |
| 75 | ```typescript |
| 76 | // Check permission for a tool call |
| 77 | const result = checkToolPermission({ |
| 78 | name: "writeFile", |
| 79 | arguments: { path: "/important.txt", content: "data" }, |
| 80 | }); |
| 81 | |
| 82 | if (result.permission === "ask") { |
| 83 | // Request user permission |
| 84 | } |
| 85 | |
| 86 | // Filter excluded tools |
| 87 | const allowedTools = filterExcludedTools([ |
| 88 | "readFile", |
| 89 | "dangerousTool", |
| 90 | "writeFile", |
| 91 | ]); |
| 92 | ``` |