Tutorial
Paginate Long MCP Tool Results
When a Result Needs a Cursor
A tool result that can grow without a bound needs a way to be read in pieces. A catalog search, a list of open tickets, an export of contacts: each returns a set that is small in a test and large in production.
Two failures follow from ignoring this. The server truncates silently and the model answers from an incomplete set. Or the server returns everything and the context fills with rows nobody asked about.
A cursor solves both, provided the client and the model are told what to do with it.
The Shape of a Page
A page has three parts, and a fourth is worth adding:
- The items for this page, each with a stable identifier.
- A cursor pointing at the next page, absent when there is none.
- A flag stating whether more results exist, so a caller need not infer it from an empty cursor.
- A total count, when the upstream can supply one cheaply. It answers the question a person asks first.
Keep the cursor opaque. It is a server detail, and a cursor the model can construct is a cursor the model will construct incorrectly.
Walking the Set
A walk is a loop with three guarantees: it stops, it does not repeat, and it reports what it did.
Stop on the absence of a cursor, not on an empty page, because an empty page in the middle of a walk is legal when the upstream filters after pagination. Cap the number of pages per call so a broken upstream cannot loop forever. And return the count of pages read in the tool's summary, so a person can see that the answer came from three pages rather than one.
When the cap is reached, say so. A partial answer presented as complete is the failure this whole mechanism exists to prevent.
Duplicate Identifiers
The most common pagination bug is a set sorted by a field that changes. A catalog sorted by updated timestamp returns the same row twice when a record is edited between pages, and omits another row entirely.
Fix it in the query rather than the loop: sort by a stable, unique key, or by a stable key with the identifier as a tie breaker. Then, in the collector, deduplicate by identifier and count what was removed.
If duplicates appear, log them. A duplicate count that rises is a signal that the underlying ordering is no longer stable, which is information about the source system rather than about your loop.
Bounds and Stops
Three limits belong in the design, not in a comment.
- Page size: bounded by the tool, not chosen by the caller, or bounded with a maximum.
- Pages per call: a small number, with a cursor returned when the limit is reached.
- Timeout: shorter than the client's patience, with a partial result rather than a hang.
Publish all three in the tool description. A model told that a call returns at most fifty records will ask for another page; a model told nothing will assume it has everything.
Verifying
Build a fixture of twenty-five records, set the page size to ten, and walk the set. Assert three calls, twenty-five unique identifiers and no duplicates. Then edit a record between the first and second call and confirm the walk still returns twenty-five unique identifiers.
Finally, set the page cap to one and confirm the response reports that more results exist rather than presenting the first page as the answer.
Pagination in the Tool Description
The mechanism is only half the work. A model will not ask for a second page unless the description tells it that pages exist.
Say three things in the description: the maximum number of records a call returns, that a cursor is available when more exist, and that the caller should continue until the cursor is absent. Without those sentences, the tool behaves correctly and the model still answers from the first page.
Then make the cursor visible in the input schema as an optional string, described as the value returned by the previous call. A cursor that exists only in the result and not in the input cannot be used at all.
Finally, decide what a summary sentence says on a partial result. A line such as twelve of forty records shown, more available tells a person exactly what they are looking at, and it takes one string to produce.
Next step
Read the release checklist before the paginated tool ships, or contact us to review a result design that has outgrown a single response.
FAQ
Question: Should the cursor be an offset or a token?
A token, in almost every case. Offsets shift when rows are inserted, and an offset that shifts silently duplicates and skips records. A token that encodes the last stable key avoids both.
Question: Can the caller choose the page size?
Allow it only with a maximum. An unbounded page size is a denial of service waiting for a polite request.
Question: What should a tool return when a walk is capped?
The records read so far, a cursor, and a sentence stating that more results exist. Silence about the cap is the one unacceptable option.