From 234dfbc1c270804f704b398603d2186c9fd7b07d Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov Date: Wed, 1 Oct 2025 16:45:30 +0200 Subject: [PATCH] refactor: extract the pagination logic --- src/McpResponse.ts | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/McpResponse.ts b/src/McpResponse.ts index 77ab5e5a..fa3c69ae 100644 --- a/src/McpResponse.ts +++ b/src/McpResponse.ts @@ -210,30 +210,12 @@ Call ${handleDialog.name} to handle it before continuing.`); response.push('## Network requests'); if (requests.length) { - const paginationResult = paginate( + const data = this.#dataWithPagination( requests, this.#networkRequestsOptions.pagination, ); - if (paginationResult.invalidPage) { - response.push('Invalid page number provided. Showing first page.'); - } - - const {startIndex, endIndex, currentPage, totalPages} = - paginationResult; - response.push( - `Showing ${startIndex + 1}-${endIndex} of ${requests.length} (Page ${currentPage + 1} of ${totalPages}).`, - ); - - if (this.#networkRequestsOptions.pagination) { - if (paginationResult.hasNextPage) { - response.push(`Next page: ${currentPage + 1}`); - } - if (paginationResult.hasPreviousPage) { - response.push(`Previous page: ${currentPage - 1}`); - } - } - - for (const request of paginationResult.items) { + response.push(...data.info); + for (const request of data.items) { response.push(getShortDescriptionForRequest(request)); } } else { @@ -264,6 +246,32 @@ Call ${handleDialog.name} to handle it before continuing.`); return [text, ...images]; } + #dataWithPagination(data: T[], pagination?: PaginationOptions) { + const response = []; + const paginationResult = paginate(data, pagination); + if (paginationResult.invalidPage) { + response.push('Invalid page number provided. Showing first page.'); + } + + const {startIndex, endIndex, currentPage, totalPages} = paginationResult; + response.push( + `Showing ${startIndex + 1}-${endIndex} of ${data.length} (Page ${currentPage + 1} of ${totalPages}).`, + ); + if (pagination) { + if (paginationResult.hasNextPage) { + response.push(`Next page: ${currentPage + 1}`); + } + if (paginationResult.hasPreviousPage) { + response.push(`Previous page: ${currentPage - 1}`); + } + } + + return { + info: response, + items: paginationResult.items, + }; + } + #getIncludeNetworkRequestsData(context: McpContext): string[] { const response: string[] = []; const url = this.#attachedNetworkRequestUrl;