context-mode
Tools

ctx_execute_file

Read a file into the sandbox and derive an answer from it without reading it into context.

ctx_execute_file loads a file into the sandbox and runs your code against it, so you can learn something about the file — a line count, pattern matches, a parsed structure, a statistical aggregate — without pulling its bytes into the context window. The file is read once, inside the sandbox, and only what your code prints comes back.

A 45 KB file that would otherwise consume the window in full returns a 155 B answer instead — a 99.7% reduction in raw bytes reaching context.

The FILE_CONTENT variable

The file's contents are exposed inside the sandbox as a variable named FILE_CONTENT. Your code reads that variable, processes it, and prints a summary. In Elixir the variable is named file_content.

Parameters

ParameterRequiredDescription
pathyesAbsolute path, or relative to the project root. The file is confined to the project root; a path that escapes it is denied.
languageyesThe language your processing code is written in.
codeyesCode that reads FILE_CONTENT and prints a summary.
timeoutnoMax execution time in milliseconds. When omitted, no server-side timer fires and the MCP host's RPC timeout governs.
intentnoA short note describing what the output is for.

The intent parameter behaves exactly as it does in ctx_execute: when the printed output exceeds about 5 KB it is auto-indexed, and only the sections matching your intent return, with the rest retrievable later through search.

Example

This run parses a CSV held in FILE_CONTENT and prints only the derived totals. The file's rows never enter context.

ctx_execute_file — aggregate a CSV without reading it in
const rows = FILE_CONTENT.trim().split("\n");
const header = rows.shift();

let count = 0;
let revenue = 0;

try {
  for (const line of rows) {
    const cols = line.split(",");
    const amount = Number(cols[3]);
    if (!Number.isNaN(amount)) {
      count += 1;
      revenue += amount;
    }
  }
} catch (e) {
  console.log("parse failed:", e && e.message);
}

console.log(`rows=${count} revenue=${revenue.toFixed(2)}`);

Only the result returns:

rows=4187 revenue=918334.50

When to use it

Use ctx_execute_file when you need a fact about a file but not the file itself. Use a plain Read when you intend to edit the file, because the editor needs the exact bytes on disk. Use Read with offset and limit when you only need one known line and already know where it is.

ctx_execute_file discards its sandbox filesystem after each run. It derives answers; it does not modify the file.

On this page