defineTool
SUPPORT · function · Runtime value
Validate one tool declaration and freeze a detached copy of it.
authorize must settle with void; a rejection has to be thrown, so a returned false cannot silently pass. Handlers are bound to the declaration object, and the input schema is re-snapshotted so later mutation cannot widen the accepted input.
Import
import { defineTool } from '@jangts/agent-core/support';import { defineTool } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
Output | — | — |
App | — | — |
Id | string | string |
Call
defineTool<Input, Output, App, const Id extends string = string>(definition: ToolDefinition<Input, Output, App> & { readonly id: Id; }): ExecutableTool<Input, Output, App, Id>Validate one tool declaration and freeze a detached copy of it.
authorize must settle with void; a rejection has to be thrown, so a returned false cannot silently pass. Handlers are bound to the declaration object, and the input schema is re-snapshotted so later mutation cannot widen the accepted input.
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
Output | — | — |
App | — | — |
Id | string | string |
| Parameter | Type | Passing | Description |
|---|---|---|---|
definition | Omit<ExecutableTool<Input, Output, App, string>, "resolve"> & { readonly resolve?: ((input: Input, context: Readonly<{ stageId: string; }>) => ToolProposal) | undefined; } & { readonly id: Id; } | Required | — |
Fields of definition
| Field | Type | Required | Description |
|---|---|---|---|
id | Id | Required | Read-only. Stable id; also the name the model calls. |
description | string | Required | Read-only. Model-visible explanation of when to call the tool. |
inputSchema | SchemaAdapter<Input> | Required | Read-only. Schema applied to raw model arguments before authorize and execute. |
active | ToolCondition<App> | undefined | Optional | Read-only. Omitted means active. Authorization remains a separate execution boundary. |
asIntent | AsIntent<App> | undefined | Optional | Read-only. Omitted means this tool is not advertised by withIntent(). |
kind | string | Required | Read-only. Declared grouping used by registries and documentation; not an authorization boundary. |
authorize | (input: Input, context: OperationContext<App>) => MaybePromise<void> | Required | Host permission check. Runs before execution and may be asynchronous. |
execute | (input: Input, context: OperationContext<App> & NodeReportingContext) => MaybePromise<Output> | Required | Executes the already-authorized input; must not repeat authorization decisions. |
debugSteps | ((result: Output, context: import("../debug.js").NodeReportingContext) => void) | undefined | Optional | Optional result hook; writes through context.createSubFlowLogger() are recorded immediately. |
parseOutput | (value: unknown) => Output | Required | Validates untrusted output (for example a cached replay) before it is returned. |
resolve | ((input: Input, context: Readonly<{ stageId: string; }>) => ToolProposal) | undefined | Optional | Read-only. See the type definition and usage context. |
Returns: ExecutableTool<Input, Output, App, Id>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
id | Id | Required | Read-only. Stable id; also the name the model calls. |
kind | string | Required | Read-only. Declared grouping used by registries and documentation; not an authorization boundary. |
description | string | Required | Read-only. Model-visible explanation of when to call the tool. |
inputSchema | SchemaAdapter<Input> | Required | Read-only. Schema applied to raw model arguments before authorize and execute. |
authorize | (input: Input, context: OperationContext<App>) => MaybePromise<void> | Required | Host permission check. Runs before execution and may be asynchronous. |
execute | (input: Input, context: OperationContext<App> & NodeReportingContext) => MaybePromise<Output> | Required | Executes the already-authorized input; must not repeat authorization decisions. |
debugSteps | ((result: Output, context: import("../debug.js").NodeReportingContext) => void) | undefined | Optional | Optional result hook; writes through context.createSubFlowLogger() are recorded immediately. |
parseOutput | (value: unknown) => Output | Required | Validates untrusted output (for example a cached replay) before it is returned. |
active | ToolCondition<App> | undefined | Optional | Read-only. Omitted means active. Authorization remains a separate execution boundary. |
asIntent | AsIntent<App> | undefined | Optional | Read-only. Omitted means this tool is not advertised by withIntent(). |
resolve | (input: Input, context: Readonly<{ stageId: string; }>) => ToolProposal | Required | Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection. |
Examples
- Tool best practice: one definition generates the model tool and execution runtime: Register Schema, authorization, execution, and output validation once, and bind the App per request instead of maintaining two tool lists.
Define and use a read-only tool
Beginner · Node.js
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value: unknown) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new TypeError('需要空对象');
return {};
},
});
const catalog = Support.defineToolCatalog([{ id: 'get_status', kind: 'read', description: '读取服务状态', inputSchema,
authorize() {},
execute() { return '服务运行正常'; },
parseOutput(value: unknown) { if (typeof value !== 'string') throw new TypeError('需要字符串'); return value; },
}]);
const tools = catalog.bind({ app: undefined });
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);
console.log(catalog.tools.ids());import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
throw new TypeError('需要空对象');
return {};
},
});
const catalog = Support.defineToolCatalog([{ id: 'get_status', kind: 'read', description: '读取服务状态', inputSchema,
authorize() { },
execute() { return '服务运行正常'; },
parseOutput(value) { if (typeof value !== 'string')
throw new TypeError('需要字符串'); return value; },
}]);
const tools = catalog.bind({ app: undefined });
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);
console.log(catalog.tools.ids());Keep the list and add this request's permissions and service dependencies
Configuration · Node.js
import { Support } from '@jangts/agent-core';
import type { OperationContext } from '@jangts/agent-core/types';
type App = { allowed: boolean; readStatus(): Promise<string> };
const inputSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value: unknown) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new TypeError('需要空对象');
return {};
},
});
const catalog = Support.defineToolCatalog<App>([{ id: 'get_status', kind: 'read', description: '读取状态', inputSchema,
authorize(_input: {}, { app }: OperationContext<App>) { if (!app.allowed) throw new Error('当前请求没有读取权限'); },
execute(_input: {}, { app }: OperationContext<App>) { return app.readStatus(); },
parseOutput(value: unknown) { if (typeof value !== 'string') throw new TypeError('需要字符串'); return value; },
}]);
const tools = catalog.bind({ app: { allowed: true, readStatus: async () => '服务运行正常' },
execution: { signal: AbortSignal.timeout(10000) },
});
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
throw new TypeError('需要空对象');
return {};
},
});
const catalog = Support.defineToolCatalog([{ id: 'get_status', kind: 'read', description: '读取状态', inputSchema,
authorize(_input, { app }) { if (!app.allowed)
throw new Error('当前请求没有读取权限'); },
execute(_input, { app }) { return app.readStatus(); },
parseOutput(value) { if (typeof value !== 'string')
throw new TypeError('需要字符串'); return value; },
}]);
const tools = catalog.bind({ app: { allowed: true, readStatus: async () => '服务运行正常' },
execution: { signal: AbortSignal.timeout(10000) },
});
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);