monitor-network
Track and filter browser network requests by URL pattern and duration to analyze real-time activity and optimize performance in Vite-based development environments.
Instructions
Monitors network requests in the browser for a specified duration
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| duration | No | Duration in milliseconds to monitor (default: 5000) | |
| urlPattern | No | URL pattern to filter (regex string) |
Implementation Reference
- src/tools/browser-tools.ts:538-597 (handler)The main handler function for the 'monitor-network' tool. It gets the current browser context, sets up a 'request' event listener on the page to capture network requests matching an optional URL pattern, waits for the specified duration (default 5000ms), collects request details (url, method, resourceType, timestamp), removes the listener, and returns the list of captured requests or a message if none.try { // Check browser status const browserStatus = getContextForOperation(); if (!browserStatus.isStarted) { return browserStatus.error; } const requests: Array<{ url: string; method: string; resourceType: string; timestamp: number; }> = []; const pattern = urlPattern ? new RegExp(urlPattern) : null; // Start network request monitoring const requestHandler = (request: Request) => { const url = request.url(); if (!pattern || pattern.test(url)) { requests.push({ url, method: request.method(), resourceType: request.resourceType(), timestamp: Date.now() }); } }; browserStatus.page.on('request', requestHandler); // Wait for specified duration await new Promise(resolve => setTimeout(resolve, duration)); // Stop monitoring browserStatus.page.off('request', requestHandler); return { content: [ { type: 'text', text: requests.length > 0 ? `Captured ${requests.length} network requests:\n${JSON.stringify(requests, null, 2)}` : 'No network requests matching the criteria were captured during the monitoring period.' } ] }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); Logger.error(`Failed to monitor network: ${errorMessage}`); return { content: [ { type: 'text', text: `Failed to monitor network: ${errorMessage}` } ], isError: true }; } }
- src/tools/browser-tools.ts:534-536 (schema)Zod input schema defining parameters for the monitor-network tool: optional urlPattern (regex string to filter requests) and optional duration (ms to monitor, default 5000).urlPattern: z.string().optional().describe('URL pattern to filter (regex string)'), duration: z.number().optional().describe('Duration in milliseconds to monitor (default: 5000)') },
- src/tools/browser-tools.ts:531-598 (registration)Registration of the 'monitor-network' MCP tool using server.tool(), including name, description, input schema, and handler function.'monitor-network', 'Monitors network requests in the browser for a specified duration', { urlPattern: z.string().optional().describe('URL pattern to filter (regex string)'), duration: z.number().optional().describe('Duration in milliseconds to monitor (default: 5000)') }, async ({ urlPattern, duration = 5000 }) => { try { // Check browser status const browserStatus = getContextForOperation(); if (!browserStatus.isStarted) { return browserStatus.error; } const requests: Array<{ url: string; method: string; resourceType: string; timestamp: number; }> = []; const pattern = urlPattern ? new RegExp(urlPattern) : null; // Start network request monitoring const requestHandler = (request: Request) => { const url = request.url(); if (!pattern || pattern.test(url)) { requests.push({ url, method: request.method(), resourceType: request.resourceType(), timestamp: Date.now() }); } }; browserStatus.page.on('request', requestHandler); // Wait for specified duration await new Promise(resolve => setTimeout(resolve, duration)); // Stop monitoring browserStatus.page.off('request', requestHandler); return { content: [ { type: 'text', text: requests.length > 0 ? `Captured ${requests.length} network requests:\n${JSON.stringify(requests, null, 2)}` : 'No network requests matching the criteria were captured during the monitoring period.' } ] }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); Logger.error(`Failed to monitor network: ${errorMessage}`); return { content: [ { type: 'text', text: `Failed to monitor network: ${errorMessage}` } ], isError: true }; } } );