Class McpToolUtils
The MCP tool system enables servers to expose executable functionality to language models, allowing them to interact with external systems, perform computations, and take actions in the real world. Each tool is uniquely identified by a name and includes metadata describing its schema.
This helper class provides methods to:
- Convert Spring AI's
ToolCallback
instances to MCP tool specification - Generate JSON schemas for tool input validation
- Author:
- Christian Tzolov
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The name of tool context key used to store the MCP exchange object. -
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<io.modelcontextprotocol.server.McpSyncServerExchange>
getMcpExchange
(ToolContext toolContext) Retrieves the MCP exchange object from the provided tool context if it exists.static List<ToolCallback>
getToolCallbacksFromAsyncClients
(io.modelcontextprotocol.client.McpAsyncClient... asyncMcpClients) Convenience method to get tool callbacks from multiple asynchronous MCP clients.static List<ToolCallback>
getToolCallbacksFromAsyncClients
(List<io.modelcontextprotocol.client.McpAsyncClient> asyncMcpClients) Gets tool callbacks from a list of asynchronous MCP clients.static List<ToolCallback>
getToolCallbacksFromSyncClients
(io.modelcontextprotocol.client.McpSyncClient... mcpClients) Convenience method to get tool callbacks from multiple synchronous MCP clients.static List<ToolCallback>
getToolCallbacksFromSyncClients
(List<io.modelcontextprotocol.client.McpSyncClient> mcpClients) Gets tool callbacks from a list of synchronous MCP clients.static String
prefixedToolName
(String prefix, String toolName) static io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification
toAsyncToolSpecification
(ToolCallback toolCallback) Converts a Spring AI tool callback to an MCP asynchronous tool specification.static io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification
toAsyncToolSpecification
(ToolCallback toolCallback, org.springframework.util.MimeType mimeType) Converts a Spring AI tool callback to an MCP asynchronous tool specification.static List<io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification>
toAsyncToolSpecifications
(List<ToolCallback> toolCallbacks) Converts a list of Spring AI tool callbacks to MCP asynchronous tool specification.static List<io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification>
toAsyncToolSpecifications
(ToolCallback... toolCallbacks) Convenience method to convert a variable number of tool callbacks to MCP asynchronous tool specification.static List<io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification>
toSyncToolSpecification
(List<ToolCallback> toolCallbacks) Converts a list of Spring AI tool callbacks to MCP synchronous tool specification.static io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification
toSyncToolSpecification
(ToolCallback toolCallback) Converts a Spring AI ToolCallback to an MCP SyncToolSpecification.static io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification
toSyncToolSpecification
(ToolCallback toolCallback, org.springframework.util.MimeType mimeType) Converts a Spring AI ToolCallback to an MCP SyncToolSpecification.static List<io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification>
toSyncToolSpecifications
(ToolCallback... toolCallbacks) Convenience method to convert a variable number of tool callbacks to MCP synchronous tool specification.
-
Field Details
-
TOOL_CONTEXT_MCP_EXCHANGE_KEY
The name of tool context key used to store the MCP exchange object.- See Also:
-
-
Method Details
-
prefixedToolName
-
toSyncToolSpecification
public static List<io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification> toSyncToolSpecification(List<ToolCallback> toolCallbacks) Converts a list of Spring AI tool callbacks to MCP synchronous tool specification.This method processes multiple tool callbacks in bulk, converting each one to its corresponding MCP tool specification while maintaining synchronous execution semantics.
- Parameters:
toolCallbacks
- the list of tool callbacks to convert- Returns:
- a list of MCP synchronous tool specification
-
toSyncToolSpecifications
public static List<io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification> toSyncToolSpecifications(ToolCallback... toolCallbacks) Convenience method to convert a variable number of tool callbacks to MCP synchronous tool specification.This is a varargs wrapper around
toSyncToolSpecification(List)
for easier usage when working with individual callbacks.- Parameters:
toolCallbacks
- the tool callbacks to convert- Returns:
- a list of MCP synchronous tool specification
-
toSyncToolSpecification
public static io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification toSyncToolSpecification(ToolCallback toolCallback) Converts a Spring AI ToolCallback to an MCP SyncToolSpecification. This enables Spring AI functions to be exposed as MCP tools that can be discovered and invoked by language models.The conversion process:
- Creates an MCP Tool with the function's name and input schema
- Wraps the function's execution in a SyncToolSpecification that handles the MCP protocol
- Provides error handling and result formatting according to MCP specifications
- Parameters:
toolCallback
- the Spring AI function callback to convert- Returns:
- an MCP SyncToolSpecification that wraps the function callback
- Throws:
RuntimeException
- if there's an error during the function execution
-
toSyncToolSpecification
public static io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification toSyncToolSpecification(ToolCallback toolCallback, org.springframework.util.MimeType mimeType) Converts a Spring AI ToolCallback to an MCP SyncToolSpecification. This enables Spring AI functions to be exposed as MCP tools that can be discovered and invoked by language models.The conversion process:
- Creates an MCP Tool with the function's name and input schema
- Wraps the function's execution in a SyncToolSpecification that handles the MCP protocol
- Provides error handling and result formatting according to MCP specifications
- Parameters:
toolCallback
- the Spring AI function callback to convertmimeType
- the MIME type of the output content- Returns:
- an MCP SyncToolSpecification that wraps the function callback
- Throws:
RuntimeException
- if there's an error during the function execution
-
getMcpExchange
public static Optional<io.modelcontextprotocol.server.McpSyncServerExchange> getMcpExchange(ToolContext toolContext) Retrieves the MCP exchange object from the provided tool context if it exists.- Parameters:
toolContext
- the tool context from which to retrieve the MCP exchange- Returns:
- the MCP exchange object, or null if not present in the context
-
toAsyncToolSpecifications
public static List<io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification> toAsyncToolSpecifications(List<ToolCallback> toolCallbacks) Converts a list of Spring AI tool callbacks to MCP asynchronous tool specification.This method processes multiple tool callbacks in bulk, converting each one to its corresponding MCP tool specification while adding asynchronous execution capabilities. The resulting specifications will execute their tools on a bounded elastic scheduler.
- Parameters:
toolCallbacks
- the list of tool callbacks to convert- Returns:
- a list of MCP asynchronous tool specifications
-
toAsyncToolSpecifications
public static List<io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification> toAsyncToolSpecifications(ToolCallback... toolCallbacks) Convenience method to convert a variable number of tool callbacks to MCP asynchronous tool specification.This is a varargs wrapper around
toAsyncToolSpecifications(List)
for easier usage when working with individual callbacks.- Parameters:
toolCallbacks
- the tool callbacks to convert- Returns:
- a list of MCP asynchronous tool specifications
- See Also:
-
toAsyncToolSpecification
public static io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification toAsyncToolSpecification(ToolCallback toolCallback) Converts a Spring AI tool callback to an MCP asynchronous tool specification.This method enables Spring AI tools to be exposed as asynchronous MCP tools that can be discovered and invoked by language models. The conversion process:
- First converts the callback to a synchronous specification
- Wraps the synchronous execution in a reactive Mono
- Configures execution on a bounded elastic scheduler for non-blocking operation
The resulting async specification will:
- Execute the tool without blocking the calling thread
- Handle errors and results asynchronously
- Provide backpressure through Project Reactor
- Parameters:
toolCallback
- the Spring AI tool callback to convert- Returns:
- an MCP asynchronous tool specification that wraps the tool callback
- See Also:
-
McpServerFeatures.AsyncToolSpecification
Mono
Schedulers.boundedElastic()
-
toAsyncToolSpecification
public static io.modelcontextprotocol.server.McpServerFeatures.AsyncToolSpecification toAsyncToolSpecification(ToolCallback toolCallback, org.springframework.util.MimeType mimeType) Converts a Spring AI tool callback to an MCP asynchronous tool specification.This method enables Spring AI tools to be exposed as asynchronous MCP tools that can be discovered and invoked by language models. The conversion process:
- First converts the callback to a synchronous specification
- Wraps the synchronous execution in a reactive Mono
- Configures execution on a bounded elastic scheduler for non-blocking operation
The resulting async specification will:
- Execute the tool without blocking the calling thread
- Handle errors and results asynchronously
- Provide backpressure through Project Reactor
- Parameters:
toolCallback
- the Spring AI tool callback to convertmimeType
- the MIME type of the output content- Returns:
- an MCP asynchronous tool specificaiotn that wraps the tool callback
- See Also:
-
McpServerFeatures.AsyncToolSpecification
Schedulers.boundedElastic()
-
getToolCallbacksFromSyncClients
public static List<ToolCallback> getToolCallbacksFromSyncClients(io.modelcontextprotocol.client.McpSyncClient... mcpClients) Convenience method to get tool callbacks from multiple synchronous MCP clients.This is a varargs wrapper around
getToolCallbacksFromSyncClients(List)
for easier usage when working with individual clients.- Parameters:
mcpClients
- the synchronous MCP clients to get callbacks from- Returns:
- a list of tool callbacks from all provided clients
- See Also:
-
getToolCallbacksFromSyncClients
public static List<ToolCallback> getToolCallbacksFromSyncClients(List<io.modelcontextprotocol.client.McpSyncClient> mcpClients) Gets tool callbacks from a list of synchronous MCP clients.This method:
- Takes a list of synchronous MCP clients
- Creates a provider for each client
- Retrieves and combines all tool callbacks into a single list
- Parameters:
mcpClients
- the list of synchronous MCP clients to get callbacks from- Returns:
- a list of tool callbacks from all provided clients
-
getToolCallbacksFromAsyncClients
public static List<ToolCallback> getToolCallbacksFromAsyncClients(io.modelcontextprotocol.client.McpAsyncClient... asyncMcpClients) Convenience method to get tool callbacks from multiple asynchronous MCP clients.This is a varargs wrapper around
getToolCallbacksFromAsyncClients(List)
for easier usage when working with individual clients.- Parameters:
asyncMcpClients
- the asynchronous MCP clients to get callbacks from- Returns:
- a list of tool callbacks from all provided clients
- See Also:
-
getToolCallbacksFromAsyncClients
public static List<ToolCallback> getToolCallbacksFromAsyncClients(List<io.modelcontextprotocol.client.McpAsyncClient> asyncMcpClients) Gets tool callbacks from a list of asynchronous MCP clients.This method:
- Takes a list of asynchronous MCP clients
- Creates a provider for each client
- Retrieves and combines all tool callbacks into a single list
- Parameters:
asyncMcpClients
- the list of asynchronous MCP clients to get callbacks from- Returns:
- a list of tool callbacks from all provided clients
-