From d88e1b1e6a43dee1674b0a632d1824b727946650 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Wed, 1 Jul 2026 13:40:48 +0800 Subject: [PATCH] fix(mcp): use node entry for Windows configs --- .../editor/EditorSettingsDialog.vue | 19 ++++++-- .../lib/__tests__/mcpConfigTemplates.spec.ts | 47 ++++++++++++++++++ apps/desktop/src/lib/http.ts | 2 + apps/desktop/src/lib/mcpConfigTemplates.ts | 48 +++++++++++++++---- apps/desktop/src/lib/tauri.ts | 2 + src-tauri/src/commands/mcp.rs | 34 ++++++++++--- 6 files changed, 132 insertions(+), 20 deletions(-) diff --git a/apps/desktop/src/components/editor/EditorSettingsDialog.vue b/apps/desktop/src/components/editor/EditorSettingsDialog.vue index 62f2ad1f29..f3ebb35e71 100644 --- a/apps/desktop/src/components/editor/EditorSettingsDialog.vue +++ b/apps/desktop/src/components/editor/EditorSettingsDialog.vue @@ -67,7 +67,8 @@ import { SHORTCUT_DEFINITIONS, findShortcutConflict, normalizeShortcutSettings, import { normalizeSidebarHiddenTablePrefixes } from "@/lib/sidebarTableNameDisplay"; import { normalizeSqlFormatterSettings, type SqlFormatterSettings } from "@/lib/sqlFormatterConfig"; import { EMPTY_TABLE_COLUMN_TEMPLATE_DATA_TYPE, parseTableColumnTemplateFields, TABLE_COLUMN_TEMPLATE_DATABASE_TYPES } from "@/lib/tableColumnTemplates"; -import { buildMcpCodexConfig, buildMcpJsonConfig, buildMcpOpenCodeConfig, buildMcpVsCodeConfig, type McpEnvEntry } from "@/lib/mcpConfigTemplates"; +import { buildMcpCodexConfig, buildMcpJsonConfig, buildMcpOpenCodeConfig, buildMcpVsCodeConfig, type McpEnvEntry, type McpLaunchConfig } from "@/lib/mcpConfigTemplates"; +import { isWindows } from "@/lib/platform"; import { combineDataTypeForDatabase, dataTypeLengthInputValue, getDataTypeOptions, getDefaultLengthForType, isDataTypeLengthDisabled, splitDataType } from "@/lib/tableStructureEditorState"; import type { DatabaseType, SqlSnippet } from "@/types/database"; import { uuid } from "@/lib/utils"; @@ -1114,13 +1115,21 @@ const mcpEnvEntries = computed(() => { return entries; }); -const mcpJsonRecommendedConfig = computed(() => buildMcpJsonConfig(mcpEnvEntries.value)); +const mcpLaunchConfig = computed(() => { + if (!isWindows() || !mcpStatus.value?.script_path) return undefined; + return { + command: mcpStatus.value.node_path || "node", + args: [mcpStatus.value.script_path], + }; +}); -const mcpVsCodeRecommendedConfig = computed(() => buildMcpVsCodeConfig(mcpEnvEntries.value)); +const mcpJsonRecommendedConfig = computed(() => buildMcpJsonConfig(mcpEnvEntries.value, mcpLaunchConfig.value)); -const mcpCodexRecommendedConfig = computed(() => buildMcpCodexConfig(mcpEnvEntries.value)); +const mcpVsCodeRecommendedConfig = computed(() => buildMcpVsCodeConfig(mcpEnvEntries.value, mcpLaunchConfig.value)); -const mcpOpenCodeRecommendedConfig = computed(() => buildMcpOpenCodeConfig(mcpEnvEntries.value)); +const mcpCodexRecommendedConfig = computed(() => buildMcpCodexConfig(mcpEnvEntries.value, mcpLaunchConfig.value)); + +const mcpOpenCodeRecommendedConfig = computed(() => buildMcpOpenCodeConfig(mcpEnvEntries.value, mcpLaunchConfig.value)); const mcpStatusTone = computed<"ok" | "warning" | "muted">(() => { if (!mcpStatus.value) return "muted"; diff --git a/apps/desktop/src/lib/__tests__/mcpConfigTemplates.spec.ts b/apps/desktop/src/lib/__tests__/mcpConfigTemplates.spec.ts index 5f95b04b75..36ad21f1b7 100644 --- a/apps/desktop/src/lib/__tests__/mcpConfigTemplates.spec.ts +++ b/apps/desktop/src/lib/__tests__/mcpConfigTemplates.spec.ts @@ -27,6 +27,19 @@ describe("MCP config templates", () => { }); }); + it("builds standard JSON configs with a direct node launch command", () => { + const config = JSON.parse(buildMcpJsonConfig([], { command: "C:\\Program Files\\nodejs\\node.exe", args: ["C:\\Users\\zhiyo\\AppData\\Roaming\\npm\\node_modules\\@dbx-app\\mcp-server\\dist\\index.js"] })); + + expect(config).toEqual({ + mcpServers: { + dbx: { + command: "C:\\Program Files\\nodejs\\node.exe", + args: ["C:\\Users\\zhiyo\\AppData\\Roaming\\npm\\node_modules\\@dbx-app\\mcp-server\\dist\\index.js"], + }, + }, + }); + }); + it("builds VS Code MCP config with the servers root", () => { const config = JSON.parse(buildMcpVsCodeConfig([["DBX_MCP_ALLOW_WRITES", "0"]])); @@ -43,10 +56,31 @@ describe("MCP config templates", () => { }); }); + it("builds VS Code config with a direct node launch command", () => { + const config = JSON.parse(buildMcpVsCodeConfig([["DBX_MCP_ALLOW_WRITES", "0"]], { command: "node", args: ["C:\\dbx\\mcp\\dist\\index.js"] })); + + expect(config).toEqual({ + servers: { + dbx: { + type: "stdio", + command: "node", + args: ["C:\\dbx\\mcp\\dist\\index.js"], + env: { + DBX_MCP_ALLOW_WRITES: "0", + }, + }, + }, + }); + }); + it("builds Codex TOML config with env entries", () => { expect(buildMcpCodexConfig([["DBX_MCP_ALLOW_WRITES", "0"]])).toBe(["[mcp_servers.dbx]", 'command = "dbx-mcp-server"', "", "[mcp_servers.dbx.env]", 'DBX_MCP_ALLOW_WRITES = "0"'].join("\n")); }); + it("builds Codex TOML config with a direct node launch command", () => { + expect(buildMcpCodexConfig([["DBX_MCP_ALLOW_WRITES", "0"]], { command: "node", args: ["C:\\dbx\\mcp\\dist\\index.js"] })).toBe(["[mcp_servers.dbx]", 'command = "node"', 'args = ["C:\\\\dbx\\\\mcp\\\\dist\\\\index.js"]', "", "[mcp_servers.dbx.env]", 'DBX_MCP_ALLOW_WRITES = "0"'].join("\n")); + }); + it("builds OpenCode config using environment entries", () => { const config = JSON.parse(buildMcpOpenCodeConfig([["DBX_MCP_ALLOW_DANGEROUS_SQL", "1"]])); @@ -62,4 +96,17 @@ describe("MCP config templates", () => { }, }); }); + + it("builds OpenCode config with a direct node launch command", () => { + const config = JSON.parse(buildMcpOpenCodeConfig([], { command: "node", args: ["C:\\dbx\\mcp\\dist\\index.js"] })); + + expect(config).toEqual({ + mcp: { + dbx: { + type: "local", + command: ["node", "C:\\dbx\\mcp\\dist\\index.js"], + }, + }, + }); + }); }); diff --git a/apps/desktop/src/lib/http.ts b/apps/desktop/src/lib/http.ts index 715aff32c9..4106b9dd7c 100644 --- a/apps/desktop/src/lib/http.ts +++ b/apps/desktop/src/lib/http.ts @@ -1849,11 +1849,13 @@ export async function checkMcpServerStatus(): Promise { return Object.fromEntries(envEntries); } -export function buildMcpJsonConfig(envEntries: readonly McpEnvEntry[] = []): string { +function launchConfig(config?: McpLaunchConfig): McpLaunchConfig { + return config ?? DEFAULT_MCP_LAUNCH_CONFIG; +} + +function withLaunchConfig(dbx: Record, config?: McpLaunchConfig): Record { + const launch = launchConfig(config); + dbx.command = launch.command; + if (launch.args && launch.args.length > 0) { + dbx.args = [...launch.args]; + } + return dbx; +} + +function tomlStringArray(values: readonly string[]): string { + return `[${values.map((value) => JSON.stringify(value)).join(", ")}]`; +} + +export function buildMcpJsonConfig(envEntries: readonly McpEnvEntry[] = [], config?: McpLaunchConfig): string { const dbx: Record = { - command: "dbx-mcp-server", + ...withLaunchConfig({}, config), }; if (envEntries.length > 0) { @@ -16,10 +42,10 @@ export function buildMcpJsonConfig(envEntries: readonly McpEnvEntry[] = []): str return JSON.stringify({ mcpServers: { dbx } }, null, 2); } -export function buildMcpVsCodeConfig(envEntries: readonly McpEnvEntry[] = []): string { +export function buildMcpVsCodeConfig(envEntries: readonly McpEnvEntry[] = [], config?: McpLaunchConfig): string { const dbx: Record = { type: "stdio", - command: "dbx-mcp-server", + ...withLaunchConfig({}, config), }; if (envEntries.length > 0) { @@ -29,8 +55,13 @@ export function buildMcpVsCodeConfig(envEntries: readonly McpEnvEntry[] = []): s return JSON.stringify({ servers: { dbx } }, null, 2); } -export function buildMcpCodexConfig(envEntries: readonly McpEnvEntry[] = []): string { - const lines = ["[mcp_servers.dbx]", 'command = "dbx-mcp-server"']; +export function buildMcpCodexConfig(envEntries: readonly McpEnvEntry[] = [], config?: McpLaunchConfig): string { + const launch = launchConfig(config); + const lines = ["[mcp_servers.dbx]", `command = ${JSON.stringify(launch.command)}`]; + + if (launch.args && launch.args.length > 0) { + lines.push(`args = ${tomlStringArray(launch.args)}`); + } if (envEntries.length > 0) { lines.push(""); @@ -43,10 +74,11 @@ export function buildMcpCodexConfig(envEntries: readonly McpEnvEntry[] = []): st return lines.join("\n"); } -export function buildMcpOpenCodeConfig(envEntries: readonly McpEnvEntry[] = []): string { +export function buildMcpOpenCodeConfig(envEntries: readonly McpEnvEntry[] = [], config?: McpLaunchConfig): string { + const launch = launchConfig(config); const dbx: Record = { type: "local", - command: ["dbx-mcp-server"], + command: [launch.command, ...(launch.args ?? [])], }; if (envEntries.length > 0) { diff --git a/apps/desktop/src/lib/tauri.ts b/apps/desktop/src/lib/tauri.ts index b902498bfa..751e7d134a 100644 --- a/apps/desktop/src/lib/tauri.ts +++ b/apps/desktop/src/lib/tauri.ts @@ -1141,11 +1141,13 @@ export interface UpdateDownloadProgress { export interface McpServerStatus { installed: boolean; npm_available: boolean; + node_path: string | null; node_version: string | null; current_version: string | null; latest_version: string | null; update_available: boolean; bin_path: string | null; + script_path: string | null; install_command: string; update_command: string; error: string | null; diff --git a/src-tauri/src/commands/mcp.rs b/src-tauri/src/commands/mcp.rs index aacef7b782..18f3e15df3 100644 --- a/src-tauri/src/commands/mcp.rs +++ b/src-tauri/src/commands/mcp.rs @@ -17,11 +17,13 @@ const CREATE_NO_WINDOW: u32 = 0x08000000; pub struct McpServerStatus { pub installed: bool, pub npm_available: bool, + pub node_path: Option, pub node_version: Option, pub current_version: Option, pub latest_version: Option, pub update_available: bool, pub bin_path: Option, + pub script_path: Option, pub install_command: String, pub update_command: String, pub error: Option, @@ -36,14 +38,18 @@ struct NpmLatestPackage { pub async fn check_mcp_server_status() -> Result { let local_status = tauri::async_runtime::spawn_blocking(|| { let npm_available = command_success("npm", &["--version"]); - let node_version = command_stdout("node", &["--version"]).ok().and_then(first_non_empty_line); + let node_path = locate_command("node"); + let node_command = node_path.as_deref().unwrap_or("node"); + let node_version = command_stdout(node_command, &["--version"]).ok().and_then(first_non_empty_line); let current_version = if npm_available { installed_mcp_version() } else { None }; let bin_path = locate_mcp_bin(); - (npm_available, node_version, current_version, bin_path) + let script_path = if npm_available { installed_mcp_bin_script() } else { None }; + (npm_available, node_path, node_version, current_version, bin_path, script_path) }); let latest_version = fetch_latest_mcp_version(); let (local_status, latest_version) = tokio::join!(local_status, latest_version); - let (npm_available, node_version, current_version, bin_path) = local_status.map_err(|err| err.to_string())?; + let (npm_available, node_path, node_version, current_version, bin_path, script_path) = + local_status.map_err(|err| err.to_string())?; let latest_version = latest_version.ok(); let update_available = current_version .as_deref() @@ -52,13 +58,15 @@ pub async fn check_mcp_server_status() -> Result { let error = if npm_available { None } else { Some("npm is not available in PATH.".to_string()) }; Ok(McpServerStatus { - installed: current_version.is_some() || bin_path.is_some(), + installed: current_version.is_some() || bin_path.is_some() || script_path.is_some(), npm_available, + node_path, node_version, current_version, latest_version, update_available, bin_path, + script_path, install_command: MCP_INSTALL_COMMAND.to_string(), update_command: MCP_INSTALL_COMMAND.to_string(), error, @@ -115,9 +123,21 @@ fn installed_mcp_version() -> Option { } pub(crate) fn resolve_mcp_server_command() -> Option<(String, Vec)> { - locate_mcp_bin() - .map(|path| (path, Vec::new())) - .or_else(|| installed_mcp_bin_script().map(|script| ("node".to_string(), vec![script]))) + #[cfg(windows)] + { + // Node.js 18.20.2+ rejects direct spawn of .cmd/.bat files without a shell + // on Windows. Prefer the package's real JS entry point for Codex CLI. + return installed_mcp_bin_script() + .map(|script| (locate_command("node").unwrap_or_else(|| "node".to_string()), vec![script])) + .or_else(|| locate_mcp_bin().map(|path| (path, Vec::new()))); + } + + #[cfg(not(windows))] + { + locate_mcp_bin() + .map(|path| (path, Vec::new())) + .or_else(|| installed_mcp_bin_script().map(|script| ("node".to_string(), vec![script]))) + } } pub(crate) fn locate_command(command: &str) -> Option {