diff --git a/.changeset/fix-default-inherited-env-vars-windows.md b/.changeset/fix-default-inherited-env-vars-windows.md new file mode 100644 index 0000000000..0f398e2e36 --- /dev/null +++ b/.changeset/fix-default-inherited-env-vars-windows.md @@ -0,0 +1,8 @@ +--- +'@modelcontextprotocol/sdk': patch +--- + +On Windows, stdio servers spawned by `StdioClientTransport` now also inherit `COMSPEC`, `PATHEXT`, `PROGRAMDATA`, `PROGRAMFILES(X86)`, `PROGRAMW6432`, and `WINDIR` (added to `DEFAULT_INHERITED_ENV_VARS`). Programs a server launches can depend on them: PowerShell finds no native +executables without `PATHEXT`, and Windows OpenSSH exits 255 without `ProgramData`. + +This is a backport of the same fix already merged on `main` in #2043, and resolves #2037 for the v1 line. diff --git a/src/client/stdio.ts b/src/client/stdio.ts index 816a686fce..b30578c4a4 100644 --- a/src/client/stdio.ts +++ b/src/client/stdio.ts @@ -54,17 +54,23 @@ export const DEFAULT_INHERITED_ENV_VARS = process.platform === 'win32' ? [ 'APPDATA', + 'COMSPEC', 'HOMEDRIVE', 'HOMEPATH', 'LOCALAPPDATA', 'PATH', + 'PATHEXT', 'PROCESSOR_ARCHITECTURE', + 'PROGRAMDATA', + 'PROGRAMFILES', + 'PROGRAMFILES(X86)', + 'PROGRAMW6432', 'SYSTEMDRIVE', 'SYSTEMROOT', 'TEMP', 'USERNAME', 'USERPROFILE', - 'PROGRAMFILES' + 'WINDIR' ] : /* list inspired by the default env inheritance of sudo */ ['HOME', 'LOGNAME', 'PATH', 'SHELL', 'TERM', 'USER']; diff --git a/test/client/stdio.test.ts b/test/client/stdio.test.ts index 577e0c946f..f7ff8ab3ff 100644 --- a/test/client/stdio.test.ts +++ b/test/client/stdio.test.ts @@ -116,3 +116,36 @@ test('should fire onerror and close when ReadBuffer overflows', async () => { expect(error.message).toMatch(/ReadBuffer exceeded maximum size/); await closed; }); + +test('should inherit the Windows variables that child tools need by default', async () => { + const originalPlatform = process.platform; + // DEFAULT_INHERITED_ENV_VARS is computed at import time, so load a fresh copy of the module as if on Windows. + Object.defineProperty(process, 'platform', { value: 'win32' }); + try { + vi.resetModules(); + const { DEFAULT_INHERITED_ENV_VARS } = await import('../../src/client/stdio.js'); + expect(DEFAULT_INHERITED_ENV_VARS).toEqual([ + 'APPDATA', + 'COMSPEC', + 'HOMEDRIVE', + 'HOMEPATH', + 'LOCALAPPDATA', + 'PATH', + 'PATHEXT', + 'PROCESSOR_ARCHITECTURE', + 'PROGRAMDATA', + 'PROGRAMFILES', + 'PROGRAMFILES(X86)', + 'PROGRAMW6432', + 'SYSTEMDRIVE', + 'SYSTEMROOT', + 'TEMP', + 'USERNAME', + 'USERPROFILE', + 'WINDIR' + ]); + } finally { + Object.defineProperty(process, 'platform', { value: originalPlatform }); + vi.resetModules(); + } +});