Content
# Xiaohongshu Automatic Comment Search Tool (MCP Server 2.0)
> This project is based on [JonaFly/RednoteMCP](https://github.com/JonaFly/RednoteMCP.git) and has been comprehensively optimized and functionally expanded based on multiple practical experiences (by windsurf). Sincere thanks to the original author for their contributions!
This is an automatic search and comment tool for Xiaohongshu developed using 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.
## Main Features and Advantages
- **Deep Integration of AI Capabilities**: Utilizes the large model capabilities of the MCP client (such as Claude) to generate more natural and relevant comment content.
- **Modular Design**: Divides functions into three independent modules: note analysis, comment generation, and comment publishing, improving code maintainability.
- **Powerful Content Retrieval Capabilities**: Integrates various methods for retrieving note content, ensuring complete acquisition of titles, authors, and body content of various notes.
- **Persistent Login**: Uses a persistent browser context, eliminating the need for repeated logins after the first login.
- **Two-Step Comment Process**: First retrieves note analysis results, then generates and publishes comments using the MCP client.
## Major Optimizations in Version 2.0
- **Enhanced Content Retrieval**: Refactored the note content retrieval module, increasing page load wait times and scroll operations, implementing four different content retrieval methods.
- **AI Comment Generation**: Refactored the comment function to return note analysis results to the MCP client, allowing the client's AI capabilities to generate more natural and relevant comments.
- **Functional Modularization**: Divided functions 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.
- **Enhanced Error Handling**: Added more detailed error handling and debugging information output.
## I. Core Functions
### 1. User Authentication and Login
- **Persistent Login**: Supports manual QR code login, saving the state after the first login, so no need to scan again for subsequent uses.
- **Login Status Management**: Automatically detects login status and prompts the user to log in when necessary.
### 2. Content Discovery and Retrieval
- **Intelligent Keyword Search**: Supports multi-keyword searches, allows specifying the number of results returned, and provides complete note information.
- **Multi-Dimensional Content Retrieval**: Integrates four different retrieval methods to ensure accurate acquisition of note titles, authors, publication times, and body content.
- **Comment Data Retrieval**: Supports retrieving comment content for notes, including commenters, comment text, and time information.
### 3. Content Analysis and Generation
- **Note Content Analysis**: Automatically analyzes note content, extracts key information, and identifies the field to which the note belongs.
- **AI Comment Generation**: Utilizes the AI capabilities of the MCP client (such as Claude) to generate natural and relevant comments based on note content.
- **Support for Multiple Comment Types**: Supports generating four different types of comments:
- **Traffic Generation**: Guides users to follow or message privately.
- **Like Type**: Simple interaction to gain favor.
- **Inquiry Type**: Increases interaction in the form of questions.
- **Professional Type**: Demonstrates expertise to establish authority.
### 4. Data Return and Feedback
- **Structured Data Return**: Returns note analysis results in JSON format to the MCP client, facilitating AI comment generation.
- **Comment Publishing Feedback**: Provides real-time feedback on comment publishing results.
## II. Installation Steps
1. **Prepare Python Environment**: Ensure Python 3.8 or higher is installed on your system. If not, download and install it from the official Python website.
2. **Obtain the Project**: Clone or download this project to your local machine.
3. **Create a 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 the MCP Server:
```json
{
"mcpServers": {
"xiaohongshu MCP": {
"command": "/absolute/path/to/venv/bin/python3",
"args": [
"/absolute/path/to/xiaohongshu_mcp.py",
"--stdio"
]
}
}
}
```
> **Important Note**:
> - Please use the **full absolute path** of the Python interpreter in the virtual environment.
> - For example: `/Users/username/Desktop/RedBook-Search-Comment-MCP/venv/bin/python3`
> - Similarly, `xiaohongshu_mcp.py` also needs to use the **full absolute path**.
## IV. Usage Instructions
### (I) Start the Server
1. **Run Directly**: In the project directory, activate the virtual environment and execute:
```bash
python3 xiaohongshu_mcp.py
```
2. **Start via MCP Client**: After configuring the MCP Client, follow the client’s operational process to start and connect.
### (II) Main Functional Operations
After connecting to the server in the MCP Client (such as Claude for Desktop), you can use the following functions:
### 1. Log in to Xiaohongshu
**Tool 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**: The first time you use it, 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 state.
### 2. Search Notes
**Tool Function**:
```
mcp0_search_notes(keywords="keyword", limit=5)
```
**Usage in MCP Client**:
Send a search request containing keywords:
```
Help me search Xiaohongshu notes with the keyword: food
```
Specify the number of results:
```
Help me search Xiaohongshu notes with the keyword travel, return 10 results
```
**Function Description**: Searches Xiaohongshu notes based on keywords and returns the specified number of results. By default, it returns 5 results.
### 3. Get Note Content
**Tool Function**:
```
mcp0_get_note_content(url="note URL")
```
**Usage in MCP Client**:
Send a request containing the note URL:
```
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**: Retrieves detailed content of the specified note URL, including title, author, publication time, and body content.
### 4. Get Note Comments
**Tool Function**:
```
mcp0_get_note_comments(url="note URL")
```
**Usage in MCP Client**:
Send a request containing the note URL for comments:
```
Help me get the comments for this note: https://www.xiaohongshu.com/search_result/xxxx
```
or:
```
Please check the comment section of this Xiaohongshu note: https://www.xiaohongshu.com/search_result/xxxx
```
**Function Description**: Retrieves comment information for the specified note URL, including commenters, comment content, and comment time.
### 5. Publish Smart Comments
**Tool 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 note analysis results and returns them to the MCP client, which generates comments and calls `post_comment` to publish.
### 6. Publish Comments
**Tool 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 publish 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.
## V. User Guide
### 0. Working Principle
This tool implements the smart comment function through a two-step process:
1. **Note Analysis**: Calls 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 note analysis results.
- Calls the `post_comment` tool to publish comments.
This design fully utilizes 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** | `Help me search for Xiaohongshu notes about [keyword]` |
| **Get Note Content** | `Help me check the content of this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx` |
| **Analyze Note** | `Help me analyze this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx` |
| **Get Comments** | `Help me check the comments for this note: https://www.xiaohongshu.com/explore/xxxx` |
| **Generate Comment** | `Help me write a [type] comment for this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx` |
#### Comment Type Options
| Type | Description | Applicable Scenarios |
|---------|------|----------|
| **Traffic Generation** | Guides users to follow or message privately | Increase followers or private message interactions |
| **Like Type** | Simple interaction to gain favor | Increase exposure and interaction rate |
| **Inquiry Type** | Increases interaction in the form of questions | Provokes responses from the author, increasing interaction depth |
| **Professional Type** | Demonstrates expertise to establish authority | Establishes a professional image, enhancing credibility |
### 2. Example of Actual Workflow
```
User: Help me write a professional type comment for this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx
Claude: I will help you write a professional type comment. Let me get the note content and generate the comment.
[Calling post_smart_comment tool]
# The tool returns the note analysis results, including title, author, content, field, and keywords.
Claude: I have obtained the note information; this is a note about [topic]. Based on the content, I generated and published the following professional comment:
"[Generated professional comment content]"
[Calling post_comment tool]
Claude: The comment has been successfully published!
```
**Note**: In the above workflow, the `post_smart_comment` tool is only responsible for retrieving the note analysis results and returning them to the MCP client; the actual comment generation is completed by the MCP client (such as Claude) itself.
### 3. Working Principle
The new Xiaohongshu MCP tool adopts a modular design, divided into three core modules:
1. **Note Analysis Module** (`analyze_note`)
- Retrieves the note's title, author, publication time, and content.
- Analyzes the note's field and keywords.
- Returns structured note information.
2. **Comment Generation Module** (implemented by the MCP client)
- Receives note analysis results.
- Generates natural and relevant comments based on note content and comment type.
- Allows users to preview and modify comments before publishing.
3. **Comment Publishing Module** (`post_comment`)
- Receives the generated comment content.
- Locates and interacts with the comment input box.
- Publishes comments and returns results.
## VI. Code Structure
- **xiaohongshu_mcp.py**: The core file implementing the main functionalities, including login, search, content retrieval, comment retrieval, and comment publishing.
- **requirements.txt**: Records the dependencies required for the project.
## VII. Common Issues and Solutions
1. **Connection Failure**:
- Ensure you are using the **full absolute path** of the Python interpreter in the virtual environment.
- Ensure the MCP server is running.
- Try restarting the MCP server and 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 a `ModuleNotFoundError` error:
- Ensure all dependencies are installed in the virtual environment.
- Check if the fastmcp package is installed.
## VIII. Notes and Problem Solving
### 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 uses do not require rescanning if the login state is valid.
- **Platform Rules**: Please strictly adhere to Xiaohongshu platform regulations during use to avoid excessive operations and prevent account bans.
- **Comment Frequency**: It is recommended to control the frequency of comment publishing, avoiding the publication of a large number of comments in a short time, with a maximum of 30 comments per day.
### 2. Common Issues and Solutions
#### Browser Instance Issues
If you encounter errors like “Page.goto: Target page, context or browser has been closed,” it may be due to the browser instance not closing properly or issues 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 problem 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 cannot retrieve note content or the content is incomplete, you can try:
1. **Increase Wait Time**: Xiaohongshu note pages may require longer loading times, especially for notes with many images or videos.
2. **Clear Browser Cache**: Sometimes, browser cache can affect content retrieval.
3. **Try Different Retrieval Methods**: The tool integrates various retrieval methods; if one method fails, you can try others.
#### Adapting to Platform Changes
Xiaohongshu may update its page structure and DOM elements, causing the tool to malfunction. If you encounter such issues:
1. **Check Project Updates**: Stay updated with 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 CSS selectors or XPath expressions.
3. **Submit Issue Feedback**: Report issues to the project maintainers, describing the specific problems encountered and page changes.
## IX. Disclaimer
This tool is intended for learning and research purposes only. Users should strictly comply with relevant laws and regulations as well as Xiaohongshu platform rules. The developers of this project bear no responsibility for any issues arising from improper use.