99import spring .ai .experimental .mcp .spec .McpTransport ;
1010import spring .ai .experimental .mcp .spec .McpError ;
1111import spring .ai .experimental .mcp .spec .McpSchema ;
12+ import spring .ai .experimental .mcp .spec .McpSchema .GetPromptRequest ;
13+ import spring .ai .experimental .mcp .spec .McpSchema .GetPromptResult ;
14+ import spring .ai .experimental .mcp .spec .McpSchema .ListPromptsResult ;
15+ import spring .ai .experimental .mcp .spec .McpSchema .PaginatedRequest ;
1216
1317/**
1418 * @author Dariusz Jędrzejczyk
1519 * @author Christian Tzolov
1620 */
1721public class McpAsyncClient extends DefaultMcpSession {
1822
23+ private static TypeReference <Void > VOID_TYPE_REFERENCE = new TypeReference <Void >() {
24+ };
25+
1926 public McpAsyncClient (McpTransport transport ) {
2027 this (transport , Duration .ofSeconds (10 ), new ObjectMapper ());
2128 }
@@ -89,24 +96,25 @@ public Mono<Void> sendRootsListChanged() {
8996 * Send a synchronous ping request.
9097 */
9198 public Mono <Void > ping () {
92- return this .sendRequest ("ping" , null ,
93- new TypeReference <Void >() {
94- });
99+ return this .sendRequest ("ping" , null , VOID_TYPE_REFERENCE );
95100 }
96101
97102 // --------------------------
98103 // Tools
99104 // --------------------------
105+ private static TypeReference <McpSchema .CallToolResult > CALL_TOOL_RESULT_TYPE_REF = new TypeReference <McpSchema .CallToolResult >() {
106+ };
107+ private static TypeReference <McpSchema .ListToolsResult > LIST_TOOLS_RESULT_TYPE_REF = new TypeReference <McpSchema .ListToolsResult >() {
108+ };
109+
100110 /**
101111 * Send a tools/call request.
102112 *
103113 * @param callToolRequest the call tool request.
104114 * @return the call tool result.
105115 */
106116 public Mono <McpSchema .CallToolResult > callTool (McpSchema .CallToolRequest callToolRequest ) {
107- return this .sendRequest ("tools/call" , callToolRequest ,
108- new TypeReference <McpSchema .CallToolResult >() {
109- });
117+ return this .sendRequest ("tools/call" , callToolRequest , CALL_TOOL_RESULT_TYPE_REF );
110118 }
111119
112120 /**
@@ -115,15 +123,20 @@ public Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToo
115123 * @return the list of tools result.
116124 */
117125 public Mono <McpSchema .ListToolsResult > listTools (String cursor ) {
118- return this .sendRequest ("tools/list" , new McpSchema .PaginatedRequest (cursor ),
119- new TypeReference <McpSchema .ListToolsResult >() {
120- });
126+ return this .sendRequest ("tools/list" , new McpSchema .PaginatedRequest (cursor ), LIST_TOOLS_RESULT_TYPE_REF );
121127 }
122128
123129 // --------------------------
124130 // Resources
125131 // --------------------------
126132
133+ private static TypeReference <McpSchema .ListResourcesResult > LIST_RESOURCES_RESULT_TYPE_REF = new TypeReference <McpSchema .ListResourcesResult >() {
134+ };
135+ private static TypeReference <McpSchema .ReadResourceResult > READ_RESOURCE_RESULT_TYPE_REF = new TypeReference <McpSchema .ReadResourceResult >() {
136+ };
137+ private static TypeReference <McpSchema .ListResourceTemplatesResult > LIST_RESOURCE_TEMPLATES_RESULT_TYPE_REF = new TypeReference <McpSchema .ListResourceTemplatesResult >() {
138+ };
139+
127140 /**
128141 * Send a resources/list request.
129142 *
@@ -132,8 +145,7 @@ public Mono<McpSchema.ListToolsResult> listTools(String cursor) {
132145 */
133146 public Mono <McpSchema .ListResourcesResult > listResources (String cursor ) {
134147 return this .sendRequest ("resources/list" , new McpSchema .PaginatedRequest (cursor ),
135- new TypeReference <McpSchema .ListResourcesResult >() {
136- });
148+ LIST_RESOURCES_RESULT_TYPE_REF );
137149 }
138150
139151 /**
@@ -153,9 +165,7 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.Resource resour
153165 * @return the resource content.
154166 */
155167 public Mono <McpSchema .ReadResourceResult > readResource (McpSchema .ReadResourceRequest readResourceRequest ) {
156- return this .sendRequest ("resources/read" , readResourceRequest ,
157- new TypeReference <McpSchema .ReadResourceResult >() {
158- });
168+ return this .sendRequest ("resources/read" , readResourceRequest , READ_RESOURCE_RESULT_TYPE_REF );
159169 }
160170
161171 /**
@@ -169,14 +179,12 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceReq
169179 */
170180 public Mono <McpSchema .ListResourceTemplatesResult > listResourceTemplates (String cursor ) {
171181 return this .sendRequest ("resources/templates/list" , new McpSchema .PaginatedRequest (cursor ),
172- new TypeReference <McpSchema .ListResourceTemplatesResult >() {
173- });
182+ LIST_RESOURCE_TEMPLATES_RESULT_TYPE_REF );
174183 }
175184
176185 /**
177186 * List Changed Notification. When the list of available resources changes,
178- * servers
179- * that declared the listChanged capability SHOULD send a notification:
187+ * servers that declared the listChanged capability SHOULD send a notification:
180188 */
181189 public Mono <Void > sendResourcesListChanged () {
182190 return this .sendNotification ("notifications/resources/list_changed" );
@@ -186,31 +194,53 @@ public Mono<Void> sendResourcesListChanged() {
186194 * Subscriptions. The protocol supports optional subscriptions to resource
187195 * changes.
188196 * Clients can subscribe to specific resources and receive notifications when
189- * they
190- * change.
197+ * they change.
191198 *
192199 * Send a resources/subscribe request.
193200 *
194201 * @param subscribeRequest the subscribe request contains the uri of the
195- * resource to
196- * subscribe to.
202+ * resource to subscribe to.
197203 */
198204 public Mono <Void > subscribeResource (McpSchema .SubscribeRequest subscribeRequest ) {
199- return this .sendRequest ("resources/subscribe" , subscribeRequest ,
200- new TypeReference <Void >() {
201- });
205+ return this .sendRequest ("resources/subscribe" , subscribeRequest , VOID_TYPE_REFERENCE );
202206 }
203207
204208 /**
205209 * Send a resources/unsubscribe request.
206210 *
207211 * @param unsubscribeRequest the unsubscribe request contains the uri of the
208- * resource
209- * to unsubscribe from.
212+ * resource to unsubscribe from.
210213 */
211214 public Mono <Void > unsubscribeResource (McpSchema .UnsubscribeRequest unsubscribeRequest ) {
212- return this .sendRequest ("resources/unsubscribe" , unsubscribeRequest ,
213- new TypeReference <Void >() {
214- });
215+ return this .sendRequest ("resources/unsubscribe" , unsubscribeRequest , VOID_TYPE_REFERENCE );
215216 }
217+
218+ // --------------------------
219+ // Prompts
220+ // --------------------------
221+ private static TypeReference <McpSchema .ListPromptsResult > LIST_PROMPTS_RESULT_TYPE_REF = new TypeReference <McpSchema .ListPromptsResult >() {
222+ };
223+ private static TypeReference <McpSchema .GetPromptResult > GET_PROMPT_RESULT_TYPE_REF = new TypeReference <McpSchema .GetPromptResult >() {
224+ };
225+
226+ public Mono <ListPromptsResult > listPrompts (String cursor ) {
227+ return this
228+ .sendRequest ("prompts/list" , new PaginatedRequest (cursor ), LIST_PROMPTS_RESULT_TYPE_REF );
229+ }
230+
231+ public Mono <GetPromptResult > getPrompt (GetPromptRequest getPromptRequest ) {
232+ return this .sendRequest ("prompts/get" , getPromptRequest , GET_PROMPT_RESULT_TYPE_REF );
233+ }
234+
235+ /**
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
240+ * any previous subscription from the client.
241+ */
242+ public Mono <Void > promptListChangedNotification () {
243+ return this .sendNotification ("notifications/prompts/list_changed" );
244+ }
245+
216246}
0 commit comments