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+ */
116package org .springframework .ai .mcp .client ;
217
318import java .time .Duration ;
419
520import com .fasterxml .jackson .core .type .TypeReference ;
621import com .fasterxml .jackson .databind .ObjectMapper ;
722import reactor .core .publisher .Mono ;
23+
824import org .springframework .ai .mcp .spec .DefaultMcpSession ;
9- import org .springframework .ai .mcp .spec .McpTransport ;
1025import org .springframework .ai .mcp .spec .McpError ;
1126import org .springframework .ai .mcp .spec .McpSchema ;
1227import org .springframework .ai .mcp .spec .McpSchema .GetPromptRequest ;
1328import org .springframework .ai .mcp .spec .McpSchema .GetPromptResult ;
1429import org .springframework .ai .mcp .spec .McpSchema .ListPromptsResult ;
1530import org .springframework .ai .mcp .spec .McpSchema .PaginatedRequest ;
31+ import org .springframework .ai .mcp .spec .McpTransport ;
1632
1733/**
1834 * @author Dariusz Jędrzejczyk
@@ -32,17 +48,15 @@ public McpAsyncClient(McpTransport transport, Duration requestTimeout, ObjectMap
3248 }
3349
3450 /**
35- * The initialization phase MUST be the first interaction between client and
36- * server.
51+ * The initialization phase MUST be the first interaction between client and server.
3752 * During this phase, the client and server:
3853 * <ul>
3954 * <li>Establish protocol version compatibility</li>
4055 * <li>Exchange and negotiate capabilities</li>
4156 * <li>Share implementation details</li>
4257 * </ul>
4358 * <br/>
44- * The client MUST initiate this phase by sending an initialize request
45- * containing:
59+ * The client MUST initiate this phase by sending an initialize request containing:
4660 * <ul>
4761 * <li>The protocol version the client supports</li>
4862 * <li>The client's capabilities</li>
@@ -51,16 +65,14 @@ public McpAsyncClient(McpTransport transport, Duration requestTimeout, ObjectMap
5165 *
5266 * The server MUST respond with its own capabilities and information:
5367 * {@link McpSchema.ServerCapabilities}. <br/>
54- * After successful initialization, the client MUST send an initialized
55- * notification
68+ * After successful initialization, the client MUST send an initialized notification
5669 * to indicate it is ready to begin normal operations.
5770 *
5871 * <br/>
5972 *
6073 * <a href=
6174 * "https://github.com/modelcontextprotocol/specification/blob/main/docs/specification/basic/lifecycle.md#initialization">Initialization
6275 * Spec</a>
63- *
6476 * @return the initialize result.
6577 */
6678 public Mono <McpSchema .InitializeResult > initialize () {
@@ -75,12 +87,11 @@ public Mono<McpSchema.InitializeResult> initialize() {
7587
7688 return result .flatMap (initializeResult -> {
7789 if (!McpSchema .LATEST_PROTOCOL_VERSION .equals (initializeResult .protocolVersion ())) {
78- return Mono .error (
79- new McpError ("Unsupported protocol version from the server: "
80- + initializeResult .protocolVersion ()));
81- } else {
82- return this .sendNotification ("notifications/initialized" , null )
83- .thenReturn (initializeResult );
90+ return Mono .error (new McpError (
91+ "Unsupported protocol version from the server: " + initializeResult .protocolVersion ()));
92+ }
93+ else {
94+ return this .sendNotification ("notifications/initialized" , null ).thenReturn (initializeResult );
8495 }
8596 });
8697 }
@@ -104,12 +115,12 @@ public Mono<Void> ping() {
104115 // --------------------------
105116 private static TypeReference <McpSchema .CallToolResult > CALL_TOOL_RESULT_TYPE_REF = new TypeReference <McpSchema .CallToolResult >() {
106117 };
118+
107119 private static TypeReference <McpSchema .ListToolsResult > LIST_TOOLS_RESULT_TYPE_REF = new TypeReference <McpSchema .ListToolsResult >() {
108120 };
109121
110122 /**
111123 * Send a tools/call request.
112- *
113124 * @param callToolRequest the call tool request.
114125 * @return the call tool result.
115126 */
@@ -119,7 +130,6 @@ public Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToo
119130
120131 /**
121132 * Send a tools/list request.
122- *
123133 * @return the list of tools result.
124134 */
125135 public Mono <McpSchema .ListToolsResult > listTools (String cursor ) {
@@ -132,14 +142,15 @@ public Mono<McpSchema.ListToolsResult> listTools(String cursor) {
132142
133143 private static TypeReference <McpSchema .ListResourcesResult > LIST_RESOURCES_RESULT_TYPE_REF = new TypeReference <McpSchema .ListResourcesResult >() {
134144 };
145+
135146 private static TypeReference <McpSchema .ReadResourceResult > READ_RESOURCE_RESULT_TYPE_REF = new TypeReference <McpSchema .ReadResourceResult >() {
136147 };
148+
137149 private static TypeReference <McpSchema .ListResourceTemplatesResult > LIST_RESOURCE_TEMPLATES_RESULT_TYPE_REF = new TypeReference <McpSchema .ListResourceTemplatesResult >() {
138150 };
139151
140152 /**
141153 * Send a resources/list request.
142- *
143154 * @param cursor the cursor
144155 * @return the list of resources result.
145156 */
@@ -150,7 +161,6 @@ public Mono<McpSchema.ListResourcesResult> listResources(String cursor) {
150161
151162 /**
152163 * Send a resources/read request.
153- *
154164 * @param resource the resource to read
155165 * @return the resource content.
156166 */
@@ -160,7 +170,6 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.Resource resour
160170
161171 /**
162172 * Send a resources/read request.
163- *
164173 * @param readResourceRequest the read resource request.
165174 * @return the resource content.
166175 */
@@ -173,7 +182,6 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceReq
173182 * templates. Arguments may be auto-completed through the completion API.
174183 *
175184 * Request a list of resource templates the server has.
176- *
177185 * @param cursor the cursor
178186 * @return the list of resource templates result.
179187 */
@@ -183,33 +191,30 @@ public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String
183191 }
184192
185193 /**
186- * List Changed Notification. When the list of available resources changes,
187- * servers that declared the listChanged capability SHOULD send a notification:
194+ * List Changed Notification. When the list of available resources changes, servers
195+ * that declared the listChanged capability SHOULD send a notification:
188196 */
189197 public Mono <Void > sendResourcesListChanged () {
190198 return this .sendNotification ("notifications/resources/list_changed" );
191199 }
192200
193201 /**
194- * Subscriptions. The protocol supports optional subscriptions to resource
195- * changes.
196- * Clients can subscribe to specific resources and receive notifications when
197- * they change.
202+ * Subscriptions. The protocol supports optional subscriptions to resource changes.
203+ * Clients can subscribe to specific resources and receive notifications when they
204+ * change.
198205 *
199206 * Send a resources/subscribe request.
200- *
201- * @param subscribeRequest the subscribe request contains the uri of the
202- * resource to subscribe to.
207+ * @param subscribeRequest the subscribe request contains the uri of the resource to
208+ * subscribe to.
203209 */
204210 public Mono <Void > subscribeResource (McpSchema .SubscribeRequest subscribeRequest ) {
205211 return this .sendRequest ("resources/subscribe" , subscribeRequest , VOID_TYPE_REFERENCE );
206212 }
207213
208214 /**
209215 * Send a resources/unsubscribe request.
210- *
211- * @param unsubscribeRequest the unsubscribe request contains the uri of the
212- * resource to unsubscribe from.
216+ * @param unsubscribeRequest the unsubscribe request contains the uri of the resource
217+ * to unsubscribe from.
213218 */
214219 public Mono <Void > unsubscribeResource (McpSchema .UnsubscribeRequest unsubscribeRequest ) {
215220 return this .sendRequest ("resources/unsubscribe" , unsubscribeRequest , VOID_TYPE_REFERENCE );
@@ -220,23 +225,21 @@ public Mono<Void> unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRe
220225 // --------------------------
221226 private static TypeReference <McpSchema .ListPromptsResult > LIST_PROMPTS_RESULT_TYPE_REF = new TypeReference <McpSchema .ListPromptsResult >() {
222227 };
228+
223229 private static TypeReference <McpSchema .GetPromptResult > GET_PROMPT_RESULT_TYPE_REF = new TypeReference <McpSchema .GetPromptResult >() {
224230 };
225231
226232 public Mono <ListPromptsResult > listPrompts (String cursor ) {
227- return this
228- .sendRequest ("prompts/list" , new PaginatedRequest (cursor ), LIST_PROMPTS_RESULT_TYPE_REF );
233+ return this .sendRequest ("prompts/list" , new PaginatedRequest (cursor ), LIST_PROMPTS_RESULT_TYPE_REF );
229234 }
230235
231236 public Mono <GetPromptResult > getPrompt (GetPromptRequest getPromptRequest ) {
232237 return this .sendRequest ("prompts/get" , getPromptRequest , GET_PROMPT_RESULT_TYPE_REF );
233238 }
234239
235240 /**
236- * (Server) An optional notification from the server to the client, informing it
237- * that
238- * the list of prompts it offers has changed. This may be issued by servers
239- * without
241+ * (Server) An optional notification from the server to the client, informing it that
242+ * the list of prompts it offers has changed. This may be issued by servers without
240243 * any previous subscription from the client.
241244 */
242245 public Mono <Void > promptListChangedNotification () {
0 commit comments