|
47 | 47 | - [Mounting to an Existing ASGI Server](#mounting-to-an-existing-asgi-server)
|
48 | 48 | - [Advanced Usage](#advanced-usage)
|
49 | 49 | - [Low-Level Server](#low-level-server)
|
| 50 | + - [Pagination (Advanced)](#pagination-advanced) |
50 | 51 | - [Writing MCP Clients](#writing-mcp-clients)
|
51 | 52 | - [Client Display Utilities](#client-display-utilities)
|
52 | 53 | - [OAuth Authentication for Clients](#oauth-authentication-for-clients)
|
@@ -1727,6 +1728,125 @@ Tools can return data in three ways:
|
1727 | 1728 |
|
1728 | 1729 | When an `outputSchema` is defined, the server automatically validates the structured output against the schema. This ensures type safety and helps catch errors early.
|
1729 | 1730 |
|
| 1731 | +### Pagination (Advanced) |
| 1732 | + |
| 1733 | +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. |
| 1734 | + |
| 1735 | +#### Server-side Implementation |
| 1736 | + |
| 1737 | +<!-- snippet-source examples/snippets/servers/pagination_example.py --> |
| 1738 | +```python |
| 1739 | +""" |
| 1740 | +Example of implementing pagination with MCP server decorators. |
| 1741 | +""" |
| 1742 | + |
| 1743 | +import mcp.types as types |
| 1744 | +from mcp.server.lowlevel import Server |
| 1745 | +from pydantic import AnyUrl |
| 1746 | + |
| 1747 | +# Initialize the server |
| 1748 | +server = Server("paginated-server") |
| 1749 | + |
| 1750 | +# Sample data to paginate |
| 1751 | +ITEMS = [f"Item {i}" for i in range(1, 101)] # 100 items |
| 1752 | + |
| 1753 | + |
| 1754 | +@server.list_resources_paginated() |
| 1755 | +async def list_resources_paginated(cursor: types.Cursor | None) -> types.ListResourcesResult: |
| 1756 | + """List resources with pagination support.""" |
| 1757 | + page_size = 10 |
| 1758 | + |
| 1759 | + # Parse cursor to get offset |
| 1760 | + start = 0 if cursor is None else int(cursor) |
| 1761 | + end = start + page_size |
| 1762 | + |
| 1763 | + # Get page of resources |
| 1764 | + page_items = [ |
| 1765 | + types.Resource( |
| 1766 | + uri=AnyUrl(f"resource://items/{item}"), |
| 1767 | + name=item, |
| 1768 | + description=f"Description for {item}" |
| 1769 | + ) |
| 1770 | + for item in ITEMS[start:end] |
| 1771 | + ] |
| 1772 | + |
| 1773 | + # Determine next cursor |
| 1774 | + next_cursor = str(end) if end < len(ITEMS) else None |
| 1775 | + |
| 1776 | + return types.ListResourcesResult( |
| 1777 | + resources=page_items, |
| 1778 | + nextCursor=next_cursor |
| 1779 | + ) |
| 1780 | +``` |
| 1781 | + |
| 1782 | +_Full example: [examples/snippets/servers/pagination_example.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/pagination_example.py)_ |
| 1783 | +<!-- /snippet-source --> |
| 1784 | + |
| 1785 | +Similar decorators are available for all list operations: |
| 1786 | + |
| 1787 | +- `@server.list_tools_paginated()` - for paginating tools |
| 1788 | +- `@server.list_resources_paginated()` - for paginating resources |
| 1789 | +- `@server.list_prompts_paginated()` - for paginating prompts |
| 1790 | + |
| 1791 | +#### Client-side Consumption |
| 1792 | + |
| 1793 | +<!-- snippet-source examples/snippets/clients/pagination_client.py --> |
| 1794 | +```python |
| 1795 | +""" |
| 1796 | +Example of consuming paginated MCP endpoints from a client. |
| 1797 | +""" |
| 1798 | + |
| 1799 | +import asyncio |
| 1800 | +from mcp.client.session import ClientSession |
| 1801 | +from mcp.client.stdio import StdioServerParameters, stdio_client |
| 1802 | + |
| 1803 | + |
| 1804 | +async def list_all_resources(): |
| 1805 | + """Fetch all resources using pagination.""" |
| 1806 | + async with stdio_client( |
| 1807 | + StdioServerParameters(command="uv", args=["run", "mcp-simple-pagination"]) |
| 1808 | + ) as (read, write): |
| 1809 | + async with ClientSession(read, write) as session: |
| 1810 | + await session.initialize() |
| 1811 | + |
| 1812 | + all_resources = [] |
| 1813 | + cursor = None |
| 1814 | + |
| 1815 | + while True: |
| 1816 | + # Fetch a page of resources |
| 1817 | + result = await session.list_resources(cursor=cursor) |
| 1818 | + all_resources.extend(result.resources) |
| 1819 | + |
| 1820 | + print(f"Fetched {len(result.resources)} resources") |
| 1821 | + |
| 1822 | + # Check if there are more pages |
| 1823 | + if result.nextCursor: |
| 1824 | + cursor = result.nextCursor |
| 1825 | + else: |
| 1826 | + break |
| 1827 | + |
| 1828 | + print(f"Total resources: {len(all_resources)}") |
| 1829 | + return all_resources |
| 1830 | + |
| 1831 | + |
| 1832 | +if __name__ == "__main__": |
| 1833 | + asyncio.run(list_all_resources()) |
| 1834 | +``` |
| 1835 | + |
| 1836 | +_Full example: [examples/snippets/clients/pagination_client.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/clients/pagination_client.py)_ |
| 1837 | +<!-- /snippet-source --> |
| 1838 | + |
| 1839 | +#### Key Points |
| 1840 | + |
| 1841 | +- **Cursors are opaque strings** - the server defines the format (numeric offsets, timestamps, etc.) |
| 1842 | +- **Return `nextCursor=None`** when there are no more pages |
| 1843 | +- **Backward compatible** - clients that don't support pagination will still work (they'll just get the first page) |
| 1844 | +- **Flexible page sizes** - Each endpoint can define its own page size based on data characteristics |
| 1845 | + |
| 1846 | +> **NOTE**: The paginated decorators (`list_tools_paginated()`, `list_resources_paginated()`, `list_prompts_paginated()`) are mutually exclusive with their non-paginated counterparts and cannot be used together on the same server instance. |
| 1847 | +
|
| 1848 | +See the [simple-pagination example](examples/servers/simple-pagination) for a complete implementation. |
| 1849 | + |
1730 | 1850 | ### Writing MCP Clients
|
1731 | 1851 |
|
1732 | 1852 | 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