auto_organize
Automatically organize emails by moving them to specific mailboxes based on sender or subject keyword rules you define.
Instructions
Automatically organize emails based on rules (sender, subject keywords, etc.)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dryRun | No | If true, only shows what would be organized without moving emails | |
| rules | Yes | Array of organization rules | |
| sourceMailbox | No | Source mailbox to organize (default: INBOX) | INBOX |
Implementation Reference
- src/lib/icloud-mail-client.ts:938-1062 (handler)Core handler function that implements the auto_organize tool logic: fetches messages from source mailbox, matches them against provided rules based on sender and subject, optionally moves them to destination mailboxes, supports dry-run mode, and returns detailed results.async autoOrganize( rules: OrganizationRule[], sourceMailbox: string = 'INBOX', dryRun: boolean = false ): Promise<{ status: string; message: string; results: Array<{ rule: string; matchedMessages: number; moved: boolean; messages?: Array<{ id: string; from: string; subject: string; destinationMailbox: string; }>; }>; }> { try { const messages = await this.getMessages(sourceMailbox, 100); const results: Array<{ rule: string; matchedMessages: number; moved: boolean; messages?: Array<{ id: string; from: string; subject: string; destinationMailbox: string; }>; }> = []; for (const rule of rules) { const matchedMessages: Array<{ id: string; from: string; subject: string; destinationMailbox: string; }> = []; for (const message of messages) { let matches = false; if (rule.condition.fromContains) { matches = matches || message.from .toLowerCase() .includes(rule.condition.fromContains.toLowerCase()); } if (rule.condition.subjectContains) { matches = matches || message.subject .toLowerCase() .includes(rule.condition.subjectContains.toLowerCase()); } if (matches) { matchedMessages.push({ id: message.id, from: message.from, subject: message.subject, destinationMailbox: rule.action.moveToMailbox, }); } } if (matchedMessages.length > 0) { let moved = false; if (!dryRun) { try { const messageIds = matchedMessages.map((m) => m.id); await this.moveMessages( messageIds, sourceMailbox, rule.action.moveToMailbox ); moved = true; } catch (moveError) { console.error( `Failed to move messages for rule '${rule.name}':`, moveError ); } } results.push({ rule: rule.name, matchedMessages: matchedMessages.length, moved: !dryRun && moved, messages: matchedMessages, }); } else { results.push({ rule: rule.name, matchedMessages: 0, moved: false, }); } } const totalMatched = results.reduce( (sum, result) => sum + result.matchedMessages, 0 ); return { status: 'success', message: dryRun ? `Dry run completed. Found ${totalMatched} messages matching organization rules` : `Organization completed. Processed ${totalMatched} messages`, results, }; } catch (error) { return { status: 'error', message: `Failed to organize emails: ${error instanceof Error ? error.message : String(error)}`, results: [], }; } }
- src/index.ts:702-737 (handler)MCP server request handler for the 'auto_organize' tool, validates configuration, extracts parameters, delegates to mailClient.autoOrganize, and formats the response.case 'auto_organize': { if (!mailClient) { throw new McpError( ErrorCode.InvalidRequest, 'iCloud Mail not configured. Please set ICLOUD_EMAIL and ICLOUD_APP_PASSWORD environment variables.' ); } const rules = args?.rules as Array<{ name: string; condition: { fromContains?: string; subjectContains?: string; }; action: { moveToMailbox: string; }; }>; const sourceMailbox = (args?.sourceMailbox as string) || 'INBOX'; const dryRun = (args?.dryRun as boolean) || false; const result = await mailClient.autoOrganize( rules, sourceMailbox, dryRun ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/index.ts:314-373 (registration)Registration of the 'auto_organize' tool in the MCP tools array, including detailed input schema and description.{ name: 'auto_organize', description: 'Automatically organize emails based on rules (sender, subject keywords, etc.)', inputSchema: { type: 'object', properties: { rules: { type: 'array', items: { type: 'object', properties: { name: { type: 'string', description: 'Rule name', }, condition: { type: 'object', properties: { fromContains: { type: 'string', description: 'Move emails if sender contains this text', }, subjectContains: { type: 'string', description: 'Move emails if subject contains this text', }, }, }, action: { type: 'object', properties: { moveToMailbox: { type: 'string', description: 'Mailbox to move matching emails to', }, }, required: ['moveToMailbox'], }, }, required: ['name', 'condition', 'action'], }, description: 'Array of organization rules', }, sourceMailbox: { type: 'string', description: 'Source mailbox to organize (default: INBOX)', default: 'INBOX', }, dryRun: { type: 'boolean', description: 'If true, only shows what would be organized without moving emails', default: false, }, }, required: ['rules'], }, },
- src/types/config.ts:51-60 (schema)TypeScript interface defining the OrganizationRule structure used for input validation and typing in the auto_organize tool.export interface OrganizationRule { name: string; condition: { fromContains?: string; subjectContains?: string; }; action: { moveToMailbox: string; }; }