Overview

A ToolType consists of the following main components:

type
ToolTypesType

The type of tool.

Constraints: Must be one of the defined tool types.

definition
ToolDefinition

The tool definition containing the schema.

Constraints: Must contain a valid schema.

Tool Types

The type field uses a discriminated union of predefined tool types.

ToolTypesType

"function"
string

Function-based tools.

Usage: Used for defining callable functions with parameters.

Currently, only one tool type is supported:

// Example of a function tool
const searchTool: ToolType = {
  type: "function",
  definition: {
    schema: {
      name: "search_database",
      description: "Search for records in the database",
      parameters: {
        type: "object",
        properties: {
          query: {
            type: "string",
            description: "The search query",
          },
          limit: {
            type: "number",
            description: "Maximum number of results",
            default: 10,
          },
        },
        required: ["query"],
      },
    },
  },
};

Function Tool

FunctionToolType

Represents a function that can be called by the LLM.

type
"function"

Tool type discriminator.

Constraints: Must be "function".

definition
FunctionToolDefinition

Contains the function schema.

Constraints: Must contain valid function schema.

FunctionToolDefinition

schema
FunctionType

The function schema definition.

Constraints: Must be a valid function schema.

Function Schema

FunctionType

Defines the structure and behavior of a callable function.

name
string

Function name.

Constraints: 1-64 characters, alphanumeric and underscore only.

description
string

Function description.

Constraints: Maximum 4096 characters.

parameters
any

Function parameters schema.

Constraints: Flexible parameter definition.

strict
boolean

Whether to enforce strict parameter validation.

Constraints: Optional field.

Name Validation

The function name must follow these rules:

  • Length: 1-64 characters
  • Characters: Only letters (a-z, A-Z), numbers (0-9), and underscores (_)
  • Pattern: ^[a-zA-Z0-9_]{1,64}$
// Valid function names
"search_database"; // ✅ Valid
"getUserInfo"; // ✅ Valid
"calculate_2024_tax"; // ✅ Valid

// Invalid function names
"search-database"; // ❌ Contains hyphen
"search database"; // ❌ Contains space
"search.database"; // ❌ Contains dot
(""); // ❌ Empty string
("a_very_long_function_name_that_exceeds_sixty_four_characters_limit"); // ❌ Too long

Function Parameters

FunctionParametersType

Defines the structure for function parameters using JSON Schema-like definitions.

type
"object"

Parameter container type.

Constraints: Must be "object".

title
string

Optional title for the parameters.

Constraints: Optional field.

$defs
Record<string, any>

Schema definitions for reusable components.

Constraints: Optional field.

properties
Record<string, FunctionParameterType>

Parameter property definitions.

Constraints: Optional field.

required
Array<string>

List of required parameter names.

Constraints: Optional field.

FunctionParameterType

Defines individual parameter properties with comprehensive JSON Schema support.

anyOf
Array<any>

Union type definitions.

Constraints: Optional field.

type
FunctionParameterTypesType

Parameter data type

Constraints: Optional, see supported types below.

default
any

Default value for the parameter.

Constraints: Optional field.

title
string

Parameter title.

Constraints: Optional field.

description
string

Parameter description.

Constraints: Maximum 4096 characters.

properties
Record<string, any>

Nested object properties

Constraints: Optional, for object types.

required
Array<string>

Required nested properties.

Constraints: Optional, for object types.

minItems
number

Minimum array length.

Constraints: Optional, for array types, non-negative

maxItems
number

Maximum array length.

Constraints: Optional, for array types

items
Record<string, any>

Array item schema.

Constraints: Optional, for array types

enum
Array<string | number | boolean | null>

Allowed values.

Constraints: Optional field.

minimum
number

Minimum numeric value.

Constraints: Optional, for number types

maximum
number

Maximum numeric value.

Constraints: Optional, for number types

minLength
number

Minimum string length.

Constraints: Optional, for string types, non-negative

maxLength
number

Maximum string length.

Constraints: Optional, for string types

FunctionParameterTypesType

Supported parameter data types:

"object"
string

Object/dictionary type.

Usage: For complex nested structures.

"array"
string

Array type.

Usage: For lists of items.

"number"
string

Numeric type.

Usage: For integers and floating-point numbers.

"string"
string

String type.

Usage: For text values.

"boolean"
string

Boolean type.

Usage: For true/false values.

"null"
string

Null type.

Usage: For null values.

Complete Examples

Simple Function Tool

const simpleFunction: ToolType = {
  type: "function",
  definition: {
    schema: {
      name: "get_weather",
      description: "Get current weather information for a location",
      parameters: {
        type: "object",
        properties: {
          location: {
            type: "string",
            description: "The city and state, e.g. San Francisco, CA",
          },
        },
        required: ["location"],
      },
    },
  },
};

Complex Function Tool with Advanced Parameters

const complexFunction: ToolType = {
  type: "function",
  definition: {
    schema: {
      name: "analyze_data",
      description: "Analyze data with various filters and options",
      parameters: {
        type: "object",
        properties: {
          data_source: {
            type: "string",
            description: "The data source to analyze",
            enum: ["database", "api", "file"],
          },
          filters: {
            type: "object",
            properties: {
              date_range: {
                type: "object",
                properties: {
                  start: {
                    type: "string",
                    description: "Start date in ISO format",
                  },
                  end: {
                    type: "string",
                    description: "End date in ISO format",
                  },
                },
                required: ["start", "end"],
              },
              categories: {
                type: "array",
                items: {
                  type: "string",
                },
                minItems: 1,
                maxItems: 10,
                description: "Categories to include in analysis",
              },
            },
          },
          limit: {
            type: "number",
            description: "Maximum number of results to return",
            minimum: 1,
            maximum: 1000,
            default: 100,
          },
          include_metadata: {
            type: "boolean",
            description: "Whether to include metadata in results",
            default: false,
          },
        },
        required: ["data_source"],
      },
      strict: true,
    },
  },
};

Function with String Constraints

const stringConstraintsFunction: ToolType = {
  type: "function",
  definition: {
    schema: {
      name: "create_user",
      description: "Create a new user account",
      parameters: {
        type: "object",
        properties: {
          username: {
            type: "string",
            description: "User's username",
            minLength: 3,
            maxLength: 20,
          },
          email: {
            type: "string",
            description: "User's email address",
          },
          role: {
            type: "string",
            description: "User's role in the system",
            enum: ["admin", "user", "moderator"],
          },
        },
        required: ["username", "email"],
      },
    },
  },
};

Function with Nested Objects and Arrays

const nestedStructureFunction: ToolType = {
  type: "function",
  definition: {
    schema: {
      name: "process_order",
      description: "Process a customer order",
      parameters: {
        type: "object",
        properties: {
          customer: {
            type: "object",
            properties: {
              id: {
                type: "string",
                description: "Customer ID",
              },
              contact: {
                type: "object",
                properties: {
                  email: { type: "string" },
                  phone: { type: "string" },
                },
                required: ["email"],
              },
            },
            required: ["id"],
          },
          items: {
            type: "array",
            description: "Items in the order",
            items: {
              type: "object",
              properties: {
                product_id: { type: "string" },
                quantity: {
                  type: "number",
                  minimum: 1,
                },
                price: {
                  type: "number",
                  minimum: 0,
                },
              },
              required: ["product_id", "quantity", "price"],
            },
            minItems: 1,
          },
        },
        required: ["customer", "items"],
      },
    },
  },
};

Best Practices

  1. Descriptive Names: Use clear, descriptive function names that indicate the action being performed
  2. Comprehensive Descriptions: Provide detailed descriptions for both functions and parameters to help the LLM understand usage
  3. Appropriate Constraints: Use parameter constraints (min/max, length limits, enums) to ensure valid inputs
  4. Required vs Optional: Clearly mark which parameters are required vs optional
  5. Default Values: Provide sensible defaults for optional parameters when appropriate
  6. Nested Structure: Use nested objects and arrays when dealing with complex data structures