Skip to content

Commit 768d123

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

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

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

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

811+
/**
812+
* Sends a completion/complete request to generate value suggestions based on a given
813+
* reference and argument. This is typically used to provide auto-completion options
814+
* for user input fields.
815+
* @param completeRequest The request containing the prompt or resource reference and
816+
* argument for which to generate completions.
817+
* @return A Mono that completes with the result containing completion suggestions.
818+
* @see McpSchema.CompleteRequest
819+
* @see McpSchema.CompleteResult
820+
*/
811821
public Mono<McpSchema.CompleteResult> completeCompletion(McpSchema.CompleteRequest completeRequest) {
812822
return this.withInitializationCheck("complete completions", initializedResult -> this.mcpSession
813823
.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
332+
* reference 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: 14 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,19 @@ private McpServerSession.RequestHandler<McpSchema.CompleteResult> completionComp
755756
};
756757
}
757758

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,14 @@ 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+
* @param completions List of completion specifications. Must not be null.
935+
* @return This builder instance for method chaining
936+
* @throws IllegalArgumentException if completions is null
937+
* @see #completions(McpServerFeatures.SyncCompletionSpecification...)
938+
*/
931939
public SyncSpecification completions(List<McpServerFeatures.SyncCompletionSpecification> completions) {
932940
Assert.notNull(completions, "Completions list must not be null");
933941
for (McpServerFeatures.SyncCompletionSpecification completion : completions) {
@@ -936,6 +944,13 @@ public SyncSpecification completions(List<McpServerFeatures.SyncCompletionSpecif
936944
return this;
937945
}
938946

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

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

Lines changed: 65 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
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+
*/
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
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+
*/
345387
static AsyncCompletionSpecification fromSync(SyncCompletionSpecification completion) {
346388
if (completion == null) {
347389
return null;
@@ -462,7 +504,30 @@ 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
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+
*/
465520
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+
*/
466531
public static CompletionRefKey from(McpSchema.CompleteRequest request) {
467532
var ref = request.ref();
468533
if (ref instanceof McpSchema.CompleteRequest.PromptReference pr) {

0 commit comments

Comments
 (0)