Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(mcp): the shell scope allowed no program at all, so no server could spawn

purplec695b7a6d79a
diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json
index 7b75392c..0445159f 100644
--- a/src-tauri/capabilities/default.json
+++ b/src-tauri/capabilities/default.json
@@ -14,7 +14,33 @@
"core:window:allow-toggle-maximize",
"core:window:allow-start-dragging",
"core:webview:default",
- "shell:allow-spawn",
+ {
+ "identifier": "shell:allow-spawn",
+ "allow": [
+ { "name": "npx", "cmd": "npx", "args": true },
+ { "name": "npx.cmd", "cmd": "npx.cmd", "args": true },
+ { "name": "npm", "cmd": "npm", "args": true },
+ { "name": "npm.cmd", "cmd": "npm.cmd", "args": true },
+ { "name": "pnpm", "cmd": "pnpm", "args": true },
+ { "name": "pnpm.cmd", "cmd": "pnpm.cmd", "args": true },
+ { "name": "yarn", "cmd": "yarn", "args": true },
+ { "name": "yarn.cmd", "cmd": "yarn.cmd", "args": true },
+ { "name": "bun", "cmd": "bun", "args": true },
+ { "name": "bun.cmd", "cmd": "bun.cmd", "args": true },
+ { "name": "bunx", "cmd": "bunx", "args": true },
+ { "name": "bunx.cmd", "cmd": "bunx.cmd", "args": true },
+ { "name": "node", "cmd": "node", "args": true },
+ { "name": "node.cmd", "cmd": "node.cmd", "args": true },
+ { "name": "deno", "cmd": "deno", "args": true },
+ { "name": "deno.cmd", "cmd": "deno.cmd", "args": true },
+ { "name": "uv", "cmd": "uv", "args": true },
+ { "name": "uvx", "cmd": "uvx", "args": true },
+ { "name": "python", "cmd": "python", "args": true },
+ { "name": "python3", "cmd": "python3", "args": true },
+ { "name": "py", "cmd": "py", "args": true },
+ { "name": "docker", "cmd": "docker", "args": true }
+ ]
+ },
"shell:allow-stdin-write",
"shell:allow-kill",
"shell:allow-open",
diff --git a/src/api/mcp/__tests__/tool-registry-external.test.ts b/src/api/mcp/__tests__/tool-registry-external.test.ts
index ea989033..22b6b5ee 100644
--- a/src/api/mcp/__tests__/tool-registry-external.test.ts
+++ b/src/api/mcp/__tests__/tool-registry-external.test.ts
@@ -1,6 +1,6 @@
import { describe, it, expect, vi } from 'vitest'
import { ToolRegistry } from '../tool-registry'
-import { resolveCommandForPlatform } from '../external-client'
+import { commandCandidatesForPlatform } from '../external-client'
import type { MCPToolDefinition, PermissionMap } from '../types'
const fullPerms: PermissionMap = {
@@ -130,35 +130,43 @@ describe('ToolRegistry — getPermissionLevelWithOverrides (Phase 12)', () => {
})
})
-describe('external-client — resolveCommandForPlatform', () => {
- it('appends .cmd for known Node-based commands on Windows', () => {
- expect(resolveCommandForPlatform('npx', 'Win32')).toBe('npx.cmd')
- expect(resolveCommandForPlatform('npm', 'Win32')).toBe('npm.cmd')
- expect(resolveCommandForPlatform('pnpm', 'Win32')).toBe('pnpm.cmd')
- expect(resolveCommandForPlatform('yarn', 'Win32')).toBe('yarn.cmd')
- expect(resolveCommandForPlatform('bun', 'Win32')).toBe('bun.cmd')
- expect(resolveCommandForPlatform('node', 'Win32')).toBe('node.cmd')
- expect(resolveCommandForPlatform('deno', 'Win32')).toBe('deno.cmd')
+describe('external-client — commandCandidatesForPlatform', () => {
+ it('tries the .cmd shim first for npm-family launchers on Windows', () => {
+ expect(commandCandidatesForPlatform('npx', 'Win32')).toEqual(['npx.cmd', 'npx'])
+ expect(commandCandidatesForPlatform('npm', 'Win32')).toEqual(['npm.cmd', 'npm'])
+ expect(commandCandidatesForPlatform('pnpm', 'Win32')).toEqual(['pnpm.cmd', 'pnpm'])
+ expect(commandCandidatesForPlatform('yarn', 'Win32')).toEqual(['yarn.cmd', 'yarn'])
})
- it('leaves commands with extensions unchanged on Windows', () => {
- expect(resolveCommandForPlatform('npx.cmd', 'Win32')).toBe('npx.cmd')
- expect(resolveCommandForPlatform('mcp-server.exe', 'Win32')).toBe('mcp-server.exe')
+ it('tries the bare name first for exe-shipping runtimes on Windows', () => {
+ // node/deno/native bun install as .exe; node.cmd does not even exist in a
+ // standard Node install, so .cmd-first would break them outright.
+ expect(commandCandidatesForPlatform('node', 'Win32')).toEqual(['node', 'node.cmd'])
+ expect(commandCandidatesForPlatform('deno', 'Win32')).toEqual(['deno', 'deno.cmd'])
+ expect(commandCandidatesForPlatform('bun', 'Win32')).toEqual(['bun', 'bun.cmd'])
})
- it('leaves absolute / path-like commands unchanged on Windows', () => {
- expect(resolveCommandForPlatform('C:\\Program Files\\node\\node.exe', 'Win32')).toBe(
- 'C:\\Program Files\\node\\node.exe'
- )
- expect(resolveCommandForPlatform('./bin/server', 'Win32')).toBe('./bin/server')
+ it('returns commands with extensions as the only candidate on Windows', () => {
+ expect(commandCandidatesForPlatform('npx.cmd', 'Win32')).toEqual(['npx.cmd'])
+ expect(commandCandidatesForPlatform('mcp-server.exe', 'Win32')).toEqual(['mcp-server.exe'])
+ })
+
+ it('returns absolute / path-like commands as the only candidate on Windows', () => {
+ expect(commandCandidatesForPlatform('C:\\Program Files\\node\\node.exe', 'Win32')).toEqual([
+ 'C:\\Program Files\\node\\node.exe',
+ ])
+ expect(commandCandidatesForPlatform('./bin/server', 'Win32')).toEqual(['./bin/server'])
})
- it('leaves unknown commands unchanged on Windows', () => {
- expect(resolveCommandForPlatform('some-custom-mcp', 'Win32')).toBe('some-custom-mcp')
+ it('offers the .cmd fallback for unknown commands on Windows', () => {
+ expect(commandCandidatesForPlatform('some-custom-mcp', 'Win32')).toEqual([
+ 'some-custom-mcp',
+ 'some-custom-mcp.cmd',
+ ])
})
- it('returns command unchanged on non-Windows platforms', () => {
- expect(resolveCommandForPlatform('npx', 'MacIntel')).toBe('npx')
- expect(resolveCommandForPlatform('npx', 'Linux x86_64')).toBe('npx')
+ it('returns the command alone on non-Windows platforms', () => {
+ expect(commandCandidatesForPlatform('npx', 'MacIntel')).toEqual(['npx'])
+ expect(commandCandidatesForPlatform('npx', 'Linux x86_64')).toEqual(['npx'])
})
})
diff --git a/src/api/mcp/external-client.ts b/src/api/mcp/external-client.ts
index 2a44f75a..f0a66c2d 100644
--- a/src/api/mcp/external-client.ts
+++ b/src/api/mcp/external-client.ts
@@ -24,14 +24,13 @@ interface JsonRpcResponse {
}
export class MCPExternalClient {
- /** The Command — owns the stdout/stderr/close events. */
- private command: any = null
/**
- * The Child that spawn() hands back — owns stdin and kill(). These live on
- * two different objects in @tauri-apps/plugin-shell, and keeping only the
- * Command meant `stdin.write` hit undefined on the very first request: every
- * external server failed to connect with "Cannot read properties of
- * undefined (reading 'write')".
+ * The Child that spawn() hands back — owns stdin and kill(). The Command
+ * owns the stdout/stderr/close events instead; these live on two different
+ * objects in @tauri-apps/plugin-shell, and keeping only the Command meant
+ * `stdin.write` hit undefined on the very first request: every external
+ * server failed to connect with "Cannot read properties of undefined
+ * (reading 'write')".
*/
private child: any = null
private requestId = 0
@@ -53,34 +52,44 @@ export class MCPExternalClient {
const shellModule = await import('@tauri-apps/plugin-shell')
const { Command } = shellModule
- // Windows gotcha: Node-based MCP servers are invoked as `npx`, `npm`,
- // `node` — but on Windows the resolvable PATH entries for those are
- // `npx.cmd`, `npm.cmd`, etc. Command.create tries to spawn the bare
- // name which fails with "program not found". Map known command names
- // to their .cmd shim here.
- const resolvedCommand = resolveCommandForPlatform(this.config.command)
-
- this.command = Command.create(resolvedCommand, this.config.args, {
- env: this.config.env,
- })
+ // Windows gotcha: npm-family launchers exist only as `.cmd` shims
+ // (npx.cmd) while node/deno/bun ship a real .exe, and tools like pnpm
+ // exist as either depending on how they were installed. Rust's spawn
+ // finds .exe from the bare name but never resolves .cmd, so neither a
+ // blanket rewrite nor the bare name alone works for every install.
+ // Try the likelier candidate first and fall back to the other.
+ const candidates = commandCandidatesForPlatform(this.config.command)
+ let spawnError: unknown = null
+ for (const program of candidates) {
+ const command = Command.create(program, this.config.args, {
+ env: this.config.env,
+ })
- // Handle stdout — parse JSON-RPC responses
- this.command.stdout.on('data', (data: string) => {
- this.outputBuffer += data
- this.processBuffer()
- })
+ // Handle stdout — parse JSON-RPC responses
+ command.stdout.on('data', (data: string) => {
+ this.outputBuffer += data
+ this.processBuffer()
+ })
- this.command.stderr.on('data', (data: string) => {
- log.warn(`[MCP:${this.config.name}] stderr`, { data })
- })
+ command.stderr.on('data', (data: string) => {
+ log.warn(`[MCP:${this.config.name}] stderr`, { data })
+ })
- this.command.on('close', () => {
- this.connected = false
- this.child = null
- this.rejectAllPending('Server process exited')
- })
+ command.on('close', () => {
+ this.connected = false
+ this.child = null
+ this.rejectAllPending('Server process exited')
+ })
- this.child = await this.command.spawn()
+ try {
+ this.child = await command.spawn()
+ spawnError = null
+ break
+ } catch (err) {
+ spawnError = err
+ }
+ }
+ if (!this.child) throw spawnError
this.connected = true
// Initialize the MCP connection
@@ -144,7 +153,6 @@ export class MCPExternalClient {
}
this.child = null
}
- this.command = null
}
isConnected() {
@@ -223,18 +231,20 @@ export class MCPExternalClient {
}
/**
- * Map a bare command name to the Windows `.cmd` shim when running on
- * Windows. Commands with an extension or an absolute path are returned
- * unchanged.
+ * Spawn candidates for a configured command, in the order worth trying.
+ * On Windows a bare name can resolve to an .exe (node, deno, native bun)
+ * or exist only as a .cmd shim (npx, npm, or an npm-installed pnpm), so
+ * both spellings are returned with the likelier one first. Commands with
+ * an extension or a path separator are returned as-is.
*/
-export function resolveCommandForPlatform(
+export function commandCandidatesForPlatform(
command: string,
platform: string = typeof navigator !== 'undefined' ? navigator.platform : ''
-): string {
+): string[] {
const isWindows = /Win/i.test(platform)
- if (!isWindows) return command
- if (/[\\/]|\.(cmd|bat|exe)$/i.test(command)) return command
- const NEEDS_CMD = new Set(['npx', 'npm', 'pnpm', 'yarn', 'bun', 'node', 'deno'])
- if (NEEDS_CMD.has(command)) return `${command}.cmd`
- return command
+ if (!isWindows) return [command]
+ if (/[\\/]|\.(cmd|bat|exe)$/i.test(command)) return [command]
+ const CMD_SHIM_FIRST = new Set(['npx', 'npm', 'pnpm', 'yarn', 'corepack'])
+ if (CMD_SHIM_FIRST.has(command)) return [`${command}.cmd`, command]
+ return [command, `${command}.cmd`]
}