get-sandbox-messages
Retrieve test email messages from the Mailtrap sandbox inbox for debugging and verification purposes.
Instructions
Get list of messages from the sandbox test inbox
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| page | No | Page number for pagination | |
| last_id | No | Pagination using last message ID. Returns messages after the specified message ID. | |
| search | No | Search query to filter messages |
Implementation Reference
- The handler function that retrieves and formats messages from the Mailtrap sandbox test inbox, supporting pagination with page/last_id and search.async function getMessages({ page, last_id, search, }: GetMessagesRequest): Promise<{ content: { type: string; text: string }[]; isError?: boolean; }> { try { const { MAILTRAP_TEST_INBOX_ID } = process.env; if (!MAILTRAP_TEST_INBOX_ID) { throw new Error( "MAILTRAP_TEST_INBOX_ID environment variable is required for sandbox mode" ); } // Check if sandbox client is available if (!sandboxClient) { throw new Error( "Sandbox client is not available. Please set MAILTRAP_TEST_INBOX_ID environment variable." ); } const inboxId = Number(MAILTRAP_TEST_INBOX_ID); if (Number.isNaN(inboxId)) { throw new Error("MAILTRAP_TEST_INBOX_ID must be a valid number"); } // Get messages from the inbox // MessageListOptions supports: page, last_id, and search const options: { page?: number; last_id?: number; search?: string; } = {}; if (page !== undefined) { options.page = page; } if (last_id !== undefined) { options.last_id = last_id; } if (search !== undefined) { options.search = search; } const messages = await sandboxClient.testing.messages.get( inboxId, Object.keys(options).length > 0 ? options : undefined ); if (!messages || messages.length === 0) { return { content: [ { type: "text", text: "No messages found in the sandbox inbox.", }, ], }; } const messageList = messages .map( (message: (typeof messages)[0]) => `• Message ID: ${message.id}\n From: ${message.from_email}\n To: ${ message.to_email }\n Subject: ${message.subject}\n Sent: ${ message.sent_at }\n Read: ${message.is_read ? "Yes" : "No"}\n` ) .join("\n"); return { content: [ { type: "text", text: `Found ${messages.length} message(s) in sandbox inbox:\n\n${messageList}`, }, ], }; } catch (error) { console.error("Error getting messages:", error); const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Failed to get messages: ${errorMessage}`, }, ], isError: true, }; } }
- Input schema defining optional parameters: page (number >=1), last_id (number >=1), search (string).const getMessagesSchema = { type: "object", properties: { page: { type: "number", description: "Page number for pagination", minimum: 1, }, last_id: { type: "number", description: "Pagination using last message ID. Returns messages after the specified message ID.", minimum: 1, }, search: { type: "string", description: "Search query to filter messages", }, }, required: [], additionalProperties: false, };
- src/server.ts:92-97 (registration)Tool registration in the tools array, linking name, description, schema, and handler.{ name: "get-sandbox-messages", description: "Get list of messages from the sandbox test inbox", inputSchema: getMessagesSchema, handler: getMessages, },