Skip to content

Comments

fix: send sdkMcpServers in initialize and defer end_input for string prompts#596

Open
drillan wants to merge 4 commits intoanthropics:mainfrom
drillan:fix/sdk-mcp-servers-in-initialize
Open

fix: send sdkMcpServers in initialize and defer end_input for string prompts#596
drillan wants to merge 4 commits intoanthropics:mainfrom
drillan:fix/sdk-mcp-servers-in-initialize

Conversation

@drillan
Copy link

@drillan drillan commented Feb 20, 2026

Summary

SDK MCP servers (type: "sdk") are invisible to the Claude Code CLI when using string prompts. This PR fixes three related issues:

  1. sdkMcpServers field missing from initialize request — The CLI registers SDK MCP servers from this field, but Query.initialize() never included it. The TypeScript SDK sends it correctly.

  2. Premature end_input() for string promptsInternalClient.process_query() closed stdin immediately after writing the user message. This prevented the CLI from completing tools/list via the bidirectional control protocol, since the SDK could no longer write responses back.

  3. 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 subsequent tools/call MCP 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: Add sdkMcpServers field to initialize control request when SDK MCP servers exist
  • client.py: For string prompts with SDK MCP servers or hooks, defer end_input() until _first_result_event fires (no timeout — result always arrives when CLI finishes)
  • client.py: Add defensive else branch for _tg=None case
  • types.py: Add sdkMcpServers: NotRequired[list[str]] to SDKControlInitializeRequest
  • Tests: 9 new test cases covering both fixes

Test plan

  • All 165 existing + new tests pass
  • Verified end-to-end: SDK MCP tools (tools/list returns tools, tools/call succeeds) with string prompts
  • Lint clean (ruff check + ruff format)

🤖 Generated with Claude Code

drillan and others added 4 commits February 20, 2026 17:45
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>
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.

1 participant