public class McpAsyncClient extends Object
This client implements the MCP specification, enabling AI models to interact with external tools and resources through a standardized interface. Key features include:
The client follows a lifecycle:
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.
McpClient,
McpSchema,
McpClientSession| Modifier and Type | Field and Description |
|---|---|
protected reactor.core.publisher.Sinks.One<McpSchema.InitializeResult> |
initializedSink |
| Modifier and Type | Method and Description |
|---|---|
reactor.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.
|
void |
close()
Closes the client connection immediately.
|
reactor.core.publisher.Mono<Void> |
closeGracefully()
Gracefully closes the client connection.
|
McpSchema.ClientCapabilities |
getClientCapabilities()
Get the client capabilities that define the supported features and functionality.
|
McpSchema.Implementation |
getClientInfo()
Get the client implementation information.
|
reactor.core.publisher.Mono<McpSchema.GetPromptResult> |
getPrompt(McpSchema.GetPromptRequest getPromptRequest)
Retrieves a specific prompt by its ID.
|
McpSchema.ServerCapabilities |
getServerCapabilities()
Get the server capabilities that define the supported features and functionality.
|
McpSchema.Implementation |
getServerInfo()
Get the server implementation information.
|
reactor.core.publisher.Mono<McpSchema.InitializeResult> |
initialize()
The initialization phase MUST be the first interaction between client and server.
|
boolean |
isInitialized()
Check if the client-server connection is initialized.
|
reactor.core.publisher.Mono<McpSchema.ListPromptsResult> |
listPrompts()
Retrieves the list of all prompts provided by the server.
|
reactor.core.publisher.Mono<McpSchema.ListPromptsResult> |
listPrompts(String cursor)
Retrieves a paginated list of prompts provided by the server.
|
reactor.core.publisher.Mono<McpSchema.ListResourcesResult> |
listResources()
Retrieves the list of all resources provided by the server.
|
reactor.core.publisher.Mono<McpSchema.ListResourcesResult> |
listResources(String cursor)
Retrieves a paginated list of resources provided by the server.
|
reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult> |
listResourceTemplates()
Retrieves the list of all resource templates provided by the server.
|
reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult> |
listResourceTemplates(String cursor)
Retrieves a paginated list of resource templates provided by the server.
|
reactor.core.publisher.Mono<McpSchema.ListToolsResult> |
listTools()
Retrieves the list of all tools provided by the server.
|
reactor.core.publisher.Mono<McpSchema.ListToolsResult> |
listTools(String cursor)
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<McpSchema.ReadResourceResult> |
readResource(McpSchema.ReadResourceRequest readResourceRequest)
Reads the content of a specific resource identified by the provided request.
|
reactor.core.publisher.Mono<McpSchema.ReadResourceResult> |
readResource(McpSchema.Resource resource)
Reads the content of a specific resource identified by the provided Resource
object.
|
reactor.core.publisher.Mono<Void> |
removeRoot(String rootUri)
Removes a root from the client's root list.
|
reactor.core.publisher.Mono<Void> |
rootsListChangedNotification()
Manually sends a roots/list_changed notification.
|
reactor.core.publisher.Mono<Void> |
setLoggingLevel(McpSchema.LoggingLevel loggingLevel)
Sets the minimum logging level for messages received from the server.
|
reactor.core.publisher.Mono<Void> |
subscribeResource(McpSchema.SubscribeRequest subscribeRequest)
Subscribes to changes in a specific resource.
|
reactor.core.publisher.Mono<Void> |
unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest)
Cancels an existing subscription to a resource.
|
protected final reactor.core.publisher.Sinks.One<McpSchema.InitializeResult> initializedSink
public McpSchema.ServerCapabilities getServerCapabilities()
public McpSchema.Implementation getServerInfo()
public boolean isInitialized()
public McpSchema.ClientCapabilities getClientCapabilities()
public McpSchema.Implementation getClientInfo()
public void close()
public reactor.core.publisher.Mono<Void> closeGracefully()
public reactor.core.publisher.Mono<McpSchema.InitializeResult> initialize()
public reactor.core.publisher.Mono<Object> ping()
public reactor.core.publisher.Mono<Void> addRoot(McpSchema.Root root)
root - The root to add.public reactor.core.publisher.Mono<Void> removeRoot(String rootUri)
rootUri - The URI of the root to remove.public reactor.core.publisher.Mono<Void> rootsListChangedNotification()
public reactor.core.publisher.Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToolRequest)
callToolRequest - The request containing the tool name and input parameters.McpSchema.CallToolRequest,
McpSchema.CallToolResult,
listTools()public reactor.core.publisher.Mono<McpSchema.ListToolsResult> listTools()
public reactor.core.publisher.Mono<McpSchema.ListToolsResult> listTools(String cursor)
cursor - Optional pagination cursor from a previous list requestpublic reactor.core.publisher.Mono<McpSchema.ListResourcesResult> listResources()
McpSchema.ListResourcesResult,
readResource(McpSchema.Resource)public reactor.core.publisher.Mono<McpSchema.ListResourcesResult> listResources(String cursor)
cursor - Optional pagination cursor from a previous list request.McpSchema.ListResourcesResult,
readResource(McpSchema.Resource)public reactor.core.publisher.Mono<McpSchema.ReadResourceResult> readResource(McpSchema.Resource resource)
resource - The resource to read, containing the URI that identifies the
resource.McpSchema.Resource,
McpSchema.ReadResourceResultpublic reactor.core.publisher.Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceRequest readResourceRequest)
readResourceRequest - The request containing the URI of the resource to readMcpSchema.ReadResourceRequest,
McpSchema.ReadResourceResultpublic reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates()
McpSchema.ListResourceTemplatesResultpublic reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String cursor)
cursor - Optional pagination cursor from a previous list request.McpSchema.ListResourceTemplatesResultpublic reactor.core.publisher.Mono<Void> subscribeResource(McpSchema.SubscribeRequest subscribeRequest)
subscribeRequest - The subscribe request containing the URI of the resource.McpSchema.SubscribeRequest,
unsubscribeResource(McpSchema.UnsubscribeRequest)public reactor.core.publisher.Mono<Void> unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest)
unsubscribeRequest - The unsubscribe request containing the URI of the
resource.McpSchema.UnsubscribeRequest,
subscribeResource(McpSchema.SubscribeRequest)public reactor.core.publisher.Mono<McpSchema.ListPromptsResult> listPrompts()
McpSchema.ListPromptsResult,
#getPrompt(GetPromptRequest)public reactor.core.publisher.Mono<McpSchema.ListPromptsResult> listPrompts(String cursor)
cursor - Optional pagination cursor from a previous list requestMcpSchema.ListPromptsResult,
#getPrompt(GetPromptRequest)public reactor.core.publisher.Mono<McpSchema.GetPromptResult> getPrompt(McpSchema.GetPromptRequest getPromptRequest)
getPromptRequest - The request containing the ID of the prompt to retrieve.McpSchema.GetPromptRequest,
McpSchema.GetPromptResult,
listPrompts()public reactor.core.publisher.Mono<Void> setLoggingLevel(McpSchema.LoggingLevel loggingLevel)
loggingLevel - The minimum logging level to receive.McpSchema.LoggingLevelCopyright © 2025. All rights reserved.