Class AbstractMcpResourceMethodCallback

java.lang.Object
org.springaicommunity.mcp.method.resource.AbstractMcpResourceMethodCallback
Direct Known Subclasses:
AsyncMcpResourceMethodCallback, AsyncStatelessMcpResourceMethodCallback, SyncMcpResourceMethodCallback, SyncStatelessMcpResourceMethodCallback

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

  • Constructor Details

    • AbstractMcpResourceMethodCallback

      protected AbstractMcpResourceMethodCallback(Method method, Object bean, String uri, String name, String description, String mimeType, McpReadResourceResultConverter resultConverter, io.modelcontextprotocol.util.McpUriTemplateManagerFactory uriTemplateMangerFactory, AbstractMcpResourceMethodCallback.ContentType contentType)
      Constructor for AbstractMcpResourceMethodCallback.
      Parameters:
      method - The method to create a callback for
      bean - The bean instance that contains the method
      uri - The URI for the resource
      name - The name of the resource (optional)
      description - The description of the resource (optional)
      mimeType - The MIME type of the resource (optional)
      resultConverter - The result converter
      uriTemplateMangerFactory - The URI template manager factory
      contentType - The content type
  • Method Details

    • validateMethod

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

      This method checks that the return type is valid and that the parameters match the expected pattern based on whether URI variables are present.

      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 resource 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
    • validateParametersWithoutUriVariables

      protected void validateParametersWithoutUriVariables(Method method)
      Validates method parameters when no URI variables are present. 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
    • validateParamType

      protected void validateParamType(Class<?> paramType)
    • validateParametersWithUriVariables

      protected void validateParametersWithUriVariables(Method method)
      Validates method parameters when URI variables are present. 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
    • assignExchangeType

      protected abstract Object assignExchangeType(Class<?> paramType, Object exchange)
    • buildArgs

      protected Object[] buildArgs(Method method, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request, Map<String,String> uriVariableValues)
      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, URI variables, progress token).

      Parameters:
      method - The method to build arguments for
      exchange - The server exchange
      request - The resource request
      uriVariableValues - Map of URI variable names to their values
      Returns:
      An array of arguments for the method invocation
    • buildArgsWithUriVariables

      protected void buildArgsWithUriVariables(Parameter[] parameters, Object[] args, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request, Map<String,String> uriVariableValues)
      Builds arguments for methods with URI variables. This method provides common argument building logic for methods with URI variables.
      Parameters:
      parameters - The method parameters
      args - The arguments array to populate
      exchange - The server exchange
      request - The resource request
      uriVariableValues - Map of URI variable names to their values
    • buildArgsWithoutUriVariables

      protected void buildArgsWithoutUriVariables(Parameter[] parameters, Object[] args, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request)
      Builds arguments for methods without URI variables. This method provides common argument building logic for methods without URI variables.
      Parameters:
      parameters - The method parameters
      args - The arguments array to populate
      exchange - The server exchange
      request - The resource request
    • isExchangeOrContextType

      protected abstract boolean isExchangeOrContextType(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
    • contentType

      Returns the content type of the resource.
      Returns:
      the content type