Tool Calling

How to expose Kaya functions to the LLM as tools

Kaya functions can be exposed to LLMs as tools, allowing the AI to call your functions during generation.

When an agent client supplies MCP tools for a session, Kaya registers them using <server>__<tool> names. See Agent Client Protocol for that separate attachment path.

The examples below require an admitted model alias or a configured session default. See Text model bindings. The flight, weather and email functions are illustrative local implementations; their returned data does not prove an external service was called.

Granted Tools

A named tool selects a capability already granted to the session. For example, this fragment requires a host-configured kaya.tool.read_range grant with an admitted workspace:

let result = generate(
  message: "Read the project's README and summarize it",
  tools: [{name: "read_range"}]
)

The admitted tool catalog supplies its description and argument schema. Listing a name does not grant access, select a host executable, or install a provider. An unadmitted name is refused.

Function Shorthand

Expose any Kaya function as a tool by referencing it:

#description:"Calculate the area of a rectangle"
function calculateArea(width: number, height: number): number {
  return width * height
}

let answer = generate(
  message: "What's the area of a 5x10 rectangle?",
  tools: [{"function": "calculateArea"}]
)
Run in Browser Text model required

The LLM can call calculateArea during generation. The #description annotation helps the LLM understand when to use it.

Tool Parameters Auto-Extraction

Function parameters are automatically converted to tool schemas:

#description:"Search for flights between cities"
function searchFlights(
  origin: string,
  destination: string,
  date: string
): json {
  // Search implementation
  return {
    flightCount: 5
  }
}

let flights = generate(
  message: "Find me a flight from San Francisco to Paris on June 1st",
  tools: [{"function": "searchFlights"}],
  chat: true
)

The LLM receives a tool schema derived from the function signature:

  • Parameter names: origin, destination, date
  • Parameter types: all string
  • Description: "Search for flights between cities"

(Note: from is a reserved keyword in Kaya — it cannot be used as a parameter name.)

Multiple Tools

#description:"Get current weather for a city"
function getWeather(city: string): json {
  return {temp: 72, condition: "sunny"}
}

#description:"Convert temperature between units"
function convertTemp(value: number, fromUnit: string, toUnit: string): number {
  // Conversion logic
  let converted = value
  return converted
}

let response = generate(
  message: "What's the weather in Tokyo in Celsius?",
  tools: [
    {"function": "getWeather"},
    {"function": "convertTemp"}
  ]
)

The LLM can use multiple tools in a single generation.

Tool Reference Objects

Native generation accepts named tool references and Kaya function references:

1. Granted tool by name — selects the session's kaya.tool.read_range capability:

let result = generate(
  message: "Read the project's README",
  tools: [{name: "read_range"}]
)

2. Function shorthand — derives the schema from a Kaya function's signature:

let result = generate(
  message: "What's the area of a 5x10 rectangle?",
  tools: [{"function": "calculateArea"}]
)

Optional name and description keys override a function tool's model-visible name and description:

function lookupOrder(orderId: string): json {
  return {id: orderId, status: "pending"}
}

let result = generate(
  message: "Look up order 12345",
  tools: [{
    name: "lookupOrder",
    "function": "lookupOrder",
    description: "Look up an order by its ID"
  }]
)

The native generate() tool reference admits only name, function, and description, with literal string values. graphRef, inputSchema, outputSchema, and {graph: ...} references are not supported.

Tool Calling Flow

#description:"Fetch user profile from database"
function getUserProfile(userId: string): json {
  return {
    id: userId,
    name: "Alice",
    email: "alice@example.com"
  }
}

#description:"Send email to user"
function sendEmail(to: string, subject: string, body: string): boolean {
  "Sending email to {{to}}"
  return true
}

// LLM can call both tools as needed
let result = generate(
  message: "Send a welcome email to user with ID 12345",
  tools: [
    {"function": "getUserProfile"},
    {"function": "sendEmail"}
  ]
)

"Operation result: {{result}}"

Execution flow:

  1. LLM receives message and available tools
  2. LLM decides to call getUserProfile("12345")
  3. Function executes, returns user data
  4. LLM decides to call sendEmail("alice@example.com", "Welcome", "...")
  5. Function executes
  6. LLM generates final response incorporating tool results

Tool with Type Validation

type FlightQuery = {
  origin: string + { min: 3 },
  destination: string + { min: 3 },
  date: string + { regex: "^\\d{4}-\\d{2}-\\d{2}$" }
}

#description:"Search for available flights"
function searchFlights(query: FlightQuery): json {
  return {
    flightCount: 5
  }
}

let flights = generate(
  message: "Find flights from NYC to LAX on 2024-06-01",
  tools: [{"function": "searchFlights"}]
)

Custom types ensure tool parameters meet validation requirements.

Conditional Tool Calling

Choose between calls with literal tool lists. This fragment uses getWeather from the earlier example:

let needsTools = true
let userQuery = "What's the weather in Tokyo?"

if needsTools {
  let response = generate(
    message: userQuery,
    tools: [{"function": "getWeather"}]
  )
  "{{response}}"
} else {
  let response = generate(message: userQuery, tools: [])
  "{{response}}"
}

Passing a variable such as tools: toolList to native generate() or chat() is refused during admission. Every selected named tool still requires its session grant.