Class McpModernToolUtils

java.lang.Object
org.springframework.ai.mcp.modern.McpModernToolUtils

public final class McpModernToolUtils extends Object
Exposes Spring AI ToolCallbacks as tools of a 2026-07-28 MCP server.

The McpRequestContext of the current call is available to the callback through the ToolContext under the TOOL_CONTEXT_MCP_REQUEST_CONTEXT_KEY key. It deliberately does not reuse the legacy exchange key, whose accessor casts to the session-based exchange type.

A ToolCallback blocks, so the asynchronous tools run it through McpModernAsyncUtils.fromBlocking(io.modelcontextprotocol.modern.server.McpRequestContext, java.util.concurrent.Callable<T>): on the calling thread when the request is served blocking, on boundedElastic otherwise.

Since:
2.1.0
Author:
Christian Tzolov
  • Field Details

  • Method Details

    • toSyncTools

      public static List<io.modelcontextprotocol.modern.server.feature.McpSyncTool> toSyncTools(List<ToolCallback> toolCallbacks)
      Converts the given callbacks to modern tools.
      Parameters:
      toolCallbacks - the callbacks to expose
      Returns:
      one tool per callback
    • toSyncTools

      public static List<io.modelcontextprotocol.modern.server.feature.McpSyncTool> toSyncTools(ToolCallback... toolCallbacks)
    • toSyncTool

      public static io.modelcontextprotocol.modern.server.feature.McpSyncTool toSyncTool(ToolCallback toolCallback)
    • toSyncTool

      public static io.modelcontextprotocol.modern.server.feature.McpSyncTool toSyncTool(ToolCallback toolCallback, @Nullable org.springframework.util.MimeType mimeType)
      Converts a Spring AI ToolCallback to a modern tool. The tool definition is built from the callback's name, description and input schema. The callback's text result becomes the tool's text content, or an image content when an image MIME type is given.
      Parameters:
      toolCallback - the callback to expose
      mimeType - the MIME type of the callback's output, or null for text
      Returns:
      the tool
    • toAsyncTools

      public static List<io.modelcontextprotocol.modern.server.feature.McpAsyncTool> toAsyncTools(List<ToolCallback> toolCallbacks)
      Converts the given callbacks to asynchronous modern tools.
      Parameters:
      toolCallbacks - the callbacks to expose
      Returns:
      one tool per callback
      Since:
      2.1.0
    • toAsyncTools

      public static List<io.modelcontextprotocol.modern.server.feature.McpAsyncTool> toAsyncTools(ToolCallback... toolCallbacks)
      Since:
      2.1.0
    • toAsyncTool

      public static io.modelcontextprotocol.modern.server.feature.McpAsyncTool toAsyncTool(ToolCallback toolCallback)
      Since:
      2.1.0
    • toAsyncTool

      public static io.modelcontextprotocol.modern.server.feature.McpAsyncTool toAsyncTool(ToolCallback toolCallback, @Nullable org.springframework.util.MimeType mimeType)
      Parameters:
      toolCallback - the callback to expose
      mimeType - the MIME type of the callback's output, or null for text
      Returns:
      the tool
      Since:
      2.1.0
    • getMcpRequestContext

      public static @Nullable io.modelcontextprotocol.modern.server.McpRequestContext getMcpRequestContext(@Nullable ToolContext toolContext)
      Retrieves the McpRequestContext of the current tool call from the ToolContext, if the callback is being invoked by a modern MCP server.
      Parameters:
      toolContext - the tool context passed to the callback
      Returns:
      the request context, or null when the callback is not being invoked by a modern MCP server