-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add paginated list decorators for prompts, resources, and tools #1286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,274
−19
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
caeda42
feat: add paginated list decorators for prompts, resources, and tools
maxisbey 62cfab1
style: apply ruff formatting to pass pre-commit checks
maxisbey b7e6a0c
feat: add pagination examples and documentation
maxisbey 203b3ad
switch pagination to single decorator with callback inspection
maxisbey a41f972
chore: clean up inspection code to remove redundant param inspection
maxisbey dd07224
feat: change to passing requests instead of cursors for pagination
maxisbey a38351f
fix: ruff error on unit test
maxisbey a42e097
chore: rename and clarify function inspection code
maxisbey 2a592d2
feature: add type checking for passing request object
maxisbey cbb0e37
feat: change to request injection on type rather than positional
maxisbey f212f8f
fix: remove deprecation code
maxisbey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# MCP Simple Pagination | ||
|
||
A simple MCP server demonstrating pagination for tools, resources, and prompts using cursor-based pagination. | ||
|
||
## Usage | ||
|
||
Start the server using either stdio (default) or SSE transport: | ||
|
||
```bash | ||
# Using stdio transport (default) | ||
uv run mcp-simple-pagination | ||
|
||
# Using SSE transport on custom port | ||
uv run mcp-simple-pagination --transport sse --port 8000 | ||
``` | ||
|
||
The server exposes: | ||
|
||
- 25 tools (paginated, 5 per page) | ||
- 30 resources (paginated, 10 per page) | ||
- 20 prompts (paginated, 7 per page) | ||
|
||
Each paginated list returns a `nextCursor` when more pages are available. Use this cursor in subsequent requests to retrieve the next page. | ||
|
||
## Example | ||
|
||
Using the MCP client, you can retrieve paginated items like this using the STDIO transport: | ||
|
||
```python | ||
import asyncio | ||
from mcp.client.session import ClientSession | ||
from mcp.client.stdio import StdioServerParameters, stdio_client | ||
|
||
|
||
async def main(): | ||
async with stdio_client( | ||
StdioServerParameters(command="uv", args=["run", "mcp-simple-pagination"]) | ||
) as (read, write): | ||
async with ClientSession(read, write) as session: | ||
await session.initialize() | ||
|
||
# Get first page of tools | ||
tools_page1 = await session.list_tools() | ||
print(f"First page: {len(tools_page1.tools)} tools") | ||
print(f"Next cursor: {tools_page1.nextCursor}") | ||
|
||
# Get second page using cursor | ||
if tools_page1.nextCursor: | ||
tools_page2 = await session.list_tools(cursor=tools_page1.nextCursor) | ||
print(f"Second page: {len(tools_page2.tools)} tools") | ||
|
||
# Similarly for resources | ||
resources_page1 = await session.list_resources() | ||
print(f"First page: {len(resources_page1.resources)} resources") | ||
|
||
# And for prompts | ||
prompts_page1 = await session.list_prompts() | ||
print(f"First page: {len(prompts_page1.prompts)} prompts") | ||
|
||
|
||
asyncio.run(main()) | ||
``` | ||
|
||
## Pagination Details | ||
|
||
The server uses simple numeric indices as cursors for demonstration purposes. In production scenarios, you might use: | ||
|
||
- Database offsets or row IDs | ||
- Timestamps for time-based pagination | ||
- Opaque tokens encoding pagination state | ||
|
||
The pagination implementation demonstrates: | ||
|
||
- Handling `None` cursor for the first page | ||
- Returning `nextCursor` when more data exists | ||
- Gracefully handling invalid cursors | ||
- Different page sizes for different resource types |
Empty file.
5 changes: 5 additions & 0 deletions
5
examples/servers/simple-pagination/mcp_simple_pagination/__main__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import sys | ||
|
||
from .server import main | ||
|
||
sys.exit(main()) # type: ignore[call-arg] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.