Package io.modelcontextprotocol.client
Class McpClient.AsyncSpec
java.lang.Object
io.modelcontextprotocol.client.McpClient.AsyncSpec
- Enclosing interface:
- McpClient
Asynchronous client specification. This class follows the builder pattern to
provide a fluent API for setting up clients with custom configurations.
The builder supports configuration of:
- Transport layer for client-server communication
- Request timeouts for operation boundaries
- Client capabilities for feature negotiation
- Client implementation details for version tracking
- Root URIs for resource access
- Change notification handlers for tools, resources, and prompts
- Custom message sampling logic
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Create an instance ofMcpAsyncClientwith the provided configurations or sensible defaults.capabilities(McpSchema.ClientCapabilities capabilities) Sets the client capabilities that will be advertised to the server during connection initialization.clientInfo(McpSchema.Implementation clientInfo) Sets the client implementation information that will be shared with the server during connection initialization.initializationTimeout(Duration initializationTimeout) loggingConsumer(Function<McpSchema.LoggingMessageNotification, reactor.core.publisher.Mono<Void>> loggingConsumer) Adds a consumer to be notified when logging messages are received from the server.loggingConsumers(List<Function<McpSchema.LoggingMessageNotification, reactor.core.publisher.Mono<Void>>> loggingConsumers) Adds multiple consumers to be notified when logging messages are received from the server.promptsChangeConsumer(Function<List<McpSchema.Prompt>, reactor.core.publisher.Mono<Void>> promptsChangeConsumer) Adds a consumer to be notified when the available prompts change.requestTimeout(Duration requestTimeout) Sets the duration to wait for server responses before timing out requests.resourcesChangeConsumer(Function<List<McpSchema.Resource>, reactor.core.publisher.Mono<Void>> resourcesChangeConsumer) Adds a consumer to be notified when the available resources change.roots(McpSchema.Root... roots) Sets the root URIs that this client can access, using a varargs parameter for convenience.roots(List<McpSchema.Root> roots) Sets the root URIs that this client can access.sampling(Function<McpSchema.CreateMessageRequest, reactor.core.publisher.Mono<McpSchema.CreateMessageResult>> samplingHandler) Sets a custom sampling handler for processing message creation requests.toolsChangeConsumer(Function<List<McpSchema.Tool>, reactor.core.publisher.Mono<Void>> toolsChangeConsumer) Adds a consumer to be notified when the available tools change.
-
Method Details
-
requestTimeout
Sets the duration to wait for server responses before timing out requests. This timeout applies to all requests made through the client, including tool calls, resource access, and prompt operations.- Parameters:
requestTimeout- The duration to wait before timing out requests. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if requestTimeout is null
-
initializationTimeout
- Parameters:
initializationTimeout- The duration to wait for the initialization lifecycle step to complete.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if initializationTimeout is null
-
capabilities
Sets the client capabilities that will be advertised to the server during connection initialization. Capabilities define what features the client supports, such as tool execution, resource access, and prompt handling.- Parameters:
capabilities- The client capabilities configuration. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if capabilities is null
-
clientInfo
Sets the client implementation information that will be shared with the server during connection initialization. This helps with version compatibility and debugging.- Parameters:
clientInfo- The client implementation details including name and version. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if clientInfo is null
-
roots
Sets the root URIs that this client can access. Roots define the base URIs for resources that the client can request from the server. For example, a root might be "file://workspace" for accessing workspace files.- Parameters:
roots- A list of root definitions. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if roots is null
-
roots
Sets the root URIs that this client can access, using a varargs parameter for convenience. This is an alternative toroots(List).- Parameters:
roots- An array of root definitions. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if roots is null- See Also:
-
sampling
public McpClient.AsyncSpec sampling(Function<McpSchema.CreateMessageRequest, reactor.core.publisher.Mono<McpSchema.CreateMessageResult>> samplingHandler) Sets a custom sampling handler for processing message creation requests. The sampling handler can modify or validate messages before they are sent to the server, enabling custom processing logic.- Parameters:
samplingHandler- A function that processes message requests and returns results. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if samplingHandler is null
-
toolsChangeConsumer
public McpClient.AsyncSpec toolsChangeConsumer(Function<List<McpSchema.Tool>, reactor.core.publisher.Mono<Void>> toolsChangeConsumer) Adds a consumer to be notified when the available tools change. This allows the client to react to changes in the server's tool capabilities, such as tools being added or removed.- Parameters:
toolsChangeConsumer- A consumer that receives the updated list of available tools. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if toolsChangeConsumer is null
-
resourcesChangeConsumer
public McpClient.AsyncSpec resourcesChangeConsumer(Function<List<McpSchema.Resource>, reactor.core.publisher.Mono<Void>> resourcesChangeConsumer) Adds a consumer to be notified when the available resources change. This allows the client to react to changes in the server's resource availability, such as files being added or removed.- Parameters:
resourcesChangeConsumer- A consumer that receives the updated list of available resources. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if resourcesChangeConsumer is null
-
promptsChangeConsumer
public McpClient.AsyncSpec promptsChangeConsumer(Function<List<McpSchema.Prompt>, reactor.core.publisher.Mono<Void>> promptsChangeConsumer) Adds a consumer to be notified when the available prompts change. This allows the client to react to changes in the server's prompt templates, such as new templates being added or existing ones being modified.- Parameters:
promptsChangeConsumer- A consumer that receives the updated list of available prompts. Must not be null.- Returns:
- This builder instance for method chaining
- Throws:
IllegalArgumentException- if promptsChangeConsumer is null
-
loggingConsumer
public McpClient.AsyncSpec loggingConsumer(Function<McpSchema.LoggingMessageNotification, reactor.core.publisher.Mono<Void>> loggingConsumer) Adds a consumer to be notified when logging messages are received from the server. This allows the client to react to log messages, such as warnings or errors, that are sent by the server.- Parameters:
loggingConsumer- A consumer that receives logging messages. Must not be null.- Returns:
- This builder instance for method chaining
-
loggingConsumers
public McpClient.AsyncSpec loggingConsumers(List<Function<McpSchema.LoggingMessageNotification, reactor.core.publisher.Mono<Void>>> loggingConsumers) Adds multiple consumers to be notified when logging messages are received from the server. This allows the client to react to log messages, such as warnings or errors, that are sent by the server.- Parameters:
loggingConsumers- A list of consumers that receive logging messages. Must not be null.- Returns:
- This builder instance for method chaining
-
build
Create an instance ofMcpAsyncClientwith the provided configurations or sensible defaults.- Returns:
- a new instance of
McpAsyncClient.
-