fix: make pywin32 imports soft to support server-only Windows installs#2241
Open
herakles-dev wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: make pywin32 imports soft to support server-only Windows installs#2241herakles-dev wants to merge 1 commit intomodelcontextprotocol:mainfrom
herakles-dev wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Wrap pywin32 imports in try/except ImportError so that server-only Windows users are not forced to install pywin32. The package is only needed for Job Object support in mcp.client.stdio, but the eager import in mcp/__init__.py pulls it in for all users. All downstream functions (_create_job_object, _maybe_assign_process_to_job, terminate_windows_process_tree) already null-check these modules before use, so graceful degradation is fully covered. Fixes server-only deployments that fail with pywin32 installation errors when antivirus software locks extracted DLLs during uv cache cleanup. Github-Issue: modelcontextprotocol#2233 Reported-by: Nikhil Suresh
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.
Problem
pywin32>=311is a Windows dependency used exclusively for Job Object support inmcp.client.stdio. However, becausemcp/__init__.pyeagerly importsfrom .client.stdio import StdioServerParameters, stdio_client, the pywin32 modules inmcp/os/win32/utilities.pyare loaded unconditionally on Windows — even for server-only users who never use the stdio client.This causes two issues:
Installation failures: The pywin32 wheel contains Windows DLLs that antivirus software (e.g., Windows Defender) locks during extraction, causing
uvcache cleanup to fail with OS error 32 (file in use). This blocks server-only deployments entirely.Unnecessary dependency: Pure MCP servers should not require pywin32 at all, since Job Objects are only relevant for client-side subprocess management.
This blocks downstream projects like ha-mcp on Windows (tracked in homeassistant-ai/ha-mcp#672).
Solution
Wrap the pywin32 imports in
src/mcp/os/win32/utilities.pywithtry/except ImportError, falling back toNonewhen the package is not installed.All downstream functions that use these modules —
_create_job_object(),_maybe_assign_process_to_job(), andterminate_windows_process_tree()— already null-check them before use, so the graceful degradation path is fully covered with no behavior change for users who have pywin32 installed.Change
Fixes #2233