Skip to content

Commit 71b23b0

Browse files
committed
Integrate with network request
1 parent e8ec461 commit 71b23b0

File tree

8 files changed

+42
-53
lines changed

8 files changed

+42
-53
lines changed

docs/tool-reference.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,12 @@
255255

256256
### `get_network_request`
257257

258-
**Description:** Gets a network request by URL. You can get all requests by calling [`list_network_requests`](#list_network_requests).
258+
**Description:** Gets a network request by reqid. You can get all requests by calling [`list_network_requests`](#list_network_requests).
259+
Get the request currently selected in the DevTools UI by ommitting reqid
259260

260261
**Parameters:**
261262

262-
- **reqid** (number) **(required)**: The reqid of a request on the page from the listed network requests
263+
- **reqid** (number) _(optional)_: The reqid of the network request. If omitted, looks up the current request selected in DevTools UI.
263264

264265
---
265266

src/McpResponse.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ export class McpResponse implements Response {
4444
pagination?: PaginationOptions;
4545
resourceTypes?: ResourceType[];
4646
includePreservedRequests?: boolean;
47+
networkRequestIdInDevToolsUI?: number;
4748
};
4849
#consoleDataOptions?: {
4950
include: boolean;
5051
pagination?: PaginationOptions;
5152
types?: string[];
5253
includePreservedMessages?: boolean;
5354
};
54-
#includeDevtoolsData = false;
5555

5656
setIncludePages(value: boolean): void {
5757
this.#includePages = value;
@@ -63,15 +63,12 @@ export class McpResponse implements Response {
6363
};
6464
}
6565

66-
includeDevtoolsData(value: boolean): void {
67-
this.#includeDevtoolsData = value;
68-
}
69-
7066
setIncludeNetworkRequests(
7167
value: boolean,
7268
options?: PaginationOptions & {
7369
resourceTypes?: ResourceType[];
7470
includePreservedRequests?: boolean;
71+
networkRequestIdInDevToolsUI?: number;
7572
},
7673
): void {
7774
if (!value) {
@@ -90,6 +87,7 @@ export class McpResponse implements Response {
9087
: undefined,
9188
resourceTypes: options?.resourceTypes,
9289
includePreservedRequests: options?.includePreservedRequests,
90+
networkRequestIdInDevToolsUI: options?.networkRequestIdInDevToolsUI,
9391
};
9492
}
9593

@@ -296,17 +294,11 @@ export class McpResponse implements Response {
296294
);
297295
}
298296

299-
let devToolsData;
300-
if (this.#includeDevtoolsData) {
301-
devToolsData = await context.getDevToolsData();
302-
}
303-
304297
return this.format(toolName, context, {
305298
bodies,
306299
consoleData,
307300
consoleListData,
308301
formattedSnapshot,
309-
devToolsData,
310302
});
311303
}
312304

@@ -321,23 +313,13 @@ export class McpResponse implements Response {
321313
consoleData: ConsoleMessageData | undefined;
322314
consoleListData: ConsoleMessageData[] | undefined;
323315
formattedSnapshot: string | undefined;
324-
devToolsData?: {requestId?: number};
325316
},
326317
): Array<TextContent | ImageContent> {
327318
const response = [`# ${toolName} response`];
328319
for (const line of this.#textResponseLines) {
329320
response.push(line);
330321
}
331322

332-
response.push('## Network requests inspected in DevTools');
333-
if (data.devToolsData?.requestId) {
334-
response.push(`Network request: reqid=${data.devToolsData?.requestId}`);
335-
} else {
336-
response.push(
337-
`Nothing inspected right now. Call list_pages to check if anything is selected by the user in DevTools.`,
338-
);
339-
}
340-
341323
const networkConditions = context.getNetworkConditions();
342324
if (networkConditions) {
343325
response.push(`## Network emulation`);
@@ -412,6 +394,8 @@ Call ${handleDialog.name} to handle it before continuing.`);
412394
getShortDescriptionForRequest(
413395
request,
414396
context.getNetworkRequestStableId(request),
397+
context.getNetworkRequestStableId(request) ===
398+
this.#networkRequestsOptions?.networkRequestIdInDevToolsUI,
415399
),
416400
);
417401
}

src/formatters/networkFormatter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const BODY_CONTEXT_SIZE_LIMIT = 10000;
1313
export function getShortDescriptionForRequest(
1414
request: HTTPRequest,
1515
id: number,
16+
selectedInDevToolsUI = false,
1617
): string {
1718
// TODO truncate the URL
18-
return `reqid=${id} ${request.method()} ${request.url()} ${getStatusFromRequest(request)}`;
19+
return `reqid=${id} ${request.method()} ${request.url()} ${getStatusFromRequest(request)}${selectedInDevToolsUI ? ` [selected in DevTools UI]` : ''}`;
1920
}
2021

2122
export function getStatusFromRequest(request: HTTPRequest): string {

src/main.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
} from './third_party/index.js';
2222
import {ToolCategory} from './tools/categories.js';
2323
import * as consoleTools from './tools/console.js';
24-
import * as devtoolsTools from './tools/devtools.js';
2524
import * as emulationTools from './tools/emulation.js';
2625
import * as inputTools from './tools/input.js';
2726
import * as networkTools from './tools/network.js';
@@ -170,7 +169,6 @@ function registerTool(tool: ToolDefinition): void {
170169

171170
const tools = [
172171
...Object.values(consoleTools),
173-
...Object.values(devtoolsTools),
174172
...Object.values(emulationTools),
175173
...Object.values(inputTools),
176174
...Object.values(networkTools),

src/tools/ToolDefinition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface Response {
5555
options?: PaginationOptions & {
5656
resourceTypes?: string[];
5757
includePreservedRequests?: boolean;
58+
networkRequestIdInDevToolsUI?: number;
5859
},
5960
): void;
6061
setIncludeConsoleData(
@@ -65,7 +66,6 @@ export interface Response {
6566
},
6667
): void;
6768
includeSnapshot(params?: SnapshotParams): void;
68-
includeDevtoolsData(value: boolean): void;
6969
attachImage(value: ImageContentData): void;
7070
attachNetworkRequest(reqid: number): void;
7171
attachConsoleMessage(msgid: number): void;
@@ -103,6 +103,7 @@ export type Context = Readonly<{
103103
text: string;
104104
timeout?: number | undefined;
105105
}): Promise<Element>;
106+
getDevToolsData(): Promise<undefined | {requestId?: number}>;
106107
}>;
107108

108109
export function defineTool<Schema extends zod.ZodRawShape>(

src/tools/devtools.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/tools/network.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,46 @@ export const listNetworkRequests = defineTool({
7070
'Set to true to return the preserved requests over the last 3 navigations.',
7171
),
7272
},
73-
handler: async (request, response) => {
73+
handler: async (request, response, context) => {
74+
const data = await context.getDevToolsData();
7475
response.setIncludeNetworkRequests(true, {
7576
pageSize: request.params.pageSize,
7677
pageIdx: request.params.pageIdx,
7778
resourceTypes: request.params.resourceTypes,
7879
includePreservedRequests: request.params.includePreservedRequests,
80+
networkRequestIdInDevToolsUI: data?.requestId,
7981
});
8082
},
8183
});
8284

8385
export const getNetworkRequest = defineTool({
8486
name: 'get_network_request',
85-
description: `Gets a network request by URL. You can get all requests by calling ${listNetworkRequests.name}.`,
87+
description: `Gets a network request by reqid. You can get all requests by calling ${listNetworkRequests.name}.
88+
Get the request currently selected in the DevTools UI by ommitting reqid`,
8689
annotations: {
8790
category: ToolCategory.NETWORK,
8891
readOnlyHint: true,
8992
},
9093
schema: {
9194
reqid: zod
9295
.number()
96+
.optional()
9397
.describe(
94-
'The reqid of a request on the page from the listed network requests',
98+
'The reqid of the network request. If omitted, looks up the current request selected in DevTools UI.',
9599
),
96100
},
97-
handler: async (request, response, _context) => {
98-
response.attachNetworkRequest(request.params.reqid);
101+
handler: async (request, response, context) => {
102+
if (request.params.reqid) {
103+
response.attachNetworkRequest(request.params.reqid);
104+
} else {
105+
const data = await context.getDevToolsData();
106+
if (data?.requestId) {
107+
response.attachNetworkRequest(data?.requestId);
108+
} else {
109+
response.appendResponseLine(
110+
`Nothing is currently selected in DevTools UI.`,
111+
);
112+
}
113+
}
99114
},
100115
});

tests/formatters/networkFormatter.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ describe('networkFormatter', () => {
7171
'reqid=1 GET http://example.com [failed - Error in Network]',
7272
);
7373
});
74+
75+
it('marks requests selected in DevTools UI', async () => {
76+
const request = getMockRequest();
77+
const result = getShortDescriptionForRequest(request, 1, true);
78+
79+
assert.equal(
80+
result,
81+
'reqid=1 GET http://example.com [pending] [selected in DevTools UI]',
82+
);
83+
});
7484
});
7585

7686
describe('getFormattedHeaderValue', () => {

0 commit comments

Comments
 (0)