Skip to main content
Glama
jlucaso1
by jlucaso1

get_message_context

Retrieve surrounding messages for a target message ID on WhatsApp, specifying the number of messages before and after for context. Integrates with AI agents to enhance chat history analysis and interaction.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
afterNoNumber of messages after (default 5)
beforeNoNumber of messages before (default 5)
message_idYesThe ID of the target message to get context around

Implementation Reference

  • The main handler function for the 'get_message_context' tool. It invokes getMessagesAround from the database module, formats the messages, and returns the context as JSON.
    async ({ message_id, before, after }) => { mcpLogger.info( `[MCP Tool] Executing get_message_context for msg ${message_id}, before=${before}, after=${after}`, ); try { const context = getMessagesAround(message_id, before, after); if (!context.target) { return { isError: true, content: [ { type: "text", text: `Message with ID ${message_id} not found.`, }, ], }; } const formattedContext = { target: formatDbMessageForJson(context.target), before: context.before.map(formatDbMessageForJson), after: context.after.map(formatDbMessageForJson), }; return { content: [ { type: "text", text: JSON.stringify(formattedContext, null, 2), }, ], }; } catch (error: any) { mcpLogger.error( `[MCP Tool Error] get_message_context failed for ${message_id}: ${error.message}`, ); return { isError: true, content: [ { type: "text", text: `Error retrieving context for message ${message_id}: ${error.message}`, }, ], }; } },
  • Zod schema defining the input parameters for the get_message_context tool: message_id (required string), before and after (optional numbers, default 5).
    { message_id: z .string() .describe("The ID of the target message to get context around"), before: z .number() .int() .nonnegative() .optional() .default(5) .describe("Number of messages before (default 5)"), after: z .number() .int() .nonnegative() .optional() .default(5) .describe("Number of messages after (default 5)"), },
  • Helper function that performs the database queries to retrieve the target message and the specified number of messages before and after it based on timestamps in the same chat.
    export function getMessagesAround( messageId: string, before: number = 5, after: number = 5, ): { before: Message[]; target: Message | null; after: Message[] } { const db = getDb(); const result: { before: Message[]; target: Message | null; after: Message[]; } = { before: [], target: null, after: [] }; try { const targetStmt = db.prepare(` SELECT m.*, c.name as chat_name FROM messages m JOIN chats c ON m.chat_jid = c.jid WHERE m.id = ? -- Positional parameter 1 `); const targetRow = targetStmt.get(messageId) as any | undefined; if (!targetRow) { return result; } result.target = rowToMessage(targetRow); const targetTimestamp = result.target.timestamp.toISOString(); const chatJid = result.target.chat_jid; const beforeStmt = db.prepare(` SELECT m.*, c.name as chat_name FROM messages m JOIN chats c ON m.chat_jid = c.jid WHERE m.chat_jid = ? AND m.timestamp < ? -- Positional params 1, 2 ORDER BY m.timestamp DESC LIMIT ? -- Positional param 3 `); const beforeRows = beforeStmt.all( chatJid, targetTimestamp, before, ) as any[]; result.before = beforeRows.map(rowToMessage).reverse(); const afterStmt = db.prepare(` SELECT m.*, c.name as chat_name FROM messages m JOIN chats c ON m.chat_jid = c.jid WHERE m.chat_jid = ? AND m.timestamp > ? -- Positional params 1, 2 ORDER BY m.timestamp ASC LIMIT ? -- Positional param 3 `); const afterRows = afterStmt.all(chatJid, targetTimestamp, after) as any[]; result.after = afterRows.map(rowToMessage); return result; } catch (error) { console.error("Error getting messages around:", error); return result; } }
  • Utility function to format a database message object into a JSON-friendly structure, used in the tool handler to prepare the response.
    function formatDbMessageForJson(msg: DbMessage) { return { id: msg.id, chat_jid: msg.chat_jid, chat_name: msg.chat_name ?? "Unknown Chat", sender_jid: msg.sender ?? null, sender_display: msg.sender ? msg.sender.split("@")[0] : msg.is_from_me ? "Me" : "Unknown", content: msg.content, timestamp: msg.timestamp.toISOString(), is_from_me: msg.is_from_me, }; }
  • src/mcp.ts:320-386 (registration)
    The server.tool call that registers the get_message_context tool with the MCP server, including schema and handler.
    server.tool( "get_message_context", { message_id: z .string() .describe("The ID of the target message to get context around"), before: z .number() .int() .nonnegative() .optional() .default(5) .describe("Number of messages before (default 5)"), after: z .number() .int() .nonnegative() .optional() .default(5) .describe("Number of messages after (default 5)"), }, async ({ message_id, before, after }) => { mcpLogger.info( `[MCP Tool] Executing get_message_context for msg ${message_id}, before=${before}, after=${after}`, ); try { const context = getMessagesAround(message_id, before, after); if (!context.target) { return { isError: true, content: [ { type: "text", text: `Message with ID ${message_id} not found.`, }, ], }; } const formattedContext = { target: formatDbMessageForJson(context.target), before: context.before.map(formatDbMessageForJson), after: context.after.map(formatDbMessageForJson), }; return { content: [ { type: "text", text: JSON.stringify(formattedContext, null, 2), }, ], }; } catch (error: any) { mcpLogger.error( `[MCP Tool Error] get_message_context failed for ${message_id}: ${error.message}`, ); return { isError: true, content: [ { type: "text", text: `Error retrieving context for message ${message_id}: ${error.message}`, }, ], }; } }, );

Other Tools

Related 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