Skip to content

Commit bc2d554

Browse files
Panniantongclaude
andauthored
fix(xiaohongshu): switch from Docker MCP to xhs-cli (pipx install) (#236)
Docker + Cookie import → `pipx install xiaohongshu-cli` + `xhs login`. Tier downgraded from 2 to 1. xhs-cli (1,477 stars) auto-extracts cookies from browser, supports search/read/comment/post/hot/feed. - xiaohongshu.py: check() detects `xhs` binary, parses `xhs status` - cli.py: install via pipx instead of Docker - skill/references/social.md: updated XHS commands - tests: 3 new tests for CLI-based check() - 77 tests passing Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 15f161e commit bc2d554

File tree

4 files changed

+87
-125
lines changed

4 files changed

+87
-125
lines changed

agent_reach/channels/xiaohongshu.py

Lines changed: 28 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
"""XiaoHongShu -- check if mcporter + xiaohongshu MCP is available."""
2+
"""XiaoHongShu check if xhs-cli (xiaohongshu-cli) is available."""
33

4-
import json
5-
import platform
64
import shutil
75
import subprocess
86
from .base import Channel
@@ -117,103 +115,48 @@ def _clean_comment(comment):
117115
return result
118116

119117

120-
def _is_arm64() -> bool:
121-
"""Detect ARM64 architecture (e.g. Apple Silicon)."""
122-
machine = platform.machine().lower()
123-
return machine in ("arm64", "aarch64")
124-
125-
126-
def _mcporter_status_ok(stdout: str) -> bool:
127-
"""Return True if mcporter JSON output indicates status == 'ok'.
128-
129-
Uses proper JSON parsing to handle Windows BOM, CRLF line endings, and
130-
whitespace variations. Falls back to normalised string matching so the
131-
check still works if mcporter ever returns non-JSON text.
132-
"""
133-
text = stdout.strip()
134-
# Strip UTF-8 BOM that Windows PowerShell sometimes prepends.
135-
if text.startswith("\ufeff"):
136-
text = text[1:]
137-
try:
138-
data = json.loads(text)
139-
if isinstance(data, dict):
140-
return str(data.get("status", "")).lower() == "ok"
141-
except (json.JSONDecodeError, ValueError):
142-
pass
143-
# Fallback: normalise whitespace and CRLF, then do string search.
144-
normalised = text.lower().replace("\r\n", "\n").replace("\r", "\n").replace(" ", "")
145-
return '"status":"ok"' in normalised
146-
147-
148-
def _docker_run_hint() -> str:
149-
"""Return the docker run command, with --platform flag for ARM64."""
150-
if _is_arm64():
151-
return (
152-
" docker run -d --name xiaohongshu-mcp -p 18060:18060 "
153-
"--platform linux/amd64 xpzouying/xiaohongshu-mcp\n"
154-
" # ARM64 also: build from source: "
155-
"https://github.com/xpzouying/xiaohongshu-mcp"
156-
)
157-
return (
158-
" docker run -d --name xiaohongshu-mcp -p 18060:18060 "
159-
"xpzouying/xiaohongshu-mcp"
160-
)
161-
162-
163118
class XiaoHongShuChannel(Channel):
164119
name = "xiaohongshu"
165120
description = "小红书笔记"
166-
backends = ["xiaohongshu-mcp"]
167-
tier = 2
121+
backends = ["xhs-cli (xiaohongshu-cli)"]
122+
tier = 1
168123

169124
def can_handle(self, url: str) -> bool:
170125
from urllib.parse import urlparse
171126
d = urlparse(url).netloc.lower()
172127
return "xiaohongshu.com" in d or "xhslink.com" in d
173128

174129
def check(self, config=None):
175-
mcporter = shutil.which("mcporter")
176-
if not mcporter:
130+
xhs = shutil.which("xhs")
131+
if not xhs:
177132
return "off", (
178-
"需要 mcporter + xiaohongshu-mcp。安装步骤\n"
179-
" 1. npm install -g mcporter\n"
180-
" 2. " + _docker_run_hint().strip() + "\n"
181-
" 3. mcporter config add xiaohongshu http://localhost:18060/mcp\n"
182-
" 详见 https://github.com/xpzouying/xiaohongshu-mcp"
133+
"需要安装 xhs-cli\n"
134+
" pipx install xiaohongshu-cli\n"
135+
"或:\n"
136+
" uv tool install xiaohongshu-cli\n"
137+
"安装后运行 `xhs login` 登录"
183138
)
184-
is_windows = platform.system() == "Windows"
185-
config_timeout = 15 if is_windows else 5
139+
186140
try:
187141
r = subprocess.run(
188-
[mcporter, "config", "get", "xiaohongshu", "--json"],
189-
capture_output=True,
190-
encoding="utf-8",
191-
errors="replace",
192-
timeout=config_timeout,
142+
[xhs, "status"], capture_output=True,
143+
encoding="utf-8", errors="replace", timeout=10,
193144
)
194-
if r.returncode != 0 or "xiaohongshu" not in r.stdout.lower():
195-
return "off", (
196-
"mcporter 已装但小红书 MCP 未配置。运行:\n"
197-
+ _docker_run_hint() + "\n"
198-
" mcporter config add xiaohongshu http://localhost:18060/mcp"
145+
output = (r.stdout or "") + (r.stderr or "")
146+
if r.returncode == 0 and "ok: true" in output:
147+
return "ok", (
148+
"完整可用(搜索、阅读、评论、发帖、热门、"
149+
"收藏、关注、用户查询)"
199150
)
200-
except Exception:
201-
return "off", "mcporter 连接异常"
202-
203-
# Use longer timeouts on Windows where mcporter may be slower to respond.
204-
list_timeout = 30 if is_windows else 10
205-
try:
206-
r = subprocess.run(
207-
[mcporter, "list", "xiaohongshu", "--json"],
208-
capture_output=True,
209-
encoding="utf-8",
210-
errors="replace",
211-
timeout=list_timeout,
151+
if "not_authenticated" in output or "expired" in output:
152+
return "warn", (
153+
"xhs-cli 已安装但未登录。运行:\n"
154+
" xhs login\n"
155+
"(自动从浏览器提取 Cookie,或扫码登录)"
156+
)
157+
return "warn", (
158+
"xhs-cli 已安装但状态异常。运行:\n"
159+
" xhs -v status 查看详细信息"
212160
)
213-
if r.returncode == 0 and _mcporter_status_ok(r.stdout):
214-
return "ok", "MCP 已连接(阅读、搜索、发帖、评论、点赞)"
215-
return "warn", "MCP 已配置,但连接异常;请检查 xiaohongshu-mcp 服务状态"
216-
except subprocess.TimeoutExpired:
217-
return "warn", "MCP 已配置,但健康检查超时;请检查 xiaohongshu-mcp 服务状态"
218161
except Exception:
219-
return "warn", "MCP 已配置,但连接异常;请检查 xiaohongshu-mcp 服务状态"
162+
return "warn", "xhs-cli 已安装但连接失败"

agent_reach/cli.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -871,30 +871,24 @@ def _install_mcporter():
871871
except Exception:
872872
print(" [!] Could not configure Exa. Run manually: mcporter config add exa https://mcp.exa.ai/mcp")
873873

874-
# Check XiaoHongShu MCP (only if server is running)
875-
try:
876-
r = subprocess.run(
877-
["mcporter", "config", "list"], capture_output=True, encoding="utf-8", errors="replace", timeout=5
878-
)
879-
if "xiaohongshu" in r.stdout:
880-
print(" ✅ XiaoHongShu MCP already configured")
881-
else:
882-
# Check if XHS MCP server is running on localhost:18060
883-
import requests
874+
# Check XiaoHongShu CLI
875+
if shutil.which("xhs"):
876+
print(" ✅ xhs-cli already installed (xiaohongshu-cli)")
877+
else:
878+
if shutil.which("pipx"):
884879
try:
885-
requests.get("http://localhost:18060/", timeout=3)
886880
subprocess.run(
887-
["mcporter", "config", "add", "xiaohongshu", "http://localhost:18060/mcp"],
888-
capture_output=True, encoding="utf-8", errors="replace", timeout=10,
881+
["pipx", "install", "xiaohongshu-cli"],
882+
capture_output=True, encoding="utf-8", errors="replace", timeout=120,
889883
)
890-
print(" ✅ XiaoHongShu MCP auto-detected and configured")
884+
if shutil.which("xhs"):
885+
print(" ✅ xhs-cli installed (run `xhs login` to authenticate)")
886+
else:
887+
print(" -- xhs-cli install failed (optional). Run: pipx install xiaohongshu-cli")
891888
except Exception:
892-
print(" -- XiaoHongShu MCP not detected (optional)")
893-
print(" Install: docker run -d --name xiaohongshu-mcp -p 18060:18060 xpzouying/xiaohongshu-mcp")
894-
print(" Then: mcporter config add xiaohongshu http://localhost:18060/mcp")
895-
print(" Repo: https://github.com/xpzouying/xiaohongshu-mcp")
896-
except Exception:
897-
pass
889+
print(" -- xhs-cli install failed (optional). Run: pipx install xiaohongshu-cli")
890+
else:
891+
print(" -- xhs-cli requires pipx (optional). Run: pipx install xiaohongshu-cli")
898892

899893

900894
def _install_mcporter_safe():

agent_reach/skill/references/social.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,35 @@
22

33
小红书、抖音、Twitter/X、微博、B站、V2EX、Reddit。
44

5-
## 小红书 / XiaoHongShu
5+
## 小红书 / XiaoHongShu (xhs-cli)
66

77
```bash
88
# 搜索笔记
9-
mcporter call 'xiaohongshu.search_feeds(keyword: "query")'
9+
xhs search "query"
1010

11-
# 获取笔记详情
12-
mcporter call 'xiaohongshu.get_feed_detail(feed_id: "xxx", xsec_token: "yyy")'
11+
# 阅读笔记详情
12+
xhs read NOTE_ID_OR_URL
1313

14-
# 获取笔记详情 + 评论
15-
mcporter call 'xiaohongshu.get_feed_detail(feed_id: "xxx", xsec_token: "yyy", load_all_comments: true)'
14+
# 查看评论
15+
xhs comments NOTE_ID_OR_URL
1616

17-
# 发布内容
18-
mcporter call 'xiaohongshu.publish_content(title: "标题", content: "正文", images: ["/path/img.jpg"], tags: ["tag"])'
17+
# 浏览热门
18+
xhs hot
19+
20+
# 推荐 feed
21+
xhs feed
22+
23+
# 用户主页
24+
xhs user USER_ID
25+
xhs user-posts USER_ID
26+
27+
# 发帖/互动
28+
xhs post --title "标题" --content "正文" --images img1.jpg img2.jpg
29+
xhs like NOTE_ID
30+
xhs comment NOTE_ID "评论内容"
1931
```
2032

21-
> **需要登录**: 使用 Cookie-Editor 浏览器插件导出 cookies。运行 `agent-reach doctor` 检查状态
33+
> **安装**: `pipx install xiaohongshu-cli`,然后 `xhs login`(自动从浏览器提取 Cookie)
2234
2335
## 抖音 / Douyin
2436

tests/test_channels.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -649,19 +649,32 @@ def fake_open(req, timeout=None):
649649

650650

651651
class TestXiaoHongShuChannel:
652-
def test_reports_ok_when_server_health_is_ok(self, monkeypatch):
653-
monkeypatch.setattr(shutil, "which", lambda _: "/opt/homebrew/bin/mcporter")
652+
def test_reports_ok_when_cli_authenticated(self, monkeypatch):
653+
monkeypatch.setattr(shutil, "which", lambda _: "/usr/local/bin/xhs")
654654

655655
def fake_run(cmd, **kwargs):
656-
if cmd[:4] == ["/opt/homebrew/bin/mcporter", "config", "get", "xiaohongshu"]:
657-
return subprocess.CompletedProcess(cmd, 0, '{"name":"xiaohongshu"}', "")
658-
if cmd[:4] == ["/opt/homebrew/bin/mcporter", "list", "xiaohongshu", "--json"]:
659-
return subprocess.CompletedProcess(cmd, 0, '{"status": "ok"}', "")
660-
raise AssertionError(f"unexpected command: {cmd}")
656+
return subprocess.CompletedProcess(cmd, 0, "ok: true\nusername: testuser\n", "")
661657

662658
monkeypatch.setattr(subprocess, "run", fake_run)
663659

664-
assert XiaoHongShuChannel().check() == (
665-
"ok",
666-
"MCP 已连接(阅读、搜索、发帖、评论、点赞)",
667-
)
660+
status, msg = XiaoHongShuChannel().check()
661+
assert status == "ok"
662+
assert "完整可用" in msg
663+
664+
def test_reports_warn_when_not_authenticated(self, monkeypatch):
665+
monkeypatch.setattr(shutil, "which", lambda _: "/usr/local/bin/xhs")
666+
667+
def fake_run(cmd, **kwargs):
668+
return subprocess.CompletedProcess(cmd, 1, "", "ok: false\nerror:\n code: not_authenticated\n")
669+
670+
monkeypatch.setattr(subprocess, "run", fake_run)
671+
672+
status, msg = XiaoHongShuChannel().check()
673+
assert status == "warn"
674+
assert "xhs login" in msg
675+
676+
def test_reports_off_when_not_installed(self, monkeypatch):
677+
monkeypatch.setattr(shutil, "which", lambda _: None)
678+
status, msg = XiaoHongShuChannel().check()
679+
assert status == "off"
680+
assert "xiaohongshu-cli" in msg

0 commit comments

Comments
 (0)