public interface McpClient
This class serves as the main entry point for establishing connections with MCP servers, implementing the client-side of the MCP specification. The protocol follows a client-server architecture where:
The class provides factory methods to create either:
McpAsyncClient for non-blocking operations with CompletableFuture responses
McpSyncClient for blocking operations with direct responses
Example of creating a basic synchronous client:
McpClient.sync(transport)
.requestTimeout(Duration.ofSeconds(5))
.build();
Example of creating a basic asynchronous client:
McpClient.async(transport)
.requestTimeout(Duration.ofSeconds(5))
.build();
Example with advanced asynchronous configuration:
McpClient.async(transport)
.requestTimeout(Duration.ofSeconds(10))
.capabilities(new ClientCapabilities(...))
.clientInfo(new Implementation("My Client", "1.0.0"))
.roots(new Root("file://workspace", "Workspace Files"))
.toolsChangeConsumer(tools -> Mono.fromRunnable(() -> System.out.println("Tools updated: " + tools)))
.resourcesChangeConsumer(resources -> Mono.fromRunnable(() -> System.out.println("Resources updated: " + resources)))
.promptsChangeConsumer(prompts -> Mono.fromRunnable(() -> System.out.println("Prompts updated: " + prompts)))
.loggingConsumer(message -> Mono.fromRunnable(() -> System.out.println("Log message: " + message)))
.build();
The client supports:
The client supports structured logging through the MCP logging utility:
McpAsyncClient,
McpSyncClient,
McpTransport| Modifier and Type | Interface and Description |
|---|---|
static class |
McpClient.AsyncSpec
Asynchronous client specification.
|
static class |
McpClient.SyncSpec
Synchronous client specification.
|
| Modifier and Type | Method and Description |
|---|---|
static McpClient.AsyncSpec |
async(McpClientTransport transport)
Start building an asynchronous MCP client with the specified transport layer.
|
static McpClient.SyncSpec |
sync(McpClientTransport transport)
Start building a synchronous MCP client with the specified transport layer.
|
static McpClient.SyncSpec sync(McpClientTransport transport)
transport - The transport layer implementation for MCP communication. Common
implementations include StdioClientTransport for stdio-based communication
and SseClientTransport for SSE-based communication.IllegalArgumentException - if transport is nullstatic McpClient.AsyncSpec async(McpClientTransport transport)
transport - The transport layer implementation for MCP communication. Common
implementations include StdioClientTransport for stdio-based communication
and SseClientTransport for SSE-based communication.IllegalArgumentException - if transport is nullCopyright © 2025. All rights reserved.