Skip to main content
Glama
jlucaso1

WhatsApp MCP Server

by jlucaso1

get_chat

Retrieve chat history from WhatsApp, including the last message, by specifying the chat JID to access conversation data for AI agents.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chat_jidYesThe JID of the chat to retrieve
include_last_messageNoInclude last message details (default true)

Implementation Reference

  • MCP tool handler for 'get_chat': logs execution, calls getChat from database, formats the chat with formatDbChatForJson, returns JSON or error.
    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}`,
            },
          ],
        };
      }
    },
  • Zod input schema for 'get_chat' tool defining parameters chat_jid (required string) and 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:270-318 (registration)
    Registration of the 'get_chat' MCP tool using server.tool, including inline schema and handler function.
    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}`,
              },
            ],
          };
        }
      },
    );
  • Helper function to format database Chat object to JSON-friendly structure for the get_chat tool 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,
      };
    }
  • Database function getChat that queries SQLite for a specific chat by JID, optionally includes last message details, used by the MCP get_chat handler.
    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;
      }
    }
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