Skip to content

Agent tools and hooks from init files

The tool’s code runs in the workspace’s host, beside the init file that declares it:

ctx.agents.tool({
name: "deploy_status",
label: "Deploy status",
description: "The state of the latest deploy of `env`, and a chart of its error rate.",
parameters: { type: "object", properties: { env: { type: "string", enum: ["staging", "prod"] } }, required: ["env"] },
readOnly: true,
async run({ env }, { signal, update }) {
update({ content: [{ type: "text", text: `asking ${env}…` }] });
const response = await fetch(`https://deploy.example/${env}`, { signal });
const { state, chart } = (await response.json()) as { state: string; chart: string };
return { content: [{ type: "text", text: state }, { type: "image", data: chart, mimeType: "image/png" }] };
},
});
Field What it is
name What the model calls: letters, digits, _, -, at most 64. A built-in tool’s name (bash, read, omfp_call, task, …) or an MCP tool’s (mcp__…) fails the declaration.
label How the windows name its calls (default: name).
description, parameters What the model reads; parameters is a JSON Schema (a TypeBox Type.Object(…) is one).
readOnly true when it changes nothing: it then runs without asking in every approval mode; other tools ask outside yolo. A function (args) => boolean decides per call, run in the workspace’s host before each call outside yolo (a throw counts as false), e.g. readOnly: ({ action }) => action === "status".
run(args, call) call = { threadId, toolCallId, signal, update(partial) }. Returns { content: [{ type: "text", text } | { type: "image", data, mimeType }], details? } (data base64), or throws: the model reads the message. signal aborts when the user stops the turn or the declaration goes.

A thread reads its workspace’s tools when its pi session starts: a tool declared or removed later reaches a running thread once it is idle, by restarting its session. A call has no deadline of its own.

@omfp/browser gives the workspace’s agents a Chromium driven over the DevTools protocol:

import browser from "@omfp/browser";
export default (ctx) => {
ctx.plugin(browser, { headless: true });
};

It declares the tool browser with actions open { url }, navigate { url }, tabs, close, click { selector | text }, type { selector, text }, press { key }, scroll { dy | selector }, evaluate { js }, screenshot and wait { selector | ms }, on the current tab or tab. Results give the page’s URL, title, headings, links, controls and text, so a screenshot is rarely needed. The widget browser shows the tabs live (views.mount { id: "browser", widget: "browser" }); the method browser.tabs lists them. tabs, screenshot, wait and scroll run without asking in every approval mode; the other actions (evaluate included: a script can change the page, and open/navigate can act or carry data out) ask outside yolo. readOnlyActions changes that list, e.g. ctx.plugin(browser, { readOnlyActions: ["tabs", "screenshot", "wait", "scroll", "open", "navigate"] }).

One Chromium per workspace starts at the first call, shared by its agents, and ends with the plugin (reload, host stop, daemon stop). Options: executable (default: chromium, chromium-browser or google-chrome from PATH; without one the plugin declares nothing and logs why), headless (default true), args, and connect ("http://127.0.0.1:9222" or a ws://… endpoint) to attach to a running Chromium or CEF, which it then never stops.

ctx.mcp.define(name, config) declares an MCP server with an mcpServers entry: { command, args?, env?, cwd? } (stdio) or { url, headers? } (HTTP). The workspace’s .pi/mcp.json, mcp.json and .mcp.json declare servers too, and win on the same name. Threads restart to load a changed server once idle; omfp:mcp shows them; widgets call them with useMcp (omfp://docs/sdk).

To know whether a server starts, omfp_call mcp.test { workspaceId, name? } starts it from the daemon (no thread needed), lists its tools and stops it: each tested server is connected with toolNames, or failed with the error and the end of a stdio server’s stderr (timeoutSeconds, default the entry’s timeout, else 60). thread.mcp_servers reports what a running thread’s session made of them: connecting, connected with the tool count, or failed. A server whose connection drops is reconnected by the thread on its own, after 1 s, then waits doubling up to 30 s: failed with the error between attempts, connecting during one.

ctx.on("tool/before", ({ tool, args }) =>
tool === "bash" && /git push --force/.test(JSON.stringify(args)) ? { ask: "Force push?" } : undefined,
);

A hook answers "allow", "deny", { ask: question } or undefined (no decision). Hooks run in registration order (the user’s init file first); the first decision wins.

ctx.agents.context({ agentsFiles?, prompt? }): agentsFiles is "self" (the default: the workspace folder’s AGENTS.md only) or a list of more files; a file whose content another loaded file has is left out. The repositories and folders of a workspace keep their own AGENTS.md: agents are told to read it before working there, so a workspace holding many of them does not carry them all in every prompt. prompt is added to the agents’ system prompt. Both apply from the agents’ next turn.

task { tasks: [{ task, name?, agent?, model?, interactive?, budget?, effort? }] }. budget softly caps a subagent’s model requests: at the cap it is told once to wrap up, at 1.5× it is stopped and its result says so (the shares are the workspace’s agents.subagents, omfp://docs/settings). effort (off, low, medium, high) is its thinking level, over the one it would inherit. The task card shows both. Steer a running subagent with omfp_call thread.steer { threadId, text }; read its transcript with read thread://<id> and any background job by id (yours by name too) with read job://<id-or-name> (omfp://docs/jobs).

Each subagent’s result reaches you as a message when it ends, also after your turn ended: once nothing else is left, end your turn rather than call task_wait (or job_wait) to stay busy. Open todo items waiting on running subagents or background jobs get no “continue working” reminder. Call task_wait only when your very next step needs a result.

Threads without tools: threads.create { tools }

Section titled “Threads without tools: threads.create { tools }”

threads.create { workspace, prompt, tools } limits the new thread’s agent to the tools named, as the model sees them: pi registers no other, so neither the model, a codemode script nor an extension reaches one. Names are exact (no *, no +/-): the thread’s own tools (read, bash, edit, write, grep, find, ls, codemode, omfp_call, omfp_eval, task, ask, …), the workspace’s ctx.agents.tool tools, or mcp__<server>__<tool> (only those servers connect); an unknown name is bad_request. Leave it out for every tool.

  • tools: []: no tool at all. The system prompt says so and carries no omfp conventions, skills or AGENTS.md: for reading untrusted text (web pages, issue comments, messages) where an injected instruction must not be able to do anything.
  • A non-empty list keeps the AGENTS.md files; the omfp conventions come only with omfp_call, the agent kinds only with task. A list with omfp_call, omfp_eval, bash or codemode is no sandbox.
  • Restricted threads load no pi extensions of their own folders. The list is fixed: restarts keep it, forks copy it, a child gets only the tools of its own list its parent has. ThreadSummary.tools shows it.
  • Without bash in the list, a !command sent to the thread is a plain message and thread.bash is unauthorized.
const { id } = await ctx.call("threads.create", {
workspace: { id: ctx.omfp.workspaceId },
title: "Page summary",
prompt: `Describe this page in three lines:\n\n${page}`,
tools: [],
});
// Wait for its answer: omfp_thread_wait { threadId: id }, or follow the `thread/<id>` topic.

Disposable threads: threads.create { ephemeral: true }

Section titled “Disposable threads: threads.create { ephemeral: true }”

A thread a script or a job makes for one run can be disposable: threads.create { …, ephemeral: true } deletes it, with its session, once a run of its agent settled and nothing waits for it. A dialog or card waiting for the user, a background job its agent started or a child that still runs keeps it until a later run settled without them. The left column does not list it unless it needs the user; ThreadSummary.ephemeral marks it. Read its answer before it goes: follow the thread/<id> topic, or wait with omfp_thread_wait { threadId }, which answers before the thread is deleted. A prompt job takes ephemeral: true too (not with thread: "reuse").

await ctx.call("threads.create", {
workspace: { id: ctx.omfp.workspaceId },
prompt: "Tidy the notes folder and say what you moved.",
ephemeral: true,
});