WhatsApp MCP
README.md
# WhatsApp MCP
<img src="docs/images/icon.png" alt="WhatsApp MCP icon" width="96" align="right">
Use WhatsApp Web in your own Chrome profile as a Python library, a CLI, a REST
API, or an MCP server. An AI assistant (Claude Code or any MCP client) can read
and search chats, contacts, and messages, start new chats, send text, files,
and contact cards, pin chats and messages, receive incoming messages, and
answer them through auto-reply profiles.
The work is done by the official WhatsApp Web that you logged into with its QR
code. This package does not reimplement the WhatsApp protocol and never holds a
password or account key. Every command runs inside the WhatsApp Web client in
your Chrome tab, through WhatsApp Web's own modules.
## Up and running
```bash
git clone https://github.com/alfa-yohannis/whatsapp-mcp
cd whatsapp-mcp
./whatsapp install # Windows: whatsapp.cmd install
```
1. In Chrome, open `chrome://extensions`, turn on **Developer mode**, click
**Load unpacked**, and choose the `whatsapp_mcp/extension` folder.
2. Open WhatsApp Web in that profile and scan its QR code with your phone if
asked (WhatsApp > Linked devices > Link a device).
3. Check with `./whatsapp status`, which should end with `logged_in`.
4. Connect Claude Code: `claude mcp add whatsapp -- "$PWD/whatsapp" mcp`.
5. For auto-reply, open the side panel from the extension's popup, create a
profile, link contacts, and press **Dry run** or **Run**.
The full walkthrough, with screenshots and troubleshooting, is in
[docs/UP-AND-RUNNING.md](docs/UP-AND-RUNNING.md).
## How it works
```
Claude Code / CLI / REST API / your Python program
│ WhatsAppClient: TCP 127.0.0.1 + token from the bridge card
▼
native messaging host (whatsapp_mcp/host.py, started by Chrome)
│ native messaging (stdio)
▼
WhatsApp MCP Chrome extension (whatsapp_mcp/extension/)
│ chrome.scripting, MAIN world
▼
bridge.js in https://web.whatsapp.com/ → WhatsApp Web modules (WAWebCollections, ...)
```
Incoming messages travel the other way: the bridge listens to WhatsApp Web's
message collection, the extension forwards each message to the host, and the
host broadcasts it to subscribed clients (the MCP server, `whatsapp watch`,
`GET /events`, and the auto-reply service).
An extension is needed because Chrome 136+ ignores DevTools remote debugging on
the default profile directory, and Chrome 137+ ignores `--load-extension`. With
an extension, WhatsApp MCP works in the same Chrome profile you use every day.
## Requirements
- Python 3.10 or newer
- Google Chrome (Linux, Windows, or macOS)
- WhatsApp on your phone, to link WhatsApp Web with its QR code
- For draft and auto replies: the [Claude Code](https://code.claude.com) CLI (`claude`)
## Installation
```bash
git clone https://github.com/alfa-yohannis/whatsapp-mcp
cd whatsapp-mcp
./whatsapp install # Windows: whatsapp.cmd install
```
The `./whatsapp` launcher installs the package and its dependencies on first
run: into `$WHATSAPP_VENV` when set, into `~/venv` when it exists, or into
`.venv` in this directory. `install` registers the native messaging host with
Chrome (a manifest file on Linux and macOS, an HKCU registry key on Windows)
and prints how to load the extension. Do this once in the Chrome profile where
WhatsApp Web is logged in:
1. Open `chrome://extensions`.
2. Turn on **Developer mode**.
3. Click **Load unpacked** and choose the `whatsapp_mcp/extension` directory.
4. Check that the extension ID is `cboalnfagfemagdelpgjlafpbcjngiom`.
The extension attaches itself to an open WhatsApp Web tab. Its toolbar icon
shows the host and login status, with buttons to open WhatsApp Web and the
auto-reply side panel.
Then check:
```bash
./whatsapp status
```
If WhatsApp Web is not logged in, scan the QR code in the WhatsApp Web tab with
your phone: WhatsApp > Linked devices > Link a device. Logging in is always
done by you; nothing in this package touches the QR code.
## Chrome and its profiles
When Chrome is not running, `./whatsapp open` (the `open_whatsapp` tool)
starts it and opens WhatsApp Web. The profile used is:
1. the one requested (`--profile "Profile 3"` or its display name), or
`WHATSAPP_CHROME_PROFILE` in `.env`;
2. otherwise the most recently used profile, preferring profiles that already
have the WhatsApp MCP extension.
`./whatsapp profiles` lists every profile with a masked email, when it was
last active, and which one will be picked.
## Using it
### MCP (Claude Code)
```bash
claude mcp add whatsapp -- /path/to/whatsapp-mcp/whatsapp mcp
```
To get incoming messages pushed into a Claude Code session as channel
notifications, add the server to your project's `.mcp.json` and start:
```bash
claude --dangerously-load-development-channels server:whatsapp
```
Claude Code channels are a research preview. Without them, incoming messages
are still available through the `incoming_messages` tool. By default only
messages from saved contacts are pushed (`WHATSAPP_NOTIFY_FROM`). Every tool is
listed in [docs/MCP.md](docs/MCP.md).
### CLI
```bash
./whatsapp chats --search alice
./whatsapp messages "Alice Example" --limit 20
./whatsapp search "faculty meeting"
./whatsapp send Alice --text "Hello" --text "Did you arrive?" # dry run
./whatsapp send Alice --text "Hello" --send # for real
./whatsapp send-files Alice report.pdf photo.jpg --caption "Here you go"
./whatsapp watch # incoming messages
```
Every command is in [docs/CLI.md](docs/CLI.md).
### REST API
```bash
./whatsapp api # http://127.0.0.1:8000, interactive docs at /docs
```
Endpoints are in [docs/API.md](docs/API.md).
### Python library
```python
from whatsapp_mcp import WhatsAppClient, list_chats, send_message
client = WhatsAppClient()
print(list_chats(client, search="alice"))
plan = send_message(client, "Alice", ["Hello"]) # dry run
send_message(client, "Alice", ["Hello"], send=True) # for real
```
## Auto-reply
Reply profiles decide how incoming messages are answered. Contacts and profiles
are many to many: one profile ("Students", "Family") covers many contacts, a
contact (or group) can carry several profiles, and the highest priority wins.
Each profile has a mode:
| Mode | What happens |
|---|---|
| `template` | fixed text with `{name}`, `{chat_name}`, `{time}`, `{date}`, `{day}`; sent right away |
| `draft` | the Claude CLI writes a reply from the profile's instructions; it waits for your approval |
| `auto` | the Claude CLI writes the reply and it is sent right away |
A profile also has a scope (only linked contacts, any saved contact, or
everyone), active hours and days, a per-chat cooldown, and a switch for groups
(off by default). A global limit caps replies per hour.
Manage profiles in the extension's side panel (toolbar icon > Auto-reply
profiles), with `./whatsapp replies ...`, the MCP tools, or the REST API. Then
start the service with the buttons at the top of the side panel: **Dry run**
to try your profiles (every reply becomes a draft), **Run** to answer for real,
**Stop** to stop. The service runs inside the native messaging host, so it
works whenever Chrome is open, and Run is remembered when Chrome restarts. The
same controls exist elsewhere:
```bash
./whatsapp service start --dry-run # like Dry run in the side panel
./whatsapp service start # like Run
./whatsapp service status # running or not, counts, recent activity
./whatsapp service stop # like Stop
./whatsapp service # run it in this terminal instead, until Ctrl+C
```
Only one service runs at a time (an OS file lock), so no reply goes out twice.
Draft and auto replies are written by Claude Haiku (`WHATSAPP_REPLY_MODEL`).
Incoming messages are untrusted text. The reply writer runs
`claude -p --tools "" --strict-mcp-config`, so it has no tools and no MCP
servers: it can only return text, and that text only goes to the chat the
message came from. Drafts wait in the side panel, `./whatsapp replies drafts`,
or the `list_replies` tool until you approve or reject them.
## Sending is always a dry run first
Everything that writes to WhatsApp (`send_message`, `send_files`,
`send_contacts`, `pin_chat`, `pin_message`, `approve_draft`) is a dry run by
default. What comes back is a summary: the chat that was found (name and id),
the content, and the files. Sending only happens with `send=True` (CLI:
`--send`), and it goes to that resolved chat id, not to whatever name was
typed. A name that matches several chats is rejected with the list of choices.
If a command went out but its answer was lost, the result is `UncertainResult`
(MCP: `sent=true, ok=false`) and it is never retried automatically, so a
message is not sent twice.
## Settings
Copy `.env.example` to `.env` and fill in only what you need. Settings that are
safe to share can also go in `whatsapp.yaml` (see `whatsapp.yaml.example`).
Precedence: `configure()` in code, the environment, `.env`, `whatsapp.yaml`,
then the default.
| Setting | Default | Purpose |
|---|---|---|
| `WHATSAPP_CHROME_PROFILE` | most recently used | Chrome profile to open |
| `WHATSAPP_CHROME` | found automatically | path of the Chrome program |
| `WHATSAPP_CHROME_USER_DATA_DIR` | OS default | Chrome user data directory |
| `WHATSAPP_COUNTRY_CODE` | `62` | replaces a leading 0 in phone numbers |
| `WHATSAPP_API_TOKEN` | empty | REST API bearer token |
| `WHATSAPP_NOTIFY_FROM` | `contacts` | `contacts`, `all`, or `none` |
| `WHATSAPP_DESKTOP_NOTIFICATIONS` | `no` | desktop notifications from the MCP server |
| `WHATSAPP_SEND_DELAY_SECONDS` | `1.5` | pause between messages in one send |
| `WHATSAPP_MAX_ATTACHMENT_MB` | `64` | largest file accepted |
| `WHATSAPP_TIMEZONE` | `Asia/Jakarta` | time zone of dates in the output |
| `WHATSAPP_CLAUDE` | `claude` | Claude CLI used by draft and auto profiles |
| `WHATSAPP_REPLY_MODEL` | `haiku` | model the Claude CLI uses for replies |
| `WHATSAPP_MAX_REPLIES_PER_HOUR` | `20` | auto-reply limit across all chats |
## Security and limits
- Bridge cards (host port and token) and the reply database live in the user's
state directory with mode 0600: `~/.local/state/whatsapp-mcp` (Linux),
`~/Library/Application Support/whatsapp-mcp` (macOS),
`%LOCALAPPDATA%\whatsapp-mcp` (Windows).
- The host only listens on 127.0.0.1. Without `WHATSAPP_API_TOKEN` the REST API
only serves this computer.
- Reading messages through this package does not mark them as read.
- Incoming message content is written by other people. The assistant is told
to treat it as data, never as instructions.
- WhatsApp does not officially support automating a personal account, and bulk
sending can get a number banned. Keep volumes low.
- WhatsApp Web can change at any time. If its internal modules are renamed,
fix `whatsapp_mcp/extension/bridge.js` and run `./whatsapp reload`.
## Layout
```
whatsapp Linux/macOS launcher
whatsapp.cmd Windows launcher
whatsapp_mcp/
extension/ Chrome extension (manifest, background.js, bridge.js, relay.js, popup, side panel)
host.py native messaging host
whatsapp_client.py WhatsAppClient and errors
chrome.py the Chrome program, its profiles, and launching it
install.py host registration for Chrome
status.py check_login and open_whatsapp
chat.py, contacts.py, messages.py, attachments.py
autoreply.py reply profiles, contact links, drafts, the engine, and the service
writes.py WriteResult (dry run and send)
cli.py, api.py, mcp_server.py
docs/ UP-AND-RUNNING.md, MCP.md, CLI.md, API.md, images/
examples/ ready-to-run examples
tests/ unit tests (./whatsapp test)
```
## License
MIT