Class AbstractMcpSamplingMethodCallback

java.lang.Object
org.springaicommunity.mcp.method.sampling.AbstractMcpSamplingMethodCallback
Direct Known Subclasses:
AsyncMcpSamplingMethodCallback, SyncMcpSamplingMethodCallback

public abstract class AbstractMcpSamplingMethodCallback extends Object
Abstract base class for creating callbacks around sampling methods. This class provides common functionality for both synchronous and asynchronous sampling method callbacks. It contains shared logic for method validation, argument building, and other common operations.
Author:
Christian Tzolov
  • Field Details

    • method

      protected final Method method
    • bean

      protected final Object bean
  • Constructor Details

    • AbstractMcpSamplingMethodCallback

      protected AbstractMcpSamplingMethodCallback(Method method, Object bean)
      Constructor for AbstractMcpSamplingMethodCallback.
      Parameters:
      method - The method to create a callback for
      bean - The bean instance that contains the method
  • Method Details

    • validateMethod

      protected void validateMethod(Method method)
      Validates that the method signature is compatible with the sampling callback.

      This method checks that the return type is valid and that the parameters match the expected pattern.

      Parameters:
      method - The method to validate
      Throws:
      IllegalArgumentException - if the method signature is not compatible
    • validateReturnType

      protected abstract void validateReturnType(Method method)
      Validates that the method return type is compatible with the sampling callback. This method should be implemented by subclasses to handle specific return type validation.
      Parameters:
      method - The method to validate
      Throws:
      IllegalArgumentException - if the return type is not compatible
    • validateParameters

      protected void validateParameters(Method method)
      Validates method parameters. This method provides common validation logic and delegates exchange type checking to subclasses.
      Parameters:
      method - The method to validate
      Throws:
      IllegalArgumentException - if the parameters are not compatible
    • buildArgs

      protected Object[] buildArgs(Method method, Object exchange, io.modelcontextprotocol.spec.McpSchema.CreateMessageRequest request)
      Builds the arguments array for invoking the method.

      This method constructs an array of arguments based on the method's parameter types and the available values (exchange, request).

      Parameters:
      method - The method to build arguments for
      exchange - The server exchange
      request - The sampling request
      Returns:
      An array of arguments for the method invocation
    • isExchangeType

      protected abstract boolean isExchangeType(Class<?> paramType)
      Checks if a parameter type is compatible with the exchange type. This method should be implemented by subclasses to handle specific exchange type checking.
      Parameters:
      paramType - The parameter type to check
      Returns:
      true if the parameter type is compatible with the exchange type, false otherwise