Class DefaultMcpClient.Builder
- Enclosing class:
DefaultMcpClient
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddListener(McpClientListener listener) Adds a listener to receive MCP client events.addListeners(List<McpClientListener> listeners) Adds multiple listeners to receive MCP client events.autoHealthCheck(boolean autoHealthCheck) Enables or disables the automatic health check feature.autoHealthCheckInterval(Duration interval) Sets the interval for the automatic health checks.build()cachePromptList(boolean cachePromptList) If set to true, the client will cache the prompt list obtained from the server until it's notified by the server that the prompts have changed.cacheResourceList(boolean cacheResourceList) If set to true, the client will cache the resource and resource template lists obtained from the server until it's notified by the server that the resources have changed.cacheToolList(boolean cacheToolList) If set to true, the client will cache the tool list obtained from the server until it's notified by the server that the tools have changed or until the cache is evicted.clientName(String clientName) Sets the name that the client will use to identify itself to the MCP server in the initialization message.clientVersion(String clientVersion) Sets the version string that the client will use to identify itself to the MCP server in the initialization message.initializationTimeout(Duration initializationTimeout) Sets the timeout for initializing the client.Sets a unique identifier for the client.listener(McpClientListener listener) Deprecated.logHandler(McpLogMessageHandler logHandler) Sets the log message handler for the client.metaSupplier(McpMetaSupplier metaSupplier) Sets a supplier of_metafields for MCP client requests and notifications.multiRoundTripMaxRetries(int multiRoundTripMaxRetries) Sets the maximum number of multi round-trip retries for operations that returninput_required(MCP protocol 2026-07-28 or later).onResourceUpdated(BiConsumer<McpClient, String> onResourceUpdated) Sets a callback to be invoked when the server sends anotifications/resources/updatednotification for a subscribed resource.pingTimeout(Duration pingTimeout) The timeout to apply when waiting for a ping response.progressHandler(McpProgressHandler progressHandler) Sets the progress handler for the client.promptsTimeout(Duration promptsTimeout) Sets the timeout for prompt-related operations (listing prompts as well as rendering the contents of a prompt).protocolDetectionTimeout(Duration protocolDetectionTimeout) Sets how long the client waits for the server to answer theserver/discoverrequest that detects which protocol version the server speaks.protocolVersion(String protocolVersion) Sets the protocol version.reconnectInterval(Duration reconnectInterval) The delay before attempting to reconnect after a failed connection.resourcesTimeout(Duration resourcesTimeout) Sets the timeout for resource-related operations (listing resources as well as reading the contents of a resource).Specify the initial set of roots that are available to the server upon its request.subscribeToPromptListChanges(boolean subscribe) Sets whether to automatically subscribe to prompt list change notifications when using MCP protocol 2026-07-28 or later.subscribeToResourceListChanges(boolean subscribe) Sets whether to automatically subscribe to resource list change notifications when using MCP protocol 2026-07-28 or later.subscribeToToolListChanges(boolean subscribe) Sets whether to automatically subscribe to tool list change notifications when using MCP protocol 2026-07-28 or later.toolExecutionTimeout(Duration toolExecutionTimeout) Sets the timeout for tool execution.toolExecutionTimeoutErrorMessage(String toolExecutionTimeoutErrorMessage) Sets the error message to return when a tool execution times out.toolResultExtractor(McpToolResultExtractor toolResultExtractor) Sets the extractor used for MCP tool responses that return ordinaryCallToolResult.result.content[]items.transport(McpTransport transport) Sets the transport protocol to use for communicating with the MCP server.
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
transport
Sets the transport protocol to use for communicating with the MCP server. This is a mandatory parameter. A successfully constructed DefaultMcpClient takes over the resource ownership of this transport and will close it when it itself is closed. -
key
Sets a unique identifier for the client. If none is provided, a UUID will be automatically generated. -
clientName
Sets the name that the client will use to identify itself to the MCP server in the initialization message. The default value is "langchain4j". -
clientVersion
Sets the version string that the client will use to identify itself to the MCP server in the initialization message. The default value is "1.0". -
protocolVersion
Sets the protocol version. If null or empty (the default), the client detects the server's protocol version, preferring 2026-07-28 over 2025-11-25. If explicitly set to "2026-07-28", modern protocol is forced. If set to "2025-11-25", legacy protocol is forced. Any other value triggers detection but uses the given string when advertising the version to the server.Detection costs one extra round trip at startup: the client sends
server/discoverand falls back to the legacyinitializehandshake if the server answers with an error or does not answer withinprotocolDetectionTimeout(Duration). Setting the version explicitly skips the detection round trip entirely, which is also the way out if a server reacts badly to being sent a method it does not know. -
initializationTimeout
Sets the timeout for initializing the client. The default value is 30 seconds. -
protocolDetectionTimeout
Sets how long the client waits for the server to answer theserver/discoverrequest that detects which protocol version the server speaks. A server that does not answer within this time is assumed to speak the legacy protocol, and the client falls back to theinitializehandshake.This only applies while the protocol version is being detected, which is the case when no explicit
protocolVersion(String)is set. It defaults toinitializationTimeout(Duration), because a server that is started as a subprocess needs time to boot before it can answer anything, and a detection request that gives up too early makes a modern server look like a legacy one.Lower it if your servers answer quickly and you would rather not wait on one that ignores the request instead of rejecting it.
-
toolExecutionTimeout
Sets the timeout for tool execution. This value applies to each tool execution individually. The default value is 60 seconds. A value of zero means no timeout. -
resourcesTimeout
Sets the timeout for resource-related operations (listing resources as well as reading the contents of a resource). The default value is 60 seconds. A value of zero means no timeout. -
promptsTimeout
Sets the timeout for prompt-related operations (listing prompts as well as rendering the contents of a prompt). The default value is 60 seconds. A value of zero means no timeout. -
toolExecutionTimeoutErrorMessage
public DefaultMcpClient.Builder toolExecutionTimeoutErrorMessage(String toolExecutionTimeoutErrorMessage) Sets the error message to return when a tool execution times out. The default value is "There was a timeout executing the tool". -
logHandler
Sets the log message handler for the client. -
pingTimeout
The timeout to apply when waiting for a ping response. Currently, this is only used in the health check - if the server does not send a pong within this timeframe, the health check will fail. The timeout is 10 seconds. -
reconnectInterval
The delay before attempting to reconnect after a failed connection. The default is 5 seconds. -
autoHealthCheck
Enables or disables the automatic health check feature. When enabled, the client will periodically send ping messages to the server to ensure the connection is alive, and will attempt to reconnect if it's not. The default is enabled -
autoHealthCheckInterval
Sets the interval for the automatic health checks. This is only used when the auto health check feature is enabled. The default is 30 seconds -
roots
Specify the initial set of roots that are available to the server upon its request. -
cacheToolList
If set to true, the client will cache the tool list obtained from the server until it's notified by the server that the tools have changed or until the cache is evicted. If set to false, there is no tool caching and the client will always fetch the tool list from the server. The default is true. -
cacheResourceList
If set to true, the client will cache the resource and resource template lists obtained from the server until it's notified by the server that the resources have changed. If set to false, there is no caching and the client will always fetch the resource list from the server. The default is true. -
cachePromptList
If set to true, the client will cache the prompt list obtained from the server until it's notified by the server that the prompts have changed. If set to false, there is no caching and the client will always fetch the prompt list from the server. The default is true. -
listener
Deprecated.UseaddListener(McpClientListener)instead.Sets a listener to receive MCP client events. A listener is notified before and after each call to the MCP server. Currently, this applies to tool calls, resource retrievals, and prompt retrievals. -
addListener
Adds a listener to receive MCP client events. Multiple listeners can be added; they will all be invoked before and after each call to the MCP server. Currently, this applies to tool calls, resource retrievals, and prompt retrievals. -
addListeners
Adds multiple listeners to receive MCP client events. All listeners will be invoked before and after each call to the MCP server. Currently, this applies to tool calls, resource retrievals, and prompt retrievals. -
progressHandler
Sets the progress handler for the client. When set, the client will include a progress token in tool execution requests, and progress notifications received from the server will be forwarded to this handler. -
metaSupplier
Sets a supplier of_metafields for MCP client requests and notifications. The supplier is called before every request or notification sent to the server. Unlike HTTP headers, this applies to all transports. -
toolResultExtractor
Sets the extractor used for MCP tool responses that return ordinaryCallToolResult.result.content[]items. Responses withstructuredContentare handled separately and are not affected by this setting. The default client only supportsstructuredContentand text content out of the box. More specialized extraction strategies, such as parsing text items and returning the first JSON object, can be implemented with a custom extractor. -
onResourceUpdated
Sets a callback to be invoked when the server sends anotifications/resources/updatednotification for a subscribed resource. The callback receives the instance of the affected MCP client and the URI of the updated resource. -
subscribeToToolListChanges
Sets whether to automatically subscribe to tool list change notifications when using MCP protocol 2026-07-28 or later. Default is true. -
subscribeToPromptListChanges
Sets whether to automatically subscribe to prompt list change notifications when using MCP protocol 2026-07-28 or later. Default is true. -
subscribeToResourceListChanges
Sets whether to automatically subscribe to resource list change notifications when using MCP protocol 2026-07-28 or later. Default is true. -
multiRoundTripMaxRetries
Sets the maximum number of multi round-trip retries for operations that returninput_required(MCP protocol 2026-07-28 or later). The default is 3. -
build
-
addListener(McpClientListener)instead.