Make search, read and status cheaper and more exact for agent clients (eight fixes from a client review) #106
Labels
No labels
bug
enhancement
epic
needs-info
needs-triage
ready-for-agent
ready-for-human
svd:go
svd:hold
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#106
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
A client review sampled 200
mail_searchrows on one mirror of about 2 GB and listed eight fixes. They are below, most important first. Each item says what the code does today, checked on main170127c, and a proposal. The proposals are open until triage.1.
mail_statusis slowToday.
Store.status(store.py, about line 2000) counts on every call:NOT EXISTSper message;Each count reads the whole table, so the cost grows with the mirror.
/healthzis not affected: it reads only the last sync (#90).Proposal (decide at triage). One of:
mail_status(quick=true)returns sync health and skips the counts.2. Dates: show the offset, sort and filter in UTC
Today.
dateas ISO 8601 with the sender's offset.format_timestampin imap-blade-mcp keeps the offset.date[:16], which drops the offset (server.py_row_cells).sinceandbeforecompareCOALESCE(date, internaldate)as text. Two dates with offsets+11:00and+00:00can sort up to 11 hours out of order. Asinceorbeforebound can include or drop mail by the same amount.Proposal.
Z, or with its offset (decide at triage).copy_key, with the same guard against unsafe dates, and an index.sinceandbeforeaccept a date or a date-time with an offset, and compare in UTC.3. Compact search rows
Today.
_row_cells(server.py:130) cuts the subject at 120 characters and the snippet at 160.|. A subject that holds|adds a cell and breaks the row format.Proposal.
mail_searchtakesfields(which cells to return) andsnippet_chars(0 drops the snippet line).|inside a cell: search, thread, people and evidence rows.4. Counts without rows
Today.
mail_mailboxescounts per mailbox,mail_peopleper sender, andmail_triageper class. None of them takes themail_searchfilters. A question such as "unread this week, by sender" therefore needs every row.Proposal.
mail_search(group_by=...)withsender,mailboxandtriage. It returnskey | messages | unreadunder the same filters, and counts a collapsed message once (#59). The mirror has no labels apart from mailboxes; on JMAP, Fastmail's labels are mailboxes. Somailboxcovers labels unless triage decides otherwise.5. Shorter
mail_readheadersToday.
mail_readprints everyto:andcc:address (server.py, about line 392). One reply was 2,049 characters for a 14-character body, because itscc:list held about 45 addresses.Proposal. Show at most N addresses per list, then
(+M more). N is decided at triage; 5 is a start. Add a parameter that returns the full lists.6. Batch reads and stable paging
Today.
mail_readtakes one id.next_cursorofmail_searchis an offset (server.py_page). A sync pass between two pages moves rows across the page boundary. The reviewer sawtotalchange during one pass.Proposal.
mail_readtakes a list of ids (decide the maximum at triage), with one sharedmax_charsbudget.7. Mailbox names
Today.
mailboxfilter matches the name exactly (store.py_filters).INBOXas its example. On a JMAP account (Fastmail) the inbox is namedInbox, so the example returns nothing, with no warning.rolefilter (SearchParams.role), butmail_searchdoes not expose it.Proposal.
roleonmail_search.not_found(decide at triage).8. Forwarded mail
Today.
mail_readtrims quotes withtrim_quotedfrom imap-blade-mcp. Its pattern cuts at anyFrom: …line after the first line. A forward that startsBegin forwarded message:, followed byFrom: …, keeps only its first line.Proposal. Do not cut at a header block that follows a forward marker, for example
Begin forwarded message:or---------- Forwarded message ---------. The fix belongs in imap-blade-mcp, a pinned dependency, or in a local wrapper (decide at triage).Notes
Sub-issues
Cut at triage 2026-11-02. Each carries its agent brief and the decision it rests on.
Filed under: operator word "file it" sha256:f61c822d8f15
Tool-definition cost and annotations (measured on main
170127c)A review of late-2026 MCP client practice (progressive tool discovery,
search_toolsmeta-tools,defer_loading, output capping, tool annotations) prompted a check of what this server costs an agent before it calls anything. The eight items above are all about result tokens. This note covers definition tokens and adds one item.Measured
tools/listas seen by a write token, serialised as JSON (themcp.types.Toolshape), 4 chars per token as the estimate:mail_searchaloneServer
instructionsare 312 characters. No tool declaresannotations. The transport is alreadystateless_http=True; every cursor is a server-minted string passed back as an ordinary argument, so the server matches the stateless 2026-07-28 model.Conclusions
Do not add a
search_toolsmeta-tool here. The catalogue is 18 tools and about 5k tokens, under every published threshold (10k fixed, or 1 to 5 percent of a 200k window). Progressive discovery is the client's job (Claude Code and others already do it). A server-side helper on a catalogue this size would add a round trip and gain nothing. Keep the catalogue small instead: this is a constraint on items 3, 4 and 7, which each add parameters.Add
ToolAnnotationsto every tool (new item 9). Zero runtime cost, and the clients that defer loading and gate human approval read them. FastMCP 3.4.11tool(annotations=...)acceptsmcp.types.ToolAnnotations. Proposed values:destructiveHintis false on the write tools because every write is a reversible outbox entry (ADR 0004): nothing is sent or deleted.openWorldHintis false everywhere: every tool reads the mirror, never a mail server (ADR 0006 §3). A test intests/test_evidence.pyortests/test_source.pyshould assert the annotation on each registered tool, the same way it asserts "untrusted" in every description.mail_searchis 19 percent of the catalogue. ItsinputSchemais 2,332 characters across 14 parameters. Items 3 (fields,snippet_chars), 4 (group_by) and 7 (role) add four more. Two options for triage: (a) accept the growth and keep each newField(description=...)under about 60 characters; (b) put item 4 in a separatemail_count(group_by, <same filters>)tool, which keeps the ranking parameters (semantic,like,limit,cursor) off the counting path. Option (b) costs one more tool (about 1k chars) but the model picks a counting tool by name instead of by reading a parameter list.Output caps already exist.
limitis bounded at 100 andmax_charsat 60,000 onmail_readandmail_attachment. Items 3, 5 and 6 are the right fixes for result tokens; the batchmail_readin item 6 is also the right shape for clients that script tool calls, because one call replaces N round trips.Nothing here needs an ADR. Annotations and schema size are not decisions in
docs/adr/. Item 9 can go in with any of items 3, 5 or 7, which already touch every tool signature.Addendum: every cap must report the whole
A cap is only safe when the caller can see it was hit. Otherwise an agent reads
limit=100as "there are 100". Audit of the current tools:next_cursor= more= total=footer[truncated at X of Y chars]more=Nfooterreceived: Nplusshown=Mthread_size=N shown=MRule for triage, so items 3 to 6 carry it:
snippet_chars): a cut snippet ends with a visible mark.group_by): a limit on groups, a footer with the group count, and a cursor or a stated top-N rule. A sender grouping over this mirror can return thousands of keys.(+M more)is the right shape.mail_read): the response names which ids were read in full, which were cut, and which were not reached within the shared budget. At minimum a footerread=N truncated=M not_read=<ids>.mail_threada cursor, or raise its cap and keepshownunderthread_size. Decide with item 6, which already changes paging.Triage 2026-11-02
Every "Today" claim was verified against main
170127c. #106 is now anepic; the work is in the six sub-issues listed in the body, eachready-for-agentwith an agent brief.Decisions taken at triage, one per open point:
quick=true; no counts table.Z; the sort and filter key is a stored column or expression index, never a VIRTUAL column (image SQLite 3.40.1, #96).mail_counttool, not agroup_byon mail_search.max_addressesoverride.max_charsbudget; semantic and hybrid paging are deferred to their own issue.Rule for every sub-issue: a cap reports the whole (footer with total or count, a visible mark on a cut cell, named unreached ids).
Item 9 (annotations) is delivered by #116, not #109. #109 keeps items 3, 5 and the batch read.
Closing. Every sub-issue is closed: #108, #109, #110, #111, #112, #113 and #133. The #113 pin bump landed in #155.