Class ClientMcpAsyncHandlersRegistry

java.lang.Object
org.springframework.ai.mcp.annotation.spring.ClientMcpAsyncHandlersRegistry
All Implemented Interfaces:
org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.beans.factory.SmartInitializingSingleton

public class ClientMcpAsyncHandlersRegistry extends Object implements org.springframework.beans.factory.SmartInitializingSingleton
Registry of methods annotated with MCP Client annotations (sampling, logging, etc.). All beans in the application context are scanned to find these methods automatically. They are then exposed by the registry by client name.

The scanning happens in two phases:

First, once bean definitions are available, all bean types are scanned for the presence of MCP annotations. In particular, this is used to prepare the result getCapabilities(String), which is then used by MCP client auto-configurations to configure the client capabilities without needing to instantiate the beans.

Second, after all singleton beans have been instantiated, all annotated beans are scanned again, MCP handlers are created to match the annotations, and stored by client.

Since:
1.1.0
Author:
Daniel Garnier-Moiroux
See Also:
  • McpSampling
  • McpElicitation
  • McpLogging
  • McpProgress
  • McpToolListChanged
  • McpPromptListChanged
  • McpResourceListChanged
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Set<String>
     
    protected org.springframework.beans.factory.config.ConfigurableListableBeanFactory
     
    protected Map<String,io.modelcontextprotocol.spec.McpSchema.ClientCapabilities>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected Map<Class<? extends Annotation>,Set<Object>>
     
    io.modelcontextprotocol.spec.McpSchema.ClientCapabilities
    getCapabilities(String clientName)
    Obtain the MCP capabilities declared for a given MCP client.
    reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.ElicitResult>
    handleElicitation(String name, io.modelcontextprotocol.spec.McpSchema.ElicitRequest elicitationRequest)
    Invoke the elicitation handler for a given MCP client.
    reactor.core.publisher.Mono<Void>
    handleLogging(String name, io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification loggingMessageNotification)
    Invoke all elicitation handlers for a given MCP client, sequentially.
    reactor.core.publisher.Mono<Void>
    handleProgress(String name, io.modelcontextprotocol.spec.McpSchema.ProgressNotification progressNotification)
    Invoke all progress handlers for a given MCP client, sequentially.
    reactor.core.publisher.Mono<Void>
    handlePromptListChanged(String name, List<io.modelcontextprotocol.spec.McpSchema.Prompt> updatedPrompts)
    Invoke all prompt list changed handlers for a given MCP client, sequentially.
    reactor.core.publisher.Mono<Void>
    handleResourceListChanged(String name, List<io.modelcontextprotocol.spec.McpSchema.Resource> updatedResources)
    Invoke all resource list changed handlers for a given MCP client, sequentially.
    reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.CreateMessageResult>
    handleSampling(String name, io.modelcontextprotocol.spec.McpSchema.CreateMessageRequest samplingRequest)
    Invoke the sampling handler for a given MCP client.
    reactor.core.publisher.Mono<Void>
    handleToolListChanged(String name, List<io.modelcontextprotocol.spec.McpSchema.Tool> updatedTools)
    Invoke all tool list changed handlers for a given MCP client, sequentially.
    void
    postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
     
    protected List<Annotation>
    scan(Class<?> beanClass)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • capabilitiesPerClient

      protected Map<String,io.modelcontextprotocol.spec.McpSchema.ClientCapabilities> capabilitiesPerClient
    • beanFactory

      protected org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory
    • allAnnotatedBeans

      protected final Set<String> allAnnotatedBeans
  • Constructor Details

    • ClientMcpAsyncHandlersRegistry

      public ClientMcpAsyncHandlersRegistry()
  • Method Details

    • getCapabilities

      public io.modelcontextprotocol.spec.McpSchema.ClientCapabilities getCapabilities(String clientName)
      Obtain the MCP capabilities declared for a given MCP client. Capabilities are registered with the McpSampling and McpElicitation annotations.
    • handleSampling

      public reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.CreateMessageResult> handleSampling(String name, io.modelcontextprotocol.spec.McpSchema.CreateMessageRequest samplingRequest)
      Invoke the sampling handler for a given MCP client.
      See Also:
      • McpSampling
    • handleElicitation

      public reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.ElicitResult> handleElicitation(String name, io.modelcontextprotocol.spec.McpSchema.ElicitRequest elicitationRequest)
      Invoke the elicitation handler for a given MCP client.
      See Also:
      • McpElicitation
    • handleLogging

      public reactor.core.publisher.Mono<Void> handleLogging(String name, io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification loggingMessageNotification)
      Invoke all elicitation handlers for a given MCP client, sequentially.
      See Also:
      • McpLogging
    • handleProgress

      public reactor.core.publisher.Mono<Void> handleProgress(String name, io.modelcontextprotocol.spec.McpSchema.ProgressNotification progressNotification)
      Invoke all progress handlers for a given MCP client, sequentially.
      See Also:
      • McpProgress
    • handleToolListChanged

      public reactor.core.publisher.Mono<Void> handleToolListChanged(String name, List<io.modelcontextprotocol.spec.McpSchema.Tool> updatedTools)
      Invoke all tool list changed handlers for a given MCP client, sequentially.
      See Also:
      • McpToolListChanged
    • handlePromptListChanged

      public reactor.core.publisher.Mono<Void> handlePromptListChanged(String name, List<io.modelcontextprotocol.spec.McpSchema.Prompt> updatedPrompts)
      Invoke all prompt list changed handlers for a given MCP client, sequentially.
      See Also:
      • McpPromptListChanged
    • handleResourceListChanged

      public reactor.core.publisher.Mono<Void> handleResourceListChanged(String name, List<io.modelcontextprotocol.spec.McpSchema.Resource> updatedResources)
      Invoke all resource list changed handlers for a given MCP client, sequentially.
      See Also:
      • McpResourceListChanged
    • afterSingletonsInstantiated

      public void afterSingletonsInstantiated()
      Specified by:
      afterSingletonsInstantiated in interface org.springframework.beans.factory.SmartInitializingSingleton
    • postProcessBeanFactory

      public void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) throws org.springframework.beans.BeansException
      Specified by:
      postProcessBeanFactory in interface org.springframework.beans.factory.config.BeanFactoryPostProcessor
      Throws:
      org.springframework.beans.BeansException
    • scan

      protected List<Annotation> scan(Class<?> beanClass)
    • getBeansByAnnotationType

      protected Map<Class<? extends Annotation>,Set<Object>> getBeansByAnnotationType()