You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Supports cursor-based pagination for list operations
43
44
44
45
### Supported Methods
45
46
@@ -1365,6 +1366,90 @@ When configured, sessions that receive no HTTP requests for this duration are au
1365
1366
transport =MCP::Server::Transports::StreamableHTTPTransport.new(server, session_idle_timeout:1800)
1366
1367
```
1367
1368
1369
+
### Pagination
1370
+
1371
+
The MCP Ruby SDK supports [pagination](https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/pagination)
1372
+
for list operations that may return large result sets. Pagination uses string cursor tokens carrying a zero-based offset,
1373
+
treated as opaque by clients: the server decides page size, and the client follows `nextCursor` until the server omits it.
1374
+
1375
+
Pagination applies to `tools/list`, `prompts/list`, `resources/list`, and `resources/templates/list`.
1376
+
1377
+
#### Server-Side: Enabling Pagination
1378
+
1379
+
Pass `page_size:` to `MCP::Server.new` to split list responses into pages. When `page_size` is omitted (the default),
1380
+
list responses contain all items in a single response, preserving the pre-pagination behavior.
1381
+
1382
+
```ruby
1383
+
server =MCP::Server.new(
1384
+
name:"my_server",
1385
+
tools: tools,
1386
+
page_size:50,
1387
+
)
1388
+
```
1389
+
1390
+
When `page_size` is set, list responses include a `nextCursor` field whenever more pages are available:
1391
+
1392
+
```json
1393
+
{
1394
+
"jsonrpc": "2.0",
1395
+
"id": 1,
1396
+
"result": {
1397
+
"tools": [
1398
+
{ "name": "example_tool" }
1399
+
],
1400
+
"nextCursor": "50"
1401
+
}
1402
+
}
1403
+
```
1404
+
1405
+
Invalid cursors (e.g. non-numeric, negative, or out-of-range) are rejected with JSON-RPC error code `-32602 (Invalid params)` per the MCP specification.
1406
+
1407
+
#### Client-Side: Iterating Pages
1408
+
1409
+
`MCP::Client` exposes `list_tools`, `list_prompts`, `list_resources`, and `list_resource_templates`.
1410
+
**Each call issues exactly one `*/list` JSON-RPC request and returns exactly one page** — not the full collection.
1411
+
The returned result object (`MCP::Client::ListToolsResult` etc.) exposes the page items and the next cursor as method accessors:
1412
+
1413
+
```ruby
1414
+
client =MCP::Client.new(transport: transport)
1415
+
1416
+
cursor =nil
1417
+
loopdo
1418
+
page = client.list_tools(cursor: cursor)
1419
+
page.tools.each { |tool| process(tool) }
1420
+
cursor = page.next_cursor
1421
+
breakunless cursor
1422
+
end
1423
+
```
1424
+
1425
+
The same pattern applies to `list_prompts` (`page.prompts`), `list_resources` (`page.resources`), and
1426
+
`list_resource_templates` (`page.resource_templates`). `next_cursor` is `nil` on the final page.
1427
+
1428
+
Because a single call returns a single page, how many items come back depends on the server's `page_size` configuration:
1429
+
1430
+
| Server `page_size`|`client.list_tools(cursor: nil)`|
0 commit comments