Add a tool to a QiForge plugin

Register a tool the agent can call: build it with tool(), return it from getTools or getRequestTools, set its visibility, and send text as its own message.

A tool is a function the agent calls with validated args. Return one from getTools (boot-time) or getRequestTools (per-request). This recipe builds the Weather plugin's get_current_weather tool and registers it both ways.

Build a PluginTool with the tool() helper

Use the tool() builder from @ixo/oracle-runtime. The handler gets validated args and a per-request RuntimeContext.

import { tool, z, type PluginTool, type RuntimeContext } from '@ixo/oracle-runtime';

export function buildCurrentWeatherTool(): PluginTool {
  return tool(
    async (args, ctx: RuntimeContext) => {
      const { city } = z.object({ city: z.string().min(1) }).parse(args);
      ctx.logger.log(`weather lookup by ${ctx.user.did} for ${city}`);
      const result = await fetchWeather(city, ctx.abortSignal);
      if (!result) return `Could not find weather for "${city}".`;
      return JSON.stringify(result);
    },
    {
      name: 'get_current_weather',
      description: 'Get the current weather for a city.',
      schema: z.object({
        city: z.string().min(1).describe('City name, e.g. "Berlin".'),
      }),
    },
  );
}

Canonical source: weather-tools.ts.

Return it from getTools(ctx) for the stable case

Pick getTools when the tool list is the same for every user, because it only depends on config and identity.

import { OraclePlugin, type PluginContext, type PluginTool } from '@ixo/oracle-runtime';

export class WeatherPlugin extends OraclePlugin {
  override getTools(ctx: PluginContext): PluginTool[] {
    return [buildCurrentWeatherTool()];
  }
}

See getTools in weather.plugin.ts.

Return it from getRequestTools(rtCtx) when it varies per request

Use getRequestTools when the tool list depends on live state: the user, session, history, or loadedPlugins.

override getRequestTools(rtCtx: RuntimeContext): PluginTool[] {
  const tz = rtCtx.user.timezone ?? 'auto';
  return [buildForecastTool(tz)];
}

Both hooks merge. Their outputs are concatenated on every build. See getRequestTools in weather.plugin.ts.

Override visibility per tool (optional)

A tool inherits its plugin's manifest.visibility by default. Add a visibility field to the PluginTool to override per-tool.

const currentTool = buildCurrentWeatherTool();
currentTool.visibility = 'always';

const forecastTool = buildForecastTool();
forecastTool.visibility = 'on-demand';

return [currentTool, forecastTool];

See Set visibility for the full recipe.

Send text as its own chat message

On a chat surface (WhatsApp through IXO Channels, a Matrix room with the chat style) the runtime groups a reply into a few messages, and a short paragraph or code block joins the message next to it. When a tool has text the person must get alone, such as a dial code or a PIN they copy with one long-press, return it as standalone messages (runtime @ixo/oracle-runtime-workers 0.48.0 and later):

import { tool, withStandaloneMessages } from '@ixo/oracle-runtime-workers';

tool(
  async (args, ctx) => {
    const voucher = await redeem(args);
    // Only chat surfaces deliver standalone messages: elsewhere return the text.
    if (ctx.session.surface?.kind !== 'chat')
      return { ready: true, pin: voucher.pin };
    return withStandaloneMessages(ctx, {
      name: 'claim_voucher',
      content: {
        ready: true,
        hint: 'The PIN goes out after your reply, as its own message. Do not repeat it.',
      },
      messages: ['```\n' + voucher.pin + '\n```'],
    });
  },
  { name: 'claim_voucher', description: '…', schema },
);
  • content is what the model reads. messages are never shown to the model.
  • Each message is sent verbatim, alone, after the whole reply, in the order the calls ran. Nothing is merged into it.
  • Send 1 to 4 non-blank messages per call, each at most 1,024 characters. The helper throws a TypeError otherwise. A reply delivers at most four, and further ones are joined to the fourth.
  • The Portal and scheduled tasks build no chat reply, so they do not deliver standalone messages. Check ctx.session.surface as above.

Replies without standalone messages are grouped exactly as before. The runtime's docs/chat-delivery.md has the details.

What to know before shipping

  • Tool name must be unique across all loaded plugins. Boot fails on collision.
  • Throw from the handler for unexpected errors (retry middleware catches them). Return a string for expected misses ("not found").
  • The agent expects a string. JSON.stringify structured output to keep the contract clean.
  • ctx.abortSignal propagates from the inbound request. Forward it to every fetch.
  • Do not override descriptions of upstream MCP tools. Pass them through verbatim and put guidance in the manifest instead.
  • Declare effect: 'read' on a tool that only reads (Workers runtime). A tool counts as a write unless it declares effect: 'read', an MCP server marks it readOnlyHint, or its name follows the read convention (get_, list_, search_, read_, preview_, describe_, check_, explain_, validate_, view_). Writes run on the write lane, are claimed per arguments and capped per turn. Reads are not. A plugin that proxies an upstream MCP read under its own name loses the hint, so it must declare the effect itself, as the bundled firecrawl_search and firecrawl_scrape do.