Skip to content

Commit 579819b

Browse files
feat: use stable id for network request querying (#382)
Provides stable id when selecting a network request, this should fix issues with multiple same places and long URL selection.
1 parent 78bf66a commit 579819b

File tree

8 files changed

+46
-35
lines changed

8 files changed

+46
-35
lines changed

docs/tool-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258

259259
**Parameters:**
260260

261-
- **url** (string) **(required)**: The URL of the request.
261+
- **reqid** (number) **(required)**: The reqid of a request on the page from the listed network requests
262262

263263
---
264264

src/McpContext.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,8 @@ export class McpContext implements Context {
155155
await page.close({runBeforeUnload: false});
156156
}
157157

158-
getNetworkRequestByUrl(url: string): HTTPRequest {
159-
const requests = this.getNetworkRequests();
160-
if (!requests.length) {
161-
throw new Error('No requests found for selected page');
162-
}
163-
164-
for (const request of requests) {
165-
if (request.url() === url) {
166-
return request;
167-
}
168-
}
169-
170-
throw new Error('Request not found for selected page');
158+
getNetworkRequestById(reqid: number): HTTPRequest {
159+
return this.#networkCollector.getById(this.getSelectedPage(), reqid);
171160
}
172161

173162
setNetworkConditions(conditions: string | null): void {

src/McpResponse.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import type {ImageContentData, Response} from './tools/ToolDefinition.js';
2424
import {paginate, type PaginationOptions} from './utils/pagination.js';
2525

2626
interface NetworkRequestData {
27-
networkRequestUrl: string;
27+
networkRequestStableId: number;
2828
requestBody?: string;
2929
responseBody?: string;
3030
}
@@ -81,9 +81,9 @@ export class McpResponse implements Response {
8181
this.#includeConsoleData = value;
8282
}
8383

84-
attachNetworkRequest(url: string): void {
84+
attachNetworkRequest(reqid: number): void {
8585
this.#attachedNetworkRequestData = {
86-
networkRequestUrl: url,
86+
networkRequestStableId: reqid,
8787
};
8888
}
8989

@@ -98,8 +98,8 @@ export class McpResponse implements Response {
9898
get includeConsoleData(): boolean {
9999
return this.#includeConsoleData;
100100
}
101-
get attachedNetworkRequestUrl(): string | undefined {
102-
return this.#attachedNetworkRequestData?.networkRequestUrl;
101+
get attachedNetworkRequestId(): number | undefined {
102+
return this.#attachedNetworkRequestData?.networkRequestStableId;
103103
}
104104
get networkRequestsPageIdx(): number | undefined {
105105
return this.#networkRequestsOptions?.pagination?.pageIdx;
@@ -138,9 +138,9 @@ export class McpResponse implements Response {
138138

139139
let formattedConsoleMessages: string[];
140140

141-
if (this.#attachedNetworkRequestData?.networkRequestUrl) {
142-
const request = context.getNetworkRequestByUrl(
143-
this.#attachedNetworkRequestData.networkRequestUrl,
141+
if (this.#attachedNetworkRequestData?.networkRequestStableId) {
142+
const request = context.getNetworkRequestById(
143+
this.#attachedNetworkRequestData.networkRequestStableId,
144144
);
145145

146146
this.#attachedNetworkRequestData.requestBody =
@@ -309,12 +309,12 @@ Call ${handleDialog.name} to handle it before continuing.`);
309309

310310
#getIncludeNetworkRequestsData(context: McpContext): string[] {
311311
const response: string[] = [];
312-
const url = this.#attachedNetworkRequestData?.networkRequestUrl;
312+
const url = this.#attachedNetworkRequestData?.networkRequestStableId;
313313
if (!url) {
314314
return response;
315315
}
316316

317-
const httpRequest = context.getNetworkRequestByUrl(url);
317+
const httpRequest = context.getNetworkRequestById(url);
318318
response.push(`## Request ${httpRequest.url()}`);
319319
response.push(`Status: ${getStatusFromRequest(httpRequest)}`);
320320
response.push(`### Request Headers`);

src/PageCollector.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ export class PageCollector<T> {
133133
getIdForResource(resource: WithSymbolId<T>): number {
134134
return resource[stableIdSymbol] ?? -1;
135135
}
136+
137+
getById(page: Page, stableId: number): T {
138+
const data = this.storage.get(page);
139+
if (!data || !data.length) {
140+
throw new Error('No requests found for selected page');
141+
}
142+
143+
for (const collected of data) {
144+
if (collected[stableIdSymbol] === stableId) {
145+
return collected;
146+
}
147+
}
148+
149+
throw new Error('Request not found for selected page');
150+
}
136151
}
137152

138153
export class NetworkCollector extends PageCollector<HTTPRequest> {

src/tools/ToolDefinition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export interface Response {
5050
setIncludeConsoleData(value: boolean): void;
5151
setIncludeSnapshot(value: boolean): void;
5252
attachImage(value: ImageContentData): void;
53-
attachNetworkRequest(url: string): void;
53+
attachNetworkRequest(reqid: number): void;
5454
}
5555

5656
/**

src/tools/network.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ export const getNetworkRequest = defineTool({
8080
readOnlyHint: true,
8181
},
8282
schema: {
83-
url: z.string().describe('The URL of the request.'),
83+
reqid: z
84+
.number()
85+
.describe(
86+
'The reqid of a request on the page from the listed network requests',
87+
),
8488
},
8589
handler: async (request, response, _context) => {
86-
response.attachNetworkRequest(request.params.url);
90+
response.attachNetworkRequest(request.params.reqid);
8791
},
8892
});

tests/McpResponse.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ reqid=2 GET http://example.com [pending]`,
248248
context.getNetworkRequests = () => {
249249
return [request];
250250
};
251-
response.attachNetworkRequest(request.url());
251+
context.getNetworkRequestById = () => {
252+
return request;
253+
};
254+
response.attachNetworkRequest(1);
252255

253256
const result = await response.handle('test', context);
254257

@@ -279,7 +282,10 @@ reqid=1 POST http://example.com [success - 200]`,
279282
context.getNetworkRequests = () => {
280283
return [request];
281284
};
282-
response.attachNetworkRequest(request.url());
285+
context.getNetworkRequestById = () => {
286+
return request;
287+
};
288+
response.attachNetworkRequest(1);
283289
const result = await response.handle('test', context);
284290
assert.strictEqual(
285291
result[0].text,

tests/tools/network.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,19 @@ describe('network', () => {
2828
const page = await context.getSelectedPage();
2929
await page.goto('data:text/html,<div>Hello MCP</div>');
3030
await getNetworkRequest.handler(
31-
{params: {url: 'data:text/html,<div>Hello MCP</div>'}},
31+
{params: {reqid: 1}},
3232
response,
3333
context,
3434
);
35-
assert.equal(
36-
response.attachedNetworkRequestUrl,
37-
'data:text/html,<div>Hello MCP</div>',
38-
);
35+
assert.equal(response.attachedNetworkRequestId, 1);
3936
});
4037
});
4138
it('should not add the request list', async () => {
4239
await withBrowser(async (response, context) => {
4340
const page = await context.getSelectedPage();
4441
await page.goto('data:text/html,<div>Hello MCP</div>');
4542
await getNetworkRequest.handler(
46-
{params: {url: 'data:text/html,<div>Hello MCP</div>'}},
43+
{params: {reqid: 1}},
4744
response,
4845
context,
4946
);

0 commit comments

Comments
 (0)