@@ -339,9 +339,51 @@ static AsyncPromptSpecification fromSync(SyncPromptSpecification prompt) {
339
339
}
340
340
}
341
341
342
+ /**
343
+ * Specification of a completion handler function with asynchronous execution support.
344
+ * Completions generate AI model outputs based on prompt or resource references and
345
+ * user-provided arguments. This abstraction enables:
346
+ * <ul>
347
+ * <li>Customizable response generation logic
348
+ * <li>Parameter-driven template expansion
349
+ * <li>Dynamic interaction with connected clients
350
+ * </ul>
351
+ *
352
+ * <p>
353
+ * Example completion specification: <pre>{@code
354
+ * new McpServerFeatures.AsyncCompletionSpecification(
355
+ * (exchange, request) -> {
356
+ * String name = request.argument().name(); // e.g., "language"
357
+ * String language = request.argument().value(); // e.g., "py"
358
+ * List<String> suggestions = List.of(
359
+ * "python",
360
+ * "pytorch",
361
+ * "pyside"
362
+ * );
363
+ * CompleteResult.CompleteCompletion completion = new CompleteResult.CompleteCompletion(
364
+ * suggestions, suggestions.size(), false
365
+ * );
366
+ * return Mono.just(new CompleteResult(completion));
367
+ * }
368
+ * )
369
+ * }</pre>
370
+ *
371
+ * @param completionHandler The asynchronous function that processes completion
372
+ * requests and returns results. The first argument is an
373
+ * {@link McpAsyncServerExchange} used to interact with the client. The second
374
+ * argument is a {@link io.modelcontextprotocol.spec.McpSchema.CompleteRequest}.
375
+ */
342
376
public record AsyncCompletionSpecification (
343
377
BiFunction <McpAsyncServerExchange , McpSchema .CompleteRequest , Mono <McpSchema .CompleteResult >> completionHandler ) {
344
378
379
+ /**
380
+ * Converts a synchronous {@link SyncCompletionSpecification} into an
381
+ * {@link AsyncCompletionSpecification} by wrapping the handler in a bounded
382
+ * elastic scheduler for safe non-blocking execution.
383
+ * @param completion the synchronous completion specification
384
+ * @return an asynchronous wrapper of the provided sync specification, or
385
+ * {@code null} if input is null
386
+ */
345
387
static AsyncCompletionSpecification fromSync (SyncCompletionSpecification completion ) {
346
388
if (completion == null ) {
347
389
return null ;
@@ -462,7 +504,30 @@ public record SyncCompletionSpecification(CompletionRefKey referenceKey,
462
504
BiFunction <McpSyncServerExchange , McpSchema .CompleteRequest , McpSchema .CompleteResult > completionHandler ) {
463
505
}
464
506
507
+ /**
508
+ * A unique key representing a completion reference, composed of its type and
509
+ * identifier. This key is used to look up asynchronous completion specifications in a
510
+ * map-like structure.
511
+ *
512
+ * <p>
513
+ * The {@code type} typically corresponds to the kind of reference, such as
514
+ * {@code "ref/prompt"} or {@code "ref/resource"}, while the {@code identifier} is the
515
+ * name or URI associated with the specific reference.
516
+ *
517
+ * @param type the reference type (e.g., "ref/prompt", "ref/resource")
518
+ * @param identifier the reference identifier (e.g., prompt name or resource URI)
519
+ */
465
520
public record CompletionRefKey (String type , String identifier ) {
521
+
522
+ /**
523
+ * Creates a {@code CompletionRefKey} from a {@link McpSchema.CompleteRequest}.
524
+ * The key is derived from the request's reference type and its associated name or
525
+ * URI.
526
+ * @param request the {@code CompleteRequest} containing a prompt or resource
527
+ * reference
528
+ * @return a unique key based on the request's reference
529
+ * @throws IllegalArgumentException if the reference type is unsupported
530
+ */
466
531
public static CompletionRefKey from (McpSchema .CompleteRequest request ) {
467
532
var ref = request .ref ();
468
533
if (ref instanceof McpSchema .CompleteRequest .PromptReference pr ) {
0 commit comments