Add list_all_* helpers that drain pagination on the client#2658
Open
adityasingh2400 wants to merge 1 commit into
Open
Conversation
Currently Client.list_tools / list_prompts / list_resources / list_resource_templates return a single page and the caller has to loop on next_cursor manually. Add list_all_tools / list_all_prompts / list_all_resources / list_all_resource_templates that walk next_cursor until exhausted, plus iter_all_* async iterators for streaming consumers. The single-page methods get a docstring update pointing at the new drains. ClientSessionGroup switches its tool/prompt/resource aggregation to the drain helper so its consumers always see the full collection across multi-page servers. Implements the helper maxisbey endorsed in modelcontextprotocol#2556.
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.
Closes #2556.
Summary
list_all_tools/list_all_prompts/list_all_resources/list_all_resource_templatesonClient. Each walksnext_cursoruntil exhausted and returns the combined list.iter_all_tools/iter_all_prompts/iter_all_resources/iter_all_resource_templatesasync iterators, for streaming consumers that don't want to materialize every page in memory.list_*methods to say so plainly and point at the new drains.ClientSessionGroup._aggregate_componentsto drain pagination so multi-server aggregators don't silently drop pages past the first (this was specifically called out in the issue as the scenario where high tool counts are most likely).Existing tests that mocked the session's
list_tools/list_resources/list_promptsnow setnext_cursor=Noneon the mock return values so the new drain loop terminates after a single page.Why
The single-page
list_*methods are easy to misuse, which is the whole point of the issue:The existing primitives are kept; this just adds a drain helper next to them so downstream client code can get the "give me everything" behavior in one call instead of writing the loop themselves.
Test plan
uv run pytest tests/client/test_list_all_pagination.py tests/client/test_session_group.py tests/client/test_list_methods_cursor.py(25 passed)uv run pytest tests/client/(208 passed, 1 xfailed)uv run pytest tests/(1180 passed, 98 skipped, 1 xfailed)uv run ruff check . && uv run ruff format --check .(clean)uv run pyright src/mcp/ tests/client/test_list_all_pagination.py tests/client/test_session_group.py(0 errors)src/mcp/client/client.pyandsrc/mcp/client/session_group.pyviatests/client/New tests cover:
next_cursorreturned by the previous page.iter_all_toolsyields one tool at a time and pages lazily.ClientSessionGroupaggregates across paginatedlist_toolscalls and supplies the cursor on the follow-up request.Notes
I went with
Clienthelpers only and leftClientSessionas the single-page primitive (matches what the issue suggested as the conservative call). Happy to also add session-level helpers if you'd prefer that.