search-emails
Search and retrieve emails by applying Gmail search syntax queries, such as 'from:', 'subject:', or 'is:unread', and specify the maximum number of results to return.
Instructions
Search emails using Gmail search syntax
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| maxResults | No | Maximum number of results to return | |
| query | Yes | Gmail search query (e.g., 'from:example@gmail.com', 'subject:meeting', 'is:unread') |
Implementation Reference
- src/tools.ts:492-531 (handler)The handler function for the 'search-emails' tool. It executes a Gmail search using toolset.executeAction('GMAIL_SEARCH_EMAILS'), processes the response to format email snippets and IDs, handles errors, and returns structured content.}, async (args, extra) => { try { const userAddress = "default-user"; const result = await toolset.executeAction({ action: "GMAIL_SEARCH_EMAILS", entityId: userAddress, params: args }); if (result.successful) { const emails = result.data?.response_data as any; const emailList = emails.messages?.map((email: any) => `• ${email.snippet} (${email.id})` ).join('\n') || 'No emails found matching your search'; return { content: [{ type: "text", text: `🔍 Search results for "${args.query}":\n\n${emailList}\n\nTotal: ${emails.messages?.length || 0} emails found` }], }; } else { return { content: [{ type: "text", text: `❌ Failed to search emails: ${result.error || 'Unknown error'}` }], }; } } catch (error) { console.error('Error searching emails:', error); return { content: [{ type: "text", text: `Error searching emails: ${error instanceof Error ? error.message : String(error)}` }], }; } });
- src/tools.ts:490-491 (schema)Input schema using Zod for validating the 'query' parameter (required string) and optional 'maxResults' (number).query: z.string().describe("Gmail search query (e.g., 'from:example@gmail.com', 'subject:meeting', 'is:unread')"), maxResults: z.number().optional().describe("Maximum number of results to return"),
- src/tools.ts:489-489 (registration)Registration of the 'search-emails' tool on the MCP server, specifying name, description, input schema, and handler function.server.tool("search-emails", "Search emails using Gmail search syntax", {