|
57 | 57 | - [Advanced Usage](#advanced-usage) |
58 | 58 | - [Low-Level Server](#low-level-server) |
59 | 59 | - [Structured Output Support](#structured-output-support) |
| 60 | + - [Pagination (Advanced)](#pagination-advanced) |
60 | 61 | - [Writing MCP Clients](#writing-mcp-clients) |
61 | 62 | - [Client Display Utilities](#client-display-utilities) |
62 | 63 | - [OAuth Authentication for Clients](#oauth-authentication-for-clients) |
@@ -1774,6 +1775,116 @@ Tools can return data in three ways: |
1774 | 1775 |
|
1775 | 1776 | When an `outputSchema` is defined, the server automatically validates the structured output against the schema. This ensures type safety and helps catch errors early. |
1776 | 1777 |
|
| 1778 | +### Pagination (Advanced) |
| 1779 | + |
| 1780 | +For servers that need to handle large datasets, the low-level server provides paginated versions of list operations. This is an optional optimization - most servers won't need pagination unless they're dealing with hundreds or thousands of items. |
| 1781 | + |
| 1782 | +#### Server-side Implementation |
| 1783 | + |
| 1784 | +<!-- snippet-source examples/snippets/servers/pagination_example.py --> |
| 1785 | +```python |
| 1786 | +""" |
| 1787 | +Example of implementing pagination with MCP server decorators. |
| 1788 | +""" |
| 1789 | + |
| 1790 | +from pydantic import AnyUrl |
| 1791 | + |
| 1792 | +import mcp.types as types |
| 1793 | +from mcp.server.lowlevel import Server |
| 1794 | + |
| 1795 | +# Initialize the server |
| 1796 | +server = Server("paginated-server") |
| 1797 | + |
| 1798 | +# Sample data to paginate |
| 1799 | +ITEMS = [f"Item {i}" for i in range(1, 101)] # 100 items |
| 1800 | + |
| 1801 | + |
| 1802 | +@server.list_resources() |
| 1803 | +async def list_resources_paginated(request: types.ListResourcesRequest) -> types.ListResourcesResult: |
| 1804 | + """List resources with pagination support.""" |
| 1805 | + page_size = 10 |
| 1806 | + |
| 1807 | + # Extract cursor from request params |
| 1808 | + cursor = request.params.cursor if request.params is not None else None |
| 1809 | + |
| 1810 | + # Parse cursor to get offset |
| 1811 | + start = 0 if cursor is None else int(cursor) |
| 1812 | + end = start + page_size |
| 1813 | + |
| 1814 | + # Get page of resources |
| 1815 | + page_items = [ |
| 1816 | + types.Resource(uri=AnyUrl(f"resource://items/{item}"), name=item, description=f"Description for {item}") |
| 1817 | + for item in ITEMS[start:end] |
| 1818 | + ] |
| 1819 | + |
| 1820 | + # Determine next cursor |
| 1821 | + next_cursor = str(end) if end < len(ITEMS) else None |
| 1822 | + |
| 1823 | + return types.ListResourcesResult(resources=page_items, nextCursor=next_cursor) |
| 1824 | +``` |
| 1825 | + |
| 1826 | +_Full example: [examples/snippets/servers/pagination_example.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/pagination_example.py)_ |
| 1827 | +<!-- /snippet-source --> |
| 1828 | + |
| 1829 | +#### Client-side Consumption |
| 1830 | + |
| 1831 | +<!-- snippet-source examples/snippets/clients/pagination_client.py --> |
| 1832 | +```python |
| 1833 | +""" |
| 1834 | +Example of consuming paginated MCP endpoints from a client. |
| 1835 | +""" |
| 1836 | + |
| 1837 | +import asyncio |
| 1838 | + |
| 1839 | +from mcp.client.session import ClientSession |
| 1840 | +from mcp.client.stdio import StdioServerParameters, stdio_client |
| 1841 | +from mcp.types import Resource |
| 1842 | + |
| 1843 | + |
| 1844 | +async def list_all_resources() -> None: |
| 1845 | + """Fetch all resources using pagination.""" |
| 1846 | + async with stdio_client(StdioServerParameters(command="uv", args=["run", "mcp-simple-pagination"])) as ( |
| 1847 | + read, |
| 1848 | + write, |
| 1849 | + ): |
| 1850 | + async with ClientSession(read, write) as session: |
| 1851 | + await session.initialize() |
| 1852 | + |
| 1853 | + all_resources: list[Resource] = [] |
| 1854 | + cursor = None |
| 1855 | + |
| 1856 | + while True: |
| 1857 | + # Fetch a page of resources |
| 1858 | + result = await session.list_resources(cursor=cursor) |
| 1859 | + all_resources.extend(result.resources) |
| 1860 | + |
| 1861 | + print(f"Fetched {len(result.resources)} resources") |
| 1862 | + |
| 1863 | + # Check if there are more pages |
| 1864 | + if result.nextCursor: |
| 1865 | + cursor = result.nextCursor |
| 1866 | + else: |
| 1867 | + break |
| 1868 | + |
| 1869 | + print(f"Total resources: {len(all_resources)}") |
| 1870 | + |
| 1871 | + |
| 1872 | +if __name__ == "__main__": |
| 1873 | + asyncio.run(list_all_resources()) |
| 1874 | +``` |
| 1875 | + |
| 1876 | +_Full example: [examples/snippets/clients/pagination_client.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/clients/pagination_client.py)_ |
| 1877 | +<!-- /snippet-source --> |
| 1878 | + |
| 1879 | +#### Key Points |
| 1880 | + |
| 1881 | +- **Cursors are opaque strings** - the server defines the format (numeric offsets, timestamps, etc.) |
| 1882 | +- **Return `nextCursor=None`** when there are no more pages |
| 1883 | +- **Backward compatible** - clients that don't support pagination will still work (they'll just get the first page) |
| 1884 | +- **Flexible page sizes** - Each endpoint can define its own page size based on data characteristics |
| 1885 | + |
| 1886 | +See the [simple-pagination example](examples/servers/simple-pagination) for a complete implementation. |
| 1887 | + |
1777 | 1888 | ### Writing MCP Clients |
1778 | 1889 |
|
1779 | 1890 | The SDK provides a high-level client interface for connecting to MCP servers using various [transports](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports): |
|
0 commit comments