liveBook · Manning

defaultToolCallbacks()

As explained in Spring AI in Action

defaultToolCallbacks() is a ChatClient builder method that registers a provider's tool callbacks as default callable tools.

defaultToolCallbacks() configures a ChatClient with the tools exposed by an MCP server. Pass a ToolCallbackProvider to the ChatClient builder so the model can call those tools while answering user questions. section 7.2

Register MCP tools in a controller

Inject ChatClient.Builder and ToolCallbackProvider, then build the client with chatClientBuilder.defaultToolCallbacks(tools).build(). Requests such as listing, creating, or reading files can then be handled through the MCP server's callable tools. section 7.2

Listing 7.2 An MCP-enabled controller for answering questions
package com.example.mcpfilesystemclient;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class McpAskController {

    private final ChatClient chatClient;

    public McpAskController(ChatClient.Builder chatClientBuilder,
                            ToolCallbackProvider tools) {  #1
        this.chatClient = chatClientBuilder
                .defaultToolCallbacks(tools)   #2
                .build();
    }

    @PostMapping("/ask")
    public Answer ask(@RequestBody Question question) {
        return chatClient.prompt()
                .user(question.question())
                .call()
                .entity(Answer.class);
    }

    public record Question(String question) { }

    public record Answer(String answer) { }

}

Shows the complete MCP-enabled controller, including ToolCallbackProvider injection and the defaultToolCallbacks(tools) builder call. section 7.2

How Spring AI in Action covers it

How deep it goes
Explains provider injection and builder initialization, but does not detail callback or model tool-call internals.
Examples
Uses a filesystem MCP server to list files, create a file, and read its contents through an HTTP endpoint.
What it assumes
Assumes a configured MCP client and server, while demonstrating the controller wiring required to expose server tools.
What you can do after
Build an MCP-enabled ChatClient whose model can invoke tools supplied by a ToolCallbackProvider.
How it connects
Connects ChatClient, ToolCallbackProvider, MCP clients, MCP servers, and tool callbacks.

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.