Kest

Search

Search the documentation

AI

Tools

Let a model call your methods.

On its own a model cannot look up an order or check stock. A tool is a method of yours that the model may ask you to run. You describe the tool (name, what it does, its inputs); the model decides when it is useful and replies "please call find_order with id = 1042"; Kest runs it and gives the result back; the model then answers using it.

Loading diagram…

Mark a method with @AiTool. Its parameter names and types become the JSON schema the model sees; the return value goes back as text (JSON for objects):

@Injectable
class OrderTools(private val orders: OrderRepository) {
 
    @AiTool("find_order", "Looks an order up by its id. Returns null when there is none.")
    fun find(id: String): Order? = orders.findById(id)
 
    @AiTool("cancel_order", "Cancels an order that has not shipped. Only call when the customer clearly asks.")
    fun cancel(id: String, reason: String?): String = orders.cancel(id, reason)
}
  • A non-nullable parameter is required; make a parameter nullable (String?) to make it optional. (Kotlin default values are not visible to the framework.)
  • A tool that throws is reported to the model as an error, so it can correct itself (a wrong id, a missing argument) and try again.
  • The same method can also be @McpTool, to serve it to outside assistants (MCP).
  • Add tools by hand: registry.add("now", "The current date and time") { Instant.now().toString() }.

Write good tools

  • The description is the instruction. Say what it does, what it returns, and when not to use it. Models follow descriptions closely.
  • Small, focused tools beat one giant tool. Prefer find_order + list_orders over orders(action, ...).
  • Return only what the model needs (not whole database rows).
  • Validate inputs inside the tool as you would for an HTTP endpoint: the model can send wrong values.

Safety: the model is not your user

What an agent can do is exactly the tools you give it.

  • Do not give a model a tool you would not give an anonymous caller. Check permissions inside the tool against the real signed-in user (passed by you), never against something the model says.
  • Make destructive or costly actions (cancel, refund, send email) require a confirmation step in your own code, or leave them out.
  • Text the model reads (a customer's message, a web page) can contain instructions meant to trick it (prompt injection). Tools must be safe even if the model is tricked.
  • Put limits in the tool itself (a refund of at most 50 without a human).

Read more