3434import org .springframework .ai .mcp .util .Assert ;
3535
3636/**
37- * Factory class for creating Model Context Protocol (MCP) clients. MCP is a protocol that enables
38- * AI models to interact with external tools and resources through a standardized interface.
39- *
40- * <p>This class serves as the main entry point for establishing connections with MCP servers,
41- * implementing the client-side of the MCP specification. The protocol follows a client-server
42- * architecture where:
37+ * Factory class for creating Model Context Protocol (MCP) clients. MCP is a protocol that
38+ * enables AI models to interact with external tools and resources through a standardized
39+ * interface.
40+ *
41+ * <p>
42+ * This class serves as the main entry point for establishing connections with MCP
43+ * servers, implementing the client-side of the MCP specification. The protocol follows a
44+ * client-server architecture where:
4345 * <ul>
4446 * <li>The client (this implementation) initiates connections and sends requests
4547 * <li>The server responds to requests and provides access to tools and resources
46- * <li>Communication occurs through a transport layer (e.g., stdio, SSE) using JSON-RPC 2.0
48+ * <li>Communication occurs through a transport layer (e.g., stdio, SSE) using JSON-RPC
49+ * 2.0
4750 * </ul>
4851 *
49- * <p>The class provides factory methods to create either:
52+ * <p>
53+ * The class provides factory methods to create either:
5054 * <ul>
5155 * <li>{@link McpAsyncClient} for non-blocking operations with CompletableFuture responses
5256 * <li>{@link McpSyncClient} for blocking operations with direct responses
5357 * </ul>
5458 *
55- * <p>Example of creating a basic client:
56- * <pre>{@code
59+ * <p>
60+ * Example of creating a basic client: <pre>{@code
5761 * McpClient.using(transport)
5862 * .requestTimeout(Duration.ofSeconds(5))
5963 * .sync(); // or .async()
6064 * }</pre>
6165 *
62- * <p>Example with advanced configuration:
63- * <pre>{@code
66+ * <p>
67+ * Example with advanced configuration: <pre>{@code
6468 * McpClient.using(transport)
6569 * .requestTimeout(Duration.ofSeconds(10))
6670 * .capabilities(new ClientCapabilities(...))
7074 * .async();
7175 * }</pre>
7276 *
73- * <p>The client supports:
77+ * <p>
78+ * The client supports:
7479 * <ul>
7580 * <li>Tool discovery and invocation
7681 * <li>Resource access and management
8893public interface McpClient {
8994
9095 /**
91- * Start building an MCP client with the specified transport layer.
92- * The transport layer handles the low-level communication between
93- * client and server using protocols like stdio or Server-Sent Events (SSE).
94- *
95- * @param transport The transport layer implementation for MCP communication.
96- * Common implementations include {@code StdioClientTransport}
97- * for stdio-based communication and {@code SseClientTransport}
98- * for SSE-based communication.
96+ * Start building an MCP client with the specified transport layer. The transport
97+ * layer handles the low-level communication between client and server using protocols
98+ * like stdio or Server-Sent Events (SSE).
99+ * @param transport The transport layer implementation for MCP communication. Common
100+ * implementations include {@code StdioClientTransport} for stdio-based communication
101+ * and {@code SseClientTransport} for SSE-based communication.
99102 * @return A new builder instance for configuring the client
100103 * @throws IllegalArgumentException if transport is null
101104 */
@@ -104,11 +107,12 @@ public static Builder using(McpTransport transport) {
104107 }
105108
106109 /**
107- * Builder class for creating and configuring MCP clients. This class follows
108- * the builder pattern to provide a fluent API for setting up clients with
109- * custom configurations.
110+ * Builder class for creating and configuring MCP clients. This class follows the
111+ * builder pattern to provide a fluent API for setting up clients with custom
112+ * configurations.
110113 *
111- * <p>The builder supports configuration of:
114+ * <p>
115+ * The builder supports configuration of:
112116 * <ul>
113117 * <li>Transport layer for client-server communication
114118 * <li>Request timeouts for operation boundaries
@@ -145,12 +149,11 @@ private Builder(McpTransport transport) {
145149 }
146150
147151 /**
148- * Sets the duration to wait for server responses before timing out requests.
149- * This timeout applies to all requests made through the client, including
150- * tool calls, resource access, and prompt operations.
151- *
152- * @param requestTimeout The duration to wait before timing out requests.
153- * Must not be null.
152+ * Sets the duration to wait for server responses before timing out requests. This
153+ * timeout applies to all requests made through the client, including tool calls,
154+ * resource access, and prompt operations.
155+ * @param requestTimeout The duration to wait before timing out requests. Must not
156+ * be null.
154157 * @return This builder instance for method chaining
155158 * @throws IllegalArgumentException if requestTimeout is null
156159 */
@@ -162,9 +165,8 @@ public Builder requestTimeout(Duration requestTimeout) {
162165
163166 /**
164167 * Sets the client capabilities that will be advertised to the server during
165- * connection initialization. Capabilities define what features the client supports,
166- * such as tool execution, resource access, and prompt handling.
167- *
168+ * connection initialization. Capabilities define what features the client
169+ * supports, such as tool execution, resource access, and prompt handling.
168170 * @param capabilities The client capabilities configuration. Must not be null.
169171 * @return This builder instance for method chaining
170172 * @throws IllegalArgumentException if capabilities is null
@@ -179,9 +181,8 @@ public Builder capabilities(ClientCapabilities capabilities) {
179181 * Sets the client implementation information that will be shared with the server
180182 * during connection initialization. This helps with version compatibility and
181183 * debugging.
182- *
183184 * @param clientInfo The client implementation details including name and version.
184- * Must not be null.
185+ * Must not be null.
185186 * @return This builder instance for method chaining
186187 * @throws IllegalArgumentException if clientInfo is null
187188 */
@@ -192,10 +193,9 @@ public Builder clientInfo(Implementation clientInfo) {
192193 }
193194
194195 /**
195- * Sets the root URIs that this client can access. Roots define the base URIs
196- * for resources that the client can request from the server. For example,
197- * a root might be "file://workspace" for accessing workspace files.
198- *
196+ * Sets the root URIs that this client can access. Roots define the base URIs for
197+ * resources that the client can request from the server. For example, a root
198+ * might be "file://workspace" for accessing workspace files.
199199 * @param roots A list of root definitions. Must not be null.
200200 * @return This builder instance for method chaining
201201 * @throws IllegalArgumentException if roots is null
@@ -209,9 +209,8 @@ public Builder roots(List<Root> roots) {
209209 }
210210
211211 /**
212- * Sets the root URIs that this client can access, using a varargs parameter
213- * for convenience. This is an alternative to {@link #roots(List)}.
214- *
212+ * Sets the root URIs that this client can access, using a varargs parameter for
213+ * convenience. This is an alternative to {@link #roots(List)}.
215214 * @param roots An array of root definitions. Must not be null.
216215 * @return This builder instance for method chaining
217216 * @throws IllegalArgumentException if roots is null
@@ -226,12 +225,11 @@ public Builder roots(Root... roots) {
226225 }
227226
228227 /**
229- * Sets a custom sampling handler for processing message creation requests.
230- * The sampling handler can modify or validate messages before they are sent
231- * to the server, enabling custom processing logic.
232- *
228+ * Sets a custom sampling handler for processing message creation requests. The
229+ * sampling handler can modify or validate messages before they are sent to the
230+ * server, enabling custom processing logic.
233231 * @param samplingHandler A function that processes message requests and returns
234- * results. Must not be null.
232+ * results. Must not be null.
235233 * @return This builder instance for method chaining
236234 * @throws IllegalArgumentException if samplingHandler is null
237235 */
@@ -242,12 +240,11 @@ public Builder sampling(Function<CreateMessageRequest, CreateMessageResult> samp
242240 }
243241
244242 /**
245- * Adds a consumer to be notified when the available tools change. This allows
246- * the client to react to changes in the server's tool capabilities, such as
247- * tools being added or removed.
248- *
243+ * Adds a consumer to be notified when the available tools change. This allows the
244+ * client to react to changes in the server's tool capabilities, such as tools
245+ * being added or removed.
249246 * @param toolsChangeConsumer A consumer that receives the updated list of
250- * available tools. Must not be null.
247+ * available tools. Must not be null.
251248 * @return This builder instance for method chaining
252249 * @throws IllegalArgumentException if toolsChangeConsumer is null
253250 */
@@ -261,9 +258,8 @@ public Builder toolsChangeConsumer(Consumer<List<McpSchema.Tool>> toolsChangeCon
261258 * Adds a consumer to be notified when the available resources change. This allows
262259 * the client to react to changes in the server's resource availability, such as
263260 * files being added or removed.
264- *
265261 * @param resourcesChangeConsumer A consumer that receives the updated list of
266- * available resources. Must not be null.
262+ * available resources. Must not be null.
267263 * @return This builder instance for method chaining
268264 * @throws IllegalArgumentException if resourcesChangeConsumer is null
269265 */
@@ -275,11 +271,10 @@ public Builder resourcesChangeConsumer(Consumer<List<McpSchema.Resource>> resour
275271
276272 /**
277273 * Adds a consumer to be notified when the available prompts change. This allows
278- * the client to react to changes in the server's prompt templates, such as
279- * new templates being added or existing ones being modified.
280- *
274+ * the client to react to changes in the server's prompt templates, such as new
275+ * templates being added or existing ones being modified.
281276 * @param promptsChangeConsumer A consumer that receives the updated list of
282- * available prompts. Must not be null.
277+ * available prompts. Must not be null.
283278 * @return This builder instance for method chaining
284279 * @throws IllegalArgumentException if promptsChangeConsumer is null
285280 */
@@ -290,25 +285,22 @@ public Builder promptsChangeConsumer(Consumer<List<McpSchema.Prompt>> promptsCha
290285 }
291286
292287 /**
293- * Builds a synchronous MCP client that provides blocking operations.
294- * Synchronous clients wait for each operation to complete before returning,
295- * making them simpler to use but potentially less performant for
296- * concurrent operations.
297- *
298- * @return A new instance of {@link McpSyncClient} configured with this
299- * builder's settings
288+ * Builds a synchronous MCP client that provides blocking operations. Synchronous
289+ * clients wait for each operation to complete before returning, making them
290+ * simpler to use but potentially less performant for concurrent operations.
291+ * @return A new instance of {@link McpSyncClient} configured with this builder's
292+ * settings
300293 */
301294 public McpSyncClient sync () {
302295 return new McpSyncClient (async ());
303296 }
304297
305298 /**
306299 * Builds an asynchronous MCP client that provides non-blocking operations.
307- * Asynchronous clients return CompletableFuture objects immediately,
308- * allowing for concurrent operations and reactive programming patterns.
309- *
310- * @return A new instance of {@link McpAsyncClient} configured with this
311- * builder's settings
300+ * Asynchronous clients return CompletableFuture objects immediately, allowing for
301+ * concurrent operations and reactive programming patterns.
302+ * @return A new instance of {@link McpAsyncClient} configured with this builder's
303+ * settings
312304 */
313305 public McpAsyncClient async () {
314306 return new McpAsyncClient (transport , requestTimeout , clientInfo , capabilities , roots , toolsChangeConsumers ,
0 commit comments