feat(autoconfigure): Support both FunctionCallback and ToolCallback in ToolCallingAutoConfiguration

- Extends the ToolCallingAutoConfiguration to support both FunctionCallback and ToolCallback types.
- The toolCallbackResolver bean now handles both callback types through ObjectProvider injection.
- Added comprehensive tests to verify the resolution of multiple function and tool callbacks.
- Introduce new StaticToolCallbackProvider implementation
- Update ToolCallbackProvider to return FunctionCallback[]
- Migrate from List to ToolCallbackProvider in configurations
- Update tests to use new provider pattern
- Enhance tool callback providers to support multiple clients
  - Refactor AsyncMcpToolCallbackProvider and SyncMcpToolCallbackProvider to handle multiple MCP clients
  - Add ToolCallbackProvider support to ChatClient API
  - Deprecate direct tool callback list methods in favor of providers
  - Fix typos in Closeable class names
  - Update MCP documentation with new examples and usage patterns

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
Christian Tzolov
2025-02-13 13:11:09 +01:00
parent 68ad742f4a
commit 1fdda61db8
16 changed files with 486 additions and 108 deletions

View File

@@ -351,12 +351,14 @@ private List<McpSyncClient> mcpSyncClients; // For sync client
private List<McpAsyncClient> mcpAsyncClients; // For async client
----
Additionally, the registered MCP Tools with all MCP clients are provided as a list of ToolCallback instances:
Additionally, the registered MCP Tools with all MCP clients are provided as a list of ToolCallback
through a ToolCallbackProvider instance:
[source,java]
----
@Autowired
private List<ToolCallback> toolCallbacks;
private SyncMcpToolCallbackProvider toolCallbackProvider;
ToolCallback[] toolCallbacks = toolCallbackProvider.getToolCallbacks();
----
== Example Applications

View File

@@ -126,9 +126,9 @@ Allows servers to expose tools that can be invoked by language models. The MCP S
[source,java]
----
@Bean
public List<ToolCallback> myTools(...) {
public ToolCallbackProvider myTools(...) {
List<ToolCallback> tools = ...
return tools;
return ToolCallbackProvider.from(tools);
}
----
@@ -284,15 +284,15 @@ public class McpServerApplication {
SpringApplication.run(McpServerApplication.class, args);
}
@Bean
public List<ToolCallback> tools(WeatherService weatherService) {
return ToolCallbacks.from(weatherService);
}
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
}
----
The auto-configuration will automatically register the tool callbacks as MCP tools.
You can have multiple beans producing lists of ToolCallbacks. The auto-configuration will merge them.
You can have multiple beans producing ToolCallbacks. The auto-configuration will merge them.
== Example Applications
* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webflux-server[Weather Server (WebFlux)] - Spring AI MCP Server Boot Starter with WebFlux transport.