From 74a2e9c08b71959d12a167233e1bf338f8c3c2d4 Mon Sep 17 00:00:00 2001 From: farion1231 Date: Tue, 2 Dec 2025 15:43:14 +0800 Subject: [PATCH] fix(mcp): use browser-compatible platform detection for MCP presets Replace Node.js process.platform check with navigator.userAgent-based detection from @/lib/platform. The previous implementation using process.platform failed in Tauri's browser environment, causing Windows platform to always return false and not apply the required 'cmd /c' wrapper for npx commands. This fix ensures that on Windows, MCP presets like sequential-thinking correctly use 'cmd /c npx' format instead of direct 'npx', eliminating the warning: "Windows requires 'cmd /c' wrapper to execute npx" Changes: - Import isWindows() from @/lib/platform instead of defining locally - Remove incorrect process.platform-based detection - Now properly detects Windows in browser/WebView environment --- src/config/mcpPresets.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/config/mcpPresets.ts b/src/config/mcpPresets.ts index e52efdc87..e7514e641 100644 --- a/src/config/mcpPresets.ts +++ b/src/config/mcpPresets.ts @@ -1,7 +1,25 @@ import { McpServer, McpServerSpec } from "../types"; +import { isWindows } from "@/lib/platform"; export type McpPreset = Omit; +// 创建跨平台 npx 命令配置 +// Windows 需要使用 cmd /c wrapper 来执行 npx.cmd +// Mac/Linux 可以直接执行 npx +const createNpxCommand = (packageName: string, extraArgs: string[] = []): { command: string; args: string[] } => { + if (isWindows()) { + return { + command: 'cmd', + args: ['/c', 'npx', ...extraArgs, packageName] + }; + } else { + return { + command: 'npx', + args: [...extraArgs, packageName] + }; + } +}; + // 预设 MCP(逻辑简化版): // - 仅包含最常用、可快速落地的 stdio 模式示例 // - 不涉及分类/模板/测速等复杂逻辑,默认以 disabled 形式"回种"到 config.json @@ -26,8 +44,7 @@ export const mcpPresets: McpPreset[] = [ tags: ["stdio", "time", "utility"], server: { type: "stdio", - command: "npx", - args: ["-y", "@modelcontextprotocol/server-time"], + ...createNpxCommand("@modelcontextprotocol/server-time", ["-y"]), } as McpServerSpec, homepage: "https://github.com/modelcontextprotocol/servers", docs: "https://github.com/modelcontextprotocol/servers/tree/main/src/time", @@ -38,8 +55,7 @@ export const mcpPresets: McpPreset[] = [ tags: ["stdio", "memory", "graph"], server: { type: "stdio", - command: "npx", - args: ["-y", "@modelcontextprotocol/server-memory"], + ...createNpxCommand("@modelcontextprotocol/server-memory", ["-y"]), } as McpServerSpec, homepage: "https://github.com/modelcontextprotocol/servers", docs: "https://github.com/modelcontextprotocol/servers/tree/main/src/memory", @@ -50,8 +66,7 @@ export const mcpPresets: McpPreset[] = [ tags: ["stdio", "thinking", "reasoning"], server: { type: "stdio", - command: "npx", - args: ["-y", "@modelcontextprotocol/server-sequential-thinking"], + ...createNpxCommand("@modelcontextprotocol/server-sequential-thinking", ["-y"]), } as McpServerSpec, homepage: "https://github.com/modelcontextprotocol/servers", docs: "https://github.com/modelcontextprotocol/servers/tree/main/src/sequentialthinking", @@ -62,8 +77,7 @@ export const mcpPresets: McpPreset[] = [ tags: ["stdio", "docs", "search"], server: { type: "stdio", - command: "npx", - args: ["-y", "@upstash/context7-mcp"], + ...createNpxCommand("@upstash/context7-mcp", ["-y"]), } as McpServerSpec, homepage: "https://context7.com", docs: "https://github.com/upstash/context7/blob/master/README.md",