Skip to main content
Glama
gfb-47

WhatsApp MCP Server

by gfb-47

send-whatsapp-message

Send a WhatsApp message to a contact programmatically using the WhatsApp MCP Server. Define the contact name and message content to automate message delivery 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 main handler function that formats the message, executes AppleScript to automate WhatsApp interaction for searching contact, typing message, and sending it.
    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 input schema defining parameters: contactName (string, full name 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, including name, description, schema, and handler reference.
    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 to format message text for AppleScript by replacing newlines with AppleScript return statements and escaping double quotes.
    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, '\\"'); }
  • Helper function to execute AppleScript code using Node.js child_process.exec, escaping single quotes properly.
    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}`); } }

Other Tools

Related 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