Skip to content

Chrome DevTools for coding agents

fix(performance): populate structuredContent for traceInsight in performance_analyze_insight (#2981) (#3115)

Yuri (IURII) Trukhinc2c6cc47c618
diff --git a/src/McpResponse.ts b/src/McpResponse.ts
index 467909e8..f272c29b 100644
--- a/src/McpResponse.ts
+++ b/src/McpResponse.ts
@@ -893,6 +893,8 @@ export class McpResponse implements Response {
insightName: string;
insightKey: string | undefined;
}>;
+ traceInsight?: string;
+ error?: string;
lighthouseResult?: object;
extensions?: object[];
thirdPartyDeveloperTools?: object[];
@@ -1232,8 +1234,10 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
);
if ('error' in insightOutput) {
response.push(insightOutput.error);
+ structuredContent.error = insightOutput.error;
} else {
response.push(insightOutput.output);
+ structuredContent.traceInsight = insightOutput.output;
}
}
diff --git a/tests/McpResponse.test.js.snapshot b/tests/McpResponse.test.js.snapshot
index faafc374..ed7f7a64 100644
--- a/tests/McpResponse.test.js.snapshot
+++ b/tests/McpResponse.test.js.snapshot
@@ -721,7 +721,9 @@ No Performance Insights for the given insight set id. Only use ids given in the
`;
exports[`McpResponse network pagination > trace insights > includes error if insight not found 2`] = `
-{}
+{
+ "error": "No Performance Insights for the given insight set id. Only use ids given in the \\"Available insight sets\\" list."
+}
`;
exports[`McpResponse network pagination > trace insights > includes the trace insight output 1`] = `
@@ -770,7 +772,9 @@ We can break this time down into the 4 subparts that combine to make the LCP tim
`;
exports[`McpResponse network pagination > trace insights > includes the trace insight output 2`] = `
-{}
+{
+ "traceInsight": "## Insight Title: LCP breakdown\\n\\n## Insight Summary:\\nThis insight is used to analyze the time spent that contributed to the final LCP time and identify which of the 4 subparts (or 2 if there was no LCP resource) are contributing most to the delay in rendering the LCP element.\\n\\n## Detailed analysis:\\nThe Largest Contentful Paint (LCP) time for this navigation was 129 ms.\\nThe LCP element is an image fetched from https://web-dev.imgix.net/image/kheDArv5csY6rvQUJDbWRscckLr1/4i7JstVZvgTFk9dxCe4a.svg (eventKey: s-1314, ts: 122411037986).\\n## LCP resource network request: https://web-dev.imgix.net/image/kheDArv5csY6rvQUJDbWRscckLr1/4i7JstVZvgTFk9dxCe4a.svg\\neventKey: s-1314\\nTimings:\\n- Queued at: 41 ms\\n- Request sent at: 47 ms\\n- Download complete at: 56 ms\\n- Main thread processing completed at: 58 ms\\nDurations:\\n- Download time: 0.3 ms\\n- Main thread processing time: 2 ms\\n- Total duration: 17 ms\\nRedirects: no redirects\\nStatus code: 200\\nMIME Type: image/svg+xml\\nProtocol: unknown\\nPriority: VeryHigh\\nRender-blocking: No\\nFrom a service worker: No\\nInitiators (root request to the request that directly loaded this one): none\\n\\n\\nWe can break this time down into the 4 subparts that combine to make the LCP time:\\n\\n- Time to first byte: 8 ms (6.1% of total LCP time)\\n- Resource load delay: 33 ms (25.7% of total LCP time)\\n- Resource load duration: 15 ms (11.4% of total LCP time)\\n- Element render delay: 73 ms (56.8% of total LCP time)\\n\\n## Estimated savings: none\\n\\n## External resources:\\n- https://developer.chrome.com/docs/performance/insights/lcp-breakdown\\n- https://web.dev/articles/lcp\\n- https://web.dev/articles/optimize-lcp"
+}
`;
exports[`McpResponse network pagination > trace summaries > includes the trace summary text and structured data 1`] = `
diff --git a/tests/McpResponse.test.ts b/tests/McpResponse.test.ts
index 6a2b1073..62a358a6 100644
--- a/tests/McpResponse.test.ts
+++ b/tests/McpResponse.test.ts
@@ -969,6 +969,13 @@ describe('McpResponse network pagination', () => {
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
+ const typedStructuredContent = structuredContent as {
+ traceInsight?: string;
+ };
+ assert.ok(
+ typedStructuredContent.traceInsight,
+ 'Expected traceInsight in structuredContent',
+ );
t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -988,6 +995,13 @@ describe('McpResponse network pagination', () => {
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
+ const typedStructuredContent = structuredContent as {
+ error?: string;
+ };
+ assert.ok(
+ typedStructuredContent.error,
+ 'Expected error in structuredContent',
+ );
t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
diff --git a/tests/daemon/client.test.ts b/tests/daemon/client.test.ts
index cf4804f8..59ead720 100644
--- a/tests/daemon/client.test.ts
+++ b/tests/daemon/client.test.ts
@@ -218,6 +218,24 @@ describe('daemon client', () => {
);
});
+ it('handles JSON response for performance_analyze_insight with traceInsight', async () => {
+ const insightResponse = {
+ structuredContent: {
+ traceInsight: '## Insight Title: LCP breakdown',
+ },
+ content: [
+ {
+ type: 'text' as const,
+ text: '## Insight Title: LCP breakdown',
+ },
+ ],
+ };
+ assert.strictEqual(
+ await handleResponse(insightResponse, 'json'),
+ JSON.stringify(insightResponse.structuredContent),
+ );
+ });
+
it('handles error response when isError is true with json format', async () => {
const errorResponse = {
isError: true,