Skip to content

Code Mode: flat token cost for big MCP catalogs

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
discover_toolsFinds the tools that fit the task and returns their TypeScript signatures
execute_codeExecutes JavaScript code containing toolmesh.* function calls

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

Discovery scales with the catalog. discover_tools narrows by regex pattern or BM25-ranked free-text query, and its detail level follows the number of matches on its own: up to 25 matches it returns full TypeScript signatures, up to 250 one-line summaries, and beyond that names only or a per-backend overview. Every response closes with a matched/shown count and a hint for refining; detail and limit override the automatic choice. Inside execute_code the same index is reachable as toolmesh.discover("<free text>") and toolmesh.describe("<tool_name>"), so a script can look up a signature mid-run without spending a round-trip.

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.

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

// LLM generates this after calling discover_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.

Each run is bounded by its wall-clock budget (TOOLMESH_CODE_TIMEOUT), at most 50 toolmesh.* calls, a call-stack depth cap and the number of runs in progress at once (TOOLMESH_CODE_MAX_CONCURRENT, default 16, at most half of them per user). A run that finds every slot taken waits up to 10 seconds, then fails. The JavaScript runtime has no memory limit of its own, so run ToolMesh with a container memory limit. See Configuration.

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