Skip to main content
Glama
gfb-47

WhatsApp MCP Server

by gfb-47

send-whatsapp-message

Send WhatsApp messages to contacts programmatically using AppleScript automation on macOS. Specify contact name and message content to deliver messages without direct UI interaction.

Instructions

Send a message to a contact on WhatsApp

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contactNameYesFull name of the contact as it appears in WhatsApp
messageYesMessage content to send

Implementation Reference

  • The async handler function that receives contactName and message, formats the message, constructs AppleScript for UI automation to send the WhatsApp message, executes it via runAppleScript, and returns success or error content.
    async ({ contactName, message }) => {
      try {
        // Format the message for proper line breaks
        const formattedMessage = formatWhatsAppMessage(message);
    
        // Optimized AppleScript with minimal delays
        const appleScript = `
          tell application "WhatsApp" to activate
          delay 1 -- Reduced from 4 to 1
          
          tell application "System Events"
            tell process "WhatsApp"
              -- Access the search field
              try
                -- Keyboard shortcut for search
                keystroke "f" using {command down}
                delay 0.5 -- Reduced from 2 to 0.5
                
                -- Clear any existing text
                keystroke "a" using {command down}
                keystroke (ASCII character 8) -- Backspace
                delay 0.5 -- Reduced from 1.5 to 0.5
                
                -- Type the contact name
                keystroke "${contactName.replace(/"/g, '\\"')}"
                
                -- Give time for search results to populate
                delay 1.5 -- Reduced from 6 to 1.5
                
                -- Contact selection method
                keystroke (ASCII character 31) -- first down arrow
                delay 0.3 -- Reduced from 1 to 0.3
                keystroke (ASCII character 31) -- second down arrow
                delay 0.3 -- Reduced from 1 to 0.3
                keystroke return -- press enter
                delay 0.8 -- Reduced from 3 to 0.8
                
                -- Type and send the message
                keystroke "${formattedMessage}"
                delay 0.5 -- Reduced from 2 to 0.5
                
                -- Send the message
                keystroke return
                delay 0.3 -- Reduced from 1 to 0.3
                
                return "Message sent using optimized timing"
              on error errMsg
                return "Failed to send message: " & errMsg
              end try
            end tell
          end tell
        `;
    
        const result = await runAppleScript(appleScript);
    
        return {
          content: [
            {
              type: "text",
              text: `Message sent to ${contactName}: "${message}"`,
            }
          ]
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Error sending message: ${error}`,
            }
          ],
          isError: true
        };
      }
    }
  • Zod schema for input parameters: contactName (string, full name as in WhatsApp) and message (string).
      contactName: z.string().describe("Full name of the contact as it appears in WhatsApp"),
      message: z.string().describe("Message content to send"),
    },
  • src/index.ts:58-140 (registration)
    Registration of the 'send-whatsapp-message' tool on the MCP server, providing name, description, input schema, and handler function.
    server.tool(
      "send-whatsapp-message",
      "Send a message to a contact on WhatsApp",
      {
        contactName: z.string().describe("Full name of the contact as it appears in WhatsApp"),
        message: z.string().describe("Message content to send"),
      },
      async ({ contactName, message }) => {
        try {
          // Format the message for proper line breaks
          const formattedMessage = formatWhatsAppMessage(message);
    
          // Optimized AppleScript with minimal delays
          const appleScript = `
            tell application "WhatsApp" to activate
            delay 1 -- Reduced from 4 to 1
            
            tell application "System Events"
              tell process "WhatsApp"
                -- Access the search field
                try
                  -- Keyboard shortcut for search
                  keystroke "f" using {command down}
                  delay 0.5 -- Reduced from 2 to 0.5
                  
                  -- Clear any existing text
                  keystroke "a" using {command down}
                  keystroke (ASCII character 8) -- Backspace
                  delay 0.5 -- Reduced from 1.5 to 0.5
                  
                  -- Type the contact name
                  keystroke "${contactName.replace(/"/g, '\\"')}"
                  
                  -- Give time for search results to populate
                  delay 1.5 -- Reduced from 6 to 1.5
                  
                  -- Contact selection method
                  keystroke (ASCII character 31) -- first down arrow
                  delay 0.3 -- Reduced from 1 to 0.3
                  keystroke (ASCII character 31) -- second down arrow
                  delay 0.3 -- Reduced from 1 to 0.3
                  keystroke return -- press enter
                  delay 0.8 -- Reduced from 3 to 0.8
                  
                  -- Type and send the message
                  keystroke "${formattedMessage}"
                  delay 0.5 -- Reduced from 2 to 0.5
                  
                  -- Send the message
                  keystroke return
                  delay 0.3 -- Reduced from 1 to 0.3
                  
                  return "Message sent using optimized timing"
                on error errMsg
                  return "Failed to send message: " & errMsg
                end try
              end tell
            end tell
          `;
    
          const result = await runAppleScript(appleScript);
    
          return {
            content: [
              {
                type: "text",
                text: `Message sent to ${contactName}: "${message}"`,
              }
            ]
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error sending message: ${error}`,
              }
            ],
            isError: true
          };
        }
      }
    );
  • Helper function runAppleScript that promisified exec of osascript to run AppleScript.
    async function runAppleScript(script: string) {
      try {
        const { stdout } = await execPromise(`osascript -e '${script.replace(/'/g, "'\\''")}'`);
        return stdout.trim();
      } catch (error) {
        console.error("AppleScript execution error:", error);
        throw new Error(`Failed to execute AppleScript: ${error}`);
      }
    }
  • Helper function formatWhatsAppMessage that replaces newlines and quotes for AppleScript compatibility.
    function formatWhatsAppMessage(message: string) {
      // Replace normal line breaks with AppleScript line breaks
      // This ensures proper line breaking in WhatsApp
      return message
        .replace(/\n/g, '" & return & "')
        .replace(/"/g, '\\"');
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action but fails to mention critical traits like authentication needs, rate limits, error handling, or whether the message is sent immediately or queued. This leaves significant gaps in understanding the tool's behavior.

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

Conciseness5/5

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

The description is a single, efficient sentence that directly states the tool's function without any unnecessary words. It is front-loaded and appropriately sized for a simple tool, making it easy to grasp quickly.

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

Completeness2/5

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

Given the lack of annotations and output schema, the description is incomplete. It doesn't address behavioral aspects like success/error responses, side effects, or integration with sibling tools. For a messaging tool with potential complexities, more context is needed to ensure proper usage.

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

Parameters3/5

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

The schema description coverage is 100%, with clear descriptions for both parameters ('contactName' and 'message'). The description adds no additional meaning beyond the schema, such as formatting details or examples, but the schema adequately documents the parameters, meeting the baseline.

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

Purpose4/5

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

The description clearly states the action ('Send a message') and target ('to a contact on WhatsApp'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'check-whatsapp-status' or 'list-recent-contacts' beyond the obvious functional difference, missing explicit distinction.

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

Usage Guidelines2/5

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

No guidance is provided on when to use this tool versus alternatives or any prerequisites. The description lacks context about usage scenarios, such as when to choose this over other messaging methods or if there are limitations like contact availability.

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/gfb-47/whatsapp-mcp-server'

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