fix: wait for first result before closing stdin for SDK MCP servers#595
Open
gspeter-max wants to merge 1 commit intoanthropics:mainfrom
Open
fix: wait for first result before closing stdin for SDK MCP servers#595gspeter-max wants to merge 1 commit intoanthropics:mainfrom
gspeter-max wants to merge 1 commit intoanthropics:mainfrom
Conversation
ROOT CAUSE: When using query() with a string prompt + SDK MCP servers, the SDK crashes with CLIConnectionError because stdin is closed immediately after writing the user message. SDK MCP servers require bidirectional communication via the control protocol, which needs stdin to remain open. CHANGES: - Added guard in client.py to check for sdk_mcp_servers or hooks - When present, wait for _first_result_event before closing stdin - Use anyio.move_on_after with timeout for safety - Add debug logging for observability IMPACT: - String prompts with SDK MCP servers now work correctly - Hooks also fixed (same control protocol issue) - No impact on string prompts without SDK MCP servers/hooks - AsyncIterable prompts unchanged (already correct) FILES MODIFIED: - src/claude_agent_sdk/_internal/client.py - tests/test_sdk_mcp_integration.py (added unit test) - e2e-tests/test_sdk_mcp_tools.py (added E2E test) Fixes anthropics#578 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #578 - When using
query()with a string prompt + SDK MCP servers, the SDK crashed withCLIConnectionError: ProcessTransport is not ready for writing.Root Cause
The string prompt path in
client.pyclosed stdin immediately after writing the user message (line 134). SDK MCP servers require bidirectional communication via the control protocol:control_request(mcp_message) via stdoutcontrol_responsevia stdin ❌ (stdin already closed)Once stdin is closed, the SDK cannot send
control_responsemessages back to the CLI, causing all subsequent writes to fail.Changes
src/claude_agent_sdk/_internal/client.pysdk_mcp_serversorconfigured_options.hooksexistquery._first_result_eventbefore closing stdinanyio.move_on_afterwith timeout (fromquery._stream_close_timeout)Tests Added
test_string_prompt_with_sdk_mcp_servers()- Verifies the fix with mocked componentstest_string_prompt_with_sdk_mcp_tool()- Verifies the fix with real CLIImpact
Verification
References
src/claude_agent_sdk/_internal/query.py(lines 270-330)src/claude_agent_sdk/_internal/query.py(lines 582-597)🤖 Generated with Claude Code