Interface SyncModernMcpClientScope
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Decides which
McpSyncClient 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, McpSyncClient> clients = new ConcurrentHashMap<>();
SyncModernMcpClientScope perUser = (connection, clientFactory) -> clients
.computeIfAbsent(connection.name() + "/" + currentUserName(), key -> clientFactory.get());
The scope is called once for each call to
SyncModernMcpToolCallbackProvider.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
SyncModernMcpConnection.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. A client that outlives a request must not hold a credential that expires: its
transportContextProvider should read the current credential of its user.
-
Method Summary
Modifier and TypeMethodDescriptionio.modelcontextprotocol.modern.client.McpSyncClientclient(McpModernConnectionInfo connectionInfo, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> clientFactory) Returns the client that serves the current request of the given connection.
-
Method Details
-
client
io.modelcontextprotocol.modern.client.McpSyncClient client(McpModernConnectionInfo connectionInfo, Supplier<io.modelcontextprotocol.modern.client.McpSyncClient> clientFactory) Returns the client that serves the current request of the given connection.- Parameters:
connectionInfo- the connection the request goes toclientFactory- 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
-