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:
perRequest(org.springframework.ai.mcp.modern.client.McpModernConnectionInfo, java.util.function.Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient>)returns a new client each time. A client keeps nothing from one request to the next, so a tool listing the server marks asprivateis never handed to another caller.shared(org.springframework.ai.mcp.modern.client.McpModernConnectionInfo, io.modelcontextprotocol.modern.client.McpAsyncClient)always returns the same client. That client keeps the server's tool listing for as long as the server allows, even aprivateone. It fits an application that calls the server under a single identity.scoped(org.springframework.ai.mcp.modern.client.McpModernConnectionInfo, java.util.function.Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient>, org.springframework.ai.mcp.modern.client.AsyncModernMcpClientScope)leaves the choice to aAsyncModernMcpClientScope, for example one that keeps a client for each user.
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 Summary
Modifier and TypeMethodDescriptiondefault io.modelcontextprotocol.modern.client.McpAsyncClientReturns 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.McpAsyncClientclient()Returns the client that serves the current request.default booleanWhether a client of this connection serves one request only, so that nothing it keeps, such as a tool listing that the server marks asprivate, lasts beyond that request.info()Describes the connection.static AsyncModernMcpConnectionperRequest(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient> clientFactory) A connection that asks the given factory for a client each timeclient()is called, to give each request its own client.static AsyncModernMcpConnectionscoped(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient> clientFactory, AsyncModernMcpClientScope clientScope) A connection that asks the given scope for the client of each request.static AsyncModernMcpConnectionshared(McpModernConnectionInfo info, io.modelcontextprotocol.modern.client.McpAsyncClient client) A connection that serves every request with the given client.default io.modelcontextprotocol.common.McpTransportContextReturns the transport context of the caller that the current thread works for.default AsyncModernMcpConnectionwithTransportContext(AsyncModernMcpTransportContextProvider transportContextProvider) Returns a connection that is this one, with the caller of each request supplied by the given provider.
-
Method Details
-
info
McpModernConnectionInfo 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 isclient().- 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 asprivate, lasts beyond that request.AsyncModernMcpToolCallbackProvideruses the answer to tell when such a listing could be kept with another kind of connection.- Returns:
trueifclient()returns a new client each time,falseby 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.EMPTYby 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
-
perRequest
static AsyncModernMcpConnection perRequest(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpAsyncClient> clientFactory) A connection that asks the given factory for a client each timeclient()is called, to give each request its own client. A factory is expected to build a new client on each call, and the connectionsays so. To keep clients, for example one for each user, usescoped(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 infoclientFactory- 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 infoclientFactory- builds a new client for the connection on each callclientScope- selects the client for a request- Returns:
- the connection
-