Skip to content

Commit 4efe1f0

Browse files
committed
hack fix env
1 parent d7bae57 commit 4efe1f0

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

src/mcp_server_metatool/server.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,47 @@
1111
import tempfile
1212
import subprocess
1313
import ast
14+
import sys
15+
16+
# Environment variables to inherit by default
17+
DEFAULT_INHERITED_ENV_VARS = (
18+
[
19+
"APPDATA",
20+
"HOMEDRIVE",
21+
"HOMEPATH",
22+
"LOCALAPPDATA",
23+
"PATH",
24+
"PROCESSOR_ARCHITECTURE",
25+
"SYSTEMDRIVE",
26+
"SYSTEMROOT",
27+
"TEMP",
28+
"USERNAME",
29+
"USERPROFILE",
30+
]
31+
if sys.platform == "win32"
32+
else ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"]
33+
)
34+
35+
36+
def get_default_environment() -> dict[str, str]:
37+
"""
38+
Returns a default environment object including only environment variables deemed
39+
safe to inherit.
40+
"""
41+
env: dict[str, str] = {}
42+
43+
for key in DEFAULT_INHERITED_ENV_VARS:
44+
value = os.environ.get(key)
45+
if value is None:
46+
continue
47+
48+
if value.startswith("()"):
49+
# Skip functions, which are a security risk
50+
continue
51+
52+
env[key] = value
53+
54+
return env
1455

1556

1657
def sanitize_name(name: str) -> str:
@@ -42,8 +83,13 @@ async def get_mcp_servers() -> list[StdioServerParameters]:
4283
# Convert empty lists and dicts to None
4384
if "args" in params and not params["args"]:
4485
params["args"] = None
45-
if "env" in params and not params["env"]:
46-
params["env"] = None
86+
87+
# Merge environment variables
88+
params["env"] = {
89+
**get_default_environment(),
90+
**(params.get("env") or {}),
91+
}
92+
4793
server_params.append(StdioServerParameters(**params))
4894
return server_params
4995
except Exception:
@@ -118,8 +164,11 @@ async def get_custom_mcp_servers() -> list[StdioServerParameters]:
118164
params["command"] = "uv"
119165
params["args"] = ["run", script_path] + params.get("additionalArgs", [])
120166

121-
if "env" in params and not params["env"]:
122-
params["env"] = None
167+
# Merge environment variables
168+
params["env"] = {
169+
**get_default_environment(),
170+
**(params.get("env") or {}),
171+
}
123172

124173
server_params.append(StdioServerParameters(**params))
125174
return server_params

0 commit comments

Comments
 (0)