Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 64 additions & 26 deletions src/McpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ export class McpResponse implements Response {
if (this.#listExtensions) {
extensions = context.listExtensions();
}

let consoleMessages: Array<ConsoleFormatter | IssueFormatter> | undefined;
if (this.#consoleDataOptions?.include) {
let messages = context.getConsoleData(
Expand Down Expand Up @@ -378,13 +377,8 @@ export class McpResponse implements Response {
}

if (requests.length) {
const data = this.#dataWithPagination(
requests,
this.#networkRequestsOptions.pagination,
);

networkRequests = await Promise.all(
data.items.map(request =>
requests.map(request =>
NetworkFormatter.from(request, {
requestId: context.getNetworkRequestStableId(request),
selectedInDevToolsUI:
Expand Down Expand Up @@ -424,9 +418,36 @@ export class McpResponse implements Response {
extensions?: InstalledExtension[];
},
): {content: Array<TextContent | ImageContent>; structuredContent: object} {
const structuredContent: {
snapshot?: object;
snapshotFilePath?: string;
tabId?: string;
networkRequest?: object;
networkRequests?: object[];
consoleMessage?: object;
consoleMessages?: object[];
traceSummary?: string;
traceInsights?: Array<{insightName: string; insightKey: string}>;
extensions?: object[];
message?: string;
networkConditions?: string;
navigationTimeout?: number;
viewport?: object;
userAgent?: string;
cpuThrottlingRate?: number;
dialog?: {
type: string;
message: string;
defaultValue?: string;
};
pages?: object[];
pagination?: object;
} = {};

const response = [`# ${toolName} response`];
for (const line of this.#textResponseLines) {
response.push(line);
if (this.#textResponseLines.length) {
structuredContent.message = this.#textResponseLines.join('\n');
response.push(...this.#textResponseLines);
}

const networkConditions = context.getNetworkConditions();
Expand All @@ -436,24 +457,29 @@ export class McpResponse implements Response {
response.push(
`Default navigation timeout set to ${context.getNavigationTimeout()} ms`,
);
structuredContent.networkConditions = networkConditions;
structuredContent.navigationTimeout = context.getNavigationTimeout();
}

const viewport = context.getViewport();
if (viewport) {
response.push(`## Viewport emulation`);
response.push(`Emulating viewport: ${JSON.stringify(viewport)}`);
structuredContent.viewport = viewport;
}

const userAgent = context.getUserAgent();
if (userAgent) {
response.push(`## UserAgent emulation`);
response.push(`Emulating userAgent: ${userAgent}`);
structuredContent.userAgent = userAgent;
}

const cpuThrottlingRate = context.getCpuThrottlingRate();
if (cpuThrottlingRate > 1) {
response.push(`## CPU emulation`);
response.push(`Emulating: ${cpuThrottlingRate}x slowdown`);
structuredContent.cpuThrottlingRate = cpuThrottlingRate;
}

const dialog = context.getDialog();
Expand All @@ -465,6 +491,11 @@ export class McpResponse implements Response {
response.push(`# Open dialog
${dialog.type()}: ${dialog.message()}${defaultValueIfNeeded}.
Call ${handleDialog.name} to handle it before continuing.`);
structuredContent.dialog = {
type: dialog.type(),
message: dialog.message(),
defaultValue: dialog.defaultValue(),
};
}

if (this.#includePages) {
Expand All @@ -475,21 +506,15 @@ Call ${handleDialog.name} to handle it before continuing.`);
);
}
response.push(...parts);
structuredContent.pages = context.getPages().map(page => {
return {
id: context.getPageId(page),
url: page.url(),
selected: context.isPageSelected(page),
};
});
}

const structuredContent: {
snapshot?: object;
snapshotFilePath?: string;
tabId?: string;
networkRequest?: object;
networkRequests?: object[];
consoleMessage?: object;
consoleMessages?: object[];
traceSummary?: string;
traceInsights?: Array<{insightName: string; insightKey: string}>;
extensions?: object[];
} = {};

if (this.#tabId) {
structuredContent.tabId = this.#tabId;
}
Expand Down Expand Up @@ -582,6 +607,7 @@ Call ${handleDialog.name} to handle it before continuing.`);
requests,
this.#networkRequestsOptions.pagination,
);
structuredContent.pagination = paginationData.pagination;
response.push(...paginationData.info);
if (data.networkRequests) {
structuredContent.networkRequests = [];
Expand All @@ -600,13 +626,16 @@ Call ${handleDialog.name} to handle it before continuing.`);

response.push('## Console messages');
if (messages.length) {
const data = this.#dataWithPagination(
const paginationData = this.#dataWithPagination(
messages,
this.#consoleDataOptions.pagination,
);
response.push(...data.info);
response.push(...data.items.map(message => message.toString()));
structuredContent.consoleMessages = data.items.map(message =>
structuredContent.pagination = paginationData.pagination;
response.push(...paginationData.info);
response.push(
...paginationData.items.map(message => message.toString()),
);
structuredContent.consoleMessages = paginationData.items.map(message =>
message.toJSON(),
);
} else {
Expand Down Expand Up @@ -654,6 +683,15 @@ Call ${handleDialog.name} to handle it before continuing.`);
return {
info: response,
items: paginationResult.items,
pagination: {
currentPage: paginationResult.currentPage,
totalPages: paginationResult.totalPages,
hasNextPage: paginationResult.hasNextPage,
hasPreviousPage: paginationResult.hasPreviousPage,
startIndex: paginationResult.startIndex,
endIndex: paginationResult.endIndex,
invalidPage: paginationResult.invalidPage,
},
};
}

Expand Down
9 changes: 9 additions & 0 deletions tests/McpContext.test.js.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ exports[`McpContext > should include file paths in structured content when savin

exports[`McpContext > should include network requests in structured content 1`] = `
{
"pagination": {
"currentPage": 0,
"totalPages": 1,
"hasNextPage": false,
"hasPreviousPage": false,
"startIndex": 0,
"endIndex": 1,
"invalidPage": false
},
"networkRequests": [
{
"requestId": 123,
Expand Down
Loading