public class McpAsyncClient
extends java.lang.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
限定符和类型 | 字段和说明 |
---|---|
protected reactor.core.publisher.Sinks.One<McpSchema.InitializeResult> |
initializedSink |
限定符和类型 | 方法和说明 |
---|---|
reactor.core.publisher.Mono<java.lang.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<java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.String cursor)
Retrieves a paginated list of tools provided by the server.
|
reactor.core.publisher.Mono<java.lang.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<java.lang.Void> |
removeRoot(java.lang.String rootUri)
Removes a root from the client's root list.
|
reactor.core.publisher.Mono<java.lang.Void> |
rootsListChangedNotification()
Manually sends a roots/list_changed notification.
|
reactor.core.publisher.Mono<java.lang.Void> |
setLoggingLevel(McpSchema.LoggingLevel loggingLevel)
Sets the minimum logging level for messages received from the server.
|
reactor.core.publisher.Mono<java.lang.Void> |
subscribeResource(McpSchema.SubscribeRequest subscribeRequest)
Subscribes to changes in a specific resource.
|
reactor.core.publisher.Mono<java.lang.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<java.lang.Void> closeGracefully()
public reactor.core.publisher.Mono<McpSchema.InitializeResult> initialize()
public reactor.core.publisher.Mono<java.lang.Object> ping()
public reactor.core.publisher.Mono<java.lang.Void> addRoot(McpSchema.Root root)
root
- The root to add.public reactor.core.publisher.Mono<java.lang.Void> removeRoot(java.lang.String rootUri)
rootUri
- The URI of the root to remove.public reactor.core.publisher.Mono<java.lang.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(java.lang.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(java.lang.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.ReadResourceResult
public reactor.core.publisher.Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceRequest readResourceRequest)
readResourceRequest
- The request containing the URI of the resource to readMcpSchema.ReadResourceRequest
,
McpSchema.ReadResourceResult
public reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates()
McpSchema.ListResourceTemplatesResult
public reactor.core.publisher.Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(java.lang.String cursor)
cursor
- Optional pagination cursor from a previous list request.McpSchema.ListResourceTemplatesResult
public reactor.core.publisher.Mono<java.lang.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<java.lang.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(java.lang.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<java.lang.Void> setLoggingLevel(McpSchema.LoggingLevel loggingLevel)
loggingLevel
- The minimum logging level to receive.McpSchema.LoggingLevel