MCP servers fail to start on Windows due to missing inherited environment variables
What version of Codex is running?
codex-cli 0.41.0
Which model were you using?
all models
What platform is your computer?
Microsoft Windows NT 10.0.26100.0 x64
What steps can reproduce the bug?
- On Windows, configure an MCP server that shells out to another CLI (e.g., Docker MCP Gateway in ~/.codex/config.toml with command = "docker.exe" and args ["mcp", "gateway", "run"]).
- Launch Codex CLI/TUI.
- Observe that the MCP server exits immediately. Docker reports the CLI plugin is missing; npm/npx-based servers such as npx @playwright/mcp@latest time out while a “request timed out” error is surfaced in Codex.
- Inspect the child process environment (or run the command manually with the same environment) and note that core variables like COMSPEC, SYSTEMROOT, PROGRAMDATA, LOCALAPPDATA, APPDATA, USERPROFILE, etc., are absent.
What is the expected behavior?
Codex should launch MCP servers with the full Windows shell environment (or at least the core variables listed above) so that CLI helpers, DNS resolution, and per-user caches work exactly as they do when the command is executed in PowerShell/CMD. Docker’s docker mcp … plugin and npx-based servers should start successfully without user-specific environment overrides.
What do you see instead?
Only PATH, PATHEXT, USERNAME, USERDOMAIN, USERPROFILE, TEMP, and TMP are preserved. Without COMSPEC the Docker CLI can’t find its plugin binary; without SYSTEMROOT DNS lookups fail (EAI_FAIL); without APPDATA npm/npx can’t locate their cache/config and exit with ENOENT. Codex reports these MCP servers as failed or timed out on start.
Additional information
A simple fix is to expand the Windows DEFAULT_ENV_VARS array in codex-rs/mcp-client/src/mcp_client.rs to include the missing core variables (COMSPEC, SYSTEMROOT, SYSTEMDRIVE, PROGRAMFILES, PROGRAMFILES(X86), PROGRAMDATA, LOCALAPPDATA, APPDATA, USERPROFILE, HOMEDRIVE, HOMEPATH, etc.) and add Windows-specific tests that assert create_env_for_mcp_server propagates each entry. I’ve prepared a branch (fix/windows-compat) that does this and verified it with the Docker gateway and npm-based MCP servers.
5 Comments
A temporary workaround for Windows users experiencing this issue (until the PR gets approved) is as follows:
Replace YOURUSERPROFILEGOESHERE with your actual Windows username before applying this config.
Note: This workaround works as long as MCP servers already run in CMD or PowerShell.
In addition to the base OS variables already covered by #4182, Windows environments that need to invoke native toolchains such as MSVC (for example, when Rust Analyzer spawns
rustcwith the MSVC target) require that a set of Visual Studio and Windows SDK related environment variables also be passed through.Without these,
cl.exe,link.exe, and the corresponding headers and libraries cannot be resolved, leading to failed builds inside Serena and downstream tools like Rust Analyzer.The minimum additional variables to consider including in the whitelist are:
PATH,INCLUDE,LIB,LIBPATHVSINSTALLDIR,VCINSTALLDIR,VCToolsInstallDir,VCToolsVersion,VCToolsRedistDir,VisualStudioVersion,VS170COMNTOOLS,DevEnvDir,VCIDEInstallDirWindowsSdkDir,WindowsSDKVersion,WindowsSDKLibVersion,WindowsSdkBinPath,WindowsSdkVerBinPath,WindowsLibPath,UCRTVersion,UniversalCRTSdkDir,ExtensionSdkDirFrameworkDir,FrameworkDir64,FrameworkVersion,FrameworkVersion64variables prefixed with
VSCMD_This list is derived from the environment produced by
vcvarsall.bat/Launch-VsDevShell.ps1, and reflects the minimum set required for consistent MSVC/Windows SDK builds.https://learn.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022
Adding these to the whitelist would make Codex → Serena → Rust Analyzer workflows viable without requiring ad-hoc workarounds.
Just chiming in as an external user to confirm the impact: on Windows, this prevents certain MCP servers (e.g., Docker or npm/npx-based) from starting as expected. An upstream fix would help a lot since per-user workarounds aren’t practical. Thanks @cpjet64 and the Codex team for looking into it!
@taichi I updated my PR to include that brilliant idea!
The missing env variables listed above are now preserved. If you see additional missing env variables, please open a new issue.