Skip to content

Chrome DevTools for coding agents

3.7 KB
Raw Blame
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {zod, ajv, type JSONSchema7} from '../third_party/index.js';

import {ToolCategory} from './categories.js';
import {
  definePageTool,
  jsonObjectTransform,
  zodTransform,
  type PageToolFactory,
} from './ToolDefinition.js';

export interface ToolDefinition {
  name: string;
  description: string;
  inputSchema: JSONSchema7;
}

export interface ToolGroup<T extends ToolDefinition> {
  name: string;
  description: string;
  tools: T[];
}

export type ToolGroups = Array<ToolGroup<ToolDefinition>>;

declare global {
  interface Window {
    // eslint-disable-next-line @typescript-eslint/naming-convention
    __dtmcp?: {
      toolGroups?: Array<
        ToolGroup<
          ToolDefinition & {
            execute: (args: Record<string, unknown>) => unknown;
          }
        >
      >;
      stashedElements?: Element[];
      executeTool?: (
        toolName: string,
        args: Record<string, unknown>,
      ) => unknown;
    };
  }
}

export const listThirdPartyDeveloperTools: PageToolFactory = definePageTool(
  () => ({
    name: 'list_3p_developer_tools',
    description: `Lists all third-party developer tools the page exposes for providing runtime information (including debugging WebMCP tools and tools exposed via the 'devtoolstooldiscovery' event).
Third-party developer tools can be called via the 'execute_3p_developer_tool()' MCP tool.
Alternatively, third-party developer tools exposed via the 'devtoolstooldiscovery' event can be executed by calling 'evaluate_script' and adding the
following command to the script:
\`window.__dtmcp.executeTool(toolName, params)\`
This might be helpful when the third-party developer tools return non-serializable values or when composing
third-party developer tools with additional functionality.`,
    annotations: {
      category: ToolCategory.THIRD_PARTY,
      readOnlyHint: true,
    },
    schema: {},
    blockedByDialog: false,
    concurrency: 'page',
    verifyFilesSchema: {},
    handler: async (_request, response) => {
      response.setListThirdPartyDeveloperTools();
    },
  }),
);

export const executeThirdPartyDeveloperTool: PageToolFactory = definePageTool(
  () => ({
    name: 'execute_3p_developer_tool',
    description: `Executes a third-party developer tool exposed by the page.`,
    annotations: {
      category: ToolCategory.THIRD_PARTY,
      readOnlyHint: false,
    },
    schema: {
      toolName: zod.string().describe('The name of the tool to execute'),
      params: zod
        .string()
        .transform(zodTransform(jsonObjectTransform))
        .optional()
        .describe('The JSON-stringified parameters to pass to the tool'),
    },
    blockedByDialog: false,
    concurrency: 'page',
    verifyFilesSchema: {},
    handler: async (request, response) => {
      const toolName = request.params.toolName;
      const params = request.params.params ?? {};

      const toolGroups = await request.page.discoverThirdPartyDeveloperTools();
      let tool;
      if (toolGroups) {
        for (const group of toolGroups) {
          tool = group.tools?.find(t => t.name === toolName);
          if (tool) {
            break;
          }
        }
      }
      if (!tool) {
        throw new Error(`Tool ${toolName} not found`);
      }
      const ajvInstance = new ajv();
      const validate = ajvInstance.compile(tool.inputSchema);
      const valid = validate(params);
      if (!valid) {
        throw new Error(
          `Invalid parameters for tool ${toolName}: ${ajvInstance.errorsText(validate.errors)}`,
        );
      }

      await request.page.executeThirdPartyDeveloperTool(
        toolName,
        params,
        response,
      );
    },
  }),
);