Package io.modelcontextprotocol.client
Class McpAsyncClient
java.lang.Object
io.modelcontextprotocol.client.McpAsyncClient
The Model Context Protocol (MCP) client implementation that provides asynchronous
communication with MCP servers using Project Reactor's Mono and Flux types.
This client implements the MCP specification, enabling AI models to interact with external tools and resources through a standardized interface. Key features include:
- Asynchronous communication using reactive programming patterns
- Tool discovery and invocation for server-provided functionality
- Resource access and management with URI-based addressing
- Prompt template handling for standardized AI interactions
- Real-time notifications for tools, resources, and prompts changes
- Structured logging with configurable severity levels
- Message sampling for AI model interactions
The client follows a lifecycle:
- Initialization - Establishes connection and negotiates capabilities
- Normal Operation - Handles requests and notifications
- Graceful Shutdown - Ensures clean connection termination
This implementation uses Project Reactor for non-blocking operations, making it suitable for high-throughput scenarios and reactive applications. All operations return Mono or Flux types that can be composed into reactive pipelines.
- Author:
- Dariusz Jędrzejczyk, Christian Tzolov
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Void>addRoot(McpSchema.Root root) Adds a new root to the client's root list.reactor.core.publisher.Mono<McpSchema.CallToolResult>callTool(McpSchema.CallToolRequest callToolRequest) Calls a tool provided by the server.voidclose()Closes the client connection immediately.reactor.core.publisher.Mono<Void>Gracefully closes the client connection.Get the client capabilities that define the supported features and functionality.Get the client implementation information.reactor.core.publisher.Mono<McpSchema.GetPromptResult>getPrompt(McpSchema.GetPromptRequest getPromptRequest) Get a prompt by its id.Get the server capabilities that define the supported features and functionality.Get the server implementation information.reactor.core.publisher.Mono<McpSchema.InitializeResult>The initialization phase MUST be the first interaction between client and server.reactor.core.publisher.Mono<McpSchema.ListPromptsResult>List all available prompts.reactor.core.publisher.Mono<McpSchema.ListPromptsResult>listPrompts(String cursor) List all available prompts.reactor.core.publisher.Mono<McpSchema.ListResourcesResult>Send a resources/list request.reactor.core.publisher.Mono<McpSchema.ListResourcesResult>listResources(String cursor) Send a resources/list request.reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult>Resource templates allow servers to expose parameterized resources using URI templates.reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult>listResourceTemplates(String cursor) Resource templates allow servers to expose parameterized resources using URI templates.reactor.core.publisher.Mono<McpSchema.ListToolsResult>Retrieves the list of all tools provided by the server.reactor.core.publisher.Mono<McpSchema.ListToolsResult>Retrieves a paginated list of tools provided by the server.reactor.core.publisher.Mono<Object>ping()Sends a ping request to the server.reactor.core.publisher.Mono<Void>(Server) An optional notification from the server to the client, informing it that the list of prompts it offers has changed.reactor.core.publisher.Mono<McpSchema.ReadResourceResult>readResource(McpSchema.ReadResourceRequest readResourceRequest) Send a resources/read request.reactor.core.publisher.Mono<McpSchema.ReadResourceResult>readResource(McpSchema.Resource resource) Send a resources/read request.reactor.core.publisher.Mono<Void>removeRoot(String rootUri) Removes a root from the client's root list.reactor.core.publisher.Mono<Void>Manually sends a roots/list_changed notification.reactor.core.publisher.Mono<Void>List Changed Notification.reactor.core.publisher.Mono<Void>setLoggingLevel(McpSchema.LoggingLevel loggingLevel) Client can set the minimum logging level it wants to receive from the server.reactor.core.publisher.Mono<Void>subscribeResource(McpSchema.SubscribeRequest subscribeRequest) Subscriptions.reactor.core.publisher.Mono<Void>unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest) Send a resources/unsubscribe request.
-
Method Details
-
initialize
The initialization phase MUST be the first interaction between client and server. During this phase, the client and server:- Establish protocol version compatibility
- Exchange and negotiate capabilities
- Share implementation details
The client MUST initiate this phase by sending an initialize request containing:- The protocol version the client supports
- The client's capabilities
- Client implementation information
McpSchema.ServerCapabilities.
After successful initialization, the client MUST send an initialized notification to indicate it is ready to begin normal operations.
Initialization Spec- Returns:
- the initialize result.
-
getServerCapabilities
Get the server capabilities that define the supported features and functionality.- Returns:
- The server capabilities
-
getServerInfo
Get the server implementation information.- Returns:
- The server implementation details
-
getClientCapabilities
Get the client capabilities that define the supported features and functionality.- Returns:
- The client capabilities
-
getClientInfo
Get the client implementation information.- Returns:
- The client implementation details
-
close
public void close()Closes the client connection immediately. -
closeGracefully
Gracefully closes the client connection.- Returns:
- A Mono that completes when the connection is closed
-
ping
Sends a ping request to the server.- Returns:
- A Mono that completes with the server's ping response
-
addRoot
Adds a new root to the client's root list.- Parameters:
root- The root to add- Returns:
- A Mono that completes when the root is added and notifications are sent
-
removeRoot
Removes a root from the client's root list.- Parameters:
rootUri- The URI of the root to remove- Returns:
- A Mono that completes when the root is removed and notifications are sent
-
rootsListChangedNotification
Manually sends a roots/list_changed notification. The addRoot and removeRoot methods automatically send the roots/list_changed notification.- Returns:
- A Mono that completes when the notification is sent
-
callTool
public reactor.core.publisher.Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToolRequest) Calls a tool provided by the server. Tools enable servers to expose executable functionality that can interact with external systems, perform computations, and take actions in the real world.- Parameters:
callToolRequest- The request containing: - name: The name of the tool to call (must match a tool name from tools/list) - arguments: Arguments that conform to the tool's input schema- Returns:
- A Mono that emits the tool execution result containing: - content: List of content items (text, images, or embedded resources) representing the tool's output - isError: Boolean indicating if the execution failed (true) or succeeded (false/absent)
-
listTools
Retrieves the list of all tools provided by the server.- Returns:
- A Mono that emits the list of tools result containing: - tools: List of available tools, each with a name, description, and input schema - nextCursor: Optional cursor for pagination if more tools are available
-
listTools
Retrieves a paginated list of tools provided by the server.- Parameters:
cursor- Optional pagination cursor from a previous list request- Returns:
- A Mono that emits the list of tools result containing: - tools: List of available tools, each with a name, description, and input schema - nextCursor: Optional cursor for pagination if more tools are available
-
listResources
Send a resources/list request.- Returns:
- A Mono that completes with the list of resources result
-
listResources
Send a resources/list request.- Parameters:
cursor- the cursor for pagination- Returns:
- A Mono that completes with the list of resources result
-
readResource
public reactor.core.publisher.Mono<McpSchema.ReadResourceResult> readResource(McpSchema.Resource resource) Send a resources/read request.- Parameters:
resource- the resource to read- Returns:
- A Mono that completes with the resource content
-
readResource
public reactor.core.publisher.Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceRequest readResourceRequest) Send a resources/read request.- Parameters:
readResourceRequest- the read resource request- Returns:
- A Mono that completes with the resource content
-
listResourceTemplates
Resource templates allow servers to expose parameterized resources using URI templates. Arguments may be auto-completed through the completion API. Request a list of resource templates the server has.- Returns:
- A Mono that completes with the list of resource templates result
-
listResourceTemplates
public reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String cursor) Resource templates allow servers to expose parameterized resources using URI templates. Arguments may be auto-completed through the completion API. Request a list of resource templates the server has.- Parameters:
cursor- the cursor for pagination- Returns:
- A Mono that completes with the list of resource templates result
-
sendResourcesListChanged
List Changed Notification. When the list of available resources changes, servers that declared the listChanged capability SHOULD send a notification.- Returns:
- A Mono that completes when the notification is sent
-
subscribeResource
public reactor.core.publisher.Mono<Void> subscribeResource(McpSchema.SubscribeRequest subscribeRequest) Subscriptions. The protocol supports optional subscriptions to resource changes. Clients can subscribe to specific resources and receive notifications when they change. Send a resources/subscribe request.- Parameters:
subscribeRequest- the subscribe request contains the uri of the resource to subscribe to- Returns:
- A Mono that completes when the subscription is complete
-
unsubscribeResource
public reactor.core.publisher.Mono<Void> unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest) Send a resources/unsubscribe request.- Parameters:
unsubscribeRequest- the unsubscribe request contains the uri of the resource to unsubscribe from- Returns:
- A Mono that completes when the unsubscription is complete
-
listPrompts
List all available prompts.- Returns:
- A Mono that completes with the list of prompts result
-
listPrompts
List all available prompts.- Parameters:
cursor- the cursor for pagination- Returns:
- A Mono that completes with the list of prompts result
-
getPrompt
public reactor.core.publisher.Mono<McpSchema.GetPromptResult> getPrompt(McpSchema.GetPromptRequest getPromptRequest) Get a prompt by its id.- Parameters:
getPromptRequest- the get prompt request- Returns:
- A Mono that completes with the get prompt result
-
promptListChangedNotification
(Server) An optional notification from the server to the client, informing it that the list of prompts it offers has changed. This may be issued by servers without any previous subscription from the client.- Returns:
- A Mono that completes when the notification is sent
-
setLoggingLevel
Client can set the minimum logging level it wants to receive from the server.- Parameters:
loggingLevel- the min logging level
-