batch_apply_labels
Apply multiple labels to selected emails simultaneously to organize your Gmail inbox efficiently.
Instructions
Apply labels to multiple emails at once
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| labelIds | Yes | Array of label IDs to apply | |
| messageIds | Yes | Array of email message IDs |
Implementation Reference
- src/tools.ts:126-131 (handler)MCP tool handler for 'batch_apply_labels': validates input using Zod schema and delegates to GmailService.batchApplyLabels, formats success/failure response.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:105-107 (handler)Core GmailService method implementing batch label application by applying addLabelIds modification to multiple message IDs via batchOperation.async batchApplyLabels(messageIds: string[], labelIds: string[]): Promise<{ successes: number; failures: number }> { return this.batchOperation(messageIds, (id) => this.modifyMessage(id, { addLabelIds: labelIds })); }
- src/tools.ts:24-27 (schema)Zod input schema for batch_apply_labels tool defining messageIds and labelIds arrays.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:113-123 (helper)Reusable batchOperation helper function that processes items in batches of 50, counts successes and failures using Promise.allSettled; used by batchApplyLabels.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/tools.ts:50-55 (registration)getToolDefinitions function that registers all tools including batch_apply_labels by converting Zod schemas to JSON schemas for MCP ListTools response.export const getToolDefinitions = () => Object.entries(schemas).map(([name, schema]) => ({ name, description: toolDescriptions[name], inputSchema: zodToJsonSchema(schema) }));