小红书

这是一款基于 Playwright 开发的小红书自动搜索和评论工具,作为 MCP Server,可通过特定配置接入 MCP Client(如Claude for Desktop),帮助用户自动完成登录小红书、搜索关键词、获取笔记内容及发布AI生成评论等操作。

browser-automation

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

  1. 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.

  2. Get 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 a virtual environment

    python3 -m venv venv

    Activate the 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 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:

  1. Determine Your Python Command:

    • Run in the terminal: python --version and python3 --version
    • Check which command returns a 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: Usually python3 or python within the virtual environment.
    • Windows: Usually 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.

IV. Usage Instructions

(I) Starting the Server

  1. Run Directly: In the project directory, after activating the virtual environment, execute: bash python3 xiaohongshu_mcp.py

  2. 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:

  1. Note Analysis: Call the post_smart_comment tool to get note information (title, author, content, etc.)

  2. Comment Generation and Posting:

    • The MCP client (e.g., Claude) generates a comment based on the note analysis results.
    • Call the post_comment tool 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

FunctionExample Command
Search NotesHelp me search for Xiaohongshu notes about [keyword]
Get Note ContentHelp me view the content of this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx
Analyze NoteHelp me analyze this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx
Get CommentsHelp me view the comments on this note: https://www.xiaohongshu.com/explore/xxxx
Generate CommentHelp me write a [type] comment for this Xiaohongshu note: https://www.xiaohongshu.com/explore/xxxx

Comment Type Options

TypeDescriptionApplicable Scenarios
Lead GenerationDirect users to follow or private messageIncrease followers or private message interactions
LikeSimple interaction to gain favorIncrease exposure and interaction rate
InquiryIncrease interaction through questionsPrompt blogger responses, deepen interaction
ProfessionalDemonstrate expertise to establish authorityBuild 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:

  1. 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
  2. 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
  3. 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

  1. 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.
  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:

    • 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:

  1. Increase Wait Time: Xiaohongshu note pages may require longer loading times, especially those with a lot 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, 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:

  1. Check for Project Updates: Keep an eye on the latest version of the project and update as needed.
  2. Adjust Selectors: If you are familiar with the code, try adjusting the CSS selectors or XPath expressions.
  3. 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'}}}]

来源

小红书 - MCP - HelloWorld