Class AbstractMcpToolMethodCallback<T,RC extends McpRequestContextTypes<?>>

java.lang.Object
org.springaicommunity.mcp.method.tool.AbstractMcpToolMethodCallback<T,RC>
Type Parameters:
T - The type of the context parameter (e.g., McpTransportContext, McpSyncServerExchange, or McpAsyncServerExchange)
Direct Known Subclasses:
AbstractAsyncMcpToolMethodCallback

public abstract class AbstractMcpToolMethodCallback<T,RC extends McpRequestContextTypes<?>> extends Object
Abstract base class for creating Function callbacks around tool methods. This class provides common functionality for converting methods annotated with McpTool into callback functions that can be used to handle tool requests. It contains all the shared logic between synchronous and asynchronous implementations.
Author:
Christian Tzolov
  • Field Details

    • toolMethod

      protected final Method toolMethod
    • toolObject

      protected final Object toolObject
    • returnMode

      protected final ReturnMode returnMode
  • Constructor Details

    • AbstractMcpToolMethodCallback

      protected AbstractMcpToolMethodCallback(ReturnMode returnMode, Method toolMethod, Object toolObject)
  • Method Details

    • callMethod

      protected Object callMethod(Object[] methodArguments)
      Invokes the tool method with the provided arguments.
      Parameters:
      methodArguments - The arguments to pass to the method
      Returns:
      The result of the method invocation
      Throws:
      IllegalStateException - if the method cannot be accessed
      RuntimeException - if there's an error invoking the method
    • buildMethodArguments

      protected Object[] buildMethodArguments(T exchangeOrContext, Map<String,Object> toolInputArguments, io.modelcontextprotocol.spec.McpSchema.CallToolRequest request)
      Builds the method arguments from the context, tool input arguments, and optionally the full request.
      Parameters:
      exchangeOrContext - The exchange or context object (e.g., McpSyncServerExchange, McpAsyncServerExchange, or McpTransportContext)
      toolInputArguments - The input arguments from the tool request
      request - The full CallToolRequest (optional, can be null)
      Returns:
      An array of method arguments
    • buildTypedArgument

      protected Object buildTypedArgument(Object value, Type type)
      Builds a typed argument from a raw value and type information.
      Parameters:
      value - The raw value
      type - The target type
      Returns:
      The typed argument
    • convertValueToCallToolResult

      protected io.modelcontextprotocol.spec.McpSchema.CallToolResult convertValueToCallToolResult(Object result)
      Converts a method result value to a CallToolResult based on the return mode and type. This method contains the common logic for processing results that is shared between synchronous and asynchronous implementations.
      Parameters:
      result - The result value to convert
      Returns:
      A CallToolResult representing the processed result
    • createErrorMessage

      protected String createErrorMessage(Throwable e)
      Creates the base error message for exceptions that occur during method invocation.
      Parameters:
      e - The exception that occurred
      Returns:
      The error message string
    • isExchangeOrContextType

      protected abstract boolean isExchangeOrContextType(Class<?> paramType)
      Determines if the given parameter type is an exchange or context type that should be injected. Subclasses must implement this method to specify which types are considered exchange or context types.
      Parameters:
      paramType - The parameter type to check
      Returns:
      true if the parameter type is an exchange or context type, false otherwise
    • findCauseUsingPlainJava

      protected Throwable findCauseUsingPlainJava(Throwable throwable)
    • createRequestContext

      protected abstract RC createRequestContext(T exchange, io.modelcontextprotocol.spec.McpSchema.CallToolRequest request)