Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(mcp): resolve a bare python3 to a real interpreter on Windows

FenjuFu6961deb5ddb9
diff --git a/apps/desktop/electron/main/mcp-stdio-launch.ts b/apps/desktop/electron/main/mcp-stdio-launch.ts
index c0d0a863a..42f6c6665 100644
--- a/apps/desktop/electron/main/mcp-stdio-launch.ts
+++ b/apps/desktop/electron/main/mcp-stdio-launch.ts
@@ -54,6 +54,7 @@ const NODE_LAUNCHERS: Record<string, true> = {
yarn: true,
};
const UV_LAUNCHERS: Record<string, true> = { uv: true, uvx: true };
+const PYTHON_LAUNCHERS: Record<string, true> = { python: true, python3: true };
export type McpLaunchFs = {
isFile: (path: string) => boolean;
@@ -293,6 +294,54 @@ export function discoverUvExecutable(
);
}
+/**
+ * `%LOCALAPPDATA%\Microsoft\WindowsApps` holds the Microsoft Store app
+ * execution aliases. Without a Store Python, its `python.exe`/`python3.exe`
+ * only print an install hint and exit 9009, yet the directory is on PATH by
+ * default — usually ahead of a python.org or winget install.
+ */
+function isWindowsAppsDir(dir: string, paths: nodePath.PlatformPath): boolean {
+ return /[\\/]microsoft[\\/]windowsapps$/i.test(paths.normalize(dir).replace(/[\\/]+$/, ""));
+}
+
+/**
+ * Find a real Python 3 for a `python3`/`python` launcher on Windows.
+ *
+ * python.org and winget installs provide `python.exe` (and the `py` launcher)
+ * but no `python3.exe`, so a manifest that says `python3` lands on the Store
+ * alias. Look for either name outside WindowsApps first, then `py -3`. When
+ * nothing else exists the caller keeps the alias: with a Store Python it is
+ * the real interpreter.
+ */
+export function discoverWindowsPython(
+ options: Pick<McpStdioLaunchOptions, "hostEnv" | "fs">,
+): { command: string; args: string[] } | undefined {
+ const hostEnv = options.hostEnv ?? process.env;
+ const fs = options.fs ?? defaultFs();
+ const paths = nodePath.win32;
+ const exts = pathextList(hostEnv, true);
+ const dirs = hostPath(hostEnv, true)
+ .split(paths.delimiter)
+ .filter((dir) => dir && !isWindowsAppsDir(dir, paths))
+ .join(paths.delimiter);
+ for (const name of ["python3", "python"]) {
+ const found = lookOnPath(name, dirs, paths, exts, fs.isFile);
+ if (found) return { command: found, args: [] };
+ }
+ const windir = hostValue(hostEnv, "SystemRoot", true) ?? hostValue(hostEnv, "WINDIR", true);
+ const localApp = hostValue(hostEnv, "LOCALAPPDATA", true);
+ const py =
+ lookOnPath("py", dirs, paths, exts, fs.isFile) ??
+ firstFile(
+ [
+ ...(windir ? [paths.join(windir, "py.exe")] : []),
+ ...(localApp ? [paths.join(localApp, "Programs", "Python", "Launcher", "py.exe")] : []),
+ ],
+ fs.isFile,
+ );
+ return py ? { command: py, args: ["-3"] } : undefined;
+}
+
function siblingTool(
executable: string,
name: string,
@@ -416,8 +465,10 @@ function withToolPath(
/**
* Turn a configured stdio command into something `spawn({ shell: false })`
* can actually start: resolve `npx`/`uvx` onto real binaries (fnm, uv,
- * nvm-windows, PATH), rewrite npm shims to `node` + `npx-cli.js`, and wrap
- * remaining Windows `.cmd` files with `cmd.exe` so arguments stay literal.
+ * nvm-windows, PATH), rewrite npm shims to `node` + `npx-cli.js`, map a bare
+ * `python3`/`python` on Windows past the Store alias to a real interpreter,
+ * and wrap remaining Windows `.cmd` files with `cmd.exe` so arguments stay
+ * literal.
*/
export function resolveMcpStdioLaunch(options: McpStdioLaunchOptions): McpStdioLaunch {
const platform = options.platform ?? process.platform;
@@ -482,6 +533,16 @@ export function resolveMcpStdioLaunch(options: McpStdioLaunchOptions): McpStdioL
} else if (uv) {
launchCommand = uv;
}
+ } else if (win32 && PYTHON_LAUNCHERS[name] && !/[\\/]/.test(command)) {
+ const python = discoverWindowsPython({ hostEnv, fs });
+ if (python) {
+ launchCommand = python.command;
+ launchArgs = [...python.args, ...args];
+ env = withToolPath(env, python.command, paths, win32);
+ } else {
+ const found = lookOnPath(name, hostPath(hostEnv, win32), paths, pathextList(hostEnv, win32), fs.isFile);
+ if (found) launchCommand = found;
+ }
} else if (paths.isAbsolute(command) && fs.isFile(command)) {
env = withToolPath(env, command, paths, win32);
} else {
diff --git a/apps/desktop/test/mcp-stdio-launch.test.mjs b/apps/desktop/test/mcp-stdio-launch.test.mjs
index d03622203..ce974fc18 100644
--- a/apps/desktop/test/mcp-stdio-launch.test.mjs
+++ b/apps/desktop/test/mcp-stdio-launch.test.mjs
@@ -299,3 +299,67 @@ test("wrapCmdShim outer-quotes survive when .cmd path contains spaces", () => {
assert.match(inner, /"pkg&whoami"/, "metachar arg stays quoted");
assert.match(inner, /"say ""hi"""/, "embedded quotes doubled");
});
+
+// A python.org / winget Python on Windows is `python.exe`; the only
+// `python3.exe` on PATH is the Microsoft Store alias, which exits 9009.
+const winUser = win.join("C:", "Users", "zhao");
+const windowsApps = win.join(winUser, "AppData", "Local", "Microsoft", "WindowsApps");
+const pythonOrg = win.join(winUser, "AppData", "Local", "Programs", "Python", "Python311");
+
+function pythonLaunch(files, pathDirs, extraEnv = {}) {
+ const hostEnv = { PATH: pathDirs.join(";"), ...extraEnv };
+ return resolveMcpStdioLaunch({
+ command: "python3",
+ args: ["-m", "ictrp_mcp.server"],
+ env: { PATH: hostEnv.PATH },
+ platform: "win32",
+ home: winUser,
+ hostEnv,
+ fs: winFs(files),
+ });
+}
+
+test("Windows python3 skips the Store alias for a real python.exe", () => {
+ const real = win.join(pythonOrg, "python.exe");
+ const launch = pythonLaunch(
+ [win.join(windowsApps, "python3.exe"), win.join(windowsApps, "python.exe"), real],
+ [windowsApps, pythonOrg],
+ );
+ assert.equal(launch.command, real);
+ assert.deepEqual(launch.args, ["-m", "ictrp_mcp.server"]);
+});
+
+test("Windows python3 falls back to the py launcher with -3", () => {
+ const systemRoot = win.join("C:", "Windows");
+ const py = win.join(systemRoot, "py.exe");
+ const launch = pythonLaunch(
+ [win.join(windowsApps, "python3.exe"), py],
+ [windowsApps, win.join(systemRoot, "System32")],
+ { SystemRoot: systemRoot },
+ );
+ assert.equal(launch.command, py);
+ assert.deepEqual(launch.args, ["-3", "-m", "ictrp_mcp.server"]);
+});
+
+test("Windows python3 keeps the WindowsApps entry when it is the only Python", () => {
+ // With a Store Python the alias is the real interpreter; never drop it.
+ const alias = win.join(windowsApps, "python3.exe");
+ const launch = pythonLaunch([alias], [windowsApps]);
+ assert.equal(launch.command, alias);
+ assert.deepEqual(launch.args, ["-m", "ictrp_mcp.server"]);
+});
+
+test("python3 outside Windows still resolves through PATH unchanged", () => {
+ const posix = path.posix;
+ const launch = resolveMcpStdioLaunch({
+ command: "python3",
+ args: ["-m", "ictrp_mcp.server"],
+ env: { PATH: "/usr/bin" },
+ platform: "darwin",
+ home: "/Users/zhao",
+ hostEnv: { PATH: "/opt/homebrew/bin:/usr/bin" },
+ fs: { isFile: (p) => p === posix.join("/usr/bin", "python3"), realpath: (p) => p },
+ });
+ assert.equal(launch.command, "/usr/bin/python3");
+ assert.deepEqual(launch.args, ["-m", "ictrp_mcp.server"]);
+});
diff --git a/docs/adr/0038-plugin-mcp-bridge.md b/docs/adr/0038-plugin-mcp-bridge.md
index 9ef9f0dde..5dd41b0e3 100644
--- a/docs/adr/0038-plugin-mcp-bridge.md
+++ b/docs/adr/0038-plugin-mcp-bridge.md
@@ -59,8 +59,11 @@ differently.
(D600). Bare `npx`/`uvx` resolve to real binaries: official Node
(`node.exe` + `npx-cli.js`) first, then fnm/nvm/Volta, then a Windows
`.cmd` shim through `cmd.exe /d /s /c` with quoted literal args (D624,
- issue #789). `command` must be a bare PATH name or stay inside the plugin
- directory; `url` may use `http` or `https`, with non-loopback HTTP subject
+ issue #789). On Windows a bare `python3`/`python` skips the Microsoft
+ Store alias in `WindowsApps` (it exits 9009 when no Store Python is
+ installed) for a `python3`/`python` elsewhere on PATH, then `py -3`, and
+ keeps the alias only when nothing else exists. `command` must be a bare
+ PATH name or stay inside the plugin directory; `url` may use `http` or `https`, with non-loopback HTTP subject
to ADR 0142 and the plugin network allowlist.
## Consequences
diff --git a/docs/plugin-development.md b/docs/plugin-development.md
index 8112a22e8..bcab60c41 100644
--- a/docs/plugin-development.md
+++ b/docs/plugin-development.md
@@ -681,8 +681,9 @@ remote server requires `mcp.server.remote`:
A stdio command must be a bare command found on `PATH` or a plugin-relative
executable; absolute paths are rejected. `npx` and `uvx` are resolved to the
real Node.js / uv binaries (PATH, official Node, fnm, nvm, Volta, default uv
-install). Remote URLs may use HTTP or HTTPS, and the host must be listed in
-`net.domains`; non-loopback HTTP is unencrypted, so use it only on a trusted
+install). On Windows, `python3` and `python` skip the Microsoft Store alias for
+a real interpreter on `PATH`, falling back to `py -3`. Remote URLs may use
+HTTP or HTTPS, and the host must be listed in `net.domains`; non-loopback HTTP is unencrypted, so use it only on a trusted
network. Setting references read only this plugin's settings—the host
environment and provider secrets are never forwarded. MCP tools follow the
same Agent-only policy and namespacing as hand-written plugin tools.