WeChat Official Account MCP
Enables AI applications to manage WeChat Official Accounts through standardized tools for authentication, media uploads, draft creation, article publishing, and permanent material management.
README
微信公众号 MCP 服务
一个为 AI 应用提供微信公众号 API 集成的 MCP (Model Context Protocol) 服务项目。
作者: xwang152-jack xwang152@163.com
更新日期: 2025年11月20日
🚀 项目概述
本项目基于 MCP 协议,为 AI 应用(如 Claude Desktop、Cursor、Trae AI 等)提供微信公众号 API 的无缝集成。通过标准化的工具接口,AI 应用可以轻松地管理微信公众号的素材、草稿、发布等功能。
当前版本:v1.1.0(查看 CHANGELOG 与 Release 说明)
✨ 核心功能
- 🔐 认证管理: 安全管理微信公众号 AppID、AppSecret 和 Access Token
- 📁 素材管理: 上传、获取、管理临时和永久素材
- 📝 草稿管理: 创建、编辑、管理图文草稿
- 📢 发布管理: 发布草稿到微信公众号
- 💾 本地存储: 使用 SQLite 本地存储配置和数据
- 🔧 MCP 集成: 完全兼容 MCP 协议标准
- 🛡️ 安全增强(v1.1.0): 支持敏感字段加密存储与日志脱敏,跨域来源白名单配置
🛠️ 技术栈
- 运行时: Node.js 18+
- 语言: TypeScript
- 协议: MCP (Model Context Protocol)
- 数据库: SQLite
- HTTP 客户端: Axios
- 参数验证: Zod
- 构建工具: Vite
📦 快速开始
方式一:使用 npx(推荐)
直接使用 npx 运行,无需安装:
# 启动 MCP 服务器
npx wechat-official-account-mcp mcp -a <your_app_id> -s <your_app_secret>
# 示例
npx wechat-official-account-mcp mcp -a wx1234567890abcdef -s your_app_secret_here
提示:如使用 SSE 模式,请设置
CORS_ORIGIN为允许访问的域名白名单。
方式二:全局安装
# 全局安装
npm install -g wechat-official-account-mcp
# 启动服务
wechat-mcp mcp -a <your_app_id> -s <your_app_secret>
方式三:本地开发
# 1. 克隆项目
git clone https://github.com/xwang152-jack/wechat-official-account-mcp.git
cd wechat-official-account-mcp
# 2. 安装依赖
npm install
# 3. 构建项目
npm run build
# 4. 启动服务
node dist/src/cli.js mcp -a <your_app_id> -s <your_app_secret>
CLI 参数说明
-a, --app-id <appId>: 微信公众号 AppID(必需)-s, --app-secret <appSecret>: 微信公众号 AppSecret(必需)-m, --mode <mode>: 传输模式,支持stdio(默认)和sse-p, --port <port>: SSE 模式下的端口号(默认 3000)-h, --help: 显示帮助信息
环境变量(常用):
CORS_ORIGIN: 逗号分隔的跨域来源白名单(示例:https://a.example.com,https://b.example.com)WECHAT_MCP_SECRET_KEY: 开启敏感字段加密存储(AES),设置即启用
🔧 MCP 工具列表
1. 认证工具 (wechat_auth)
管理微信公众号认证配置和 Access Token。
支持操作:
configure: 配置 AppID 和 AppSecretget_token: 获取当前 Access Tokenrefresh_token: 刷新 Access Tokenget_config: 查看当前配置
2. 素材上传工具 (wechat_media_upload)
上传和管理微信公众号临时素材。
支持操作:
upload: 上传素材(图片、语音、视频、缩略图)get: 获取素材信息list: 暂不支持(临时素材有效期 3 天,建议使用永久素材功能)
支持格式:
- 图片:JPG、PNG(大小不超过 10MB)
- 语音:MP3、WMA、WAV、AMR(大小不超过 10MB,时长不超过 60s)
- 视频:MP4(大小不超过 10MB)
- 缩略图:JPG(大小不超过 64KB)
3. 图文消息图片上传工具 (wechat_upload_img)
上传图文消息内所需的图片,不占用素材库限制。
支持操作:
upload: 上传图片(支持文件路径或base64数据)
支持格式:
- 图片:JPG、PNG(大小不超过 1MB)
特点:
- 不占用公众号素材库的100000个图片限制
- 专用于图文消息内容中的图片
- 返回可直接在图文消息中使用的图片URL
4. 永久素材工具 (wechat_permanent_media)
管理微信公众号永久素材。
支持操作:
add: 上传永久素材(图片、语音、视频、缩略图)get: 获取永久素材delete: 删除永久素材list: 获取素材列表count: 获取素材总数统计
5. 草稿管理工具 (wechat_draft)
管理微信公众号图文草稿。
支持操作:
add: 新建草稿get: 获取草稿详情delete: 删除草稿list: 获取草稿列表count: 获取草稿总数
6. 发布工具 (wechat_publish)
管理微信公众号文章发布。
支持操作:
submit: 发布草稿get: 获取发布状态delete: 删除发布list: 获取发布列表
📁 项目结构
src/
├── cli.ts # CLI 入口文件
├── index.ts # 模块导出入口
├── mcp-server/ # MCP 服务器实现
│ ├── shared/ # 共享组件
│ │ ├── init.ts # 服务器初始化
│ │ └── types.ts # 类型定义
│ └── transport/ # 传输层实现
│ ├── stdio.ts # stdio 传输
│ └── sse.ts # SSE 传输
├── mcp-tool/ # MCP 工具实现
│ ├── index.ts # 工具管理器
│ ├── types.ts # 类型定义
│ └── tools/ # 具体工具实现
│ ├── index.ts
│ ├── auth-tool.ts
│ ├── media-upload-tool.ts
│ ├── upload-img-tool.ts
│ ├── permanent-media-tool.ts
│ ├── draft-tool.ts
│ └── publish-tool.ts
├── auth/ # 认证管理
│ └── auth-manager.ts
├── wechat/ # 微信 API 客户端
│ └── api-client.ts
├── storage/ # 数据存储
│ └── storage-manager.ts
└── utils/ # 工具函数
├── logger.ts
└── db-init.ts
🔗 在 AI 应用中使用
Claude Desktop
在 claude_desktop_config.json 中添加:
{
"mcpServers": {
"wechat-official-account": {
"command": "npx",
"args": [
"wechat-official-account-mcp",
"mcp",
"-a", "your_wechat_app_id",
"-s", "your_wechat_app_secret"
]
}
}
}
或者使用全局安装的版本:
{
"mcpServers": {
"wechat-official-account": {
"command": "wechat-mcp",
"args": [
"mcp",
"-a", "your_wechat_app_id",
"-s", "your_wechat_app_secret"
]
}
}
}
Cursor / Trae AI
在 MCP 配置中添加服务器配置:
{
"mcpServers": {
"wechat-official-account": {
"command": "npx",
"args": [
"wechat-official-account-mcp",
"mcp",
"-a", "your_wechat_app_id",
"-s", "your_wechat_app_secret"
]
}
}
}
或者使用全局安装的版本:
{
"mcpServers": {
"wechat-official-account": {
"command": "wechat-mcp",
"args": [
"mcp",
"-a", "your_wechat_app_id",
"-s", "your_wechat_app_secret"
]
}
}
}
🧪 开发指南
开发模式
# 安装依赖
npm install
# 构建项目
npm run build
# 本地测试 CLI
node dist/src/cli.js mcp -a test_app_id -s test_app_secret
# 类型检查
npm run check
# 代码检查
npm run lint
构建和发布
# 构建项目
npm run build
# 本地测试包
npm pack
# 发布到 npm
npm publish
测试
# 运行测试
npm test
# 测试 CLI 功能
node dist/src/cli.js --help
📝 配置说明
环境变量
创建 .env 文件:
# 开发模式(可选)
NODE_ENV=development
# 调试模式(可选)
DEBUG=true
# 跨域来源白名单(强烈建议生产环境设置)
CORS_ORIGIN=https://your-domain.com,https://another-domain.com
# 开启敏感字段加密(设置后启用 AES 加密存储)
WECHAT_MCP_SECRET_KEY=your-strong-secret-key
# 数据库路径(可选,默认为 ./data/wechat-mcp.db)
DB_PATH=./data/wechat-mcp.db
微信公众号配置
- 登录微信公众平台
- 进入「开发」->「基本配置」
- 获取 AppID 和 AppSecret
- 使用
wechat_auth工具进行配置
🔒 安全说明
- 加密存储:设置
WECHAT_MCP_SECRET_KEY后,app_secret/token/encoding_aes_key/access_token以加密形式持久化(带enc:前缀标识) - 日志脱敏:错误日志仅记录状态码或消息,避免泄露响应体与敏感信息
- 跨域白名单:生产环境务必设置
CORS_ORIGIN为精确域名列表,避免* - 参数校验:工具参数使用 Zod 校验,降低不当输入风险
- 切勿提交密钥:不要将 AppSecret、Token 等放入代码仓库或构建产物
🤝 贡献指南
- Fork 本项目
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 开启 Pull Request
📄 许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
🆘 支持
如果您遇到问题或有建议,请:
- 查看 Issues 页面
- 创建新的 Issue
- 联系项目维护者: xwang152-jack xwang152@163.com
🙏 致谢
- Model Context Protocol - MCP 协议标准
- 微信公众平台 - 微信公众号 API
- Anthropic - Claude Desktop MCP 支持
注意: 本项目仅供学习和开发使用,请遵守微信公众平台的使用条款和相关法律法规。
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.