batch_apply_labels
Apply labels to multiple emails simultaneously in Gmail to organize your inbox efficiently.
Instructions
Apply labels to multiple emails at once
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| messageIds | Yes | Array of email message IDs | |
| labelIds | Yes | Array of label IDs to apply |
Implementation Reference
- src/gmail-service.ts:105-107 (handler)Core handler function that batches label applications to multiple emails by calling batchOperation with modifyMessage.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 validation for the batch_apply_labels tool parameters.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/tools.ts:50-55 (registration)Generates MCP tool definitions including schema and description for batch_apply_labels.export const getToolDefinitions = () => Object.entries(schemas).map(([name, schema]) => ({ name, description: toolDescriptions[name], inputSchema: zodToJsonSchema(schema) }));
- src/tools.ts:126-131 (handler)Dispatcher in handleToolCall that validates input and invokes the GmailService batchApplyLabels handler.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 function used by batchApplyLabels to process operations in batches with success/failure counting.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 }; }