Skip to content

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

Open

Description

@shivamm-gupta

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.

Activity

  1. added
    enhancementRequest for a new feature that's not currently supported
    on Nov 19, 2026
  2. Shanmukh-l33t commented on Nov 19, 2026

    @Shanmukh-l33t

    I reproduced this and prepared a tested branch for it.

    Problem

    On Windows shutil.which only matches extensions listed in PATHEXT, and .ps1 is not in the default list (_WIN_DEFAULT_PATHEXT). So the f"{command}.ps1" probe in get_windows_executable_command can never resolve, and a returned .ps1 path could not be launched anyway: passing one to anyio.open_process or Popen raises OSError: [WinError 193].

    Branch

    fix/3878-windows-ps1-probe, commit 31e5684, on my fork. Changes:

    • src/mcp/os/win32/utilities.py: .ps1 removed from the fallback extension list, with a one line comment citing PATHEXT and WinError 193, and the docstring updated.
    • tests/issues/test_3878_windows_ps1_probe.py: new regression test that makes a .ps1 probe resolve and asserts the resolver still returns the bare command, plus three small tests for the existing bare name hit, extension hit and OSError fallback paths.

    I left the .cmd / .bat / .exe loop in place. Under the default PATHEXT those probes duplicate the bare name lookup, but if PATHEXT is set without one of the entries, shutil.which("<name>.<ext>") still matches that exact filename, so deleting the loop would change behavior in that configuration. If you consider that configuration out of scope, the loop can go in the same branch.

    Evidence

    RED on unmodified main, 1 failed, 3 passed:

    E       AssertionError: assert 'C:\\tools\\pyserver.ps1' == 'pyserver'
    tests/issues/test_3878_windows_ps1_probe.py:26: AssertionError
    Results (0.05s):
             1 failed
    

    GREEN with the branch, 4 passed in 0.02s. The related stdio tests (tests/client/test_stdio.py, tests/transports/stdio/test_posix.py) also pass, 41 tests.

    Coverage for the touched function is 100% of statements and branches. The file itself stays at 34% locally because the rest of it is Windows only code and src/mcp/os/win32/utilities.py is omitted from the coverage source in pyproject.toml, so fail_under = 100 is unaffected.

    Branch: Shanmukh-l33t/python-sdk@main...fix/3878-windows-ps1-probe

    If a maintainer assigns this issue, I am happy to open a PR from this branch.

    Implemented with AI assistance and reviewed by the account owner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementRequest for a new feature that's not currently supported

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions