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,
McpClientTransport| Modifier and Type | Field and Description |
|---|---|
static com.fasterxml.jackson.core.type.TypeReference<McpSchema.CreateMessageRequest> |
CREATE_MESSAGE_REQUEST_TYPE_REF |
static com.fasterxml.jackson.core.type.TypeReference<McpSchema.InitializeResult> |
INITIALIZE_RESULT_TYPE_REF |
static com.fasterxml.jackson.core.type.TypeReference<McpSchema.LoggingMessageNotification> |
LOGGING_MESSAGE_NOTIFICATION_TYPE_REF |
static com.fasterxml.jackson.core.type.TypeReference<Object> |
OBJECT_TYPE_REF |
static com.fasterxml.jackson.core.type.TypeReference<McpSchema.PaginatedRequest> |
PAGINATED_REQUEST_TYPE_REF |
static com.fasterxml.jackson.core.type.TypeReference<McpSchema.ProgressNotification> |
PROGRESS_NOTIFICATION_TYPE_REF |
| 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.
|
reactor.core.publisher.Mono<McpSchema.CompleteResult> |
completeCompletion(McpSchema.CompleteRequest completeRequest)
Sends a completion/complete request to generate value suggestions based on a given
reference and argument.
|
McpSchema.ClientCapabilities |
getClientCapabilities()
Get the client capabilities that define the supported features and functionality.
|
McpSchema.Implementation |
getClientInfo()
Get the client implementation information.
|
McpSchema.InitializeResult |
getCurrentInitializationResult()
Get the current initialization result.
|
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.
|
String |
getServerInstructions()
Get the server instructions that provide guidance to the client on how to interact
with this server.
|
reactor.core.publisher.Mono<McpSchema.InitializeResult> |
initialize()
The initialization phase should 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.
|
public static final com.fasterxml.jackson.core.type.TypeReference<Object> OBJECT_TYPE_REF
public static final com.fasterxml.jackson.core.type.TypeReference<McpSchema.PaginatedRequest> PAGINATED_REQUEST_TYPE_REF
public static final com.fasterxml.jackson.core.type.TypeReference<McpSchema.InitializeResult> INITIALIZE_RESULT_TYPE_REF
public static final com.fasterxml.jackson.core.type.TypeReference<McpSchema.CreateMessageRequest> CREATE_MESSAGE_REQUEST_TYPE_REF
public static final com.fasterxml.jackson.core.type.TypeReference<McpSchema.LoggingMessageNotification> LOGGING_MESSAGE_NOTIFICATION_TYPE_REF
public static final com.fasterxml.jackson.core.type.TypeReference<McpSchema.ProgressNotification> PROGRESS_NOTIFICATION_TYPE_REF
public McpSchema.InitializeResult getCurrentInitializationResult()
public McpSchema.ServerCapabilities getServerCapabilities()
public String getServerInstructions()
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()
During this phase, the client and server:
The server MUST respond with its own capabilities and information.
After successful initialization, the client MUST send an initialized notification to indicate it is ready to begin normal operations.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.LoggingLevelpublic reactor.core.publisher.Mono<McpSchema.CompleteResult> completeCompletion(McpSchema.CompleteRequest completeRequest)
completeRequest - The request containing the prompt or resource reference and
argument for which to generate completions.McpSchema.CompleteRequest,
McpSchema.CompleteResultCopyright © 2025. All rights reserved.