Bound no-arg list operations to prevent unbounded pagination - #1100
Open
ZYZ666-RGB wants to merge 1 commit into
Open
Bound no-arg list operations to prevent unbounded pagination#1100ZYZ666-RGB wants to merge 1 commit into
ZYZ666-RGB wants to merge 1 commit into
Conversation
The no-arg listTools(), listResources(), listResourceTemplates() and listPrompts() follow the server-provided nextCursor chain via Mono.expand with no page limit, no duplicate-cursor detection and no total deadline. A server that returns an endless stream of non-empty cursors makes the client issue an unbounded number of requests, accumulate unbounded memory, and block synchronous callers forever. Add a client-side pagination guard: each no-arg list operation now tracks the cursors it has already seen and the number of pages fetched, and aborts with a new McpPaginationException when the configured maxPaginationPages (default 100) or paginationTimeout (disabled by default) is exceeded, or when a cursor is returned more than once. The bounds are configurable on both McpClient.SyncSpec and McpClient.AsyncSpec (maxPaginationPages(int), paginationTimeout(Duration)); existing behavior and APIs are unchanged for servers that terminate pagination normally. Adds McpAsyncClientPaginationTests covering normal multi-page aggregation, duplicate-cursor loops, ever-changing cursor loops (page limit and total timeout) and the synchronous client path. Fixes modelcontextprotocol#1084 Signed-off-by: zhaoyuzhe <1991039819@qq.com>
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.
Motivation and Context
The no-arg list operations (
listTools(),listResources(),listResourceTemplates(),listPrompts()) follow the server-providednextCursorchain viaMono.expandwithno page limit, no duplicate-cursor detection and no total deadline. A server that
returns an endless stream of non-empty cursors makes the client:
McpSyncClient.listTools()has no duration).requestTimeoutdoes not help: it bounds each individual request, not the number ofrequests. The existing #954 fix only stops servers that signal the end with an empty
cursor. Fixes #1084.
Changes
Add a client-side pagination guard applied to all four no-arg list operations:
maxPaginationPages(builder-configurable, default 100)caps the total number of pages fetched.
0disables the limit.the operation aborts immediately — an unambiguous sign of a loop.
paginationTimeout(builder-configurable, disabled by default)bounds the wall-clock time of the whole list operation.
When a bound is exceeded the operation fails with a new
McpPaginationExceptioncarrying a clear message. Configuration is exposed on bothMcpClient.SyncSpecandMcpClient.AsyncSpec(maxPaginationPages(int),paginationTimeout(Duration)). No existing API or behavior changes for servers thatterminate pagination normally — the guard state is created per subscription, so shared
Monos can be subscribed multiple times safely.How Has This Been Tested?
New
McpAsyncClientPaginationTests(6 tests):nullcursor (tools + resources),McpPaginationException,McpPaginationExceptionaftermaxPaginationPagespages,McpPaginationExceptionafterpaginationTimeout,McpSyncClient.listTools()) propagates the same exception.Verified locally:
mvn -pl mcp-core test(360 tests) and the mcp-test client testclasses (17 tests) all pass, with
spring-javaformatvalidation green.Breaking Changes
None. The new builder options are additive; defaults keep the API source-compatible and
only add protection against misbehaving servers.
Types of changes
Checklist
Additional context
Design note:
Mono.expandruns per subscription, so a shared listMonosubscribedtwice would otherwise share stale guard state; the guard is therefore created inside
Mono.deferper subscription.