-
Notifications
You must be signed in to change notification settings - Fork 488
mcp: harden paginate against infinite loops and param mutation #1110
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
Open
dongjiang1989
wants to merge
2
commits into
modelcontextprotocol:main
Choose a base branch
from
dongjiang1989:update-paginate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,8 +204,19 @@ type ClientOptions struct { | |
| // reset" guidance, letting a transient miss pass without tearing down an | ||
| // otherwise live session. Has no effect unless KeepAlive is non-zero. | ||
| KeepAliveFailureThreshold int | ||
| // ListMaxPages is the maximum number of pages to fetch during automatic | ||
| // pagination of list operations (Tools, Resources, ResourceTemplates, | ||
| // Prompts). A value of 0 uses the default of [DefaultListMaxPages] (64). | ||
| // A negative value means unlimited. A positive value caps the number of | ||
| // pages. This prevents runaway pagination loops caused by server-side | ||
| // cursor cycles. | ||
| ListMaxPages int | ||
| } | ||
|
|
||
| // DefaultListMaxPages is the default value for [ClientOptions.ListMaxPages], | ||
| // matching the TypeScript SDK's DEFAULT_LIST_MAX_PAGES. | ||
| const DefaultListMaxPages = 64 | ||
|
|
||
| // toolContextKeyType is the context key type for passing tool definitions | ||
| // from CallTool to the transport layer. | ||
| type toolContextKeyType struct{} | ||
|
|
@@ -1550,7 +1561,7 @@ func (cs *ClientSession) Tools(ctx context.Context, params *ListToolsParams) ite | |
| if params == nil { | ||
| params = &ListToolsParams{} | ||
| } | ||
| return paginate(ctx, params, cs.ListTools, func(res *ListToolsResult) []*Tool { | ||
| return paginate(ctx, params, cs.client.opts.ListMaxPages, cs.ListTools, func(res *ListToolsResult) []*Tool { | ||
| return res.Tools | ||
| }) | ||
| } | ||
|
|
@@ -1563,7 +1574,7 @@ func (cs *ClientSession) Resources(ctx context.Context, params *ListResourcesPar | |
| if params == nil { | ||
| params = &ListResourcesParams{} | ||
| } | ||
| return paginate(ctx, params, cs.ListResources, func(res *ListResourcesResult) []*Resource { | ||
| return paginate(ctx, params, cs.client.opts.ListMaxPages, cs.ListResources, func(res *ListResourcesResult) []*Resource { | ||
| return res.Resources | ||
| }) | ||
| } | ||
|
|
@@ -1576,7 +1587,7 @@ func (cs *ClientSession) ResourceTemplates(ctx context.Context, params *ListReso | |
| if params == nil { | ||
| params = &ListResourceTemplatesParams{} | ||
| } | ||
| return paginate(ctx, params, cs.ListResourceTemplates, func(res *ListResourceTemplatesResult) []*ResourceTemplate { | ||
| return paginate(ctx, params, cs.client.opts.ListMaxPages, cs.ListResourceTemplates, func(res *ListResourceTemplatesResult) []*ResourceTemplate { | ||
| return res.ResourceTemplates | ||
| }) | ||
| } | ||
|
|
@@ -1589,16 +1600,38 @@ func (cs *ClientSession) Prompts(ctx context.Context, params *ListPromptsParams) | |
| if params == nil { | ||
| params = &ListPromptsParams{} | ||
| } | ||
| return paginate(ctx, params, cs.ListPrompts, func(res *ListPromptsResult) []*Prompt { | ||
| return paginate(ctx, params, cs.client.opts.ListMaxPages, cs.ListPrompts, func(res *ListPromptsResult) []*Prompt { | ||
| return res.Prompts | ||
| }) | ||
| } | ||
|
|
||
| // paginate is a generic helper function to provide a paginated iterator. | ||
| func paginate[P listParams, R listResult[T], T any](ctx context.Context, params P, listFunc func(context.Context, P) (R, error), items func(R) []*T) iter.Seq2[*T, error] { | ||
| // | ||
| // It fetches pages by calling listFunc until the result has no NextCursor, | ||
| // maxPages is exceeded (if non-zero), or a cursor cycle is detected. | ||
| // The caller's params struct is not mutated; a local copy is used instead. | ||
| func paginate[P listParams, R listResult[T], T any](ctx context.Context, params P, maxPages int, listFunc func(context.Context, P) (R, error), items func(R) []*T) iter.Seq2[*T, error] { | ||
| return func(yield func(*T, error) bool) { | ||
| // Copy the underlying struct so we don't mutate the caller's params. | ||
| // P is always a pointer to a struct (e.g. *ListToolsParams). | ||
| // We use reflect to create a shallow copy of the pointed-to struct. | ||
| localParams := params | ||
| if v := reflect.ValueOf(params); v.Kind() == reflect.Pointer { | ||
| cp := reflect.New(v.Type().Elem()) | ||
| cp.Elem().Set(v.Elem()) | ||
| localParams = cp.Interface().(P) | ||
| } | ||
|
Comment on lines
+1618
to
+1623
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could be simplified to sth like |
||
| var seen map[string]bool | ||
| pages := 0 | ||
| // 0 means use default (64); negative means unlimited. | ||
| effectiveMax := maxPages | ||
| if effectiveMax == 0 { | ||
| effectiveMax = DefaultListMaxPages | ||
| } | ||
|
|
||
| for { | ||
| res, err := listFunc(ctx, params) | ||
| pages++ | ||
| res, err := listFunc(ctx, localParams) | ||
| if err != nil { | ||
| yield(nil, err) | ||
| return | ||
|
|
@@ -1612,7 +1645,21 @@ func paginate[P listParams, R listResult[T], T any](ctx context.Context, params | |
| if nextCursorVal == nil || *nextCursorVal == "" { | ||
| return | ||
| } | ||
| *params.cursorPtr() = *nextCursorVal | ||
| // Check max pages limit. | ||
| if effectiveMax > 0 && pages >= effectiveMax { | ||
| yield(nil, fmt.Errorf("mcp: pagination exceeded maximum page limit of %d", effectiveMax)) | ||
| return | ||
| } | ||
| // Detect cursor cycles to prevent infinite loops. | ||
| if seen == nil { | ||
| seen = make(map[string]bool) | ||
| } | ||
| if seen[*nextCursorVal] { | ||
| yield(nil, fmt.Errorf("mcp: pagination detected cursor cycle: %q", *nextCursorVal)) | ||
| return | ||
| } | ||
| seen[*nextCursorVal] = true | ||
| *localParams.cursorPtr() = *nextCursorVal | ||
| } | ||
| } | ||
| } | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a behavioral change for existing users