From 792cb628028e325aa67f62b1305c111e4c15a74d Mon Sep 17 00:00:00 2001 From: EnesKartal Date: Wed, 25 Nov 2026 22:03:49 +0300 Subject: [PATCH] fix(win32): remove .ps1 probe and redundant PATHEXT loop in get_windows_executable_command --- src/mcp/os/win32/utilities.py | 16 ++-------------- tests/client/test_stdio.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/mcp/os/win32/utilities.py b/src/mcp/os/win32/utilities.py index 321fda8a66..b1e747f89b 100644 --- a/src/mcp/os/win32/utilities.py +++ b/src/mcp/os/win32/utilities.py @@ -62,21 +62,9 @@ def rebind_std_handle_to_fd(fd: int) -> None: 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). - """ + """Resolve a command name to a Windows executable path via `shutil.which`.""" 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 + return shutil.which(command) or command except OSError: return command # path probing failed (permissions, broken symlinks) diff --git a/tests/client/test_stdio.py b/tests/client/test_stdio.py index b64eb01902..4efc2e269b 100644 --- a/tests/client/test_stdio.py +++ b/tests/client/test_stdio.py @@ -1456,3 +1456,26 @@ async def test_a_graceful_exit_with_a_surviving_child_leaks_no_pipe_fds( # prag # Subset, not equality: other machinery may close fds, but never open new # ones; a leaked pipe fd would show up as an extra entry. assert set(os.listdir("/proc/self/fd")) <= baseline + + +def test_get_windows_executable_command_probes_once_without_ps1(monkeypatch: pytest.MonkeyPatch) -> None: + """get_windows_executable_command delegates to shutil.which once and never probes .ps1.""" + from mcp.os.win32.utilities import get_windows_executable_command + + probed: list[str] = [] + + def fake_which(cmd: str) -> str | None: + probed.append(cmd) + return None + + monkeypatch.setattr("shutil.which", fake_which) + assert get_windows_executable_command("my-tool") == "my-tool" + assert probed == ["my-tool"] + + probed.clear() + + def fake_which_found(_cmd: str) -> str: + return r"C:\bin\my-tool.cmd" + + monkeypatch.setattr("shutil.which", fake_which_found) + assert get_windows_executable_command("my-tool") == r"C:\bin\my-tool.cmd"