这是一款基于 Playwright 开发的小红书自动搜索和评论工具,作为 MCP Server,可通过特定配置接入 MCP Client(如Claude for Desktop),帮助用户自动完成登录小红书、搜索关键词、获取笔记内容及发布AI生成评论等操作。
6.8k 查看 · 2026-07-07 更新
简介
这是一款基于 Playwright 开发的小红书自动搜索和评论工具,作为 MCP Server,可通过特定配置接入 MCP Client(如Claude for Desktop),帮助用户自动完成登录小红书、搜索关键词、获取笔记内容及发布AI生成评论等操作。
简介
这是一款基于 Playwright 开发的小红书自动搜索和评论工具,作为 MCP Server,可通过特定配置接入 MCP Client(如Claude for Desktop),帮助用户自动完成登录小红书、搜索关键词、获取笔记内容及发布AI生成评论等操作。
Xiaohongshu Automatic Search and Comment Tool (MCP Server 2.0)
English | Chinese
This project is based on JonaFly/RednoteMCP and has been comprehensively optimized and functionally expanded with practical experience (by windsurf). Sincere thanks to the original author for their contributions!
This is a Xiaohongshu automatic search and comment tool developed using Playwright, serving as an MCP Server that can be connected to an MCP Client (such as Claude for Desktop) through specific configurations. It helps users automatically complete actions such as logging into Xiaohongshu, searching for keywords, obtaining note content, and posting AI-generated comments.
Key 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 functionality into three independent modules—note analysis, comment generation, and comment posting—to improve code maintainability.
- Powerful Content Acquisition: Integrates multiple methods for acquiring note content, ensuring the complete retrieval of various notes' titles, authors, and main text.
- Persistent Login: Uses persistent browser context, so there's no need to log in again after the first login.
- Two-Step Comment Process: First, obtains the note analysis results, then the MCP client generates and posts the comments.
Major Enhancements in Version 2.0
- Enhanced Content Acquisition: The note content acquisition module has been refactored, adding page load wait times and scrolling operations, implementing four different content acquisition methods.
- AI-Generated Comments: The comment feature has been refactored, returning the note analysis results to the MCP client, which uses its AI capabilities to generate more natural and relevant comments.
- Functional Modularization: Divides functionality into three independent modules—note analysis, comment generation, and comment posting—to improve code maintainability.
- Optimized Search Results: Resolved the issue of titles not displaying during note searches, 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 scanning for login. After the first login, the state is saved, and subsequent use does not require re-scanning.
- 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 searches, allows specifying the number of returned results, and provides complete note information.
- Multi-Dimensional Content Acquisition: Integrates four different acquisition methods to ensure accurate retrieval of notes' titles, authors, publication times, and main text.
- Comment Data Acquisition: Supports retrieving the comment content of notes, including commenter, 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-Generated Comments: Uses the AI capabilities of the MCP client (such as Claude) to generate natural and relevant comments based on the note content.
- Support for Multiple Types of Comments:
- Lead Generation Type: Guides users to follow or private message.
- Like Type: Simple interaction to gain favor.
- Inquiry Type: Increases interaction through questions.
- Professional Type: Demonstrates professional knowledge to establish authority.
4. Data Return and Feedback
- Structured Data Return: Returns the note analysis results in JSON format to the MCP client, facilitating AI-generated comments.
- Comment Posting Feedback: Provides real-time feedback on the results of comment posting.
II. Installation Steps
-
Prepare Python Environment: Ensure that Python 3.8 or a higher version is installed on your system. If not, download and install it from the official Python website.
-
Get the Project: Clone or download this project to your local machine.
-
Create a Virtual Environment: Create and activate a virtual environment in the project directory (recommended): bash
Create a virtual environment
python3 -m venv venv
Activate the virtual environment
Windows
venv\Scripts\activate
macOS/Linux
source venv/bin/activate
-
Install Dependencies: Install the required dependencies in the activated virtual environment: bash pip install -r requirements.txt pip install fastmcp
-
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 set up 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 should also use the full absolute path.
- In JSON, backslashes in Windows paths need to be double escaped (use
\).
Python Command Distinction (python vs python3)
The Python command may vary depending on your system's configuration. Here’s how to determine which command you should use:
-
Determine Your Python Command:
- Run in the terminal:
python --versionandpython3 --version - Check which command returns a Python 3.x version (this project requires Python 3.8+).
- Run in the terminal:
-
Confirm in the Virtual Environment:
- After activating the virtual environment, run
which pythonorwhere python(Windows). - This will display the full path of the Python interpreter.
- After activating the virtual environment, run
-
Use the Correct Command in Configuration:
- Mac: Usually
python3orpythonwithin the virtual environment. - Windows: Usually
pythonorpython.exe.
- Mac: Usually
In the configuration file, always use the full absolute path of the Python interpreter in the virtual environment, rather than the command name.
IV. Usage Instructions
(I) Starting the Server
-
Run Directly: In the project directory, after activating the virtual environment, execute: bash python3 xiaohongshu_mcp.py
-
Start via MCP Client: After configuring the MCP Client, follow the client's procedure to start and connect.
(II) Main Function Operations
After connecting to the server in the MCP Client (such as Claude for Desktop), the following functions can be used:
1. Log in to Xiaohongshu
Tool Function:
mcp0_login()
Usage in MCP Client: Send the following text directly:
Help me log in to my Xiaohongshu account
or:
Please log in to Xiaohongshu
Function Description: When used for the first time, it will open a browser window, waiting for the user to scan the QR code to log in. After successful login, the tool will save the login status.
2. Search for Notes
Tool Function:
mcp0_search_notes(keywords="keywords", limit=5)
Usage in MCP Client: Send a search request containing keywords:
Help me search Xiaohongshu notes, keywords are: food
Specify the number of results to return:
Help me search Xiaohongshu notes, keywords are travel, return 10 results
Function Description: Searches for Xiaohongshu notes based on keywords and returns the specified number of results. The default is 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 the detailed content of the specified note URL, including title, author, publication date, and body content.
4. Get Note Comments
Tool Function:
mcp0_get_note_comments(url="note URL")
Usage in MCP Client: Send a comment request containing the note URL:
Help me get the comments of this note: https://www.xiaohongshu.com/search_result/xxxxor:
Please check the comments section of this Xiaohongshu post: https://www.xiaohongshu.com/search_result/xxxx
Function Description: Retrieve comment information for a specified post URL, including commenter, comment content, and comment time.
5. Posting Smart Comments
Tool Function:
mcp0_post_smart_comment(url="Post URL", comment_type="Comment Type")
Usage in MCP Client: Send a request containing the post URL and comment type:
Help me write a [type] comment for this post: https://www.xiaohongshu.com/explore/xxxx
Function Description: Obtain the analysis results of the post and return them to the MCP client, which then generates the comment and calls post_comment to publish it.
6. Posting Comments
Tool Function:
mcp0_post_comment(url="Post URL", comment="Comment Content")
Usage in MCP Client: Send a request containing the post URL and comment content:
Help me post this comment to the post: https://www.xiaohongshu.com/explore/xxxx Comment content: [Comment content]
Function Description: Publish the specified comment content to the post page.
IV. User Guide
0. Working Principle
This tool uses a two-step process to implement the smart commenting feature:
-
Note Analysis: Call the
post_smart_commenttool to get note information (title, author, content, etc.) -
Comment Generation and Posting:
- The MCP client (e.g., Claude) generates a comment based on the note analysis results.
- Call the
post_commenttool to publish the comment.
This design fully leverages the AI capabilities of the MCP client, enabling 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 view 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 view the comments on 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 |
|---|---|---|
| Lead Generation | Direct users to follow or private message | Increase followers or private message interactions |
| Like | Simple interaction to gain favor | Increase exposure and interaction rate |
| Inquiry | Increase interaction through questions | Prompt blogger responses, deepen interaction |
| Professional | Demonstrate expertise to establish authority | Build a professional image, enhance credibility |
2. Example 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. [Call the post_smart_comment tool]
The tool returns the note analysis result, 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 posted the following professional comment:
"[Generated professional comment content]"
[Call the post_comment tool]
Claude: The comment has been successfully posted!
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. The actual comment generation is completed by the MCP client itself (e.g., Claude).
3. Working Principle
The new version of the Xiaohongshu MCP tool adopts a modular design, divided into three core modules:
-
Note Analysis Module (analyze_note)
- Get the note's title, author, publication time, and content
- Analyze the field and keywords of the note
- Return structured note information
-
Comment Generation Module (implemented by the MCP client)
- Receive the note analysis results
- Generate natural and relevant comments based on the note content and comment type
- Allow users to preview and modify the comment before posting
-
Comment Posting Module (post_comment)
- Receive the generated comment content
- Locate and operate the comment input box
- Post the comment and return the result
V. Code Structure- xiaohongshu_mcp.py: The core file that implements the main functionalities, including the code logic for login, search, content and comment retrieval, and posting comments.
- requirements.txt: Records the dependencies required for the project.
Six. Common Issues and Solutions
-
Connection Failure:
- Ensure you are using the full absolute path of the Python interpreter in your virtual environment.
- Make sure the MCP server is running.
- Try restarting both the MCP server and the client.
-
Browser Session Issues: If you encounter the
Page.goto: Target page, context or browser has been closederror:- Restart the MCP server.
- Reconnect and log in again.
-
Dependency Installation Issues: If you encounter a
ModuleNotFoundError:- Ensure all dependencies are installed within the virtual environment.
- Check if the fastmcp package is installed.
Seven. Precautions and Problem Solving
1. Usage Precautions
- Browser Mode: The tool runs in non-headless mode using Playwright, which means it will open a real browser window during operation.
- Login Method: The first login requires manual QR code scanning. For subsequent uses, if the login status is still valid, there's no need to scan the QR code again.
- Platform Rules: Please strictly adhere to the relevant regulations of the Xiaohongshu platform while using the tool. Avoid excessive operations to prevent the risk of account bans.
- Comment Frequency: It is recommended to control the frequency of posting comments. Avoid posting a large number of comments in a short period. Do not post more than 30 comments per day.
2. Common Issues and Solutions
Browser Instance Issues
If you encounter an error similar to "Page.goto: Target page, context or browser has been closed", it might be due to the browser instance not being properly closed or issues with the data directory lock files. Please try:
bash
Remove browser lock files
rm -f /path/to/project/browser_data/SingletonLock /path/to/project/browser_data/SingletonCookie
If the problem persists, back up and recreate the browser data directory
mkdir -p /path/to/project/backup_browser_data mv /path/to/project/browser_data/* /path/to/project/backup_browser_data/ mkdir -p /path/to/project/browser_data
Content Retrieval Issues
If you cannot retrieve note content or the content is incomplete, try:
- Increase Wait Time: Xiaohongshu note pages may require longer loading times, especially those with a lot of images or videos.
- Clear Browser Cache: Sometimes, the browser cache can affect content retrieval.
- Try Different Retrieval Methods: The tool integrates multiple retrieval methods. If one method fails, try another.
Adapting to Platform Changes
Xiaohongshu may update its page structure and DOM elements, causing the tool to malfunction. If you encounter such issues:
- Check for Project Updates: Keep an eye on the latest version of the project and update as needed.
- Adjust Selectors: If you are familiar with the code, try adjusting the CSS selectors or XPath expressions.
- Submit Issue Feedback: Report the issue to the project maintainers, describing the specific problem and any changes in the page.
Eight. Disclaimer
This tool is intended for learning and research purposes only. Users should strictly comply with relevant laws and regulations as well as the rules set by the Xiaohongshu platform. The developers of this project are not responsible for any issues arising from improper use.
服务配置
[{'mcpServers': {'xiaohongshu MCP': {'args': ['/绝对路径/到/xiaohongshu_mcp.py', '--stdio'], 'command': '/绝对路径/到/venv/bin/python3'}}}, {'mcpServers': {'xiaohongshu MCP': {'args': ['C:\\Users\\username\\Desktop\\MCP\\Redbook-Search-Comment-MCP2.0\\xiaohongshu_mcp.py', '--stdio'], 'command': 'C:\\Users\\username\\Desktop\\MCP\\Redbook-Search-Comment-MCP2.0\\venv\\Scripts\\python.exe'}}}]