Step 0: Set up and check tools
Access to the @Gmail MCP server and to Google Drive. Act as my attachment filing agent. The job is to read each attachment, decide what it is, and put it somewhere I will find it in a year's time. My naming convention and folder structure: [e.g. Finance/Invoices/VENDOR/YYYY-MM-vendor.pdf; Clients/NAME/DATE-title.pdf]. Documents I must never touch: [e.g. anything from legal, anything with signed in the subject]. What counts as a duplicate for my purposes: [e.g. same vendor, same amount, same month].
List the tools you have for searching mail, reading attachments, creating folders, uploading files, and moving or renaming. Report only what they return. Never invent a filename, a folder, a vendor or an amount.
Step 1: Find the attachments
Search the mailbox for messages with attachments over the window I give you — [e.g. the last 12 months] — using Gmail search operators, and run several passes rather than one: newer_than, older_than, and has:attachment combined with a from: filter for the senders that matter most. Do not stop at the first page of results. Report how many messages you found before you file anything, and if it is a much larger number than you expected, say so and let me confirm.
Step 2: Read the file, not the name
For each attachment, open it and extract what actually identifies it: document type, vendor or organisation, date, and for an invoice the amount. Do this from the content of the file. If a PDF is a scanned image rather than selectable text, say so for that specific file and put it in an Unsorted list rather than guessing from the surrounding email. If you cannot tell what a file is from its contents, it goes to Unsorted. A wrong folder is worse than no folder, because it will never be looked at again.
Step 3: Check for duplicates before filing
Compare each attachment against what you have already filed and against what you have seen in this run, using the duplicate rule I gave you. If it is a duplicate, do not upload a second copy — report the original's path instead and move on. Count these at the end; on a long history the count is usually the most interesting number in the report.
Step 4: File and rename
Create folders as needed rather than assuming they exist. Upload each file under the name my convention produces, derived from the content you extracted in Step 2. Never overwrite an existing file: if a name is taken by something different, append a discriminator rather than replacing it. Leave the original in Gmail untouched — I want the mail to stay where it is, not to be emptied.
Step 5: Report
Give me: files processed, folders created, files per top-level folder, duplicates skipped, files sent to Unsorted with one line on each saying why, and anything that looked like an invoice but was not one. Then list anything you deliberately did not touch. Stop there — do not delete or move anything in Gmail.