Class AsyncModernMcpChangeSubscriber

java.lang.Object
org.springframework.ai.mcp.modern.client.AsyncModernMcpChangeSubscriber
All Implemented Interfaces:
org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle

public final class AsyncModernMcpChangeSubscriber extends Object implements org.springframework.context.SmartLifecycle
Keeps a listen stream open to each of its modern MCP connections and tells its listeners what the servers report.

A listen stream is a long-lived request, so it is not opened by the client of a chat request. Each connection gets one client for its stream, taken from AsyncModernMcpConnection.applicationClient() when the subscriber starts. That happens outside of any request, so the stream is opened for the application and not for a caller: the server must accept it with the credentials of the application, or with none.

The streams are opened when the subscriber is started, without waiting for the servers, and a server that cannot be reached does not fail the start. A subscriber without listeners opens no stream, so add the listeners before it is started.

A stream that ends, or that cannot be opened, is opened again for as long as the subscriber runs: after one second at first, and after twice the previous pause each time, up to the maximum reopen delay. The pauses start over with one second once a stream has stayed up for the maximum reopen delay. So a short interruption is over quickly, and neither a server that stays away nor one that accepts the stream only to end it is asked more often than the maximum delay allows.

The listeners are told that changes may have been missed when a stream is established and when an established stream ends, but not for a stream that could not be opened.

Register a AsyncModernMcpToolCallbackProvider as a listener to have it drop a kept tool listing as soon as the server reports that its tools changed. A stream always listens for the changes of the tools, and for those of the prompts and of the list of resources if the connection was added with them.

Since:
2.1.0
Author:
Christian Tzolov
See Also:
  • Method Details

    • builder

      public static AsyncModernMcpChangeSubscriber.Builder builder()
      Creates a builder for a AsyncModernMcpChangeSubscriber.
      Returns:
      the builder
    • addListener

      public void addListener(AsyncModernMcpChangeListener listener)
      Adds a listener. The listeners are told in their order: one that is Ordered or annotated with Order by that order, and the others last, in the order they were added. A subscriber that was started without listeners has opened no stream, and it opens the streams when the first listener is added.
      Parameters:
      listener - the listener
    • connectionNames

      public List<String> connectionNames()
      The names of the connections a listen stream is kept open to.
      Returns:
      the connection names
    • start

      public void start()
      Opens the listen streams, without waiting for the servers.
      Specified by:
      start in interface org.springframework.context.Lifecycle
    • stop

      public void stop()
      Closes the listen streams. They are not opened again until the next start.
      Specified by:
      stop in interface org.springframework.context.Lifecycle
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface org.springframework.context.Lifecycle