@wsapichat/mcp-server 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +96 -0
- package/LICENSE +21 -0
- package/README.md +403 -0
- package/dist/client/index.d.ts +16 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +154 -0
- package/dist/client/index.js.map +1 -0
- package/dist/config/index.d.ts +85 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +67 -0
- package/dist/config/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +46 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +20 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +173 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/account.d.ts +14 -0
- package/dist/tools/account.d.ts.map +1 -0
- package/dist/tools/account.js +78 -0
- package/dist/tools/account.js.map +1 -0
- package/dist/tools/chats.d.ts +14 -0
- package/dist/tools/chats.d.ts.map +1 -0
- package/dist/tools/chats.js +81 -0
- package/dist/tools/chats.js.map +1 -0
- package/dist/tools/contacts.d.ts +18 -0
- package/dist/tools/contacts.d.ts.map +1 -0
- package/dist/tools/contacts.js +213 -0
- package/dist/tools/contacts.js.map +1 -0
- package/dist/tools/groups.d.ts +12 -0
- package/dist/tools/groups.d.ts.map +1 -0
- package/dist/tools/groups.js +65 -0
- package/dist/tools/groups.js.map +1 -0
- package/dist/tools/instance.d.ts +12 -0
- package/dist/tools/instance.d.ts.map +1 -0
- package/dist/tools/instance.js +54 -0
- package/dist/tools/instance.js.map +1 -0
- package/dist/tools/messaging-advanced.d.ts +16 -0
- package/dist/tools/messaging-advanced.d.ts.map +1 -0
- package/dist/tools/messaging-advanced.js +386 -0
- package/dist/tools/messaging-advanced.js.map +1 -0
- package/dist/tools/messaging.d.ts +28 -0
- package/dist/tools/messaging.d.ts.map +1 -0
- package/dist/tools/messaging.js +491 -0
- package/dist/tools/messaging.js.map +1 -0
- package/dist/tools/session.d.ts +14 -0
- package/dist/tools/session.d.ts.map +1 -0
- package/dist/tools/session.js +61 -0
- package/dist/tools/session.js.map +1 -0
- package/dist/types/api.d.ts +2730 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/api.js +6 -0
- package/dist/types/api.js.map +1 -0
- package/dist/types/webhooks.d.ts +1001 -0
- package/dist/types/webhooks.d.ts.map +1 -0
- package/dist/types/webhooks.js +6 -0
- package/dist/types/webhooks.js.map +1 -0
- package/dist/utils/errors.d.ts +39 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +151 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/logger.d.ts +12 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +99 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/validation/schemas.d.ts +732 -0
- package/dist/validation/schemas.d.ts.map +1 -0
- package/dist/validation/schemas.js +269 -0
- package/dist/validation/schemas.js.map +1 -0
- package/package.json +83 -0
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
This is a TypeScript-based Model Context Protocol (MCP) server that provides LLMs with WhatsApp functionality through the WSAPI service. The server exposes WhatsApp messaging, contact management, group operations, and session handling as MCP tools.
|
|
8
|
+
|
|
9
|
+
## Development Commands
|
|
10
|
+
|
|
11
|
+
### Build & Run
|
|
12
|
+
- `npm run build` - Compile TypeScript to JavaScript in dist/ directory
|
|
13
|
+
- `npm run dev` - Run in development mode with tsx
|
|
14
|
+
- `npm start` - Run compiled server from dist/
|
|
15
|
+
- `npm run clean` - Remove dist/ directory
|
|
16
|
+
|
|
17
|
+
### Code Quality
|
|
18
|
+
- `npm run lint` - Run ESLint on TypeScript files
|
|
19
|
+
- `npm run lint:fix` - Fix ESLint issues automatically
|
|
20
|
+
- `npm run type-check` - Run TypeScript compiler without emitting files
|
|
21
|
+
|
|
22
|
+
### Testing
|
|
23
|
+
- `npm test` - Run Jest test suite
|
|
24
|
+
- `npm run test:watch` - Run tests in watch mode
|
|
25
|
+
|
|
26
|
+
### Type Generation
|
|
27
|
+
- `npm run generate-types` - Generate TypeScript types from wsapi.yml OpenAPI spec
|
|
28
|
+
|
|
29
|
+
## Architecture
|
|
30
|
+
|
|
31
|
+
### Core Components
|
|
32
|
+
- `src/index.ts` - Entry point with server startup and graceful shutdown
|
|
33
|
+
- `src/server.ts` - WSAPIMCPServer class that manages MCP server and tool registration
|
|
34
|
+
- `src/client/index.ts` - WSAPI HTTP client wrapper
|
|
35
|
+
- `src/config/index.ts` - Environment configuration management
|
|
36
|
+
|
|
37
|
+
### Tool Organization
|
|
38
|
+
Tools are organized by functionality in `src/tools/`:
|
|
39
|
+
- `messaging.ts` - Basic message sending (text, media, stickers, etc.)
|
|
40
|
+
- `messaging-advanced.ts` - Advanced message operations (edit, delete, reactions)
|
|
41
|
+
- `contacts.ts` - Contact management and presence
|
|
42
|
+
- `groups.ts` - Group creation and management
|
|
43
|
+
- `chats.ts` - Chat operations (archive, pin, presence)
|
|
44
|
+
- `session.ts` - WhatsApp session management (QR codes, logout)
|
|
45
|
+
- `instance.ts` - WSAPI instance configuration
|
|
46
|
+
- `account.ts` - Account profile management
|
|
47
|
+
|
|
48
|
+
### Utilities
|
|
49
|
+
- `src/utils/logger.ts` - Winston-based logging with structured output
|
|
50
|
+
- `src/utils/errors.ts` - Error handling and user-friendly error messages
|
|
51
|
+
- `src/validation/schemas.ts` - Zod schemas for input validation
|
|
52
|
+
- `src/types/api.ts` - Generated TypeScript types from OpenAPI spec
|
|
53
|
+
|
|
54
|
+
## Key Technical Details
|
|
55
|
+
|
|
56
|
+
### Module System
|
|
57
|
+
- Uses ES modules (`"type": "module"` in package.json)
|
|
58
|
+
- All imports must use `.js` extensions for compiled output
|
|
59
|
+
- TypeScript compiles to ESNext targeting ES2022
|
|
60
|
+
|
|
61
|
+
### Error Handling
|
|
62
|
+
- Custom error classes for different error types (ValidationError, APIError, etc.)
|
|
63
|
+
- Centralized error handling with user-friendly messages
|
|
64
|
+
- Comprehensive logging for debugging
|
|
65
|
+
|
|
66
|
+
### Configuration
|
|
67
|
+
Required environment variables:
|
|
68
|
+
- `WSAPI_API_KEY` - API authentication key
|
|
69
|
+
- `WSAPI_INSTANCE_ID` - WSAPI instance identifier
|
|
70
|
+
- `WSAPI_BASE_URL` - API base URL (defaults to https://api.wsapi.chat)
|
|
71
|
+
|
|
72
|
+
### Tool Pattern
|
|
73
|
+
Each tool follows a consistent pattern:
|
|
74
|
+
- Zod schema for input validation
|
|
75
|
+
- Handler function that calls WSAPI client
|
|
76
|
+
- Error handling with proper MCP responses
|
|
77
|
+
- Structured logging
|
|
78
|
+
|
|
79
|
+
### Testing Setup
|
|
80
|
+
- Jest configured for ES modules with ts-jest
|
|
81
|
+
- Test files use `.test.ts` or `.spec.ts` extensions
|
|
82
|
+
- Coverage collection excludes test files and type definitions
|
|
83
|
+
|
|
84
|
+
## Development Notes
|
|
85
|
+
|
|
86
|
+
### Building
|
|
87
|
+
Always run `npm run build` after changes before testing with `npm start`. The MCP server runs the compiled JavaScript, not TypeScript directly.
|
|
88
|
+
|
|
89
|
+
### API Types
|
|
90
|
+
When the WSAPI specification changes, regenerate types with `npm run generate-types` to update `src/types/api.ts`.
|
|
91
|
+
|
|
92
|
+
### Logging
|
|
93
|
+
Use the logger from `src/utils/logger.ts` for consistent structured logging. Log levels: error, warn, info, debug.
|
|
94
|
+
|
|
95
|
+
### Tool Registration
|
|
96
|
+
New tools must be registered in the appropriate tool module and imported/registered in `src/server.ts`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 WSAPI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
# @wsapichat/mcp-server
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/@wsapichat%2Fmcp-server)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
A comprehensive Model Context Protocol (MCP) server that provides LLMs with seamless access to WhatsApp functionality through the WSAPI service. This server enables AI assistants to send messages, manage contacts and groups, handle chat operations, and monitor session status.
|
|
7
|
+
|
|
8
|
+
## 🚀 Features
|
|
9
|
+
|
|
10
|
+
### 📱 Core Messaging
|
|
11
|
+
- **Text Messages**: Send rich text with mentions and replies
|
|
12
|
+
- **Media Messages**: Send images, videos, audio files, and voice messages
|
|
13
|
+
- **Documents**: Share files with captions
|
|
14
|
+
- **Stickers**: Send animated and static stickers
|
|
15
|
+
- **Location**: Share location data with coordinates and place information
|
|
16
|
+
- **Contacts**: Send vCard contacts
|
|
17
|
+
- **Links**: Send URLs with rich preview metadata
|
|
18
|
+
- **Reactions**: React to messages with emojis
|
|
19
|
+
|
|
20
|
+
### 📧 Message Management
|
|
21
|
+
- **Edit Messages**: Modify sent text messages
|
|
22
|
+
- **Delete Messages**: Remove messages for all participants or just yourself
|
|
23
|
+
- **Star Messages**: Mark important messages
|
|
24
|
+
- **Mark as Read**: Update message read status
|
|
25
|
+
- **Reply to Messages**: Create threaded conversations
|
|
26
|
+
|
|
27
|
+
### 👥 Contact Management
|
|
28
|
+
- **List Contacts**: Get all WhatsApp contacts
|
|
29
|
+
- **Contact Details**: Retrieve contact information and business profiles
|
|
30
|
+
- **Create/Update Contacts**: Manage your contact list
|
|
31
|
+
- **Profile Pictures**: Get contact profile images
|
|
32
|
+
- **Presence Subscription**: Monitor contact online status
|
|
33
|
+
|
|
34
|
+
### 🏢 Group Management
|
|
35
|
+
- **Create Groups**: Start new group chats with participants
|
|
36
|
+
- **Group Info**: Get group details and participant lists
|
|
37
|
+
- **Manage Participants**: Add/remove group members
|
|
38
|
+
- **Group Settings**: Update name, description, and picture
|
|
39
|
+
- **Invite Links**: Generate and manage group invite links
|
|
40
|
+
|
|
41
|
+
### 💬 Chat Management
|
|
42
|
+
- **List Chats**: Get all active conversations
|
|
43
|
+
- **Chat Presence**: Set typing/recording indicators
|
|
44
|
+
- **Archive/Unarchive**: Organize chat list
|
|
45
|
+
- **Pin/Unpin**: Prioritize important chats
|
|
46
|
+
- **Mute/Unmute**: Control notifications
|
|
47
|
+
- **Ephemeral Messages**: Configure disappearing messages
|
|
48
|
+
|
|
49
|
+
### 🔐 Session & Instance Management
|
|
50
|
+
- **Session Status**: Monitor connection state
|
|
51
|
+
- **QR Code Login**: Get QR codes for device pairing
|
|
52
|
+
- **Pair Code**: Generate pairing codes for phone linking
|
|
53
|
+
- **Logout**: Disconnect from WhatsApp
|
|
54
|
+
- **Instance Settings**: Configure instance preferences
|
|
55
|
+
- **API Key Management**: Generate new authentication keys
|
|
56
|
+
|
|
57
|
+
### 👤 Account Management
|
|
58
|
+
- **Profile Info**: Get account details
|
|
59
|
+
- **Display Name**: Update account name
|
|
60
|
+
- **Profile Picture**: Change profile image
|
|
61
|
+
- **Status Message**: Update account status
|
|
62
|
+
- **Presence Status**: Set availability (available/unavailable)
|
|
63
|
+
|
|
64
|
+
## 📦 Installation
|
|
65
|
+
|
|
66
|
+
### Prerequisites
|
|
67
|
+
- Node.js 18.0.0 or higher
|
|
68
|
+
- npm or yarn package manager
|
|
69
|
+
- WSAPI account with API credentials
|
|
70
|
+
|
|
71
|
+
### NPM Installation
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
npm install -g @wsapichat/mcp-server
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Usage with MCP Clients
|
|
78
|
+
|
|
79
|
+
#### Claude Desktop Configuration
|
|
80
|
+
|
|
81
|
+
Add the server to your Claude Desktop configuration file:
|
|
82
|
+
|
|
83
|
+
**macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
84
|
+
**Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
85
|
+
|
|
86
|
+
```json
|
|
87
|
+
{
|
|
88
|
+
"mcpServers": {
|
|
89
|
+
"wsapi": {
|
|
90
|
+
"command": "npx",
|
|
91
|
+
"args": ["@wsapichat/mcp-server"],
|
|
92
|
+
"env": {
|
|
93
|
+
"WSAPI_API_KEY": "your_api_key_here",
|
|
94
|
+
"WSAPI_INSTANCE_ID": "your_instance_id_here"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Alternative: Local Installation
|
|
102
|
+
|
|
103
|
+
1. **Install the package:**
|
|
104
|
+
```bash
|
|
105
|
+
npm install @wsapichat/mcp-server
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
2. **Configure environment variables:**
|
|
109
|
+
```env
|
|
110
|
+
WSAPI_API_KEY=your_api_key_here
|
|
111
|
+
WSAPI_INSTANCE_ID=your_instance_id_here
|
|
112
|
+
WSAPI_BASE_URL=https://api.wsapi.chat
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
3. **Add to MCP client configuration:**
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"mcpServers": {
|
|
119
|
+
"wsapi": {
|
|
120
|
+
"command": "node",
|
|
121
|
+
"args": ["./node_modules/@wsapichat/mcp-server/dist/index.js"],
|
|
122
|
+
"env": {
|
|
123
|
+
"WSAPI_API_KEY": "your_api_key_here",
|
|
124
|
+
"WSAPI_INSTANCE_ID": "your_instance_id_here"
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Development Setup
|
|
132
|
+
|
|
133
|
+
1. **Clone and install dependencies:**
|
|
134
|
+
```bash
|
|
135
|
+
git clone https://github.com/wsapi-chat/wsapi-mcp
|
|
136
|
+
cd wsapi-mcp
|
|
137
|
+
npm install
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
2. **Configure environment:**
|
|
141
|
+
```bash
|
|
142
|
+
cp .env.example .env
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
3. **Edit `.env` file with your WSAPI credentials:**
|
|
146
|
+
```env
|
|
147
|
+
WSAPI_API_KEY=your_api_key_here
|
|
148
|
+
WSAPI_INSTANCE_ID=your_instance_id_here
|
|
149
|
+
WSAPI_BASE_URL=https://api.wsapi.chat
|
|
150
|
+
|
|
151
|
+
# Server configuration
|
|
152
|
+
LOG_LEVEL=info
|
|
153
|
+
NODE_ENV=development
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
4. **Build and start:**
|
|
157
|
+
```bash
|
|
158
|
+
npm run build
|
|
159
|
+
npm start
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## 🔧 Development
|
|
163
|
+
|
|
164
|
+
### Development Mode
|
|
165
|
+
```bash
|
|
166
|
+
npm run dev
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Type Checking
|
|
170
|
+
```bash
|
|
171
|
+
npm run type-check
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Linting
|
|
175
|
+
```bash
|
|
176
|
+
npm run lint
|
|
177
|
+
npm run lint:fix
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Testing
|
|
181
|
+
```bash
|
|
182
|
+
npm test
|
|
183
|
+
npm run test:watch
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Generate Types from OpenAPI
|
|
187
|
+
```bash
|
|
188
|
+
npm run generate-types
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## 🛠️ Configuration
|
|
192
|
+
|
|
193
|
+
### Environment Variables
|
|
194
|
+
|
|
195
|
+
| Variable | Required | Default | Description |
|
|
196
|
+
|----------|----------|---------|-------------|
|
|
197
|
+
| `WSAPI_API_KEY` | Yes | - | Your WSAPI API key |
|
|
198
|
+
| `WSAPI_INSTANCE_ID` | Yes | - | Your WSAPI instance ID |
|
|
199
|
+
| `WSAPI_BASE_URL` | No | `https://api.wsapi.chat` | WSAPI base URL |
|
|
200
|
+
| `WSAPI_TIMEOUT` | No | `30000` | Request timeout in milliseconds |
|
|
201
|
+
| `WSAPI_RETRY_ATTEMPTS` | No | `3` | Number of retry attempts |
|
|
202
|
+
| `WSAPI_RETRY_DELAY` | No | `1000` | Delay between retries in milliseconds |
|
|
203
|
+
| `PORT` | No | `3000` | Server port |
|
|
204
|
+
| `LOG_LEVEL` | No | `info` | Logging level (error, warn, info, debug) |
|
|
205
|
+
| `NODE_ENV` | No | `development` | Environment (development, production, test) |
|
|
206
|
+
|
|
207
|
+
### Getting WSAPI Credentials
|
|
208
|
+
|
|
209
|
+
1. Visit [WSAPI.chat](https://wsapi.chat) and create an account
|
|
210
|
+
2. Create a new WhatsApp instance
|
|
211
|
+
3. Get your API key and Instance ID from the dashboard
|
|
212
|
+
4. Configure your instance settings as needed
|
|
213
|
+
|
|
214
|
+
## 📚 Usage Examples
|
|
215
|
+
|
|
216
|
+
### Send a Text Message
|
|
217
|
+
```typescript
|
|
218
|
+
// Tool: whatsapp_send_text
|
|
219
|
+
{
|
|
220
|
+
"to": "1234567890@s.whatsapp.net",
|
|
221
|
+
"text": "Hello! How are you today?",
|
|
222
|
+
"mentions": ["1234567890@s.whatsapp.net"]
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Send an Image with Caption
|
|
227
|
+
```typescript
|
|
228
|
+
// Tool: whatsapp_send_image
|
|
229
|
+
{
|
|
230
|
+
"to": "1234567890@s.whatsapp.net",
|
|
231
|
+
"imageURL": "https://example.com/image.jpg",
|
|
232
|
+
"mimeType": "image/jpeg",
|
|
233
|
+
"caption": "Check out this amazing photo!"
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Create a Group
|
|
238
|
+
```typescript
|
|
239
|
+
// Tool: whatsapp_create_group
|
|
240
|
+
{
|
|
241
|
+
"name": "Project Team",
|
|
242
|
+
"participants": [
|
|
243
|
+
"1234567890@s.whatsapp.net",
|
|
244
|
+
"0987654321@s.whatsapp.net"
|
|
245
|
+
]
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Send Location
|
|
250
|
+
```typescript
|
|
251
|
+
// Tool: whatsapp_send_location
|
|
252
|
+
{
|
|
253
|
+
"to": "1234567890@s.whatsapp.net",
|
|
254
|
+
"latitude": 37.7749,
|
|
255
|
+
"longitude": -122.4194,
|
|
256
|
+
"name": "San Francisco",
|
|
257
|
+
"address": "San Francisco, CA, USA"
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Set Chat Presence
|
|
262
|
+
```typescript
|
|
263
|
+
// Tool: whatsapp_set_chat_presence
|
|
264
|
+
{
|
|
265
|
+
"chatId": "1234567890@s.whatsapp.net",
|
|
266
|
+
"state": "typing"
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## 🔗 Available Tools
|
|
271
|
+
|
|
272
|
+
### Messaging Tools
|
|
273
|
+
- `whatsapp_send_text` - Send text messages
|
|
274
|
+
- `whatsapp_send_image` - Send image messages
|
|
275
|
+
- `whatsapp_send_video` - Send video messages
|
|
276
|
+
- `whatsapp_send_audio` - Send audio files
|
|
277
|
+
- `whatsapp_send_voice` - Send voice messages
|
|
278
|
+
- `whatsapp_send_document` - Send document files
|
|
279
|
+
- `whatsapp_send_sticker` - Send stickers
|
|
280
|
+
- `whatsapp_send_location` - Send location data
|
|
281
|
+
- `whatsapp_send_contact` - Send contact vCards
|
|
282
|
+
- `whatsapp_send_link` - Send links with preview
|
|
283
|
+
- `whatsapp_send_reaction` - React to messages
|
|
284
|
+
- `whatsapp_edit_message` - Edit sent messages
|
|
285
|
+
- `whatsapp_delete_message` - Delete messages
|
|
286
|
+
- `whatsapp_mark_message_read` - Mark messages as read
|
|
287
|
+
- `whatsapp_star_message` - Star messages
|
|
288
|
+
|
|
289
|
+
### Contact Tools
|
|
290
|
+
- `whatsapp_get_contacts` - List all contacts
|
|
291
|
+
- `whatsapp_get_contact` - Get contact details
|
|
292
|
+
- `whatsapp_create_contact` - Create new contact
|
|
293
|
+
- `whatsapp_update_contact` - Update contact info
|
|
294
|
+
- `whatsapp_get_contact_picture` - Get contact profile picture
|
|
295
|
+
- `whatsapp_get_contact_business` - Get business profile
|
|
296
|
+
- `whatsapp_subscribe_contact_presence` - Monitor contact status
|
|
297
|
+
|
|
298
|
+
### Group Tools
|
|
299
|
+
- `whatsapp_get_groups` - List all groups
|
|
300
|
+
- `whatsapp_create_group` - Create new group
|
|
301
|
+
- `whatsapp_get_group` - Get group details
|
|
302
|
+
- `whatsapp_update_group_name` - Change group name
|
|
303
|
+
|
|
304
|
+
### Chat Tools
|
|
305
|
+
- `whatsapp_get_chats` - List all chats
|
|
306
|
+
- `whatsapp_get_chat` - Get chat details
|
|
307
|
+
- `whatsapp_set_chat_presence` - Set typing/recording status
|
|
308
|
+
- `whatsapp_archive_chat` - Archive/unarchive chats
|
|
309
|
+
- `whatsapp_pin_chat` - Pin/unpin chats
|
|
310
|
+
|
|
311
|
+
### Session Tools
|
|
312
|
+
- `whatsapp_get_session_status` - Check connection status
|
|
313
|
+
- `whatsapp_get_qr_code` - Get QR code for login
|
|
314
|
+
- `whatsapp_get_qr_code_image` - Get QR code image
|
|
315
|
+
- `whatsapp_get_pair_code` - Get pairing code
|
|
316
|
+
- `whatsapp_logout` - Logout from WhatsApp
|
|
317
|
+
|
|
318
|
+
### Instance Tools
|
|
319
|
+
- `whatsapp_get_instance_settings` - Get instance configuration
|
|
320
|
+
- `whatsapp_update_instance_settings` - Update instance settings
|
|
321
|
+
- `whatsapp_restart_instance` - Restart instance
|
|
322
|
+
- `whatsapp_update_api_key` - Generate new API key
|
|
323
|
+
|
|
324
|
+
### Account Tools
|
|
325
|
+
- `whatsapp_get_account_info` - Get account information
|
|
326
|
+
- `whatsapp_set_account_name` - Update display name
|
|
327
|
+
- `whatsapp_set_account_picture` - Update profile picture
|
|
328
|
+
- `whatsapp_set_account_presence` - Set availability status
|
|
329
|
+
- `whatsapp_set_account_status` - Update status message
|
|
330
|
+
|
|
331
|
+
## 🐛 Troubleshooting
|
|
332
|
+
|
|
333
|
+
### Common Issues
|
|
334
|
+
|
|
335
|
+
1. **Authentication Errors**
|
|
336
|
+
- Verify your API key and instance ID are correct
|
|
337
|
+
- Check that your WSAPI subscription is active
|
|
338
|
+
- Ensure the instance is properly configured
|
|
339
|
+
|
|
340
|
+
2. **Connection Issues**
|
|
341
|
+
- Check your internet connection
|
|
342
|
+
- Verify the WSAPI service status
|
|
343
|
+
- Review firewall settings
|
|
344
|
+
|
|
345
|
+
3. **Message Sending Failures**
|
|
346
|
+
- Ensure the recipient number format is correct (include @s.whatsapp.net)
|
|
347
|
+
- Check that the WhatsApp session is active
|
|
348
|
+
- Verify media file formats and sizes
|
|
349
|
+
|
|
350
|
+
4. **Validation Errors**
|
|
351
|
+
- Review input parameters against the tool schemas
|
|
352
|
+
- Check data types and required fields
|
|
353
|
+
- Ensure string lengths don't exceed limits
|
|
354
|
+
|
|
355
|
+
### Debug Mode
|
|
356
|
+
|
|
357
|
+
Enable debug logging for detailed troubleshooting:
|
|
358
|
+
|
|
359
|
+
```bash
|
|
360
|
+
LOG_LEVEL=debug npm start
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Health Check
|
|
364
|
+
|
|
365
|
+
The server includes a built-in health check that verifies API connectivity on startup.
|
|
366
|
+
|
|
367
|
+
## 🤝 Contributing
|
|
368
|
+
|
|
369
|
+
1. Fork the repository
|
|
370
|
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
|
371
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
372
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
373
|
+
5. Open a Pull Request
|
|
374
|
+
|
|
375
|
+
### Development Guidelines
|
|
376
|
+
|
|
377
|
+
- Follow TypeScript best practices
|
|
378
|
+
- Add tests for new functionality
|
|
379
|
+
- Update documentation for new features
|
|
380
|
+
- Run linting and type checking before committing
|
|
381
|
+
- Follow the existing code style and conventions
|
|
382
|
+
|
|
383
|
+
## 📄 License
|
|
384
|
+
|
|
385
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
386
|
+
|
|
387
|
+
## 🔗 Links
|
|
388
|
+
|
|
389
|
+
- [WSAPI Documentation](https://docs.wsapi.chat)
|
|
390
|
+
- [Model Context Protocol](https://modelcontextprotocol.io)
|
|
391
|
+
- [OpenAPI Specification](./wsapi.yml)
|
|
392
|
+
|
|
393
|
+
## 📞 Support
|
|
394
|
+
|
|
395
|
+
For support and questions:
|
|
396
|
+
|
|
397
|
+
- Create an issue in this repository
|
|
398
|
+
- Check the [WSAPI documentation](https://docs.wsapi.chat)
|
|
399
|
+
- Contact WSAPI support for API-related issues
|
|
400
|
+
|
|
401
|
+
---
|
|
402
|
+
|
|
403
|
+
Built with ❤️ using TypeScript, Zod, and the Model Context Protocol SDK.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class WSAPIClient {
|
|
2
|
+
private readonly client;
|
|
3
|
+
constructor();
|
|
4
|
+
private setupInterceptors;
|
|
5
|
+
private request;
|
|
6
|
+
get(path: string, params?: any): Promise<any>;
|
|
7
|
+
post(path: string, data: any, params?: any): Promise<any>;
|
|
8
|
+
put(path: string, data: any, params?: any): Promise<any>;
|
|
9
|
+
delete(path: string, params?: any): Promise<void>;
|
|
10
|
+
private buildUrl;
|
|
11
|
+
healthCheck(): Promise<boolean>;
|
|
12
|
+
getInstanceInfo(): Promise<any>;
|
|
13
|
+
}
|
|
14
|
+
export declare const wsapiClient: WSAPIClient;
|
|
15
|
+
export default WSAPIClient;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAqDA,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;;IAiBvC,OAAO,CAAC,iBAAiB;YA8DX,OAAO;IAsBf,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAS7C,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAUzD,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAUxD,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IASvD,OAAO,CAAC,QAAQ;IA8BV,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAW/B,eAAe,IAAI,OAAO,CAAC,GAAG,CAAC;CAGtC;AAGD,eAAO,MAAM,WAAW,aAAoB,CAAC;AAC7C,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { config } from '../config/index.js';
|
|
3
|
+
import { createLogger, logRequest } from '../utils/logger.js';
|
|
4
|
+
import { handleError, withRetry } from '../utils/errors.js';
|
|
5
|
+
const logger = createLogger('wsapi-client');
|
|
6
|
+
export class WSAPIClient {
|
|
7
|
+
client;
|
|
8
|
+
constructor() {
|
|
9
|
+
this.client = axios.create({
|
|
10
|
+
baseURL: config.wsapi.baseUrl,
|
|
11
|
+
timeout: config.wsapi.timeout,
|
|
12
|
+
headers: {
|
|
13
|
+
'Content-Type': 'application/json',
|
|
14
|
+
'X-Api-Key': config.wsapi.apiKey,
|
|
15
|
+
'X-Instance-Id': config.wsapi.instanceId,
|
|
16
|
+
'User-Agent': 'WSAPI-MCP-Server/1.0.0',
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
this.setupInterceptors();
|
|
20
|
+
}
|
|
21
|
+
setupInterceptors() {
|
|
22
|
+
// Request interceptor
|
|
23
|
+
this.client.interceptors.request.use((config) => {
|
|
24
|
+
const startTime = Date.now();
|
|
25
|
+
config.metadata = { startTime };
|
|
26
|
+
logger.debug('Making API request', {
|
|
27
|
+
method: config.method?.toUpperCase(),
|
|
28
|
+
url: config.url,
|
|
29
|
+
headers: { ...config.headers, 'X-Api-Key': '[REDACTED]' },
|
|
30
|
+
});
|
|
31
|
+
return config;
|
|
32
|
+
}, (error) => {
|
|
33
|
+
logger.error('Request interceptor error', { error: error.message });
|
|
34
|
+
return Promise.reject(error);
|
|
35
|
+
});
|
|
36
|
+
// Response interceptor
|
|
37
|
+
this.client.interceptors.response.use((response) => {
|
|
38
|
+
const duration = response.config.metadata?.startTime
|
|
39
|
+
? Date.now() - response.config.metadata.startTime
|
|
40
|
+
: undefined;
|
|
41
|
+
logRequest(response.config.method?.toUpperCase() || 'UNKNOWN', response.config.url || 'UNKNOWN', response.status, duration);
|
|
42
|
+
logger.debug('API request successful', {
|
|
43
|
+
method: response.config.method?.toUpperCase(),
|
|
44
|
+
url: response.config.url,
|
|
45
|
+
status: response.status,
|
|
46
|
+
duration,
|
|
47
|
+
});
|
|
48
|
+
return response;
|
|
49
|
+
}, (error) => {
|
|
50
|
+
const duration = error.config?.metadata?.startTime
|
|
51
|
+
? Date.now() - error.config.metadata.startTime
|
|
52
|
+
: undefined;
|
|
53
|
+
logRequest(error.config?.method?.toUpperCase() || 'UNKNOWN', error.config?.url || 'UNKNOWN', error.response?.status, duration);
|
|
54
|
+
return Promise.reject(error);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// Generic request method with retry logic
|
|
58
|
+
async request(requestConfig) {
|
|
59
|
+
return withRetry(async () => {
|
|
60
|
+
try {
|
|
61
|
+
const response = await this.client.request(requestConfig);
|
|
62
|
+
return response.data;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
throw handleError(error, {
|
|
66
|
+
method: requestConfig.method,
|
|
67
|
+
url: requestConfig.url,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}, {
|
|
71
|
+
maxAttempts: config.wsapi.retryAttempts,
|
|
72
|
+
delay: config.wsapi.retryDelay,
|
|
73
|
+
retryableErrors: ['NETWORK_ERROR', 'INSTANCE_ERROR'],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
// GET request
|
|
77
|
+
async get(path, params) {
|
|
78
|
+
const url = this.buildUrl(path, params);
|
|
79
|
+
return this.request({
|
|
80
|
+
method: 'GET',
|
|
81
|
+
url,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// POST request
|
|
85
|
+
async post(path, data, params) {
|
|
86
|
+
const url = this.buildUrl(path, params);
|
|
87
|
+
return this.request({
|
|
88
|
+
method: 'POST',
|
|
89
|
+
url,
|
|
90
|
+
data,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
// PUT request
|
|
94
|
+
async put(path, data, params) {
|
|
95
|
+
const url = this.buildUrl(path, params);
|
|
96
|
+
return this.request({
|
|
97
|
+
method: 'PUT',
|
|
98
|
+
url,
|
|
99
|
+
data,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
// DELETE request
|
|
103
|
+
async delete(path, params) {
|
|
104
|
+
const url = this.buildUrl(path, params);
|
|
105
|
+
return this.request({
|
|
106
|
+
method: 'DELETE',
|
|
107
|
+
url,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
// Helper to build URL with path parameters
|
|
111
|
+
buildUrl(path, params) {
|
|
112
|
+
let url = path;
|
|
113
|
+
if (params) {
|
|
114
|
+
// Replace path parameters
|
|
115
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
116
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
117
|
+
url = url.replace(`{${key}}`, encodeURIComponent(value.toString()));
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
// Add query parameters
|
|
121
|
+
const queryParams = new URLSearchParams();
|
|
122
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
123
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
124
|
+
if (!path.includes(`{${key}}`)) {
|
|
125
|
+
queryParams.append(key, value.toString());
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
if (queryParams.toString()) {
|
|
130
|
+
url += `?${queryParams.toString()}`;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return url;
|
|
134
|
+
}
|
|
135
|
+
// Health check method
|
|
136
|
+
async healthCheck() {
|
|
137
|
+
try {
|
|
138
|
+
await this.get('/session/status');
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
logger.warn('Health check failed', { error: error.message });
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// Get instance info
|
|
147
|
+
async getInstanceInfo() {
|
|
148
|
+
return this.get('/instance/settings');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Export singleton instance
|
|
152
|
+
export const wsapiClient = new WSAPIClient();
|
|
153
|
+
export default WSAPIClient;
|
|
154
|
+
//# sourceMappingURL=index.js.map
|