Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/mcp/os/win32/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions tests/client/test_stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading