Skip to content

Code Mode

Connect 15 MCP servers to a single AI agent? Without ToolMesh, that simply does not work — the context window fills up, the client chokes. Code Mode makes it possible by replacing hundreds of tool definitions with two meta-tools and a compact TypeScript interface.

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 from multiple backends, the context window fills up and the client becomes unusable. Code Mode reduces 50,000+ tokens to ~1,000 — the difference between “doesn’t work” and “just runs.”

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.