Docs

Tool Calling & Programmatic Prompts

Register tool objects for LLM invocation and send prompts programmatically without a Message Input component.

Tools let the LLM call methods in your application during a conversation — query a database, look up an order, send an email, or any other action you want the assistant to be able to perform. Vaadin supports two ways to expose tools: tool objects registered with the AI framework (covered on this page) and framework-agnostic Controllers.

A third source of tools is an MCP server, whose tools the AI framework’s own client contributes to each request. See Framework Features.

Tool Calling

A tool object has methods with the @Tool annotation of the AI framework. The framework offers these methods to the LLM as tools and runs them when the LLM calls them. Register the tool objects with the framework when you create the LLM provider. The annotation and the place to register depend on the framework.

For Spring AI, use @org.springframework.ai.tool.annotation.Tool with a description attribute. Add the tool objects to the ChatClient that you pass to the provider:

Source code
Java
public class WeatherTools {
    // Spring AI: org.springframework.ai.tool.annotation.Tool
    @Tool(description = "Get current weather for a city")
    public String getWeather(String city) {
        return weatherService.getCurrentWeather(city);
    }
}

ChatClient chatClient = ChatClient.builder(chatModel)
        .defaultTools(new WeatherTools())
        .build();
SpringAiLlmProvider provider = new SpringAiLlmProvider(chatClient);

For LangChain4j, use @dev.langchain4j.agent.tool.Tool, whose description is the annotation’s value.

Add the tool objects in a customizer of the provider. LangChain4jLlmProvider takes the customizer as an optional second constructor argument, and passes it the builder of the LangChain4j AI service that it runs each turn through:

Source code
Java
public class WeatherTools {
    // LangChain4j: dev.langchain4j.agent.tool.Tool
    @Tool("Get current weather for a city")
    public String getWeather(String city) {
        return weatherService.getCurrentWeather(city);
    }
}

LangChain4jLlmProvider provider = new LangChain4jLlmProvider(chatModel,
        service -> service.tools(new WeatherTools()));

Every conversation that runs through the same ChatClient calls the same tool objects. When a tool works with the state of one view, such as its components, create the client or the provider for that view.

Note
Tool Threading
With a streaming provider or background execution, tool methods are invoked off the request thread, where UI.getCurrent() and other Vaadin thread locals are not available. Wrap component access in ui.access(), or capture the needed state before the turn starts.
Tip
Framework-Agnostic Tools via Controllers
For a reusable set of tools that does not depend on a specific LLM framework’s annotations, or when a lifecycle hook is needed after each LLM request cycle, implement AiController instead. GridAiController, ChartAiController, and FormAiController are built-in examples. A controller can be combined with tool objects registered with the AI framework.
Note
Tool Errors

Tool objects are executed by the AI framework, whose own error handling decides what the LLM sees when a tool throws. By default, both LangChain4j and Spring AI relay the raw exception message. To control what the LLM learns about failures, replace that default in the framework:

  • Spring AI: A ToolExecutionExceptionProcessor decides what the LLM sees. See Exception Handling in the Spring AI documentation.

  • LangChain4j: Set a ToolExecutionErrorHandler with toolExecutionErrorHandler() in the customizer. See Handling Tool Execution Errors in the LangChain4j documentation.

You can also define the tool through a controller instead, and throw a ToolException for messages the LLM is meant to see; see Tool Error Handling.

Programmatic Prompts

Send prompts without a Message Input component using prompt(). This is useful for triggering AI interaction from button clicks or other events:

Source code
AiOrchestratorProgrammaticPrompt.java

Programmatic prompts go through the same pipeline as chat submits: a configured request interceptor can change or reject them before anything is sent.

Note
One Request at a Time
The orchestrator processes one prompt at a time. If prompt() is called while a previous request is still streaming, the new call is logged as a warning and silently dropped. Wait for the current response to complete before sending another prompt.
Important
UI Context Required
prompt() requires an active UI context. If called from a background thread or outside a Vaadin request, it throws an IllegalStateException. Always call prompt() from within a UI event handler or wrap the call in ui.access().

Updated –