Skip to content

Commit 2d2b4ba

Browse files
author
Piotr Paulski
committed
TOONify test response, not structuredContent.
1 parent 5860be9 commit 2d2b4ba

4 files changed

Lines changed: 62 additions & 49 deletions

File tree

src/McpResponse.ts

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {McpContext} from './McpContext.js';
1717
import type {McpPage} from './McpPage.js';
1818
import {UncaughtError} from './PageCollector.js';
1919
import {TextSnapshot} from './TextSnapshot.js';
20-
import {DevTools, type Protocol} from './third_party/index.js';
20+
import {DevTools, toonEncode, type Protocol} from './third_party/index.js';
2121
import type {
2222
ConsoleMessage,
2323
ImageContent,
@@ -456,6 +456,7 @@ export class McpResponse implements Response {
456456
async handle(
457457
toolName: string,
458458
context: McpContext,
459+
useToon = false,
459460
): Promise<{
460461
content: Array<TextContent | ImageContent>;
461462
structuredContent: object;
@@ -674,20 +675,25 @@ export class McpResponse implements Response {
674675
}
675676
}
676677

677-
return this.format(toolName, context, {
678-
detailedConsoleMessage,
679-
consoleMessages,
680-
snapshot,
681-
detailedNetworkRequest,
682-
networkRequests,
683-
traceInsight: this.#attachedTraceInsight,
684-
traceSummary: this.#attachedTraceSummary,
685-
extensions,
686-
lighthouseResult: this.#attachedLighthouseResult,
687-
thirdPartyDeveloperTools,
688-
webmcpTools,
689-
errorMessage: this.#error?.message,
690-
});
678+
return this.format(
679+
toolName,
680+
context,
681+
{
682+
detailedConsoleMessage,
683+
consoleMessages,
684+
snapshot,
685+
detailedNetworkRequest,
686+
networkRequests,
687+
traceInsight: this.#attachedTraceInsight,
688+
traceSummary: this.#attachedTraceSummary,
689+
extensions,
690+
lighthouseResult: this.#attachedLighthouseResult,
691+
thirdPartyDeveloperTools,
692+
webmcpTools,
693+
errorMessage: this.#error?.message,
694+
},
695+
useToon,
696+
);
691697
}
692698

693699
format(
@@ -707,6 +713,7 @@ export class McpResponse implements Response {
707713
webmcpTools?: WebMCPTool[];
708714
errorMessage?: string;
709715
},
716+
useToon: boolean,
710717
): {content: Array<TextContent | ImageContent>; structuredContent: object} {
711718
const structuredContent: {
712719
snapshot?: object;
@@ -953,9 +960,13 @@ Call ${handleDialog.name} to handle it before continuing.`);
953960
response.push(`Saved snapshot to ${data.snapshot}.`);
954961
structuredContent.snapshotFilePath = data.snapshot;
955962
} else {
956-
response.push('## Latest page snapshot');
957-
response.push(data.snapshot.toString());
958963
structuredContent.snapshot = data.snapshot.toJSON();
964+
response.push('## Latest page snapshot');
965+
response.push(
966+
useToon
967+
? toonEncode(structuredContent.snapshot)
968+
: data.snapshot.toString(),
969+
);
959970
}
960971
}
961972

@@ -988,8 +999,12 @@ Call ${handleDialog.name} to handle it before continuing.`);
988999
const paginatedRecord = Object.fromEntries(paginationData.items);
9891000
const formatter = new HeapSnapshotFormatter(paginatedRecord);
9901001

991-
response.push(formatter.toString());
9921002
structuredContent.heapSnapshotData = formatter.toJSON();
1003+
response.push(
1004+
useToon
1005+
? toonEncode(structuredContent.heapSnapshotData)
1006+
: formatter.toString(),
1007+
);
9931008
}
9941009
const nodes = this.#heapSnapshotOptions.nodes;
9951010
if (nodes) {
@@ -1109,11 +1124,14 @@ Call ${handleDialog.name} to handle it before continuing.`);
11091124
structuredContent.pagination = paginationData.pagination;
11101125
response.push(...paginationData.info);
11111126
if (data.networkRequests) {
1112-
structuredContent.networkRequests = [];
1113-
for (const formatter of paginationData.items) {
1114-
response.push(formatter.toString());
1115-
structuredContent.networkRequests.push(formatter.toJSON());
1116-
}
1127+
structuredContent.networkRequests = paginationData.items.map(i =>
1128+
i.toJSON(),
1129+
);
1130+
response.push(
1131+
...(useToon
1132+
? [toonEncode(structuredContent.networkRequests)]
1133+
: paginationData.items.map(i => i.toString())),
1134+
);
11171135
}
11181136
} else {
11191137
response.push('No requests found.');
@@ -1131,11 +1149,15 @@ Call ${handleDialog.name} to handle it before continuing.`);
11311149
this.#consoleDataOptions.pagination,
11321150
);
11331151
structuredContent.pagination = paginationData.pagination;
1134-
response.push(...paginationData.info);
1135-
response.push(...paginationData.items.map(item => item.toString()));
11361152
structuredContent.consoleMessages = paginationData.items.map(item =>
11371153
item.toJSON(),
11381154
);
1155+
response.push(...paginationData.info);
1156+
if (useToon) {
1157+
response.push(toonEncode(structuredContent.consoleMessages));
1158+
} else {
1159+
response.push(...paginationData.items.map(item => item.toString()));
1160+
}
11391161
} else {
11401162
response.push('<no console messages found>');
11411163
}

src/ToolHandler.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {SlimMcpResponse} from './SlimMcpResponse.js';
1313
import {ClearcutLogger} from './telemetry/ClearcutLogger.js';
1414
import {bucketizeLatency} from './telemetry/transformation.js';
1515
import type {CallToolResult} from './third_party/index.js';
16-
import {zod, toonEncode} from './third_party/index.js';
16+
import {zod} from './third_party/index.js';
1717
import type {ToolCategory} from './tools/categories.js';
1818
import {labels, OFF_BY_DEFAULT_CATEGORIES} from './tools/categories.js';
1919
import type {DefinedPageTool, ToolDefinition} from './tools/ToolDefinition.js';
@@ -256,6 +256,7 @@ export class ToolHandler {
256256
const {content, structuredContent} = await response.handle(
257257
this.tool.name,
258258
context,
259+
this.serverArgs.experimentalToonFormat ?? false,
259260
);
260261
const result: CallToolResult & {
261262
structuredContent?: Record<string, unknown>;
@@ -267,11 +268,7 @@ export class ToolHandler {
267268
}
268269
success = true;
269270
if (this.serverArgs.experimentalStructuredContent) {
270-
if (this.serverArgs.experimentalStructuredContentFormat === 'toon') {
271-
result.structuredContent = { format: 'toon', data: toonEncode(structuredContent)};
272-
} else {
273-
result.structuredContent = structuredContent as Record<string, unknown>;
274-
}
271+
result.structuredContent = structuredContent as Record<string, unknown>;
275272
}
276273
return result;
277274
} catch (err) {

src/bin/chrome-devtools-mcp-cli-options.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ export const cliOptions = {
169169
type: 'boolean',
170170
describe: 'Whether to output structured formatted content.',
171171
},
172-
experimentalStructuredContentFormat: {
173-
type: 'string',
174-
describe: 'The format of the structured content. Defaults to "json".',
175-
choices: ['toon', 'json'] as const,
172+
experimentalToonFormat: {
173+
type: 'boolean',
174+
describe:
175+
'Whether to format structured data in text response using Token-Oriented Object Notation. Defaults to false which represents the embedded content as formatted JSON instead.',
176176
hidden: true,
177177
},
178178
experimentalIncludeAllPages: {
@@ -316,20 +316,6 @@ export function parseArguments(
316316
);
317317
args.usageStatistics = false;
318318
}
319-
if (args.experimentalStructuredContentFormat && args.experimentalStructuredContent === undefined) {
320-
args.experimentalStructuredContent = true;
321-
}
322-
if (args.experimentalStructuredContentFormat === undefined && args.experimentalStructuredContent) {
323-
args.experimentalStructuredContentFormat = 'json';
324-
}
325-
})
326-
.check(args => {
327-
if (args.experimentalStructuredContentFormat && !args.experimentalStructuredContent) {
328-
throw new Error(
329-
'The --experimentalStructuredContentFormat option can only be used when --experimentalStructuredContent is enabled.'
330-
);
331-
}
332-
return true;
333319
})
334320
.example([
335321
[

src/telemetry/flag_usage_metrics.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,5 +295,13 @@
295295
{
296296
"name": "category_experimental_third_party",
297297
"flagType": "boolean"
298+
},
299+
{
300+
"name": "experimental_toon_format_present",
301+
"flagType": "boolean"
302+
},
303+
{
304+
"name": "experimental_toon_format",
305+
"flagType": "boolean"
298306
}
299307
]

0 commit comments

Comments
 (0)