Skip to content

Conversation

@szwang
Copy link
Collaborator

@szwang szwang commented Oct 7, 2025

This commit completes the canUseTool permission functionality implementation in the Python SDK to match the TypeScript SDK behavior:

Key Changes:

  1. Transport Layer Enhancement (subprocess_cli.py):

    • Added --can-use-tool CLI flag when canUseTool callback is provided
    • Ensures the CLI process knows to enable permission callback mode
  2. Example Implementation (can_use_tool_example.py):

    • Comprehensive example showing how to use canUseTool callbacks
    • Demonstrates security policies, input modification, and denial patterns
    • Shows integration with ClaudeSDKClient in streaming mode

Features Already Implemented:

  • Type Definitions: CanUseTool, PermissionResult*, ToolPermissionContext in types.py
  • Client Integration: Full canUseTool support in ClaudeSDKClient with proper validation
  • Query Support: Both query() function and ClaudeSDKClient handle canUseTool callbacks
  • Control Protocol: Complete bidirectional permission request/response handling
  • Input Validation: Ensures streaming mode is required, mutual exclusion with permission_prompt_tool_name
  • Comprehensive Tests: Full test coverage in test_tool_callbacks.py

The implementation follows the TypeScript SDK's architecture exactly, providing:

  • Tool permission interception before execution
  • Input modification capabilities
  • Permission updates and suggestions
  • Proper error handling and validation

🤖 Generated with Claude Code

This commit completes the canUseTool permission functionality implementation in the Python SDK to match the TypeScript SDK behavior:

## Key Changes:

1. **Transport Layer Enhancement** (`subprocess_cli.py`):
   - Added `--can-use-tool` CLI flag when canUseTool callback is provided
   - Ensures the CLI process knows to enable permission callback mode

2. **Example Implementation** (`can_use_tool_example.py`):
   - Comprehensive example showing how to use canUseTool callbacks
   - Demonstrates security policies, input modification, and denial patterns
   - Shows integration with ClaudeSDKClient in streaming mode

## Features Already Implemented:

- **Type Definitions**: CanUseTool, PermissionResult*, ToolPermissionContext in `types.py`
- **Client Integration**: Full canUseTool support in ClaudeSDKClient with proper validation
- **Query Support**: Both query() function and ClaudeSDKClient handle canUseTool callbacks
- **Control Protocol**: Complete bidirectional permission request/response handling
- **Input Validation**: Ensures streaming mode is required, mutual exclusion with permission_prompt_tool_name
- **Comprehensive Tests**: Full test coverage in test_tool_callbacks.py

The implementation follows the TypeScript SDK's architecture exactly, providing:
- Tool permission interception before execution
- Input modification capabilities
- Permission updates and suggestions
- Proper error handling and validation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@szwang szwang marked this pull request as draft October 7, 2025 04:55
@szwang szwang closed this Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants