4
4
5
5
package io .modelcontextprotocol .client ;
6
6
7
- import java .time .Duration ;
8
-
9
- import org .slf4j .Logger ;
10
- import org .slf4j .LoggerFactory ;
11
-
7
+ import io .modelcontextprotocol .server .McpTransportContext ;
12
8
import io .modelcontextprotocol .spec .McpSchema ;
13
9
import io .modelcontextprotocol .spec .McpSchema .ClientCapabilities ;
14
10
import io .modelcontextprotocol .spec .McpSchema .GetPromptRequest ;
15
11
import io .modelcontextprotocol .spec .McpSchema .GetPromptResult ;
16
12
import io .modelcontextprotocol .spec .McpSchema .ListPromptsResult ;
13
+ import io .modelcontextprotocol .spec .McpTransport ;
17
14
import io .modelcontextprotocol .util .Assert ;
15
+ import java .time .Duration ;
16
+ import java .util .function .Supplier ;
17
+ import org .slf4j .Logger ;
18
+ import org .slf4j .LoggerFactory ;
18
19
19
20
/**
20
21
* A synchronous client implementation for the Model Context Protocol (MCP) that wraps an
@@ -63,14 +64,18 @@ public class McpSyncClient implements AutoCloseable {
63
64
64
65
private final McpAsyncClient delegate ;
65
66
67
+ private final Supplier <McpTransportContext > contextProvider ;
68
+
66
69
/**
67
70
* Create a new McpSyncClient with the given delegate.
68
71
* @param delegate the asynchronous kernel on top of which this synchronous client
69
72
* provides a blocking API.
70
73
*/
71
- McpSyncClient (McpAsyncClient delegate ) {
74
+ McpSyncClient (McpAsyncClient delegate , Supplier < McpTransportContext > contextProvider ) {
72
75
Assert .notNull (delegate , "The delegate can not be null" );
76
+ Assert .notNull (contextProvider , "The contextProvider can not be null" );
73
77
this .delegate = delegate ;
78
+ this .contextProvider = contextProvider ;
74
79
}
75
80
76
81
/**
@@ -177,36 +182,43 @@ public boolean closeGracefully() {
177
182
public McpSchema .InitializeResult initialize () {
178
183
// TODO: block takes no argument here as we assume the async client is
179
184
// configured with a requestTimeout at all times
180
- return this .delegate .initialize ().block ();
185
+ var context = this .contextProvider .get ();
186
+ return this .delegate .initialize ().contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
181
187
}
182
188
183
189
/**
184
190
* Send a roots/list_changed notification.
185
191
*/
186
192
public void rootsListChangedNotification () {
187
- this .delegate .rootsListChangedNotification ().block ();
193
+ var context = this .contextProvider .get ();
194
+ this .delegate .rootsListChangedNotification ()
195
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
196
+ .block ();
188
197
}
189
198
190
199
/**
191
200
* Add a roots dynamically.
192
201
*/
193
202
public void addRoot (McpSchema .Root root ) {
194
- this .delegate .addRoot (root ).block ();
203
+ var context = this .contextProvider .get ();
204
+ this .delegate .addRoot (root ).contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
195
205
}
196
206
197
207
/**
198
208
* Remove a root dynamically.
199
209
*/
200
210
public void removeRoot (String rootUri ) {
201
- this .delegate .removeRoot (rootUri ).block ();
211
+ var context = this .contextProvider .get ();
212
+ this .delegate .removeRoot (rootUri ).contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
202
213
}
203
214
204
215
/**
205
216
* Send a synchronous ping request.
206
217
* @return
207
218
*/
208
219
public Object ping () {
209
- return this .delegate .ping ().block ();
220
+ var context = this .contextProvider .get ();
221
+ return this .delegate .ping ().contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
210
222
}
211
223
212
224
// --------------------------
@@ -224,7 +236,11 @@ public Object ping() {
224
236
* Boolean indicating if the execution failed (true) or succeeded (false/absent)
225
237
*/
226
238
public McpSchema .CallToolResult callTool (McpSchema .CallToolRequest callToolRequest ) {
227
- return this .delegate .callTool (callToolRequest ).block ();
239
+ var context = this .contextProvider .get ();
240
+ return this .delegate .callTool (callToolRequest )
241
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
242
+ .block ();
243
+
228
244
}
229
245
230
246
/**
@@ -234,7 +250,8 @@ public McpSchema.CallToolResult callTool(McpSchema.CallToolRequest callToolReque
234
250
* pagination if more tools are available
235
251
*/
236
252
public McpSchema .ListToolsResult listTools () {
237
- return this .delegate .listTools ().block ();
253
+ var context = this .contextProvider .get ();
254
+ return this .delegate .listTools ().contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
238
255
}
239
256
240
257
/**
@@ -245,7 +262,9 @@ public McpSchema.ListToolsResult listTools() {
245
262
* pagination if more tools are available
246
263
*/
247
264
public McpSchema .ListToolsResult listTools (String cursor ) {
248
- return this .delegate .listTools (cursor ).block ();
265
+ var context = this .contextProvider .get ();
266
+ return this .delegate .listTools (cursor ).contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
267
+
249
268
}
250
269
251
270
// --------------------------
@@ -257,7 +276,9 @@ public McpSchema.ListToolsResult listTools(String cursor) {
257
276
* @return The list of all resources result
258
277
*/
259
278
public McpSchema .ListResourcesResult listResources () {
260
- return this .delegate .listResources ().block ();
279
+ var context = this .contextProvider .get ();
280
+ return this .delegate .listResources ().contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
281
+
261
282
}
262
283
263
284
/**
@@ -266,7 +287,11 @@ public McpSchema.ListResourcesResult listResources() {
266
287
* @return The list of resources result
267
288
*/
268
289
public McpSchema .ListResourcesResult listResources (String cursor ) {
269
- return this .delegate .listResources (cursor ).block ();
290
+ var context = this .contextProvider .get ();
291
+ return this .delegate .listResources (cursor )
292
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
293
+ .block ();
294
+
270
295
}
271
296
272
297
/**
@@ -275,7 +300,11 @@ public McpSchema.ListResourcesResult listResources(String cursor) {
275
300
* @return the resource content.
276
301
*/
277
302
public McpSchema .ReadResourceResult readResource (McpSchema .Resource resource ) {
278
- return this .delegate .readResource (resource ).block ();
303
+ var context = this .contextProvider .get ();
304
+ return this .delegate .readResource (resource )
305
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
306
+ .block ();
307
+
279
308
}
280
309
281
310
/**
@@ -284,15 +313,23 @@ public McpSchema.ReadResourceResult readResource(McpSchema.Resource resource) {
284
313
* @return the resource content.
285
314
*/
286
315
public McpSchema .ReadResourceResult readResource (McpSchema .ReadResourceRequest readResourceRequest ) {
287
- return this .delegate .readResource (readResourceRequest ).block ();
316
+ var context = this .contextProvider .get ();
317
+ return this .delegate .readResource (readResourceRequest )
318
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
319
+ .block ();
320
+
288
321
}
289
322
290
323
/**
291
324
* Retrieves the list of all resource templates provided by the server.
292
325
* @return The list of all resource templates result.
293
326
*/
294
327
public McpSchema .ListResourceTemplatesResult listResourceTemplates () {
295
- return this .delegate .listResourceTemplates ().block ();
328
+ var context = this .contextProvider .get ();
329
+ return this .delegate .listResourceTemplates ()
330
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
331
+ .block ();
332
+
296
333
}
297
334
298
335
/**
@@ -304,7 +341,11 @@ public McpSchema.ListResourceTemplatesResult listResourceTemplates() {
304
341
* @return The list of resource templates result.
305
342
*/
306
343
public McpSchema .ListResourceTemplatesResult listResourceTemplates (String cursor ) {
307
- return this .delegate .listResourceTemplates (cursor ).block ();
344
+ var context = this .contextProvider .get ();
345
+ return this .delegate .listResourceTemplates (cursor )
346
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
347
+ .block ();
348
+
308
349
}
309
350
310
351
/**
@@ -317,7 +358,11 @@ public McpSchema.ListResourceTemplatesResult listResourceTemplates(String cursor
317
358
* subscribe to.
318
359
*/
319
360
public void subscribeResource (McpSchema .SubscribeRequest subscribeRequest ) {
320
- this .delegate .subscribeResource (subscribeRequest ).block ();
361
+ var context = this .contextProvider .get ();
362
+ this .delegate .subscribeResource (subscribeRequest )
363
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
364
+ .block ();
365
+
321
366
}
322
367
323
368
/**
@@ -326,7 +371,11 @@ public void subscribeResource(McpSchema.SubscribeRequest subscribeRequest) {
326
371
* to unsubscribe from.
327
372
*/
328
373
public void unsubscribeResource (McpSchema .UnsubscribeRequest unsubscribeRequest ) {
329
- this .delegate .unsubscribeResource (unsubscribeRequest ).block ();
374
+ var context = this .contextProvider .get ();
375
+ this .delegate .unsubscribeResource (unsubscribeRequest )
376
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
377
+ .block ();
378
+
330
379
}
331
380
332
381
// --------------------------
@@ -338,7 +387,8 @@ public void unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest)
338
387
* @return The list of all prompts result.
339
388
*/
340
389
public ListPromptsResult listPrompts () {
341
- return this .delegate .listPrompts ().block ();
390
+ var context = this .contextProvider .get ();
391
+ return this .delegate .listPrompts ().contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
342
392
}
343
393
344
394
/**
@@ -347,19 +397,29 @@ public ListPromptsResult listPrompts() {
347
397
* @return The list of prompts result.
348
398
*/
349
399
public ListPromptsResult listPrompts (String cursor ) {
350
- return this .delegate .listPrompts (cursor ).block ();
400
+ var context = this .contextProvider .get ();
401
+ return this .delegate .listPrompts (cursor ).contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context )).block ();
402
+
351
403
}
352
404
353
405
public GetPromptResult getPrompt (GetPromptRequest getPromptRequest ) {
354
- return this .delegate .getPrompt (getPromptRequest ).block ();
406
+ var context = this .contextProvider .get ();
407
+ return this .delegate .getPrompt (getPromptRequest )
408
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
409
+ .block ();
410
+
355
411
}
356
412
357
413
/**
358
414
* Client can set the minimum logging level it wants to receive from the server.
359
415
* @param loggingLevel the min logging level
360
416
*/
361
417
public void setLoggingLevel (McpSchema .LoggingLevel loggingLevel ) {
362
- this .delegate .setLoggingLevel (loggingLevel ).block ();
418
+ var context = this .contextProvider .get ();
419
+ this .delegate .setLoggingLevel (loggingLevel )
420
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
421
+ .block ();
422
+
363
423
}
364
424
365
425
/**
@@ -369,7 +429,11 @@ public void setLoggingLevel(McpSchema.LoggingLevel loggingLevel) {
369
429
* @return the completion result containing suggested values.
370
430
*/
371
431
public McpSchema .CompleteResult completeCompletion (McpSchema .CompleteRequest completeRequest ) {
372
- return this .delegate .completeCompletion (completeRequest ).block ();
432
+ var context = this .contextProvider .get ();
433
+ return this .delegate .completeCompletion (completeRequest )
434
+ .contextWrite (ctx -> ctx .put (McpTransportContext .KEY , context ))
435
+ .block ();
436
+
373
437
}
374
438
375
439
}
0 commit comments