@@ -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 requests
372
+ * and returns results. The first argument is an {@link McpAsyncServerExchange}
373
+ * used to interact with the client. The second argument is a
374
+ * {@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 elastic
382
+ * scheduler for safe non-blocking execution.
383
+ *
384
+ * @param completion the synchronous completion specification
385
+ * @return an asynchronous wrapper of the provided sync specification, or {@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,27 @@ 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 identifier.
509
+ * This key is used to look up asynchronous completion specifications in a map-like structure.
510
+ *
511
+ * <p>The {@code type} typically corresponds to the kind of reference, such as
512
+ * {@code "ref/prompt"} or {@code "ref/resource"}, while the {@code identifier} is the
513
+ * name or URI associated with the specific reference.
514
+ *
515
+ * @param type the reference type (e.g., "ref/prompt", "ref/resource")
516
+ * @param identifier the reference identifier (e.g., prompt name or resource URI)
517
+ */
465
518
public record CompletionRefKey (String type , String identifier ) {
519
+
520
+ /**
521
+ * Creates a {@code CompletionRefKey} from a {@link McpSchema.CompleteRequest}.
522
+ * The key is derived from the request's reference type and its associated name or URI.
523
+ *
524
+ * @param request the {@code CompleteRequest} containing a prompt or resource reference
525
+ * @return a unique key based on the request's reference
526
+ * @throws IllegalArgumentException if the reference type is unsupported
527
+ */
466
528
public static CompletionRefKey from (McpSchema .CompleteRequest request ) {
467
529
var ref = request .ref ();
468
530
if (ref instanceof McpSchema .CompleteRequest .PromptReference pr ) {
0 commit comments