An MCP list tool usually returns a page of rows and a cursor for the next page. That is
the shape the MCP specification uses: a response holds the current page and a
nextCursor "if more results exist". The spec defines no total
(MCP spec, Pagination).
Why an agent needs a total
A cursor tells the agent there is more. It doesn't tell it how much more. With a cursor
alone, the agent can't tell whether it has read 2% of a list or 98%, and the cursor
invites another call.
With a total, the agent can see that a list is too large and narrow the query (by
status, date, and so on) instead of paging through it.
Why an exact total is a problem
Getting an exact count can be expensive. PostgreSQL has to process all matching rows,
which can be slow on large tables
(PostgreSQL wiki, Slow Counting).
But does the agent really need to know the exact number? In most cases, it only needs
to know whether there are more results than it can page through. Knowing whether there
are 1,000 or 100,000 matches doesn't change what the agent should do next.
Count only up to a cap
pganalyze calls this a "limited COUNT"
(Speeding up partial COUNT(*) in Postgres by using LIMIT in a subquery):
put a LIMIT inside a subquery and count its rows.
SELECT COUNT(*) FROM (
SELECT 1 FROM runs WHERE workflow_id = 42 LIMIT 501
) sub;
Postgres stops reading at 501 rows. The extra row is how you tell "exactly 500" from
"more than 500".
In Django, a sliced queryset produces this query:
CAP = 500
count = queryset[: CAP + 1].count()
total = min(count, CAP)
total_capped = count > CAP
The result carries both fields. total_capped: true means "at least 500".
Use one number for both limits
A total tells the agent how big the list is. It doesn't stop the agent from asking for
page after page, so paging needs its own limit too.
Say the tool returns 50 rows per call and stops giving a next cursor after page 10.
Then the agent can read at most 500 rows.
Use 500 as the count cap too. Now total_capped: true says two things at once: there
are more than 500 rows, and the agent can't get to the end by paging. So it should ask
for less instead.
{
"rows": ["...50 rows..."],
"total": 500,
"total_capped": true,
"next_cursor": "..."
}
Write this advice in the field's description, because the agent reads it together with
the result: "More match than paging can reach. Narrow the filters instead of paging."
One more saving: if the first page already has every row, just count the rows on that
page. No count query is needed.
How much it saves
Both counts on one workflow with about 550,000 rows in Postgres, warm cache, no extra
filters:
| Query | Time | Pages read |
|---|---|---|
| Exact count | 91.4 ms, 3 parallel workers | 253,078 (about 2 GB) |
| Count up to 501 | 0.21 ms | 81 (about 650 KB) |
The capped count is about 430 times faster and reads about 3,100 times fewer pages. It
costs the same no matter how big the list grows. The exact count grows with the data,
and gets more expensive once real permission filters are added.
Originally published at serhiisoldatov.dev.
Top comments (0)