Interface SyncModernMcpConnection


public interface SyncModernMcpConnection
A connection to one modern MCP server, and the rule that decides which McpSyncClient 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 SyncModernMcpToolCallbackProvider keeps a listing the server marks as public for as long as the server allows.

SyncModernMcpToolCallbackProvider 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.

Since:
2.1.0
Author:
Christian Tzolov
  • Method Summary

    Modifier and Type
    Method
    Description
    default io.modelcontextprotocol.modern.client.McpSyncClient
    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.
    io.modelcontextprotocol.modern.client.McpSyncClient
    Returns the client that serves the current request.
    default boolean
    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.
    Describes the connection.
    perRequest(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> clientFactory)
    A connection that asks the given factory for a client each time client() is called, to give each request its own client.
    scoped(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> clientFactory, SyncModernMcpClientScope clientScope)
    A connection that asks the given scope for the client of each request.
    shared(McpModernConnectionInfo info, io.modelcontextprotocol.modern.client.McpSyncClient client)
    A connection that serves every request with the given client.
  • Method Details

    • info

      Describes the connection.
      Returns:
      the connection info
    • client

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

      default io.modelcontextprotocol.modern.client.McpSyncClient 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. SyncModernMcpToolCallbackProvider 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
    • shared

      static SyncModernMcpConnection shared(McpModernConnectionInfo info, io.modelcontextprotocol.modern.client.McpSyncClient 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 SyncModernMcpConnection perRequest(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> 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.McpSyncClient>, org.springframework.ai.mcp.modern.client.SyncModernMcpClientScope).
      Parameters:
      info - the connection info
      clientFactory - creates the client for a request
      Returns:
      the connection
    • scoped

      static SyncModernMcpConnection scoped(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> clientFactory, SyncModernMcpClientScope 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