Skip to main content
Glama
jlucaso1

WhatsApp MCP Server

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
      );
    `);
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