
用 Python 实现的 Whatsapp MCP 服务器
概览
一台服务器,提供模型上下文协议(MCP)接口,通过FastMCP与WhatsApp商务API交互。 WhatsApp MCP Server 是一个 Python 实现,使 Claude 等语言模型能够通过 GreenAPI 与 WhatsApp 功能交互。它利用 FastMCP 提升性能、更优质的开发体验和更简洁的实现。 - : 向WhatsApp联系人发送短信 - : 创建群组,列出成员,添加/移除参与者 - : 管理WhatsApp API会话 - : 获取聊天列表和消息历史 该项目使用 whatsapp-api-client-python 库与 WhatsApp 交互。客户端提供访问 WhatsApp Cloud API,该 API 需要 GreenAPI 账户才能使用。 本项目使用环境变量进行配置: - GREENAPI_ID_INSTANCE:您的 GreenAPI ID 实例- GREENAPI_API_TOKEN:您的 GreenAPI 令牌 你可以在环境中设置这些文件,也可以使用提供的.env文件(参见安装说明)。
概述(AI 翻译)
一台服务器,提供模型上下文协议(MCP)接口,通过FastMCP与WhatsApp商务API交互。
引言
WhatsApp MCP Server 是一个 Python 实现,使 Claude 等语言模型能够通过 GreenAPI 与 WhatsApp 功能交互。它利用 FastMCP 提升性能、更优质的开发体验和更简洁的实现。
特色
- 消息:向WhatsApp联系人发送短信
- 组管理:创建组,列出成员,添加/移除参与者
- 会话处理:管理 WhatsApp API 会话
- 聊天记录:检索聊天列表和消息历史
WhatsApp API 客户端
该项目使用 whatsapp-api-client-python 库与 WhatsApp 交互。客户端提供访问 WhatsApp Cloud API,该 API 需要 GreenAPI 账户才能使用。
环境变量
本项目使用环境变量进行配置:
GREENAPI_ID_INSTANCE:您的GreenAPI ID实例GREENAPI_API_TOKEN:您的GreenAPI API令牌
你可以在环境中设置这些文件,或者使用提供的 .env 文件(详见安装说明)。
安装
用途
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
WhatsApp MCP Server
A server that provides a Model Context Protocol (MCP) interface to interact with WhatsApp Business API using FastMCP.
Introduction
WhatsApp MCP Server is a Python implementation that enables language models like Claude to interact with WhatsApp functionality through GreenAPI. It leverages FastMCP for improved performance, better developer experience, and a cleaner implementation.
Features
- Messaging: Send text messages to WhatsApp contacts
- Group Management: Create groups, list members, add/remove participants
- Session Handling: Manage WhatsApp API sessions
- Chat History: Retrieve chat lists and message history
WhatsApp API Client
This project uses the whatsapp-api-client-python library to interact with WhatsApp. The client provides access to the WhatsApp Cloud API, which requires a GreenAPI account to use.
Environment Variables
This project uses environment variables for configuration:
GREENAPI_ID_INSTANCE: Your GreenAPI ID instanceGREENAPI_API_TOKEN: Your GreenAPI API token
You can either set these in your environment or use the provided .env file (see Installation instructions).
Installation
# Clone the repository
git clone https://github.com/yourusername/whatsapp-mcp-server.git
cd whatsapp-mcp-server
# Install dependencies
pip install -e .
# Set up environment variables
cp .env-template .env
# Edit the .env file with your GreenAPI credentials
Usage
Run the MCP server:
# Run the MCP server on default host (127.0.0.1) and port (8000)
whatsapp-mcp
# Specify host and port
whatsapp-mcp --host 0.0.0.0 --port 9000
For debugging:
whatsapp-mcp --debug
The server communicates using the Model Context Protocol (MCP) and can be accessed via HTTP or WebSockets when running with FastMCP.
Available Tools
open_session: Open a new WhatsApp sessionsend_message: Send a message to a chatget_chats: Get a list of chatscreate_group: Create a new WhatsApp groupget_group_participants: Get the participants of a group
FastMCP API Reference
The WhatsApp MCP Server uses FastMCP to provide both WebSocket and HTTP endpoints:
- WebSocket:
ws://localhost:8000/mcp - HTTP:
http://localhost:8000/mcp
You can test the API directly using tools like curl:
# List available tools
curl -X POST http://localhost:8000/mcp/listTools
# Call a tool
curl -X POST http://localhost:8000/mcp/callTool \
-H "Content-Type: application/json" \
-d '{"name": "open_session", "arguments": {}}'
How to add it to Claude Code
To add a WhatsApp server to Claude, use the claude mcp add command:
# Add the WhatsApp MCP server
$ claude mcp add whatsapp -- whatsapp-mcp
# List existing MCP servers - Validate that the server is running
claude mcp list
# Start claude code
claude
Using with Claude
安装
docker run --rm -i -e GREENAPI_ID_INSTANCE=your_instance_id -e GREENAPI_API_TOKEN=your_api_token whatsapp-mcp-server配置
"mcpServers": {
"whatsapp": {
"command": "python",
"args": ["-m", "whatsapp_mcp"]
}
}