modelcontextprotocol / modelcontextprotocol/python-sdk

Windows MCP SSH stdio transport closes after initialize when PROGRAMDATA is missing from inherited environment

未关闭
#3,335 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

v1 v2
主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

Description

On Windows, an MCP server launched through the stdio client can terminate shortly after the initialize message when the spawned process is given the SDK's restricted inherited environment.

During investigation of Windows MCP + SSH behavior, I found that PROGRAMDATA is missing from DEFAULT_INHERITED_ENV_VARS in src/mcp/client/stdio.py.

Environment
  • Windows 11
  • MCP Python SDK
  • MCP server launched through stdio
  • SSH-based MCP server
Observed behavior

The MCP SSH subprocess can close shortly after the initialize message with a transport-closed error.

The same SSH setup works when launched manually and behaves differently when the expected Windows environment is available.

Investigation

The stdio client constructs a restricted environment for spawned processes.

PROGRAMDATA was not included in the Windows inherited environment list:

DEFAULT_INHERITED_ENV_VARS = (
    [
        "APPDATA",
        "HOMEDRIVE",
        "HOMEPATH",
        "LOCALAPPDATA",
        "PATH",
        "PATHEXT",
        "PROCESSOR_ARCHITECTURE",
        "SYSTEMDRIVE",
        "SYSTEMROOT",
        "TEMP",
        "USERNAME",
        "USERPROFILE",
    ]
    if sys.platform == "win32"
    else ...
)

Adding PROGRAMDATA allows the spawned Windows process to receive this standard environment variable.

Proposed fix

Add:

"PROGRAMDATA",

to the Windows DEFAULT_INHERITED_ENV_VARS list in:

src/mcp/client/stdio.py

Testing

I reproduced the Windows MCP + SSH behavior and investigated the restricted subprocess environment.

After adding PROGRAMDATA:

  • Verified that PROGRAMDATA is inherited by the spawned process.
  • Tested the restricted environment with Popen.
  • Ran the stdio client tests successfully: 25 passed, 8 skipped.
  • git diff --check passes.

I have prepared PR #3334 with this change.

Related issue

This investigation was motivated by MCP SSH transport closing immediately after initialize on Windows.

I would appreciate maintainer confirmation that this is the appropriate fix and whether this issue can be assigned to me so PR #3334 can be reopened.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 src/mcp/client/stdio.py 开始,检查 Windows 的 DEFAULT_INHERITED_ENV_VARS 列表。运行 stdio 客户端测试,并验证生成的进程继承了 PROGRAMDATA;报告 25 个测试通过、8 个测试跳过且 git diff --check 干净,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
cli
Issue 类型
缺陷
难度
1/5
预计耗时
1 小时以内
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。