Skip to content

fix: keep stdin open for can_use_tool permission responses#1106

Open
Echolonius wants to merge 1 commit into
anthropics:mainfrom
Echolonius:fix/can-use-tool-keep-stdin-open
Open

fix: keep stdin open for can_use_tool permission responses#1106
Echolonius wants to merge 1 commit into
anthropics:mainfrom
Echolonius:fix/can-use-tool-keep-stdin-open

Conversation

@Echolonius

Copy link
Copy Markdown

Fixes #1105.

Problem

wait_for_result_and_end_input() keeps stdin open until the first result only when SDK MCP servers or hooks are present. A can_use_tool callback needs the same treatment: the CLI delivers can_use_tool permission requests over the control protocol mid-turn, and the SDK writes the permission response back over stdin.

With only can_use_tool configured (the query() + AsyncIterable path that the callback's own ValueError directs users to), stdin closed as soon as the input stream was exhausted. Every subsequent permission request then failed with Tool permission request failed: Error: Stream closed, the callback was never invoked, and the model burned its turns retrying the tool.

Fix

Add self.can_use_tool to the keep-stdin-open condition — the same contract already applied for SDK MCP servers (#731) and hooks.

Verification

  • Real CLI A/B repro (SDK 0.2.116, CLI 2.1.207): with can_use_tool only, the callback never fired and every Write failed with Stream closed; adding a no-op hook whose matcher never fires (which only flips this condition) made the same run succeed. After this fix, the no-hook case invokes the callback and completes: can_use_tool calls: ['Write'], file created.
  • New regression test test_streaming_prompt_with_can_use_tool_waits_for_result: the mock transport enforces the real CLI contract — the can_use_tool control request arrives only after the input stream is exhausted, the result is not produced until the permission response has been written, and writes after end_input() raise. Fails on main with RuntimeError: stdin closed; passes with the fix.
  • pytest tests/test_query.py tests/test_tool_callbacks.py: 75 passed. ruff check / ruff format --check clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YcMi8ny9DUBDf6y6m76HWk

wait_for_result_and_end_input() kept stdin open until the first result
only when SDK MCP servers or hooks were present. A can_use_tool callback
also needs the control protocol: the CLI sends can_use_tool requests
mid-turn and the SDK writes the permission response back over stdin.

With only can_use_tool configured (the query() + AsyncIterable path its
own ValueError directs users to), stdin closed as soon as the input
stream was exhausted, so every permission request failed with
"Tool permission request failed: Error: Stream closed" and the callback
was never invoked.

Include can_use_tool in the keep-stdin-open condition, matching the
existing behavior for SDK MCP servers and hooks.
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.

can_use_tool callback never invoked without hooks/MCP servers: stdin closes before permission requests arrive ("Error: Stream closed")

1 participant