fix: address Windows MCP server tool registration issues (#902)#1011
Open
Oxygen56 wants to merge 1 commit into
Open
fix: address Windows MCP server tool registration issues (#902)#1011Oxygen56 wants to merge 1 commit into
Oxygen56 wants to merge 1 commit into
Conversation
) On Windows 11, stdio MCP servers configured via mcp_servers={...} were failing to register their tools. The MCP server instructions appeared in the agent's context, but tool calls returned 'No such tool available' and the agent detected a standard Claude Code session rather than an SDK- launched one. Root causes addressed: 1. MCP config passed as inline JSON on command line (Windows): Writing the --mcp-config JSON directly as a CLI argument on Windows is fragile. On Windows, the config is now written to a temporary .json file and the file path is passed instead. This follows the same pattern established in anthropics#245 for --agents. 2. Case-insensitive env var collision on Windows: Windows environment variable names are case-insensitive, but the env block passed to CreateProcess preserves dict-key casing. Strip any inherited entries whose case-insensitive key matches SDK-controlled keys (CLAUDE_CODE_ENTRYPOINT, CLAUDE_AGENT_SDK_VERSION). 3. Command-line length safety net: Added a warning when the total command-line length approaches the Windows CreateProcess limit (8191 chars), helping diagnose 'command line too long' errors proactively. Changes: - Add import tempfile - Add self._temp_files list for cleanup - Write MCP config to temp file on Windows - Clean up temp files in close() - Case-insensitive env key dedup on Windows - Windows command-line length warning Closes anthropics#902 Co-Authored-By: Claude Opus 4.8 <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 #902 -- MCP server tools fail to register on Windows 11. The MCP server instructions appear in the agent context, but tool calls return "No such tool available" and the agent detects a standard Claude Code session rather than an SDK-launched one.
Root causes addressed
1. MCP config passed as inline JSON on the Windows command line
On non-Windows platforms, passing
--mcp-configas inline JSON works fine because the subprocess receives each argument as a separate string via exec*. On Windows, the entire command line is a single string processed by CommandLineToArgvW. While Python's subprocess.list2cmdline usually handles quoting correctly, inline JSON with special characters in env var values, long configs, or edge cases in the quoting rules can corrupt the JSON.Fix: On Windows, write the
--mcp-configJSON to a temporary.jsonfile and pass the file path instead. The CLI already supports file paths for this flag (when the value does not start with{). This follows the same pattern established in #245 for--agents.2. Case-insensitive environment variable collision on Windows
On Windows, environment variable names are case-insensitive, but the environment block passed to CreateProcess preserves the casing from the dict keys. If the inherited environment happens to contain CLAUDE_CODE_ENTRYPOINT with different casing than the SDK's value (e.g., claude_code_entrypoint), Python's dict merge treats them as distinct keys. Windows' GetEnvironmentVariable may then return the inherited entry instead of the SDK-controlled sdk-py value, causing the CLI to think it is running in a standard session.
Fix: Strip any inherited environment entries whose case-insensitive key matches SDK-controlled keys (CLAUDE_CODE_ENTRYPOINT, CLAUDE_AGENT_SDK_VERSION) before merging.
3. Command-line length safety net
Added a warning log when the total command-line length approaches the Windows CreateProcess limit (8191 characters) to proactively diagnose "command line too long" errors.
Changes
Testing
Closes #902