使用说明
WhatsApp MCP服务器🚀
📲 FastMCP WhatsApp服务器
基于Python的MCP(模型上下文协议)微服务,使用 Twilio WhatsApp API 发送消息。内置于 FastMCP 用于快速、模块化的人工智能工具和自动化。
🚀 特性
- ✅ 使用Twilio发送WhatsApp消息
- ⚙️ 由FastMCP提供支持,便于工具管理
- 🔒 用途
.env用于安全的凭证存储 - 🧪 包含用于测试消息发送的客户端脚本
📁 项目结构
whatsapp mcp服务器/ ├── .env#Twilio凭据(未提交) ├── .gitignore#忽略venv和env文件 ├── whatsapp_server.py#主FastMCP服务器 ├── client_test.py#调用MCP工具的示例客户端 ├── requirements.txt#项目依赖关系 ├── README.md#您正在阅读! └── venv/#Python虚拟环境(自动生成)
格式 复制 编辑
⚙️ 需求
- Python 3.10或更高版本
- Twilio帐户与WhatsApp沙盒设置
- FastMCP 安装
🛠️ 安装指南
1.📦 克隆或下载项目
git clone https://github.com/yourusername/whatsapp-mcp-server.git
cd whatsapp-mcp-server
2. 🧪 Create and Activate Virtual Environment
bash
Copy
Edit
python -m venv venv
.\venv\Scripts\activate # For Windows (VS Code, PowerShell, CMD)
3. 📥 Install Dependencies
bash
Copy
Edit
pip install -r requirements.txt
4. 🔐 Create .env File
Create a file named .env in the root directory:
ini
Copy
Edit
TWILIO_ACCOUNT_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_WHATSAPP_FROM=whatsapp:+14155238886
🛑 Important: Never share this file publicly.
▶️ Running the Server
Start the MCP WhatsApp Server
bash
Copy
Edit
python whatsapp_server.py
Output will show:
arduino
Copy
Edit
Server running at http://127.0.0.1:8000/mcp
📤 Sending a WhatsApp Message
Use the included test client to send a message:
bash
Copy
Edit
python client_test.py
Make sure to update client_test.py with the recipient WhatsApp number like:
python
Copy
Edit
"to": "whatsapp:+919999999999",
"body": "Hello from FastMCP 🚀"
🧪 Sample Tool Code
Tool to send WhatsApp message:
python
Copy
Edit
@mcp.tool
def send_whatsapp_message(to: str, body: str) -> str:
try:
message = twilio_client.messages.create(
body=body,
from_=from_number,
to=to
)
return f"✅ Sent! SID: {message.sid}"
except Exception as e:
return f"❌ Error: {str(e)}"
💡 Tips
You can expose more tools in whatsapp_server.py for different message types.
Try deploying the MCP server to the cloud using Render, Replit, or Railway.
Connect this MCP tool to a bigger AI Agent stack.
📜 License
This project is licensed under the Apache 2.0 License.
🙌 Acknowledgements
FastMCP — for simplifying MCP servers
Twilio — for the WhatsApp API
yaml
Copy
Edit
---
Let me know if you'd like this auto-generated in your project as a real file or need a zipped version!
