message_id is designed to remain consistent as long as the mailbox’s UIDVALIDITY doesn’t change.
Format
Message IDs follow a structured format that encodes all necessary information to locate a message:Components
| Component | Description | Example |
|---|---|---|
imap | Fixed prefix identifying the format | imap |
account_id | Account identifier from configuration | default, work, personal |
mailbox | IMAP mailbox/folder name | INBOX, Archive, Projects:2026 |
uidvalidity | IMAP UIDVALIDITY value (session identifier) | 12345 |
uid | IMAP UID (message identifier) | 42 |
Examples
Handling Mailbox Names with Colons
Mailbox names containing colons are fully preserved in the format. The parsing logic is designed to handle such cases correctly. For a mailbox namedProjects:2026:Q1:
- The
message_idwould be:imap:personal:Projects:2026:Q1:999:7 - Parsing correctly extracts:
account_id=personalmailbox=Projects:2026:Q1uidvalidity=999uid=7
The parser works by extracting the last two segments as
uid and uidvalidity, then joining all intermediate segments as the mailbox name.Implementation Details
The message ID parsing and encoding logic is implemented insrc/message_id.rs:
Parsing
The parser validates that:- The string starts with
imap: - At least 5 segments are present (prefix, account, mailbox, uidvalidity, uid)
uidvalidityanduidare valid non-negative integers- The mailbox segment is not empty
Encoding
Theencode() method produces the canonical string format:
Stability and UIDVALIDITY
Themessage_id is only stable while the mailbox’s UIDVALIDITY remains unchanged. IMAP servers may change UIDVALIDITY when:
- The mailbox is deleted and recreated
- A server-side mailbox migration occurs
- Certain mailbox operations are performed on the server
Usage in Tools
Themessage_id is required by the following MCP tools:
imap_get_message- Fetch message details (src/server.rs:183)imap_get_message_raw- Fetch RFC822 source (src/server.rs:206)imap_update_message_flags- Modify flags (src/server.rs:228)imap_copy_message- Copy to another mailbox (src/server.rs:246)imap_move_message- Move to another mailbox (src/server.rs:266)imap_delete_message- Delete message (src/server.rs:286)
Always obtain
message_ids from imap_search_messages output rather than constructing them manually. This ensures the UIDVALIDITY is current and the format is correct.Validation
When tools receive amessage_id, the server performs several validation steps:
- Parse the message ID - Extract components and validate format
- Validate mailbox name - Check for control characters and length
- Match account ID - Ensure the embedded account matches the request
- Verify UIDVALIDITY - Confirm it still matches the current mailbox state
src/server.rs:1809:
Error Handling
Invalid Format
If the message ID format is invalid:Account Mismatch
If the embedded account doesn’t match the request:UIDVALIDITY Conflict
If the mailbox UIDVALIDITY has changed:Best Practices
- Don’t construct manually - Always get
message_ids from search results - Check UIDVALIDITY conflicts - Handle conflict errors by re-searching
- Don’t cache long-term - Message IDs may become invalid if UIDVALIDITY changes
- Validate before use - Let the server validate rather than parsing client-side
- Use atomic operations - Complete operations quickly to avoid UIDVALIDITY changes mid-workflow