Make search, read and status cheaper and more exact for agent clients (eight fixes from a client review) #106

Closed
opened 2026-11-01 20:38:51 +00:00 by svc-agent-writer · 5 comments

A client review sampled 200 mail_search rows 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 main 170127c, and a proposal. The proposals are open until triage.

1. mail_status is slow

Today. Store.status (store.py, about line 2000) counts on every call:

  • the messages per account;
  • the attachment text rows;
  • the attachment backlog, through a NOT EXISTS per message;
  • the attachment chunk vectors.

Each count reads the whole table, so the cost grows with the mirror. /healthz is not affected: it reads only the last sync (#90).

Proposal (decide at triage). One of:

  • the sync worker keeps the counts in a small table, updated after each pass;
  • mail_status(quick=true) returns sync health and skips the counts.

2. Dates: show the offset, sort and filter in UTC

Today.

  • The sources store date as ISO 8601 with the sender's offset. format_timestamp in imap-blade-mcp keeps the offset.
  • Search rows show date[:16], which drops the offset (server.py _row_cells).
  • Search order, since and before compare COALESCE(date, internaldate) as text. Two dates with offsets +11:00 and +00:00 can sort up to 11 hours out of order. A since or before bound can include or drop mail by the same amount.

Proposal.

  • Rows show every date in UTC with a Z, or with its offset (decide at triage).
  • Order and filter on a UTC key. One way is a VIRTUAL generated column like the #96 copy_key, with the same guard against unsafe dates, and an index.
  • since and before accept a date or a date-time with an offset, and compare in UTC.
  • Bench in the image, with head and base alternating in one container. The image's SQLite 3.40.1 does not cover an index on a virtual column (#96).

3. Compact search rows

Today.

  • _row_cells (server.py:130) cuts the subject at 120 characters and the snippet at 160.
  • No cell escapes |. A subject that holds | adds a cell and breaks the row format.
  • The snippet is the stored source snippet, with URLs, Markdown and JSON-LD left in. The reviewer measured snippets at about 56% of each row, and found such text in 123 of 200 snippets. These two figures are the reviewer's and are not checked here.

Proposal.

  • mail_search takes fields (which cells to return) and snippet_chars (0 drops the snippet line).
  • Every row format escapes or replaces | inside a cell: search, thread, people and evidence rows.
  • Snippets drop URLs, Markdown link and emphasis syntax, and JSON-LD blocks. Clean at render or at ingest (decide at triage).

4. Counts without rows

Today. mail_mailboxes counts per mailbox, mail_people per sender, and mail_triage per class. None of them takes the mail_search filters. A question such as "unread this week, by sender" therefore needs every row.

Proposal. mail_search(group_by=...) with sender, mailbox and triage. It returns key | messages | unread under 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. So mailbox covers labels unless triage decides otherwise.

5. Shorter mail_read headers

Today. mail_read prints every to: and cc: address (server.py, about line 392). One reply was 2,049 characters for a 14-character body, because its cc: 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_read takes one id.
  • The next_cursor of mail_search is an offset (server.py _page). A sync pass between two pages moves rows across the page boundary. The reviewer saw total change during one pass.

Proposal.

  • mail_read takes a list of ids (decide the maximum at triage), with one shared max_chars budget.
  • Keyword and filter modes page by a keyset cursor over the UTC date and the id. This depends on item 2. Semantic and hybrid modes page over a ranking, so they need a separate decision.

7. Mailbox names

Today.

  • The mailbox filter matches the name exactly (store.py _filters).
  • The parameter description gives INBOX as its example. On a JMAP account (Fastmail) the inbox is named Inbox, so the example returns nothing, with no warning.
  • The store already has a role filter (SearchParams.role), but mail_search does not expose it.

Proposal.

  • Expose role on mail_search.
  • Match mailbox names without regard to case.
  • When a name matches no mirrored mailbox, say so: a warning line, or the code not_found (decide at triage).
  • Fix the example in the description.

8. Forwarded mail

Today. mail_read trims quotes with trim_quoted from imap-blade-mcp. Its pattern cuts at any From: … line after the first line. A forward that starts Begin forwarded message:, followed by From: …, 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

  • The sample held no text addressed to an assistant. Every tool already wraps content as untrusted data.
  • This is eight changes. Triage may split it into sub-issues. Items 2 and 6 share the UTC key, and items 3 and 5 share the row budget.

Sub-issues

Cut at triage 2026-11-02. Each carries its agent brief and the decision it rests on.

  • #108 items 2, 6 (paging), 10: UTC key, keyset paging, mail_thread cursor
  • #109 items 3, 5: escaped cells, cleaned snippets, address cap (tool annotations shipped with #116)
  • #133 item 6 (batch read): mail_read with up to 20 ids
  • #110 item 1: mail_status(quick=true)
  • #111 item 4: mail_count
  • #112 item 7: mailbox names, role filter, unknown-mailbox warning
  • #113 item 8: trim_quoted forward fix in imap-blade-mcp plus pin bump

Filed under: operator word "file it" sha256:f61c822d8f15

A client review sampled 200 `mail_search` rows 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 main `170127c`, and a proposal. The proposals are open until triage. ## 1. `mail_status` is slow **Today.** `Store.status` (`store.py`, about line 2000) counts on every call: - the messages per account; - the attachment text rows; - the attachment backlog, through a `NOT EXISTS` per message; - the attachment chunk vectors. Each count reads the whole table, so the cost grows with the mirror. `/healthz` is not affected: it reads only the last sync (#90). **Proposal (decide at triage).** One of: - the sync worker keeps the counts in a small table, updated after each pass; - `mail_status(quick=true)` returns sync health and skips the counts. ## 2. Dates: show the offset, sort and filter in UTC **Today.** - The sources store `date` as ISO 8601 with the sender's offset. `format_timestamp` in imap-blade-mcp keeps the offset. - Search rows show `date[:16]`, which drops the offset (`server.py` `_row_cells`). - Search order, `since` and `before` compare `COALESCE(date, internaldate)` as text. Two dates with offsets `+11:00` and `+00:00` can sort up to 11 hours out of order. A `since` or `before` bound can include or drop mail by the same amount. **Proposal.** - Rows show every date in UTC with a `Z`, or with its offset (decide at triage). - Order and filter on a UTC key. One way is a VIRTUAL generated column like the #96 `copy_key`, with the same guard against unsafe dates, and an index. - `since` and `before` accept a date or a date-time with an offset, and compare in UTC. - Bench in the image, with head and base alternating in one container. The image's SQLite 3.40.1 does not cover an index on a virtual column (#96). ## 3. Compact search rows **Today.** - `_row_cells` (`server.py:130`) cuts the subject at 120 characters and the snippet at 160. - No cell escapes ` | `. A subject that holds ` | ` adds a cell and breaks the row format. - The snippet is the stored source snippet, with URLs, Markdown and JSON-LD left in. The reviewer measured snippets at about 56% of each row, and found such text in 123 of 200 snippets. These two figures are the reviewer's and are not checked here. **Proposal.** - `mail_search` takes `fields` (which cells to return) and `snippet_chars` (0 drops the snippet line). - Every row format escapes or replaces ` | ` inside a cell: search, thread, people and evidence rows. - Snippets drop URLs, Markdown link and emphasis syntax, and JSON-LD blocks. Clean at render or at ingest (decide at triage). ## 4. Counts without rows **Today.** `mail_mailboxes` counts per mailbox, `mail_people` per sender, and `mail_triage` per class. None of them takes the `mail_search` filters. A question such as "unread this week, by sender" therefore needs every row. **Proposal.** `mail_search(group_by=...)` with `sender`, `mailbox` and `triage`. It returns `key | messages | unread` under 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. So `mailbox` covers labels unless triage decides otherwise. ## 5. Shorter `mail_read` headers **Today.** `mail_read` prints every `to:` and `cc:` address (`server.py`, about line 392). One reply was 2,049 characters for a 14-character body, because its `cc:` 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_read` takes one id. - The `next_cursor` of `mail_search` is an offset (`server.py` `_page`). A sync pass between two pages moves rows across the page boundary. The reviewer saw `total` change during one pass. **Proposal.** - `mail_read` takes a list of ids (decide the maximum at triage), with one shared `max_chars` budget. - Keyword and filter modes page by a keyset cursor over the UTC date and the id. This depends on item 2. Semantic and hybrid modes page over a ranking, so they need a separate decision. ## 7. Mailbox names **Today.** - The `mailbox` filter matches the name exactly (`store.py` `_filters`). - The parameter description gives `INBOX` as its example. On a JMAP account (Fastmail) the inbox is named `Inbox`, so the example returns nothing, with no warning. - The store already has a `role` filter (`SearchParams.role`), but `mail_search` does not expose it. **Proposal.** - Expose `role` on `mail_search`. - Match mailbox names without regard to case. - When a name matches no mirrored mailbox, say so: a warning line, or the code `not_found` (decide at triage). - Fix the example in the description. ## 8. Forwarded mail **Today.** `mail_read` trims quotes with `trim_quoted` from imap-blade-mcp. Its pattern cuts at any `From: …` line after the first line. A forward that starts `Begin forwarded message:`, followed by `From: …`, 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 - The sample held no text addressed to an assistant. Every tool already wraps content as untrusted data. - This is eight changes. Triage may split it into sub-issues. Items 2 and 6 share the UTC key, and items 3 and 5 share the row budget. ## Sub-issues Cut at triage 2026-11-02. Each carries its agent brief and the decision it rests on. - #108 items 2, 6 (paging), 10: UTC key, keyset paging, mail_thread cursor - #109 items 3, 5: escaped cells, cleaned snippets, address cap (tool annotations shipped with #116) - #133 item 6 (batch read): mail_read with up to 20 ids - #110 item 1: mail_status(quick=true) - #111 item 4: mail_count - #112 item 7: mailbox names, role filter, unknown-mailbox warning - #113 item 8: trim_quoted forward fix in imap-blade-mcp plus pin bump Filed under: operator word "file it" sha256:f61c822d8f15
Author
Member

Tool-definition cost and annotations (measured on main 170127c)

A review of late-2026 MCP client practice (progressive tool discovery, search_tools meta-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/list as seen by a write token, serialised as JSON (the mcp.types.Tool shape), 4 chars per token as the estimate:

Scope Tools Chars Approx. tokens
write (all tools) 18 20,209 5,050
mail_search alone 1 3,760 940

Server instructions are 312 characters. No tool declares annotations. The transport is already stateless_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

  1. Do not add a search_tools meta-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.

  2. Add ToolAnnotations to every tool (new item 9). Zero runtime cost, and the clients that defer loading and gate human approval read them. FastMCP 3.4.11 tool(annotations=...) accepts mcp.types.ToolAnnotations. Proposed values:

Tools readOnlyHint destructiveHint idempotentHint openWorldHint
mail_search, mail_read, mail_thread, mail_attachment, mail_mailboxes, mail_people, mail_evidence, mail_status, mail_changes, mail_triage, mail_triage_rows, mail_actions, mail_wakes true false true false
mail_wakes_ack false false true false
mail_draft, mail_flag, mail_move false false false false
mail_undo false false true false

destructiveHint is false on the write tools because every write is a reversible outbox entry (ADR 0004): nothing is sent or deleted. openWorldHint is false everywhere: every tool reads the mirror, never a mail server (ADR 0006 §3). A test in tests/test_evidence.py or tests/test_source.py should assert the annotation on each registered tool, the same way it asserts "untrusted" in every description.

  1. mail_search is 19 percent of the catalogue. Its inputSchema is 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 new Field(description=...) under about 60 characters; (b) put item 4 in a separate mail_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.

  2. Output caps already exist. limit is bounded at 100 and max_chars at 60,000 on mail_read and mail_attachment. Items 3, 5 and 6 are the right fixes for result tokens; the batch mail_read in item 6 is also the right shape for clients that script tool calls, because one call replaces N round trips.

  3. 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.

## Tool-definition cost and annotations (measured on main 170127c) A review of late-2026 MCP client practice (progressive tool discovery, `search_tools` meta-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/list` as seen by a write token, serialised as JSON (the `mcp.types.Tool` shape), 4 chars per token as the estimate: | Scope | Tools | Chars | Approx. tokens | | --- | --- | --- | --- | | write (all tools) | 18 | 20,209 | 5,050 | | `mail_search` alone | 1 | 3,760 | 940 | Server `instructions` are 312 characters. No tool declares `annotations`. The transport is already `stateless_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 1. **Do not add a `search_tools` meta-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. 2. **Add `ToolAnnotations` to every tool (new item 9).** Zero runtime cost, and the clients that defer loading and gate human approval read them. FastMCP 3.4.11 `tool(annotations=...)` accepts `mcp.types.ToolAnnotations`. Proposed values: | Tools | readOnlyHint | destructiveHint | idempotentHint | openWorldHint | | --- | --- | --- | --- | --- | | mail_search, mail_read, mail_thread, mail_attachment, mail_mailboxes, mail_people, mail_evidence, mail_status, mail_changes, mail_triage, mail_triage_rows, mail_actions, mail_wakes | true | false | true | false | | mail_wakes_ack | false | false | true | false | | mail_draft, mail_flag, mail_move | false | false | false | false | | mail_undo | false | false | true | false | `destructiveHint` is false on the write tools because every write is a reversible outbox entry (ADR 0004): nothing is sent or deleted. `openWorldHint` is false everywhere: every tool reads the mirror, never a mail server (ADR 0006 §3). A test in `tests/test_evidence.py` or `tests/test_source.py` should assert the annotation on each registered tool, the same way it asserts "untrusted" in every description. 3. **`mail_search` is 19 percent of the catalogue.** Its `inputSchema` is 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 new `Field(description=...)` under about 60 characters; (b) put item 4 in a separate `mail_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. 4. **Output caps already exist.** `limit` is bounded at 100 and `max_chars` at 60,000 on `mail_read` and `mail_attachment`. Items 3, 5 and 6 are the right fixes for result tokens; the batch `mail_read` in item 6 is also the right shape for clients that script tool calls, because one call replaces N round trips. 5. **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.
Author
Member

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=100 as "there are 100". Audit of the current tools:

Tool Cap Signal today Gap
mail_search, mail_people, mail_triage_rows limit ≤ 100 next_cursor= more= total= footer none
mail_read, mail_attachment max_chars ≤ 60,000 [truncated at X of Y chars] none
mail_changes, mail_wakes, mail_actions limit ≤ 100 more=N footer none (cursor logs)
mail_evidence max_messages ≤ 50 received: N plus shown=M none
mail_thread limit ≤ 100 thread_size=N shown=M no cursor: a thread over 100 messages can never be read past the first 100
mail_mailboxes, mail_triage none n/a none

Rule for triage, so items 3 to 6 carry it:

  • Item 3 (snippet_chars): a cut snippet ends with a visible mark.
  • Item 4 (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.
  • Item 5 (address lists): the proposed (+M more) is the right shape.
  • Item 6 (batch 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 footer read=N truncated=M not_read=<ids>.
  • New item 10: give mail_thread a cursor, or raise its cap and keep shown under thread_size. Decide with item 6, which already changes paging.
## 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=100` as "there are 100". Audit of the current tools: | Tool | Cap | Signal today | Gap | | --- | --- | --- | --- | | mail_search, mail_people, mail_triage_rows | limit ≤ 100 | `next_cursor= more= total=` footer | none | | mail_read, mail_attachment | max_chars ≤ 60,000 | `[truncated at X of Y chars]` | none | | mail_changes, mail_wakes, mail_actions | limit ≤ 100 | `more=N` footer | none (cursor logs) | | mail_evidence | max_messages ≤ 50 | `received: N` plus `shown=M` | none | | mail_thread | limit ≤ 100 | `thread_size=N shown=M` | **no cursor**: a thread over 100 messages can never be read past the first 100 | | mail_mailboxes, mail_triage | none | n/a | none | Rule for triage, so items 3 to 6 carry it: - **Item 3** (`snippet_chars`): a cut snippet ends with a visible mark. - **Item 4** (`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. - **Item 5** (address lists): the proposed `(+M more)` is the right shape. - **Item 6** (batch `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 footer `read=N truncated=M not_read=<ids>`. - **New item 10**: give `mail_thread` a cursor, or raise its cap and keep `shown` under `thread_size`. Decide with item 6, which already changes paging.
Author
Member

This was generated by AI during triage.

Triage 2026-11-02

Every "Today" claim was verified against main 170127c. #106 is now an epic; the work is in the six sub-issues listed in the body, each ready-for-agent with an agent brief.

Decisions taken at triage, one per open point:

  1. mail_status gets quick=true; no counts table.
  2. Rows show UTC with Z; the sort and filter key is a stored column or expression index, never a VIRTUAL column (image SQLite 3.40.1, #96).
  3. Snippets are cleaned at render; the stored snippet and FTS index are unchanged.
  4. Counts live in a new mail_count tool, not a group_by on mail_search.
  5. Address lists cap at 5 with a numeric max_addresses override.
  6. Batch mail_read takes up to 20 ids under one max_chars budget; semantic and hybrid paging are deferred to their own issue.
  7. An unknown mailbox name gives a warning line, not an error.
  8. The forward fix lands in imap-blade-mcp, then the pin is bumped here.

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).

> *This was generated by AI during triage.* ## Triage 2026-11-02 Every "Today" claim was verified against main 170127c. #106 is now an `epic`; the work is in the six sub-issues listed in the body, each `ready-for-agent` with an agent brief. Decisions taken at triage, one per open point: 1. mail_status gets `quick=true`; no counts table. 2. Rows show UTC with `Z`; the sort and filter key is a stored column or expression index, never a VIRTUAL column (image SQLite 3.40.1, #96). 3. Snippets are cleaned at render; the stored snippet and FTS index are unchanged. 4. Counts live in a new `mail_count` tool, not a `group_by` on mail_search. 5. Address lists cap at 5 with a numeric `max_addresses` override. 6. Batch mail_read takes up to 20 ids under one `max_chars` budget; semantic and hybrid paging are deferred to their own issue. 7. An unknown mailbox name gives a warning line, not an error. 8. The forward fix lands in imap-blade-mcp, then the pin is bumped here. 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).
Author
Member

This was generated by AI during triage.

Item 9 (annotations) is delivered by #116, not #109. #109 keeps items 3, 5 and the batch read.

> *This was generated by AI during triage.* Item 9 (annotations) is delivered by #116, not #109. #109 keeps items 3, 5 and the batch read.
Author
Member

Closing. Every sub-issue is closed: #108, #109, #110, #111, #112, #113 and #133. The #113 pin bump landed in #155.

Closing. Every sub-issue is closed: #108, #109, #110, #111, #112, #113 and #133. The #113 pin bump landed in #155.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/mail-mind-mcp#106
No description provided.