θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content

Fix/mcp environment inheritance #3909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions server/utils/MCP/hypervisor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,40 @@ 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,
},
};
}
Expand Down