MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / paginate

Function paginate

mcp/client.go:1163–1183  ·  view source on GitHub ↗

paginate is a generic helper function to provide a paginated iterator.

(ctx context.Context, params P, listFunc func(context.Context, P) (R, error), items func(R) []*T)

Source from the content-addressed store, hash-verified

1161
1162// paginate is a generic helper function to provide a paginated iterator.
1163func 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] {
1164 return func(yield func(*T, error) bool) {
1165 for {
1166 res, err := listFunc(ctx, params)
1167 if err != nil {
1168 yield(nil, err)
1169 return
1170 }
1171 for _, r := range items(res) {
1172 if !yield(r, nil) {
1173 return
1174 }
1175 }
1176 nextCursorVal := res.nextCursorPtr()
1177 if nextCursorVal == nil || *nextCursorVal == "" {
1178 return
1179 }
1180 *params.cursorPtr() = *nextCursorVal
1181 }
1182 }
1183}

Callers 6

TestClientPaginateBasicFunction · 0.85
ToolsMethod · 0.85
ResourcesMethod · 0.85
ResourceTemplatesMethod · 0.85
PromptsMethod · 0.85

Calls 2

nextCursorPtrMethod · 0.65
cursorPtrMethod · 0.65

Tested by 2

TestClientPaginateBasicFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…