An agent calls your tickets_list tool with limit: 1000, and the tool hands the number to the
ticketing API. Every ticket comes back whole, body and requester and SLA, at about 200 tokens a
row. The result lands in the agent’s history and travels with every later call, so one list
request crowds out the rest of the task.
What you get
You will end up with a list tool whose results stay under a fixed token budget, a page size chosen from measured tokens per row, and a cursor that survives inserts. This is for you if you write MCP servers, or wrap a paginated API in a tool for your own agent.
Short answer
Give the MCP list tool a limit argument with a schema maximum and a stated default. Return
a projection of each row rather than the whole row, and cut every page to a fixed token budget.
Continue with an opaque next_cursor that encodes a keyset position and the query’s filter,
the pattern MCP uses for tools/list. Avoid offset paging, which repeats rows inserted between
calls and never ends on a busy list.
You will need
Node 22 or later, @modelcontextprotocol/server and @modelcontextprotocol/client, zod, and
js-tiktoken for counting. Verified 2026-11-29 against Node 22.23.5 and version 2.4.0 of both
SDK packages, which the server package’s README calls the stable release line of the
MCP TypeScript SDK. The client and
server in the sample agreed on protocol revision 2025-11-25, the highest one 2.4.0 offers in its
initialize handshake, although the README names 2026-07-28. The links below point at the
2025-11-25 edition. A stand-in ticketing API with 400 seeded tickets runs in process. Field selection itself
is covered in
shape a tool result so an agent can reason about it.
Approaches compared
| Approach | When it fits | What it costs you | When to pick something else |
|---|---|---|---|
Capped limit with has_more | Short lists, or an API that only offers offset paging | Offsets shift when rows are inserted, so pages repeat rows or never end | The list changes while an agent reads it |
Opaque cursor with next_cursor | Long or busy lists an agent reads page by page | A cursor format to version and validate, and no total or random access | The agent needs a count or a jump to page nine |
resource_link to the full result | Hosts that fetch resources into a file or a program, not into context | A stored export per call, and the whole set arrives if a host inlines it | The host reads resources into the model’s context |
A capped limit is the cheapest design to add over an offset API, and it costs you correctness on
a list that moves. The cursor costs a format you own, in exchange for pages that hold still. A
resource_link keeps the tool result at 15 tokens, but the export behind it is 8,638 tokens of
NDJSON, so it only helps a host that keeps resources out of the prompt.
Measure tokens per row before choosing a page size
A page size is a token budget divided by the cost of one row, so measure the row. The script
counts each ticket three ways with the o200k_base encoding, then finds the largest page that
fits a 1,200-token result, cursor included.
node measure.mjs
tokens per row (o200k_base) min median max
API row as returned 178 215 253
every field 164 201 239
default fields 44 45 47
rows that fit a 1200-token result
default fields 26
every field 5
default fields, largest rows first 24
At the median, five default fields cost under a quarter of a full row. That projection lets 26 rows fit at the head of the list where five full rows would, so the cap is 25. The 25 largest rows in the data do not fit, which is why the tool still cuts every page to the budget. Anthropic’s guidance on writing tools asks for sensible default parameters on any tool that could use up lots of context.
Cap the limit where the agent reads it
The schema carries the cap, and the description repeats it in words. An agent sees both in
tools/list, so it stops asking for 1,000.
const status = z.enum(['open', 'pending', 'closed']).optional().describe('Only tickets with this status.')
const fields = z.array(z.enum(FIELDS)).min(1).optional()
.describe(`Fields to return. Default: ${DEFAULT_FIELDS.join(', ')}. body is the largest; ask for it only when you need it.`)
const limit = z.number().int().min(1).max(MAX_LIMIT).default(DEFAULT_LIMIT)
.describe(`Tickets per page, 1 to ${MAX_LIMIT}, default ${DEFAULT_LIMIT}. Larger values are refused.`)
Return an opaque cursor bound to the query
The cursor design reads one page past the last id it returned, then cuts the page to the budget.
page measures the whole result, cursor and all, and refuses one that cannot fit a single row.
if (design === 'cursor') {
server.registerTool('tickets_list', {
description: `List support tickets, newest first, at most ${MAX_LIMIT} per call. ` +
'If the result has next_cursor, pass it back as cursor to get the next page; if it has none, there are no more.',
inputSchema: z.object({ status, fields, limit, cursor: z.string().optional().describe('next_cursor from the previous page.') }),
annotations: { readOnlyHint: true },
}, async (args) => {
const pos = args.cursor ? decodeCursor(args.cursor, args.status) : {}
if (pos.error) return fail(pos.error)
const { rows, more } = api.listAfter({ after: pos.after, limit: args.limit, status: args.status })
const tickets = rows.map((r) => project(r, args.fields))
return page(tickets.length, (n) => {
const result = { tickets: tickets.slice(0, n) }
if (n < rows.length || more) result.next_cursor = encodeCursor({ after: rows[n - 1].id, status: args.status })
return result
})
})
}
The cursor is base64url JSON holding a version, the last id, and the status filter.
decodeCursor in paging.mjs refuses anything else with a message that tells the agent what to
do next. For its own list operations, the specification asks servers to handle invalid cursors
gracefully.1
node demo.mjs cursor
protocol revision agreed: 2025-11-25
limit as advertised: {"default":10,"description":"Tickets per page, 1 to 25, default 10. Larger values are refused.","type":"integer","minimum":1,"maximum":25}
limit 1000: Input validation error: Invalid arguments for tool tickets_list: limit: Too big: expected number to be <=25
page 1: tkt_000399 tkt_000398 tkt_000396 next_cursor eyJ2IjoxLCJhZnRlciI6InRrdF8wMDAzOTYiLCJzdGF0dXMiOiJvcGVuIn0
page 2: tkt_000395 tkt_000392 tkt_000388
same cursor, status closed: cursor belongs to a query with another status filter; repeat that filter or start over
cursor "page=2": cursor is not one this tool issued; call again without a cursor to start over
limit 25, every field: 5 tickets, 1058 tokens, next_cursor present
The SDK refuses limit: 1000 before the handler runs, and returns the refusal as a tool error the
agent can read. The last line is the budget at work: asked for every field, the tool sends five
tickets and a cursor rather than 25 tickets over budget.
Keep the default page small
Each page the agent reads stays in its history. A scripted agent, a loop and not a model, pages through pending tickets until it meets the first urgent one, and the script adds up the result tokens it kept.
node measure.mjs
find the newest urgent pending ticket (scripted policy)
limit fields calls tickets read tokens in history
5 default 4 20 1081
10 default 2 20 979
25 default 1 25 1147
25 every field 4 20 4280
A limit of 25 saves round trips and reads five tickets the agent never needed. A limit of five pays a cursor and a wrapper on every call. Ten lands lowest for this policy, which is why it is the default. Asking for every field costs more than three and a half times as much as any default run, so projection matters more than page size.
Hand over the whole set as a resource_link
The third design returns a link instead of rows. A host that can read resources fetches the
export with resources/read.
node demo.mjs link
tool result: {"name":"tickets-exp_1.ndjson","uri":"tickets://exports/exp_1","description":"192 tickets, newest first","mimeType":"application/x-ndjson","type":"resource_link"}
tool result text: {"count":192,"export":"tickets://exports/exp_1"} (15 tokens)
resources/read tickets://exports/exp_1: 192 lines, 8638 tokens
resources/read tickets://exports/exp_9: error -32602, Resource not found: tickets://exports/exp_9
The tool result fits any budget, while the export behind it is seven times the budget. What happens to those 8,638 tokens is the host’s decision, since the tools page of the specification describes a link that the client can fetch or subscribe to. The server also keeps every export it made, so a real one needs an expiry and a size cap.2
Check it worked
The eleven tests check the cap, the budget, the refusals, and how both paging designs behave under inserts. One
walks every open ticket by cursor and checks each arrives once, in order. Another reads the
resource_link export back.
node --test paginate.test.mjs
1..11
# tests 11
# suites 0
# pass 11
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 2495.583906
Expect # pass 11. Remove the budget check from fitToBudget and three tests fail. The 25
largest rows go out as one page, the walk with every field sends pages over 1,200 tokens, and the
oversized ticket goes out instead of a refusal.
When it goes wrong
An agent pages with offset and the list keeps growing. The offset counts from the head of a
newest-first list, so every insert pushes rows the agent has read back into its next page. The
script walks 60 tickets ten at a time while tickets arrive between calls.
node pitfall.mjs
design inserts calls done of the 60 seen arrivals seen repeated
offset 0 6 yes 60 0 0
offset 3 9 yes 60 0 24
offset 10 50 no 10 0 10
cursor 3 6 yes 60 0 0
cursor 10 6 yes 60 0 0
Three inserts per call cost the offset walk three extra calls and 24 repeated tickets. Ten
inserts per call match the page size, so has_more stays true and the walk stops only at the
50-call ceiling, having seen ten of the 60. The cursor walk finishes in six calls either way,
because a keyset position does not move when rows land ahead of it. Neither design returned an
arrival, because each one lands at the head of the list, behind where the walk had reached.
A cursor issued for one filter and sent back with another resumes the second query at a position the first one chose. The cursor here carries its filter, so the tool refuses the mismatch instead of guessing.
When not to do this
Do not page a list the agent should filter instead. If it wants one ticket, give it a search or
a status argument, because the cheapest page is the one it never reads.
Do not expose offsets to an agent on a list that changes while it reads. Keep has_more and a
total for a short, static list, and use a cursor everywhere else.
Do not return a resource_link to a host you have not checked. If the host inlines the
resource, the agent gets the 8,638 tokens the budget was meant to stop.
Do not treat the 1,200-token budget or the limit of ten as constants. They come from this data
and one tokenizer. Measure your own rows, and count with your model’s tokenizer when one is
published, since o200k_base only approximates it.
Related how-tos
Last verified
Verified 2026-11-29 against Node 22.23.5, @modelcontextprotocol/server 2.4.0,
@modelcontextprotocol/client 2.4.0, zod 4.6.7, and js-tiktoken 1.0.21. Every output block
is what the command preceding it printed. No model was run: the agent in measure.mjs and
pitfall.mjs is a loop with a fixed policy. Token counts come from the o200k_base encoding
in js-tiktoken, so they approximate a result’s size in model tokens and differ from what
another tokenizer counts.
Footnotes
-
The pagination page of revision 2025-11-25 says MCP pages by opaque cursor “instead of numbered pages” in its pagination model. Its example
nextCursordecodes from base64 to{"page": 3}, and the request that follows sends back{"page": 2}. The same page in revision 2026-07-28 sends back the page 3 cursor it was given, so that edition’s example agrees with itself. ↩︎ Back to text -
Revision 2025-11-25 asks a server to answer a missing resource with
-32002. The resources page of revision 2026-07-28 moves the code to-32602and asks clients to accept the earlier one too. The SDK’sResourceNotFoundErrorsends-32602, on a connection that agreed on 2025-11-25. ↩︎ Back to text