fix #227: Fix PermissionResultAllow conversion to control_response (#232)
Some checks are pending
Lint / lint (push) Waiting to run
Test / test-examples (3.11) (push) Blocked by required conditions
Test / test-examples (3.10) (push) Blocked by required conditions
Test / test (macos-latest, 3.10) (push) Waiting to run
Test / test (macos-latest, 3.11) (push) Waiting to run
Test / test (macos-latest, 3.12) (push) Waiting to run
Test / test (macos-latest, 3.13) (push) Waiting to run
Test / test (ubuntu-latest, 3.10) (push) Waiting to run
Test / test (ubuntu-latest, 3.11) (push) Waiting to run
Test / test (ubuntu-latest, 3.12) (push) Waiting to run
Test / test (ubuntu-latest, 3.13) (push) Waiting to run
Test / test (windows-latest, 3.10) (push) Waiting to run
Test / test (windows-latest, 3.11) (push) Waiting to run
Test / test (windows-latest, 3.12) (push) Waiting to run
Test / test (windows-latest, 3.13) (push) Waiting to run
Test / test-e2e (macos-latest, 3.10) (push) Blocked by required conditions
Test / test-e2e (macos-latest, 3.11) (push) Blocked by required conditions
Test / test-e2e (macos-latest, 3.12) (push) Blocked by required conditions
Test / test-e2e (macos-latest, 3.13) (push) Blocked by required conditions
Test / test-e2e (ubuntu-latest, 3.10) (push) Blocked by required conditions
Test / test-e2e (ubuntu-latest, 3.11) (push) Blocked by required conditions
Test / test-e2e (ubuntu-latest, 3.12) (push) Blocked by required conditions
Test / test-e2e (ubuntu-latest, 3.13) (push) Blocked by required conditions
Test / test-e2e (windows-latest, 3.10) (push) Blocked by required conditions
Test / test-e2e (windows-latest, 3.11) (push) Blocked by required conditions
Test / test-e2e (windows-latest, 3.12) (push) Blocked by required conditions
Test / test-e2e (windows-latest, 3.13) (push) Blocked by required conditions
Test / test-examples (3.12) (push) Blocked by required conditions
Test / test-examples (3.13) (push) Blocked by required conditions

https://github.com/anthropics/claude-agent-sdk-python/issues/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'}]
```
This commit is contained in:
Victor Mota 2025-10-09 12:24:13 -07:00 committed by GitHub
parent 71a85ac9aa
commit dcd51c9ecb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -195,6 +195,7 @@ class Query:
if subtype == "can_use_tool":
permission_request: SDKControlPermissionRequest = request_data # type: ignore[assignment]
original_input = permission_request["input"]
# Handle tool permission request
if not self.can_use_tool:
raise Exception("canUseTool callback is not provided")
@ -213,9 +214,14 @@ class Query:
# Convert PermissionResult to expected dict format
if isinstance(response, PermissionResultAllow):
response_data = {"behavior": "allow"}
if response.updated_input is not None:
response_data["updatedInput"] = response.updated_input
response_data = {
"behavior": "allow",
"updatedInput": (
response.updated_input
if response.updated_input is not None
else original_input
),
}
if response.updated_permissions is not None:
response_data["updatedPermissions"] = [
permission.to_dict()