Labsco
mario-andreschak logo

WhatsApp Web

β˜… 16

from mario-andreschak

An MCP server for interacting with WhatsApp Web, allowing you to send and receive messages.

πŸ”₯πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeQuick setup

MCP WhatsApp Web (TypeScript)

A Model Context Protocol (MCP) server for WhatsApp Web, implemented in TypeScript. This project is a TypeScript port of the original whatsapp-mcp repository.

With this MCP server, you can:

  • Search and read your personal WhatsApp messages (including media)
  • Search your contacts
  • Send messages to individuals or groups
  • Send and receive media files (images, videos, documents, audio)

image image

Features

  • TypeScript Implementation: Fully typed codebase for better developer experience and code reliability
  • WhatsApp Web Integration: Uses whatsapp-web.js for direct connection to WhatsApp Web
  • MCP Server: Implements the Model Context Protocol for seamless integration with AI assistants
  • Media Support: Send and receive images, videos, documents, and audio messages
  • Multiple Transport Options: Supports stdio and Streamable HTTP transports β€” even both at once from a single process (start with stdio and set MCP_HTTP_PORT to additionally expose http://127.0.0.1:<port>/mcp, or run HTTP-only with --http)
  • Flexible Authentication: QR code (as an MCP image tool), pairing code (request_pairing_code tool, or automatically printed to stderr at startup via WHATSAPP_PAIRING_PHONE_NUMBER), and an optional OAuth flow for HTTP clients (MCP_OAUTH=true) where the browser authorization page shows the WhatsApp QR code β€” unlinking WhatsApp revokes tokens so clients automatically re-authenticate

Architecture

This MCP server consists of:

  1. TypeScript MCP Server: Implements the Model Context Protocol to provide standardized tools for AI assistants to interact with WhatsApp
  2. WhatsApp Web Service: Connects to WhatsApp Web via whatsapp-web.js, handles authentication, and manages message sending/receiving
  3. Tool Implementations: Provides various tools for contacts, chats, messages, media, and authentication

Authentication

The first time you run the server, you'll need to authenticate with WhatsApp:

  1. Start the MCP server
  2. Important: You must use the get_qr_code tool to generate a QR code
    • In Claude or other AI assistants, explicitly ask to "use the get_qr_code tool to authenticate WhatsApp"
    • The assistant will call this tool and display the QR code image
  3. Scan the QR code with your WhatsApp mobile app
    • Open WhatsApp on your phone
    • Go to Settings > Linked Devices > Link a Device
    • Point your phone camera at the QR code displayed

Your session will be saved locally in the whatsapp-sessions directory and will be reused automatically on subsequent runs. If you don't authenticate using the QR code, you won't be able to use any WhatsApp functionality.

Authentication Status and Logout

You can check your current authentication status and manage your session:

  • Use the check_auth_status tool to verify if you're currently authenticated
  • If you need to authenticate with a different WhatsApp account or re-authenticate:
    1. Use the logout tool to log out from your current session
    2. Then use the get_qr_code tool to authenticate with a new QR code

This is particularly useful when:

  • You want to switch between different WhatsApp accounts
  • Your session has expired or been invalidated
  • You're experiencing connection issues and need to re-authenticate

Available MCP Tools

Authentication

  • get_qr_code- Get the QR code for WhatsApp Web authentication
  • check_auth_status- Check if you're currently authenticated with WhatsApp
  • logout- Log out from WhatsApp and clear the current session

Contacts

  • search_contacts- Search for contacts by name or phone number
  • get_contact- Get information about a specific contact

Chats

  • list_chats- List available chats with metadata
  • get_chat- Get information about a specific chat
  • get_direct_chat_by_contact- Find a direct chat with a specific contact

Messages

  • list_messages- Retrieve messages with optional filters
  • get_message- Get a specific message by ID
  • send_message- Send a text message to a chat

Media

  • send_file- Send a file (image, video, document) to a chat
  • send_audio_message- Send an audio message (voice note)
  • download_media- Download media from a message

Browser Process Management

This MCP server uses Puppeteer to control Chrome browsers for WhatsApp Web connectivity. The server includes a robust browser process management system to prevent orphaned Chrome processes.

Automatic Browser Cleanup

The server automatically:

  • Tracks Chrome browser processes using a PID tracking system
  • Cleans up orphaned processes on startup
  • Properly closes browser processes during shutdown
  • Maintains a record of browser PIDs in .chrome-pids.json

Manual Browser Cleanup

If you notice orphaned Chrome processes that weren't automatically cleaned up, you can use the included cleanup utility:

npm run cleanup-browsers

This utility will:

  1. Scan for Chrome processes that might be related to WhatsApp Web
  2. Display a list of potentially orphaned processes
  3. Ask for confirmation before terminating them
  4. Clean up the PID tracking file

Development

Project Structure

  • src/index.ts- Entry point
  • src/server.ts- MCP server implementation
  • src/services/whatsapp.ts- WhatsApp Web service
  • src/tools/- Tool implementations for various WhatsApp features
  • src/types/- TypeScript type definitions
  • src/utils/- Utility functions

Scripts

  • npm run build- Build the TypeScript code
  • npm run dev- Run in development mode with watch
  • npm run lint- Run ESLint
  • npm run format- Format code with Prettier
  • npm run cleanup-browsers- Detect and clean up orphaned Chrome browser processes
  • npm test - Run the unit test suite (fast, no browser needed)
  • npm run test:watch - Run unit tests in watch mode during development
  • npm run test:e2e - Build, then run end-to-end tests (spawns the real server incl. a headless browser)