Agent Wealth HubFree public resources
中文 EN العربية

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

  1. Register an async callback with ClaudeAgentOptions(can_use_tool=callback, permission_mode="default"). [perm]
  2. Return PermissionResultAllow() or PermissionResultDeny(message=...) based on tool_name and input_data; a write can be redirected with Allow carrying updated_input. [perm]
  3. 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

Permissions

Limitations

The example’s prefix check is not full path canonicalization; production policy must also address symlinks and traversal.

Pinned upstream source

https://github.com/anthropics/claude-agent-sdk-python/blob/b6e9d12fe1cc98dde988ab7b7713c1feeee50c6c/examples/tool_permission_callback.py

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

Related resources