import { Static, TObject } from "./libs/typebox.mjs"; /** * Makes multi-line tool text safe for a terminal. Unlike {@link sanitizeLine} * it keeps the layout: line feeds, tabs and the joiners of emoji and scripts * (ZWJ, ZWNJ) stay, CR and CRLF become a line feed, and the Unicode line and * paragraph separators become a space so words do not run together. * * @param text - Tool text. * @returns {string} The text without escape sequences or other control characters. */ export declare function sanitizeText(text: string): string; /** Content block understood by MCP, Pi and OMP alike. */ export type ToolContent = { type: "text"; text: string; } | { type: "image"; data: string; mimeType: string; }; /** Text for the model plus details for the harness. */ export interface ToolResult
{ content: ToolContent[]; details: Details; /** Set when the tool could not answer. Each adapter carries it to its host. */ isError?: boolean; } /** * What a call does to the world, mapped to MCP annotations and OMP approval. * * - `read` changes nothing. * - `write` changes state that the next call can undo or overwrite. * - `destructive` deletes or overwrites what cannot be recovered. * - `exec` starts processes or runs code, which can then do anything at all. */ export type ToolEffect = "read" | "write" | "destructive" | "exec"; /** * How a tool reads on the command line. Without hints the command is the tool * name without its package prefix, in kebab case, and every property is a flag. */ export interface ToolCliHints { /** Command name instead of the derived one. Two words, as `servers list`, put it in a group. */ readonly command?: string; /** More words for the command's last word: `ls` reaches `servers list` as `servers ls`. */ readonly aliases?: readonly string[]; /** Line in the command list. Defaults to the first sentence of `description`. */ readonly description?: string; /** Properties taken in this order as positional arguments instead of flags. */ readonly positional?: readonly string[]; /** Words left after `positional`: joined by spaces into a string, one per item into an array. */ readonly rest?: string; /** Positionals no dashed word may fill, such as ids. One there fails as an unknown option. */ readonly plain?: readonly string[]; /** Short flags by property, `{ limit: "n" }` for `-n 5`. Only a whole word counts. */ readonly short?: Readonly>; /** String properties that read stdin when given as `-`. */ readonly stdin?: readonly string[]; /** `false` takes `--json` away, for a command that writes its own bytes to stdout. */ readonly json?: boolean; } /** How far a long call has come, for hosts that draw it. */ export interface ToolProgress { /** Work done so far. MCP skips an update where it doesn't grow. */ readonly progress: number; /** All the work there is, when the tool knows it. */ readonly total?: number; } /** A small form for the user: one message over flat fields, the shape MCP elicitation takes. */ export interface ToolQuestion { /** What the user reads above the fields. Lines and tabs survive, escapes don't. */ readonly message: string; /** Strings, numbers, booleans, `Type.Enum` picks and arrays of such an enum. Nothing nested. */ readonly schema: Schema; } /** What came back: the checked fields on `accept`, nothing on a "no" or a closed form. */ export type ToolAnswer = { readonly action: "accept"; readonly content: Content; } | { readonly action: "decline" | "cancel"; }; /** * Puts a {@link ToolQuestion} in front of the user and waits for the {@link ToolAnswer}. * `Static` is only `Content`'s default: in the return type, two installs clashed. */ export type ToolAsk = = Static>(question: ToolQuestion) => Promise>; /** An MCP icon, the SDK's own shape: a URL or `data:` URI a client can draw on its card. */ export interface Icon { readonly src: string; readonly mimeType?: string; /** `WxH` sizes such as `48x48`, or `any` for an SVG. */ readonly sizes?: readonly string[]; /** Background the icon is drawn for. */ readonly theme?: "light" | "dark"; } /** Per-call context the adapters hand to `execute`. */ export interface ToolCallContext { /** On cancel a read answers at once and a write still waits. Pass it on so the work stops too. */ signal?: AbortSignal; /** Says the call is still at it. Absent where the host can't show it, so call `progress?.()`. */ progress?: (message: string, amount?: ToolProgress) => void; /** * Asks the user and waits. Absent where nobody can answer, so check before calling. * * Over MCP the call starts over from the top for each answer, the earlier ones * replayed, so ask before the first effect and in the same order every time. */ ask?: ToolAsk; /** Pi's or OMP's `ctx`, the CLI's `CliHost`, MCP's `McpHost` or nothing. Narrow it yourself. */ host?: unknown; } export interface ToolDefinition { /** Wire name, `_`. Kept literal, so `toAiTools` keys its set by it. */ readonly name: Name; /** Human label: MCP `title` and `annotations.title`, Pi and OMP `label`. */ readonly title: string; /** Complete instruction for the model. MCP clients see nothing else. */ readonly description: string; /** MCP `icons` in `tools/list`. Pi, OMP, the AI SDK and the CLI don't draw them. */ readonly icons?: readonly Icon[]; /** MCP `_meta` in `tools/list`, such as `ui` or `openai/extensions`. No other surface reads it. */ readonly meta?: Readonly>; /** Schema of `details`. MCP lists it as `outputSchema` and sends them as `structuredContent`. */ readonly output?: TObject; /** Pi `promptSnippet`. */ readonly snippet?: string; /** Pi `promptGuidelines`. Must not promise anything `description` lacks. */ readonly guidelines?: readonly string[]; readonly effect: ToolEffect; /** Repeating the call with the same input has no further effect. Defaults to `effect === "read"`. */ readonly idempotent?: boolean; /** The call reaches outside the local machine. */ readonly openWorld?: boolean; readonly input: Input; /** Command line shape for `@agntn/tools/cli`. No other surface reads it. */ readonly cli?: ToolCliHints; /** * Runs the tool on input that already passed {@link validateInput}. * * Method syntax on purpose: it keeps the parameter bivariant, so a list of * tools with different inputs is still a `readonly ToolDefinition[]`. */ execute(input: Static, context: ToolCallContext): ToolResult
| Promise>; } /** Thrown when a tool definition breaks a contract every surface relies on. */ export declare class ToolDefinitionError extends Error { name: string; } /** One schema failure, with its place kept apart so a surface can name it in its own words. */ export interface InputIssue { /** The line as {@link ToolInputError.lines} has it. */ readonly line: string; /** JSON pointer of the failing value, empty for the arguments as a whole. */ readonly at: string; /** What is wrong there, without the place. */ readonly problem: string; /** Required properties the value at `at` lacks, when that is the failure. */ readonly missing?: readonly string[]; } /** Thrown by {@link invokeTool} when the arguments fail the tool schema. */ export declare class ToolInputError extends Error { name: string; /** One validation failure per line. Only these line breaks are the core's own. */ readonly lines: readonly string[]; /** The schema failures behind the lines, empty when the lines come from elsewhere. */ readonly issues: readonly InputIssue[]; /** The tool whose schema turned the input down, unset on a refusal an executor wrote itself. */ readonly tool: string | undefined; /** * @param lines - One validation failure per line. * @param issues - The schema failures behind them, if any. * @param tool - Name of the tool whose schema failed, when the core validated. */ constructor(lines: readonly string[], issues?: readonly InputIssue[], tool?: string); } /** * Declares a tool and checks the schema rules every surface depends on. * * @param tool - Tool definition. * @returns {ToolDefinition} The same definition, typed. */ export declare function defineTool(tool: ToolDefinition): ToolDefinition; /** * The input schema as the plain JSON Schema object every host receives. * * TypeBox 1.x schemas are plain objects already; this only drops the static * type, which lacks the index signature host APIs declare. * * @param tool - Tool whose schema to emit. * @returns {Record} JSON Schema document. */ export declare function wireSchema(tool: ToolDefinition): Record; /** * Indexes tools by name. * * A `Map`, not a plain object: a lookup of `toString` in a `Record` finds * `Object.prototype.toString` and skips the unknown-tool branch. * * @param tools - Tools to index. * @returns {ReadonlyMap} Tools by wire name. */ export declare function indexTools(tools: readonly ToolDefinition[]): ReadonlyMap; export type InputCheck = { ok: true; value: Static; } | { ok: false; lines: readonly string[]; issues: readonly InputIssue[]; }; /** * Validates arguments against the tool schema, reporting every failure. * * Runs on every surface, not only MCP: a host may skip its own validation, and * OMP's omptype drops `pattern` from the schema it emits. The message has one * line per failure: an undeclared key names itself with the keys its object * takes, at any depth, and an enum failure names the allowed values, so the * caller does not go back to the tool list and guess. Format from agntn/hashes * (#58). TypeBox stops at eight errors, so root keys are read off the arguments. * * @param tool - Tool whose schema applies. * @param args - Arguments as received from the host. * @returns {InputCheck} The typed value, or the failures, one per line. */ export declare function validateInput(tool: ToolDefinition, args: unknown): InputCheck; /** * Validates and runs a tool. Every adapter calls through here. * * Progress is best effort: a line after the call settles goes nowhere, and a host that throws on * one doesn't fail the call. The call answers once every question it started has settled, and a * question after that point rejects: nobody waits for the answer. A cancelled read doesn't wait * for anything and rejects with the abort reason. A write waits, since the host may have it. * * @param tool - Tool to run. * @param args - Arguments as received from the host. * @param context - Per-call context. * @returns {Promise} The executor's result, unchanged. * @throws {ToolInputError} When the arguments fail the schema. */ export declare function invokeTool(tool: ToolDefinition, args: unknown, context?: ToolCallContext): Promise; /** * Makes a value safe for one line of terminal or protocol text. * * Removes escape sequences, then replaces every control, format, line and * paragraph separator character with a space: a raw newline, U+2028 or a bidi * override (U+202E, a `Cf`) in an echoed value forges a line that reads as the * tool's own output. `String()` first, since hostile JSON ignores declared types. * * @param value - Value to render. * @returns {string} Single-line text. */ export declare function sanitizeLine(value: unknown): string; /** * Text of a result, for hosts that carry failures as thrown errors. * * @param result - Tool result. * @returns {string} Text blocks joined by newlines. */ export declare function resultText(result: ToolResult): string;