Skip to content

Commit 5c871c3

Browse files
authored
refactor: cleanup data fetching (#441)
Drive-by: fixed extra newline
1 parent 962ac88 commit 5c871c3

File tree

5 files changed

+66
-65
lines changed

5 files changed

+66
-65
lines changed

src/McpResponse.ts

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import type {ConsoleMessage, ResourceType} from 'puppeteer-core';
77

8+
import type {ConsoleMessageData} from './formatters/consoleFormatter.js';
89
import {
910
formatConsoleEventShort,
1011
formatConsoleEventVerbose,
@@ -27,26 +28,12 @@ import type {ImageContentData, Response} from './tools/ToolDefinition.js';
2728
import {paginate} from './utils/pagination.js';
2829
import type {PaginationOptions} from './utils/types.js';
2930

30-
interface NetworkRequestData {
31-
networkRequestStableId: number;
32-
requestBody?: string;
33-
responseBody?: string;
34-
}
35-
36-
export interface ConsoleMessageData {
37-
consoleMessageStableId: number;
38-
type?: string;
39-
message?: string;
40-
args?: string[];
41-
}
42-
4331
export class McpResponse implements Response {
4432
#includePages = false;
4533
#includeSnapshot = false;
4634
#includeVerboseSnapshot = false;
47-
#attachedNetworkRequestData?: NetworkRequestData;
48-
#attachedConsoleMessageData?: ConsoleMessageData;
49-
#consoleMessagesData?: ConsoleMessageData[];
35+
#attachedNetworkRequestId?: number;
36+
#attachedConsoleMessageId?: number;
5037
#textResponseLines: string[] = [];
5138
#images: ImageContentData[] = [];
5239
#networkRequestsOptions?: {
@@ -118,15 +105,11 @@ export class McpResponse implements Response {
118105
}
119106

120107
attachNetworkRequest(reqid: number): void {
121-
this.#attachedNetworkRequestData = {
122-
networkRequestStableId: reqid,
123-
};
108+
this.#attachedNetworkRequestId = reqid;
124109
}
125110

126-
attachConsoleMessage(id: number): void {
127-
this.#attachedConsoleMessageData = {
128-
consoleMessageStableId: id,
129-
};
111+
attachConsoleMessage(msgid: number): void {
112+
this.#attachedConsoleMessageId = msgid;
130113
}
131114

132115
get includePages(): boolean {
@@ -141,7 +124,7 @@ export class McpResponse implements Response {
141124
return this.#consoleDataOptions?.include ?? false;
142125
}
143126
get attachedNetworkRequestId(): number | undefined {
144-
return this.#attachedNetworkRequestData?.networkRequestStableId;
127+
return this.#attachedNetworkRequestId;
145128
}
146129
get networkRequestsPageIdx(): number | undefined {
147130
return this.#networkRequestsOptions?.pagination?.pageIdx;
@@ -188,31 +171,34 @@ export class McpResponse implements Response {
188171
await context.createTextSnapshot(this.#includeVerboseSnapshot);
189172
}
190173

191-
if (this.#attachedNetworkRequestData?.networkRequestStableId) {
174+
const bodies: {
175+
requestBody?: string;
176+
responseBody?: string;
177+
} = {};
178+
179+
if (this.#attachedNetworkRequestId) {
192180
const request = context.getNetworkRequestById(
193-
this.#attachedNetworkRequestData.networkRequestStableId,
181+
this.#attachedNetworkRequestId,
194182
);
195183

196-
this.#attachedNetworkRequestData.requestBody =
197-
await getFormattedRequestBody(request);
184+
bodies.requestBody = await getFormattedRequestBody(request);
198185

199186
const response = request.response();
200187
if (response) {
201-
this.#attachedNetworkRequestData.responseBody =
202-
await getFormattedResponseBody(response);
188+
bodies.responseBody = await getFormattedResponseBody(response);
203189
}
204190
}
205191

206-
if (this.#attachedConsoleMessageData?.consoleMessageStableId) {
192+
let consoleData: ConsoleMessageData | undefined;
193+
194+
if (this.#attachedConsoleMessageId) {
207195
const message = context.getConsoleMessageById(
208-
this.#attachedConsoleMessageData.consoleMessageStableId,
196+
this.#attachedConsoleMessageId,
209197
);
210-
const consoleMessageStableId =
211-
this.#attachedConsoleMessageData.consoleMessageStableId;
212-
let data: ConsoleMessageData;
198+
const consoleMessageStableId = this.#attachedConsoleMessageId;
213199
if ('args' in message) {
214200
const consoleMessage = message as ConsoleMessage;
215-
data = {
201+
consoleData = {
216202
consoleMessageStableId,
217203
type: consoleMessage.type(),
218204
message: consoleMessage.text(),
@@ -228,16 +214,16 @@ export class McpResponse implements Response {
228214
),
229215
};
230216
} else {
231-
data = {
217+
consoleData = {
232218
consoleMessageStableId,
233219
type: 'error',
234220
message: (message as Error).message,
235221
args: [],
236222
};
237223
}
238-
this.#attachedConsoleMessageData = data;
239224
}
240225

226+
let consoleListData: ConsoleMessageData[] | undefined;
241227
if (this.#consoleDataOptions?.include) {
242228
let messages = context.getConsoleData();
243229

@@ -251,7 +237,7 @@ export class McpResponse implements Response {
251237
});
252238
}
253239

254-
this.#consoleMessagesData = await Promise.all(
240+
consoleListData = await Promise.all(
255241
messages.map(async (item): Promise<ConsoleMessageData> => {
256242
const consoleMessageStableId =
257243
context.getConsoleMessageStableId(item);
@@ -283,12 +269,24 @@ export class McpResponse implements Response {
283269
);
284270
}
285271

286-
return this.format(toolName, context);
272+
return this.format(toolName, context, {
273+
bodies,
274+
consoleData,
275+
consoleListData,
276+
});
287277
}
288278

289279
format(
290280
toolName: string,
291281
context: McpContext,
282+
data: {
283+
bodies: {
284+
requestBody?: string;
285+
responseBody?: string;
286+
};
287+
consoleData: ConsoleMessageData | undefined;
288+
consoleListData: ConsoleMessageData[] | undefined;
289+
},
292290
): Array<TextContent | ImageContent> {
293291
const response = [`# ${toolName} response`];
294292
for (const line of this.#textResponseLines) {
@@ -342,8 +340,8 @@ Call ${handleDialog.name} to handle it before continuing.`);
342340
}
343341
}
344342

345-
response.push(...this.#getIncludeNetworkRequestsData(context));
346-
response.push(...this.#getAttachedConsoleMessageData());
343+
response.push(...this.#formatNetworkRequestData(context, data.bodies));
344+
response.push(...this.#formatConsoleData(data.consoleData));
347345

348346
if (this.#networkRequestsOptions?.include) {
349347
let requests = context.getNetworkRequests();
@@ -380,7 +378,7 @@ Call ${handleDialog.name} to handle it before continuing.`);
380378
}
381379

382380
if (this.#consoleDataOptions?.include) {
383-
const messages = this.#consoleMessagesData ?? [];
381+
const messages = data.consoleListData ?? [];
384382

385383
response.push('## Console messages');
386384
if (messages.length) {
@@ -437,9 +435,8 @@ Call ${handleDialog.name} to handle it before continuing.`);
437435
};
438436
}
439437

440-
#getAttachedConsoleMessageData(): string[] {
438+
#formatConsoleData(data: ConsoleMessageData | undefined): string[] {
441439
const response: string[] = [];
442-
const data = this.#attachedConsoleMessageData;
443440
if (!data) {
444441
return response;
445442
}
@@ -448,24 +445,30 @@ Call ${handleDialog.name} to handle it before continuing.`);
448445
return response;
449446
}
450447

451-
#getIncludeNetworkRequestsData(context: McpContext): string[] {
448+
#formatNetworkRequestData(
449+
context: McpContext,
450+
data: {
451+
requestBody?: string;
452+
responseBody?: string;
453+
},
454+
): string[] {
452455
const response: string[] = [];
453-
const url = this.#attachedNetworkRequestData?.networkRequestStableId;
454-
if (!url) {
456+
const id = this.#attachedNetworkRequestId;
457+
if (!id) {
455458
return response;
456459
}
457460

458-
const httpRequest = context.getNetworkRequestById(url);
461+
const httpRequest = context.getNetworkRequestById(id);
459462
response.push(`## Request ${httpRequest.url()}`);
460463
response.push(`Status: ${getStatusFromRequest(httpRequest)}`);
461464
response.push(`### Request Headers`);
462465
for (const line of getFormattedHeaderValue(httpRequest.headers())) {
463466
response.push(line);
464467
}
465468

466-
if (this.#attachedNetworkRequestData?.requestBody) {
469+
if (data.requestBody) {
467470
response.push(`### Request Body`);
468-
response.push(this.#attachedNetworkRequestData.requestBody);
471+
response.push(data.requestBody);
469472
}
470473

471474
const httpResponse = httpRequest.response();
@@ -476,9 +479,9 @@ Call ${handleDialog.name} to handle it before continuing.`);
476479
}
477480
}
478481

479-
if (this.#attachedNetworkRequestData?.responseBody) {
482+
if (data.responseBody) {
480483
response.push(`### Response Body`);
481-
response.push(this.#attachedNetworkRequestData.responseBody);
484+
response.push(data.responseBody);
482485
}
483486

484487
const httpFailure = httpRequest.failure();

src/formatters/consoleFormatter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {ConsoleMessageData} from '../McpResponse.js';
7+
export interface ConsoleMessageData {
8+
consoleMessageStableId: number;
9+
type?: string;
10+
message?: string;
11+
args?: string[];
12+
}
813

914
// The short format for a console message, based on a previous format.
1015
export function formatConsoleEventShort(msg: ConsoleMessageData): string {
@@ -28,7 +33,7 @@ export function formatConsoleEventVerbose(msg: ConsoleMessageData): string {
2833
`ID: ${msg.consoleMessageStableId}`,
2934
`Message: ${msg.type}> ${msg.message}`,
3035
formatArgs(msg),
31-
];
36+
].filter(line => !!line);
3237

3338
return result.join('\n');
3439
}

tests/formatters/consoleFormatter.test.js.snapshot

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ msgid=2 [log] Processing file: (1 args)
1313
exports[`consoleFormatter > formatConsoleEventVerbose > formats a console.error message 1`] = `
1414
ID: 4
1515
Message: error> Something went wrong
16-
1716
`;
1817

1918
exports[`consoleFormatter > formatConsoleEventVerbose > formats a console.log message 1`] = `
2019
ID: 1
2120
Message: log> Hello, world!
22-
2321
`;
2422

2523
exports[`consoleFormatter > formatConsoleEventVerbose > formats a console.log message with multiple arguments 1`] = `

tests/formatters/consoleFormatter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import {describe, it} from 'node:test';
88

9+
import type {ConsoleMessageData} from '../../src/formatters/consoleFormatter.js';
910
import {
1011
formatConsoleEventShort,
1112
formatConsoleEventVerbose,
1213
} from '../../src/formatters/consoleFormatter.js';
13-
import type {ConsoleMessageData} from '../../src/McpResponse.js';
1414

1515
describe('consoleFormatter', () => {
1616
describe('formatConsoleEventShort', () => {

tests/tools/console.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ describe('console', () => {
2828
'<script>console.error("This is an error")</script>',
2929
);
3030
await listConsoleMessages.handler({params: {}}, response, context);
31-
await response.handle('test', context);
32-
33-
const formattedResponse = response.format('test', context);
34-
31+
const formattedResponse = await response.handle('test', context);
3532
const textContent = formattedResponse[0] as {text: string};
3633
assert.ok(
3734
textContent.text.includes('msgid=1 [error] This is an error'),
@@ -54,9 +51,7 @@ describe('console', () => {
5451
response,
5552
context,
5653
);
57-
await response.handle('test', context);
58-
59-
const formattedResponse = response.format('test', context);
54+
const formattedResponse = await response.handle('test', context);
6055
const textContent = formattedResponse[0] as {text: string};
6156
assert.ok(
6257
textContent.text.includes('msgid=1 [error] This is an error'),

0 commit comments

Comments
 (0)