一个基于事件流的WhatsApp消息服务器,使用了FastMCP和Twilio API构建。 可以通过编程方式或浏览器使用服务端发送事件(Server-Sent Events)发送消息——非常适合代理、自动化或仪表板。
text/event-stream进行流式响应git clone https://github.com/your-username/whatsapp-mcp.git
cd whatsapp-mcp
python -m venv venv
venv\Scripts\activate # 或者 source venv/bin/activate
pip install -r requirements.txt
创建一个.env文件:
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_WHATSAPP_FROM=whatsapp:+14155238886
python server.py
你会看到:
🔗 服务器URL: http://localhost:8000/mcp
📦 传输方式: 流式-HTTP
在浏览器中打开index.html。
输入:
+91xxxxxxxxxx)点击发送WhatsApp消息——立即接收!
curl测试curl -X POST http://localhost:8000/mcp ^
-H "Content-Type: application/json" ^
-H "Accept: text/event-stream" ^
-d "{\"tool\":\"send_whatsapp_message\", \"inputs\":{\"to\":\"+91xxxxxxxxxx\", \"body\":\"Hello from curl!\"}}"
✅ 将
+91xxxxxxxxxx替换为你已验证的WhatsApp号码
graph TD
A[🧑 用户] -->|输入消息和号码| B[🌐 HTML界面]
B -->|POST /mcp| C[🌀 FastMCP服务器]
C -->|调用| D[📲 Twilio API]
D -->|发送| E[📱 WhatsApp消息]
C -->|流式响应| B
.env中的秘密信息from fastmcp import Client
from server import mcp
async with Client(mcp) as client:
await client.call_tool("send_whatsapp_message", {
"to": "+91xxxxxxxxxx",
"body": "从代理触发的消息 🚀"
})
由K Rajtilak用心制作