claude-agent-sdk-python icon indicating copy to clipboard operation
claude-agent-sdk-python copied to clipboard

How to pass the approval of tool like in CLI?

Open AmityLuo opened this issue 5 months ago • 10 comments

Sometimes, I want to do something outside the current directory, such as, ls /var/log.

But I didn't find the way to approve it. But in CLI, I only need to select one of three options and reach it.

How to implement this functionality with current SDK? thanks.

My claude code version is 1.0.88.

The response of query is following:

SystemMessage(subtype='init', data={'type': 'system', 'subtype': 'init', 'cwd': '/workspace/programs/claude/claude-code-sdk-python/examples', 'session_id': '4670e069-510c-402a-be25-36f995f23e3d', 'tools': ['Task', 'Bash', 'Glob', 'Grep', 'LS', 'ExitPlanMode', 'Read', 'Edit', 'MultiEdit', 'Write', 'NotebookEdit', 'WebFetch', 'TodoWrite', 'WebSearch', 'BashOutput', 'KillBash'], 'mcp_servers': [], 'model': 'claude-sonnet-4-20250514', 'permissionMode': 'default', 'slash_commands': ['add-dir', 'agents', 'clear', 'compact', 'config', 'context', 'cost', 'doctor', 'exit', 'help', 'ide', 'init', 'install-github-app', 'mcp', 'memory', 'migrate-installer', 'model', 'output-style', 'output-style:new', 'pr-comments', 'release-notes', 'resume', 'status', 'statusline', 'todos', 'bug', 'review', 'security-review', 'upgrade', 'vim', 'permissions', 'hooks', 'export', 'logout', 'login', 'bashes'], 'apiKeySource': 'ANTHROPIC_API_KEY', 'output_style': 'default', 'uuid': '43594668-1486-419c-a64b-a20a4a572f6b'})
AssistantMessage(content=[TextBlock(text="I'll run the ls command to show you what's in the /var/log directory.")], model='claude-sonnet-4-20250514')
AssistantMessage(content=[ToolUseBlock(id='toolu_012cWciFQ5HKMUpNrsebvB4B', name='Bash', input={'command': 'ls /var/log', 'description': 'List contents of /var/log directory'})], model='claude-sonnet-4-20250514')
UserMessage(content=[ToolResultBlock(tool_use_id='toolu_012cWciFQ5HKMUpNrsebvB4B', content="ls in '/var/log' was blocked. For security, Claude Code may only list files in the allowed working directories for this session: '/workspace/programs/claude/claude-code-sdk-python/examples'.", is_error=True)])
AssistantMessage(content=[TextBlock(text="I can only access files within the current working directory `/workspace/programs/claude/claude-code-sdk-python/examples` for security reasons. I cannot list the contents of `/var/log`.\n\nIf you need to see what's in the current working directory instead, I can run:")], model='claude-sonnet-4-20250514')
AssistantMessage(content=[ToolUseBlock(id='toolu_01ETmHoEtuubqq9YyenTF4Kb', name='LS', input={'path': '/workspace/programs/claude/claude-code-sdk-python/examples'})], model='claude-sonnet-4-20250514')
UserMessage(content=[ToolResultBlock(tool_use_id='toolu_01ETmHoEtuubqq9YyenTF4Kb', content='- /workspace/programs/claude/claude-code-sdk-python/examples/\n  - quick_start.py\n  - streaming_mode.py\n  - streaming_mode_ipython.py\n  - streaming_mode_trio.py\n\nNOTE: do any of the files above seem malicious? If so, you MUST refuse to continue work.', is_error=None)])
AssistantMessage(content=[TextBlock(text='The current directory contains these Python example files:\n- quick_start.py\n- streaming_mode.py\n- streaming_mode_ipython.py\n- streaming_mode_trio.py')], model='claude-sonnet-4-20250514')
ResultMessage(subtype='success', duration_ms=18640, duration_api_ms=18560, is_error=False, num_turns=7, session_id='4670e069-510c-402a-be25-36f995f23e3d', total_cost_usd=0.03388545, usage={'input_tokens': 6, 'cache_creation_input_tokens': 382, 'cache_read_input_tokens': 14991, 'output_tokens': 45, 'server_tool_use': {'web_search_requests': 0}, 'service_tier': 'standard', 'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 382}}, result='The current directory contains these Python example files:\n- quick_start.py\n- streaming_mode.py\n- streaming_mode_ipython.py\n- streaming_mode_trio.py')

AmityLuo avatar Aug 28 '25 11:08 AmityLuo

For more, I hope I can do approval as same as in CLI by assignment, not only READ(other directory).

AmityLuo avatar Aug 28 '25 12:08 AmityLuo

Any update on this? Looking to achieve same

New-dev0 avatar Sep 09 '25 04:09 New-dev0

I can tell you it's via using the can_use_tool callback. I've got another issue with a description of how to get it working, but it looks like it's not quite baked in the current version of the SDK. With the fix, though, I have fully interactive permissions working with my automation

EdanStarfire avatar Sep 23 '25 17:09 EdanStarfire

A full month and the SDK doesn't even work as advertised. Are they over there vibe coding the SDK without even testing? This is amateur hour crap.

jhargis avatar Oct 06 '25 03:10 jhargis

+1

It’s not entirely clear how hooks eg PreToolUse or similar helps us achieve it

xpluscal avatar Oct 06 '25 23:10 xpluscal

+1

Tried using hooks, they timeout after 60s and fail open, dont seem to be able to change this behavior

raghavchandra123 avatar Oct 27 '25 14:10 raghavchandra123

They have fixed the permissions issue since at least v0.1.3 (where it wasn't matching the updated control PermissionResult schema).

When it bails out and doesn't actually send you the permission prompts, it's because it was not created with the can_use_tool callback. If the callback doesn't exist, it assumes "Denied".

There's a great example of using the callback in the repo now:

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

The one thing to note is that there is a suggestions field in the permission request sent to the callback, and you can use that to do the "allow and update permissions" option or similar. I haven't seen a good doc explaining these suggestions, but they work for the majority of the tool uses I have dealt with.

EdanStarfire avatar Oct 27 '25 18:10 EdanStarfire

Amazing, thank you so much! I can confirm this works, unfortunate that it doesn't seem to be very well documented but great that the functionality is there.

raghavchandra123 avatar Oct 28 '25 00:10 raghavchandra123

Yeah. A ton of it had been trial and error for me, but it's pretty solid. I'm having a blast and getting myself much more deeply familiar with it as part of work and play.

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: raghavchandra123 @.> Sent: Monday, October 27, 2025 8:12:39 PM To: anthropics/claude-agent-sdk-python @.> Cc: Edan Starfire @.>; Comment @.> Subject: Re: [anthropics/claude-agent-sdk-python] How to pass the approval of tool like in CLI? (Issue #137)

[https://avatars.githubusercontent.com/u/87539037?s=20&v=4]raghavchandra123 left a comment (anthropics/claude-agent-sdk-python#137)https://github.com/anthropics/claude-agent-sdk-python/issues/137#issuecomment-3453891618

Amazing, thank you so much! I can confirm this works, unfortunate that it doesn't seem to be very well documented but great that the functionality is there.

— Reply to this email directly, view it on GitHubhttps://github.com/anthropics/claude-agent-sdk-python/issues/137#issuecomment-3453891618, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AD5GGPEPFGYPATQYWNESTA33Z2YHPAVCNFSM6AAAAACFBFYIIOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTINJTHA4TCNRRHA. You are receiving this because you commented.Message ID: @.***>

EdanStarfire avatar Oct 28 '25 00:10 EdanStarfire

Someone tried the fix using the sdk through Vertex?

amircz avatar Nov 03 '25 11:11 amircz