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

@Echolonius
Echolonius force-pushed the fix/can-use-tool-keep-stdin-open branch 2 times, most recently from c595720 to 5683053 Compare July 31, 2026 04:43
@Echolonius
Echolonius force-pushed the fix/can-use-tool-keep-stdin-open branch from 5683053 to 9a90bd4 Compare July 31, 2026 04:44
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