Skip to content

fix: address Windows MCP server tool registration issues (#902)#1011

Open
Oxygen56 wants to merge 1 commit into
anthropics:mainfrom
Oxygen56:fix/windows-mcp-tools-902
Open

fix: address Windows MCP server tool registration issues (#902)#1011
Oxygen56 wants to merge 1 commit into
anthropics:mainfrom
Oxygen56:fix/windows-mcp-tools-902

Conversation

@Oxygen56
Copy link
Copy Markdown

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-config as 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-config JSON to a temporary .json file 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

File Change
src/claude_agent_sdk/_internal/transport/subprocess_cli.py Add import tempfile
src/claude_agent_sdk/_internal/transport/subprocess_cli.py Add self._temp_files list for cleanup
src/claude_agent_sdk/_internal/transport/subprocess_cli.py Write MCP config to temp file on Windows
src/claude_agent_sdk/_internal/transport/subprocess_cli.py Clean up temp files in close()
src/claude_agent_sdk/_internal/transport/subprocess_cli.py Case-insensitive env key dedup on Windows
src/claude_agent_sdk/_internal/transport/subprocess_cli.py Windows command-line length warning

Testing

  • All 154 core unit tests pass (including all MCP-config and env-var tests)
  • Changes are non-Windows-platform no-ops (guarded by platform.system() == "Windows")
  • macOS and Linux behavior is completely unchanged

Closes #902

)

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>
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.

MCP server tools fail to register on Windows: server config visible to agent but tools return "No such tool available"

1 participant