55import com .fasterxml .jackson .core .type .TypeReference ;
66import com .fasterxml .jackson .databind .ObjectMapper ;
77import reactor .core .publisher .Mono ;
8- import spring .ai .experimental .mcp .spec .McpAsyncSession ;
9- import spring .ai .experimental .mcp .spec .McpAsyncTransport ;
8+ import spring .ai .experimental .mcp .spec .McpSession ;
9+ import spring .ai .experimental .mcp .spec .McpTransport ;
1010import spring .ai .experimental .mcp .spec .McpSchema ;
1111
12- public class McpAsyncClient extends McpAsyncSession {
12+ /**
13+ * @author Dariusz Jędrzejczyk
14+ * @author Christian Tzolov
15+ */
16+ public class McpAsyncClient extends McpSession {
1317
14- public McpAsyncClient (McpAsyncTransport transport ) {
18+ public McpAsyncClient (McpTransport transport ) {
1519 this (transport , Duration .ofSeconds (10 ), new ObjectMapper ());
1620 }
1721
18- public McpAsyncClient (McpAsyncTransport transport , Duration requestTimeout , ObjectMapper objectMapper ) {
22+ public McpAsyncClient (McpTransport transport , Duration requestTimeout , ObjectMapper objectMapper ) {
1923 super (requestTimeout , objectMapper , transport );
2024 }
2125
22-
2326 /**
24- * The initialization phase MUST be the first interaction between client and server.
27+ * The initialization phase MUST be the first interaction between client and
28+ * server.
2529 * During this phase, the client and server:
2630 * <ul>
2731 * <li>Establish protocol version compatibility</li>
2832 * <li>Exchange and negotiate capabilities</li>
2933 * <li>Share implementation details</li>
3034 * </ul>
3135 * <br/>
32- * The client MUST initiate this phase by sending an initialize request containing:
36+ * The client MUST initiate this phase by sending an initialize request
37+ * containing:
3338 * <ul>
3439 * <li>The protocol version the client supports</li>
3540 * <li>The client's capabilities</li>
@@ -38,14 +43,16 @@ public McpAsyncClient(McpAsyncTransport transport, Duration requestTimeout, Obje
3843 *
3944 * The server MUST respond with its own capabilities and information:
4045 * {@link McpSchema.ServerCapabilities}. <br/>
41- * After successful initialization, the client MUST send an initialized notification
46+ * After successful initialization, the client MUST send an initialized
47+ * notification
4248 * to indicate it is ready to begin normal operations.
4349 *
4450 * <br/>
4551 *
4652 * <a href=
4753 * "https://github.com/modelcontextprotocol/specification/blob/main/docs/specification/basic/lifecycle.md#initialization">Initialization
4854 * Spec</a>
55+ *
4956 * @return the initialize result.
5057 */
5158 public Mono <McpSchema .InitializeResult > initialize () {
@@ -54,8 +61,9 @@ public Mono<McpSchema.InitializeResult> initialize() {
5461 new McpSchema .ClientCapabilities (null , new McpSchema .ClientCapabilities .RootCapabilities (true ), null ),
5562 new McpSchema .Implementation ("mcp-java-client" , "0.0.1" )); // @formatter:on
5663
57- Mono <McpSchema .InitializeResult > result =
58- this .sendRequest ("initialize" , initializeRequest , new TypeReference <McpSchema .InitializeResult >() {});
64+ Mono <McpSchema .InitializeResult > result = this .sendRequest ("initialize" , initializeRequest ,
65+ new TypeReference <McpSchema .InitializeResult >() {
66+ });
5967
6068 return result .flatMap (initializeResult -> {
6169 if (!McpSchema .LATEST_PROTOCOL_VERSION .equals (initializeResult .protocolVersion ())) {
@@ -64,7 +72,7 @@ public Mono<McpSchema.InitializeResult> initialize() {
6472 + initializeResult .protocolVersion ()));
6573 } else {
6674 return this .sendNotification ("notifications/initialized" , null )
67- .thenReturn (initializeResult );
75+ .thenReturn (initializeResult );
6876 }
6977 });
7078 }
@@ -81,29 +89,34 @@ public Mono<Void> sendRootsListChanged() {
8189 */
8290 public Mono <Void > ping () {
8391 return this .sendRequest ("ping" , null ,
84- new TypeReference <Void >() {});
92+ new TypeReference <Void >() {
93+ });
8594 }
8695
8796 // --------------------------
8897 // Tools
8998 // --------------------------
9099 /**
91100 * Send a tools/call request.
101+ *
92102 * @param callToolRequest the call tool request.
93103 * @return the call tool result.
94104 */
95105 public Mono <McpSchema .CallToolResult > callTool (McpSchema .CallToolRequest callToolRequest ) {
96106 return this .sendRequest ("tools/call" , callToolRequest ,
97- new TypeReference <McpSchema .CallToolResult >() {});
107+ new TypeReference <McpSchema .CallToolResult >() {
108+ });
98109 }
99110
100111 /**
101112 * Send a tools/list request.
113+ *
102114 * @return the list of tools result.
103115 */
104116 public Mono <McpSchema .ListToolsResult > listTools (String cursor ) {
105117 return this .sendRequest ("tools/list" , new McpSchema .PaginatedRequest (cursor ),
106- new TypeReference <McpSchema .ListToolsResult >() {});
118+ new TypeReference <McpSchema .ListToolsResult >() {
119+ });
107120 }
108121
109122 // --------------------------
@@ -112,16 +125,19 @@ public Mono<McpSchema.ListToolsResult> listTools(String cursor) {
112125
113126 /**
114127 * Send a resources/list request.
128+ *
115129 * @param cursor the cursor
116130 * @return the list of resources result.
117131 */
118132 public Mono <McpSchema .ListResourcesResult > listResources (String cursor ) {
119133 return this .sendRequest ("resources/list" , new McpSchema .PaginatedRequest (cursor ),
120- new TypeReference <McpSchema .ListResourcesResult >() {});
134+ new TypeReference <McpSchema .ListResourcesResult >() {
135+ });
121136 }
122137
123138 /**
124139 * Send a resources/read request.
140+ *
125141 * @param resource the resource to read
126142 * @return the resource content.
127143 */
@@ -131,56 +147,69 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.Resource resour
131147
132148 /**
133149 * Send a resources/read request.
150+ *
134151 * @param readResourceRequest the read resource request.
135152 * @return the resource content.
136153 */
137154 public Mono <McpSchema .ReadResourceResult > readResource (McpSchema .ReadResourceRequest readResourceRequest ) {
138155 return this .sendRequest ("resources/read" , readResourceRequest ,
139- new TypeReference <McpSchema .ReadResourceResult >() {});
156+ new TypeReference <McpSchema .ReadResourceResult >() {
157+ });
140158 }
141159
142160 /**
143161 * Resource templates allow servers to expose parameterized resources using URI
144162 * templates. Arguments may be auto-completed through the completion API.
145163 *
146164 * Request a list of resource templates the server has.
165+ *
147166 * @param cursor the cursor
148167 * @return the list of resource templates result.
149168 */
150169 public Mono <McpSchema .ListResourceTemplatesResult > listResourceTemplates (String cursor ) {
151170 return this .sendRequest ("resources/templates/list" , new McpSchema .PaginatedRequest (cursor ),
152- new TypeReference <McpSchema .ListResourceTemplatesResult >() {});
171+ new TypeReference <McpSchema .ListResourceTemplatesResult >() {
172+ });
153173 }
154174
155175 /**
156- * List Changed Notification. When the list of available resources changes, servers
176+ * List Changed Notification. When the list of available resources changes,
177+ * servers
157178 * that declared the listChanged capability SHOULD send a notification:
158179 */
159180 public Mono <Void > sendResourcesListChanged () {
160181 return this .sendNotification ("notifications/resources/list_changed" );
161182 }
162183
163184 /**
164- * Subscriptions. The protocol supports optional subscriptions to resource changes.
165- * Clients can subscribe to specific resources and receive notifications when they
185+ * Subscriptions. The protocol supports optional subscriptions to resource
186+ * changes.
187+ * Clients can subscribe to specific resources and receive notifications when
188+ * they
166189 * change.
167190 *
168191 * Send a resources/subscribe request.
169- * @param subscribeRequest the subscribe request contains the uri of the resource to
170- * subscribe to.
192+ *
193+ * @param subscribeRequest the subscribe request contains the uri of the
194+ * resource to
195+ * subscribe to.
171196 */
172197 public Mono <Void > subscribeResource (McpSchema .SubscribeRequest subscribeRequest ) {
173198 return this .sendRequest ("resources/subscribe" , subscribeRequest ,
174- new TypeReference <Void >() {});
199+ new TypeReference <Void >() {
200+ });
175201 }
176202
177203 /**
178204 * Send a resources/unsubscribe request.
179- * @param unsubscribeRequest the unsubscribe request contains the uri of the resource
180- * to unsubscribe from.
205+ *
206+ * @param unsubscribeRequest the unsubscribe request contains the uri of the
207+ * resource
208+ * to unsubscribe from.
181209 */
182210 public Mono <Void > unsubscribeResource (McpSchema .UnsubscribeRequest unsubscribeRequest ) {
183211 return this .sendRequest ("resources/unsubscribe" , unsubscribeRequest ,
184- new TypeReference <Void >() {});
212+ new TypeReference <Void >() {
213+ });
185214 }
186215}
0 commit comments