Run code in a sandbox (executes the supplied code)
ctx_executeRun code in a sandboxed subprocess to compute answers from data, printing only results so raw output stays out of the conversation.
Instructions
Run code in a sandboxed subprocess. Languages: javascript, shell, typescript, python, perl.
Only what you console.log() enters your conversation — the bytes your code processes stay in the sandbox.
WHEN:
You intend to derive an answer FROM data (filter, count, aggregate, parse, compare) — print only the answer
Output shape or size cannot be predicted before execution (recursive finds, repo-wide greps, list endpoints, log scans)
You would read raw output then mentally compute — that compute belongs here, where its inputs stay out
You need a long-running process kept alive (dev server, watcher, daemon) — pass
background: trueto detach on timeoutOutput may be large but you want recall-by-topic later — pass
intent; outputs over ~5KB are auto-indexed and only section titles + previews come back
WHEN NOT:
Single short observational command you will consume verbatim (whoami, pwd, git status on a clean tree) — Bash is simpler
File mutations (Edit/Write) or navigation (cd/ls) — Bash is the right surface
RETURNS: Only what your code prints. Wrap risky calls in try/catch — uncaught errors go to stderr. Drill in via ctx_search(queries: [...]).
EXAMPLE: ctx_execute(language: "javascript", code: "const out = require('child_process').execSync('npm test', {encoding:'utf8', stdio:['ignore','pipe','pipe']}); console.log(out.split('\n').filter(l => /(FAIL|✗|×|Error:|Tests +.*(failed|passed))/i.test(l)).slice(0, 60).join('\n'))")
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cwd | No | Optional working directory for shell commands. Non-shell languages still execute from their sandbox temp directory. | |
| code | Yes | Source code to execute. Use console.log (JS/TS), print (Python/Ruby/Perl/R), echo (Shell), echo (PHP), fmt.Println (Go), IO.puts (Elixir), or Console.WriteLine (C#) to output a summary to context. | |
| intent | No | What you're looking for in the output. When provided and output is large (>5KB), indexes output into knowledge base and returns section titles + previews — not full content. Use ctx_search(queries: [...]) to retrieve specific sections. Example: 'failing tests', 'HTTP 500 errors'. TIP: Use specific technical terms, not just concepts. Check 'Searchable terms' in the response for available vocabulary. | |
| language | Yes | Runtime language | |
| timeoutMs | No | Max execution time in MILLISECONDS — timeoutMs: 120000 is two minutes, timeoutMs: 120 is 0.12 seconds. When omitted, the host's own RPC timeout governs where it has one; on hosts that do not bound a call (Pi, Antigravity CLI) a generous server-side default applies instead. Pass an explicit value for long-running builds (Gradle/Maven/SBT), or background: true to detach. | |
| background | No | Keep process running after timeout (for servers/daemons). Returns partial output without killing the process. IMPORTANT: Do NOT add setTimeout/self-close timers in background scripts — the process must stay alive until the timeout detaches it. For server+fetch patterns, prefer putting both server and fetch in ONE ctx_execute call instead of using background. |