Skip to main content
Glama

get_direct_chat_by_contact_number

Read-onlyIdempotent

Retrieve a direct WhatsApp chat JID using a contact's phone number. Use this to start a chat when only the number is known, though reliability may vary.

Instructions

Get direct WhatsApp chat JID by contact phone number (less reliable, use get_chat_by_id if JID is known).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
phone_numberYesThe phone number of the contact (e.g., 1234567890)

Implementation Reference

  • The handler function that executes the 'get_direct_chat_by_contact_number' tool logic. It constructs a JID from the phone number, calls getChatById, and validates it's a direct (non-group) chat.
    server.tool(
      "get_direct_chat_by_contact_number",
      "Get direct WhatsApp chat JID by contact phone number (less reliable, use get_chat_by_id if JID is known).",
      {
        phone_number: z
          .string()
          .describe("The phone number of the contact (e.g., 1234567890)"),
      },
      async ({ phone_number }): Promise<CallToolResult> => {
        try {
          // Construct potential JID
          const jid = `${phone_number}@s.whatsapp.net`;
          const chat = await whatsappService.getChatById(jid);
          if (!chat || chat.isGroup) {
            // Ensure it's a direct chat
            return {
              content: [
                {
                  type: "text",
                  text: `Direct chat not found for number: ${phone_number}`,
                },
              ],
              isError: true,
            };
          }
          return {
            // Return only the JID or the full chat object? Let's return the object.
            content: [{ type: "text", text: JSON.stringify(chat, null, 2) }],
          };
        } catch (error: any) {
          log.error(
            `Error in get_direct_chat_by_contact_number tool for number ${phone_number}:`,
            error,
          );
          // Don't expose detailed errors, just indicate not found
          return {
            content: [
              {
                type: "text",
                text: `Could not find direct chat for number: ${phone_number}`,
              },
            ],
            isError: true,
          };
        }
      },
    );
  • Zod schema defining the input parameter: phone_number (string).
    {
      phone_number: z
        .string()
        .describe("The phone number of the contact (e.g., 1234567890)"),
    },
  • Tool registration via server.tool() call in the registerChatTools function.
    server.tool(
      "get_direct_chat_by_contact_number",
      "Get direct WhatsApp chat JID by contact phone number (less reliable, use get_chat_by_id if JID is known).",
      {
        phone_number: z
          .string()
          .describe("The phone number of the contact (e.g., 1234567890)"),
      },
      async ({ phone_number }): Promise<CallToolResult> => {
        try {
          // Construct potential JID
          const jid = `${phone_number}@s.whatsapp.net`;
          const chat = await whatsappService.getChatById(jid);
          if (!chat || chat.isGroup) {
            // Ensure it's a direct chat
            return {
              content: [
                {
                  type: "text",
                  text: `Direct chat not found for number: ${phone_number}`,
                },
              ],
              isError: true,
            };
          }
          return {
            // Return only the JID or the full chat object? Let's return the object.
            content: [{ type: "text", text: JSON.stringify(chat, null, 2) }],
          };
        } catch (error: any) {
          log.error(
            `Error in get_direct_chat_by_contact_number tool for number ${phone_number}:`,
            error,
          );
          // Don't expose detailed errors, just indicate not found
          return {
            content: [
              {
                type: "text",
                text: `Could not find direct chat for number: ${phone_number}`,
              },
            ],
            isError: true,
          };
        }
      },
    );
  • src/server.ts:140-144 (registration)
    Metadata registration for the tool in TOOL_EXECUTION_METADATA, marking it as readOnly, idempotent, and not open-world.
    get_direct_chat_by_contact_number: {
      readOnlyHint: true,
      idempotentHint: true,
      openWorldHint: false,
    },
  • StoreService.getChatById delegates to messageStore.getChatById.
    getChatById(jid: string): StoredChat | null {
      return this.messageStore ? this.messageStore.getChatById(jid) : null;
  • MessageStore.getChatById queries the SQLite database for a chat by JID.
    getChatById(jid: string): StoredChat | null {
      const stmt = this.db.prepare(
        `SELECT id, name, is_group, unread_count, timestamp FROM chats WHERE id = ?`,
      );
      return stmt.get(jid) || null;
Behavior4/5

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

Annotations already declare readOnlyHint and idempotentHint. Description adds the behavioral trait 'less reliable' beyond annotations, which is useful context for the agent.

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?

Two sentences with no wasted words. Front-loaded with main purpose and additional usage guidance fits efficiently.

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

Completeness5/5

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

Given single parameter, no output schema, and low complexity, the description covers purpose, usage context, and reliability. No missing information.

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?

Schema coverage is 100% with a clear description of the phone_number parameter. Description does not add any additional meaning beyond what the schema provides, so baseline score of 3.

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

Purpose5/5

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

Clearly states verb 'Get' and resource 'direct WhatsApp chat JID by contact phone number'. Distinguishes from sibling tool get_chat_by_id by noting it's less reliable and to use that if JID is known.

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

Usage Guidelines5/5

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

Explicitly tells when to use (when only phone number is available) and when not to (if JID known, use get_chat_by_id). Provides clear alternative.

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/loglux/whatsapp-mcp-stream'

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