Skip to content

Commit c37fe6f

Browse files
author
Piotr Paulski
committed
TOONify test response, not structuredContent.
1 parent 0b0fc3a commit c37fe6f

3 files changed

Lines changed: 20 additions & 31 deletions

File tree

src/McpResponse.ts

Lines changed: 15 additions & 8 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: boolean = false,
459460
): Promise<{
460461
content: Array<TextContent | ImageContent>;
461462
structuredContent: object;
@@ -687,7 +688,7 @@ export class McpResponse implements Response {
687688
thirdPartyDeveloperTools,
688689
webmcpTools,
689690
errorMessage: this.#error?.message,
690-
});
691+
}, useToon);
691692
}
692693

693694
format(
@@ -707,6 +708,7 @@ export class McpResponse implements Response {
707708
webmcpTools?: WebMCPTool[];
708709
errorMessage?: string;
709710
},
711+
useToon: boolean,
710712
): {content: Array<TextContent | ImageContent>; structuredContent: object} {
711713
const structuredContent: {
712714
snapshot?: object;
@@ -953,9 +955,9 @@ Call ${handleDialog.name} to handle it before continuing.`);
953955
response.push(`Saved snapshot to ${data.snapshot}.`);
954956
structuredContent.snapshotFilePath = data.snapshot;
955957
} else {
956-
response.push('## Latest page snapshot');
957-
response.push(data.snapshot.toString());
958958
structuredContent.snapshot = data.snapshot.toJSON();
959+
response.push('## Latest page snapshot');
960+
response.push(useToon ? toonEncode(structuredContent.snapshot) : data.snapshot.toString());
959961
}
960962
}
961963

@@ -988,8 +990,8 @@ Call ${handleDialog.name} to handle it before continuing.`);
988990
const paginatedRecord = Object.fromEntries(paginationData.items);
989991
const formatter = new HeapSnapshotFormatter(paginatedRecord);
990992

991-
response.push(formatter.toString());
992993
structuredContent.heapSnapshotData = formatter.toJSON();
994+
response.push(useToon ? toonEncode(structuredContent.heapSnapshotData) : formatter.toString());
993995
}
994996
const nodes = this.#heapSnapshotOptions.nodes;
995997
if (nodes) {
@@ -1111,9 +1113,10 @@ Call ${handleDialog.name} to handle it before continuing.`);
11111113
if (data.networkRequests) {
11121114
structuredContent.networkRequests = [];
11131115
for (const formatter of paginationData.items) {
1114-
response.push(formatter.toString());
11151116
structuredContent.networkRequests.push(formatter.toJSON());
1117+
if (!useToon) response.push(formatter.toString());
11161118
}
1119+
if (useToon) response.push(toonEncode(structuredContent.networkRequests));
11171120
}
11181121
} else {
11191122
response.push('No requests found.');
@@ -1131,11 +1134,15 @@ Call ${handleDialog.name} to handle it before continuing.`);
11311134
this.#consoleDataOptions.pagination,
11321135
);
11331136
structuredContent.pagination = paginationData.pagination;
1134-
response.push(...paginationData.info);
1135-
response.push(...paginationData.items.map(item => item.toString()));
11361137
structuredContent.consoleMessages = paginationData.items.map(item =>
11371138
item.toJSON(),
11381139
);
1140+
response.push(...paginationData.info);
1141+
if (useToon) {
1142+
response.push(toonEncode(structuredContent.consoleMessages));
1143+
} else {
1144+
response.push(...paginationData.items.map(item => item.toString()));
1145+
}
11391146
} else {
11401147
response.push('<no console messages found>');
11411148
}

src/ToolHandler.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ 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: '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.',
176175
hidden: true,
177176
},
178177
experimentalIncludeAllPages: {
@@ -316,20 +315,6 @@ export function parseArguments(
316315
);
317316
args.usageStatistics = false;
318317
}
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;
333318
})
334319
.example([
335320
[

0 commit comments

Comments
 (0)