> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pctx.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Use the exact commands, endpoints, and product terms in these docs. Commands that start with `pctx` mean the Port of Context platform CLI installed from pctx.ai, not the open-source tool of the same name.

# Code mode

> Agents call many tools from one script and keep large responses out of their context window.

Normally an agent calls tools one at a time, and every full response lands in its context window. With code mode, the agent writes one short TypeScript script. The gateway runs it, and only what the script returns goes back to the agent.

![Without code mode, five tool calls put five full responses in the agent's context window. With code mode, one script makes the same five calls and only its small result enters the window.](https://mintcdn.com/pctx-dev/2mfowY9CQyGHWDkm/images/code-mode-light.svg?fit=max\&auto=format\&n=2mfowY9CQyGHWDkm\&q=85\&s=4a027fa680ad60bc0d6d39c361b885c0)

## What the agent sees

| Tool | What it does |
| - | - |
| `get_context` | Runs a script against the context map and your indexed sources |
| `run_code` | Runs a script against connected services |
| `get_signatures` | Lists the available functions and their exact types |

Each service is a namespace of typed functions. This script searches a handbook and returns only the path and heading of each match:

```typescript theme={null}
async function run() {
  const result = await Corpus.searchCorpus({
    query: "parental leave policy",
    sources: ["handbook"],
    limit: 3,
  });
  return result.hits.map((hit) => ({ path: hit.path, section: hit.heading_path }));
}
```

A script can only call these functions. It cannot read files, run shell commands, or reach the web, and it stops after two minutes.

## Turn it on

The context map and indexed sources use code mode already. For connected services, an admin turns it on one server at a time, and it works with any MCP server in the catalog.

1. Open **Admin** → **MCP servers** and select **Code mode** on the server.
2. Check that most of its tools are typed, shown as a count such as `23/23 typed`.
3. Turn on the **Code mode** switch.

A typed tool tells the script what shape its result has. If a tool is not typed, open it in the same panel, look at **Recent responses**, and save a JSON Schema for it. An agent connected to the [admin MCP](https://docs.pctx.dev/administration/admin-mcp.md) can do this for you.

## Measure the saving

The **Insights** page shows how many tokens code mode kept out of your agents' context windows.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.