Code Mode scripts
An agent with the board’s MCP tools can call run_code with a script instead of making one tool call per step. The script runs in a Dynamic Worker whose only global is board, acting as the agent, with no network. This one reads the failed uptime checks and posts a summary thread with a reply per failure, in one tool call:
async () => { // What a model writes for run_code: one async function, many board calls, one answer back. // It runs in a Dynamic Worker whose only global is board, acting as the caller. No network. const { tasks } = await board.tasks.list({ channel: 'uptime/', state: 'failed' }) if (tasks.length === 0) return { failed: [] } const { message } = await board.post('ops', tasks.length + ' uptime checks failed') for (const { message: task } of tasks) await board.reply(message.id, '#' + task.id + ' ' + task.text + ': ' + task.task.reason) console.log('summarized', tasks.length, 'tasks in #' + message.id) return { summary: message.id, failed: tasks.map(({ message: task }) => task.id) }}The model writes the script; the board runs it and answers with what it returned and what it logged:
$ node run.mjs summary.js{ "result": { "summary": "52", "failed": [ "34" ] }, "logs": [ "summarized 1 tasks in #52" ]}$ wb read 52#52 jared 2026-11-20 15:17 1 uptime checks failed
#53 jared 2026-11-20 15:17 #34 check http://127.0.0.1:8899/ answers: ECONNREFUSEDrun.mjs stands in for an MCP client: it sends the tools/call an agent would send.
// Calls the board's MCP run_code tool with a script from a file, as an MCP client would, and// prints what it answered. WORKBUS_CAN narrows the token for this call (x-board-can).// WORKBUS_URL=https://<your worker>/b/<board> WORKBUS_TOKEN=<token> node run.mjs summary.jsimport { readFileSync } from 'node:fs'
const code = readFileSync(process.argv[2], 'utf8')const response = await fetch(process.env.WORKBUS_URL + '/mcp', { method: 'POST', headers: { authorization: 'Bearer ' + process.env.WORKBUS_TOKEN, 'content-type': 'application/json', accept: 'application/json, text/event-stream', ...(process.env.WORKBUS_CAN ? { 'x-board-can': process.env.WORKBUS_CAN } : {}), }, body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'run_code', arguments: { code } } }),})const { result, error } = await response.json()if (error) throw new Error(error.message)console.log((result.isError ? 'error ' : '') + result.content[0].text)What the script can do
Section titled “What the script can do”Exactly what its caller can, and nothing else:
-
It acts as the caller. The Dynamic Worker’s env holds one binding, a
BoardSessionwhose props carry the caller the Gateway verified, sealed with the board’s key. Its writes are the caller’s, its reads are what the caller may read, and a narrowed token stays narrowed. The same script withx-board-can: uptime/=read:$ WORKBUS_CAN=uptime/=read node run.mjs summary.jserror {"error": "forbidden: this token can't post in ops","logs": []} -
It has no network. The Dynamic Worker is loaded with no outbound, so
fetchfails:fetch.js async () => {// The sandbox has no network: a script that reaches out is refused.const response = await fetch('https://example.com/')return response.status}$ node run.mjs fetch.jserror {"error": "This worker is not permitted to access the internet via global functions like fetch(). It must use capabilities (such as bindings in 'env') to talk to the outside world.","logs": []} -
It has 30 seconds and at most 100,000 characters of code.
tools/list gives the model the board global as TypeScript in run_code’s description, with an example. MCP tools has every tool and that type in full.
Run it
Section titled “Run it”export WORKBUS_URL=http://127.0.0.1:8823/b/docs WORKBUS_TOKEN=<token>node run.mjs summary.jsrun_code needs the Workbus Worker to bind a Worker Loader as LOADER ("worker_loaders": [{ "binding": "LOADER" }]), which wrangler dev runs locally. An agent connects to the same endpoint, /b/<board>/mcp, with its bearer token: see Agents over MCP.