DEV Community

Cover image for Cloudflare Code Mode Makes Security Part of the Tool Design
jamilxt
jamilxt

Posted on Edited on Originally published at jamilxt.com

Cloudflare Code Mode Makes Security Part of the Tool Design

An AI agent connected to a large tool catalog faces a context problem. The model must search descriptions and find the right operation before it can do useful work.

Cloudflare's Code Mode takes another route. The model writes a small program that calls typed methods, and a sandbox runs that program. That can reduce the tool descriptions sent to the model. But the bigger design question is what the generated program is allowed to reach.

I have not built an application with Code Mode. This article is based on Cloudflare's current documentation and its original design post. That distinction matters because the current docs label Code Mode experimental and warn that it may change in breaking ways.

From a list of tools to a small program

In a typical tool-calling setup, the model sees separate operations such as list_projects and list_tasks. It calls one, reads the result, then calls the next. When the second operation depends on the first, intermediate data travels back through the model.

Code Mode lets the model write code to compose those calls. For example, it could list active projects, fetch tasks for each project, and return only blocked tasks. The intermediate project and task data can stay inside the execution rather than becoming separate model replies.

Cloudflare's original September 2025 post described this as turning MCP tools into a TypeScript API. Its current implementation supports connectors backed by MCP servers, OpenAPI documents, AI SDK tools, or custom code. A model can discover a method, inspect its TypeScript description, and then call it from generated JavaScript.

That is the context-saving idea. The security design is what makes it more than a prompt-format trick.

The sandbox gets methods, not credentials

Cloudflare documents two different sandbox environments. A container runs a Linux image and can use its installed tools and processes. A Dynamic Worker runs JavaScript against methods and values that the host explicitly provides.

Code Mode uses Dynamic Workers for generated JavaScript. The host decides which methods are available. A method might list pull requests for one repository while keeping the API token in the host. The generated code can filter those pull requests, but it does not receive the token or gain access to every repository.

Cloudflare's documented executor blocks outbound fetch() and connect() calls by default. External operations go through connector methods, which cross the sandbox boundary through Workers RPC. The host can intercept those calls to enforce policy and record them for replay or approval.

This is a narrower permission model than giving generated code a general network connection and hoping it uses the right API safely. It also means the host must define methods carefully. If a connector exposes an overly broad operation, the sandbox cannot make that operation narrow by itself.

Approval is part of execution, not a prompt

The current Code Mode runtime can pause when a connector method requires approval. It records the pending action and stops the current pass. An application can show the method and arguments to a person before approval.

After approval, the runtime runs the same generated code again. Earlier connector calls return their recorded results instead of running twice. The approved call then executes, and the program continues. This replay lets the script stay linear while the approval happens outside the original request.

That mechanism is useful, but its limits matter. Rejecting a pending action ends the execution; it does not undo earlier actions. Rollback is separate. It calls connector-provided revert methods in reverse order, where available. A method without a revert action remains applied, and a failed revert does not prevent attempts to compensate for other calls.

Cloudflare explicitly says rollback is compensation, not database transaction isolation. In plain terms, an application cannot treat it as a universal undo button. The connector author defines what reversal means, and another system may have changed since the original action.

Smaller context does not mean automatic safety

Code Mode changes how an agent composes operations. It does not decide whether an operation is allowed for a particular user or whether an action needs confirmation. Those rules still belong in the host application and connector handlers.

Cloudflare's MCP server guide makes this point directly: authorization and approval for side effects must be enforced in upstream handlers. Credentials should not appear in tool results or OpenAPI descriptions, because generated code can read those values if they are returned to it.

There is another tradeoff. Dynamic Workers suit code that calls known methods. They are not a replacement for a Linux environment when an agent needs a compiler, filesystem, package manager, or child process. Cloudflare's sandbox guide recommends containers for workloads that need those operating-system features. A single application can use both environments for different parts of a task.

A practical review list

Before using Code Mode for real actions, check the application around it:

  • Scope each connector method. Limit its account, repository, tenant, or data set on the host side.
  • Keep secrets host-side. Never return credentials in descriptions, arguments, or results.
  • Require approval for consequential actions. Show the exact method and arguments that will run.
  • Plan for partial completion. Rejection does not reverse earlier calls. Add explicit compensation where it is safe and meaningful.
  • Review replay behavior. Calls may be repeated during approval replay unless the runtime has recorded and reuses their results. Keep side effects behind the runtime's connector path.
  • Choose the right sandbox. Use a Dynamic Worker for scoped JavaScript methods; use a container when the task needs Linux tools.
  • Treat the API as experimental. Pin and test versions, and re-check the official documentation before upgrades.

The checklist is more important than the number of tools visible to the model. A compact interface can save context, but a poorly scoped method can still grant too much power.

Code Mode's real design choice

Cloudflare's proposal is often summarized as "one tool instead of hundreds." That is true, but incomplete. Code Mode moves the model's work from selecting individual calls to writing a program, while the host keeps authority over which operations that program can invoke.

That split is promising because it separates flexible composition from access control. Yet the control boundary is only as good as the methods, approvals, logs, and compensating actions the application provides. The sandbox can limit what code reaches. It cannot decide whether the business action itself is appropriate.

For now, the clearest use case is an agent that needs to combine a defined set of API operations without exposing broad network access or credentials. Teams that need production guarantees should read the current experimental warning as part of the design, not as a footnote.

Would you trust generated code to compose operations in your system if every external action had to pass through a narrowly scoped host method? What would you require before enabling writes?

Sources

Top comments (0)