Skip to main content
Glama
jlucaso1

WhatsApp MCP Server

by jlucaso1

send_message

Send text messages through WhatsApp to individual contacts or group chats using recipient JIDs and message content.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
recipientYesRecipient JID (user or group, e.g., '12345@s.whatsapp.net' or 'group123@g.us')
messageYesThe text message to send

Implementation Reference

  • The core handler function for the 'send_message' tool. It logs execution, checks socket availability, normalizes recipient JID, handles errors, calls the sendWhatsAppMessage helper, and returns 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}` },
          ],
        };
      }
    },
  • Zod schema defining input parameters for send_message: 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)
    MCP server.tool registration of the 'send_message' tool, specifying name, input schema, and handler function.
    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}` },
            ],
          };
        }
      },
    );
  • Supporting helper function sendWhatsAppMessage that performs the actual message sending via Baileys library sock.sendMessage, with 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;
      }
    }
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jlucaso1/whatsapp-mcp-ts'

If you have feedback or need assistance with the MCP directory API, please join our Discord server