|
1 | 1 | /* |
2 | | -* Copyright 2024 - 2024 the original author or authors. |
3 | | -* |
4 | | -* Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | -* you may not use this file except in compliance with the License. |
6 | | -* You may obtain a copy of the License at |
7 | | -* |
8 | | -* https://www.apache.org/licenses/LICENSE-2.0 |
9 | | -* |
10 | | -* Unless required by applicable law or agreed to in writing, software |
11 | | -* distributed under the License is distributed on an "AS IS" BASIS, |
12 | | -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | -* See the License for the specific language governing permissions and |
14 | | -* limitations under the License. |
15 | | -*/ |
| 2 | + * Copyright 2024 - 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
16 | 16 | package spring.ai.experimental.mcp.client; |
17 | 17 |
|
18 | 18 | import java.time.Duration; |
|
21 | 21 | import spring.ai.experimental.mcp.spec.McpTransport; |
22 | 22 |
|
23 | 23 | /** |
24 | | - * The MCP client is the main entry point for interacting with the Model Context Protocol |
25 | | - * (MCP) server. |
| 24 | + * Factory class providing static methods for creating Model Context Protocol (MCP) clients. |
| 25 | + * This class serves as the main entry point for establishing connections with MCP servers, |
| 26 | + * offering both synchronous and asynchronous client implementations. |
| 27 | + * |
| 28 | + * <p>The class provides factory methods to create either: |
| 29 | + * <ul> |
| 30 | + * <li>{@link McpAsyncClient} for non-blocking operations |
| 31 | + * <li>{@link McpSyncClient} for blocking operations |
| 32 | + * </ul> |
| 33 | + * |
| 34 | + * <p>Each client type can be instantiated with default settings or with custom configuration |
| 35 | + * including request timeout and JSON object mapping. |
| 36 | + * |
| 37 | + * <p>Future implementations will introduce a builder pattern for more flexible client configuration. |
26 | 38 | * |
27 | 39 | * @author Christian Tzolov |
28 | 40 | * @author Dariusz Jędrzejczyk |
29 | 41 | */ |
30 | 42 | public class McpClient { |
31 | 43 |
|
32 | | - private McpClient() { |
33 | | - } |
34 | | - |
35 | | - // TODO: introduce a builder like: |
36 | | - // McpClient.using(transport) |
37 | | - // .withRequestTimeout(Duration.ofSeconds(5)) |
38 | | - // .withObjectMapper(objectMapper); <- or even a more sophisticated |
39 | | - // JSONtoPOJOCodec type |
40 | | - // .sync(); |
41 | | - |
42 | | - public static McpAsyncClient async(McpTransport transport) { |
43 | | - return new McpAsyncClient(transport); |
44 | | - } |
| 44 | + /** |
| 45 | + * Private constructor to prevent instantiation as this is a utility class |
| 46 | + * containing only static factory methods. |
| 47 | + */ |
| 48 | + private McpClient() { |
| 49 | + } |
45 | 50 |
|
46 | | - public static McpAsyncClient async(McpTransport transport, Duration requestTimeout, |
47 | | - ObjectMapper objectMapper) { |
48 | | - return new McpAsyncClient(transport, requestTimeout, objectMapper); |
49 | | - } |
| 51 | + // TODO: introduce a builder like: |
| 52 | + // McpClient.using(transport) |
| 53 | + // .withRequestTimeout(Duration.ofSeconds(5)) |
| 54 | + // .withObjectMapper(objectMapper); <- or even a more sophisticated |
| 55 | + // JSONtoPOJOCodec type |
| 56 | + // .sync(); |
50 | 57 |
|
51 | | - public static McpSyncClient sync(McpTransport transport) { |
52 | | - return new McpSyncClient(async(transport)); |
53 | | - } |
| 58 | + /** |
| 59 | + * Creates an asynchronous MCP client with default configuration. |
| 60 | + * |
| 61 | + * @param transport The transport layer implementation for MCP communication |
| 62 | + * @return A new instance of {@link McpAsyncClient} |
| 63 | + */ |
| 64 | + public static McpAsyncClient async(McpTransport transport) { |
| 65 | + return new McpAsyncClient(transport); |
| 66 | + } |
54 | 67 |
|
55 | | - public static McpSyncClient sync(McpTransport transport, Duration requestTimeout, |
56 | | - ObjectMapper objectMapper) { |
57 | | - return new McpSyncClient(async(transport, requestTimeout, objectMapper)); |
58 | | - } |
| 68 | + /** |
| 69 | + * Creates an asynchronous MCP client with custom configuration. |
| 70 | + * |
| 71 | + * @param transport The transport layer implementation for MCP communication |
| 72 | + * @param requestTimeout The duration to wait before timing out requests |
| 73 | + * @param objectMapper The Jackson object mapper for JSON serialization/deserialization |
| 74 | + * @return A new instance of {@link McpAsyncClient} |
| 75 | + */ |
| 76 | + public static McpAsyncClient async(McpTransport transport, Duration requestTimeout, |
| 77 | + ObjectMapper objectMapper) { |
| 78 | + return new McpAsyncClient(transport, requestTimeout, objectMapper); |
| 79 | + } |
59 | 80 |
|
| 81 | + /** |
| 82 | + * Creates a synchronous MCP client with default configuration. |
| 83 | + * This method wraps an asynchronous client to provide synchronous operations. |
| 84 | + * |
| 85 | + * @param transport The transport layer implementation for MCP communication |
| 86 | + * @return A new instance of {@link McpSyncClient} |
| 87 | + */ |
| 88 | + public static McpSyncClient sync(McpTransport transport) { |
| 89 | + return new McpSyncClient(async(transport)); |
| 90 | + } |
60 | 91 |
|
| 92 | + /** |
| 93 | + * Creates a synchronous MCP client with custom configuration. |
| 94 | + * This method wraps an asynchronous client to provide synchronous operations. |
| 95 | + * |
| 96 | + * @param transport The transport layer implementation for MCP communication |
| 97 | + * @param requestTimeout The duration to wait before timing out requests |
| 98 | + * @param objectMapper The Jackson object mapper for JSON serialization/deserialization |
| 99 | + * @return A new instance of {@link McpSyncClient} |
| 100 | + */ |
| 101 | + public static McpSyncClient sync(McpTransport transport, Duration requestTimeout, |
| 102 | + ObjectMapper objectMapper) { |
| 103 | + return new McpSyncClient(async(transport, requestTimeout, objectMapper)); |
| 104 | + } |
61 | 105 | } |
0 commit comments