MCP Utilities

MCP 工具为将模型上下文协议 (Model Context Protocol) 与 Spring AI 应用程序集成提供了基础支持。这些工具实现了 Spring AI 的工具系统与 MCP 服务器之间的无缝通信,支持同步和异步操作。它们通常用于程序化 MCP 客户端和服务器的配置和交互。为了更简化的配置,请考虑使用引导启动器。

The MCP utilities provide foundational support for integrating Model Context Protocol with Spring AI applications. These utilities enable seamless communication between Spring AI’s tool system and MCP servers, supporting both synchronous and asynchronous operations. They are typically used for programmatic MCP Client and Server configuration and interaction. For a more streamlined configuration, consider using the boot starters.

ToolCallback Utility

Tool Callback Adapter

使 MCP 工具适应 Spring AI 的工具接口,支持同步和异步执行。

Adapts MCP tools to Spring AI’s tool interface with both synchronous and asynchronous execution support.

  • Sync

  • Async

McpSyncClient mcpClient = // obtain MCP client
Tool mcpTool = // obtain MCP tool definition
ToolCallback callback = new SyncMcpToolCallback(mcpClient, mcpTool);

// Use the tool through Spring AI's interfaces
ToolDefinition definition = callback.getToolDefinition();
String result = callback.call("{\"param\": \"value\"}");
McpAsyncClient mcpClient = // obtain MCP client
Tool mcpTool = // obtain MCP tool definition
ToolCallback callback = new AsyncMcpToolCallback(mcpClient, mcpTool);

// Use the tool through Spring AI's interfaces
ToolDefinition definition = callback.getToolDefinition();
String result = callback.call("{\"param\": \"value\"}");

Tool Callback Providers

从 MCP 客户端发现并提供 MCP 工具。

Discovers and provides MCP tools from MCP clients.

Sync
McpSyncClient mcpClient = // obtain MCP client
ToolCallbackProvider provider = new SyncMcpToolCallbackProvider(mcpClient);

// Get all available tools
ToolCallback[] tools = provider.getToolCallbacks();

对于多个客户端:

For multiple clients:

List<McpSyncClient> clients = // obtain list of clients
List<ToolCallback> callbacks = SyncMcpToolCallbackProvider.syncToolCallbacks(clients);
Async
McpAsyncClient mcpClient = // obtain MCP client
ToolCallbackProvider provider = new AsyncMcpToolCallbackProvider(mcpClient);

// Get all available tools
ToolCallback[] tools = provider.getToolCallbacks();

对于多个客户端:

For multiple clients:

List<McpAsyncClient> clients = // obtain list of clients
Flux<ToolCallback> callbacks = AsyncMcpToolCallbackProvider.asyncToolCallbacks(clients);

McpToolUtils

ToolCallbacks to ToolSpecifications

将 Spring AI 工具回调转换为 MCP 工具规范:

Converting Spring AI tool callbacks to MCP tool specifications:

Sync
List<ToolCallback> toolCallbacks = // obtain tool callbacks
List<SyncToolSpecifications> syncToolSpecs = McpToolUtils.toSyncToolSpecifications(toolCallbacks);

然后你可以使用 McpServer.SyncSpecification 注册工具规范:

then you can use the McpServer.SyncSpecification to register the tool specifications:

McpServer.SyncSpecification syncSpec = ...
syncSpec.tools(syncToolSpecs);
Async
List<ToolCallback> toolCallbacks = // obtain tool callbacks
List<AsyncToolSpecification> asyncToolSpecifications = McpToolUtils.toAsyncToolSpecifications(toolCallbacks);

然后你可以使用 McpServer.AsyncSpecification 注册工具规范:

then you can use the McpServer.AsyncSpecification to register the tool specifications:

McpServer.AsyncSpecification asyncSpec = ...
asyncSpec.tools(asyncToolSpecifications);

MCP Clients to ToolCallbacks

从 MCP 客户端获取工具回调

Getting tool callbacks from MCP clients

  • Sync

  • Async

List<McpSyncClient> syncClients = // obtain sync clients
List<ToolCallback> syncCallbacks = McpToolUtils.getToolCallbacksFromSyncClients(syncClients);
List<McpAsyncClient> asyncClients = // obtain async clients
List<ToolCallback> asyncCallbacks = McpToolUtils.getToolCallbacksFromAsyncClients(asyncClients);

Native Image Support

McpHints 类为 MCP 模式类提供了 GraalVM 本机映像提示。该类在构建本机映像时自动注册 MCP 模式类的所有必要反射提示。

The McpHints class provides GraalVM native image hints for MCP schema classes. This class automatically registers all necessary reflection hints for MCP schema classes when building native images.