From e7683ddb205c540fc550668d02ec7fde97418f8c Mon Sep 17 00:00:00 2001 From: Paolo Calvi Date: Thu, 29 May 2025 16:08:07 +0200 Subject: [PATCH 1/2] Fix: MCP server environment inheritance across platforms - Fix GUI applications not inheriting proper PATH/NODE_PATH - Correct Docker NODE_PATH to point to modules directory - Ensure base environment is always provided - Preserve user environment variable overrides - Resolves -32000 Connection closed errors on macOS/Linux GUI --- server/utils/MCP/hypervisor/index.js | 36 +++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/server/utils/MCP/hypervisor/index.js b/server/utils/MCP/hypervisor/index.js index cdb1569bf4b..3dfabf32956 100644 --- a/server/utils/MCP/hypervisor/index.js +++ b/server/utils/MCP/hypervisor/index.js @@ -228,21 +228,41 @@ class MCPHypervisor { } /** - * Build the MCP server environment variables key - will add back the default environment variables that are required for MCP to function - * in the docker context (eg: PATH and NODE_PATH) since when any env is provided to the MCP server it is not merged. + * Build the MCP server environment variables - ensures proper PATH and NODE_PATH + * inheritance across all platforms and deployment scenarios. * @param {Object} server - The server definition * @returns {{env: { [key: string]: string } | {}}} - The environment variables */ #buildMCPServerENV(server) { - if (!server?.env || Object.keys(server.env).length === 0) return {}; - if (process.env.ANYTHING_LLM_RUNTIME !== "docker") return server.env; + // Start with essential environment variables, inheriting from current process + // This ensures GUI applications on macOS/Linux get proper PATH inheritance + let baseEnv = { + PATH: process.env.PATH || "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin", + NODE_PATH: process.env.NODE_PATH || "/usr/local/lib/node_modules" + }; + + // Docker-specific environment setup + if (process.env.ANYTHING_LLM_RUNTIME === "docker") { + baseEnv = { + // Fixed: NODE_PATH should point to modules directory, not node binary + NODE_PATH: "/usr/local/lib/node_modules", + PATH: "/usr/local/bin:/usr/bin:/bin", + ...baseEnv // Allow inheritance to override docker defaults if needed + }; + } + // No custom environment specified - return base environment + if (!server?.env || Object.keys(server.env).length === 0) { + return { env: baseEnv }; + } + + // Merge user-specified environment with base environment + // User environment takes precedence over defaults return { env: { - NODE_PATH: "/usr/bin/node", - PATH: "/usr/bin/node:/usr/local/bin:/usr/bin:/bin", - ...server.env, // allow overrides - }, + ...baseEnv, + ...server.env + } }; } From 943c1ae2c647ca506d1dd87093015e70a92ae5fd Mon Sep 17 00:00:00 2001 From: Paolo Calvi Date: Thu, 29 May 2025 16:33:14 +0200 Subject: [PATCH 2/2] Fix: MCP server environment inheritance across platforms after linting --- server/utils/MCP/hypervisor/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/utils/MCP/hypervisor/index.js b/server/utils/MCP/hypervisor/index.js index 3dfabf32956..eef2365216d 100644 --- a/server/utils/MCP/hypervisor/index.js +++ b/server/utils/MCP/hypervisor/index.js @@ -238,7 +238,7 @@ class MCPHypervisor { // This ensures GUI applications on macOS/Linux get proper PATH inheritance let baseEnv = { PATH: process.env.PATH || "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin", - NODE_PATH: process.env.NODE_PATH || "/usr/local/lib/node_modules" + NODE_PATH: process.env.NODE_PATH || "/usr/local/lib/node_modules", }; // Docker-specific environment setup @@ -247,7 +247,7 @@ class MCPHypervisor { // Fixed: NODE_PATH should point to modules directory, not node binary NODE_PATH: "/usr/local/lib/node_modules", PATH: "/usr/local/bin:/usr/bin:/bin", - ...baseEnv // Allow inheritance to override docker defaults if needed + ...baseEnv, // Allow inheritance to override docker defaults if needed }; } @@ -261,8 +261,8 @@ class MCPHypervisor { return { env: { ...baseEnv, - ...server.env - } + ...server.env, + }, }; }