MCPcopy
hub / github.com/ChromeDevTools/chrome-devtools-mcp / executeThirdPartyDeveloperTool

Method executeThirdPartyDeveloperTool

src/McpPage.ts:148–335  ·  view source on GitHub ↗
(
    toolName: string,
    params: Record<string, unknown>,
    response: Response,
  )

Source from the content-addressed store, hash-verified

146 }
147
148 async executeThirdPartyDeveloperTool(
149 toolName: string,
150 params: Record<string, unknown>,
151 response: Response,
152 ): Promise<void> {
153 // Creates array of ElementHandles from the UIDs in the params.
154 // We do not replace the uids with the ElementsHandles yet, because
155 // the `evaluate` function only turns them into DOM elements if they
156 // are passed as non-nested arguments.
157 const handles: ElementHandle[] = [];
158 for (const value of Object.values(params)) {
159 if (
160 value instanceof Object &&
161 'uid' in value &&
162 typeof value.uid === 'string' &&
163 Object.keys(value).length === 1
164 ) {
165 handles.push(await this.getElementByUid(value.uid));
166 }
167 }
168
169 const result = await this.pptrPage.evaluate(
170 async (name, args, ...elements) => {
171 // Replace the UIDs with DOM elements.
172 for (const [key, value] of Object.entries(args)) {
173 if (
174 value instanceof Object &&
175 'uid' in value &&
176 typeof value.uid === 'string' &&
177 Object.keys(value).length === 1
178 ) {
179 args[key] = elements.shift();
180 }
181 }
182
183 if (!window.__dtmcp?.executeTool) {
184 throw new Error('No tools found on the page');
185 }
186 const toolResult = await window.__dtmcp.executeTool(name, args);
187
188 const stashDOMElement = (el: Element) => {
189 if (!window.__dtmcp) {
190 window.__dtmcp = {};
191 }
192 if (window.__dtmcp.stashedElements === undefined) {
193 window.__dtmcp.stashedElements = [];
194 }
195 window.__dtmcp.stashedElements.push(el);
196 return {
197 stashedId: `stashed-${window.__dtmcp.stashedElements.length - 1}`,
198 };
199 };
200
201 const ancestors: unknown[] = [];
202 // Recursively walks the tool result:
203 // - Replaces DOM elements with an ID and stashes the DOM element on the window object
204 // - Replaces non-plain objects with a string representation of the object
205 // - Replaces circular references with the string '<Circular reference>'

Callers 1

Calls 8

getElementByUidMethod · 0.95
resolveCdpElementIdMethod · 0.95
keysMethod · 0.80
evaluateMethod · 0.80
createMethod · 0.80
includeSnapshotMethod · 0.65
appendResponseLineMethod · 0.65
disposeMethod · 0.45

Tested by

no test coverage detected