@@ -13,6 +13,14 @@ import {describe, it} from 'node:test';
1313import sinon from 'sinon' ;
1414
1515import type { ParsedArguments } from '../src/bin/chrome-devtools-mcp-cli-options.js' ;
16+ import type { McpContext } from '../src/McpContext.js' ;
17+ import type { McpResponse } from '../src/McpResponse.js' ;
18+ import {
19+ closePage ,
20+ listPages ,
21+ navigatePage ,
22+ selectPage ,
23+ } from '../src/tools/pages.js' ;
1624import type { InsightName } from '../src/trace-processing/parse.js' ;
1725import {
1826 parseRawTraceBuffer ,
@@ -1091,4 +1099,93 @@ describe('inPage tools', () => {
10911099 { categoryInPageTools : true } as ParsedArguments ,
10921100 ) ;
10931101 } ) ;
1102+
1103+ async function testIncludesInPageTools (
1104+ handlerAction : (
1105+ response : McpResponse ,
1106+ context : McpContext ,
1107+ ) => Promise < void > ,
1108+ toolName : string ,
1109+ ) {
1110+ await withMcpContext (
1111+ async ( response , context ) => {
1112+ const mcpPage = context . getSelectedMcpPage ( ) ;
1113+ stubToolDiscovery ( mcpPage . pptrPage ) ;
1114+
1115+ const initScript = `
1116+ window.__dtmcp = {
1117+ toolGroup: {
1118+ name: 'In-Page group',
1119+ description: 'Test tools',
1120+ tools: [
1121+ {
1122+ name: 'inPageTool',
1123+ description: 'A test tool',
1124+ inputSchema: {
1125+ type: 'object',
1126+ properties: {},
1127+ },
1128+ execute: () => 'result',
1129+ },
1130+ ],
1131+ },
1132+ };
1133+ window.addEventListener('devtoolstooldiscovery', (e) => {
1134+ e.respondWith(window.__dtmcp?.toolGroup);
1135+ });
1136+ ` ;
1137+ await mcpPage . pptrPage . evaluateOnNewDocument ( initScript ) ;
1138+ await mcpPage . pptrPage . evaluate ( initScript ) ;
1139+
1140+ await handlerAction ( response , context ) ;
1141+
1142+ const { content} = await response . handle ( toolName , context ) ;
1143+ const responseText = getTextContent ( content [ 0 ] ) ;
1144+ assert . ok (
1145+ responseText . includes ( 'inPageTool' ) ,
1146+ `Should include in-page tool name in the ${ toolName } response` ,
1147+ ) ;
1148+ } ,
1149+ undefined ,
1150+ { categoryInPageTools : true } as ParsedArguments ,
1151+ ) ;
1152+ }
1153+
1154+ it ( 'includes in-page tools in list_pages response' , async ( ) => {
1155+ await testIncludesInPageTools ( async ( response , context ) => {
1156+ const listPagesDef = listPages ( {
1157+ categoryInPageTools : true ,
1158+ } as ParsedArguments ) ;
1159+ await listPagesDef . handler ( { params : { } } , response , context ) ;
1160+ } , 'list_pages' ) ;
1161+ } ) ;
1162+
1163+ it ( 'includes in-page tools in select_page response' , async ( ) => {
1164+ await testIncludesInPageTools ( async ( response , context ) => {
1165+ const pageId =
1166+ context . getPageId ( context . getSelectedMcpPage ( ) . pptrPage ) ?? 1 ;
1167+ await selectPage . handler ( { params : { pageId} } , response , context ) ;
1168+ } , 'select_page' ) ;
1169+ } ) ;
1170+
1171+ it ( 'includes in-page tools in close_page response' , async ( ) => {
1172+ await testIncludesInPageTools ( async ( response , context ) => {
1173+ const pageId =
1174+ context . getPageId ( context . getSelectedMcpPage ( ) . pptrPage ) ?? 1 ;
1175+ await closePage . handler ( { params : { pageId} } , response , context ) ;
1176+ } , 'close_page' ) ;
1177+ } ) ;
1178+
1179+ it ( 'includes in-page tools in navigate_page response' , async ( ) => {
1180+ await testIncludesInPageTools ( async ( response , context ) => {
1181+ await navigatePage . handler (
1182+ {
1183+ params : { type : 'url' , url : 'about:blank' } ,
1184+ page : context . getSelectedMcpPage ( ) ,
1185+ } ,
1186+ response ,
1187+ context ,
1188+ ) ;
1189+ } , 'navigate_page' ) ;
1190+ } ) ;
10941191} ) ;
0 commit comments