modelcontextprotocol / modelcontextprotocol/python-sdk

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

Đang mở
#3,335 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

v1 v2
Ngôn ngữ chính
Python
Star
24.3k
Fork
4k
Merge trung bình
1 ngày 1 giờ
Pull request đã merge (30 ngày)
31

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong src/mcp/client/stdio.py và kiểm tra danh sách DEFAULT_INHERITED_ENV_VARS của Windows. Chạy các bài kiểm thử của client stdio và xác minh rằng tiến trình được khởi chạy kế thừa PROGRAMDATA; việc hoàn tất được biểu thị bằng 25 bài kiểm thử đạt và 8 bài bị bỏ qua được báo cáo, cùng với git diff --check sạch.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
cli
Loại issue
Lỗi
Độ khó
1/5
Thời gian dự kiến
Dưới một giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.