Class SyncMcpSamplingProvider

java.lang.Object
org.springaicommunity.mcp.provider.sampling.SyncMcpSamplingProvider

public class SyncMcpSamplingProvider extends Object
Provider for synchronous 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.

Example usage:


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

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

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

    • SyncMcpSamplingProvider

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

    • getSamplingSpecifications

      public List<SyncSamplingSpecification> getSamplingSpecifications()
      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