liveBook · Manning

defaultTools()

As explained in Spring AI in Action

defaultTools() is a ChatClient builder method that registers tools available to prompts sent through the resulting client.

defaultTools() is a ChatClient builder method that registers tool objects for every prompt sent through the resulting client. Spring injects the tool, and annotated methods become available to the LLM. section 6.1.1

Use defaultTools() when tools should be consistently available:

  • Inject a Spring-managed tool such as TimeTools or GameTools into the ChatClient configuration. section 6.1.1 section 6.2.3
  • Pass the tool to defaultTools() before calling build(). Any @Tool-annotated methods are then available in client prompts. section 6.2.3
  • Choose tools() instead when availability should be limited to an individual prompt. section 6.2.3
Listing 6.3 Applying tools when sending a prompt
package com.example.simpletools;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GetTimeController {

  private static final String CURRENT_TIME_TEMPLATE =
      "What is the current time in {city}?";

  private final ChatClient chatClient;

  public GetTimeController(ChatClient.Builder chatClientBuilder,
                           TimeTools timeTools) {   #1
    this.chatClient = chatClientBuilder
        .defaultTools(timeTools)   #2
        .build();
  }

  @GetMapping(path="/time", params = "city")
  public String getTime(@RequestParam("city") String city) {
    return chatClient.prompt()
          .user(userSpec -> {
              userSpec
                  .text(CURRENT_TIME_TEMPLATE)
                  .param("city", city);
            })
          .call()
          .content();
  }

}

Builds a ChatClient with TimeTools registered as a default tool. section 6.1.1

Current-time tool

A controller injects TimeTools, registers it with defaultTools(), and sends a city-based prompt. The LLM can then call getCurrentTime and return the specified local time. section 6.1.1

How Spring AI in Action covers it

How deep it goes
Explains build-time registration and contrasts it with prompt-time tools(), stopping short of tool invocation internals.
Examples
Demonstrates current-time lookup with TimeTools and board-game complexity answers with GameTools.
What it assumes
Assumes familiarity with ChatClient, Spring injection, and @Tool-annotated methods.
What you can do after
Apply it via ChatClient.Builder to make annotated application methods available to every client prompt.
How it connects
Connects ChatClient, ChatClient.Builder, @Tool methods, Spring dependency injection, and tools().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.