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
Controllers and Initial Tools
Prefix related tools, and run a tool when a client connects.
Options and Annotations
Every @Tool option, and behavior hints for clients.
Input and Output
Zod input schemas, output schemas, and resource links.
Execution Context in Tools
Logging, caller identity, and cancellation in a handler.
Ask the User for Input
Pause a tool for confirmation with multi round-trip requests.
Middleware, Caching, and Errors
Middleware, caching, rate limits, services, and errors.
Tool Widgets
Render a React widget with the tool result.
Dynamic Registration
Register tools at runtime.
