Windows: get_windows_executable_command() attempts invalid .ps1 probe and contains redundant extension loop

Đang mở Phù hợp với người mới
#3,878 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

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

  • #3927 của @EnesKartal — đã đóng, không merge

Đánh giá

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

Hướng nghiên cứu

Bắt đầu với src/mcp/os/win32/utilities.py và kiểm tra get_windows_executable_command, sau đó xem các bài kiểm thử dành riêng cho Windows về việc phân giải tệp thực thi. Xóa bước kiểm tra .ps1 và các bước kiểm tra phần mở rộng dư thừa được mô tả trong issue, đồng thời bổ sung kiểm thử cho hành vi dự phòng. Hoàn tất khi các bài kiểm thử cho thấy .ps1 không được trả về dưới dạng lệnh có thể thực thi trực tiếp và việc tra cứu tệp thực thi Windows hiện có vẫn hoạt động.

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

Mô tả

enhancement
Release Line

2.x (main branch)

Problem Description

In src/mcp/os/win32/utilities.py:64-81, get_windows_executable_command is defined as:

def get_windows_executable_command(command: str) -> str:
    """Resolves the command to a Windows executable path.

    Tries the bare name first, then the common script extensions (.cmd, .bat,
    .exe, .ps1).
    """
    try:
        if command_path := shutil.which(command):
            return command_path

        for ext in [".cmd", ".bat", ".exe", ".ps1"]:
            ext_version = f"{command}{ext}"
            if ext_path := shutil.which(ext_version):
                return ext_path

        return command
    except OSError:
        return command  # path probing failed (permissions, broken symlinks)

There are two issues with this fallback loop:

  1. .ps1 cannot be resolved by shutil.which and cannot be directly executed as a Win32 executable:

    • On Windows, shutil.which() requires file extensions to match an entry in the system PATHEXT environment variable. By default, PATHEXT contains .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC, but does not include .ps1. Therefore, shutil.which(f"{command}.ps1") never resolves a PowerShell script.
    • Furthermore, even if a path ending in .ps1 were returned, attempting to pass a .ps1 file directly to anyio.open_process() or subprocess.Popen without invoking powershell.exe -File fails with OSError: [WinError 193] %1 is not a valid Win32 application.
  2. Redundant fallback loop for .cmd, .bat, and .exe:

    • Because shutil.which(command) at line 71 already checks the current directory and PATH against all entries in PATHEXT (which already includes .cmd, .bat, and .exe), looping over [".cmd", ".bat", ".exe"] with f"{command}{ext}" is redundant dead code when the executable is on PATH.
Minimal Reproduction
import shutil
from mcp.os.win32.utilities import get_windows_executable_command

# Notice shutil.which("something.ps1") returns None because .ps1 is not in PATHEXT
print("PATHEXT:", shutil._get_pathext())  # .ps1 is absent by default on Windows
Expected Behavior
  • Do not attempt to treat .ps1 as a direct executable target via shutil.which() unless invoked via the PowerShell host, or remove .ps1 from the extension list.
  • Simplify executable probing to avoid redundant checks for extensions already covered by shutil.which's PATHEXT handling.
Environment
  • OS: Windows 10 / 11
  • Python: 3.11+
  • MCP Python SDK: 2.x (main branch)

I would be happy to submit a PR with test coverage if assigned.


Assisted by AI; reviewed and verified.

Ngôn ngữ chính
Python
Star
25k
Fork
4.2k
Merge trung bình
10 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

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.

Issue khác của modelcontextprotocol/python-sdk

Tất cả issue của modelcontextprotocol/python-sdk

Issue tương tự

Thêm issue về Python

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.