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

Offen Anfängerfreundlich
#3,878 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

  • #3927 von @EnesKartal — ohne Merge geschlossen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
76/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python
Bereich
backend

Rechercherichtung

Beginne mit src/mcp/os/win32/utilities.py und untersuche get_windows_executable_command. Prüfe anschließend die Windows-spezifischen Tests zur Auflösung ausführbarer Dateien. Entferne die .ps1-Prüfung und die im Issue beschriebenen redundanten Erweiterungsprüfungen und ergänze Tests für das Fallback-Verhalten. Fertig ist die Änderung, wenn die Tests zeigen, dass .ps1 nicht als direkt ausführbarer Befehl zurückgegeben wird und die bestehende Suche nach Windows-Programmen weiterhin funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Python
Sterne
25k
Forks
4.2k
Ø Merge
10 Min.
Gemergte PRs (30 T.)
1

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/python-sdk

Alle Issues in modelcontextprotocol/python-sdk

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.