Content
# Xianyu MCP Server 🏪
Let AI Agents (like OpenCode) search for Xianyu second-hand item prices through the MCP protocol.
## ✨ Core Features
- **🔍 Search Xianyu Second-hand Prices** — Input keywords, get item title, price, region, and link
- **🍪 Automatic Cookie Detection** — No need to manually import cookies! Automatically try 4 methods in priority order:
1. **Automatically extract from Chrome browser** (if logged in to Xianyu, fully automatic, and seamless)
2. **Read from saved configuration file** (if logged in before, automatically reuse)
3. **Read from environment variables** (for server/CI scenarios)
4. **Pop up browser window for login** (for first-time use without cookies and with a graphical interface, only once)
- **🔄 One-time login, permanent use** — After interactive login success, cookies are automatically saved and reused
- **⚡ Based on Playwright** — Reliably render Xianyu SPA pages, accurately extract item information
## 📦 Installation
```bash
# 1. Install Python dependencies
pip install -r /home/hesheng/projects/xianyu-mcp/requirements.txt
# 2. Install Playwright Chromium browser
python -m playwright install chromium
```
## 🚀 Launch
```bash
# Method 1: Direct launch
mcp run /home/hesheng/projects/xianyu-mcp/main.py
# Method 2: Configure to OpenCode
```
Add the following to `opencode.jsonc`:
```jsonc
{
"mcpServers": {
"xianyu-mcp": {
"command": "mcp",
"args": ["run", "/home/hesheng/projects/xianyu-mcp/main.py"]
}
}
}
```
## 🎯 Usage Example
Ask OpenCode directly:
> 「Help me search for the second-hand price of **iPhone 15 Pro Max 256G** on Xianyu」
> 「**MacBook Air M3** is approximately how much on Xianyu」
> 「**Sony A7M4** second-hand market conditions」
The Agent will automatically call the `xianyu_search` tool and return:
| Title | Price | Region | Link |
|------|------|------|------|
| iPhone 15 Pro Max 256G Original Color 99 New | ¥8,299 | Shanghai | [View] |
| iPhone 15 Pro Max 256G National Bank in Warranty | ¥7,999 | Shenzhen | [View] |
## 🍪 Cookie Acquisition Methods (sorted by automation level)
### Method 1: Automatic Extraction from Chrome (fully automatic, recommended ✅)
If your Chrome browser has logged in to Xianyu, MCP will automatically extract the login state from Chrome's Cookie database, **no operation required**.
> **Note**: This method requires installing the `keyring` library to decrypt Chrome's encrypted Cookie:
> ```bash
> pip install keyring
> ```
> If `keyring` is not installed, MCP will automatically downgrade to Method 4.
### Method 2: Saved Configuration File (automatic reuse)
If you have logged in once through Method 4 before, the Cookie will be automatically saved to `~/.config/xianyu/cookies.json` and reused at the next launch, no need to log in again.
### Method 3: Environment Variables (server/headless environment)
```bash
export XIANYU_COOKIE='[{"name":"_m_h5_tk","value":"xxx"...}]'
```
Use the EditThisCookie plugin to export JSON and set it as an environment variable.
### Method 4: Pop-up Login (first-time use, with graphical interface)
If all above methods are unavailable and the system has a graphical interface, MCP will automatically open a browser window. After you scan the QR code or log in to Xianyu, the Cookie will be automatically captured and saved.
## 🔧 Self-testing
```bash
# Check syntax
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')"
# Test imports (only test parts without external dependencies)
python -c "from cookie_manager import CookieManager; print('CookieManager import successful')"
```
## 📁 Project Structure
```
xianyu-mcp/
├── main.py # MCP service entry, FastMCP defines tools
├── cookie_manager.py # Automatic Cookie detection (4 methods in chain try)
├── browser.py # Playwright browser manager (headless/headful dual mode)
├── search.py # Xianyu search logic (page loading → scrolling → DOM parsing)
├── __init__.py
├── requirements.txt
└── README.md
```
## ⚠️ Precautions
- For first-time use, if there are no cookies and a graphical interface is available, a browser window will pop up automatically
- Searching too frequently may trigger Xianyu's risk control, it is recommended to search at intervals of a few seconds
- Cookie validity period is about 1-3 days, need to log in again after expiration
MCP Config
Below is the configuration for this MCP Server. You can copy it directly to Cursor or other MCP clients.
mcp.json
Connection Info
You Might Also Like
Filesystem
Node.js MCP Server for filesystem operations with dynamic access control.
Fetch
Retrieve and process content from web pages by converting HTML into markdown format.
Agent-Reach
Give your AI agent eyes to see the entire internet. Read & search Twitter,...
Context 7
Context7 MCP provides up-to-date code documentation for any prompt.
context7-mcp
Context7 MCP Server provides natural language access to documentation for...
mempalace
The highest-scoring AI memory system ever benchmarked. And it's free.