Class AsyncMcpSamplingProvider

java.lang.Object
org.springframework.ai.mcp.annotation.provider.sampling.AsyncMcpSamplingProvider

public class AsyncMcpSamplingProvider extends Object
Provider for asynchronous sampling callbacks.

This class scans a list of objects for methods annotated with McpSampling and creates Function callbacks for them. These callbacks can be used to handle sampling requests from MCP servers in a reactive way.

Example usage:

// Create a provider with a list of objects containing @McpSampling methods
AsyncMcpSamplingProvider provider = new AsyncMcpSamplingProvider(List.of(samplingHandler));

// Get the sampling handler
Function<CreateMessageRequest, Mono<CreateMessageResult>> samplingHandler = provider.getSamplingHandler();

// Add the handler to the client features
McpClientFeatures.Async clientFeatures = new McpClientFeatures.Async(
    clientInfo, clientCapabilities, roots,
    toolsChangeConsumers, resourcesChangeConsumers, promptsChangeConsumers,
    loggingConsumers, samplingHandler);
Author:
Christian Tzolov
See Also:
  • Constructor Details

    • AsyncMcpSamplingProvider

      public AsyncMcpSamplingProvider(List<Object> samplingObjects)
      Create a new AsyncMcpSamplingProvider.
      Parameters:
      samplingObjects - the objects containing methods annotated with McpSampling
  • Method Details

    • getSamplingSpecifictions

      public List<AsyncSamplingSpecification> getSamplingSpecifictions()
      Get the sampling handler.
      Returns:
      the sampling handler
      Throws:
      IllegalStateException - if no sampling methods are found or if multiple sampling methods are found
    • doGetClassMethods

      protected Method[] doGetClassMethods(Object bean)
      Returns the methods of the given bean class.
      Parameters:
      bean - the bean instance
      Returns:
      the methods of the bean class