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