fix: Windows encoding + .cmd subprocess resolution (#64) (#66)

- Replace text=True with encoding='utf-8', errors='replace' in all
  subprocess.run calls (channels + cli.py) to fix GBK decode errors
  on Chinese Windows systems
- Use shutil.which() resolved paths in channel subprocess calls to
  handle Windows .cmd wrappers (npm global installs)

Co-authored-by: Panniantong <panniantong@users.noreply.github.com>
This commit is contained in:
Pnant
2026-03-04 17:30:11 +08:00
committed by GitHub
parent 7434c3cb9f
commit a91a74d880
8 changed files with 45 additions and 33 deletions
+4 -2
View File
@@ -18,7 +18,8 @@ class BossZhipinChannel(Channel):
return "zhipin.com" in domain or "boss.com" in domain return "zhipin.com" in domain or "boss.com" in domain
def check(self, config=None): def check(self, config=None):
if not shutil.which("mcporter"): mcporter = shutil.which("mcporter")
if not mcporter:
return "off", ( return "off", (
"可通过 Jina Reader 读取职位页面。完整功能需要:\n" "可通过 Jina Reader 读取职位页面。完整功能需要:\n"
" 1. git clone https://github.com/mucsbr/mcp-bosszp.git\n" " 1. git clone https://github.com/mucsbr/mcp-bosszp.git\n"
@@ -28,7 +29,8 @@ class BossZhipinChannel(Channel):
) )
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "list"], capture_output=True, text=True, timeout=10 [mcporter, "list"], capture_output=True,
encoding="utf-8", errors="replace", timeout=10
) )
out = r.stdout.lower() out = r.stdout.lower()
if "boss" in out or "zhipin" in out: if "boss" in out or "zhipin" in out:
+6 -4
View File
@@ -18,7 +18,8 @@ class DouyinChannel(Channel):
return "douyin.com" in d or "iesdouyin.com" in d return "douyin.com" in d or "iesdouyin.com" in d
def check(self, config=None): def check(self, config=None):
if not shutil.which("mcporter"): mcporter = shutil.which("mcporter")
if not mcporter:
return "off", ( return "off", (
"需要 mcporter + douyin-mcp-server。安装步骤:\n" "需要 mcporter + douyin-mcp-server。安装步骤:\n"
" 1. npm install -g mcporter\n" " 1. npm install -g mcporter\n"
@@ -29,7 +30,8 @@ class DouyinChannel(Channel):
) )
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "config", "list"], capture_output=True, text=True, timeout=5 [mcporter, "config", "list"], capture_output=True,
encoding="utf-8", errors="replace", timeout=5
) )
if "douyin" not in r.stdout: if "douyin" not in r.stdout:
return "off", ( return "off", (
@@ -42,8 +44,8 @@ class DouyinChannel(Channel):
return "off", "mcporter 连接异常" return "off", "mcporter 连接异常"
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "call", "douyin.parse_douyin_video_info(share_link: \"https://www.douyin.com\")"], [mcporter, "call", "douyin.parse_douyin_video_info(share_link: \"https://www.douyin.com\")"],
capture_output=True, text=True, timeout=15 capture_output=True, encoding="utf-8", errors="replace", timeout=15
) )
if r.returncode == 0: if r.returncode == 0:
return "ok", "完整可用(视频解析、下载链接获取)" return "ok", "完整可用(视频解析、下载链接获取)"
+4 -2
View File
@@ -16,7 +16,8 @@ class ExaSearchChannel(Channel):
return False # Search-only channel return False # Search-only channel
def check(self, config=None): def check(self, config=None):
if not shutil.which("mcporter"): mcporter = shutil.which("mcporter")
if not mcporter:
return "off", ( return "off", (
"需要 mcporter + Exa MCP。安装:\n" "需要 mcporter + Exa MCP。安装:\n"
" npm install -g mcporter\n" " npm install -g mcporter\n"
@@ -24,7 +25,8 @@ class ExaSearchChannel(Channel):
) )
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "config", "list"], capture_output=True, text=True, timeout=5 [mcporter, "config", "list"], capture_output=True,
encoding="utf-8", errors="replace", timeout=5
) )
if "exa" in r.stdout.lower(): if "exa" in r.stdout.lower():
return "ok", "全网语义搜索可用(免费,无需 API Key)" return "ok", "全网语义搜索可用(免费,无需 API Key)"
+4 -3
View File
@@ -17,12 +17,13 @@ class GitHubChannel(Channel):
return "github.com" in urlparse(url).netloc.lower() return "github.com" in urlparse(url).netloc.lower()
def check(self, config=None): def check(self, config=None):
if not shutil.which("gh"): gh = shutil.which("gh")
if not gh:
return "warn", "gh CLI 未安装。安装:https://cli.github.com" return "warn", "gh CLI 未安装。安装:https://cli.github.com"
try: try:
r = subprocess.run( r = subprocess.run(
["gh", "auth", "status"], [gh, "auth", "status"],
capture_output=True, text=True, timeout=5 capture_output=True, encoding="utf-8", errors="replace", timeout=5
) )
if r.returncode == 0: if r.returncode == 0:
return "ok", "完整可用(读取、搜索、Fork、Issue、PR 等)" return "ok", "完整可用(读取、搜索、Fork、Issue、PR 等)"
+4 -2
View File
@@ -17,7 +17,8 @@ class LinkedInChannel(Channel):
return "linkedin.com" in urlparse(url).netloc.lower() return "linkedin.com" in urlparse(url).netloc.lower()
def check(self, config=None): def check(self, config=None):
if not shutil.which("mcporter"): mcporter = shutil.which("mcporter")
if not mcporter:
return "off", ( return "off", (
"基本内容可通过 Jina Reader 读取。完整功能需要:\n" "基本内容可通过 Jina Reader 读取。完整功能需要:\n"
" pip install linkedin-scraper-mcp\n" " pip install linkedin-scraper-mcp\n"
@@ -26,7 +27,8 @@ class LinkedInChannel(Channel):
) )
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "config", "list"], capture_output=True, text=True, timeout=5 [mcporter, "config", "list"], capture_output=True,
encoding="utf-8", errors="replace", timeout=5
) )
if "linkedin" in r.stdout.lower(): if "linkedin" in r.stdout.lower():
return "ok", "完整可用(Profile、公司、职位搜索)" return "ok", "完整可用(Profile、公司、职位搜索)"
+2 -1
View File
@@ -26,7 +26,8 @@ class TwitterChannel(Channel):
) )
try: try:
r = subprocess.run( r = subprocess.run(
[xreach, "auth", "check"], capture_output=True, text=True, timeout=10 [xreach, "auth", "check"], capture_output=True,
encoding="utf-8", errors="replace", timeout=10
) )
if r.returncode == 0: if r.returncode == 0:
return "ok", "完整可用(读取、搜索推文)" return "ok", "完整可用(读取、搜索推文)"
+6 -4
View File
@@ -40,7 +40,8 @@ class XiaoHongShuChannel(Channel):
return "xiaohongshu.com" in d or "xhslink.com" in d return "xiaohongshu.com" in d or "xhslink.com" in d
def check(self, config=None): def check(self, config=None):
if not shutil.which("mcporter"): mcporter = shutil.which("mcporter")
if not mcporter:
return "off", ( return "off", (
"需要 mcporter + xiaohongshu-mcp。安装步骤:\n" "需要 mcporter + xiaohongshu-mcp。安装步骤:\n"
" 1. npm install -g mcporter\n" " 1. npm install -g mcporter\n"
@@ -50,7 +51,8 @@ class XiaoHongShuChannel(Channel):
) )
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "config", "list"], capture_output=True, text=True, timeout=5 [mcporter, "config", "list"], capture_output=True,
encoding="utf-8", errors="replace", timeout=5
) )
if "xiaohongshu" not in r.stdout: if "xiaohongshu" not in r.stdout:
return "off", ( return "off", (
@@ -62,8 +64,8 @@ class XiaoHongShuChannel(Channel):
return "off", "mcporter 连接异常" return "off", "mcporter 连接异常"
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "call", "xiaohongshu.check_login_status()"], [mcporter, "call", "xiaohongshu.check_login_status()"],
capture_output=True, text=True, timeout=10 capture_output=True, encoding="utf-8", errors="replace", timeout=10
) )
if "已登录" in r.stdout or "logged" in r.stdout.lower(): if "已登录" in r.stdout or "logged" in r.stdout.lower():
return "ok", "完整可用(阅读、搜索、发帖、评论、点赞)" return "ok", "完整可用(阅读、搜索、发帖、评论、点赞)"
+15 -15
View File
@@ -319,7 +319,7 @@ def _install_system_deps():
list_path = "/etc/apt/sources.list.d/github-cli.list" list_path = "/etc/apt/sources.list.d/github-cli.list"
arch = subprocess.run( arch = subprocess.run(
["dpkg", "--print-architecture"], ["dpkg", "--print-architecture"],
capture_output=True, text=True, timeout=10, capture_output=True, encoding="utf-8", errors="replace", timeout=10,
).stdout.strip() or "amd64" ).stdout.strip() or "amd64"
subprocess.run( subprocess.run(
["curl", "-fsSL", "https://cli.github.com/packages/githubcli-archive-keyring.gpg", "-o", keyring_path], ["curl", "-fsSL", "https://cli.github.com/packages/githubcli-archive-keyring.gpg", "-o", keyring_path],
@@ -394,7 +394,7 @@ def _install_system_deps():
try: try:
subprocess.run( subprocess.run(
["npm", "install", "-g", "xreach-cli"], ["npm", "install", "-g", "xreach-cli"],
capture_output=True, text=True, timeout=120, capture_output=True, encoding="utf-8", errors="replace", timeout=120,
) )
if shutil.which("xreach"): if shutil.which("xreach"):
print(" ✅ xreach CLI installed (Twitter search + timeline)") print(" ✅ xreach CLI installed (Twitter search + timeline)")
@@ -407,13 +407,13 @@ def _install_system_deps():
# ── undici (proxy support for Node.js fetch) ── # ── undici (proxy support for Node.js fetch) ──
if shutil.which("npm"): if shutil.which("npm"):
npm_root = subprocess.run(["npm", "root", "-g"], capture_output=True, text=True, timeout=5).stdout.strip() npm_root = subprocess.run(["npm", "root", "-g"], capture_output=True, encoding="utf-8", errors="replace", timeout=5).stdout.strip()
undici_path = os.path.join(npm_root, "undici", "index.js") if npm_root else "" undici_path = os.path.join(npm_root, "undici", "index.js") if npm_root else ""
if os.path.exists(undici_path): if os.path.exists(undici_path):
print(" ✅ undici already installed (Node.js proxy support)") print(" ✅ undici already installed (Node.js proxy support)")
else: else:
try: try:
subprocess.run(["npm", "install", "-g", "undici"], capture_output=True, text=True, timeout=60) subprocess.run(["npm", "install", "-g", "undici"], capture_output=True, encoding="utf-8", errors="replace", timeout=60)
print(" ✅ undici installed (Node.js proxy support)") print(" ✅ undici installed (Node.js proxy support)")
except Exception: except Exception:
print(" ⬜ undici install failed (optional — xreach may not work behind proxies)") print(" ⬜ undici install failed (optional — xreach may not work behind proxies)")
@@ -506,7 +506,7 @@ def _install_mcporter():
try: try:
subprocess.run( subprocess.run(
["npm", "install", "-g", "mcporter"], ["npm", "install", "-g", "mcporter"],
capture_output=True, text=True, timeout=120, capture_output=True, encoding="utf-8", errors="replace", timeout=120,
) )
if shutil.which("mcporter"): if shutil.which("mcporter"):
print(" ✅ mcporter installed") print(" ✅ mcporter installed")
@@ -520,12 +520,12 @@ def _install_mcporter():
# Configure Exa MCP (free, no key needed) # Configure Exa MCP (free, no key needed)
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "config", "list"], capture_output=True, text=True, timeout=5 ["mcporter", "config", "list"], capture_output=True, encoding="utf-8", errors="replace", timeout=5
) )
if "exa" not in r.stdout: if "exa" not in r.stdout:
subprocess.run( subprocess.run(
["mcporter", "config", "add", "exa", "https://mcp.exa.ai/mcp"], ["mcporter", "config", "add", "exa", "https://mcp.exa.ai/mcp"],
capture_output=True, text=True, timeout=10, capture_output=True, encoding="utf-8", errors="replace", timeout=10,
) )
print(" ✅ Exa search configured (free, no API key needed)") print(" ✅ Exa search configured (free, no API key needed)")
else: else:
@@ -536,7 +536,7 @@ def _install_mcporter():
# Check XiaoHongShu MCP (only if server is running) # Check XiaoHongShu MCP (only if server is running)
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "config", "list"], capture_output=True, text=True, timeout=5 ["mcporter", "config", "list"], capture_output=True, encoding="utf-8", errors="replace", timeout=5
) )
if "xiaohongshu" in r.stdout: if "xiaohongshu" in r.stdout:
print(" ✅ XiaoHongShu MCP already configured") print(" ✅ XiaoHongShu MCP already configured")
@@ -547,7 +547,7 @@ def _install_mcporter():
requests.get("http://localhost:18060/", timeout=3) requests.get("http://localhost:18060/", timeout=3)
subprocess.run( subprocess.run(
["mcporter", "config", "add", "xiaohongshu", "http://localhost:18060/mcp"], ["mcporter", "config", "add", "xiaohongshu", "http://localhost:18060/mcp"],
capture_output=True, text=True, timeout=10, capture_output=True, encoding="utf-8", errors="replace", timeout=10,
) )
print(" ✅ XiaoHongShu MCP auto-detected and configured") print(" ✅ XiaoHongShu MCP auto-detected and configured")
except Exception: except Exception:
@@ -606,7 +606,7 @@ def _detect_environment():
# systemd-detect-virt # systemd-detect-virt
try: try:
import subprocess import subprocess
result = subprocess.run(["systemd-detect-virt"], capture_output=True, text=True, timeout=3) result = subprocess.run(["systemd-detect-virt"], capture_output=True, encoding="utf-8", errors="replace", timeout=3)
if result.returncode == 0 and result.stdout.strip() != "none": if result.returncode == 0 and result.stdout.strip() != "none":
indicators += 1 indicators += 1
except: except:
@@ -737,7 +737,7 @@ def _cmd_configure(args):
env["CT0"] = ct0 env["CT0"] = ct0
result = subprocess.run( result = subprocess.run(
[xreach, "search", "test", "-n", "1"], [xreach, "search", "test", "-n", "1"],
capture_output=True, text=True, timeout=15, capture_output=True, encoding="utf-8", errors="replace", timeout=15,
env=env, env=env,
) )
if result.returncode == 0 and result.stdout.strip(): if result.returncode == 0 and result.stdout.strip():
@@ -828,7 +828,7 @@ def _cmd_uninstall(args):
for mcp_name in ("exa", "xiaohongshu"): for mcp_name in ("exa", "xiaohongshu"):
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "list"], capture_output=True, text=True, timeout=10 ["mcporter", "list"], capture_output=True, encoding="utf-8", errors="replace", timeout=10
) )
if mcp_name in r.stdout: if mcp_name in r.stdout:
if dry_run: if dry_run:
@@ -836,7 +836,7 @@ def _cmd_uninstall(args):
else: else:
subprocess.run( subprocess.run(
["mcporter", "config", "remove", mcp_name], ["mcporter", "config", "remove", mcp_name],
capture_output=True, text=True, timeout=10, capture_output=True, encoding="utf-8", errors="replace", timeout=10,
) )
print(f" Removed mcporter entry: {mcp_name}") print(f" Removed mcporter entry: {mcp_name}")
removed_any = True removed_any = True
@@ -896,7 +896,7 @@ def _cmd_setup():
else: else:
try: try:
r = subprocess.run( r = subprocess.run(
["mcporter", "config", "list"], capture_output=True, text=True, timeout=10 ["mcporter", "config", "list"], capture_output=True, encoding="utf-8", errors="replace", timeout=10
) )
if "exa" in r.stdout.lower(): if "exa" in r.stdout.lower():
print(" 当前状态: ✅ 已配置") print(" 当前状态: ✅ 已配置")
@@ -906,7 +906,7 @@ def _cmd_setup():
if setup_now in ("", "y", "yes"): if setup_now in ("", "y", "yes"):
add_r = subprocess.run( add_r = subprocess.run(
["mcporter", "config", "add", "exa", "https://mcp.exa.ai/mcp"], ["mcporter", "config", "add", "exa", "https://mcp.exa.ai/mcp"],
capture_output=True, text=True, timeout=10, capture_output=True, encoding="utf-8", errors="replace", timeout=10,
) )
if add_r.returncode == 0: if add_r.returncode == 0:
print(" ✅ Exa 已配置") print(" ✅ Exa 已配置")