Interface AsyncModernMcpClientScope

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface AsyncModernMcpClientScope
Decides which McpAsyncClient of a connection serves a request, for an application that wants neither a new client for each request nor one client for all of them. A typical scope keeps one client for each user, so that the tool listing a server marks as private is kept for that user for as long as the server allows:
 Map<String, McpAsyncClient> clients = new ConcurrentHashMap<>();
 AsyncModernMcpClientScope perUser = (connection, clientFactory) -> clients
     .computeIfAbsent(connection.name() + "/" + currentUserName(), key -> clientFactory.get());
 
The scope is called once for each call to AsyncModernMcpToolCallbackProvider.getToolCallbacks(), on the thread that resolves the tools, and the returned tool callbacks are bound to the client it returns. It is not called for what the application does for itself, such as a listen stream: see AsyncModernMcpConnection.applicationClient().

A scope that keeps clients decides how many it keeps and for how long. A client holds no resource that must be released, so a client that is no longer needed is simply dropped. An asynchronous client holds no caller: the caller of a request comes from an AsyncModernMcpTransportContextProvider.

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

    Modifier and Type
    Method
    Description
    io.modelcontextprotocol.modern.client.McpAsyncClient
    client(McpModernConnectionInfo connectionInfo, Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient> clientFactory)
    Returns the client that serves the current request of the given connection.
  • Method Details

    • client

      io.modelcontextprotocol.modern.client.McpAsyncClient client(McpModernConnectionInfo connectionInfo, Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient> clientFactory)
      Returns the client that serves the current request of the given connection.
      Parameters:
      connectionInfo - the connection the request goes to
      clientFactory - builds a new client for the connection on each call, configured as every client of that connection is
      Returns:
      the client, either one that the scope kept or a new one from the factory