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.
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?”Scaling That Otherwise Doesn’t Work
Section titled “Scaling That Otherwise Doesn’t Work”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.”
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.