Skip to content

Interface: Tool

@providerprotocol/ai


@providerprotocol/ai / @providerprotocol/ai / Tool

Defined in: src/types/tool.ts:115

Tool definition for LLM function calling.

Defines a tool that can be called by the LLM, including its name, description, parameter schema, and execution function.

const weatherTool: Tool<{ location: string }, WeatherData> = {
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' }
},
required: ['location']
},
run: async (params) => {
return fetchWeather(params.location);
}
};

TParams = unknown

The type of parameters the tool accepts

TResult = unknown

The type of result the tool returns

description: string

Defined in: src/types/tool.ts:120

Human-readable description for the model to understand when to use this tool


optional metadata: ToolMetadata

Defined in: src/types/tool.ts:142

Provider-specific metadata, namespaced by provider name.

Used for provider-specific features like prompt caching:

const tool: Tool = {
name: 'search_docs',
description: 'Search documentation',
parameters: {...},
run: async (params) => {...},
metadata: {
anthropic: { cache_control: { type: 'ephemeral' } }
}
};

name: string

Defined in: src/types/tool.ts:117

Tool name (must be unique within an llm() instance)


parameters: JSONSchema

Defined in: src/types/tool.ts:123

JSON Schema defining the tool’s parameters

optional approval(params): boolean | Promise<boolean>

Defined in: src/types/tool.ts:161

Optional approval handler for sensitive operations.

If provided, this function is called before the tool executes. Return false to prevent execution.

TParams

The parameters the tool would be called with

boolean | Promise<boolean>

Whether to approve the execution


run(params): TResult | Promise<TResult>

Defined in: src/types/tool.ts:150

Executes the tool with the provided parameters.

TParams

The parameters passed by the model

TResult | Promise<TResult>

The tool result, synchronously or as a Promise