Content
# Xiaohongshu Automatic Comment Search Tool (MCP Server 2.0)
<div align="right">
[English](README_EN.md) | 中文
</div>
> This project is based on [JonaFly/RednoteMCP](https://github.com/JonaFly/RednoteMCP.git) and has been comprehensively optimized and functionally expanded (by windsurf) based on multiple practical experiences. Sincere thanks to the original author for their contributions!
This is an automatic search and comment tool for Xiaohongshu developed based on Playwright. As an MCP Server, it can connect to the MCP Client (such as Claude for Desktop) through specific configurations, helping users automatically log in to Xiaohongshu, search for keywords, retrieve note content, and publish AI-generated comments.
## Key Features and Advantages
- **Deep Integration of AI Capabilities**: Leverage the large model capabilities of MCP clients (such as Claude) to generate more natural and relevant comment content.
- **Modular Design**: Functions are divided into three independent modules: note analysis, comment generation, and comment publishing, enhancing code maintainability.
- **Powerful Content Acquisition**: Integrates various methods to obtain note content, ensuring complete retrieval of titles, authors, and body content for all types of notes.
- **Persistent Login**: Utilizes persistent browser context, eliminating the need for repeated logins after the first login.
- **Two-Step Comment Process**: First, obtain the note analysis results, then generate and publish comments using the MCP client.
## Major Optimizations in Version 2.0
- **Content Acquisition Enhancement**: Refactored the note content acquisition module, increased page load wait time and scrolling operations, and implemented four different content acquisition methods.
- **AI Comment Generation**: Refactored the comment feature to return note analysis results to the MCP client, allowing the client's AI capabilities to generate more natural and relevant comments.
- **Modular Functionality**: Divided functionalities into three independent modules: note analysis, comment generation, and comment publishing, improving code maintainability.
- **Search Result Optimization**: Resolved the issue of titles not displaying when searching for notes, providing more complete search results.
- **Error Handling Enhancement**: Added more detailed error handling and debugging information output.
## 1. Core Features
### 1. User Authentication and Login
- **Persistent Login**: Supports manual QR code login, saves the state after the first login, and does not require repeated scanning for subsequent uses.
- **Login Status Management**: Automatically detects login status and prompts the user to log in when necessary.
### 2. Content Discovery and Acquisition
- **Intelligent Keyword Search**: Supports multi-keyword search, allows specification of the number of results returned, and provides complete note information.
- **Multi-Dimensional Content Acquisition**: Integrates four different acquisition methods to ensure accurate retrieval of note titles, authors, publication times, and main content.
- **Comment Data Acquisition**: Supports the retrieval of comment content for notes, including commenter, comment text, and time information.
### 3. Content Analysis and Generation
- **Note Content Analysis**: Automatically analyze note content, extract key information, and identify the domain of the notes.
- **AI Comment Generation**: Utilize the AI capabilities of the MCP client (such as Claude) to generate natural and relevant comments based on the note content.
- **Multi-Type Comment Support**: Supports the generation of four different types of comments:
- **Traffic Generation**: Guide users to follow or message privately.
- **Like-Type**: Simple interactions to gain favor.
- **Consultation-Type**: Increase interaction in the form of questions.
- **Professional-Type**: Showcase expertise to establish authority.
### 4. Data Return and Feedback
- **Structured Data Return**: Return the note analysis results in JSON format to the MCP client, facilitating AI-generated comments.
- **Comment Publishing Feedback**: Provide real-time feedback on the comment publishing results.
## II. Installation Steps
1. **Prepare Python Environment**: Ensure that Python 3.8 or higher is installed on the system. If not installed, you can download and install it from the official Python website.
2. **Obtain the Project**: Clone or download this project to your local machine.
3. **Create Virtual Environment**: Create and activate a virtual environment in the project directory (recommended):
```bash
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activate
```
4. **Install Dependencies**: Install the required dependencies in the activated virtual environment:
```bash
pip install -r requirements.txt
pip install fastmcp
```
5. **Install Browsers**: Install the browsers required by Playwright:
```bash
playwright install
```
## III. MCP Server Configuration
Add the following content to the configuration file of the MCP Client (such as Claude for Desktop) to configure this tool as an MCP Server:
### Mac Configuration Example
```json
{
"mcpServers": {
"xiaohongshu MCP": {
"command": "/absolute/path/to/venv/bin/python3",
"args": [
"/absolute/path/to/xiaohongshu_mcp.py",
"--stdio"
]
}
}
}
```
### Windows Configuration Example
```json
{
"mcpServers": {
"xiaohongshu MCP": {
"command": "C:\\Users\\username\\Desktop\\MCP\\Redbook-Search-Comment-MCP2.0\\venv\\Scripts\\python.exe",
"args": [
"C:\\Users\\username\\Desktop\\MCP\\Redbook-Search-Comment-MCP2.0\\xiaohongshu_mcp.py",
"--stdio"
]
}
}
}
```
> **Important Note**:
> - Please use the **full absolute path** of the Python interpreter in the virtual environment.
> - Mac example: `/Users/username/Desktop/RedBook-Search-Comment-MCP/venv/bin/python3`
> - Windows example: `C:\Users\username\Desktop\MCP\Redbook-Search-Comment-MCP2.0\venv\Scripts\python.exe`
> - Similarly, xiaohongshu_mcp.py also needs to use the **full absolute path**.
> - Backslashes in Windows paths need to be escaped twice in JSON (use `\`).
### Python Command Distinction (python vs python3)
The Python command may vary across different system environments, depending on your system configuration. Here’s how to determine which command you should use:
1. **Determine Your Python Command**:
- Run in the terminal: `python --version` and `python3 --version`
- Check which command returns the Python 3.x version (this project requires Python 3.8+)
2. **Confirm in the Virtual Environment**:
- After activating the virtual environment, run `which python` or `where python` (Windows)
- This will display the full path of the Python interpreter
3. **Use the Correct Command in Configuration**:
- Mac: Typically `python3` or `python` in the virtual environment
- Windows: Typically `python` or `python.exe`
In the configuration file, always use the **full absolute path** of the Python interpreter in the virtual environment, rather than the command name.
## 4. Usage Instructions
### (1) Starting the Server
1. **Direct Execution**: In the project directory, activate the virtual environment and execute:
```bash
python3 xiaohongshu_mcp.py
```
2. **Starting via MCP Client**: After configuring the MCP Client, follow the client’s operational process to start and connect.
### (2) Main Function Operations
After connecting to the server in MCP Client (such as Claude for Desktop), the following functions can be used:
### 1. Log in to Xiaohongshu
**Utility Function**:
```
mcp0_login()
```
**Usage in MCP Client**:
Simply send the following text:
```
Help me log in to my Xiaohongshu account
```
or:
```
Please log in to Xiaohongshu
```
**Function Description**: On the first use, a browser window will open, waiting for the user to manually scan the QR code to log in. After a successful login, the tool will save the login status.
### 2. Search Notes
**Utility Function**:
```
mcp0_search_notes(keywords="关键词", limit=5)
```
**Usage in MCP Client**:
Send a search request containing keywords:
```
Please help me search for Xiaohongshu notes with the keyword: food
```
Specify the number of results to return:
```
Please help me search for Xiaohongshu notes with the keyword: travel, return 10 results
```
**Function Description**: Search for Xiaohongshu notes based on keywords and return the specified number of results. The default is to return 5 results.
### 3. Get Note Content
**Utility Function**:
```
mcp0_get_note_content(url="Note URL")
```
**Usage in MCP Client**:
Send a request containing the note URL:
```
Please help me get the content of this note: https://www.xiaohongshu.com/search_result/xxxx
```
or:
```
Please check the content of this Xiaohongshu note: https://www.xiaohongshu.com/search_result/xxxx
```
**Function Description**: Retrieve detailed content of the specified note URL, including title, author, publication time, and main content.
### 4. Get Note Comments
**Utility Function**:
```
mcp0_get_note_comments(url="Note URL")
```
**Usage in MCP Client**:
Send a comment request containing the note URL:
```
Please help me get the comments for this note: https://www.xiaohongshu.com/search_result/xxxx
```
or:
```
Please check the comments section of this Xiaohongshu note: https://www.xiaohongshu.com/search_result/xxxx
```
**Function Description**: Retrieve comment information for the specified note URL, including the commenter, comment content, and comment time.
### 5. Publish Smart Comments
**Utility Function**:
```
mcp0_post_smart_comment(url="Note URL", comment_type="Comment Type")
```
**Usage in MCP Client**:
Send a request containing the note URL and comment type:
```
Help me write a [type] comment for this note: https://www.xiaohongshu.com/explore/xxxx
```
**Function Description**: Retrieves the analysis results of the note and returns them to the MCP client, which generates the comment and calls post_comment to publish it.
### 6. Publish Comment
**Utility Function**:
```
mcp0_post_comment(url="Note URL", comment="Comment content")
```
**Usage in MCP Client**:
Send a request containing the note URL and comment content:
```
Help me post this comment to the note: https://www.xiaohongshu.com/explore/xxxx
Comment content: [Comment content]
```
**Function Description**: Publishes the specified comment content to the note page.
## IV. User Guide
### 0. Working Principle
This tool implements the smart commenting feature through a two-step process:
1. **Note Analysis**: Call the `post_smart_comment` tool to retrieve note information (title, author, content, etc.)
2. **Comment Generation and Publishing**:
- The MCP client (such as Claude) generates comments based on the results of the note analysis.
- Call the `post_comment` tool to publish the comments.
This design fully leverages the AI capabilities of the MCP client to achieve more natural and relevant comment generation.
### 1. Usage in MCP Client
#### Basic Operations
| Function | Example Command |
|---------|----------|
| **Search Notes** | `帮我搜索关于[关键词]的小红书笔记` |
| **Get Note Content** | `帮我查看这篇小红书笔记的内容:https://www.xiaohongshu.com/explore/xxxx` |
| **Analyze Note** | `帮我分析这篇小红书笔记:https://www.xiaohongshu.com/explore/xxxx` |
| **Get Comments** | `帮我查看这篇笔记的评论:https://www.xiaohongshu.com/explore/xxxx` |
| **Generate Comment** | `帮我为这篇小红书笔记写一条[类型]评论:https://www.xiaohongshu.com/explore/xxxx` |
#### Comment Type Options
| Type | Description | Applicable Scenarios |
|----------|--------------------------------------|------------------------------------|
| **Traffic** | Guide users to follow or message privately | Increase followers or private message interactions |
| **Like** | Simple interaction to gain favor | Increase exposure and interaction rate |
| **Consultation** | Increase interaction in the form of questions | Prompt blogger replies, enhance interaction depth |
| **Professional** | Showcase expertise to establish authority | Build a professional image, enhance credibility |
### 2. Example of Actual Workflow
```
User: Please help me write a professional comment for this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx
Claude: I will help you write a professional comment. Let me fetch the note content and generate the comment.
[Calling post_smart_comment tool]
```
# The tool returns note analysis results, including title, author, content, domain, and keywords
Claude: I have retrieved the note information, and this is a note about [topic]. Based on the content, I have generated and published the following professional comment:
"[Generated professional comment content]"
[Call post_comment tool]
Claude: The comment has been successfully published!
```
**Note**: In the above process, the `post_smart_comment` tool is only responsible for obtaining the note analysis results and returning them to the MCP client, while the actual comment generation is completed by the MCP client (such as Claude) itself.
### 3. Working Principle
The new version of the Xiaohongshu MCP tool adopts a modular design, divided into three core modules:
1. **Note Analysis Module** (analyze_note)
- Retrieves the title, author, publication time, and content of the note
- Analyzes the field and keywords of the note
- Returns structured note information
2. **Comment Generation Module** (implemented by the MCP client)
- Receives the note analysis results
- Generates natural and relevant comments based on the note content and comment type
- Allows users to preview and modify comments before publishing
3. **Comment Posting Module** (post_comment)
- Receives the generated comment content
- Locates and interacts with the comment input box
- Publishes the comment and returns the result
## V. Code Structure
- **xiaohongshu_mcp.py**: The core file that implements the main functionalities, containing the code logic for login, search, retrieving content and comments, posting comments, and more.
- **requirements.txt**: Records the dependency libraries required for the project.
## VI. Common Issues and Solutions
1. **Connection Failure**:
- Ensure that you are using the **full absolute path** of the Python interpreter in the virtual environment.
- Make sure the MCP Server is running.
- Try restarting the MCP Server and the client.
2. **Browser Session Issues**:
If you encounter the `Page.goto: Target page, context or browser has been closed` error:
- Restart the MCP Server.
- Reconnect and log in again.
3. **Dependency Installation Issues**:
If you encounter the `ModuleNotFoundError` error:
- Ensure that all dependencies are installed in the virtual environment.
- Check if the fastmcp package is installed.
## 7. Notes and Troubleshooting
### 1. Usage Notes
- **Browser Mode**: The tool runs in Playwright's non-headless mode, opening a real browser window during execution.
- **Login Method**: The first login requires manual QR code scanning; subsequent logins do not require scanning again if the login status is valid.
- **Platform Rules**: Please strictly adhere to the relevant regulations of the Xiaohongshu platform during use to avoid excessive operations and prevent the risk of account suspension.
- **Comment Frequency**: It is recommended to control the frequency of comment posting, avoiding the publication of a large number of comments in a short period, with a daily limit of no more than 30 comments.
### 2. Frequently Asked Questions and Solutions
#### Browser Instance Issues
If you encounter an error like “Page.goto: Target page, context or browser has been closed,” it may be due to the browser instance not being closed properly or an issue with the data directory lock file. Please try:
```bash
# Delete Browser Lock Files
rm -f /project_path/browser_data/SingletonLock /project_path/browser_data/SingletonCookie
# If the issue persists, back up and rebuild the browser data directory
mkdir -p /project_path/backup_browser_data
mv /project_path/browser_data/* /project_path/backup_browser_data/
mkdir -p /project_path/browser_data
```
#### Content Retrieval Issues
If you are unable to retrieve note content or the content is incomplete, you can try:
1. **Increase the waiting time**: The Xiaohongshu (Little Red Book) note page may require a longer loading time, especially for notes that contain a large number of images or videos.
2. **Clear browser cache**: Sometimes, the browser cache can affect content retrieval.
3. **Try different retrieval methods**: The tool integrates multiple retrieval methods; if one method fails, you can try other methods.
#### Platform Change Adaptation
The Xiaohongshu platform may update its page structure and DOM elements, causing the tool to malfunction. If you encounter such issues:
1. **Check for Project Updates**: Keep an eye on the latest version of the project and update in a timely manner.
2. **Adjust Selectors**: If you are familiar with the code, you can try adjusting the CSS selectors or XPath expressions.
3. **Submit Issue Feedback**: Report the issue to the project maintainers, describing the specific problems encountered and the changes in the page.
## VIII. Disclaimer
This tool is intended for learning and research purposes only. Users must strictly comply with relevant laws and regulations as well as the rules of the Xiaohongshu platform. The developers of this project bear no responsibility for any issues arising from improper use.
Connection Info
You Might Also Like
Git
Model Context Protocol Servers
TrendRadar
🎯 Say goodbye to information overload. AI helps you understand news hotspots...
repomix
📦 Repomix is a powerful tool that packs your entire repository into a...
n8n-workflow-builder
AI assistant integration for n8n workflow automation through Model Context...
llm-mcp-rag
LLM + MCP + RAG = Magic
ReAct-MCP
ReAct + MCP = Magic