何不乘东风?利用mcp实现小红书自动发布
1.准备环境
conda create -n mcpo python=3.11
conda activate mcpo
pip install mcpo
pip install uvnpx @puppeteer/browsers install chromedriver@134.0.6998.166pip install xhs-mcp-server
2.进行登陆
env phone=YOUR_PHONE_NUMBER json_path=PATH_TO_STORE_YOUR_COOKIES uvx --from xhs_mcp_server@latest login#这里的json_path是你登录后放cookie的路径
输入验证码,(记住验证码一定要再黑窗口输入,不要直接在页面登录)就会自动登录
env phone=YOUR_PHONE_NUMBER json_path=PATH_TO_STORE_YOUR_COOKIES uvx --from xhs_mcp_server@latest login
再来一遍验证,如果显示“使用cookies登录成功”就登陆成功啦。
3.直接实现
1)原生代码调用小红书的mcp服务实现自动发布
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_clientasync def main():# 配置 MCP 服务的启动命令和参数server_params = StdioServerParameters(command="/Users/v_chenxinhao/miniconda3/envs/mcpo/bin/python",args=["-m", "xhs_mcp_server"],env={"phone": "xxxx","json_path": "/Users/v_chenxinhao/Desktop/20250122/xiaohongshu"})# 使用 stdio_client 启动 MCP 服务器并获取读写流async with stdio_client(server_params) as (read_stream, write_stream):# 创建 MCP 客户端会话async with ClientSession(read_stream, write_stream) as session:# 初始化会话await session.initialize()# 发送请求并接收响应response = await session.call_tool("create_note",arguments={"title": "示例标题","content": "这是一个示例内容。","images": ["/Users/v_chenxinhao/Desktop/20250324/images.png"]}
)print("响应结果:", response)# 运行主函数
asyncio.run(main())
2)mcpo实现
先写好配置
{"mcpServers": {"xhs-mcp-server": {"isActive": true,"name": "xhs-mcp-server","type": "stdio","registryUrl": "","command": "/Users/v_chenxinhao/miniconda3/envs/mcpo/bin/python","args": ["-m","xhs_mcp_server"],"env": {"phone": "xxxx","json_path": "/Users/v_chenxinhao/Desktop/20250122/xiaohongshu" #登陆的cookie的存储位置}}}}
然后一键运行:uvx mcpo --config mcpo.json --port 8002
开始调用
import requestsdef create_note(base_url, title, content, image_paths):"""发布图文笔记到小红书:param base_url: MCP 服务地址,例如 http://localhost:8000/xhs-mcp-server:param title: 标题(不超过20字):param content: 正文描述:param image_paths: 图片路径列表"""url = f"{base_url}/create_note"payload = {"title": title,"content": content,"images": image_paths}resp = requests.post(url, json=payload)resp.raise_for_status()return resp.json()
def create_video_note(base_url, title, content, video_paths):"""发布视频笔记到小红书:param base_url: MCP 服务地址,例如 http://localhost:8000/xhs-mcp-server:param title: 标题(不超过20字):param content: 正文描述:param video_paths: 视频路径或URL列表"""url = f"{base_url}/create_video_note"payload = {"title": title,"content": content,"videos": video_paths}resp = requests.post(url, json=payload)resp.raise_for_status()return resp.json()
if __name__ == "__main__":base_url = "http://localhost:8002/xhs-mcp-server"# 图文发布note_result = create_note(base_url,title="魔塔自动发笔记支持视频啦",content="xhs-mcp 现在可以自动发视频笔记了!一键搞定!",image_paths=["/Users/v_chenxinhao/Desktop/20250324/images.png"])print("图文笔记返回:", note_result)# # 视频发布# video_result = create_video_note(# base_url,# title="魔塔mcp支持视频发布",# content="测试发布小红书视频笔记,自动化无压力~",# video_paths=["/Users/you/Desktop/demo.mp4"]# )# print("视频笔记返回:", video_result)
3)效果实现
4.cherry studio实现
1)打开cherry studio,设置—模型服务—ModelScope魔搭—获取密钥,填入密钥即可完成魔搭社区的模型服务配置
2)配置小红书server和文生图server,设置—MCP服务器—编辑MCP配置
{"mcpServers": {"xhs-mcp-server": {"isActive": true,"name": "xhs-mcp-server","type": "stdio","registryUrl": "","command": "/Users/v_chenxinhao/miniconda3/envs/mcpo/bin/python","args": ["-m","xhs_mcp_server"],"env": {"phone": "xxxxxx","json_path": "/Users/v_chenxinhao/Desktop/20250122/xiaohongshu"}}}
}
3)开始使用