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.- 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.
How it works
The model finds the tools
trustgate_search. Each match
comes with its signature, which is how the program will call it:It writes a program
trustgate_execute with the program as its only argument: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.The gateway runs it
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.The client gets one answer
How the model knows to write a program
Code Mode tells the model in three places:- The tool description.
trustgate_executesays 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
initializeinstructions say the same. - The search answers. Each
trustgate_searchanswer ends with how to run the tools it found from a program.
Writing programs
What a program can use:What a call returns
A call returns the tool’s result in the most useful form available:- the tool’s structured result (
structuredContent), when the server sends one; - otherwise its text, parsed, when the text is JSON, as with most servers;
- otherwise the text itself.
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:name says why the call failed:
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: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).Loop and filter: issues with no comments yet
Loop and filter: issues with no comments yet
["ENG-103", "ENG-106"]. One program instead of one call per
issue, and none of the comments reaches the model.Aggregate: who reviews the most open pull requests
Aggregate: who reviews the most open pull requests
[["jon", 3], ["ana", 2], ["marta", 1]].Join across servers: pull requests and the issues they fix
Join across servers: pull requests and the issues they fix
[{ "pr": 41, "issue": "ENG-101", "state": "started" }, { "pr": 63, "issue": "ENG-105", "state": "done" }]. In Activity, the program’s provider
is both servers.Filter by dates and contents: stale pull requests on one service
Filter by dates and contents: stale pull requests on one service
Compare within one result: overlapping meetings
Compare within one result: overlapping meetings
[["Design review", "Vendor call"]].Write in bulk: comment on every unassigned urgent issue
Write in bulk: comment on every unassigned urgent issue
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.
trustgate_execute request. Its
details add two rows:
- 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:- 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: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.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 withtrustgate_searchand calls them withtrustgate_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…”.
- 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.
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’sreadOnlyHint
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_executeis marked destructive, so clients that confirm such tools show the program first.
Limits
Activity and usage
A program appears in Activity as onetrustgate_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
The model calls tools one by one instead of writing a program
The model calls tools one by one instead of writing a program
NotAllowedError: not one of the tools this connection may call
NotAllowedError: not one of the tools this connection may call
NotFoundError: not one of the user's tools
NotFoundError: not one of the user's tools
linear_list_issues, not
list_issues. The error lists the closest names. trustgate_search finds
the exact one.The program ran too long, made too many calls or returned too much
The program ran too long, made too many calls or returned too much
The client asks the person to connect an account
The client asks the person to connect an account