Skip to main content
An agent that uses tools one at a time pays for every step twice: a round trip through the model, and the step’s whole result in the model’s context. A task like which of my issues have no comments yet? is one call to list the issues, then one call per issue, and the model reads every comment of every issue only to count them. With Code Mode, the agent writes those steps as a short JavaScript program instead. The gateway runs it next to the tools and sends back only what the program returns:
To the client, a program is one more tool call: nothing to install, and nothing to change in the agent. The whole surface is two tools: trustgate_search to find the user’s tools, and trustgate_execute to run a program that calls them. Every call the program makes is checked by the gateway like a direct call.

Why it matters

Take that question with 54 started issues. Say the issue list is about 44 KB and each issue’s comments about 1.5 KB, an ordinary size for a server that returns authors, dates and bodies. What changes:
  • A smaller context. Only the answer enters the conversation. The 125 KB never does, so it is not sent again on every later turn, and does not crowd the window that the rest of the task needs.
  • Fewer round trips. The loop runs on the gateway, with no model turn between one call and the next.
  • Exact work. Counting, filtering, joining and deduplicating are done by code. A model that reads 55 results to count them can miss one. A program does not.
  • The same governance. Each call a program makes is checked like a direct call: access, consent, policies and rate limits. See Governance and security.
  • Measured. Activity and Analytics show, per program and per gateway, the calls made and the tokens kept out of context. See Measuring the savings.
Code Mode helps when a task takes several calls: a loop over results, a filter, a count, or a join across servers. A single call gains nothing from a program. Call the tool directly.

How it works

1

The model finds the tools

The model looks up the tools it needs with trustgate_search. Each match comes with its signature, which is how the program will call it:
A client that already lists the user’s tools can skip this step.
2

It writes a program

The model calls trustgate_execute with the program as its only argument:
The code is the body of an async function: it can await, and it ends with return. trustgate_execute is marked destructive, since a program may call tools that write, so clients that confirm such tools show the person the program before it runs.
3

The gateway runs it

The program runs in a sandbox on the gateway. Each tools.<name>(…) call leaves the sandbox and takes the same path as a direct call from the client. The gateway checks that the application and the user may call the tool, runs the request policies, calls the server and runs the response policies, then hands the result to the program. Calls started together with Promise.all run up to 4 at a time.
4

The client gets one answer

The answer carries the program’s return value, which is all the model needs to read. It also lists each call the program made, with its server, its outcome and its duration (trimmed here to the first two):
Before the answer leaves, the gateway runs the response policies on it once more: a program can join or reshape what it read, so its output is inspected as a whole.

How the model knows to write a program

Code Mode tells the model in three places:
  • The tool description. trustgate_execute says to use it instead of several calls when a task needs a loop, a filter, a join or a count.
  • The connection instructions. The gateway’s initialize instructions say the same.
  • The search answers. Each trustgate_search answer ends with how to run the tools it found from a program.
When the user’s own tools are also in the client’s list, the model may still call them one by one. Listing only the gateway’s tools steers it to programs.

Writing programs

What a program can use: A program has no network, file or timer access, and cannot import modules. The user’s tools are the only thing it can reach.

What a call returns

A call returns the tool’s result in the most useful form available:
  1. the tool’s structured result (structuredContent), when the server sends one;
  2. otherwise its text, parsed, when the text is JSON, as with most servers;
  3. otherwise the text itself.
When a tool’s description does not say what its result looks like, a first program can return a sample, such as return (await tools.x({})).items?.[0], before the real one.

Errors

A call that fails throws an error. A program can catch it and go on:
The error’s name says why the call failed: Two things stop the whole program instead, because the person must act. A tool that needs the person to connect their account first returns the connect link to the client, exactly as a direct call would. A rate limit returns the rate limit error.

When a program fails

A program that throws, runs too long or returns too much does not leave the model guessing. The answer says what went wrong, and lists the calls made before the program stopped:
The model fixes the program and runs it again, usually in the next turn.

Examples

Each example shows what the user asks, the program a model writes for it, and what comes back. The tool names are those of the Linear, GitHub and calendar servers used here; each gateway publishes its own (see tool names).
“Which of my started issues have no comments yet?”
Returns ["ENG-103", "ENG-106"]. One program instead of one call per issue, and none of the comments reaches the model.
“Who is asked to review the most open pull requests?” The list does not name reviewers, so each pull request needs its details.
Returns the top three reviewers with their counts, such as [["jon", 3], ["ana", 2], ["marta", 1]].
“Which open pull requests mention a Linear issue, and what state is that issue in?” GitHub and Linear are different servers. A program joins them in one step.
Returns [{ "pr": 41, "issue": "ENG-101", "state": "started" }, { "pr": 63, "issue": "ENG-105", "state": "done" }]. In Activity, the program’s provider is both servers.
“Which open pull requests touch the payments service and are more than a week old?”
“Do any of my meetings tomorrow overlap?” This takes one call, but the model would otherwise compare every pair of events itself.
Returns [["Design review", "Vendor call"]].
“Ask for an owner on every urgent issue nobody has picked up.” A program can write as well as read. Clients that confirm destructive tools show the person this program and ask before it runs.
Each linear_save_comment call passes the gateway’s request policies on its own, just as a direct call would.

Measuring the savings

The gateway measures every program it runs:
  • Bytes read: what the program’s calls handed it. That is each result as the program saw it, or the error’s message and data.
  • Bytes returned: the text the client got back. That is the return value and logs, or the reason the program failed.
Without the program, the model would have read the first. With it, the model read only the second. Activity. Open a trustgate_execute request. Its details add two rows: Analytics. Once a program has run in the selected range, for the gateway or application:
  • Cost adds Tokens Saved, the tokens programs kept out of context.
  • MCP adds Round trips saved to its first row of cards: the tool calls programs made, less one per program.

How the estimate is made

Tokens kept out of context are estimated as:
So the program above, which read 128,021 bytes and returned 21, kept (128,021 − 21) / 4 = 32,000 tokens out of context. It also saved 54 round trips: 55 calls, less the program itself. Four bytes a token is the usual rate for English text and JSON. Read the estimate as a lower bound, for two reasons:
  • Each direct result is bigger than what the program read. A direct call also carries its MCP envelope, and some clients pass both a tool’s text and its structured result to the model.
  • What enters a conversation stays there. It is sent again on every later turn. Prompt caching can make those turns cheaper, but the tokens still take room in the context window.

Turning it on

In the console, open Settings → Agent Gateway → General → MCP Code Mode, pick On and Save. Only admins can change it. Through the Admin API:
Claude’s hosted connectors keep the tool list they fetched when the person connected. After turning Code Mode on, people see trustgate_execute once they reconnect. Until then, their model can still run a program by calling trustgate_execute through trustgate_call, as the gateway’s instructions tell it.
Earlier versions also offered trustgate_query, for programs limited to tools their servers marked read-only. It is retired: a gateway set to Read only or Full is now On, and a client that still calls trustgate_query is told to send the same program to trustgate_execute.

Getting models to use programs

When a client lists the user’s own tools, the model tends to take the obvious path and call them one by one, even where a program would do the task in one call. Two things help:
  • List only the gateway’s tools. Set Tool listing to Gateway tools only, under MCP Code Mode in the same settings, or send {"mcp_tool_listing": "gateway"} through the Admin API. Clients then see only the gateway’s tools. The model finds the user’s tools with trustgate_search and calls them with trustgate_call, or several at once from a program.
  • Ask for it. A prompt that names the tool works with any listing, for example “use trustgate_execute to…”.
Listing only the gateway’s tools has two costs:
  • A single call needs a search first.
  • Approval in the client is coarser. The client asks once for trustgate_call, not once per tool. The gateway’s policies still apply to every call.

Governance and security

A program cannot do anything the user could not do with direct calls. Every call it makes goes through the same checks:
  • The application’s tools. Its tool restrictions, and its users’ access.
  • Consent. A tool that needs the person to connect first stops the program, and the client shows the connect link as usual.
  • Policies. Request and response policies run on each call. The program’s final result passes the response policies once more.
  • Rate limits. Each call counts.
The program itself runs in an isolated sandbox inside the gateway. It has no network, file system, environment variables or clock to wait on. It is limited in time, memory and calls, and is discarded when it ends. The only way out is a call to the user’s tools, and each such call is checked as above.

Why there is no read-only mode

A program can call tools that write. Limiting programs to tools that only read would rest on what each server says about its own tools (MCP’s readOnlyHint and openWorldHint). Those claims are not verified, and servers make them differently: one marks its channel reads as reaching outside, another marks nothing at all. A rule built on them refuses ordinary reads, and lets through whatever a server mislabels. The controls that hold are the gateway’s own, and they apply to every call a program makes:
  • To keep an application’s agents from writing, restrict the application to the tools that read. A tool outside its restrictions does not exist for its programs either.
  • To check what is written, use request policies. They see each call’s arguments, whether a program or the model made it.
  • To have a person confirm, rely on the client: trustgate_execute is marked destructive, so clients that confirm such tools show the program first.

Limits

Each limit fails with a message that says what to change. For example, “Return less: pick the fields you need, slice, count or summarize in the program.”

Activity and usage

A program appears in Activity as one trustgate_execute request. Its provider is the server its calls went to, or the list of servers when there were several. Its details show the calls and savings. Usage counts each call that reached its tool as one request, exactly as if the client had made it. The program itself is not counted.

Troubleshooting

Check that Code Mode is on for the gateway, and that the client has reconnected since then. If the client also lists the user’s tools, set Tool listing to Gateway tools only, or ask for a program in the prompt.
The application’s tool restrictions leave the tool out, for programs as for direct calls. Add it to the application’s tools if its agents should use it.
The name must be the published one, such as linear_list_issues, not list_issues. The error lists the closest names. trustgate_search finds the exact one.
Narrow what the program lists, using a server’s filters or page size, before it loops. Return only the fields the answer needs, or count and summarize in the program.
One of the tools needs the person’s own account. They connect it from the link, and the model runs the program again.