Back to catalog / Agent safety
Approve or rewrite tool inputs with a callback
Use can_use_tool to decide separately for reads, writes, and commands, and redirect writes to a safe directory.
Goal
Require application policy checks before agent tool requests are allowed to run.
Steps
- Register an async callback with
ClaudeAgentOptions(can_use_tool=callback, permission_mode="default"). [perm] - Return
PermissionResultAllow()orPermissionResultDeny(message=...)based ontool_nameandinput_data; a write can be redirected with Allow carryingupdated_input. [perm] - Test allow, deny, and rewrite branches with read-only or temporary paths; ensure callback logs are redacted. [perm]
How to verify
Assert denied operations did not run, rewritten file_path equals the safe target, and allowed actions stay in scope.
Troubleshooting
If the callback is not invoked, inspect permission_mode and existing auto-approval settings; do not log raw inputs.
Permissions
Prerequisites
- Python 3.10+, the SDK, and a temporary working directory for permission tests.
Permissions
- Authorize only the test directory; do not hard-code sensitive paths or log confidential inputs.
Limitations
The example’s prefix check is not full path canonicalization; production policy must also address symlinks and traversal.
Pinned upstream source
- Repository
- anthropics/claude-agent-sdk-python
- File path
- examples/tool_permission_callback.py
- Pinned commit
b6e9d12fe1cc98dde988ab7b7713c1feeee50c6c- Source checked
- 2026-10-14 · Reviewed against the pinned upstream source
Repository metrics
These values describe the upstream repository, not popularity or likes for an individual skill or playbook.
- Stars
- Not collected
- Forks
- Not collected
- Observed at
- Not collected
No metrics snapshot is available yet.
License: MIT
Main topic
Make Agent behavior safer and easier to verify