All articles

When should an MCP agent use Code Mode?

Yapr4 min read
Table of contents

If one lookup answers the question, call the MCP tool directly. Reach for Code Mode when the job requires multiple reads, paging, filtering, or math that should happen before anything lands in the chat.

The dividing line is simple: looking up the status of one project is a single read; counting open tasks by owner across five projects is a small program.

Match the method to the question

Take three everyday requests against a fictional project tracker:

RequestBest fitWhy
"What is the status of project P-42?"Direct read toolOne call returns the exact answer.
"Count open tasks by owner across these five projects."Code ModeLoops, filters, and groups inside a script so only the summary hits the chat.
"Move P-42's deadline to Friday."Direct write tool and approval flowIt changes a record; Yapr's Code Mode is strictly read-only.

Keep discovery in mind when estimating the size of a task. Even a short question turns into multiple calls if the agent first has to search for the right tool, read its schema, pick the right connection, and resolve an ID.

How Code Mode changes the work

In the code-execution pattern, the AI client writes a short script that calls tools and processes their responses locally inside a sandbox. As Anthropic's write-up on code execution with MCP explains, doing that filtering in code keeps noisy intermediate rows out of the model's context and lets the agent pull only what it needs.

For the five-project task count, a script reads the permitted task lists, tallies open items per owner, and returns a tiny JSON map. The model spends its attention explaining the summary instead of wading through hundreds of raw task objects.

Code Mode does not make provider limits disappear, though. Every page request still goes to the connected app, and if one project's read fails, the script must report that project as unavailable instead of folding it into a zero count.

Know your execution sandbox

"Code Mode" means slightly different things across platforms. Cloudflare's Code Mode, for instance, exposes MCP tools as a generated code API. Yapr's Code Mode gives your client a read-only JavaScript sandbox via stackyapper_run_code.

Inside Yapr, scripts run under the signed-in person's current workspace permissions and recheck access on every call. Write tools and confirmation-required reads are blocked inside scripts and must run through the direct approval flow. Scripts have zero direct access to the network, credentials, local files, or external packages.

That shapes how you prompt multi-step jobs. If you want an agent to summarize overdue tasks across five projects and then reassign the oldest three, treat it as two steps: read and summarize in Code Mode, then hand off the specific changes to direct write tools with confirmation.

Budget for the reads and the summary

Every Code Mode run works inside explicit limits documented in the Yapr Code Mode limits, including up to 12 tool calls per run (counting discovery and schema lookups), 30 seconds of runtime, 1 MiB of total tool results read, and 64 KiB of returned output.

Before running a loop over several projects, set a sensible page cap and leave breathing room for slow responses. Return the connection used, the number of records examined, and any source that stopped early or failed. If the job will not fit in one pass, split the work into named parts or continue cleanly using our Code Mode pagination guide.

Keep the evidence the reader actually needs

Code Mode shines when raw records compress cleanly into counts, groupings, or exception lists. Still, smaller is not always better. If a manager needs to read the actual wording of ten escalations to make a judgment call, stripping everything down to a bare count throws away the evidence. Return a compact summary plus the handful of record IDs or snippets needed to verify it.

Next time you prompt a multi-record check, spell out the scope, the grouping, and the coverage check in one clear line: "Count open tasks by owner across these five project IDs, list any project that could not be read completely, and show the filter you used." That gives the agent everything it needs to pick Code Mode and return an answer you can verify.

Keep reading

All articles