Hosts

Pi

registerPiTools puts every tool on Pi with its prompt snippet and guidelines. And throws on failure because older Pi only listens to throws
IDpi02 / 04@earendil-works/pi-coding-agent
Host / Pi

Pi extension

Pi's registerTool with the prompt snippet and guidelines filled in.

label
Text Slug
prompt
snippet, 1 rule
parameters
text, separator
failure
thrown

Access

Importimport { registerPiTools } from "@agntn/tools/pi"
Peer@earendil-works/pi-coding-agent >=0.84.2 <2
Failsthrown
tspackages/pi/extensions/mypackage.ts
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { registerPiTools } from "@agntn/tools/pi";
import { tools } from "../../../src/tools.ts";

export default function (pi: ExtensionAPI) {
  registerPiTools(pi, tools);
}

Pi gets name, label from title, the description, promptSnippet and promptGuidelines when you set them, and the schema as parameters. Same JSON Schema every other host gets. Pi's own validator checks it, then the core checks it again, because a second check costs nothing and a missing one costs an afternoon.

Failures

Up to 0.98, Pi reads a returned object as a success no matter what's in it. A tool that returns isError: true would look fine to the model. So by default the adapter throws the result's text when isError is set, and Pi records an error.

On Pi 0.99 or newer you can keep the result as it is:

ts
registerPiTools(pi, tools, { failures: "return" });

That keeps details on a failure too. Only do it with a peer range of >=0.99.0, since Pi doesn't check peer ranges when it installs a package.

Pi's model reads message, not the error object. So where does a cause go? Into the message, as the same caused by: lines MCP prints. Your catch still gets the original error as cause.

Fixing arguments

Ever had a model type an array as text? registerPiTools fixes that for every tool, no option needed.

Pi 1.5 validates with coercion, so words: '["abandon","zoo"]' turns into ['["abandon","zoo"]']. A lookup for one word with brackets in it. Plainly wrong, and no wordlist has ever heard of ["abandon","zoo"].

An enum list like kinds: '["assignment"]' fails validation instead. So does fields: '{"Priority":"Medium"}', with "expected object, got string".

The fix checks each top-level property before Pi does. A string starting with [ or {, spaces aside, gets parsed. The parse replaces the text only if that property's schema accepts it. Otherwise the text goes on to Pi as it came. Nested values aren't touched.

So id: "grok" stays a string. On a property that takes a string or a list, id: '["grok","claude"]' becomes the list. But words: '["abandon",7]' stays text, since it doesn't fit a string array. A plain string property keeps '["abandon"]' as it came.

Need another repair? Use prepareArguments, keyed by tool name:

ts
registerPiTools(pi, tools, {
  prepareArguments: { harnesses_info: splitCommas },
});

Here, splitCommas turns id: "grok,claude" into a list. It gets the arguments after the JSON text fix. Input can have any shape, so hand back what you don't recognise untouched.

Pi validates the result, then the core checks it again. If the repair throws, the model reads the throw as the call's error.

A name in prepareArguments that isn't in the tool list throws at registration, just like confirm. Otherwise a typo would skip the repair without a word. Every supported Pi version has the hook. The MCP, OMP and AI SDK adapters don't take this option.

Asking first

Some calls shouldn't run just because the model asked. Opening a pull request, say. Or overwriting release notes?

Give the adapter a confirm builder per tool, and Pi asks the user before the call runs:

ts
registerPiTools(pi, tools, {
  confirm: {
    notes_publish: (input) => ({
      title: "Publish note?",
      message: `Title  ${String(input.title)}`,
    }),
  },
});

The arguments pass validation first, so the builder never sees junk. Then the dialog opens through ctx.ui.confirm with the call's abort signal. A yes runs the tool. A no throws, and the model reads that it shouldn't retry unless the user asks again.

No UI, no write. In pi -p there's nobody to ask, so the call throws instead of running unasked. Return undefined from the builder to skip the question for one call, a dry run for example.

The title goes through sanitizeLine. The message keeps its lines and tabs, but escape sequences and bidi tricks don't make it to the dialog. A model can't paint its own yes button. A name in confirm that isn't in the tool list throws at registration. A typo there would drop the check without a sound, the worst kind of quiet.

OMP doesn't need any of this. Its approval: "write" already stands in front of every write.

Questions from the tool

confirm asks before the call. ask asks from inside it, once the tool knows what to ask. Pi has no form dialog, so the adapter walks the fields one ctx.ui dialog at a time:

FieldDialog
Type.Enumselect with the values
Array of an enumselect with [x] toggles and Done
Type.Booleanselect with Yes and No
Anything elseinput, read as a number for a numeric field

An optional field with a list gets a (skip) line, toggles included. An empty input takes the default, or leaves an optional field out. Typed 0 into a field with minimum: 1? Pi shows a warning with the reason and asks again. Escape on any dialog closes the whole form, and the tool reads cancel.

Every dialog gets the call's abort signal. With ctx.hasUI off, as in pi -p, there's no ask at all. Nobody to ask, nothing to wait for.

Call lines

Left alone, Pi draws a call as the tool name and a key=value dump of every argument. Fine for read. For a tool whose one interesting argument is a long URL? Mostly noise.

Hand the adapter a describeCall and Pi's Text, and the line becomes the title plus your summary, styled like Pi's own tools:

ts
import { Text } from "@earendil-works/pi-tui";

registerPiTools(pi, tools, {
  Text,
  renderers: { text_slug: { describeCall: (args) => args.text } },
});

Pi resolves @earendil-works/pi-tui for every extension it loads, so the import works at runtime with nothing installed. The summary goes through sanitizeLine first. A newline in a model's argument can't forge a line. Forget Text and registration throws, which beats a summary that never shows up.

It's the same describeCall OMP takes. One function, two hosts.

Renderers

Pass Pi's own renderCall and renderResult per tool through renderers. They reach Pi unchanged, and a renderCall beats describeCall for the same tool:

ts
registerPiTools(pi, tools, {
  renderers: { books_text: { renderResult: preview } },
});

Handy when a tool returns a lot of text and you want the collapsed row to stay short.

Progress

A progress line reaches Pi through onUpdate. It's a partial result: one text block, details: {}. Pi shows it while the call runs. Your own renderResult gets it too, with isPartial set. Reading details in there? Check that flag first.

Keeping tools out of the prompt

A tool nobody called today still sends its schema with every request. That's a bad deal. It gets a seat at every conversation, invited or not.

By default, registerPiTools registers every tool as direct. Pi declares its schema and includes its promptGuidelines every time.

Want tools available without that baggage? Set exposure to deferred. Pi leaves their declarations and guidelines out. When needed, tool_search finds and declares them for the next call.

Setting exposure as a package author

Pass one value for every tool, or choose by name:

ts
registerPiTools(pi, tools, {
  exposure: { default: "codemode", tools: { keys_wallet_generate: "direct" } },
  namespace: { name: "keys", description: "Keys, mnemonics and addresses" },
});

Here, the named tool stays direct. The rest are called from codemode scripts. Leave exposure out, and Pi's direct default applies.

The available values are direct, model-only, codemode, deferred and hidden. A name in exposure.tools that's absent from the tool list throws at registration.

The namespace applies to every tool in the call. Pi groups them like tools from one MCP server. Codemode's describeNamespace() returns it. tool_search ranks tools using its name and description.

Choosing as a user

You don't need to change a package. These variables defer tools by default but keep keys_ tools direct:

shell
export AGNTN_PI_EXPOSURE=deferred
export AGNTN_PI_EXPOSURE_KEYS=direct

The package prefix comes before the first _, uppercased. A package variable beats the global variable. Both beat the package's exposure option. An empty variable counts as unset.

Values are case-sensitive. An unknown value throws at registration and names its source. For example, AGNTN_PI_EXPOSURE_DEMO is "Deferred"; takes direct, model-only, codemode, deferred, hidden.

How does the model find deferred tools?

Add tool_search to your Pi settings:

json
{ "defaultTools": ["+tool_search"] }

Without it, deferred extension tools are unreachable. Pi enables tool_search automatically only for deferred MCP servers. For codemode tools, add "+codemode" instead.

Both exposure and namespace need Pi 0.99 or newer. The MCP, OMP and AI SDK adapters don't take these options. OMP has its own loadMode option.