基于 SSE 服务的微信小程序客户端,提供社区选择、聊天和个人信息等功能。
该项目是一个基于微信小程序的 SSE(Server Sent Events)客户端,主要包括三个核心页面:社区选择页面、聊天页面和个人信息页面。
.
├── miniprogram/ # 微信小程序客户端
│ ├── app.js # 小程序入口文件
│ ├── app.json # 小程序配置文件
│ ├── app.wxss # 全局样式文件
│ ├── images/ # 图片资源
│ ├── pages/ # 页面文件
│ │ ├── community/ # 社区精选页面
│ │ │ ├── index.js # 社区页面逻辑
│ │ │ ├── index.json # 页面配置
│ │ │ ├── index.wxml # 页面结构
│ │ │ └── index.wxss # 页面样式
│ │ ├── chat/ # 聊天页面
│ │ │ ├── index.js # 聊天页面逻辑
│ │ │ ├── index.json # 页面配置
│ │ │ ├── index.wxml # 页面结构
│ │ │ └── index.wxss # 页面样式
│ │ ├── profile/ # 个人信息页面
│ │ │ ├── index.js # 个人页面逻辑
│ │ │ ├── index.json # 页面配置
│ │ │ ├── index.wxml # 页面结构
│ │ │ └── index.wxss # 页面样式
│ │ └── travel-guide/ # 出行攻略渲染页面
│ │ ├── index.js # 攻略页面逻辑
│ │ ├── index.json # 页面配置
│ │ ├── index.wxml # 页面结构
│ │ └── index.wxss # 页面样式
│ └── utils/ # 工具函数
│ ├── sseClient.js # SSE 客户端服务
│ ├── markdown.js # Markdown 解析工具
│ └── mcpConfig.js/ # MCP SSE 配置的地方(Server id 可从 Cherry Studio 获取)
├── server_back/package/ # SSE 服务器
│ ├── sse_proxy_server.ts # SSE 中转服务器主文件
│ ├── package.json # 服务器依赖配置
│ └── .env # 环境变量配置
├── package.json # 项目依赖配置
├── deploy.sh # 部署脚本
├── 微信开发者工具配置.md # 配置说明文档
└── README.md # 项目说明文档
SSE 服务器基于 Express 实现,并提供以下功能:
位置: server_back/package/sse_proxy_server.ts
POST /sse-chat 创建用于实时聊天的 SSE 连接POST /chat 普通聊天 API,不使用 SSE 连接GET /status: 获取服务器状态以下是系统处理高德地图天气查询的实际案例:
收到 SSE 聊天请求
OpenAI 响应: 工具调用请求 mcp_amap_maps_maps_weather
处理工具调用: mcp_amap_maps_maps_weather
工具调用参数: { city: '深圳市' }
工具调用结果: {
"status": "1",
"count": "1",
"info": "OK",
"lives": [
{
"province": "广东",
"city": "深圳市",
"adcode": "440300",
"weather": "阴",
"temperature": "26",
"winddirection": "西南",
"windpower": "≤3",
"humidity": "69",
"reporttime": "2025-04-09 11:00:36"
}
]
}
最终响应: 深圳市昨天的天气状况是阴天,气温为26°C,西南风风力≤3级,湿度为69%。
SSE 客户端在微信小程序中实现,并提供以下功能:
createSseConnection(): 创建 SSE 连接sendChatMessage(): 发送普通聊天消息checkServerStatus() 检查服务器状态# 安装依赖并启动服务器
npm run start-server
# 或者使用调试脚本
./debug.sh
服务器默认运行在 3091 端口,如果该端口被占用会自动尝试释放。
该项目实现了完整的微信账号登录和聊天频率管理功能:
wx.getUserProfile API 获取用户信息此服务器是 Model Context Protocol (MCP) 的中转服务器,使用 Server Send Events (SSE) 与 MCP 服务器通信,并集成了 OpenAI GPT-4o 来智能选择和调用工具。
npm install
.env.example 文件为 .env.env 文件中配置 OpenAI API 密钥和其他设置OPENAI_API_KEY=your_openai_api_key_here
OPENAI_MODEL=gpt-4o
OPENAI_BASE_URL=https://api.openai.com/v1
PORT=3091
启动服务器:
npm start
创建连接 - POST /connect
{
"sseUrl": "https://example-sse-server.com/sse"
}
发送消息 - POST /send/:connectionId
{
"message": "我想知道纽约现在的时间"
}
或
{
"type": "tool_call",
"tool": "get_current_time",
"args": { "timezone": "America/New_York" }
}
获取可用工具 - GET /tools/:connectionId
关闭连接 - DELETE /connection/:connectionId
检查服务器状态 - GET /status
此项目由两个主要的服务组件构成:
项目提供了自动化部署脚本 deploy.sh 执行以下命令进行部署:
chmod +x deploy.sh
./deploy.sh
部署脚本将:
部署后,需要为两个服务配置环境变量:
登录服务器 (.env)
# 服务配置
PORT=3090
NODE_ENV=production
# JWT 配置
JWT_SECRET=your_jwt_secret_here
JWT_EXPIRES_IN=7d
# 微信小程序配置
WX_APPID=your_wx_appid_here
WX_SECRET=your_wx_secret_here
# 数据存储配置
DATA_DIR=data
MCP 代理服务 (.env)
# 服务配置
PORT=3091
NODE_ENV=production
# OpenAI 配置
OPENAI_API_KEY=your_openai_api_key_here
OPENAI_MODEL=gpt-4o
OPENAI_BASE_URL=https://api.openai.com/v1
如果需要手动部署,请参考以下步骤:
cd loginServer && npm install
cd package && npm install && npm run build
cd loginServer && pm2 start app.js --name wxapp-login-server
cd package && pm2 start dist/sse_proxy_server.js --name mcp-proxy-server
server {
listen 80;
server_name your-domain.com;
# 登录服务器
location /api/ {
proxy_pass http://localhost:3090;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# MCP 代理服务器
location / {
proxy_pass http://localhost:3091;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
登录服务负责处理微信小程序的登录认证和用户管理。主要的 API 端点包括:
/api/user/login - 用户登录/api/user/info - 获取/更新用户信息/api/user/chat-credits - 管理聊天频率请参阅详细的 API 文档 server_back/loginServer/README.md
MCP 代理服务提供了通过 SSE 连接协议与 AI 模型交互的能力。主要的端点包括:
/discover-mcp - 发现 MCP 服务器/connect - 创建连接/send/:connectionId - 发送消息/tools/:connectionId - 列出可用工具如果遇到部署问题,请检查:
服务器日志
pm2 logs
Nginx 日志
tail -f /var/log/nginx/error.log
确保正确配置了环境变量
检查所需端口是否开放