get_direct_chat_by_contact_number
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
| Name | Required | Description | Default |
|---|---|---|---|
| phone_number | Yes | The phone number of the contact (e.g., 1234567890) |
Implementation Reference
- src/tools/chats.ts:170-216 (handler)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, }; } }, ); - src/tools/chats.ts:173-177 (schema)Zod schema defining the input parameter: phone_number (string).
{ phone_number: z .string() .describe("The phone number of the contact (e.g., 1234567890)"), }, - src/tools/chats.ts:170-216 (registration)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; - src/storage/message-store.ts:253-257 (helper)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;