Skip to main content
Glama
jlucaso1
by jlucaso1

search_contacts

Find WhatsApp contacts by name or phone number to enable messaging and chat interactions through the WhatsApp MCP Server.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSearch term for contact name or phone number part of JID

Implementation Reference

  • src/mcp.ts:71-111 (registration)
    Registration of the 'search_contacts' MCP tool, including inline input schema (zod) and the handler function that executes the search logic by calling searchDbForContacts from database.ts and returns formatted JSON results.
    "search_contacts", { query: z .string() .min(1) .describe("Search term for contact name or phone number part of JID"), }, async ({ query }) => { mcpLogger.info( `[MCP Tool] Executing search_contacts with query: "${query}"`, ); try { const contacts = searchDbForContacts(query, 20); const formattedContacts = contacts.map((c) => ({ jid: c.jid, name: c.name ?? c.jid.split("@")[0], })); return { content: [ { type: "text", text: JSON.stringify(formattedContacts, null, 2), }, ], }; } catch (error: any) { mcpLogger.error( `[MCP Tool Error] search_contacts failed: ${error.message}`, ); return { isError: true, content: [ { type: "text", text: `Error searching contacts: ${error.message}`, }, ], }; } }, );
  • The executor/handler function for the search_contacts tool. Logs execution, calls searchDbForContacts(query, 20), formats contacts with jid and name, returns as text/JSON or error.
    async ({ query }) => { mcpLogger.info( `[MCP Tool] Executing search_contacts with query: "${query}"`, ); try { const contacts = searchDbForContacts(query, 20); const formattedContacts = contacts.map((c) => ({ jid: c.jid, name: c.name ?? c.jid.split("@")[0], })); return { content: [ { type: "text", text: JSON.stringify(formattedContacts, null, 2), }, ], }; } catch (error: any) { mcpLogger.error( `[MCP Tool Error] search_contacts failed: ${error.message}`, ); return { isError: true, content: [ { type: "text", text: `Error searching contacts: ${error.message}`, }, ], }; } },
  • Input schema for search_contacts tool using Zod: requires 'query' string min length 1.
    { query: z .string() .min(1) .describe("Search term for contact name or phone number part of JID"),
  • Core helper function implementing contact search: queries SQLite 'contacts' table for matches in name/notify/phone_number/jid using LIKE, limits results, formats as {jid, name}.
    export function searchDbForContacts( query: string, limit: number = 20 ): { jid: string; name: string | null }[] { const db = getDb(); try { const pattern = `%${query}%`; const stmt = db.prepare(` SELECT jid, COALESCE(name, notify, phone_number, jid) AS display_name FROM contacts WHERE LOWER(COALESCE(name, notify, phone_number, jid)) LIKE LOWER(?) LIMIT ? `); const rows = stmt.all(pattern, limit) as { jid: string; display_name: string | null; }[]; return rows.map((r) => ({ jid: r.jid, name: r.display_name, })); } catch (error) { console.error("Error searching contacts:", error); return []; } }
  • Database schema definition for the 'contacts' table used by search_contacts.
    CREATE TABLE IF NOT EXISTS contacts ( jid TEXT PRIMARY KEY, name TEXT, notify TEXT, phone_number TEXT ); `);

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