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:
perRequest(org.springframework.ai.mcp.modern.client.McpModernConnectionInfo, java.util.function.Supplier<io.modelcontextprotocol.modern.client.McpSyncClient>)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.McpSyncClient)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.McpSyncClient>, org.springframework.ai.mcp.modern.client.SyncModernMcpClientScope)leaves the choice to aSyncModernMcpClientScope, for example one that keeps a client for each user.
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 TypeMethodDescriptiondefault io.modelcontextprotocol.modern.client.McpSyncClientReturns 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.McpSyncClientclient()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 SyncModernMcpConnectionperRequest(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> clientFactory) A connection that asks the given factory for a client each timeclient()is called, to give each request its own client.static SyncModernMcpConnectionscoped(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> clientFactory, SyncModernMcpClientScope clientScope) A connection that asks the given scope for the client of each request.static SyncModernMcpConnectionshared(McpModernConnectionInfo info, io.modelcontextprotocol.modern.client.McpSyncClient client) A connection that serves every request with the given client.
-
Method Details
-
info
McpModernConnectionInfo 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 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.SyncModernMcpToolCallbackProvideruses 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
-
perRequest
static SyncModernMcpConnection perRequest(McpModernConnectionInfo info, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> 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.McpSyncClient>, org.springframework.ai.mcp.modern.client.SyncModernMcpClientScope).- Parameters:
info- the connection infoclientFactory- 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 infoclientFactory- builds a new client for the connection on each callclientScope- selects the client for a request- Returns:
- the connection
-