feat: implement comprehensive MCP response pagination system
- Add universal pagination guard with session-isolated cursor management - Implement withPagination() decorator for any tool returning large datasets - Update browser_console_messages with pagination and advanced filtering - Update browser_get_requests with pagination while preserving all filters - Add adaptive chunk sizing for optimal performance (target 500ms responses) - Include query consistency validation to handle parameter changes - Provide smart response size detection with user recommendations - Add automatic cursor cleanup and 24-hour expiration - Create comprehensive documentation and usage examples Resolves: Large MCP response token overflow warnings Benefits: Predictable response sizes, resumable data exploration, universal UX
This commit is contained in:
parent
ab68039f2e
commit
17d99f6ff2
5 changed files with 976 additions and 86 deletions
396
src/pagination.ts
Normal file
396
src/pagination.ts
Normal file
|
|
@ -0,0 +1,396 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
import { randomUUID } from 'crypto';
|
||||
import type { Context } from './context.js';
|
||||
import type { Response } from './response.js';
|
||||
|
||||
export const paginationParamsSchema = z.object({
|
||||
limit: z.number().min(1).max(1000).optional().default(50).describe('Maximum items per page (1-1000)'),
|
||||
cursor_id: z.string().optional().describe('Continue from previous page using cursor ID'),
|
||||
session_id: z.string().optional().describe('Session identifier for cursor isolation'),
|
||||
});
|
||||
|
||||
export type PaginationParams = z.infer<typeof paginationParamsSchema>;
|
||||
|
||||
export interface CursorState {
|
||||
id: string;
|
||||
sessionId: string;
|
||||
toolName: string;
|
||||
queryStateFingerprint: string;
|
||||
position: Record<string, any>;
|
||||
createdAt: Date;
|
||||
expiresAt: Date;
|
||||
lastAccessedAt: Date;
|
||||
resultCount: number;
|
||||
performanceMetrics: {
|
||||
avgFetchTimeMs: number;
|
||||
totalFetches: number;
|
||||
optimalChunkSize: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface QueryState {
|
||||
filters: Record<string, any>;
|
||||
parameters: Record<string, any>;
|
||||
}
|
||||
|
||||
export class QueryStateManager {
|
||||
static fromParams(params: any, excludeKeys: string[] = ['limit', 'cursor_id', 'session_id']): QueryState {
|
||||
const filters: Record<string, any> = {};
|
||||
const parameters: Record<string, any> = {};
|
||||
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
if (excludeKeys.includes(key)) continue;
|
||||
|
||||
if (key.includes('filter') || key.includes('Filter')) {
|
||||
filters[key] = value;
|
||||
} else {
|
||||
parameters[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return { filters, parameters };
|
||||
}
|
||||
|
||||
static fingerprint(queryState: QueryState): string {
|
||||
const combined = { ...queryState.filters, ...queryState.parameters };
|
||||
const sorted = Object.keys(combined)
|
||||
.sort()
|
||||
.reduce((result: Record<string, any>, key) => {
|
||||
result[key] = combined[key];
|
||||
return result;
|
||||
}, {});
|
||||
|
||||
return JSON.stringify(sorted);
|
||||
}
|
||||
}
|
||||
|
||||
export interface PaginatedData<T> {
|
||||
items: T[];
|
||||
totalCount?: number;
|
||||
hasMore: boolean;
|
||||
cursor?: string;
|
||||
metadata: {
|
||||
pageSize: number;
|
||||
fetchTimeMs: number;
|
||||
isFreshQuery: boolean;
|
||||
totalFetched?: number;
|
||||
estimatedTotal?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export class SessionCursorManager {
|
||||
private cursors: Map<string, CursorState> = new Map();
|
||||
private cleanupIntervalId: NodeJS.Timeout | null = null;
|
||||
|
||||
constructor() {
|
||||
this.startCleanupTask();
|
||||
}
|
||||
|
||||
private startCleanupTask() {
|
||||
this.cleanupIntervalId = setInterval(() => {
|
||||
this.cleanupExpiredCursors();
|
||||
}, 5 * 60 * 1000); // Every 5 minutes
|
||||
}
|
||||
|
||||
private cleanupExpiredCursors() {
|
||||
const now = new Date();
|
||||
for (const [cursorId, cursor] of this.cursors.entries()) {
|
||||
if (cursor.expiresAt < now) {
|
||||
this.cursors.delete(cursorId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async createCursor(
|
||||
sessionId: string,
|
||||
toolName: string,
|
||||
queryState: QueryState,
|
||||
initialPosition: Record<string, any>
|
||||
): Promise<string> {
|
||||
const cursorId = randomUUID().substring(0, 12);
|
||||
const now = new Date();
|
||||
|
||||
const cursor: CursorState = {
|
||||
id: cursorId,
|
||||
sessionId,
|
||||
toolName,
|
||||
queryStateFingerprint: QueryStateManager.fingerprint(queryState),
|
||||
position: initialPosition,
|
||||
createdAt: now,
|
||||
expiresAt: new Date(now.getTime() + 24 * 60 * 60 * 1000), // 24 hours
|
||||
lastAccessedAt: now,
|
||||
resultCount: 0,
|
||||
performanceMetrics: {
|
||||
avgFetchTimeMs: 0,
|
||||
totalFetches: 0,
|
||||
optimalChunkSize: 50
|
||||
}
|
||||
};
|
||||
|
||||
this.cursors.set(cursorId, cursor);
|
||||
return cursorId;
|
||||
}
|
||||
|
||||
async getCursor(cursorId: string, sessionId: string): Promise<CursorState | null> {
|
||||
const cursor = this.cursors.get(cursorId);
|
||||
if (!cursor) return null;
|
||||
|
||||
if (cursor.sessionId !== sessionId) {
|
||||
throw new Error(`Cursor ${cursorId} not accessible from session ${sessionId}`);
|
||||
}
|
||||
|
||||
if (cursor.expiresAt < new Date()) {
|
||||
this.cursors.delete(cursorId);
|
||||
return null;
|
||||
}
|
||||
|
||||
cursor.lastAccessedAt = new Date();
|
||||
return cursor;
|
||||
}
|
||||
|
||||
async updateCursorPosition(cursorId: string, newPosition: Record<string, any>, itemCount: number) {
|
||||
const cursor = this.cursors.get(cursorId);
|
||||
if (!cursor) return;
|
||||
|
||||
cursor.position = newPosition;
|
||||
cursor.resultCount += itemCount;
|
||||
cursor.lastAccessedAt = new Date();
|
||||
}
|
||||
|
||||
async recordPerformance(cursorId: string, fetchTimeMs: number) {
|
||||
const cursor = this.cursors.get(cursorId);
|
||||
if (!cursor) return;
|
||||
|
||||
const metrics = cursor.performanceMetrics;
|
||||
metrics.totalFetches++;
|
||||
metrics.avgFetchTimeMs = (metrics.avgFetchTimeMs * (metrics.totalFetches - 1) + fetchTimeMs) / metrics.totalFetches;
|
||||
|
||||
// Adaptive chunk sizing: adjust for target 500ms response time
|
||||
const targetTime = 500;
|
||||
if (fetchTimeMs > targetTime && metrics.optimalChunkSize > 10) {
|
||||
metrics.optimalChunkSize = Math.max(10, Math.floor(metrics.optimalChunkSize * 0.8));
|
||||
} else if (fetchTimeMs < targetTime * 0.5 && metrics.optimalChunkSize < 200) {
|
||||
metrics.optimalChunkSize = Math.min(200, Math.floor(metrics.optimalChunkSize * 1.2));
|
||||
}
|
||||
}
|
||||
|
||||
async invalidateCursor(cursorId: string) {
|
||||
this.cursors.delete(cursorId);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (this.cleanupIntervalId) {
|
||||
clearInterval(this.cleanupIntervalId);
|
||||
this.cleanupIntervalId = null;
|
||||
}
|
||||
this.cursors.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Global cursor manager instance
|
||||
export const globalCursorManager = new SessionCursorManager();
|
||||
|
||||
export interface PaginationGuardOptions<T> {
|
||||
maxResponseTokens?: number;
|
||||
defaultPageSize?: number;
|
||||
dataExtractor: (context: Context, params: any) => Promise<T[]> | T[];
|
||||
itemFormatter: (item: T, format?: string) => string;
|
||||
sessionIdExtractor?: (params: any) => string;
|
||||
positionCalculator?: (items: T[], startIndex: number) => Record<string, any>;
|
||||
}
|
||||
|
||||
export async function withPagination<TParams extends Record<string, any>, TData>(
|
||||
toolName: string,
|
||||
params: TParams & PaginationParams,
|
||||
context: Context,
|
||||
response: Response,
|
||||
options: PaginationGuardOptions<TData>
|
||||
): Promise<void> {
|
||||
const startTime = Date.now();
|
||||
const sessionId = options.sessionIdExtractor?.(params) || context.sessionId || 'default';
|
||||
|
||||
// Extract all data
|
||||
const allData = await options.dataExtractor(context, params);
|
||||
|
||||
// Detect if this is a fresh query or cursor continuation
|
||||
const isFreshQuery = !params.cursor_id;
|
||||
|
||||
if (isFreshQuery) {
|
||||
await handleFreshQuery(toolName, params, context, response, allData, options, sessionId, startTime);
|
||||
} else {
|
||||
await handleCursorContinuation(toolName, params, context, response, allData, options, sessionId, startTime);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleFreshQuery<TParams extends Record<string, any>, TData>(
|
||||
toolName: string,
|
||||
params: TParams & PaginationParams,
|
||||
context: Context,
|
||||
response: Response,
|
||||
allData: TData[],
|
||||
options: PaginationGuardOptions<TData>,
|
||||
sessionId: string,
|
||||
startTime: number
|
||||
): Promise<void> {
|
||||
const limit = params.limit || options.defaultPageSize || 50;
|
||||
const pageItems = allData.slice(0, limit);
|
||||
|
||||
// Check if response would be too large
|
||||
const sampleResponse = pageItems.map(item => options.itemFormatter(item)).join('\n');
|
||||
const estimatedTokens = Math.ceil(sampleResponse.length / 4);
|
||||
const maxTokens = options.maxResponseTokens || 8000;
|
||||
|
||||
let cursorId: string | undefined;
|
||||
|
||||
if (allData.length > limit) {
|
||||
// Create cursor for continuation
|
||||
const queryState = QueryStateManager.fromParams(params);
|
||||
const initialPosition = options.positionCalculator?.(allData, limit - 1) || {
|
||||
lastIndex: limit - 1,
|
||||
totalItems: allData.length
|
||||
};
|
||||
|
||||
cursorId = await globalCursorManager.createCursor(
|
||||
sessionId,
|
||||
toolName,
|
||||
queryState,
|
||||
initialPosition
|
||||
);
|
||||
}
|
||||
|
||||
const fetchTimeMs = Date.now() - startTime;
|
||||
|
||||
// Format response
|
||||
if (estimatedTokens > maxTokens && pageItems.length > 10) {
|
||||
// Response is too large, recommend pagination
|
||||
const recommendedLimit = Math.max(10, Math.floor(limit * maxTokens / estimatedTokens));
|
||||
|
||||
response.addResult(
|
||||
`⚠️ **Large response detected (~${estimatedTokens.toLocaleString()} tokens)**\n\n` +
|
||||
`Showing first ${pageItems.length} of ${allData.length} items. ` +
|
||||
`Use pagination to explore all data:\n\n` +
|
||||
`**Continue with next page:**\n` +
|
||||
`${toolName}({...same_params, limit: ${limit}, cursor_id: "${cursorId}"})\n\n` +
|
||||
`**Reduce page size for faster responses:**\n` +
|
||||
`${toolName}({...same_params, limit: ${recommendedLimit}})\n\n` +
|
||||
`**First ${pageItems.length} items:**`
|
||||
);
|
||||
} else {
|
||||
if (cursorId) {
|
||||
response.addResult(
|
||||
`**Results: ${pageItems.length} of ${allData.length} items** ` +
|
||||
`(${fetchTimeMs}ms) • [Next page available]\n`
|
||||
);
|
||||
} else {
|
||||
response.addResult(
|
||||
`**Results: ${pageItems.length} items** (${fetchTimeMs}ms)\n`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Add formatted items
|
||||
pageItems.forEach(item => {
|
||||
response.addResult(options.itemFormatter(item, (params as any).format));
|
||||
});
|
||||
|
||||
// Add pagination footer
|
||||
if (cursorId) {
|
||||
response.addResult(
|
||||
`\n**📄 Pagination**\n` +
|
||||
`• Page: 1 of ${Math.ceil(allData.length / limit)}\n` +
|
||||
`• Next: \`${toolName}({...same_params, cursor_id: "${cursorId}"})\`\n` +
|
||||
`• Items: ${pageItems.length}/${allData.length}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCursorContinuation<TParams extends Record<string, any>, TData>(
|
||||
toolName: string,
|
||||
params: TParams & PaginationParams,
|
||||
context: Context,
|
||||
response: Response,
|
||||
allData: TData[],
|
||||
options: PaginationGuardOptions<TData>,
|
||||
sessionId: string,
|
||||
startTime: number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const cursor = await globalCursorManager.getCursor(params.cursor_id!, sessionId);
|
||||
if (!cursor) {
|
||||
response.addResult(`⚠️ Cursor expired or invalid. Starting fresh query...\n`);
|
||||
await handleFreshQuery(toolName, params, context, response, allData, options, sessionId, startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify query consistency
|
||||
const currentQuery = QueryStateManager.fromParams(params);
|
||||
if (QueryStateManager.fingerprint(currentQuery) !== cursor.queryStateFingerprint) {
|
||||
response.addResult(`⚠️ Query parameters changed. Starting fresh with new filters...\n`);
|
||||
await handleFreshQuery(toolName, params, context, response, allData, options, sessionId, startTime);
|
||||
return;
|
||||
}
|
||||
|
||||
const limit = params.limit || options.defaultPageSize || 50;
|
||||
const startIndex = cursor.position.lastIndex + 1;
|
||||
const pageItems = allData.slice(startIndex, startIndex + limit);
|
||||
|
||||
let newCursorId: string | undefined;
|
||||
if (startIndex + limit < allData.length) {
|
||||
const newPosition = options.positionCalculator?.(allData, startIndex + limit - 1) || {
|
||||
lastIndex: startIndex + limit - 1,
|
||||
totalItems: allData.length
|
||||
};
|
||||
|
||||
await globalCursorManager.updateCursorPosition(cursor.id, newPosition, pageItems.length);
|
||||
newCursorId = cursor.id;
|
||||
} else {
|
||||
await globalCursorManager.invalidateCursor(cursor.id);
|
||||
}
|
||||
|
||||
const fetchTimeMs = Date.now() - startTime;
|
||||
await globalCursorManager.recordPerformance(cursor.id, fetchTimeMs);
|
||||
|
||||
const currentPage = Math.floor(startIndex / limit) + 1;
|
||||
const totalPages = Math.ceil(allData.length / limit);
|
||||
|
||||
response.addResult(
|
||||
`**Results: ${pageItems.length} items** (${fetchTimeMs}ms) • ` +
|
||||
`Page ${currentPage}/${totalPages} • Total fetched: ${cursor.resultCount + pageItems.length}/${allData.length}\n`
|
||||
);
|
||||
|
||||
// Add formatted items
|
||||
pageItems.forEach(item => {
|
||||
response.addResult(options.itemFormatter(item, (params as any).format));
|
||||
});
|
||||
|
||||
// Add pagination footer
|
||||
response.addResult(
|
||||
`\n**📄 Pagination**\n` +
|
||||
`• Page: ${currentPage} of ${totalPages}\n` +
|
||||
(newCursorId ?
|
||||
`• Next: \`${toolName}({...same_params, cursor_id: "${newCursorId}"})\`` :
|
||||
`• ✅ End of results`) +
|
||||
`\n• Progress: ${cursor.resultCount + pageItems.length}/${allData.length} items fetched`
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
response.addResult(`⚠️ Pagination error: ${error}. Starting fresh query...\n`);
|
||||
await handleFreshQuery(toolName, params, context, response, allData, options, sessionId, startTime);
|
||||
}
|
||||
}
|
||||
|
|
@ -15,19 +15,86 @@
|
|||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
import { defineTabTool } from './tool.js';
|
||||
import { defineTool } from './tool.js';
|
||||
import { paginationParamsSchema, withPagination } from '../pagination.js';
|
||||
import type { Context } from '../context.js';
|
||||
import type { Response } from '../response.js';
|
||||
import type { ConsoleMessage } from '../tab.js';
|
||||
|
||||
const console = defineTabTool({
|
||||
const consoleMessagesSchema = paginationParamsSchema.extend({
|
||||
level_filter: z.enum(['all', 'error', 'warning', 'info', 'debug', 'log']).optional().default('all').describe('Filter messages by level'),
|
||||
source_filter: z.enum(['all', 'console', 'javascript', 'network']).optional().default('all').describe('Filter messages by source'),
|
||||
search: z.string().optional().describe('Search text within console messages'),
|
||||
});
|
||||
|
||||
const console = defineTool({
|
||||
capability: 'core',
|
||||
schema: {
|
||||
name: 'browser_console_messages',
|
||||
title: 'Get console messages',
|
||||
description: 'Returns all console messages',
|
||||
inputSchema: z.object({}),
|
||||
description: 'Returns console messages with pagination support. Large message lists are automatically paginated for better performance.',
|
||||
inputSchema: consoleMessagesSchema,
|
||||
type: 'readOnly',
|
||||
},
|
||||
handle: async (tab, params, response) => {
|
||||
tab.consoleMessages().map(message => response.addResult(message.toString()));
|
||||
handle: async (context: Context, params: z.output<typeof consoleMessagesSchema>, response: Response) => {
|
||||
const tab = context.currentTabOrDie();
|
||||
|
||||
await withPagination(
|
||||
'browser_console_messages',
|
||||
params,
|
||||
context,
|
||||
response,
|
||||
{
|
||||
maxResponseTokens: 8000,
|
||||
defaultPageSize: 50,
|
||||
dataExtractor: async () => {
|
||||
const allMessages = tab.consoleMessages();
|
||||
|
||||
// Apply filters
|
||||
let filteredMessages = allMessages;
|
||||
|
||||
if (params.level_filter !== 'all') {
|
||||
filteredMessages = filteredMessages.filter((msg: ConsoleMessage) => {
|
||||
if (!msg.type) return params.level_filter === 'log'; // Default to 'log' for undefined types
|
||||
return msg.type === params.level_filter ||
|
||||
(params.level_filter === 'log' && msg.type === 'info');
|
||||
});
|
||||
}
|
||||
|
||||
if (params.source_filter !== 'all') {
|
||||
filteredMessages = filteredMessages.filter((msg: ConsoleMessage) => {
|
||||
const msgStr = msg.toString().toLowerCase();
|
||||
switch (params.source_filter) {
|
||||
case 'console': return msgStr.includes('console') || msgStr.includes('[log]');
|
||||
case 'javascript': return msgStr.includes('javascript') || msgStr.includes('js');
|
||||
case 'network': return msgStr.includes('network') || msgStr.includes('security');
|
||||
default: return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (params.search) {
|
||||
const searchTerm = params.search.toLowerCase();
|
||||
filteredMessages = filteredMessages.filter((msg: ConsoleMessage) =>
|
||||
msg.toString().toLowerCase().includes(searchTerm) ||
|
||||
msg.text.toLowerCase().includes(searchTerm)
|
||||
);
|
||||
}
|
||||
|
||||
return filteredMessages;
|
||||
},
|
||||
itemFormatter: (message: ConsoleMessage) => {
|
||||
const timestamp = new Date().toISOString();
|
||||
return `[${timestamp}] ${message.toString()}`;
|
||||
},
|
||||
sessionIdExtractor: () => context.sessionId,
|
||||
positionCalculator: (items, lastIndex) => ({
|
||||
lastIndex,
|
||||
totalItems: items.length,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
}
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
import { z } from 'zod';
|
||||
import { defineTool } from './tool.js';
|
||||
import { paginationParamsSchema, withPagination } from '../pagination.js';
|
||||
import { RequestInterceptorOptions } from '../requestInterceptor.js';
|
||||
import type { Context } from '../context.js';
|
||||
|
||||
|
|
@ -37,7 +38,7 @@ const startMonitoringSchema = z.object({
|
|||
outputPath: z.string().optional().describe('Custom output directory path. If not specified, uses session artifact directory')
|
||||
});
|
||||
|
||||
const getRequestsSchema = z.object({
|
||||
const getRequestsSchema = paginationParamsSchema.extend({
|
||||
filter: z.enum(['all', 'failed', 'slow', 'errors', 'success']).optional().default('all').describe('Filter requests by type: all, failed (network failures), slow (>1s), errors (4xx/5xx), success (2xx/3xx)'),
|
||||
|
||||
domain: z.string().optional().describe('Filter requests by domain hostname'),
|
||||
|
|
@ -46,8 +47,6 @@ const getRequestsSchema = z.object({
|
|||
|
||||
status: z.number().optional().describe('Filter requests by HTTP status code'),
|
||||
|
||||
limit: z.number().optional().default(100).describe('Maximum number of requests to return (default: 100)'),
|
||||
|
||||
format: z.enum(['summary', 'detailed', 'stats']).optional().default('summary').describe('Response format: summary (basic info), detailed (full data), stats (statistics only)'),
|
||||
|
||||
slowThreshold: z.number().optional().default(1000).describe('Threshold in milliseconds for considering requests "slow" (default: 1000ms)')
|
||||
|
|
@ -167,7 +166,7 @@ const getRequests = defineTool({
|
|||
schema: {
|
||||
name: 'browser_get_requests',
|
||||
title: 'Get captured requests',
|
||||
description: 'Retrieve and analyze captured HTTP requests with advanced filtering. Shows timing, status codes, headers, and bodies. Perfect for identifying performance issues, failed requests, or analyzing API usage patterns.',
|
||||
description: 'Retrieve and analyze captured HTTP requests with pagination support. Shows timing, status codes, headers, and bodies. Large request lists are automatically paginated for better performance.',
|
||||
inputSchema: getRequestsSchema,
|
||||
type: 'readOnly',
|
||||
},
|
||||
|
|
@ -182,49 +181,8 @@ const getRequests = defineTool({
|
|||
return;
|
||||
}
|
||||
|
||||
let requests = interceptor.getData();
|
||||
|
||||
// Apply filters
|
||||
if (params.filter !== 'all') {
|
||||
switch (params.filter) {
|
||||
case 'failed':
|
||||
requests = interceptor.getFailedRequests();
|
||||
break;
|
||||
case 'slow':
|
||||
requests = interceptor.getSlowRequests(params.slowThreshold);
|
||||
break;
|
||||
case 'errors':
|
||||
requests = requests.filter(r => r.response && r.response.status >= 400);
|
||||
break;
|
||||
case 'success':
|
||||
requests = requests.filter(r => r.response && r.response.status < 400);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (params.domain) {
|
||||
requests = requests.filter(r => {
|
||||
try {
|
||||
return new URL(r.url).hostname === params.domain;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (params.method)
|
||||
requests = requests.filter(r => r.method.toLowerCase() === params.method!.toLowerCase());
|
||||
|
||||
|
||||
if (params.status)
|
||||
requests = requests.filter(r => r.response?.status === params.status);
|
||||
|
||||
|
||||
// Limit results
|
||||
const limitedRequests = requests.slice(0, params.limit);
|
||||
|
||||
// Special case for stats format - no pagination needed
|
||||
if (params.format === 'stats') {
|
||||
// Return statistics only
|
||||
const stats = interceptor.getStats();
|
||||
response.addResult('📊 **Request Statistics**');
|
||||
response.addResult('');
|
||||
|
|
@ -255,50 +213,90 @@ const getRequests = defineTool({
|
|||
return;
|
||||
}
|
||||
|
||||
// Return request data
|
||||
if (limitedRequests.length === 0) {
|
||||
response.addResult('ℹ️ **No requests found matching the criteria**');
|
||||
response.addResult('');
|
||||
response.addResult('💡 Try different filters or ensure the page has made HTTP requests');
|
||||
return;
|
||||
}
|
||||
// Use pagination for request data
|
||||
await withPagination(
|
||||
'browser_get_requests',
|
||||
params,
|
||||
context,
|
||||
response,
|
||||
{
|
||||
maxResponseTokens: 8000,
|
||||
defaultPageSize: 25, // Smaller default for detailed request data
|
||||
dataExtractor: async () => {
|
||||
let requests = interceptor.getData();
|
||||
|
||||
response.addResult(`📋 **Captured Requests (${limitedRequests.length} of ${requests.length} total)**`);
|
||||
response.addResult('');
|
||||
// Apply filters
|
||||
if (params.filter !== 'all') {
|
||||
switch (params.filter) {
|
||||
case 'failed':
|
||||
requests = interceptor.getFailedRequests();
|
||||
break;
|
||||
case 'slow':
|
||||
requests = interceptor.getSlowRequests(params.slowThreshold);
|
||||
break;
|
||||
case 'errors':
|
||||
requests = requests.filter(r => r.response && r.response.status >= 400);
|
||||
break;
|
||||
case 'success':
|
||||
requests = requests.filter(r => r.response && r.response.status < 400);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
limitedRequests.forEach((req, index) => {
|
||||
const duration = req.duration ? `${req.duration}ms` : 'pending';
|
||||
const status = req.failed ? 'FAILED' : req.response?.status || 'pending';
|
||||
const size = req.response?.bodySize ? ` (${(req.response.bodySize / 1024).toFixed(1)}KB)` : '';
|
||||
if (params.domain) {
|
||||
requests = requests.filter(r => {
|
||||
try {
|
||||
return new URL(r.url).hostname === params.domain;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
response.addResult(`**${index + 1}. ${req.method} ${status}** - ${duration}`);
|
||||
response.addResult(` ${req.url}${size}`);
|
||||
if (params.method)
|
||||
requests = requests.filter(r => r.method.toLowerCase() === params.method!.toLowerCase());
|
||||
|
||||
if (params.format === 'detailed') {
|
||||
response.addResult(` 📅 ${req.timestamp}`);
|
||||
if (req.response) {
|
||||
response.addResult(` 📊 Status: ${req.response.status} ${req.response.statusText}`);
|
||||
response.addResult(` ⏱️ Duration: ${req.response.duration}ms`);
|
||||
response.addResult(` 🔄 From Cache: ${req.response.fromCache ? 'Yes' : 'No'}`);
|
||||
if (params.status)
|
||||
requests = requests.filter(r => r.response?.status === params.status);
|
||||
|
||||
// Show key headers
|
||||
const contentType = req.response.headers['content-type'];
|
||||
if (contentType)
|
||||
response.addResult(` 📄 Content-Type: ${contentType}`);
|
||||
return requests;
|
||||
},
|
||||
itemFormatter: (req, format) => {
|
||||
const duration = req.duration ? `${req.duration}ms` : 'pending';
|
||||
const status = req.failed ? 'FAILED' : req.response?.status || 'pending';
|
||||
const size = req.response?.bodySize ? ` (${(req.response.bodySize / 1024).toFixed(1)}KB)` : '';
|
||||
|
||||
}
|
||||
let result = `**${req.method} ${status}** - ${duration}\n ${req.url}${size}`;
|
||||
|
||||
if (req.failed && req.failure)
|
||||
response.addResult(` ❌ Failure: ${req.failure.errorText}`);
|
||||
if (format === 'detailed') {
|
||||
result += `\n 📅 ${req.timestamp}`;
|
||||
if (req.response) {
|
||||
result += `\n 📊 Status: ${req.response.status} ${req.response.statusText}`;
|
||||
result += `\n ⏱️ Duration: ${req.response.duration}ms`;
|
||||
result += `\n 🔄 From Cache: ${req.response.fromCache ? 'Yes' : 'No'}`;
|
||||
|
||||
// Show key headers
|
||||
const contentType = req.response.headers['content-type'];
|
||||
if (contentType)
|
||||
result += `\n 📄 Content-Type: ${contentType}`;
|
||||
}
|
||||
|
||||
response.addResult('');
|
||||
if (req.failed && req.failure)
|
||||
result += `\n ❌ Failure: ${req.failure.errorText}`;
|
||||
|
||||
result += '\n';
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
sessionIdExtractor: () => context.sessionId,
|
||||
positionCalculator: (items, lastIndex) => ({
|
||||
lastIndex,
|
||||
totalItems: items.length,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
if (requests.length > params.limit)
|
||||
response.addResult(`💡 Showing first ${params.limit} results. Use higher limit or specific filters to see more.`);
|
||||
|
||||
);
|
||||
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to get requests: ${error.message}`);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue