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
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search 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}`, }, ], }; } }, );
- src/mcp.ts:78-110 (handler)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}`, }, ], }; } },
- src/mcp.ts:72-76 (schema)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"),
- src/database.ts:353-384 (helper)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 []; } }
- src/database.ts:66-72 (schema)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 ); `);