MCPcopy Index your code
hub / github.com/anomalyco/opencode / paginate

Function paginate

packages/opencode/src/mcp/catalog.ts:18–36  ·  view source on GitHub ↗
(
  list: (cursor?: string) => Promise<R>,
  items: (result: R) => T[],
)

Source from the content-addressed store, hash-verified

16})
17
18export async function paginate<T, R extends { nextCursor?: string }>(
19 list: (cursor?: string) => Promise<R>,
20 items: (result: R) => T[],
21) {
22 const result: T[] = []
23 const cursors = new Set<string>()
24 let cursor: string | undefined
25
26 for (let page = 0; page < MAX_LIST_PAGES; page++) {
27 const page = await list(cursor)
28 result.push(...items(page))
29 if (page.nextCursor === undefined) return result
30 if (cursors.has(page.nextCursor)) throw new Error(`MCP list returned duplicate cursor: ${page.nextCursor}`)
31 cursors.add(page.nextCursor)
32 cursor = page.nextCursor
33 }
34
35 throw new Error(`MCP list exceeded ${MAX_LIST_PAGES} pages`)
36}
37
38export function defs(client: Client, timeout?: number) {
39 return listTools(client, timeout ?? DEFAULT_TIMEOUT).pipe(Effect.catch(() => Effect.void))

Callers 4

promptsFunction · 0.85
resourcesFunction · 0.85
resourceTemplatesFunction · 0.85
listToolsFunction · 0.85

Calls 4

pushMethod · 0.80
addMethod · 0.65
listFunction · 0.50
itemsFunction · 0.50

Tested by

no test coverage detected