Skill: alexduming/xhs-mcp-installer (version 1.0.0)

--- name: xhs-mcp-installer description: 一键安装并启动小红书 MCP 服务（xiaohongshu-mcp）。当用户说"帮我安装小红书MCP"、"安装 xhs-mcp"、"配置小红书 MCP"、"帮我搞个小红书"、或提供 GitHub 链接 https://github.com/xpzouying/xiaohongshu-mcp 时触发。自动检测系统、下载二进制、配置开机自启、启动服务。 metadata: {"openclaw": {"emoji": "📕", "requires": {"bins": ["curl"]}}} --- # 📕 小红书 MCP 一键安装器 自动完成以下工作： 1. 检测系统（macOS / Linux）并选择对应二进制 2. 下载 xhs-mcp 到 `~/xiaohongshu-mcp/` 3. 配置为后台服务（macOS: launchd，Linux: systemd） 4. 启动服务并验证健康状态 --- ## 一、安装前检查 ```bash # 检查是否已安装 lsof -i :18060 2>/dev/null | grep LISTEN && echo "✅ 已在运行" || echo "❌ 未运行" # 检查二进制是否存在 [ -f ~/xiaohongshu-mcp/xhs-mcp ] && echo "✅ 已下载" || echo "❌ 未下载" ``` --- ## 二、自动安装流程 ### 2.1 检测系统 ```bash OS="$(uname -s)" ARCH="$(uname -m)" echo "系统: $OS, 架构: $ARCH" ``` | 系统 | 架构 | 二进制名 | |------|------|----------| | Darwin (macOS) | x86_64 | xhs-mcp-darwin-amd64 | | Darwin (macOS) | arm64 | xhs-mcp-darwin-arm64 | | Linux | x86_64 | xhs-mcp-linux-amd64 | | Linux | aarch64 | xhs-mcp-linux-arm64 | ### 2.2 下载二进制 ```bash mkdir -p ~/xiaohongshu-mcp cd ~/xiaohongshu-mcp # 自动识别架构 case "$(uname -s)-$(uname -m)" in Darwin-x86_64) BINARY="xhs-mcp-darwin-amd64" ;; Darwin-arm64) BINARY="xhs-mcp-darwin-arm64" ;; Linux-x86_64) BINARY="xhs-mcp-linux-amd64" ;; Linux-aarch64) BINARY="xhs-mcp-linux-arm64" ;; *) echo "❌ 不支持的平台: $(uname -s) $(uname -m)"; exit 1 ;; esac echo "下载 $BINARY ..." curl -L "https://github.com/xpzouying/xiaohongshu-mcp/releases/latest/download/${BINARY}" \ -o ./xhs-mcp --progress-bar chmod +x ./xhs-mcp ``` ### 2.3 启动服务（手动测试） ```bash cd ~/xiaohongshu-mcp # 杀掉旧进程 lsof -ti :18060 | xargs kill -9 2>/dev/null sleep 1 # 启动 nohup ./xhs-mcp server --port 18060 > ~/xiaohongshu-mcp/mcp.log 2>&1 & sleep 3 # 验证 if lsof -i :18060 | grep LISTEN > /dev/null; then echo "✅ 服务启动成功，端口 18060 监听中" else echo "❌ 启动失败，查看日志：tail ~/xiaohongshu-mcp/mcp.log" tail -20 ~/xiaohongshu-mcp/mcp.log fi ``` ### 2.4 配置开机自启 #### macOS — launchd plist ```bash XHS_PLIST="$HOME/Library/LaunchAgents/com.openclaw.xhs-mcp.plist" mkdir -p "$HOME/Library/LaunchAgents" cat > "$XHS_PLIST" << 'EOFPLIST' <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.openclaw.xhs-mcp</string> <key>ProgramArguments</key> <array> <string>/Users/$(whoami)/xiaohongshu-mcp/xhs-mcp</string> <string>server</string> <string>--port</string> <string>18060</string> </array> <key>WorkingDirectory</key> <string>/Users/$(whoami)/xiaohongshu-mcp</string> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>StandardOutPath</key> <string>/Users/$(whoami)/xiaohongshu-mcp/mcp.log</string> <key>StandardErrorPath</key> <string>/Users/$(whoami)/xiaohongshu-mcp/mcp.log</string> <key>EnvironmentVariables</key> <dict/> </dict> </plist> EOFPLIST # 加载服务 launchctl unload "$XHS_PLIST" 2>/dev/null launchctl load "$XHS_PLIST" echo "✅ launchd 服务已加载" ``` #### Linux — systemd ```bash sudo tee /etc/systemd/system/xhs-mcp.service > /dev/null << 'EOFSYSTEMD' [Unit] Description=Xiaohongshu MCP Service After=network.target [Service] WorkingDirectory=/root/xiaohongshu-mcp ExecStart=/root/xiaohongshu-mcp/xhs-mcp server --port 18060 Restart=always RestartSec=5 StandardOutput=append:/root/xiaohongshu-mcp/mcp.log StandardError=append:/root/xiaohongshu-mcp/mcp.log [Install] WantedBy=multi-user.target EOFSYSTEMD sudo systemctl daemon-reload sudo systemctl enable xhs-mcp sudo systemctl start xhs-mcp echo "✅ systemd 服务已启动" ``` ### 2.5 创建看门狗脚本（自动恢复崩溃） ```bash cat > ~/xiaohongshu-mcp/watchdog.sh << 'EOFWATCHDOG' #!/bin/bash # xhs-mcp 看门狗：服务崩溃后自动重启 PORT=18060 LOG=~/xiaohongshu-mcp/watchdog.log check() { curl -s --max-time 3 http://localhost:$PORT/health > /dev/null 2>&1 } restart() { echo "$(date '+%Y-%m-%d %H:%M:%S') 重启服务..." >> "$LOG" lsof -ti :$PORT | xargs kill -9 2>/dev/null sleep 2 cd ~/xiaohongshu-mcp nohup ./xhs-mcp server --port $PORT >> ~/xiaohongshu-mcp/mcp.log 2>&1 & sleep 3 if check; then echo "$(date '+%Y-%m-%d %H:%M:%S') 重启成功" >> "$LOG" fi } if ! check; then restart fi EOFWATCHDOG chmod +x ~/xiaohongshu-mcp/watchdog.sh # macOS: 添加定期检查到 crontab (crontab -l 2>/dev/null | grep -v watchdog; echo "*/5 * * * * ~/xiaohongshu-mcp/watchdog.sh") | crontab - echo "✅ 看门狗已配置（每5分钟检查一次）" ``` --- ## 三、验证安装 ### 3.1 服务健康检查 ```bash # 检查进程 ps aux | grep -i xhs-mcp | grep -v grep # 检查端口 lsof -i :18060 | grep LISTEN # 检查登录状态（调用 MCP） MCP_URL="http://localhost:18060/mcp" SESSION_ID=$(curl -s -D /tmp/xhs_check -X POST "$MCP_URL" \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"openclaw","version":"1.0"}},"id":1}' > /dev/null \ && grep -i 'Mcp-Session-Id' /tmp/xhs_check | tr -d '\r' | awk '{print $2}') curl -s -X POST "$MCP_URL" -H "Content-Type: application/json" \ -H "Mcp-Session-Id: $SESSION_ID" \ -d '{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}' > /dev/null curl -s -X POST "$MCP_URL" -H "Content-Type: application/json" \ -H "Mcp-Session-Id: $SESSION_ID" \ -d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"check_login_status","arguments":{}},"id":2}' ``` ### 3.2 判断结果 | 输出 | 含义 | 操作 | |------|------|------| | `✅ 已在运行` + `✅ 已下载` | 已安装，跳过 | 静默完成 | | 服务启动成功 + 登录状态正常 | 全新安装完成 | 告知用户 | | 服务启动成功 + `❌ 未登录` | 需扫码登录 | 进入登录流程 | | ❌ 启动失败 | 下载或权限问题 | 查看日志排查 | --- ## 四、扫码登录（如需要） ### 方式一：直接获取二维码（推荐） ```bash MCP_URL="http://localhost:18060/mcp" SESSION_ID=$(curl -s -D /tmp/xhs_qr_h -X POST "$MCP_URL" \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"openclaw","version":"1.0"}},"id":1}' > /dev/null \ && grep -i 'Mcp-Session-Id' /tmp/xhs_qr_h | tr -d '\r' | awk '{print $2}') curl -s -X POST "$MCP_URL" -H "Content-Type: application/json" \ -H "Mcp-Session-Id: $SESSION_ID" \ -d '{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}' > /dev/null curl -s -X POST "$MCP_URL" -H "Content-Type: application/json" \ -H "Mcp-Session-Id: $SESSION_ID" \ -d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"get_login_qrcode","arguments":{}},"id":2}' | \ python3 -c " import sys,json d=json.load(sys.stdin) text=d.get('result',{}).get('content',[{}])[0].get('text','') # 提取base64图片 import re m=re.search(r'data:image/png;base64,([A-Za-z0-9+/=]+)', text) if m: open('/tmp/xhs_qr.png','wb').write(__import__('base64').b64decode(m.group(1))) print('二维码已保存到 /tmp/xhs_qr.png') else: print(text) " ``` ### 方式二：Cookie 导入（无需扫码） 用户提供浏览器 Cookie 字符串后，转换为 JSON 保存： ```bash python3 -c " import json, sys cookie_str = sys.argv[1].strip() cookies = [] for pair in cookie_str.split(';'): pair = pair.strip() if '=' not in pair: continue name, value = pair.split('=', 1) cookies.append({ 'name': name.strip(), 'value': value.strip(), 'domain': '.xiaohongshu.com', 'path': '/', 'expires': -1, 'httpOnly': name.strip() in ('web_session', 'id_token', 'acw_tc'), 'secure': name.strip() in ('web_session', 'id_token'), 'session': False, 'priority': 'Medium', 'sameParty': False, 'sourceScheme': 'Secure', 'sourcePort': 443 }) with open('$HOME/xiaohongshu-mcp/cookies.json', 'w') as f: json.dump(cookies, f, ensure_ascii=False) print(f'已保存 {len(cookies)} 个 Cookie') " '用户粘贴的cookie字符串' ``` --- ## 五、故障排查 ```bash # 服务状态 ps aux | grep xhs-mcp | grep -v grep lsof -i :18060 # 查看日志 tail -30 ~/xiaohongshu-mcp/mcp.log # 重启服务（macOS） launchctl unload ~/Library/LaunchAgents/com.openclaw.xhs-mcp.plist launchctl load ~/Library/LaunchAgents/com.openclaw.xhs-mcp.plist # 重启服务（Linux） sudo systemctl restart xhs-mcp # 卸载服务 launchctl unload ~/Library/LaunchAgents/com.openclaw.xhs-mcp.plist 2>/dev/null sudo systemctl disable xhs-mcp 2>/dev/null ``` --- ## 六、安装完成告知模板 安装成功后，告知用户： > ✅ **小红书 MCP 安装完成！** > > 🐹 服务已就绪，后台运行中 > 📍 端口：18060 > 🔄 开机自启：已配置 > 🐕 看门狗：每5分钟自动检查 > > 📌 下一步：用小红书APP扫码登录（告诉我就行，我会发二维码给你）

path: _meta.json
content: {
  "name": "xhs-mcp-installer",
  "version": "1.0.0",
  "description": "一键安装小红书 MCP 服务（xiaohongshu-mcp），支持 macOS/Linux，自动配置开机自启和看门狗。",
  "author": "alexduming",
  "homepage": "https://github.com/xpzouying/xiaohongshu-mcp",
  "tags": ["xiaohongshu", "mcp", "小红书", "安装"]
}


path: scripts/install.sh
content: #!/bin/bash
# xhs-mcp 一键安装脚本（macOS & Linux）
# 用法: bash <(curl -sSL https://raw.githubusercontent.com/xpzouying/xiaohongshu-mcp/main/scripts/install.sh)

set -e

OS="$(uname -s)"
ARCH="$(uname -m)"
HOME_DIR="$HOME"
PORT=18060
INSTALL_DIR="$HOME_DIR/xiaohongshu-mcp"

log() { echo "[$(date '+%H:%M:%S')] $1"; }

# ---- 检测架构选二进制 ----
case "$OS-$ARCH" in
  Darwin-x86_64)  ARCH_URL="xiaohongshu-mcp-darwin-amd64.tar.gz" ;;
  Darwin-arm64)   ARCH_URL="xiaohongshu-mcp-darwin-arm64.tar.gz" ;;
  Linux-x86_64)   ARCH_URL="xiaohongshu-mcp-linux-amd64.tar.gz" ;;
  Linux-aarch64)  ARCH_URL="xiaohongshu-mcp-linux-arm64.tar.gz" ;;
  *) log "❌ 不支持: $OS $ARCH"; exit 1 ;;
esac

mkdir -p "$INSTALL_DIR"

# ---- 检查是否已安装并运行 ----
if curl -s --max-time 2 http://localhost:$PORT/health > /dev/null 2>&1; then
  log "✅ 服务已在运行（端口 $PORT），跳过安装"
  exit 0
fi

# ---- 获取最新版本号 ----
log "🔍 查询最新版本..."
TAG=$(curl -sL "https://api.github.com/repos/xpzouying/xiaohongshu-mcp/releases/latest" | \
  python3 -c "import sys,json; d=json.load(sys.stdin); print(d['tag_name'])")
DOWNLOAD_URL="https://github.com/xpzouying/xiaohongshu-mcp/releases/download/${TAG}/${ARCH_URL}"
log "📦 版本: $TAG"

# ---- 下载 ----
log "⬇️  下载 $ARCH_URL ..."
cd "$INSTALL_DIR"
curl -L --progress-bar "$DOWNLOAD_URL" -o ./xhs-mcp.tar.gz

# 解压
log "📂 解压..."
tar xzf ./xhs-mcp.tar.gz
rm ./xhs-mcp.tar.gz

# 找二进制文件
BINARY=$(ls xiaohongshu-mcp-* 2>/dev/null | head -1 || echo "")
if [ -n "$BINARY" ]; then
  mv "$BINARY" ./xhs-mcp-bin
  chmod +x ./xhs-mcp-bin
else
  log "❌ 解压失败，未找到二进制文件"
  ls -la
  exit 1
fi

# ---- 启动服务 ----
log "🚀 启动服务 ..."
# 杀掉旧进程
python3 -c "
import socket, os, signal
try:
    s=socket.socket(); s.bind(('localhost',$PORT)); s.close()
except:
    import subprocess
    r=subprocess.run(['lsof','-ti',':$PORT'],capture_output=True,text=True)
    for pid in r.stdout.strip().split():
        try: os.kill(int(pid),9)
        except: pass
" 2>/dev/null || true

sleep 1
cd "$INSTALL_DIR"
nohup ./xhs-mcp-bin server --port $PORT > mcp.log 2>&1 &
MCP_PID=$!
sleep 3

# ---- 验证 ----
if curl -s --max-time 3 http://localhost:$PORT/health > /dev/null 2>&1; then
  log "✅ 服务启动成功（PID $MCP_PID），端口 $PORT"
else
  log "❌ 启动失败，查看: tail $INSTALL_DIR/mcp.log"
  tail -10 "$INSTALL_DIR/mcp.log"
  exit 1
fi

# ---- 开机自启 ----
if [ "$OS" = "Darwin" ]; then
  PLIST="$HOME_DIR/Library/LaunchAgents/com.openclaw.xhs-mcp.plist"
  mkdir -p "$HOME_DIR/Library/LaunchAgents"
  cat > "$PLIST" << PLISTEOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key><string>com.openclaw.xhs-mcp</string>
    <key>ProgramArguments</key>
    <array>
        <string>${INSTALL_DIR}/xhs-mcp-bin</string>
        <string>server</string>
        <string>--port</string>
        <string>${PORT}</string>
    </array>
    <key>WorkingDirectory</key><string>${INSTALL_DIR}</string>
    <key>RunAtLoad</key><true/>
    <key>KeepAlive</key><true/>
    <key>StandardOutPath</key><string>${INSTALL_DIR}/mcp.log</string>
    <key>StandardErrorPath</key><string>${INSTALL_DIR}/mcp.log</string>
</dict>
</plist>
PLISTEOF
  launchctl unload "$PLIST" 2>/dev/null || true
  launchctl load "$PLIST"
  log "✅ launchd 开机自启已配置"

elif [ "$OS" = "Linux" ]; then
  cat > /tmp/xhs-mcp.service << SERVICEEOF
[Unit]
Description=Xiaohongshu MCP Service
After=network.target

[Service]
WorkingDirectory=${INSTALL_DIR}
ExecStart=${INSTALL_DIR}/xhs-mcp-bin server --port ${PORT}
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
SERVICEEOF
  sudo mv /tmp/xhs-mcp.service /etc/systemd/system/xhs-mcp.service
  sudo systemctl daemon-reload
  sudo systemctl enable xhs-mcp
  sudo systemctl start xhs-mcp
  log "✅ systemd 开机自启已配置"
fi

# ---- 看门狗 ----
cat > "$INSTALL_DIR/watchdog.sh" << 'WATCHDOG'
#!/bin/bash
PORT=18060
LOG="$HOME/xiaohongshu-mcp/watchdog.log"
if ! curl -s --max-time 3 http://localhost:$PORT/health > /dev/null 2>&1; then
  echo "$(date '+%Y-%m-%d %H:%M:%S') 重启服务..." >> "$LOG"
  lsof -ti :$PORT | xargs kill -9 2>/dev/null
  sleep 2
  cd "$HOME/xiaohongshu-mcp"
  nohup ./xhs-mcp-bin server --port $PORT >> mcp.log 2>&1 &
fi
WATCHDOG
chmod +x "$INSTALL_DIR/watchdog.sh"

# macOS crontab 看门狗
if [ "$OS" = "Darwin" ]; then
  (crontab -l 2>/dev/null | grep -v watchdog; echo "*/5 * * * * $INSTALL_DIR/watchdog.sh") | crontab -
  log "✅ 看门狗已配置（每5分钟检查）"
fi

log ""
log "========================================="
log "  ✅ 小红书 MCP 安装完成！"
log "  📍 端口: $PORT"
log "  📁 目录: $INSTALL_DIR"
log "  🔄 服务管理:"
log "     macOS: launchctl unload/load $HOME/Library/LaunchAgents/com.openclaw.xhs-mcp.plist"
log "     Linux:  sudo systemctl restart xhs-mcp"
log "========================================="
log ""
log "📌 下一步：告诉我就行，我会发二维码给你扫码登录 🐹"


Security scan verdicts — ClawScan: suspicious | static analysis: clean | VirusTotal: clean | SkillSpector: suspicious (CRITICAL)