Skip to content

Code Mode

Code Mode is ToolMesh’s approach to efficient tool invocation. Instead of calling each tool individually (one round-trip per tool), the LLM writes a JavaScript snippet that calls multiple tools in sequence.

ToolMesh exposes two meta-tools:

ToolPurpose
list_toolsReturns all available tools with TypeScript interface definitions
execute_codeExecutes JavaScript code containing toolmesh.* function calls

The LLM calls list_tools once, sees the available functions with their type signatures, then writes JavaScript that chains multiple calls together.

Every tool in the MCP tool list costs context window budget. With 50+ tools, this becomes significant. Code Mode reduces this to just two tools (list_tools + execute_code), regardless of how many backend tools exist.

Instead of:

LLM → tool_a → result → LLM → tool_b(result) → result → LLM → tool_c(result)

Code Mode enables:

LLM → execute_code("let a = await tool_a(); let b = await tool_b(a); return tool_c(b);")

One round-trip instead of three.

list_tools returns TypeScript interfaces, so the LLM generates well-typed code that the AST parser can validate before execution.

// LLM generates this after calling list_tools
const repos = await toolmesh.github_list_repos({ sort: "updated" });
const issues = [];
for (const repo of repos.slice(0, 3)) {
const repoIssues = await toolmesh.github_list_issues({
owner: repo.owner.login,
repo: repo.name,
state: "open"
});
issues.push(...repoIssues);
}
issues

Code Mode uses AST parsing to ensure only toolmesh.* function calls are executed. The JavaScript runs in a sandboxed environment with no access to fetch(), require(), the filesystem, or any globals beyond the toolmesh namespace.

Code Mode was pioneered by Cloudflare for their own API. ToolMesh brings it to any backend — MCP servers and DADL-described REST APIs alike.