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
