Skip to content

Commit c683541

Browse files
committed
add comments to new lines
Signed-off-by: jitokim <pigberger70@gmail.com>
1 parent 09cbe0a commit c683541

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

mcp/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,17 @@ void setProtocolVersions(List<String> protocolVersions) {
808808
private static final TypeReference<McpSchema.CompleteResult> COMPLETION_COMPLETE_RESULT_TYPE_REF = new TypeReference<>() {
809809
};
810810

811+
812+
/**
813+
* Sends a completion/complete request to generate value suggestions based on a given reference
814+
* and argument. This is typically used to provide auto-completion options for user input fields.
815+
*
816+
* @param completeRequest The request containing the prompt or resource reference and argument
817+
* for which to generate completions.
818+
* @return A Mono that completes with the result containing completion suggestions.
819+
* @see McpSchema.CompleteRequest
820+
* @see McpSchema.CompleteResult
821+
*/
811822
public Mono<McpSchema.CompleteResult> completeCompletion(McpSchema.CompleteRequest completeRequest) {
812823
return this.withInitializationCheck("complete completions", initializedResult -> this.mcpSession
813824
.sendRequest(McpSchema.METHOD_COMPLETION_COMPLETE, completeRequest, COMPLETION_COMPLETE_RESULT_TYPE_REF));

mcp/src/main/java/io/modelcontextprotocol/client/McpSyncClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ public void setLoggingLevel(McpSchema.LoggingLevel loggingLevel) {
326326
this.delegate.setLoggingLevel(loggingLevel).block();
327327
}
328328

329+
/**
330+
* Send a completion/complete request.
331+
* @param completeRequest the completion request contains the prompt or resource reference
332+
* and arguments for generating suggestions.
333+
* @return the completion result containing suggested values.
334+
*/
329335
public McpSchema.CompleteResult completeCompletion(McpSchema.CompleteRequest completeRequest) {
330336
return this.delegate.completeCompletion(completeRequest).block();
331337
}

mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ private McpServerSession.RequestHandler<McpSchema.CompleteResult> completionComp
728728

729729
String type = request.ref().type();
730730

731+
// check if the referenced resource exists
731732
if (type.equals("ref/prompt")
732733
&& request.ref() instanceof McpSchema.CompleteRequest.PromptReference promptReference) {
733734
McpServerFeatures.AsyncPromptSpecification prompt = this.prompts.get(promptReference.name());
@@ -755,6 +756,17 @@ private McpServerSession.RequestHandler<McpSchema.CompleteResult> completionComp
755756
};
756757
}
757758

759+
/**
760+
* Parses the raw JSON-RPC request parameters into a {@link McpSchema.CompleteRequest} object.
761+
* <p>
762+
* This method manually extracts the `ref` and `argument` fields from the input map,
763+
* determines the correct reference type (either prompt or resource),
764+
* and constructs a fully-typed {@code CompleteRequest} instance.
765+
*
766+
* @param object the raw request parameters, expected to be a Map containing "ref" and "argument" entries.
767+
* @return a {@link McpSchema.CompleteRequest} representing the structured completion request.
768+
* @throws IllegalArgumentException if the "ref" type is not recognized.
769+
*/
758770
@SuppressWarnings("unchecked")
759771
private McpSchema.CompleteRequest parseCompletionParams(Object object) {
760772
Map<String, Object> params = (Map<String, Object>) object;

mcp/src/main/java/io/modelcontextprotocol/server/McpServer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,15 @@ public SyncSpecification prompts(McpServerFeatures.SyncPromptSpecification... pr
928928
return this;
929929
}
930930

931+
/**
932+
* Registers multiple completions with their handlers using a List. This method is
933+
* useful when completions need to be added in bulk from a collection.
934+
*
935+
* @param completions List of completion specifications. Must not be null.
936+
* @return This builder instance for method chaining
937+
* @throws IllegalArgumentException if completions is null
938+
* @see #completions(McpServerFeatures.SyncCompletionSpecification...)
939+
*/
931940
public SyncSpecification completions(List<McpServerFeatures.SyncCompletionSpecification> completions) {
932941
Assert.notNull(completions, "Completions list must not be null");
933942
for (McpServerFeatures.SyncCompletionSpecification completion : completions) {
@@ -936,6 +945,14 @@ public SyncSpecification completions(List<McpServerFeatures.SyncCompletionSpecif
936945
return this;
937946
}
938947

948+
/**
949+
* Registers multiple completions with their handlers using varargs. This method is
950+
* useful when completions are defined inline and added directly.
951+
*
952+
* @param completions Array of completion specifications. Must not be null.
953+
* @return This builder instance for method chaining
954+
* @throws IllegalArgumentException if completions is null
955+
*/
939956
public SyncSpecification completions(McpServerFeatures.SyncCompletionSpecification... completions) {
940957
Assert.notNull(completions, "Completions list must not be null");
941958
for (McpServerFeatures.SyncCompletionSpecification completion : completions) {

mcp/src/main/java/io/modelcontextprotocol/server/McpServerFeatures.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,51 @@ static AsyncPromptSpecification fromSync(SyncPromptSpecification prompt) {
339339
}
340340
}
341341

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+
*/
342376
public record AsyncCompletionSpecification(
343377
BiFunction<McpAsyncServerExchange, McpSchema.CompleteRequest, Mono<McpSchema.CompleteResult>> completionHandler) {
344378

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+
*/
345387
static AsyncCompletionSpecification fromSync(SyncCompletionSpecification completion) {
346388
if (completion == null) {
347389
return null;
@@ -462,7 +504,27 @@ public record SyncCompletionSpecification(CompletionRefKey referenceKey,
462504
BiFunction<McpSyncServerExchange, McpSchema.CompleteRequest, McpSchema.CompleteResult> completionHandler) {
463505
}
464506

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+
*/
465518
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+
*/
466528
public static CompletionRefKey from(McpSchema.CompleteRequest request) {
467529
var ref = request.ref();
468530
if (ref instanceof McpSchema.CompleteRequest.PromptReference pr) {

0 commit comments

Comments
 (0)