Code Mode: A Smarter Way to Use MCP Tools and Reduce LLM Context Usage A developer outlined an approach, based on Cloudflare's September 2025 "Code Mode" proposal, that converts MCP tools into a TypeScript API so an LLM writes code to call them instead of invoking each tool directly. The method targets two sources of token consumption in traditional tool-calling: large tool-definition catalogs and intermediate tool results that the model must process between dependent calls. The writeup argues that programmatic handling of intermediate results, such as extracting a single repository ID from a 100-record search, can cut context usage. What if, instead of asking an LLM to call dozens of tools, we simply asked it to write code that uses those tools? As AI agents become more capable, they are increasingly connected to external services through tools. GitHub, Slack, Jira, databases, browsers, and countless other integrations are becoming part of a single agent's workflow. The Model Context Protocol MCP has made connecting these services significantly easier. But there is another problem emerging: context window consumption and token cost. Every tool definition, description, parameter schema, tool call, and returned result can consume tokens. As the number of connected tools increases, so does the amount of information an LLM must process. Cloudflare proposed an interesting approach to this problem in its September 2025 article, Code Mode: The better way to use MCP . The idea is surprisingly simple: Instead of exposing MCP tools directly to an LLM, convert them into a TypeScript API and let the LLM write code to call them. Let's understand why this works, how it is implemented, and how you can start applying the same architecture. MCP Model Context Protocol provides a standardized way for AI applications to discover and interact with external tools and resources. An MCP server exposes tools with information such as: For example, imagine an MCP server that exposes these tools: search github repositories query get repository details owner, repo list repository issues owner, repo get issue details owner, repo, issue number When an agent connects to this server, its tool definitions are made available to the LLM. When the user asks: "Find the most popular repository related to FastAPI and summarize its open issues." The agent might perform something like this: LLM | |-- search github repositories | | | v | MCP Server | | | v | Repository results | | | v | LLM processes results | |-- get repository details | | | v | MCP Server | | | v | Repository details | | | v | LLM processes results | |-- list repository issues | v MCP Server | v Issue results | v LLM generates summary Notice what happens here. The output of one tool call is returned to the LLM. The LLM processes that output, decides what to do next, and generates another tool call. This is perfectly reasonable for simple workflows. However, imagine an agent connected to 10 MCP servers, each exposing 15–20 tools. Now the agent has to work with potentially hundreds of tool definitions. That's where things get interesting. There are two important sources of token consumption in traditional tool-calling workflows. A. Tool definitions The LLM needs information about available tools to decide which one to invoke and how to invoke it. Each definition contains descriptions, parameters, types, and other metadata. As the tool catalog grows, this information can occupy a significant portion of the context window. B. Intermediate tool results This is arguably the more interesting problem. Consider a workflow involving five dependent tool calls. The result of each call is returned to the LLM, which must understand it before generating the next call. Sometimes, the LLM doesn't need to understand the entire result at all. It only needs to pass a particular ID or field into the next operation. For example: Search repositories | v Get repository ID | v Fetch repository issues | v Filter open issues | v Fetch details of selected issues If the repository search returns 100 records, does the LLM really need to process all 100 records just to extract one repository ID? Not necessarily. The application could handle that operation programmatically. But in a traditional tool-calling loop, intermediate results are often sent back through the model, consuming additional tokens and inference time. This is the problem Code Mode tries to address. Cloudflare's approach changes the interaction between the LLM and MCP tools. Instead of exposing every MCP tool as an individual callable tool, the system: The architecture looks like this: User Request | v LLM | Generates TypeScript | v Code Execution Tool | v Secure Sandbox | TypeScript API bindings | +--------+--------+ | | | v v v GitHub Slack Jira | | | +--------+--------+ | v Filter / Process Intermediate Data | v Final Results | v LLM The important difference: The LLM writes the orchestration logic, but the actual tool-to-tool data processing happens inside the execution environment. The LLM doesn't have to inspect every intermediate result. This is probably the most interesting reasoning behind Cloudflare's approach. LLMs are trained on enormous amounts of programming code. They have seen countless examples of: On the other hand, structured tool calling is a relatively different interaction pattern. When an LLM invokes a tool, it typically generates a structured message that the surrounding application interprets. Conceptually, it looks like this: