Chrome DevTools for coding agents
fix: harden third-party tool result serialization (#2892)
Wolfgang Beyer4418a7b3643b
diff --git a/src/McpPage.ts b/src/McpPage.tsindex 98f361d2..8b0d56ea 100644--- a/src/McpPage.ts+++ b/src/McpPage.ts@@ -631,29 +631,23 @@ export class McpPage implements ContextPage {const toolResult = await window.__dtmcp.executeTool(name, args);+ const stashedElements: Element[] = [];+const stashDOMElement = (el: Element) => {- if (!window.__dtmcp) {- window.__dtmcp = {};- }- if (window.__dtmcp.stashedElements === undefined) {- window.__dtmcp.stashedElements = [];- }- window.__dtmcp.stashedElements.push(el);+ stashedElements.push(el);return {- stashedId: `stashed-${window.__dtmcp.stashedElements.length - 1}`,+ stashedId: `stashed-${stashedElements.length - 1}`,};};- const ancestors: unknown[] = [];+ const ancestors = new Set<unknown>();// Recursively walks the tool result:// - Replaces DOM elements with an ID and stashes the DOM element on the window object// - Replaces non-plain objects with a string representation of the object// - Replaces circular references with the string '<Circular reference>'// - Replaces functions with the string '<Function object>'- const processToolResult = (- data: unknown,- parentEl?: unknown,- ): unknown => {+ // - Replaces symbols and bigints with their string representation+ const processToolResult = (data: unknown): unknown => {// 1. Handle DOM Elementsif (data instanceof Element) {return stashDOMElement(data);@@ -661,31 +655,38 @@ export class McpPage implements ContextPage {// 2. Handle Arraysif (Array.isArray(data)) {- return data.map((item: unknown) =>- processToolResult(item, parentEl),- );+ if (ancestors.has(data)) {+ return '<Circular reference>';+ }+ ancestors.add(data);+ try {+ return data.map((item: unknown) => processToolResult(item));+ } finally {+ ancestors.delete(data);+ }}// 3. Handle Objectsif (data !== null && typeof data === 'object') {- while (ancestors.length > 0 && ancestors.at(-1) !== parentEl) {- ancestors.pop();- }- if (ancestors.includes(data)) {- return '<Circular reference>';- }- ancestors.push(data);-+ const proto = Object.getPrototypeOf(data);// If not a plain object, return a string representation of the object- if (Object.getPrototypeOf(data) !== Object.prototype) {- return `<${data.constructor.name} instance>`;+ if (proto !== null && proto !== Object.prototype) {+ return `<${data.constructor?.name || 'Object'} instance>`;}- const processedObj: Record<string, unknown> = {};- for (const [key, value] of Object.entries(data)) {- processedObj[key] = processToolResult(value, data);+ if (ancestors.has(data)) {+ return '<Circular reference>';+ }+ ancestors.add(data);+ try {+ const processedObj: Record<string, unknown> = {};+ for (const [key, value] of Object.entries(data)) {+ processedObj[key] = processToolResult(value);+ }+ return processedObj;+ } finally {+ ancestors.delete(data);}- return processedObj;}// 4. Handle Functions@@ -693,13 +694,27 @@ export class McpPage implements ContextPage {return '<Function object>';}- // 5. Return primitives (strings, numbers, booleans) as-is+ // 5. Handle Symbols and BigInts (not JSON/CDP-by-value serializable)+ if (typeof data === 'symbol') {+ return data.toString();+ }+ if (typeof data === 'bigint') {+ return `${data.toString()}n`;+ }++ // 6. Return primitives (strings, numbers, booleans, undefined, null) as-isreturn data;};+ const processed = processToolResult(toolResult);+ const serialized =+ processed !== undefined ? JSON.stringify(processed) : undefined;+ if (stashedElements.length > 0) {+ window.__dtmcp.stashedElements = stashedElements;+ }return {- result: processToolResult(toolResult),- stashed: window.__dtmcp?.stashedElements?.length ?? 0,+ result: serialized,+ stashed: stashedElements.length,};},toolName,@@ -708,22 +723,30 @@ export class McpPage implements ContextPage {);const elementHandles: ElementHandle[] = [];- for (let i = 0; i < (result.stashed ?? 0); i++) {- const elementHandle = await this.pptrPage.evaluateHandle(index => {- const el = window.__dtmcp?.stashedElements?.[index];- if (!el) {- throw new Error(`Stashed element at index ${index} not found`);+ if (result.stashed > 0) {+ try {+ for (let i = 0; i < result.stashed; i++) {+ const elementHandle = await this.pptrPage.evaluateHandle(index => {+ const el = window.__dtmcp?.stashedElements?.[index];+ if (!el) {+ throw new Error(`Stashed element at index ${index} not found`);+ }+ return el;+ }, i);+ elementHandles.push(elementHandle);+ }+ } finally {+ try {+ await this.pptrPage.evaluate(() => {+ if (window.__dtmcp) {+ window.__dtmcp.stashedElements = undefined;+ }+ });+ } catch (error) {+ logger?.('Failed to clean up stashed elements', error);}- return el;- }, i);- elementHandles.push(elementHandle);- }-- await this.pptrPage.evaluate(() => {- if (window.__dtmcp) {- window.__dtmcp.stashedElements = undefined;}- });+ }if (elementHandles.length) {using stack = new DisposableStack();@@ -780,8 +803,13 @@ export class McpPage implements ContextPage {return node;};- const resultWithUids = recursivelyReplaceStashedElements(result.result);- response.appendResponseLine(JSON.stringify(resultWithUids, null, 2));+ if (result.result !== undefined) {+ const parsedResult: unknown = JSON.parse(result.result);+ const resultWithUids = recursivelyReplaceStashedElements(parsedResult);+ response.appendResponseLine(JSON.stringify(resultWithUids, null, 2));+ } else {+ response.appendResponseLine('Tool returned no result.');+ }}async getElementByUid(uid: string): Promise<ElementHandle<Element>> {diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.tsindex 5ab7b8d7..1e02ba91 100644--- a/tests/McpPage.test.ts+++ b/tests/McpPage.test.ts@@ -16,7 +16,11 @@ import {DevTools, Locator} from '../src/third_party/index.js';import type {JSONSchema7Definition} from '../src/third_party/index.js';import {TextSnapshot} from '../src/TextSnapshot.js';import type {TextSnapshotNode} from '../src/types.js';-import {createMockPuppeteerPage, createMockPuppeteerTarget} from './mocks.js';+import {+ createMockMcpResponse,+ createMockPuppeteerPage,+ createMockPuppeteerTarget,+} from './mocks.js';import {serverHooks} from './server.js';import {getMockRequest, html, withMcpContext} from './utils.js';@@ -1211,4 +1215,44 @@ describe('McpPage', () => {});});});++ describe('executeThirdPartyDeveloperTool()', () => {+ afterEach(() => {+ sinon.restore();+ });++ it('appends "Tool returned no result." and skips cleanup evaluate when the tool returns undefined and stashed is 0', async () => {+ const {mcpPage, pptrPage} = await createMcpPage();+ const response = createMockMcpResponse();+ pptrPage.evaluate.resolves({result: undefined, stashed: 0});++ await mcpPage.executeThirdPartyDeveloperTool('my-tool', {}, response);++ sinon.assert.calledOnce(pptrPage.evaluate);+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ 'Tool returned no result.',+ );+ });++ it('preserves the original evaluateHandle error when stashedElements cleanup fails', async () => {+ const {mcpPage, pptrPage} = await createMcpPage();+ const response = createMockMcpResponse();+ pptrPage.evaluate+ .onFirstCall()+ .resolves({result: '{"stashedId":"stashed-0"}', stashed: 1});+ pptrPage.evaluateHandle.rejects(+ new Error('Execution context was destroyed'),+ );+ pptrPage.evaluate.onSecondCall().rejects(new Error('Target closed'));++ await assert.rejects(+ () => mcpPage.executeThirdPartyDeveloperTool('my-tool', {}, response),+ /Execution context was destroyed/,+ );++ sinon.assert.calledTwice(pptrPage.evaluate);+ sinon.assert.notCalled(response.appendResponseLine);+ });+ });});diff --git a/tests/tools/thirdPartyDeveloper.test.ts b/tests/tools/thirdPartyDeveloper.test.tsindex 7c4b94ce..c80e1a73 100644--- a/tests/tools/thirdPartyDeveloper.test.ts+++ b/tests/tools/thirdPartyDeveloper.test.ts@@ -734,6 +734,82 @@ describe('thirdPartyDeveloperTools', () => {);});+ it('processToolResult handles symbols, bigints, circular arrays, shared references, and null-prototype objects', async () => {+ await withMcpContext(+ async (response, context, args) => {+ const page = await context.newPage();+ response.setPage(page);++ page.thirdPartyDeveloperTools = [+ {+ name: 'test-group',+ description: 'test description',+ tools: [+ {+ name: 'test-tool',+ description: 'test tool description',+ inputSchema: {},+ },+ ],+ },+ ];++ await page.pptrPage.evaluate(() => {+ window.__dtmcp = {+ executeTool: async () => {+ const circularArr: unknown[] = ['item'];+ circularArr.push(circularArr);+ const sharedObj = {sharedKey: 'sharedVal'};+ const nullProtoObj: Record<string, unknown> =+ Object.create(null);+ nullProtoObj.key = 'val';+ const AnonymousClass = (() => class {})();+ return {+ sym: Symbol('test-sym'),+ big: BigInt(42),+ circularArr,+ shared1: sharedObj,+ shared2: sharedObj,+ nullProtoObj,+ anonInstance: new AnonymousClass(),+ };+ },+ };+ });++ await executeThirdPartyDeveloperTool(args).handler(+ {+ params: {+ toolName: 'test-tool',+ params: JSON.stringify({}),+ },+ page,+ },+ response,+ context,+ );+ assert.strictEqual(+ response.responseLines[0],+ JSON.stringify(+ {+ sym: 'Symbol(test-sym)',+ big: '42n',+ circularArr: ['item', '<Circular reference>'],+ shared1: {sharedKey: 'sharedVal'},+ shared2: {sharedKey: 'sharedVal'},+ nullProtoObj: {key: 'val'},+ anonInstance: '<Object instance>',+ },+ null,+ 2,+ ),+ );+ },+ undefined,+ {categoryExperimentalThirdParty: true},+ );+ });+it('stashDOMElement stashes elements and returns UID', async () => {await withMcpContext(async (response, context, args) => {