|
31 | 31 | - [Prompts](#prompts)
|
32 | 32 | - [Images](#images)
|
33 | 33 | - [Context](#context)
|
| 34 | + - [Getting Context in Functions](#getting-context-in-functions) |
| 35 | + - [Context Properties and Methods](#context-properties-and-methods) |
34 | 36 | - [Completions](#completions)
|
35 | 37 | - [Elicitation](#elicitation)
|
36 | 38 | - [Sampling](#sampling)
|
37 | 39 | - [Logging and Notifications](#logging-and-notifications)
|
38 | 40 | - [Authentication](#authentication)
|
39 | 41 | - [FastMCP Properties](#fastmcp-properties)
|
40 |
| - - [Session Properties](#session-properties-and-methods) |
| 42 | + - [Session Properties and Methods](#session-properties-and-methods) |
41 | 43 | - [Request Context Properties](#request-context-properties)
|
42 | 44 | - [Running Your Server](#running-your-server)
|
43 | 45 | - [Development Mode](#development-mode)
|
44 | 46 | - [Claude Desktop Integration](#claude-desktop-integration)
|
45 | 47 | - [Direct Execution](#direct-execution)
|
46 | 48 | - [Streamable HTTP Transport](#streamable-http-transport)
|
| 49 | + - [CORS Configuration for Browser-Based Clients](#cors-configuration-for-browser-based-clients) |
47 | 50 | - [Mounting to an Existing ASGI Server](#mounting-to-an-existing-asgi-server)
|
| 51 | + - [StreamableHTTP servers](#streamablehttp-servers) |
| 52 | + - [Basic mounting](#basic-mounting) |
| 53 | + - [Host-based routing](#host-based-routing) |
| 54 | + - [Multiple servers with path configuration](#multiple-servers-with-path-configuration) |
| 55 | + - [Path configuration at initialization](#path-configuration-at-initialization) |
| 56 | + - [SSE servers](#sse-servers) |
48 | 57 | - [Advanced Usage](#advanced-usage)
|
49 | 58 | - [Low-Level Server](#low-level-server)
|
| 59 | + - [Structured Output Support](#structured-output-support) |
| 60 | + - [Pagination (Advanced)](#pagination-advanced) |
50 | 61 | - [Writing MCP Clients](#writing-mcp-clients)
|
51 | 62 | - [Client Display Utilities](#client-display-utilities)
|
52 | 63 | - [OAuth Authentication for Clients](#oauth-authentication-for-clients)
|
@@ -400,7 +411,7 @@ def get_weather(city: str) -> WeatherData:
|
400 | 411 | """Get weather for a city - returns structured data."""
|
401 | 412 | # Simulated weather data
|
402 | 413 | return WeatherData(
|
403 |
| - temperature=72.5, |
| 414 | + temperature=22.5, |
404 | 415 | humidity=45.0,
|
405 | 416 | condition="sunny",
|
406 | 417 | wind_speed=5.2,
|
@@ -1727,6 +1738,116 @@ Tools can return data in three ways:
|
1727 | 1738 |
|
1728 | 1739 | 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 | 1740 |
|
| 1741 | +### Pagination (Advanced) |
| 1742 | + |
| 1743 | +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. |
| 1744 | + |
| 1745 | +#### Server-side Implementation |
| 1746 | + |
| 1747 | +<!-- snippet-source examples/snippets/servers/pagination_example.py --> |
| 1748 | +```python |
| 1749 | +""" |
| 1750 | +Example of implementing pagination with MCP server decorators. |
| 1751 | +""" |
| 1752 | + |
| 1753 | +from pydantic import AnyUrl |
| 1754 | + |
| 1755 | +import mcp.types as types |
| 1756 | +from mcp.server.lowlevel import Server |
| 1757 | + |
| 1758 | +# Initialize the server |
| 1759 | +server = Server("paginated-server") |
| 1760 | + |
| 1761 | +# Sample data to paginate |
| 1762 | +ITEMS = [f"Item {i}" for i in range(1, 101)] # 100 items |
| 1763 | + |
| 1764 | + |
| 1765 | +@server.list_resources() |
| 1766 | +async def list_resources_paginated(request: types.ListResourcesRequest) -> types.ListResourcesResult: |
| 1767 | + """List resources with pagination support.""" |
| 1768 | + page_size = 10 |
| 1769 | + |
| 1770 | + # Extract cursor from request params |
| 1771 | + cursor = request.params.cursor if request.params is not None else None |
| 1772 | + |
| 1773 | + # Parse cursor to get offset |
| 1774 | + start = 0 if cursor is None else int(cursor) |
| 1775 | + end = start + page_size |
| 1776 | + |
| 1777 | + # Get page of resources |
| 1778 | + page_items = [ |
| 1779 | + types.Resource(uri=AnyUrl(f"resource://items/{item}"), name=item, description=f"Description for {item}") |
| 1780 | + for item in ITEMS[start:end] |
| 1781 | + ] |
| 1782 | + |
| 1783 | + # Determine next cursor |
| 1784 | + next_cursor = str(end) if end < len(ITEMS) else None |
| 1785 | + |
| 1786 | + return types.ListResourcesResult(resources=page_items, nextCursor=next_cursor) |
| 1787 | +``` |
| 1788 | + |
| 1789 | +_Full example: [examples/snippets/servers/pagination_example.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/pagination_example.py)_ |
| 1790 | +<!-- /snippet-source --> |
| 1791 | + |
| 1792 | +#### Client-side Consumption |
| 1793 | + |
| 1794 | +<!-- snippet-source examples/snippets/clients/pagination_client.py --> |
| 1795 | +```python |
| 1796 | +""" |
| 1797 | +Example of consuming paginated MCP endpoints from a client. |
| 1798 | +""" |
| 1799 | + |
| 1800 | +import asyncio |
| 1801 | + |
| 1802 | +from mcp.client.session import ClientSession |
| 1803 | +from mcp.client.stdio import StdioServerParameters, stdio_client |
| 1804 | +from mcp.types import Resource |
| 1805 | + |
| 1806 | + |
| 1807 | +async def list_all_resources() -> None: |
| 1808 | + """Fetch all resources using pagination.""" |
| 1809 | + async with stdio_client(StdioServerParameters(command="uv", args=["run", "mcp-simple-pagination"])) as ( |
| 1810 | + read, |
| 1811 | + write, |
| 1812 | + ): |
| 1813 | + async with ClientSession(read, write) as session: |
| 1814 | + await session.initialize() |
| 1815 | + |
| 1816 | + all_resources: list[Resource] = [] |
| 1817 | + cursor = None |
| 1818 | + |
| 1819 | + while True: |
| 1820 | + # Fetch a page of resources |
| 1821 | + result = await session.list_resources(cursor=cursor) |
| 1822 | + all_resources.extend(result.resources) |
| 1823 | + |
| 1824 | + print(f"Fetched {len(result.resources)} resources") |
| 1825 | + |
| 1826 | + # Check if there are more pages |
| 1827 | + if result.nextCursor: |
| 1828 | + cursor = result.nextCursor |
| 1829 | + else: |
| 1830 | + break |
| 1831 | + |
| 1832 | + print(f"Total resources: {len(all_resources)}") |
| 1833 | + |
| 1834 | + |
| 1835 | +if __name__ == "__main__": |
| 1836 | + asyncio.run(list_all_resources()) |
| 1837 | +``` |
| 1838 | + |
| 1839 | +_Full example: [examples/snippets/clients/pagination_client.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/clients/pagination_client.py)_ |
| 1840 | +<!-- /snippet-source --> |
| 1841 | + |
| 1842 | +#### Key Points |
| 1843 | + |
| 1844 | +- **Cursors are opaque strings** - the server defines the format (numeric offsets, timestamps, etc.) |
| 1845 | +- **Return `nextCursor=None`** when there are no more pages |
| 1846 | +- **Backward compatible** - clients that don't support pagination will still work (they'll just get the first page) |
| 1847 | +- **Flexible page sizes** - Each endpoint can define its own page size based on data characteristics |
| 1848 | + |
| 1849 | +See the [simple-pagination example](examples/servers/simple-pagination) for a complete implementation. |
| 1850 | + |
1730 | 1851 | ### Writing MCP Clients
|
1731 | 1852 |
|
1732 | 1853 | 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):
|
@@ -2137,6 +2258,7 @@ MCP servers declare capabilities during initialization:
|
2137 | 2258 |
|
2138 | 2259 | ## Documentation
|
2139 | 2260 |
|
| 2261 | +- [API Reference](https://modelcontextprotocol.github.io/python-sdk/api/) |
2140 | 2262 | - [Model Context Protocol documentation](https://modelcontextprotocol.io)
|
2141 | 2263 | - [Model Context Protocol specification](https://spec.modelcontextprotocol.io)
|
2142 | 2264 | - [Officially supported servers](https://github.com/modelcontextprotocol/servers)
|
|
0 commit comments