05BasicsRuns offline

Tools

Create tools and run tool calls without a model.

flowchart LR
  M([assistant message with tool_calls]) --> I[ToolInvoker] --> T([tool messages])

What it shows

  • createTool(name, description, fn, schema) turns a function into a tool.
  • ToolInvoker runs every call in a message. An unknown tool becomes an error message, not an exception.

The program

examples/tools/index.ts
import { ToolInvoker } from '@astack-tech/components';
import { createTool } from '@astack-tech/tools';

// A tool is a name, a description, a JSON Schema and a function.
const add = createTool('add', 'Add two numbers.', args => Number(args['a']) + Number(args['b']), {
  type: 'object',
  properties: { a: { type: 'number' }, b: { type: 'number' } },
  required: ['a', 'b'],
});

const now = createTool('now', 'Return the current ISO date.', () => new Date().toISOString());

// ToolInvoker runs the tool calls found in a message. A model usually writes this message;
// here it is written by hand so the example runs without an API key.
const invoker = new ToolInvoker({ tools: [add, now] });

const { tool_messages } = await invoker.run({
  messages: [
    {
      role: 'assistant',
      tool_calls: [
        { id: 'call-1', tool_name: 'add', arguments: { a: 2, b: 3 } },
        { id: 'call-2', tool_name: 'missing', arguments: {} },
      ],
    },
  ],
});

console.log(tool_messages);
// [
//   { role: 'tool', content: '5', tool_call_id: 'call-1' },
//   { role: 'tool', content: '"Tool not found: missing"', tool_call_id: 'call-2' }
// ]

Run it

pnpm install && pnpm build

pnpm --filter @astack-examples/tools start
CONCEPTToolsTurn functions, components and MCP methods into tools that agents can call.View the folder on GitHub