toAgentInvokeCode
Writes the snippet that repeats a tool call from the browser console or a test; the runner's copy buttons use it.
Writes the snippet that repeats a tool call from the browser console or a test; the runner’s copy buttons use it. toExecuteToolCode writes the same call through WebMCP directly. toAgentRunCode is the earlier name of toAgentInvokeCode.
Import
import { toAgentInvokeCode, toExecuteToolCode } from "@webmcp-agents/devtools";
Example
Copy a call as code, or print the WebMCP version.
import {
toAgentInvokeCode,
toExecuteToolCode,
} from "@webmcp-agents/devtools";
const args = { quantity: 2 };
// For the console: the devtools' helper.
await navigator.clipboard.writeText(
toAgentInvokeCode("add_to_cart", args),
);
// For a test or a script: the same call through WebMCP.
console.log(toExecuteToolCode("add_to_cart", args));
Options
toAgentInvokeCode(name, args) and toExecuteToolCode(name, args) take the same two arguments.
| Name | Type | Default | Description |
|---|---|---|---|
name |
string |
required | The tool name, written with JSON.stringify. |
args |
unknown |
required | The arguments, written with JSON.stringify(args, null, 2). |
Returns
A string of code. toAgentInvokeCode uses the devtools’ own console helper:
await agent.invoke("add_to_cart", {
"quantity": 2
});
toExecuteToolCode finds the tool object first, because Chrome takes the tool object from getTools():
const tool = (await document.modelContext.getTools()).find((t) => t.name === "add_to_cart");
await document.modelContext.executeTool(tool, {
"quantity": 2
});
Notes
agent.invoke(andagent.run, the same function) exists only while the devtools are installed. The runner’s “Copy as” button writesagent.invoke(…).toAgentRunCodeis deprecated: the same function astoAgentInvokeCode.executeToolargument shapes differ by Chrome version (object or JSON string). The snippet passes an object. Library code should use core’sexecuteTool()instead of callingdocument.modelContext.executeTooldirectly.
See also
- Devtools, the page that teaches the console helper
createDevtoolsStore:window.agentSchemaForm