refactor(mcp): migrate MCP demos to ToolCallbackProvider
- Replace List<ToolCallback> with ToolCallbackProvider for tool registration - Use MethodToolCallbackProvider.builder() pattern across all modules Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
@@ -122,23 +122,19 @@ logging.file.name=./target/mcp.webflux-server-starter.log
|
||||
```java
|
||||
@SpringBootApplication
|
||||
public class McpServerApplication {
|
||||
@Bean
|
||||
public List<ToolCallback> tools(OpenLibrary openLibrary) {
|
||||
List<ToolCallback> tools = new ArrayList<>();
|
||||
|
||||
// Add OpenLibrary tools
|
||||
tools.addAll(List.of(ToolCallbacks.from(openLibrary)));
|
||||
|
||||
// Add toUpperCase tool
|
||||
tools.add(FunctionToolCallback
|
||||
.builder("toUpperCase",
|
||||
(Function<ToUpperCaseInput, String>) s -> s.input().toUpperCase())
|
||||
.description("To upper case")
|
||||
.inputType(ToUpperCaseInput.class)
|
||||
.build());
|
||||
|
||||
return tools;
|
||||
}
|
||||
@Bean
|
||||
public ToolCallbackProvider openLibraryTools(OpenLibrary openLibrary) {
|
||||
return MethodToolCallbackProvider.builder().toolObjects(openLibrary).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ToolCallback tools(OpenLibrary openLibrary) {
|
||||
return FunctionToolCallback
|
||||
.builder("toUpperCase", (Function<ToUpperCaseInput, String>) s -> s.input().toUpperCase())
|
||||
.description("To upper case")
|
||||
.inputType(ToUpperCaseInput.class)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -17,8 +17,10 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.ai.tool.ToolCallbacks;
|
||||
import org.springframework.ai.tool.function.FunctionToolCallback;
|
||||
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -36,19 +38,17 @@ public class McpServerApplication {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public List<ToolCallback> tools(OpenLibrary openLibrary) {
|
||||
public ToolCallbackProvider openLibraryTools(OpenLibrary openLibrary) {
|
||||
return MethodToolCallbackProvider.builder().toolObjects(openLibrary).build();
|
||||
}
|
||||
|
||||
List<ToolCallback> tools = new ArrayList<>();
|
||||
|
||||
tools.addAll(List.of(ToolCallbacks.from(openLibrary)));
|
||||
|
||||
tools.add(FunctionToolCallback
|
||||
.builder("toUpperCase", (Function<ToUpperCaseInput, String>) s -> s.input().toUpperCase())
|
||||
.description("To upper case")
|
||||
.inputType(ToUpperCaseInput.class)
|
||||
.build());
|
||||
|
||||
return tools;
|
||||
@Bean
|
||||
public ToolCallback tools(OpenLibrary openLibrary) {
|
||||
return FunctionToolCallback
|
||||
.builder("toUpperCase", (Function<ToUpperCaseInput, String>) s -> s.input().toUpperCase())
|
||||
.description("To upper case")
|
||||
.inputType(ToUpperCaseInput.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -15,10 +15,8 @@
|
||||
*/
|
||||
package org.springframework.ai.mcp.samples.client;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -37,7 +35,7 @@ public class Application {
|
||||
private String userInput;
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, List<ToolCallback> tools,
|
||||
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools,
|
||||
ConfigurableApplicationContext context) {
|
||||
|
||||
return args -> {
|
||||
|
||||
@@ -15,10 +15,8 @@
|
||||
*/
|
||||
package org.springframework.ai.mcp.samples.client;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -37,7 +35,7 @@ public class Application {
|
||||
private String userInput;
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, List<ToolCallback> tools,
|
||||
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools,
|
||||
ConfigurableApplicationContext context) {
|
||||
|
||||
return args -> {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package org.springframework.ai.mcp.sample.server;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbacks;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -16,8 +14,8 @@ public class McpServerApplication {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public List<ToolCallback> weatherTools(WeatherService weatherService) {
|
||||
return List.of(ToolCallbacks.from(weatherService));
|
||||
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
|
||||
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package org.springframework.ai.mcp.sample.server;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbacks;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.ai.tool.function.FunctionToolCallback;
|
||||
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -17,20 +16,19 @@ public class McpServerApplication {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public List<ToolCallback> weatherTools(WeatherService weatherService) {
|
||||
return List.of(ToolCallbacks.from(weatherService));
|
||||
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
|
||||
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
|
||||
}
|
||||
|
||||
public record TextInput(String input) {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public List<ToolCallback> toUpperCase() {
|
||||
var tool = FunctionToolCallback.builder("toUpperCase", (TextInput input) -> input.input().toUpperCase())
|
||||
public ToolCallback toUpperCase() {
|
||||
return FunctionToolCallback.builder("toUpperCase", (TextInput input) -> input.input().toUpperCase())
|
||||
.inputType(TextInput.class)
|
||||
.description("Put the text to upper case")
|
||||
.build();
|
||||
return List.of(tool);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||
import org.springframework.ai.chat.memory.InMemoryChatMemory;
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -20,7 +21,7 @@ public class Application {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner chatbot(ChatClient.Builder chatClientBuilder, List<ToolCallback> tools) {
|
||||
public CommandLineRunner chatbot(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools) {
|
||||
|
||||
return args -> {
|
||||
|
||||
|
||||
@@ -37,9 +37,13 @@
|
||||
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<!-- <dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
|
||||
</dependency> -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-anthropic-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
@@ -15,10 +15,8 @@
|
||||
*/
|
||||
package org.springframework.ai.mcp.samples.brave;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -33,7 +31,7 @@ public class Application {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, List<ToolCallback> tools,
|
||||
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools,
|
||||
ConfigurableApplicationContext context) {
|
||||
|
||||
return args -> {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
spring.application.name=mcp
|
||||
spring.main.web-application-type=none
|
||||
|
||||
spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY}
|
||||
|
||||
spring.ai.openai.api-key=${OPENAI_API_KEY}
|
||||
|
||||
# There are two ways to configure the MCP client:
|
||||
|
||||
Reference in New Issue
Block a user