Chrome DevTools for coding agents
fix: discover third-party developer tools in page tools (#3207)
Wolfgang Beyercf33529b969c
diff --git a/src/McpResponse.ts b/src/McpResponse.tsindex 5a22ee6c..3adeb072 100644--- a/src/McpResponse.ts+++ b/src/McpResponse.ts@@ -533,9 +533,6 @@ export class McpResponse implements Response {async #handleSnapshot(context: McpContext,): Promise<SnapshotFormatter | string | undefined> {- if (this.#includePages) {- await context.createPagesSnapshot();- }if (!this.#snapshotParams) {return undefined;}@@ -787,9 +784,16 @@ export class McpResponse implements Response {content: Array<TextContent | ImageContent>;structuredContent: object;}> {- if (this.#attachedWaitForResult?.openedPages && !this.#includePages) {+ if (this.#includePages || this.#attachedWaitForResult?.openedPages) {await context.createPagesSnapshot();}+ if (this.#includePages && (!this.#page || this.#page.isClosed())) {+ try {+ this.#page = context.getSelectedMcpPage();+ } catch {+ this.#page = undefined;+ }+ }const [snapshot,diff --git a/tests/McpResponse.test.ts b/tests/McpResponse.test.tsindex e1493749..eb151ca1 100644--- a/tests/McpResponse.test.ts+++ b/tests/McpResponse.test.ts@@ -56,6 +56,8 @@ import {createMockObjectInfo,createMockParsedArguments,createMockPuppeteerTarget,+ createMockWebMCPTool,+ type MockMcpPage,} from './mocks.js';import {createTempDir,@@ -1116,68 +1118,46 @@ describe('lighthouse', () => {});describe('third-party developer tools', () => {- function stubToolDiscovery(page: object) {- // @ts-expect-error Internal API- const client = page._client();- const originalSend = client.send.bind(client);- sinon- .stub(client, 'send')- .callsFake(async (method: string, params?: Record<string, unknown>) => {- if (method === 'DOMDebugger.getEventListeners') {- return {- listeners: [- {- type: 'devtoolstooldiscovery',- useCapture: false,- passive: false,- once: false,- scriptId: '0',- lineNumber: 0,- columnNumber: 0,- },- ],- };- }- return originalSend(method, params);- });- }+ afterEach(() => {+ sinon.restore();+ });it('lists third-party developer tools', async t => {- await withMcpContext(- async (response, context) => {- const mcpPage = context.getSelectedMcpPage();- stubToolDiscovery(mcpPage.pptrPage);- sinon.stub(mcpPage.pptrPage, 'evaluate').resolves([+ const args = createMockParsedArguments({+ categoryExperimentalThirdParty: true,+ });+ const page = createMockMcpPage();+ page.getToolGroups.resolves([+ {+ name: 'My Tool Group',+ description: 'A group of tools',+ tools: [{- name: 'My Tool Group',- description: 'A group of tools',- tools: [- {- name: 'myTool',- description: 'Does something',- inputSchema: {- type: 'object',- properties: {- foo: {type: 'string'},- },- },+ name: 'myTool',+ description: 'Does something',+ inputSchema: {+ type: 'object',+ properties: {+ foo: {type: 'string'},},- ],+ },},- ]);- response.setListThirdPartyDeveloperTools();- const {content, structuredContent} = await response.handle(context);- const responseText = getTextContent(content[0]);- t.assert.snapshot(responseText);- assert.ok(- responseText.includes('inputSchema={"type":"object"'),- 'Response should include inputSchema',- );- t.assert.snapshot(stabilizeStructuredContent(structuredContent));+ ],},- undefined,- {categoryExperimentalThirdParty: true},+ ]);+ const context = createMockMcpContext({selectedPage: page});+ const response = new McpResponse(args);+ response.setPage(page);+ response.setListThirdPartyDeveloperTools();++ const {content, structuredContent} = await response.handle(context);+ const responseText = getTextContent(content[0]);+ t.assert.snapshot(responseText);+ assert.ok(+ responseText.includes('inputSchema={"type":"object"'),+ 'Response should include inputSchema',);+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));});it('lists no third-party developer tools if there are none', async t => {@@ -1202,50 +1182,45 @@ describe('third-party developer tools', () => {response: McpResponse,context: McpContext,args: ParsedArguments,+ page: MockMcpPage,) => Promise<void>,toolName: string,) {- await withMcpContext(- async (response, context, args) => {- const mcpPage = context.getSelectedMcpPage();- stubToolDiscovery(mcpPage.pptrPage);-- const initScript = `- window.__dtmcp = {- toolGroup: {- name: 'Tool group name',- description: 'Test tools',- tools: [- {- name: '3pDeveloperTool',- description: 'A test tool',- inputSchema: {- type: 'object',- properties: {},- },- execute: () => 'result',- },- ],+ const args = createMockParsedArguments({+ categoryExperimentalThirdParty: true,+ });+ const page = createMockMcpPage();+ Object.defineProperty(page, 'id', {value: 1});+ page.url.returns('about:blank');+ page.getToolGroups.resolves([+ {+ name: 'Tool group name',+ description: 'Test tools',+ tools: [+ {+ name: '3pDeveloperTool',+ description: 'A test tool',+ inputSchema: {+ type: 'object',+ properties: {},},- };- window.addEventListener('devtoolstooldiscovery', (e) => {- e.respondWith(window.__dtmcp?.toolGroup);- });- `;- await mcpPage.pptrPage.evaluateOnNewDocument(initScript);- await mcpPage.pptrPage.evaluate(initScript);-- await handlerAction(response, context, args);-- const {content} = await response.handle(context);- const responseText = getTextContent(content[0]);- assert.ok(- responseText.includes('3pDeveloperTool'),- `Should include third-party developer tool name in the ${toolName} response`,- );+ },+ ],},- undefined,- {categoryExperimentalThirdParty: true},+ ]);+ const context = createMockMcpContext({selectedPage: page});+ context.getPages.returns([page]);+ context.getPageById.withArgs(1).returns(page);+ context.newPage.resolves(page);+ const response = new McpResponse(args);++ await handlerAction(response, context, args, page);++ const {content} = await response.handle(context);+ const responseText = getTextContent(content[0]);+ assert.ok(+ responseText.includes('3pDeveloperTool'),+ `Should include third-party developer tool name in the ${toolName} response`,);}@@ -1261,9 +1236,12 @@ describe('third-party developer tools', () => {it('includes third-party developer tools in select_page response', async () => {await testIncludesThirdPartyDeveloperTools(- async (response, context, args) => {- const pageId = context.getSelectedMcpPage().id;- await selectPage(args).handler({params: {pageId}}, response, context);+ async (response, context, args, page) => {+ await selectPage(args).handler(+ {params: {pageId: page.id}},+ response,+ context,+ );},'select_page',);@@ -1271,9 +1249,12 @@ describe('third-party developer tools', () => {it('includes third-party developer tools in close_page response', async () => {await testIncludesThirdPartyDeveloperTools(- async (response, context, args) => {- const pageId = context.getSelectedMcpPage().id;- await closePage(args).handler({params: {pageId}}, response, context);+ async (response, context, args, page) => {+ await closePage(args).handler(+ {params: {pageId: page.id}},+ response,+ context,+ );},'close_page',);@@ -1281,11 +1262,12 @@ describe('third-party developer tools', () => {it('includes third-party developer tools in navigate_page response', async () => {await testIncludesThirdPartyDeveloperTools(- async (response, context, args) => {+ async (response, context, args, page) => {+ response.setPage(page);await navigatePage(args).handler({params: {type: 'url', url: 'about:blank'},- page: context.getSelectedMcpPage(),+ page,},response,context,@@ -1298,9 +1280,6 @@ describe('third-party developer tools', () => {it('includes third-party developer tools in new_page response', async () => {await testIncludesThirdPartyDeveloperTools(async (response, context, args) => {- // Workaround to ensure the test environment's new page contain third-party developer tools- sinon.stub(context, 'newPage').resolves(context.getSelectedMcpPage());-await newPage(args).handler({params: {url: 'about:blank'},@@ -1315,6 +1294,10 @@ describe('third-party developer tools', () => {});describe('webmcp', () => {+ afterEach(() => {+ sinon.restore();+ });+async function testIncludesWebmcpTools(t: it.TestContext,parseArguments: Partial<ParsedArguments>,@@ -1322,35 +1305,37 @@ describe('webmcp', () => {response: McpResponse,context: McpContext,args: ParsedArguments,+ page: MockMcpPage,) => Promise<void>,) {- await withMcpContext(- async (response, context, args) => {- response.setListWebMcpTools();+ const args = createMockParsedArguments(parseArguments);+ const page = createMockMcpPage();+ Object.defineProperty(page, 'id', {value: 1});+ page.url.returns('about:blank');+ page.getTitle.returns('My test page');+ page.getWebMcpTools.returns([+ createMockWebMCPTool({+ name: 'test_tool',+ description: 'A test tool',+ inputSchema: {+ type: 'object',+ properties: {},+ required: [],+ },+ }),+ ]);+ const context = createMockMcpContext({selectedPage: page});+ context.getPages.returns([page]);+ context.isPageSelected.returns(true);+ context.getPageById.withArgs(1).returns(page);+ const response = new McpResponse(args);- await handlerAction(response, context, args);+ await handlerAction(response, context, args, page);- const page = context.getSelectedMcpPage().pptrPage;- const {resolve, promise} = Promise.withResolvers();- page.webmcp.once('toolsadded', () => {- resolve(undefined);- });- await page.setContent(- html`<form- toolname="test_tool"- tooldescription="A test tool"- ></form>`,- );- await promise;-- const {content, structuredContent} = await response.handle(context);- assert.ok(getTextContent(content[0]));- t.assert.snapshot(getTextContent(content[0]));- t.assert.snapshot(stabilizeStructuredContent(structuredContent));- },- {args: ['--enable-features=WebMCP,DevToolsWebMCPSupport']},- parseArguments,- );+ const {content, structuredContent} = await response.handle(context);+ assert.ok(getTextContent(content[0]));+ t.assert.snapshot(getTextContent(content[0]));+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));}it('includes webmcp tools in list_pages response', async t => {@@ -1367,9 +1352,12 @@ describe('webmcp', () => {await testIncludesWebmcpTools(t,{categoryExperimentalWebmcp: true},- async (response, context, args) => {- const pageId = context.getSelectedMcpPage().id;- await selectPage(args).handler({params: {pageId}}, response, context);+ async (response, context, args, page) => {+ await selectPage(args).handler(+ {params: {pageId: page.id}},+ response,+ context,+ );},);});@@ -1378,11 +1366,12 @@ describe('webmcp', () => {await testIncludesWebmcpTools(t,{categoryExperimentalWebmcp: true},- async (response, context, args) => {+ async (response, context, args, page) => {+ response.setPage(page);await navigatePage(args).handler({params: {type: 'url', url: 'about:blank'},- page: context.getSelectedMcpPage(),+ page,},response,context,@@ -1392,28 +1381,31 @@ describe('webmcp', () => {});it('list no webmcp tools if there are none', async t => {- await withMcpContext(- async (response, context) => {- response.setListWebMcpTools();- const {content, structuredContent} = await response.handle(context);- assert.ok(getTextContent(content[0]));- t.assert.snapshot(getTextContent(content[0]));- t.assert.snapshot(stabilizeStructuredContent(structuredContent));- },- {args: ['--enable-features=WebMCP,DevToolsWebMCPSupport']},- {categoryExperimentalWebmcp: true},- );+ const args = createMockParsedArguments({+ categoryExperimentalWebmcp: true,+ });+ const page = createMockMcpPage();+ const context = createMockMcpContext({selectedPage: page});+ const response = new McpResponse(args);+ response.setPage(page);+ response.setListWebMcpTools();++ const {content, structuredContent} = await response.handle(context);+ assert.ok(getTextContent(content[0]));+ t.assert.snapshot(getTextContent(content[0]));+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));});it('list no webmcp tools if experimentalWebmcp is false', async t => {await testIncludesWebmcpTools(t,{categoryExperimentalWebmcp: false},- async (response, context, args) => {+ async (response, context, args, page) => {+ response.setPage(page);await navigatePage(args).handler({params: {type: 'url', url: 'about:blank'},- page: context.getSelectedMcpPage(),+ page,},response,context,diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.tsindex 929e2791..8b261b4c 100644--- a/tests/tools/pages.test.ts+++ b/tests/tools/pages.test.ts@@ -232,6 +232,29 @@ describe('pages', () => {});});describe('new_page', () => {+ it('sets the selected page and response flags', async () => {+ const {page, context, response, args} = createHandlerMocks();+ context.newPage.resolves(page);++ await newPage(args).handler(+ {params: {url: 'https://example.com'}},+ response,+ context,+ );++ sinon.assert.calledOnceWithExactly(context.newPage, undefined, undefined);+ sinon.assert.calledOnceWithExactly(+ page.pptrPage.goto,+ 'https://example.com',+ {+ timeout: undefined,+ },+ );+ sinon.assert.calledOnceWithExactly(response.setIncludePages, true);+ sinon.assert.calledOnceWithExactly(+ response.setListThirdPartyDeveloperTools,+ );+ });it('create a page', async () => {await withMcpContext(async (response, context, args) => {assert.strictEqual(@@ -746,6 +769,20 @@ describe('pages', () => {});});describe('select_page', () => {+ it('sets the selected page and response flags', async () => {+ const {page, context, response, args} = createHandlerMocks();+ context.getPageById.withArgs(1).returns(page);++ await selectPage(args).handler({params: {pageId: 1}}, response, context);++ sinon.assert.calledOnceWithExactly(page.init);+ sinon.assert.calledOnceWithExactly(context.selectPage, page);+ sinon.assert.calledOnceWithExactly(response.setIncludePages, true);+ sinon.assert.calledOnceWithExactly(+ response.setListThirdPartyDeveloperTools,+ );+ sinon.assert.calledOnceWithExactly(response.setListWebMcpTools);+ });it('selects a page', async () => {await withMcpContext(async (response, context, args) => {await context.newPage();