forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebmcp.ts
More file actions
70 lines (63 loc) · 2.01 KB
/
webmcp.ts
File metadata and controls
70 lines (63 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {zod} from '../third_party/index.js';
import {ToolCategory} from './categories.js';
import {definePageTool} from './ToolDefinition.js';
export const listWebMcpTools = definePageTool({
name: 'list_webmcp_tools',
description: `Lists all WebMCP tools the page exposes.`,
annotations: {
category: ToolCategory.DEBUGGING,
readOnlyHint: true,
conditions: ['experimentalWebmcp'],
},
schema: {},
handler: async (_request, response, _context) => {
response.setListWebMcpTools();
},
});
export const executeWebMcpTool = definePageTool({
name: 'execute_webmcp_tool',
description: `Executes a WebMCP tool exposed by the page.`,
annotations: {
category: ToolCategory.DEBUGGING,
readOnlyHint: false,
conditions: ['experimentalWebmcp'],
},
schema: {
toolName: zod.string().describe('The name of the WebMCP tool to execute'),
input: zod
.string()
.optional()
.describe('The JSON-stringified parameters to pass to the WebMCP tool'),
},
handler: async (request, response) => {
const toolName = request.params.toolName;
let input: Record<string, unknown> = {};
if (request.params.input) {
try {
const parsed = JSON.parse(request.params.input);
if (typeof parsed === 'object' && parsed !== null) {
input = parsed;
} else {
throw new Error('Parsed input is not an object');
}
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e);
throw new Error(`Failed to parse input as JSON: ${errorMessage}`);
}
}
const tools = request.page.pptrPage.webmcp.tools();
const tool = tools.find(t => t.name === toolName);
if (!tool) {
throw new Error(`Tool ${toolName} not found`);
}
const {status, output, errorText} = await tool.execute(input);
response.appendResponseLine(
JSON.stringify({status, output, errorText}, null, 2),
);
},
});