Skip to content

Conversation

@OzTamir
Copy link
Contributor

@OzTamir OzTamir commented Sep 24, 2025

Summary

This PR enhances the list_network_requests tool with two major features:

  • Pagination support to handle large numbers of network requests efficiently
  • Request type filtering to allow filtering by specific resource types

Motivation

Playing around with this MCP, I often found that calls to list_network_requests would frequently cause the LLM (in my case - Claude Code) hit token limits, resulting in errors like:

Error: MCP tool "list_network_requests" response (450039 tokens) exceeds maximum allowed tokens (25000). Please use pagination, filtering, or limit parameters to reduce the response size.

This made the tool unusable for pages with many network requests, which is common in modern web applications. The new pagination and filtering capabilities solve this by allowing users to:

  • Limit the number of requests returned per call
  • Filter to only specific types of requests they're interested in
  • Navigate through results in manageable chunks

Changes

Pagination Support

  • Added pageSize parameter (max 100, defaults to 20) to control the number of requests returned
  • Added pageToken parameter for pagination navigation
  • Implemented pagination logic in McpResponse.ts and networkUtils.ts
  • Added comprehensive pagination metadata in responses (nextPageToken, previousPageToken, startIndex, endIndex, total)

Request Type Filtering

  • Added requestType parameter to filter requests by resource type
  • Supports filtering by 12 different resource types: document, stylesheet, image, media, font, script, xhr, fetch, prefetch, websocket, preflight, other
  • Supports both single type and array of types for filtering
  • Added robust type validation and sanitization

Implementation Details

  • Created new networkUtils.ts with comprehensive utility functions for filtering and pagination
  • Updated McpResponse.ts to handle the new parameters and pass them to the pagination logic
  • Enhanced ToolDefinition.ts to support the new parameter structure
  • Added extensive test coverage for both features

Documentation

  • Updated tool reference documentation to reflect the new parameters
  • Added clear descriptions for all new parameters with examples

Testing

  • Added comprehensive unit tests for pagination logic
  • Added tests for request type filtering functionality
  • Updated existing network tool tests to cover new functionality
  • All tests pass and maintain backward compatibility

Backward Compatibility

  • All new parameters are optional, maintaining full backward compatibility
  • Existing calls to list_network_requests without parameters continue to work as before
  • Default behavior returns all requests when no pagination or filtering is specified

@OrKoN OrKoN self-requested a review September 24, 2025 09:14
Copy link
Collaborator

@OrKoN OrKoN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thank you for the PR! I have left a few comments but I also have a general suggestion: let's split the PR into two 1) for the pagination 2) for the filtering by request type.

return;
}

const sanitizedOptions: NetworkRequestsListingOptions = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to sanitize the values as we can rely on the types and JSON schema.

Could we replace this with the following?

this.#networkRequestsPaginationOptions = options;

export type NetworkRequestsOptions = {
pageSize?: number;
pageToken?: string | null;
requestType?: FilterableResourceType | FilterableResourceType[] | null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that FilterableResourceType[] is not used.

});
}

export function paginateNetworkRequests(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make paginate function generic to support any list of objects.

options?: NetworkRequestsListingOptions,
): NetworkRequestsListingResult {
const sanitizedOptions = options ?? {};
const filteredRequests = filterNetworkRequests(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's filter before the paginate function.

);
}

function validatePageSize(pageSize: number | undefined, total: number): number {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not the JSON schema already validate the page size? If not, can we update the JSON schema.

http://example.com GET [pending]`,
);
const text = result[0].text as string;
assert.ok(text.includes(`## Network requests`));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert to the previous assertion style to ensure relative order and verify full structure.

@OzTamir
Copy link
Contributor Author

OzTamir commented Sep 24, 2025

First of all, thank you for the PR! I have left a few comments but I also have a general suggestion: let's split the PR into two 1) for the pagination 2) for the filtering by request type.

Sounds good! Closing this PR, I'll work through the comment and reopen the pagination PR. Once we get this merged I'll rebase the filtering and open another one.

@OzTamir OzTamir closed this Sep 24, 2025
OrKoN pushed a commit that referenced this pull request Sep 26, 2025
### Summary
This PR build upon #145 to also adds filtering by resource type to
`list_network_requests` (Also see: #137 and #107).

### Motivation
Agents often need specific request types (e.g., scripts, stylesheets,
images). Filtering reduces noise and improves performance.

### Changes
- **New parameter**: `resourceType` (array) to filter by resource types
- **Supported types**: all resource types supported by Puppeteer
- **Backward compatible**: when omitted, returns all requests

Filtering runs before pagination, so pagination applies to the filtered
results.
Copy link

@fc806063 fc806063 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uploading Screenshot_20250926_204540_Chrome.jpg…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants