From 77d5d5cc6a6ae6021697ff04f41b62ae1e879d37 Mon Sep 17 00:00:00 2001 From: SoloLevelingAI <2914633928@QQ.com> Date: Fri, 3 Apr 2026 16:51:20 +0800 Subject: [PATCH] fix(windows): inherit all system environment variables into MCP runner for Windows (#7054) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Merge environment variables case‑insensitively - Auto‑configure dotnet PATH and suppress console window - Keep PATHEXT inheritance (from old implementation) Replaces the minimal PATHEXT fix with a robust solution that resolves common Windows subprocess issues, especially for .NET servers. * fix(stdio): improve Windows environment setup for subprocesses - Merge environment variables case‑insensitively - Auto‑configure dotnet PATH and suppress console window - Keep PATHEXT inheritance (from old implementation) -Add `show_console` option to control `CREATE_NO_WINDOW` flag.Default remains hidden (backward compatible) but can be overridden. Replaces the minimal PATHEXT fix with a robust solution that resolves common Windows subprocess issues, especially for .NET servers. * fix(stdio): improve Windows environment setup for subprocesses 修复(标准输入输出):改进 Windows 环境设置以支持子进程 - Merge environment variables case‑insensitively - Auto‑configure dotnet PATH and suppress console window - Keep PATHEXT inheritance (from old implementation) -Add `show_console` option to control `CREATE_NO_WINDOW` flag.Default remains hidden (backward compatible) but can be overridden. Replaces the minimal PATHEXT fix with a robust solution that resolves common Windows subprocess issues, especially for .NET servers. * fix(stdio): improve Windows environment setup for subprocesses 修复(标准输入输出):改进 Windows 环境设置以支持子进程 - Merge environment variables case‑insensitively - Auto‑configure dotnet PATH and suppress console window - Keep PATHEXT inheritance (from old implementation) -Add `no_console` option to control `CREATE_NO_WINDOW` flag.Default remains hidden (backward compatible) but can be overridden. Replaces the minimal PATHEXT fix with a robust solution that resolves common Windows subprocess issues, especially for .NET servers. * fix(stdio): improve Windows environment setup for subprocesses 修复(标准输入输出):改进 Windows 环境设置以支持子进程 - Merge environment variables case‑insensitively - Auto‑configure dotnet PATH and suppress console window - Keep PATHEXT inheritance (from old implementation) -Add `no_console` option to control `CREATE_NO_WINDOW` flag.Default remains hidden (backward compatible) but can be overridden. Replaces the minimal PATHEXT fix with a robust solution that resolves common Windows subprocess issues, especially for .NET servers. * fix(windows): inherit all system environment variables for subprocesses - Merge environment variables case‑insensitively - Auto‑configure dotnet PATH and suppress console window - Keep PATHEXT inheritance (from old implementation) - No longer handle as a special case for C#. Replaces the minimal PATHEXT fix with a robust solution that resolves common Windows subprocess issues, especially for .NET servers. * chore: ruff format --------- Co-authored-by: Soulter <905617992@qq.com> --- astrbot/core/agent/mcp_client.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/astrbot/core/agent/mcp_client.py b/astrbot/core/agent/mcp_client.py index af969a3fa..aae1aaa77 100644 --- a/astrbot/core/agent/mcp_client.py +++ b/astrbot/core/agent/mcp_client.py @@ -51,18 +51,29 @@ def _prepare_stdio_env(config: dict) -> dict: """Preserve Windows executable resolution for stdio subprocesses.""" if sys.platform != "win32": return config - - pathext = os.environ.get("PATHEXT") - if not pathext: - return config - prepared = config.copy() env = dict(prepared.get("env") or {}) - env.setdefault("PATHEXT", pathext) + env = _merge_environment_variables(env) prepared["env"] = env return prepared +def _merge_environment_variables(env: dict) -> dict: + """合并环境变量,处理Windows不区分大小写的情况""" + merged = env.copy() + + # 将用户环境变量转换为统一的大小写形式便于比较 + user_keys_lower = {k.lower(): k for k in merged.keys()} + + for sys_key, sys_value in os.environ.items(): + sys_key_lower = sys_key.lower() + if sys_key_lower not in user_keys_lower: + # 使用系统环境变量中的原始大小写 + merged[sys_key] = sys_value + + return merged + + async def _quick_test_mcp_connection(config: dict) -> tuple[bool, str]: """Quick test MCP server connectivity""" import aiohttp