豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions scripts/generate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import type {Tool} from '@modelcontextprotocol/sdk/types.js';
import {get_encoding} from 'tiktoken';

import {cliOptions} from '../build/src/cli.js';
import type {ParsedArguments} from '../build/src/cli.js';
import {ToolCategory, labels} from '../build/src/tools/categories.js';
import {tools as slimTools} from '../build/src/tools/slim/tools.js';
import {tools} from '../build/src/tools/tools.js';
import {createTools} from '../build/src/tools/tools.js';

const OUTPUT_PATH = './docs/tool-reference.md';
const SLIM_OUTPUT_PATH = './docs/slim-tool-reference.md';
Expand Down Expand Up @@ -504,7 +504,7 @@ async function generateToolDocumentation(): Promise<void> {

{
const {toolsWithAnnotations, categories, sortedCategories} =
getToolsAndCategories(tools);
getToolsAndCategories(createTools({slim: false} as ParsedArguments));
await generateReference(
'Chrome DevTools MCP Tool Reference',
OUTPUT_PATH,
Expand All @@ -521,7 +521,7 @@ async function generateToolDocumentation(): Promise<void> {

{
const {toolsWithAnnotations, categories, sortedCategories} =
getToolsAndCategories(slimTools);
getToolsAndCategories(createTools({slim: true} as ParsedArguments));
await generateReference(
'Chrome DevTools MCP Slim Tool Reference',
SLIM_OUTPUT_PATH,
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ export const cliOptions = {
},
} satisfies Record<string, YargsOptions>;

export type ParsedArguments = ReturnType<typeof parseArguments>;

export function parseArguments(version: string, argv = process.argv) {
const yargsInstance = yargs(hideBin(argv))
.scriptName('npx chrome-devtools-mcp@latest')
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import {
SetLevelRequestSchema,
} from './third_party/index.js';
import {ToolCategory} from './tools/categories.js';
import {tools as slimTools} from './tools/slim/tools.js';
import type {ToolDefinition} from './tools/ToolDefinition.js';
import {tools} from './tools/tools.js';
import {createTools} from './tools/tools.js';
import {VERSION} from './version.js';

export const args = parseArguments(VERSION);
Expand Down Expand Up @@ -256,7 +255,8 @@ function registerTool(tool: ToolDefinition): void {
);
}

for (const tool of args.slim ? slimTools : tools) {
const tools = createTools(args);
Comment thread
nroscino marked this conversation as resolved.
for (const tool of tools) {
registerTool(tool);
}

Expand Down
15 changes: 15 additions & 0 deletions src/tools/ToolDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type {ParsedArguments} from '../cli.js';
import type {TextSnapshotNode, GeolocationOptions} from '../McpContext.js';
import {zod} from '../third_party/index.js';
import type {
Expand Down Expand Up @@ -157,6 +158,20 @@ export type Context = Readonly<{

export function defineTool<Schema extends zod.ZodRawShape>(
definition: ToolDefinition<Schema>,
): ToolDefinition<Schema>;

export function defineTool<
Schema extends zod.ZodRawShape,
Args extends ParsedArguments = ParsedArguments,
>(
definition: (args: Args) => ToolDefinition<Schema>,
): (args: Args) => ToolDefinition<Schema>;

export function defineTool<
Schema extends zod.ZodRawShape,
Args extends ParsedArguments = ParsedArguments,
>(
definition: ToolDefinition<Schema> | ((args: Args) => ToolDefinition<Schema>),
) {
return definition;
}
Expand Down
3 changes: 0 additions & 3 deletions src/tools/slim/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import type {Dialog} from '../../third_party/index.js';
import {zod} from '../../third_party/index.js';
import {ToolCategory} from '../categories.js';
import type {ToolDefinition} from '../ToolDefinition.js';
import {defineTool} from '../ToolDefinition.js';

export const screenshot = defineTool({
Expand Down Expand Up @@ -86,5 +85,3 @@ export const evaluate = defineTool({
}
},
});

export const tools = [screenshot, evaluate, navigate] as ToolDefinition[];
53 changes: 35 additions & 18 deletions src/tools/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type {ParsedArguments} from '../cli.js';

import * as consoleTools from './console.js';
import * as emulationTools from './emulation.js';
import * as extensionTools from './extensions.js';
Expand All @@ -15,26 +17,41 @@ import * as performanceTools from './performance.js';
import * as screencastTools from './screencast.js';
import * as screenshotTools from './screenshot.js';
import * as scriptTools from './script.js';
import * as slimTools from './slim/tools.js';
import * as snapshotTools from './snapshot.js';
import type {ToolDefinition} from './ToolDefinition.js';

const tools = [
...Object.values(consoleTools),
...Object.values(emulationTools),
...Object.values(extensionTools),
...Object.values(inputTools),
...Object.values(memoryTools),
...Object.values(networkTools),
...Object.values(pagesTools),
...Object.values(performanceTools),
...Object.values(screencastTools),
...Object.values(screenshotTools),
...Object.values(scriptTools),
...Object.values(snapshotTools),
] as ToolDefinition[];
export const createTools = (args: ParsedArguments) => {
const rawTools = args.slim
? Object.values(slimTools)
: [
...Object.values(consoleTools),
...Object.values(emulationTools),
...Object.values(extensionTools),
...Object.values(inputTools),
...Object.values(memoryTools),
...Object.values(networkTools),
...Object.values(pagesTools),
...Object.values(performanceTools),
...Object.values(screencastTools),
...Object.values(screenshotTools),
...Object.values(scriptTools),
...Object.values(snapshotTools),
];

const tools: ToolDefinition[] = [];
for (const tool of rawTools) {
if (typeof tool === 'function') {
// @ts-expect-error none of the tools for now implement the function type tool has type "never"
tools.push(tool(args) as ToolDefinition);
} else {
tools.push(tool as ToolDefinition);
}
}

tools.sort((a, b) => {
return a.name.localeCompare(b.name);
});
tools.sort((a, b) => {
return a.name.localeCompare(b.name);
});

export {tools};
return tools;
};
29 changes: 24 additions & 5 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,35 @@ describe('e2e', () => {
const files = fs.readdirSync('build/src/tools');
const definedNames = [];
for (const file of files) {
if (file === 'ToolDefinition.js' || file === 'slim') {
if (
file === 'ToolDefinition.js' ||
file === 'tools.js' ||
file === 'slim'
) {
continue;
}
const fileTools = await import(`../src/tools/${file}`);
for (const maybeTool of Object.values<ToolDefinition>(fileTools)) {
if ('name' in maybeTool) {
if (maybeTool.annotations?.conditions) {
for (const maybeTool of Object.values<unknown>(fileTools)) {
if (typeof maybeTool === 'function') {
const tool = (maybeTool as (val: boolean) => ToolDefinition)(false);
if (tool && typeof tool === 'object' && 'name' in tool) {
if (tool.annotations?.conditions) {
continue;
}
definedNames.push(tool.name);
}
continue;
}
if (
typeof maybeTool === 'object' &&
maybeTool !== null &&
'name' in maybeTool
) {
const tool = maybeTool as ToolDefinition;
if (tool.annotations?.conditions) {
continue;
}
definedNames.push(maybeTool.name);
definedNames.push(tool.name);
}
}
}
Expand Down