WhatsApp Claude MCP

WhatsApp Claude MCP

Enables WhatsApp bots to process incoming messages through Claude AI, supporting conversational memory and extensible MCP tools for tasks like fetching jokes from external APIs.

Category
访问服务器

README

WhatsApp Claude MCP

A powerful WhatsApp bot integrated with Claude AI using the Model Context Protocol (MCP). Send messages to your WhatsApp bot and get intelligent responses powered by Claude, with access to external APIs and tools.

🌟 Features

  • Claude AI Integration: Uses Claude 3.5 Sonnet for intelligent conversations
  • MCP Tools: Extensible tool system for Claude to interact with external APIs
  • Joke Generator: Built-in tool that fetches random jokes from external API
  • Conversation Memory: Maintains context across multiple messages per user
  • WhatsApp Webhook: Simple REST API for integrating with WhatsApp services
  • Easy Deployment: Works with express server, ready for cloud deployment

📋 Prerequisites

  • Node.js 18+
  • npm or yarn
  • Anthropic API Key (get from console.anthropic.com)
  • WhatsApp Cloud API access (for production integration)

🚀 Quick Start

1. Clone and Install

git clone https://github.com/yulianheroes-lgtm/whatsapp-claude-mcp.git
cd whatsapp-claude-mcp
npm install

2. Set Environment Variables

cp .env.example .env

Edit .env and add your Anthropic API key:

ANTHROPIC_API_KEY=your_anthropic_api_key_here
PORT=3000

3. Start the Server

npm start

You should see:

✅ WhatsApp Claude MCP Server running on http://localhost:3000
🤖 Ready to process WhatsApp messages!

📡 API Usage

Health Check

curl http://localhost:3000/health

Send Message to Claude

curl -X POST http://localhost:3000/webhook/whatsapp \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "1234567890",
    "message": "Tell me a joke"
  }'

Response:

{
  "success": true,
  "userId": "1234567890",
  "message": "😂 Here's a programming joke for you!\n\nWhy do programmers prefer dark mode?\n\nBecause light attracts bugs! 🐛"
}

Clear Conversation History

curl -X POST http://localhost:3000/webhook/clear-history \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "1234567890"
  }'

🛠️ Available Tools

Joke Generator

Claude can automatically use this tool when appropriate:

  • Trigger: When user asks for jokes
  • Types: random, programming, general
  • API: Official Joke API

Example interaction:

User: Tell me a funny programming joke
Bot: [Uses joke_generator tool] 😂 Here's a programming joke...

📁 Project Structure

whatsapp-claude-mcp/
├── src/
│   ├── index.js              # Main Express server
│   ├── whatsapp-handler.js   # Message handling & Claude integration
│   ├── mcp-server.js         # MCP tool definitions & execution
│   └── tools/
│       └── joke-generator.js # Joke generator tool implementation
├── .env.example              # Environment variables template
├── .gitignore               # Git ignore rules
├── package.json             # Dependencies
└── README.md                # This file

🔌 Integration with WhatsApp

Option 1: WhatsApp Cloud API

For production, integrate with WhatsApp Cloud API:

  1. Set up webhook on Meta Business Platform
  2. Point webhook URL to: https://your-domain.com/webhook/whatsapp
  3. When WhatsApp sends messages, forward them to this endpoint

Option 2: Local Testing

Use tools like curl, Postman, or a test script to send messages:

// test.js
const userId = '1234567890';
const message = 'Tell me a joke';

const response = await fetch('http://localhost:3000/webhook/whatsapp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ userId, message })
});

const result = await response.json();
console.log(result.message);

🧠 How It Works

  1. Message Received → WhatsApp webhook receives a message
  2. Claude Processing → Message sent to Claude with available tools
  3. Tool Selection → Claude decides if tools are needed
  4. Tool Execution → MCP server executes tools (e.g., fetch jokes)
  5. Response Generation → Claude generates response using tool results
  6. Message Sent → Response sent back via WhatsApp

🚀 Adding More Tools

To add a new tool (e.g., weather, translations):

1. Create Tool File

// src/tools/weather.js
export const weatherTool = {
  name: 'get_weather',
  description: 'Get current weather for a location',
  inputSchema: {
    type: 'object',
    properties: {
      location: { type: 'string', description: 'City name' }
    }
  }
};

export async function executeWeather(location) {
  // Fetch weather data
  return { /* weather data */ };
}

2. Register in MCP Server

// src/mcp-server.js
import { weatherTool, executeWeather } from './tools/weather.js';

export class MCPServer {
  constructor() {
    this.tools = [
      jokeGeneratorTool,
      weatherTool  // Add here
    ];
  }

  async processTool(toolName, toolInput) {
    switch (toolName) {
      case 'get_weather':
        return await executeWeather(toolInput.location);
      // ...
    }
  }
}

📚 API Reference

POST /webhook/whatsapp

Request Body:

{
  "userId": "string (required)",
  "message": "string (required)"
}

Response:

{
  "success": boolean,
  "userId": "string",
  "message": "string"
}

POST /webhook/clear-history

Request Body:

{
  "userId": "string (required)"
}

Response:

{
  "success": boolean,
  "message": "string"
}

🔐 Security Considerations

  • API Keys: Never commit .env file to version control
  • Rate Limiting: Consider adding rate limiting for production
  • Input Validation: Always validate webhook payloads
  • HTTPS: Use HTTPS in production
  • Authentication: Add webhook signature verification for WhatsApp integration

📝 Environment Variables

Variable Description Example
ANTHROPIC_API_KEY Claude API key sk-ant-...
PORT Server port 3000
NODE_ENV Environment development
JOKE_API_URL Joke API endpoint https://official-joke-api.appspot.com/random_joke

🤝 Contributing

Feel free to fork, modify, and contribute back!

📄 License

MIT License - see LICENSE file for details

🆘 Troubleshooting

"API key not found"

"Tool execution failed"

  • Check external APIs are accessible
  • Verify network connectivity
  • Review error logs in console output

"No response from Claude"

  • Check ANTHROPIC_API_KEY is correct
  • Ensure Claude model is available
  • Check API rate limits

📞 Support

For issues or questions:

  1. Check the troubleshooting section
  2. Review Claude API documentation
  3. Open an issue on GitHub

🎯 Future Enhancements

  • [ ] Support for image/media in WhatsApp messages
  • [ ] Additional tools (weather, news, translations)
  • [ ] Database for persistent conversation history
  • [ ] Rate limiting and authentication
  • [ ] Admin dashboard for monitoring
  • [ ] Multi-language support
  • [ ] Custom Claude system prompts per user

Made with ❤️ by yulianheroes-lgtm

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选