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.
How It Works
Section titled “How It Works”ToolMesh exposes two meta-tools:
| Tool | Purpose |
|---|---|
list_tools | Returns all available tools with TypeScript interface definitions |
execute_code | Executes 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.
Why Code Mode?
Section titled “Why Code Mode?”Context Window Savings
Section titled “Context Window Savings”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.
Fewer Round-Trips
Section titled “Fewer Round-Trips”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.
Type Safety
Section titled “Type Safety”list_tools returns TypeScript interfaces, so the LLM generates well-typed code that the AST parser can validate before execution.
Example
Section titled “Example”// LLM generates this after calling list_toolsconst 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);}issuesSecurity
Section titled “Security”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.
Origin
Section titled “Origin”Code Mode was pioneered by Cloudflare for their own API. ToolMesh brings it to any backend — MCP servers and DADL-described REST APIs alike.