TypeScriptTools

Tools

Expose actions an AI model can call, with validated input

Tools are how an AI model acts through your server. A tool is a method on a class, marked with @Tool: the model sees its name, its description, and its input schema, decides when to call it, and gets back whatever the method returns. NitroStack validates the input before your method runs, so handlers stay small.

Basic Tool Definition

Tools are defined using the @Tool decorator on class methods:

import { ToolDecorator as Tool, z, ExecutionContext } from '@nitrostack/core';

export class WeatherTools {
  @Tool({
    name: 'get_weather',
    description: 'Retrieve current weather conditions for a specified location',
    inputSchema: z.object({
      city: z.string().describe('City name (e.g., "San Francisco")'),
      units: z.enum(['celsius', 'fahrenheit']).optional().default('celsius')
        .describe('Temperature unit preference')
    })
  })
  async getWeather(
    input: { city: string; units?: 'celsius' | 'fahrenheit' },
    context: ExecutionContext
  ) {
    context.logger.info('Fetching weather data', { city: input.city });

    const weather = await this.weatherService.getCurrentConditions(input.city);

    return {
      city: input.city,
      temperature: input.units === 'fahrenheit'
        ? this.toFahrenheit(weather.tempCelsius)
        : weather.tempCelsius,
      units: input.units ?? 'celsius',
      conditions: weather.conditions,
      humidity: weather.humidity,
      timestamp: new Date().toISOString()
    };
  }
}

Tool topics

On this page