Interface AsyncModernMcpConnection


public interface AsyncModernMcpConnection
A connection to one modern MCP server, and the rule that decides which McpAsyncClient serves a request.

A modern client is cheap to create and does no I/O when built, so an application may hold one client for all requests or create a client for each request:

In all cases AsyncModernMcpToolCallbackProvider keeps a listing the server marks as public for as long as the server allows.

AsyncModernMcpToolCallbackProvider asks for the client once for each call to getToolCallbacks() and binds the returned tool callbacks to it. Listing and calling a tool therefore use the same client.

An asynchronous client holds no caller. The caller of a request is the transport context of the connection, which the provider also asks for once for each call to getToolCallbacks(), and which goes with the listing and with the tool calls of that request.

Since:
2.1.0
Author:
Christian Tzolov
  • Method Details

    • info

      Describes the connection.
      Returns:
      the connection info
    • client

      io.modelcontextprotocol.modern.client.McpAsyncClient client()
      Returns the client that serves the current request.
      Returns:
      the client
    • applicationClient

      default io.modelcontextprotocol.modern.client.McpAsyncClient applicationClient()
      Returns a client for what the application does for itself, outside of any request: a listen stream, or listing the tools, prompts or resources after the server reported a change. The caller keeps the client for as long as it needs it. By default this is client().
      Returns:
      the client
    • clientPerRequest

      default boolean clientPerRequest()
      Whether a client of this connection serves one request only, so that nothing it keeps, such as a tool listing that the server marks as private, lasts beyond that request. AsyncModernMcpToolCallbackProvider uses the answer to tell when such a listing could be kept with another kind of connection.
      Returns:
      true if client() returns a new client each time, false by default
    • transportContext

      default io.modelcontextprotocol.common.McpTransportContext transportContext()
      Returns the transport context of the caller that the current thread works for. It is read on the thread that resolves the tools, as a blocking call has no Reactor context to read the caller from.
      Returns:
      the transport context, McpTransportContext.EMPTY by default
    • withTransportContext

      default AsyncModernMcpConnection withTransportContext(AsyncModernMcpTransportContextProvider transportContextProvider)
      Returns a connection that is this one, with the caller of each request supplied by the given provider.
      Parameters:
      transportContextProvider - supplies the transport context of the caller
      Returns:
      the connection
    • shared

      static AsyncModernMcpConnection shared(McpModernConnectionInfo info, io.modelcontextprotocol.modern.client.McpAsyncClient client)
      A connection that serves every request with the given client.
      Parameters:
      info - the connection info
      client - the shared client
      Returns:
      the connection
    • perRequest

      static AsyncModernMcpConnection perRequest(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient> clientFactory)
      A connection that asks the given factory for a client each time client() is called, to give each request its own client. A factory is expected to build a new client on each call, and the connection says so. To keep clients, for example one for each user, use scoped(org.springframework.ai.mcp.modern.client.McpModernConnectionInfo, java.util.function.Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient>, org.springframework.ai.mcp.modern.client.AsyncModernMcpClientScope).
      Parameters:
      info - the connection info
      clientFactory - creates the client for a request
      Returns:
      the connection
    • scoped

      static AsyncModernMcpConnection scoped(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient> clientFactory, AsyncModernMcpClientScope clientScope)
      A connection that asks the given scope for the client of each request. The scope gets the factory to build a client with, and it may keep the clients it builds, for example one for each user. A scope may serve several connections, and it must return a client that the factory of this connection built: a client of another connection, which would send the requests to the server of that connection, is rejected. applicationClient() does not ask the scope: it returns a new client from the factory, so that the scope is never asked for a client when there is no request.
      Parameters:
      info - the connection info
      clientFactory - builds a new client for the connection on each call
      clientScope - selects the client for a request
      Returns:
      the connection