ToolRegistryPorts
CORE · interface · Type contract
Host ports a tool registry needs in addition to the tool definitions.
Import
ts
import type { ToolRegistryPorts } from '@jangts/agent-core/tools';js
/** @typedef {import('@jangts/agent-core/tools').ToolRegistryPorts} ToolRegistryPorts */Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/tools.
Definition
ts
interface ToolRegistryPortsField structure
| Field | Type | Required | Description |
|---|---|---|---|
signal | AbortSignal | undefined | Optional | Request lifetime; late external results cannot become successful receipts. |
diagnostics | DiagnosticContext | undefined | Optional | See the type definition and usage context. |
unavailable | ((name: string) => Error) | undefined | Optional | Builds the error for a tool id that is not available in this request. |
errorCode | ((error: unknown) => string) | undefined | Optional | Maps an arbitrary thrown value to a stable error code for events. |
Examples
- Real tool invocation and in-request runtime: Core registers and executes real tools and returns receipts carrying an invocation; business authorization is owned by the implementation registered by the App.
- 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.
- Tools: App tool lifecycle and content contribution: Register tool invocations, output rules, context contribution, and delivery handling for existing complex App tools in one place.
Minimal local read-only tool
Beginner · Node.js
ts
import { ToolRegistry } from '@jangts/agent-core/tools';
const tools = new ToolRegistry().register({
id: 'get_status', kind: 'read', description: '读取演示服务状态',
parseInput(value: unknown) { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('需要对象'); return {}; },
execute: () => '服务运行正常',
parseOutput(value: unknown) { if (typeof value !== 'string') throw new TypeError('需要文本'); return value; },
}).seal();
const result = tools.invoke({ toolId: 'get_status', input: {} });
console.log(result.value, result.invocation.invocationId);js
import { ToolRegistry } from '@jangts/agent-core/tools';
const tools = new ToolRegistry().register({
id: 'get_status', kind: 'read', description: '读取演示服务状态',
parseInput(value) { if (!value || typeof value !== 'object' || Array.isArray(value))
throw new TypeError('需要对象'); return {}; },
execute: () => '服务运行正常',
parseOutput(value) { if (typeof value !== 'string')
throw new TypeError('需要文本'); return value; },
}).seal();
const result = tools.invoke({ toolId: 'get_status', input: {} });
console.log(result.value, result.invocation.invocationId);Bind in advance so nodes no longer inject tools repeatedly
Configuration · Node.js
The first example validates external input strictly; the second only shows the binding relationship, and real business code should use the appropriate Schema and authorization.
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
import { ToolRegistry } from '@jangts/agent-core/tools';
type State = { answer?: unknown };
const tools = new ToolRegistry().register({ id: 'get_status', kind: 'read',
parseInput: () => ({}), execute: async () => '服务运行正常', parseOutput: value => value,
}).seal();
const workflow = defineWorkflow<State, unknown>({ id: 'status', nodes: [{
id: 'read', description: '读取状态', startsRound: true,
async run(_state, { tools }) { const { value } = await tools.invoke({ toolId: 'get_status', input: {} }); return { answer: value }; },
routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow }).bindToolRuntime(tools);
console.log(await agent.run({}));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
import { ToolRegistry } from '@jangts/agent-core/tools';
const tools = new ToolRegistry().register({ id: 'get_status', kind: 'read',
parseInput: () => ({}), execute: async () => '服务运行正常', parseOutput: value => value,
}).seal();
const workflow = defineWorkflow({ id: 'status', nodes: [{
id: 'read', description: '读取状态', startsRound: true,
async run(_state, { tools }) { const { value } = await tools.invoke({ toolId: 'get_status', input: {} }); return { answer: value }; },
routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow }).bindToolRuntime(tools);
console.log(await agent.run({}));Related policy source
- Check order for tool input, execution, output, and late results
- Authorization and model tool projection come from the same definition
- Registration policy, lifecycle binding, and content projection