Skip to content

Commit dcd51c9

Browse files
authored
fix #227: Fix PermissionResultAllow conversion to control_response (#232)
#227 Fixes zod issues returned by cli subprocess transport - example now runs correctly to completion: ``` ============================================================ Tool Permission Callback Example ============================================================ This example demonstrates how to: 1. Allow/deny tools based on type 2. Modify tool inputs for safety 3. Log tool usage 4. Prompt for unknown tools ============================================================ 📝 Sending query to Claude... 📨 Receiving response... 💬 Claude: I'll help you with these tasks. Let me execute them in sequence. 💬 Claude: Now I'll create a simple Python hello world script: 🔧 Tool Permission Request: Write Input: { "file_path": "/Users/vimota/code/claude-agent-sdk-python/hello.py", "content": "#!/usr/bin/env python3\n\nprint(\"Hello, World!\")\n" } ⚠️ Redirecting write from /Users/vimota/code/claude-agent-sdk-python/hello.py to ./safe_output/hello.py 💬 Claude: Now let's run the script: 🔧 Tool Permission Request: Bash Input: { "command": "python hello.py", "description": "Run hello.py script" } ✅ Allowing bash command: python hello.py 💬 Claude: Let me check where the file was created: 💬 Claude: I see the file was created in the `safe_output` directory. Let me run it from there: 🔧 Tool Permission Request: Bash Input: { "command": "python ./safe_output/hello.py", "description": "Run hello.py from safe_output" } ✅ Allowing bash command: python ./safe_output/hello.py 💬 Claude: Perfect! All tasks completed successfully: 1. **Listed files** - The directory contains a Python SDK project with source code in `src/`, tests, examples, and configuration files. 2. **Created hello.py** - A simple Python script was created at `./safe_output/hello.py` with a basic "Hello, World!" print statement. 3. **Ran the script** - The script executed successfully and printed "Hello, World!" to the console. Note: The file was created in the `safe_output/` subdirectory rather than the root directory. ✅ Task completed! Duration: 31158ms Cost: $0.0736 Messages processed: 18 ============================================================ Tool Usage Summary ============================================================ 1. Tool: Write Input: { "file_path": "/Users/vimota/code/claude-agent-sdk-python/hello.py", "content": "#!/usr/bin/env python3\n\nprint(\"Hello, World!\")\n" } Suggestions: [{'type': 'setMode', 'mode': 'acceptEdits', 'destination': 'session'}] 2. Tool: Bash Input: { "command": "python hello.py", "description": "Run hello.py script" } Suggestions: [{'type': 'addRules', 'rules': [{'toolName': 'Bash', 'ruleContent': 'python:*'}], 'behavior': 'allow', 'destination': 'localSettings'}] 3. Tool: Bash Input: { "command": "python ./safe_output/hello.py", "description": "Run hello.py from safe_output" } Suggestions: [{'type': 'addRules', 'rules': [{'toolName': 'Bash', 'ruleContent': 'python:*'}], 'behavior': 'allow', 'destination': 'localSettings'}] ```
1 parent 71a85ac commit dcd51c9

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/claude_agent_sdk/_internal/query.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ async def _handle_control_request(self, request: SDKControlRequest) -> None:
195195

196196
if subtype == "can_use_tool":
197197
permission_request: SDKControlPermissionRequest = request_data # type: ignore[assignment]
198+
original_input = permission_request["input"]
198199
# Handle tool permission request
199200
if not self.can_use_tool:
200201
raise Exception("canUseTool callback is not provided")
@@ -213,9 +214,14 @@ async def _handle_control_request(self, request: SDKControlRequest) -> None:
213214

214215
# Convert PermissionResult to expected dict format
215216
if isinstance(response, PermissionResultAllow):
216-
response_data = {"behavior": "allow"}
217-
if response.updated_input is not None:
218-
response_data["updatedInput"] = response.updated_input
217+
response_data = {
218+
"behavior": "allow",
219+
"updatedInput": (
220+
response.updated_input
221+
if response.updated_input is not None
222+
else original_input
223+
),
224+
}
219225
if response.updated_permissions is not None:
220226
response_data["updatedPermissions"] = [
221227
permission.to_dict()

0 commit comments

Comments
 (0)