微信公众号文章读取 MCP 服务器
这是一个基于 Model Context Protocol (MCP) 的服务器,用于读取微信公众号文章内容、搜索文章以及生成文章摘要。
功能特性
- 文章获取:从微信公众号文章URL获取文章内容、元数据和统计信息
- 文章搜索:根据关键词搜索微信公众号文章
- 文章摘要:自动生成文章摘要,支持自定义长度
- 多格式输出:支持Markdown和纯文本格式输出
- 错误处理:完善的错误处理和响应格式化
- 性能优化:支持并发请求和性能监控
安装与配置
环境要求
- Python 3.8+
- Playwright(用于浏览器自动化)
安装步骤
- 克隆项目仓库:
git clone https://github.com/your-username/mcp-server-wechat.git
cd mcp-server-wechat
- 创建虚拟环境:
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或
venv\Scripts\activate # Windows
- 安装依赖:
pip install -r requirements.txt
- 安装Playwright浏览器:
playwright install
配置
服务器支持多种配置方式:
- 配置文件:创建
config.json文件:
{
"transport": "stdio",
"host": "localhost",
"port": 8080,
"browser": {
"headless": true,
"timeout": 30000,
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
},
"request": {
"timeout": 30000,
"max_retries": 3,
"retry_delay": 1000
}
}
- 环境变量:
export MCP_SERVER_TRANSPORT=stdio
export MCP_SERVER_HOST=localhost
export MCP_SERVER_PORT=8080
export MCP_BROWSER_HEADLESS=true
export MCP_BROWSER_TIMEOUT=30000
使用方法
启动服务器
- STDIO模式(默认):
python main.py
- HTTP模式:
python main.py --transport http --host 0.0.0.0 --port 8080
MCP工具
1. 获取文章内容
# 调用fetch_article工具
{
"name": "fetch_article",
"arguments": {
"url": "https://mp.weixin.qq.com/s/xxxxxxxx",
"content_formats": ["markdown", "text"],
"request_id": "optional_request_id"
}
}
2. 搜索文章
# 调用search_articles工具
{
"name": "search_articles",
"arguments": {
"query": "搜索关键词",
"account": "可选的公众号名称",
"sort_by": "relevance", # relevance, time, popularity
"limit": 10,
"request_id": "optional_request_id"
}
}
3. 提取文章摘要
# 调用extract_article_summary工具
{
"name": "extract_article_summary",
"arguments": {
"url": "https://mp.weixin.qq.com/s/xxxxxxxx",
"max_length": 200,
"request_id": "optional_request_id"
}
}
响应格式
所有工具返回统一的响应格式:
{
"success": true,
"message": "操作成功",
"request_id": "optional_request_id",
"data": {
// 具体数据内容
}
}
错误响应:
{
"success": false,
"message": "错误描述",
"error_code": "ERROR_CODE",
"error_type": "ErrorType",
"request_id": "optional_request_id"
}
开发指南
项目结构
mcp-server-wechat/
├── src/
│ └── mcp_server_wechat/
│ ├── __init__.py
│ ├── main.py # 主服务器文件
│ ├── models.py # 数据模型和类型定义
│ ├── tools/
│ │ ├── __init__.py
│ │ └── wechat_tools.py # MCP工具实现
│ └── utils/
│ ├── __init__.py
│ ├── browser_client.py # 浏览器客户端
│ ├── article_parser.py # 文章解析器
│ ├── search_client.py # 搜索客户端
│ ├── summary_generator.py # 摘要生成器
│ └── response_formatter.py # 响应格式化器
├── tests/
│ ├── __init__.py
│ ├── test_wechat_tools.py # 单元测试
│ ├── test_integration.py # 集成测试
│ └── test_performance.py # 性能测试
├── config.json # 配置文件
├── requirements.txt # 依赖列表
├── run_tests.py # 测试运行器
└── README.md # 项目文档
运行测试
- 运行所有测试:
python run_tests.py --type all
- 运行单元测试:
python run_tests.py --type unit
- 运行集成测试:
python run_tests.py --type integration
- 运行性能测试:
python run_tests.py --type performance
添加新工具
- 在
src/mcp_server_wechat/tools/wechat_tools.py中添加新工具类 - 在
handle_list_tools函数中注册新工具 - 在
handle_call_tool函数中添加工具调用逻辑 - 添加相应的测试用例
常见问题
1. 浏览器启动失败
确保已安装Playwright浏览器:
playwright install
2. 微信文章URL无效
确保URL格式正确:
https://mp.weixin.qq.com/s/xxxxxxxx
3. 搜索结果为空
检查搜索关键词是否正确,或尝试使用更通用的关键词。
4. 性能问题
调整浏览器配置中的超时时间,或使用HTTP模式以提高性能。
许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
贡献
欢迎提交问题和拉取请求。请确保:
- 代码符合项目风格
- 添加适当的测试
- 更新相关文档
联系方式
如有问题或建议,请通过以下方式联系:
- 提交 Issue: GitHub Issues
- 邮箱: your-email@example.com
