Class AsyncMcpResourceMethodCallback

java.lang.Object
org.springaicommunity.mcp.method.resource.AbstractMcpResourceMethodCallback
org.springaicommunity.mcp.method.resource.AsyncMcpResourceMethodCallback
All Implemented Interfaces:
BiFunction<io.modelcontextprotocol.server.McpAsyncServerExchange,io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest,reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.ReadResourceResult>>

public final class AsyncMcpResourceMethodCallback extends AbstractMcpResourceMethodCallback implements BiFunction<io.modelcontextprotocol.server.McpAsyncServerExchange,io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest,reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.ReadResourceResult>>
Class for creating BiFunction callbacks around resource methods with asynchronous processing. This class provides a way to convert methods annotated with McpResource into callback functions that can be used to handle resource requests asynchronously. 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 reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.ReadResourceResult> apply(io.modelcontextprotocol.server.McpAsyncServerExchange 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.McpAsyncServerExchange,io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest,reactor.core.publisher.Mono<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:
      A Mono that emits 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 AsyncMcpResourceMethodCallback.Builder builder()
      Create a new builder.
      Returns:
      A new builder instance
    • validateReturnType

      protected void validateReturnType(Method method)
      Validates that the method return type is compatible with the resource callback.
      Specified by:
      validateReturnType in class AbstractMcpResourceMethodCallback
      Parameters:
      method - The method to validate
      Throws:
      IllegalArgumentException - if the return type is not compatible
    • isExchangeOrContextType

      protected boolean isExchangeOrContextType(Class<?> paramType)
      Checks if a parameter type is compatible with the exchange type.
      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