defineTool
The defineTool
function allows you to create a reusable tool that can be used by agents.
API Reference
defineTool(options)
Defines a tool with the following options:
name
(string, required): The name of the tool (should be camelCase or snake_case)description
(string | string[], required): The tool description for the LLM (should be descriptive and concise)input
(object or zchema, optional): A type definition of the input parametersoutput
(zchema, optional): A type definition of the output (defaults tovoid
)state
(object or zchema, optional): A type definition of the tool stateevent
(zchema, optional): A type definition of the tool eventhandler
(async function, required): The function that implements the tool logic- Receives
ToolContext
as an argument with:input
: The typed input parametersstate
: The tool state objectevents
: Array of tool eventstrace
: String for execution tracingagent
: The agent objectresult
: Helper for returning results
- Returns:
- A value of the defined
output
type (or a Promise of it) TOOL_AWAIT_RESULT
if the tool is waiting for events
- A value of the defined
- Receives
Example
import { defineTool } from 'agentscript-ai/core';
import * as s from 'agentscript-ai/schema';
const add = defineTool({
name: 'add',
description: 'Add two numbers.',
input: {
a: s.number({description: "First number"}),
b: s.number({description: "Second number"}),
},
output: s.number({description: 'The result of addition.'}),
handler: ({ input }) => input.a + input.b,
});
The defined tool can be used in agent definitions and will be available to the agent during execution.