mcp/client: add list_all_* helpers with pagination safety guards#3156
Open
dongjiang1989 wants to merge 1 commit into
Open
mcp/client: add list_all_* helpers with pagination safety guards#3156dongjiang1989 wants to merge 1 commit into
dongjiang1989 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
dongjiang1989
added a commit
to dongjiang1989/python-sdk
that referenced
this pull request
Jul 24, 2026
- Collapse super().__init__() to single line for ruff format - Add blank line after CursorCycleError class - Simplify cursor cycle test to eliminate uncovered branch - Add pragma: no branch for coverage.py false positive on nested async Fixes CI failures on PR modelcontextprotocol#3156 Signed-off-by: dongjiang <dongjiang1989@126.com>
dongjiang1989
added a commit
to dongjiang1989/python-sdk
that referenced
this pull request
Jul 24, 2026
Replace **kwargs: object with explicit meta and cache_mode parameters matching the list_tools signature to satisfy pyright strict checking. Fixes CI pyright failure on PR modelcontextprotocol#3156 Signed-off-by: dongjiang <dongjiang1989@126.com>
dongjiang1989
added a commit
to dongjiang1989/python-sdk
that referenced
this pull request
Jul 24, 2026
- Use accumulator with extend() instead of list concatenation to avoid quadratic CPU/memory churn on large paginated listings - Absorb final aggregated tools result as complete to prune stale session state (x-mcp-header maps and output schemas) Fixes cubic review comments on PR modelcontextprotocol#3156 Signed-off-by: dongjiang <dongjiang1989@126.com>
dongjiang1989
force-pushed
the
update-paginate-py
branch
from
July 24, 2026 10:57
1d83eb7 to
cfb21e8
Compare
dongjiang1989
force-pushed
the
update-paginate-py
branch
from
July 24, 2026 11:08
cfb21e8 to
7f60551
Compare
Add four auto-pagination helpers (list_all_tools, list_all_resources, list_all_resource_templates, list_all_prompts) that drain all pages into a single aggregated result with safety guards: - Cursor cycle detection via a seen-set; raises CursorCycleError - Configurable page cap via list_max_pages (default 64); raises PaginationExceededError - Use accumulator with extend() to avoid quadratic list copying - Absorb final aggregated tools result as complete to prune stale session state (x-mcp-header maps and output schemas) Aligns with TypeScript SDK's _listAllPages helper (DEFAULT_LIST_MAX_PAGES=64). Signed-off-by: dongjiang <dongjiang1989@126.com>
dongjiang1989
force-pushed
the
update-paginate-py
branch
from
July 24, 2026 11:11
7f60551 to
e401eae
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The Python SDK lacks auto-pagination helpers for the four list verbs (
tools/list,resources/list,resources/templates/list,prompts/list). Callers must write their own cursor-draining loops, and those loops have no protection against infinite pagination or cursor cycles.The TypeScript SDK already provides this via
_listAllPageswith alistMaxPagescap and cursor cycle detection (see typescript-sdk#2336). The Go SDK is getting the same guards in go-sdk#1110. This PR brings the Python SDK to parity.What changed
Four new methods on the high-level
Client:Each drains all pages into a single aggregated result via a shared
_drain_all_pageshelper with two safety guards:Page cap:
Client.list_max_pages(default 64, matching the TSSDK's
DEFAULT_LIST_MAX_PAGES). Exceeding the cap raisesPaginationExceededError. Setting to 0 or a negative valuedisables the cap (unlimited).
Cursor cycle detection: a
seenset tracks all cursorsreturned by the server. A repeated cursor raises
CursorCycleError, preventing infinite loops from buggy servers.Both exception types are exported from
mcp.client.Configuration
How Has This Been Tested
9 new tests in
tests/client/test_list_all_pagination.py:PaginationExceededErrorwhen cap exceededCursorCycleErroron repeated cursorlist_max_pages=0)next_cursor is None)All 709 existing client tests continue to pass. Lint (
ruff check)and type check (
pyright) are clean.Types of changes
Checklist