send_message
Send WhatsApp messages programmatically by specifying recipient details and message content, enabling automated communication through the WhatsApp MCP Server.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| message | Yes | The text message to send | |
| recipient | Yes | Recipient JID (user or group, e.g., '12345@s.whatsapp.net' or 'group123@g.us') |
Implementation Reference
- src/mcp.ts:398-472 (handler)The main handler function for the 'send_message' MCP tool. It validates the recipient JID, checks WhatsApp socket availability, normalizes the JID, calls the helper sendWhatsAppMessage, and handles success/error responses.async ({ recipient, message }) => { mcpLogger.info(`[MCP Tool] Executing send_message to ${recipient}`); if (!sock) { mcpLogger.error( "[MCP Tool Error] send_message failed: WhatsApp socket is not available.", ); return { isError: true, content: [ { type: "text", text: "Error: WhatsApp connection is not active." }, ], }; } let normalizedRecipient: string; try { normalizedRecipient = jidNormalizedUser(recipient); if (!normalizedRecipient.includes("@")) { throw new Error('JID must contain "@" symbol'); } } catch (normError: any) { mcpLogger.error( `[MCP Tool Error] Invalid recipient JID format: ${recipient}. Error: ${normError.message}`, ); return { isError: true, content: [ { type: "text", text: `Invalid recipient format: "${recipient}". Please provide a valid JID (e.g., number@s.whatsapp.net or group@g.us).`, }, ], }; } try { const result = await sendWhatsAppMessage( waLogger, sock, normalizedRecipient, message, ); if (result && result.key && result.key.id) { return { content: [ { type: "text", text: `Message sent successfully to ${normalizedRecipient} (ID: ${result.key.id}).`, }, ], }; } else { return { isError: true, content: [ { type: "text", text: `Failed to send message to ${normalizedRecipient}. See server logs for details.`, }, ], }; } } catch (error: any) { mcpLogger.error( `[MCP Tool Error] send_message failed for ${recipient}: ${error.message}`, ); return { isError: true, content: [ { type: "text", text: `Error sending message: ${error.message}` }, ], }; } },
- src/mcp.ts:391-397 (schema)Zod schema defining the input parameters for the send_message tool: recipient (string JID) and message (non-empty string).recipient: z .string() .describe( "Recipient JID (user or group, e.g., '12345@s.whatsapp.net' or 'group123@g.us')", ), message: z.string().min(1).describe("The text message to send"), },
- src/mcp.ts:388-473 (registration)Registration of the 'send_message' tool on the MCP server using server.tool(), including name, schema, and handler.server.tool( "send_message", { recipient: z .string() .describe( "Recipient JID (user or group, e.g., '12345@s.whatsapp.net' or 'group123@g.us')", ), message: z.string().min(1).describe("The text message to send"), }, async ({ recipient, message }) => { mcpLogger.info(`[MCP Tool] Executing send_message to ${recipient}`); if (!sock) { mcpLogger.error( "[MCP Tool Error] send_message failed: WhatsApp socket is not available.", ); return { isError: true, content: [ { type: "text", text: "Error: WhatsApp connection is not active." }, ], }; } let normalizedRecipient: string; try { normalizedRecipient = jidNormalizedUser(recipient); if (!normalizedRecipient.includes("@")) { throw new Error('JID must contain "@" symbol'); } } catch (normError: any) { mcpLogger.error( `[MCP Tool Error] Invalid recipient JID format: ${recipient}. Error: ${normError.message}`, ); return { isError: true, content: [ { type: "text", text: `Invalid recipient format: "${recipient}". Please provide a valid JID (e.g., number@s.whatsapp.net or group@g.us).`, }, ], }; } try { const result = await sendWhatsAppMessage( waLogger, sock, normalizedRecipient, message, ); if (result && result.key && result.key.id) { return { content: [ { type: "text", text: `Message sent successfully to ${normalizedRecipient} (ID: ${result.key.id}).`, }, ], }; } else { return { isError: true, content: [ { type: "text", text: `Failed to send message to ${normalizedRecipient}. See server logs for details.`, }, ], }; } } catch (error: any) { mcpLogger.error( `[MCP Tool Error] send_message failed for ${recipient}: ${error.message}`, ); return { isError: true, content: [ { type: "text", text: `Error sending message: ${error.message}` }, ], }; } }, );
- src/whatsapp.ts:230-263 (helper)Helper function that performs the actual WhatsApp message sending using the Baileys socket, including logging and error handling.export async function sendWhatsAppMessage( logger: P.Logger, sock: WhatsAppSocket | null, recipientJid: string, text: string ): Promise<proto.WebMessageInfo | void> { if (!sock || !sock.user) { logger.error( "Cannot send message: WhatsApp socket not connected or initialized." ); return; } if (!recipientJid) { logger.error("Cannot send message: Recipient JID is missing."); return; } if (!text) { logger.error("Cannot send message: Message text is empty."); return; } try { logger.info( `Sending message to ${recipientJid}: ${text.substring(0, 50)}...` ); const normalizedJid = jidNormalizedUser(recipientJid); const result = await sock.sendMessage(normalizedJid, { text: text }); logger.info({ msgId: result?.key.id }, "Message sent successfully"); return result; } catch (error) { logger.error({ err: error, recipientJid }, "Failed to send message"); return; } }