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

Commit 4637ab9

Browse files
authored
chore: Add execute_in_page_tool tool (#1766)
This allows the MCP server to call the in-page tools provided by the inspected page. Handling of (non-serializable) DOM elements as tool parameters or tool output will be added in follow-ups.
1 parent 44e511f commit 4637ab9

File tree

8 files changed

+340
-5
lines changed

8 files changed

+340
-5
lines changed

src/McpContext.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type {
3131
} from './third_party/index.js';
3232
import {Locator} from './third_party/index.js';
3333
import {PredefinedNetworkConditions} from './third_party/index.js';
34+
import type {ToolGroup, ToolDefinition} from './tools/inPage.js';
3435
import {listPages} from './tools/pages.js';
3536
import {CLOSE_PAGE_ERROR} from './tools/ToolDefinition.js';
3637
import type {Context, DevToolsData} from './tools/ToolDefinition.js';
@@ -101,6 +102,7 @@ export class McpContext implements Context {
101102
#screenRecorderData: {recorder: ScreenRecorder; filePath: string} | null =
102103
null;
103104

105+
#inPageTools?: ToolGroup<ToolDefinition>;
104106
#nextPageId = 1;
105107
#extensionPages = new WeakMap<Target, Page>();
106108

@@ -464,6 +466,14 @@ export class McpContext implements Context {
464466
this.#updateSelectedPageTimeouts();
465467
}
466468

469+
setInPageTools(toolGroup?: ToolGroup<ToolDefinition>) {
470+
this.#inPageTools = toolGroup;
471+
}
472+
473+
getInPageTools(): ToolGroup<ToolDefinition> | undefined {
474+
return this.#inPageTools;
475+
}
476+
467477
#updateSelectedPageTimeouts() {
468478
const page = this.#getSelectedMcpPage();
469479
// For waiters 5sec timeout should be sufficient.

src/McpResponse.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ export class McpResponse implements Response {
422422
let inPageTools: ToolGroup<ToolDefinition> | undefined;
423423
if (this.#listInPageTools) {
424424
inPageTools = await getToolGroup(context.getSelectedMcpPage());
425+
context.setInPageTools(inPageTools);
425426
}
426427

427428
let consoleMessages: Array<ConsoleFormatter | IssueFormatter> | undefined;

src/third_party/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export {
3030
type TextContent,
3131
} from '@modelcontextprotocol/sdk/types.js';
3232
export {z as zod} from 'zod';
33+
export {default as ajv} from 'ajv';
3334
export {
3435
Locator,
3536
PredefinedNetworkConditions,

src/tools/ToolDefinition.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import type {InstalledExtension} from '../utils/ExtensionRegistry.js';
2424
import type {PaginationOptions} from '../utils/types.js';
2525

2626
import type {ToolCategory} from './categories.js';
27+
import type {
28+
ToolGroup,
29+
ToolDefinition as InPageToolDefinition,
30+
} from './inPage.js';
2731

2832
export interface BaseToolDefinition<
2933
Schema extends zod.ZodRawShape = zod.ZodRawShape,
@@ -194,6 +198,7 @@ export type Context = Readonly<{
194198
triggerExtensionAction(id: string): Promise<void>;
195199
listExtensions(): InstalledExtension[];
196200
getExtension(id: string): InstalledExtension | undefined;
201+
getInPageTools(): ToolGroup<InPageToolDefinition> | undefined;
197202
getSelectedMcpPage(): McpPage;
198203
getExtensionServiceWorkers(): ExtensionServiceWorker[];
199204
getExtensionServiceWorkerId(

src/tools/inPage.ts

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

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

99
import {ToolCategory} from './categories.js';
1010
import {definePageTool} from './ToolDefinition.js';
@@ -37,9 +37,13 @@ declare global {
3737

3838
export const listInPageTools = definePageTool({
3939
name: 'list_in_page_tools',
40-
description: `Lists all in-page-tools the page exposes for providing runtime information.
41-
To call 'list_in_page_tools', call 'evaluate_script' with
42-
'window.__dtmcp.executeTool("list_in_page_tools", {})'.`,
40+
description: `Lists all in-page tools the page exposes for providing runtime information.
41+
In-page tools can be called via the 'execute_in_page_tool()' MCP tool.
42+
Alternatively, in-page tools can be executed by calling 'evaluate_script' and adding the
43+
following command to the script:
44+
'window.__dtmcp.executeTool(toolName, params)'
45+
This might be helpful when the in-page-tools return non-serializable values or when composing
46+
the in-page-tools with additional functionality.`,
4347
annotations: {
4448
category: ToolCategory.IN_PAGE,
4549
readOnlyHint: true,
@@ -50,3 +54,68 @@ export const listInPageTools = definePageTool({
5054
response.setListInPageTools();
5155
},
5256
});
57+
58+
export const executeInPageTool = definePageTool({
59+
name: 'execute_in_page_tool',
60+
description: `Executes a tool exposed by the page.`,
61+
annotations: {
62+
category: ToolCategory.IN_PAGE,
63+
readOnlyHint: false,
64+
conditions: ['inPageTools'],
65+
},
66+
schema: {
67+
toolName: zod.string().describe('The name of the tool to execute'),
68+
params: zod
69+
.string()
70+
.optional()
71+
.describe('The JSON-stringified parameters to pass to the tool'),
72+
},
73+
handler: async (request, response, context) => {
74+
const page = context.getSelectedMcpPage();
75+
const toolName = request.params.toolName;
76+
let params: Record<string, unknown> = {};
77+
if (request.params.params) {
78+
try {
79+
const parsed = JSON.parse(request.params.params);
80+
if (typeof parsed === 'object' && parsed !== null) {
81+
params = parsed;
82+
} else {
83+
throw new Error('Parsed params is not an object');
84+
}
85+
} catch (e) {
86+
const errorMessage = e instanceof Error ? e.message : String(e);
87+
throw new Error(`Failed to parse params as JSON: ${errorMessage}`);
88+
}
89+
}
90+
91+
const toolGroup = context.getInPageTools();
92+
const tool = toolGroup?.tools.find(t => t.name === toolName);
93+
if (!tool) {
94+
throw new Error(`Tool ${toolName} not found`);
95+
}
96+
const ajvInstance = new ajv();
97+
const validate = ajvInstance.compile(tool.inputSchema);
98+
const valid = validate(params);
99+
if (!valid) {
100+
throw new Error(
101+
`Invalid parameters for tool ${toolName}: ${ajvInstance.errorsText(validate.errors)}`,
102+
);
103+
}
104+
105+
const result = await page.pptrPage.evaluate(
106+
async (name, args) => {
107+
if (!window.__dtmcp?.executeTool) {
108+
throw new Error('No tools found on the page');
109+
}
110+
const toolResult = await window.__dtmcp.executeTool(name, args);
111+
112+
return {
113+
result: toolResult,
114+
};
115+
},
116+
toolName,
117+
params,
118+
);
119+
response.appendResponseLine(JSON.stringify(result, null, 2));
120+
},
121+
});

src/tools/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ async function fillFormElement(
217217
}
218218
}
219219

220+
// here
220221
export const fill = definePageTool({
221222
name: 'fill',
222223
description: `Type text into a input, text area or select an option from a <select> element.`,

tests/third_party_notices.test.js.snapshot

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,56 @@ The above copyright notice and this permission notice shall be included in all c
765765
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
766766

767767

768+
-------------------- DEPENDENCY DIVIDER --------------------
769+
770+
Name: uri-js
771+
URL: https://github.com/garycourt/uri-js
772+
Version: <VERSION>
773+
License: BSD-2-Clause
774+
775+
Copyright 2011 Gary Court. All rights reserved.
776+
777+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
778+
779+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
780+
781+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
782+
783+
THIS SOFTWARE IS PROVIDED BY GARY COURT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARY COURT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
784+
785+
The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Gary Court.
786+
787+
788+
-------------------- DEPENDENCY DIVIDER --------------------
789+
790+
Name: fast-json-stable-stringify
791+
URL: https://github.com/epoberezkin/fast-json-stable-stringify
792+
Version: <VERSION>
793+
License: MIT
794+
795+
This software is released under the MIT license:
796+
797+
Copyright (c) 2017 Evgeny Poberezkin
798+
Copyright (c) 2013 James Halliday
799+
800+
Permission is hereby granted, free of charge, to any person obtaining a copy of
801+
this software and associated documentation files (the "Software"), to deal in
802+
the Software without restriction, including without limitation the rights to
803+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
804+
the Software, and to permit persons to whom the Software is furnished to do so,
805+
subject to the following conditions:
806+
807+
The above copyright notice and this permission notice shall be included in all
808+
copies or substantial portions of the Software.
809+
810+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
811+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
812+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
813+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
814+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
815+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
816+
817+
768818
-------------------- DEPENDENCY DIVIDER --------------------
769819

770820
Name: puppeteer-core

0 commit comments

Comments
 (0)