watch_mailbox
Monitor your Gmail mailbox for new emails and changes by setting up notifications through Cloud Pub/Sub. Configure label filters to track specific email categories and receive real-time updates.
Instructions
Watch for changes to the user's mailbox
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| topicName | Yes | The name of the Cloud Pub/Sub topic to publish notifications to | |
| labelIds | No | Label IDs to restrict notifications to | |
| labelFilterAction | No | Whether to include or exclude the specified labels |
Implementation Reference
- src/index.ts:1281-1294 (registration)Registration of the 'watch_mailbox' tool. Defines input schema (topicName required, optional labelIds and labelFilterAction) and handler that uses the Gmail API's users.watch method to watch the mailbox for changes and publish notifications to a Pub/Sub topic.server.tool("watch_mailbox", "Watch for changes to the user's mailbox", { topicName: z.string().describe("The name of the Cloud Pub/Sub topic to publish notifications to"), labelIds: z.array(z.string()).optional().describe("Label IDs to restrict notifications to"), labelFilterAction: z.enum(['include', 'exclude']).optional().describe("Whether to include or exclude the specified labels") }, async (params) => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { const { data } = await gmail.users.watch({ userId: 'me', requestBody: params }) return formatResponse(data) }) } )
- src/index.ts:1288-1292 (handler)The inline handler function for watch_mailbox tool. It wraps the Gmail API call in handleTool for authentication and executes gmail.users.watch with the provided parameters to set up mailbox watching.async (params) => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { const { data } = await gmail.users.watch({ userId: 'me', requestBody: params }) return formatResponse(data) })
- src/index.ts:1283-1286 (schema)Input schema for watch_mailbox tool using Zod: requires topicName (Pub/Sub topic), optional labelIds array and labelFilterAction ('include' or 'exclude').{ topicName: z.string().describe("The name of the Cloud Pub/Sub topic to publish notifications to"), labelIds: z.array(z.string()).optional().describe("Label IDs to restrict notifications to"), labelFilterAction: z.enum(['include', 'exclude']).optional().describe("Whether to include or exclude the specified labels")
- src/index.ts:49-65 (helper)Shared helper function handleTool used by watch_mailbox (and all tools) to handle OAuth2 authentication, create Gmail client, execute the API call, and handle errors.const handleTool = async (queryConfig: Record<string, any> | undefined, apiCall: (gmail: gmail_v1.Gmail) => Promise<any>) => { try { const oauth2Client = queryConfig ? createOAuth2Client(queryConfig) : defaultOAuth2Client if (!oauth2Client) throw new Error('OAuth2 client could not be created, please check your credentials') const credentialsAreValid = await validateCredentials(oauth2Client) if (!credentialsAreValid) throw new Error('OAuth2 credentials are invalid, please re-authenticate') const gmailClient = queryConfig ? google.gmail({ version: 'v1', auth: oauth2Client }) : defaultGmailClient if (!gmailClient) throw new Error('Gmail client could not be created, please check your credentials') const result = await apiCall(gmailClient) return result } catch (error: any) { return `Tool execution failed: ${error.message}` } }
- src/index.ts:47-47 (helper)Shared helper to format API responses as MCP content (JSON stringified). Used by watch_mailbox handler.const formatResponse = (response: any) => ({ content: [{ type: "text", text: JSON.stringify(response) }] })