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

Aperta Adatta ai principianti
#3,878 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #3927 di @EnesKartal — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia da src/mcp/os/win32/utilities.py ed esamina get_windows_executable_command, poi controlla i test specifici di Windows per la risoluzione degli eseguibili. Rimuovi il controllo di .ps1 e i controlli ridondanti delle estensioni descritti nell’issue, e aggiungi test per il comportamento di fallback. Il lavoro è completato quando i test mostrano che .ps1 non viene restituito come comando direttamente eseguibile e che la ricerca esistente degli eseguibili Windows continua a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
25k
Fork
4.2k
Merge medio
10m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/python-sdk

Tutte le issue di modelcontextprotocol/python-sdk

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.