batch_apply_labels
Apply labels to multiple email messages at once to quickly organize your Gmail inbox by categorizing or filtering groups of emails.
Instructions
Apply labels to multiple emails at once
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| messageIds | Yes | Array of email message IDs | |
| labelIds | Yes | Array of label IDs to apply |
Implementation Reference
- src/tools.ts:24-27 (schema)Zod schema for batch_apply_labels input: requires messageIds (array of strings) and labelIds (array of strings).
batch_apply_labels: z.object({ messageIds: z.array(z.string()).describe("Array of email message IDs"), labelIds: z.array(z.string()).describe("Array of label IDs to apply") }), - src/gmail-service.ts:105-107 (handler)Core handler: batchApplyLabels method on GmailService that calls modifyMessage for each messageId with all labelIds, using batchOperation for concurrency control.
async batchApplyLabels(messageIds: string[], labelIds: string[]): Promise<{ successes: number; failures: number }> { return this.batchOperation(messageIds, (id) => this.modifyMessage(id, { addLabelIds: labelIds })); } - src/tools.ts:126-131 (registration)Registration handler: case 'batch_apply_labels' in handleToolCall switch, validates input via Zod schema and delegates to GmailService.batchApplyLabels.
case "batch_apply_labels": { const v = validated as z.infer<typeof schemas.batch_apply_labels>; const result = await gmailService.batchApplyLabels(v.messageIds, v.labelIds); return { content: [{ type: "text", text: `Batch label application completed:\nSuccessfully processed: ${result.successes} emails\nFailed: ${result.failures} emails` }] }; } - src/gmail-service.ts:113-123 (helper)Helper: batchOperation method processes items in batches of 50 with Promise.allSettled, returning success/failure counts.
private async batchOperation<T>(items: T[], operation: (item: T) => Promise<any>): Promise<{ successes: number; failures: number }> { let successes = 0, failures = 0; const batchSize = 50; for (let i = 0; i < items.length; i += batchSize) { const results = await Promise.allSettled(items.slice(i, i + batchSize).map(operation)); results.forEach(r => r.status === 'fulfilled' ? successes++ : failures++); } return { successes, failures }; } - src/gmail-service.ts:109-111 (helper)Helper: modifyMessage calls the Gmail API users.messages.modify endpoint.
private async modifyMessage(id: string, requestBody: any): Promise<void> { await this.gmail.users.messages.modify({ userId: 'me', id, requestBody }); }