xianyu-mcp

xianyu-mcp

Enables AI agents to search Xianyu second-hand marketplace for product prices with automatic cookie detection.

Category
Visit Server

README

闲鱼 MCP Server 🏪

让 AI Agent(如 OpenCode)能够通过 MCP 协议搜索闲鱼二手商品价格。

✨ 核心特性

  • 🔍 搜索闲鱼二手价格 — 输入关键词,获取商品标题、价格、地区、链接
  • 🍪 Cookie 全自动检测 — 不再需要手动导 Cookie!按优先级自动尝试 4 种方式:
    1. 从 Chrome 浏览器自动提取(已登录闲鱼时,全自动,无感)
    2. 从已保存的配置文件读取(之前登录过,自动复用)
    3. 从环境变量读取(服务器/CI 场景)
    4. 弹出浏览器窗口让您登录(首次无 Cookie + 有图形界面时,只需一次)
  • 🔄 一次登录,永久使用 — 交互式登录成功后,Cookie 自动保存复用
  • ⚡ 基于 Playwright — 可靠渲染闲鱼 SPA 页面,精确提取商品信息

📦 安装

# 1. 安装 Python 依赖
pip install -r /home/hesheng/projects/xianyu-mcp/requirements.txt

# 2. 安装 Playwright Chromium 浏览器
python -m playwright install chromium

🚀 启动

# 方式一:直接启动
mcp run /home/hesheng/projects/xianyu-mcp/main.py

# 方式二:配置到 OpenCode

opencode.jsonc 中添加:

{
  "mcpServers": {
    "xianyu-mcp": {
      "command": "mcp",
      "args": ["run", "/home/hesheng/projects/xianyu-mcp/main.py"]
    }
  }
}

🎯 使用示例

在 OpenCode 中直接问:

「帮我搜一下闲鱼上 iPhone 15 Pro Max 256G 二手什么价」 「MacBook Air M3 在闲鱼上大概多少钱」 「索尼 A7M4 二手行情怎么样」

Agent 会自动调用 xianyu_search 工具,返回:

标题 价格 地区 链接
iPhone 15 Pro Max 256G 原色 99新 ¥8,299 上海 [查看]
iPhone 15 Pro Max 256G 国行在保 ¥7,999 深圳 [查看]

🍪 Cookie 获取方式(按自动化程度排序)

方式 1:从 Chrome 自动提取(全自动,推荐 ✅)

如果您的 Chrome 浏览器已经登录过闲鱼,MCP 会自动从 Chrome 的 Cookie 数据库中提取登录态,无需任何操作

注意:此方式需要安装 keyring 库以解密 Chrome 的加密 Cookie:

pip install keyring

如果未安装 keyring,MCP 会自动降级到方式 4。

方式 2:已保存的配置文件(自动复用)

如果您之前通过方式 4 登录过一次,Cookie 会自动保存到 ~/.config/xianyu/cookies.json,后续启动时自动复用,无需再次登录。

方式 3:环境变量(服务器/无界面环境)

export XIANYU_COOKIE='[{"name":"_m_h5_tk","value":"xxx"...}]'

用 EditThisCookie 插件导出 JSON 后,将内容设为环境变量。

方式 4:弹窗登录(首次使用,有图形界面时)

如果以上方式都不可用,且系统有图形界面,MCP 会自动打开一个浏览器窗口,您扫码或登录闲鱼后,Cookie 自动捕获并保存。

🔧 自测试

# 检查语法
python -c "import ast; ast.parse(open('/home/hesheng/projects/xianyu-mcp/main.py').read()); print('Syntax OK')"
python -c "import ast; ast.parse(open('/home/hesheng/projects/xianyu-mcp/cookie_manager.py').read()); print('Syntax OK')"
python -c "import ast; ast.parse(open('/home/hesheng/projects/xianyu-mcp/browser.py').read()); print('Syntax OK')"
python -c "import ast; ast.parse(open('/home/hesheng/projects/xianyu-mcp/search.py').read()); print('Syntax OK')"

# 测试导入(仅测试无外部依赖的部分)
python -c "from cookie_manager import CookieManager; print('CookieManager 导入成功')"

📁 项目结构

xianyu-mcp/
├── main.py              # MCP 服务入口,FastMCP 定义 tools
├── cookie_manager.py    # Cookie 自动检测(4 种方式链式尝试)
├── browser.py           # Playwright 浏览器管理器(headless/headful 双模式)
├── search.py            # 闲鱼搜索逻辑(页面加载 → 滚动 → DOM 解析)
├── __init__.py
├── requirements.txt
└── README.md

⚠️ 注意事项

  • 首次使用时,如果无 Cookie 且有图形界面,会自动弹出浏览器窗口
  • 搜索过频繁可能触发闲鱼风控,建议间隔几秒再搜
  • Cookie 有效期约 1-3 天,过期后需要重新登录

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured