Class SyncMcpResourceMethodCallback

java.lang.Object
org.springaicommunity.mcp.method.resource.AbstractMcpResourceMethodCallback
org.springaicommunity.mcp.method.resource.SyncMcpResourceMethodCallback
All Implemented Interfaces:
BiFunction<io.modelcontextprotocol.server.McpSyncServerExchange,io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest,io.modelcontextprotocol.spec.McpSchema.ReadResourceResult>

public final class SyncMcpResourceMethodCallback extends AbstractMcpResourceMethodCallback implements BiFunction<io.modelcontextprotocol.server.McpSyncServerExchange,io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest,io.modelcontextprotocol.spec.McpSchema.ReadResourceResult>
Class for creating BiFunction callbacks around resource methods. This class provides a way to convert methods annotated with McpResource into callback functions that can be used to handle resource requests. It supports various method signatures and return types, and handles URI template variables.
Author:
Christian Tzolov
  • Method Details

    • validateParamType

      protected void validateParamType(Class<?> paramType)
      Overrides:
      validateParamType in class AbstractMcpResourceMethodCallback
    • assignExchangeType

      protected Object assignExchangeType(Class<?> paramType, Object exchange)
      Specified by:
      assignExchangeType in class AbstractMcpResourceMethodCallback
    • apply

      public io.modelcontextprotocol.spec.McpSchema.ReadResourceResult apply(io.modelcontextprotocol.server.McpSyncServerExchange exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request)
      Apply the callback to the given exchange and request.

      This method extracts URI variable values from the request URI, builds the arguments for the method call, invokes the method, and converts the result to a ReadResourceResult.

      Specified by:
      apply in interface BiFunction<io.modelcontextprotocol.server.McpSyncServerExchange,io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest,io.modelcontextprotocol.spec.McpSchema.ReadResourceResult>
      Parameters:
      exchange - The server exchange, may be null if the method doesn't require it
      request - The resource request, must not be null
      Returns:
      The resource result
      Throws:
      McpResourceMethodException - if there is an error invoking the resource method
      IllegalArgumentException - if the request is null or if URI variable extraction fails
    • builder

      public static SyncMcpResourceMethodCallback.Builder builder()
      Create a new builder.
      Returns:
      A new builder instance
    • validateReturnType

      protected void validateReturnType(Method method)
      Description copied from class: AbstractMcpResourceMethodCallback
      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.
      Specified by:
      validateReturnType in class AbstractMcpResourceMethodCallback
      Parameters:
      method - The method to validate
    • isExchangeOrContextType

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