Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667---title: paginationsidebarTitle: pagination---
# `fastmcp.utilities.pagination`
Pagination utilities for MCP list operations.
## Functions
### `paginate_sequence` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/pagination.py#L50" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonpaginate_sequence(items: Sequence[T], cursor: str | None, page_size: int) -> tuple[list[T], str | None]```
Paginate a sequence of items.
**Args:**- `items`: The full sequence to paginate.- `cursor`: Optional cursor from a previous request. None for first page.- `page_size`: Maximum number of items per page.
**Returns:**- Tuple of (page_items, next_cursor). next_cursor is None if no more pages.
**Raises:**- `ValueError`: If the cursor is invalid.
## Classes
### `CursorState` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/pagination.py#L16" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Internal representation of pagination cursor state.
The cursor encodes the offset into the result set. This is opaque to clientsper the MCP spec - they should not parse or modify cursors.
**Methods:**
#### `encode` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/pagination.py#L25" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonencode(self) -> str```
Encode cursor state to an opaque string.
#### `decode` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/pagination.py#L31" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythondecode(cls, cursor: str) -> CursorState```
Decode cursor from an opaque string.
**Raises:**- `ValueError`: If the cursor is invalid or malformed.