get_chat
Retrieve detailed chat history from WhatsApp, including the last message, by specifying the chat JID. Integrates with MCP servers for AI-driven message management.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chat_jid | Yes | The JID of the chat to retrieve | |
| include_last_message | No | Include last message details (default true) |
Implementation Reference
- src/mcp.ts:270-318 (handler)Full implementation of the 'get_chat' MCP tool: registration via server.tool, input schema using Zod, and the async handler function that calls getChat from database.ts, formats the result, and returns JSON response or error.server.tool( "get_chat", { chat_jid: z.string().describe("The JID of the chat to retrieve"), include_last_message: z .boolean() .optional() .default(true) .describe("Include last message details (default true)"), }, async ({ chat_jid, include_last_message }) => { mcpLogger.info( `[MCP Tool] Executing get_chat for ${chat_jid}, lastMsg=${include_last_message}`, ); try { const chat = getChat(chat_jid, include_last_message); if (!chat) { return { isError: true, content: [ { type: "text", text: `Chat with JID ${chat_jid} not found.` }, ], }; } const formattedChat = formatDbChatForJson(chat); return { content: [ { type: "text", text: JSON.stringify(formattedChat, null, 2), }, ], }; } catch (error: any) { mcpLogger.error( `[MCP Tool Error] get_chat failed for ${chat_jid}: ${error.message}`, ); return { isError: true, content: [ { type: "text", text: `Error retrieving chat ${chat_jid}: ${error.message}`, }, ], }; } }, );
- src/mcp.ts:272-279 (schema)Input schema for get_chat tool using Zod: requires chat_jid (string), optional include_last_message (boolean, default true).{ chat_jid: z.string().describe("The JID of the chat to retrieve"), include_last_message: z .boolean() .optional() .default(true) .describe("Include last message details (default true)"), },
- src/mcp.ts:37-52 (helper)Helper function to format DbChat object to JSON-friendly structure for get_chat response.function formatDbChatForJson(chat: DbChat) { return { jid: chat.jid, name: chat.name ?? chat.jid.split("@")[0] ?? "Unknown Chat", is_group: chat.jid.endsWith("@g.us"), last_message_time: chat.last_message_time?.toISOString() ?? null, last_message_preview: chat.last_message ?? null, last_sender_jid: chat.last_sender ?? null, last_sender_display: chat.last_sender ? chat.last_sender.split("@")[0] : chat.last_is_from_me ? "Me" : null, last_is_from_me: chat.last_is_from_me ?? null, }; }
- src/database.ts:250-281 (helper)Core helper function getChat that queries the SQLite database for a specific chat by JID, optionally including last message details via subqueries, and returns Chat object or null.export function getChat( jid: string, includeLastMessage: boolean = true, ): Chat | null { const db = getDb(); try { let sql = ` SELECT c.jid, c.name, c.last_message_time ${ includeLastMessage ? `, (SELECT m.content FROM messages m WHERE m.chat_jid = c.jid ORDER BY m.timestamp DESC LIMIT 1) as last_message, (SELECT m.sender FROM messages m WHERE m.chat_jid = c.jid ORDER BY m.timestamp DESC LIMIT 1) as last_sender, (SELECT m.is_from_me FROM messages m WHERE m.chat_jid = c.jid ORDER BY m.timestamp DESC LIMIT 1) as last_is_from_me ` : "" } FROM chats c WHERE c.jid = ? -- Positional parameter 1 `; const stmt = db.prepare(sql); const row = stmt.get(jid) as any | undefined; return row ? rowToChat(row) : null; } catch (error) { console.error("Error getting chat:", error); return null; } }