fix: send sdkMcpServers in initialize and defer end_input for string prompts#596
Open
drillan wants to merge 4 commits intoanthropics:mainfrom
Open
fix: send sdkMcpServers in initialize and defer end_input for string prompts#596drillan wants to merge 4 commits intoanthropics:mainfrom
drillan wants to merge 4 commits intoanthropics:mainfrom
Conversation
The Python SDK's Query.initialize() was not including the sdkMcpServers field in the initialize control request. This caused the Claude Code CLI to never register SDK MCP servers, making their tools invisible to the model. The TypeScript SDK (v0.2.49) correctly sends this field. This fix aligns the Python SDK with the TypeScript SDK behavior, following the same pattern as PR anthropics#468 which added the agents field to initialize. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Closing stdin immediately after writing the user message prevents the CLI from completing tools/list via control protocol for SDK MCP servers. This makes SDK MCP tools invisible to the model even after the sdkMcpServers field is correctly sent in the initialize request. The fix defers end_input() until the first result event is received (or timeout), matching the behavior already used for async iterable prompts in stream_input(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The 60-second _stream_close_timeout is designed for async iterable prompts as a safety net. For string prompts with SDK MCP servers, the timeout was closing stdin before the conversation finished, causing "Stream closed" errors on tools/call MCP messages. String prompts wait for the result event without timeout — the result message always arrives when the CLI finishes, and transport cleanup handles crash scenarios. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add else branch for _tg=None case to prevent stdin resource leak - Add sdkMcpServers field to SDKControlInitializeRequest TypedDict - Include hooks in bidirectional communication check (matching stream_input) - Fix inaccurate comments (timeout reference, transport cleanup) - Strengthen test assertions with delayed result gate pattern - Extract shared RESULT_MESSAGE fixture, fix docstring inaccuracies Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Feb 20, 2026
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
SDK MCP servers (type: "sdk") are invisible to the Claude Code CLI when using string prompts. This PR fixes three related issues:
sdkMcpServersfield missing from initialize request — The CLI registers SDK MCP servers from this field, butQuery.initialize()never included it. The TypeScript SDK sends it correctly.Premature
end_input()for string prompts —InternalClient.process_query()closed stdin immediately after writing the user message. This prevented the CLI from completingtools/listvia the bidirectional control protocol, since the SDK could no longer write responses back.60-second timeout on deferred
end_input()— The_stream_close_timeout(designed for async iterable prompts) was used for string prompts too. Conversations taking longer than 60 seconds would have stdin closed mid-conversation, causing "Stream closed" errors on subsequenttools/callMCP messages.Root Cause
The TypeScript SDK always uses async iterable (streaming) mode, where
streamInput()naturally keeps stdin open until the first result. The Python SDK's string prompt path had no equivalent mechanism — it closed stdin synchronously, severing the control protocol channel before the CLI could discover SDK MCP tools.Changes
query.py: AddsdkMcpServersfield to initialize control request when SDK MCP servers existclient.py: For string prompts with SDK MCP servers or hooks, deferend_input()until_first_result_eventfires (no timeout — result always arrives when CLI finishes)client.py: Add defensive else branch for_tg=Nonecasetypes.py: AddsdkMcpServers: NotRequired[list[str]]toSDKControlInitializeRequestTest plan
tools/listreturns tools,tools/callsucceeds) with string prompts🤖 Generated with Claude Code