ai icon indicating copy to clipboard operation
ai copied to clipboard

@ai-sdk/mcp: stdio transport overrides explicit env values (PATH, HOME, TEMP, USERPROFILE) with the parent's

Open ohad6k opened this issue 2 months ago • 8 comments

Description

Experimental_StdioMCPTransport({ command, args, env }) does not pass explicit env values through for any key on its inherited-defaults list. On Windows that list is APPDATA, HOMEDRIVE, HOMEPATH, LOCALAPPDATA, PATH, PROCESSOR_ARCHITECTURE, SYSTEMDRIVE, SYSTEMROOT, TEMP, USERNAME, USERPROFILE. On POSIX it is HOME, LOGNAME, PATH, SHELL, TERM, USER. The parent process's values replace them, so a stdio MCP server cannot be given a sandbox home, a private temp dir or a pinned PATH.

get-environment.ts copies customEnv first and then writes the inherited defaults over it:

const env = customEnv ? { ...customEnv } : {};
for (const key of DEFAULT_INHERITED_ENV_VARS) { /* ... */ env[key] = value; }

This contradicts:

  • the reference docs, where env is "The environment variables to set for the MCP server";
  • the official MCP TypeScript SDK client, which merges { ...getDefaultEnvironment(), ...env } (modelcontextprotocol/typescript-sdk#216), so exact-case explicit values win there (it does share the Windows Path casing gap below);
  • the mock in create-child-process.test.ts, which returns { ...DEFAULT_ENV, ...customEnv }.

Expected: explicit env values win over inherited defaults.

Actual, on Windows 11 with Node 24.12.0, for the same server and the same env:

@ai-sdk/mcp 2.0.57   OVERRIDDEN PATH / USERPROFILE / TEMP, ok REPRO_FLAG
@ai-sdk/mcp 1.0.85   same
@ai-sdk/mcp 0.0.35   same
@modelcontextprotocol/sdk 1.30.1   all ok

On Windows only, an explicit Path (the native casing, which is what { ...process.env } yields) also loses: the result contains both Path and the inherited PATH, and the child receives the inherited value. The official SDK 1.30.1 client shows the same result for this case, so parity with it covers exact-case keys only.

Reproduction

A runnable folder with pinned versions: an MCP server whose one tool returns its env, driven by the three @ai-sdk/mcp lines and the official SDK. Its exit code is 1 when a value is overridden. https://gist.github.com/ohad6k/6751f344f3d2d3703b1e289e3b54e4eb

A failing unit test for this, in packages/mcp/src/tool/mcp-stdio/get-environment.test.ts, is ready and will be in the PR that follows this issue:

× should let an explicit custom value override an inherited default
  AssertionError: expected 'parent-path' to be 'custom-path'

AI SDK Version

  • @ai-sdk/mcp: 2.0.57 (also 1.0.85 and 0.0.35)
  • Node: 24.12.0, Windows 11 x64

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

ohad6k avatar Sep 24 '26 14:09 ohad6k

Identify issue type

Identified as Bug
Confidence: High

Rationale

This is a concrete, reproducible failure in existing stdio MCP transport behavior: caller-provided environment values do not take precedence as documented and expected.

Evidence

  • Explicit env values such as PATH, HOME, TEMP, and USERPROFILE are overwritten by inherited parent-process values.
  • The behavior contradicts the documented purpose of the env option and the merge behavior represented in the project's test mock.
  • The report includes a runnable reproduction, affected versions, and a failing unit test showing parent-path instead of custom-path.

ai-sdk-factory[bot] avatar Sep 24 '26 14:09 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 7

Bug was reproduced.

Branch: main PR: successful reproduction pull request

Details

  • examples/ai-functions/src/reproduction/mcp-stdio-explicit-env-overridden.ts reproduces the user-visible failure through the public stdio transport API: the child received the parent's HOME and PATH instead of the explicit values, while the unrelated REPRO_FLAG was preserved.
  • Expected behavior is for explicit env values to reach the MCP child, allowing sandbox homes, private temporary directories, and pinned executable paths.
  • packages/mcp/src/tool/mcp-stdio/get-environment.ts copies custom values first and then overwrites inherited-default keys from process.env; packages/mcp/package.json is version 2.0.57, matching the reported version.
  • The reproduction includes the Windows exact-case and native Path scenarios, but the Windows-only behavior could not be executed in the POSIX sandbox.

Relevant Documentation

  • https://ai-sdk.dev/docs/reference/ai-sdk-core/mcp-stdio-transport
  • https://github.com/modelcontextprotocol/typescript-sdk/blob/1.30.1/src/client/stdio.ts
  • https://ts.sdk.modelcontextprotocol.io/v2/api/%40modelcontextprotocol/client/client/stdio.html

ai-sdk-factory[bot] avatar Sep 24 '26 14:09 ai-sdk-factory[bot]

Bugfix - AI SDK 7

Created bugfix and opened PR: #21435

Root Cause

getEnvironment copied custom values before unconditionally assigning inherited defaults from process.env; the original public-API reproduction confirmed those later assignments reached the child instead of explicit values.

Summary

Inherited defaults are now skipped when a matching custom key exists, including case-insensitive key matching on Windows, while unrelated defaults remain inherited. Added a patch changeset for @ai-sdk/mcp.

Testing

Added regression tests for explicit inherited-key overrides and Windows Path casing while retaining the existing immutability coverage.

ai-sdk-factory[bot] avatar Sep 24 '26 14:09 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 5

Bug was reproduced.

Branch: release-v5.0 PR: successful reproduction pull request

Details

  • examples/ai-functions/src/reproduction/mcp-stdio-explicit-env-overridden.ts reproduces the public stdio transport failure: the child received parent values for POSIX HOME, LOGNAME, PATH, SHELL, TERM, and USER, while the unrelated explicit REPRO_FLAG remained intact.
  • Expected behavior is for every explicit env value to reach the MCP child, enabling sandbox homes, private temporary directories, and pinned executable paths.
  • packages/mcp/src/tool/mcp-stdio/get-environment.ts copies custom values first and then overwrites inherited-default keys from process.env; packages/mcp/package.json identifies the target-branch version as affected version 0.0.35.
  • examples/ai-functions/package.json provides the workspace context required by the replay command.
  • The reproduction includes assertions for Windows inherited keys and native Path casing, but those Windows-only cases could not be executed in the Linux sandbox.

Relevant Documentation

  • https://ai-sdk.dev/docs/reference/ai-sdk-core/mcp-stdio-transport
  • https://github.com/modelcontextprotocol/typescript-sdk/blob/1.30.1/src/client/stdio.ts
  • https://ts.sdk.modelcontextprotocol.io/v2/api/%40modelcontextprotocol/client/client/stdio.html

ai-sdk-factory[bot] avatar Sep 24 '26 18:09 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 6

Bug was reproduced.

Branch: release-v6.0 PR: successful reproduction pull request

Details

  • packages/mcp/package.json identifies the target package as @ai-sdk/mcp 1.0.85, exactly matching one reported affected version.
  • packages/mcp/src/tool/mcp-stdio/get-environment.ts copies custom values before replacing inherited-default keys with parent-process values.
  • examples/ai-functions/src/reproduction/issue-21430-mcp-stdio-explicit-env.ts reproduces the user-visible failure through the public stdio transport API: the child received parent values for all six POSIX inherited keys instead of the explicit values, while unrelated REPRO_FLAG remained intact.
  • Expected behavior is for explicit environment values to reach the child, enabling sandbox homes, private temporary directories, and pinned executable paths.
  • The artifact contains Windows uppercase-key and native Path scenarios, but those Windows-only assertions could not be executed in the Linux sandbox.

Relevant Documentation

  • https://ai-sdk.dev/docs/reference/ai-sdk-core/mcp-stdio-transport
  • https://github.com/modelcontextprotocol/typescript-sdk/blob/1.30.1/src/client/stdio.ts
  • https://ts.sdk.modelcontextprotocol.io/v2/api/%40modelcontextprotocol/client/client/stdio.html

ai-sdk-factory[bot] avatar Sep 24 '26 18:09 ai-sdk-factory[bot]

:rocket: Published in:

Package Version
ai 7.0.114 github npm
@ai-sdk/amazon-bedrock 5.0.94 github npm
@ai-sdk/angular 3.0.114 github npm
@ai-sdk/anthropic 4.0.63 github npm
@ai-sdk/anthropic-aws 2.0.55 github npm
@ai-sdk/azure 4.0.79 github npm
@ai-sdk/code-mode 1.0.71 github npm
@ai-sdk/gateway 4.0.92 github npm
@ai-sdk/google 4.0.80 github npm
@ai-sdk/google-vertex 5.0.93 github npm
@ai-sdk/harness 1.0.124 github npm
@ai-sdk/harness-acp 1.0.62 github npm
@ai-sdk/harness-claude-code 1.0.128 github npm
@ai-sdk/harness-cline 1.0.51 github npm
@ai-sdk/harness-codex 1.0.126 github npm
@ai-sdk/harness-cursor 1.0.37 github npm
@ai-sdk/harness-deepagents 1.0.124 github npm
@ai-sdk/harness-fx 1.0.37 github npm
@ai-sdk/harness-github-copilot 1.0.19 github npm
@ai-sdk/harness-grok-build 1.0.61 github npm
@ai-sdk/harness-opencode 1.0.126 github npm
@ai-sdk/harness-pi 1.0.126 github npm
@ai-sdk/langchain 3.0.114 github npm
@ai-sdk/llamaindex 3.0.114 github npm
@ai-sdk/mcp 2.0.58 github npm
@ai-sdk/minimax 3.0.40 github npm
@ai-sdk/open-responses 2.0.52 github npm
@ai-sdk/openai 4.0.75 github npm
@ai-sdk/otel 1.0.114 github npm
@ai-sdk/policy-opa 1.0.114 github npm
@ai-sdk/quiverai 2.0.48 github npm
@ai-sdk/react 4.0.117 github npm
@ai-sdk/rsc 3.0.114 github npm
@ai-sdk/sandbox-just-bash 1.0.124 github npm
@ai-sdk/sandbox-vercel 1.0.124 github npm
@ai-sdk/svelte 5.0.114 github npm
@ai-sdk/tui 1.0.115 github npm
@ai-sdk/vue 4.0.114 github npm
@ai-sdk/workflow 2.0.45 github npm
@ai-sdk/workflow-harness 1.0.124 github npm
@ai-sdk/xai 5.0.8 github npm

github-actions[bot] avatar Sep 24 '26 18:09 github-actions[bot]

Backport to v6.0

created: #21465 (clean).

ai-sdk-factory[bot] avatar Sep 24 '26 18:09 ai-sdk-factory[bot]

Backport to v5.0

created: #21470 (clean).

ai-sdk-factory[bot] avatar Sep 24 '26 19:09 ai-sdk-factory[bot]