Skip to content

Commit 623e37a

Browse files
authored
Merge pull request modelcontextprotocol#359 from tzolov/java-sdk-completion-docs
docs(java-sdk): add completion capabilities documentation
2 parents 45466e9 + 913da0a commit 623e37a

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

docs/sdk/java/mcp-client.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,40 @@ client.executePrompt("echo", Map.of(
347347
</Tab>
348348
</Tabs>
349349

350+
### Using Completion
351+
352+
As part of the [Completion capabilities](/specification/2025-03-26/server/utilities/completion), MCP provides a provides a standardized way for servers to offer argument autocompletion suggestions for prompts and resource URIs.
353+
354+
Check the [Server Completion capabilities](/sdk/java/mcp-server#completion-specification) to learn how to enable and configure completions on the server side.
355+
356+
On the client side, the MCP client provides methods to request auto-completions:
357+
358+
<Tabs>
359+
<Tab title="Sync API">
360+
361+
```java
362+
363+
CompleteRequest request = new CompleteRequest(
364+
new PromptReference("code_review"),
365+
new CompleteRequest.CompleteArgument("language", "py"));
366+
367+
CompleteResult result = syncMcpClient.completeCompletion(request);
368+
369+
```
370+
</Tab>
371+
372+
<Tab title="Async API">
373+
374+
```java
375+
376+
CompleteRequest request = new CompleteRequest(
377+
new PromptReference("code_review"),
378+
new CompleteRequest.CompleteArgument("language", "py"));
379+
380+
Mono<CompleteResult> result = mcpClient.completeCompletion(request);
381+
382+
```
383+
384+
</Tab>
385+
</Tabs>
386+

docs/sdk/java/mcp-server.mdx

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ McpSyncServer syncServer = McpServer.sync(transportProvider)
4141
.tools(true) // Enable tool support
4242
.prompts(true) // Enable prompt support
4343
.logging() // Enable logging support
44+
.completions() // Enable completions support
4445
.build())
4546
.build();
4647

@@ -335,7 +336,7 @@ Example resource specification:
335336
<Tab title="Sync">
336337
```java
337338
// Sync resource specification
338-
var syncResourceSpecification = new McpServerFeatures.syncResourceSpecification(
339+
var syncResourceSpecification = new McpServerFeatures.SyncResourceSpecification(
339340
new Resource("custom://resource", "name", "description", "mime-type", null),
340341
(exchange, request) -> {
341342
// Resource read implementation
@@ -348,7 +349,7 @@ var syncResourceSpecification = new McpServerFeatures.syncResourceSpecification(
348349
<Tab title="Async">
349350
```java
350351
// Async resource specification
351-
var asyncResourceSpecification = new McpServerFeatures.asyncResourceSpecification(
352+
var asyncResourceSpecification = new McpServerFeatures.AsyncResourceSpecification(
352353
new Resource("custom://resource", "name", "description", "mime-type", null),
353354
(exchange, request) -> {
354355
// Resource read implementation
@@ -374,7 +375,7 @@ The Prompt Specification is a structured template for AI model interactions that
374375
<Tab title="Sync">
375376
```java
376377
// Sync prompt specification
377-
var syncPromptSpecification = new McpServerFeatures.syncPromptSpecification(
378+
var syncPromptSpecification = new McpServerFeatures.SyncPromptSpecification(
378379
new Prompt("greeting", "description", List.of(
379380
new PromptArgument("name", "description", true)
380381
)),
@@ -389,7 +390,7 @@ var syncPromptSpecification = new McpServerFeatures.syncPromptSpecification(
389390
<Tab title="Async">
390391
```java
391392
// Async prompt specification
392-
var asyncPromptSpecification = new McpServerFeatures.asyncPromptSpecification(
393+
var asyncPromptSpecification = new McpServerFeatures.AsyncPromptSpecification(
393394
new Prompt("greeting", "description", List.of(
394395
new PromptArgument("name", "description", true)
395396
)),
@@ -406,6 +407,82 @@ The prompt definition includes name (identifier for the prompt), description (pu
406407
The handler function processes requests and returns formatted templates.
407408
The first argument is `McpAsyncServerExchange` for client interaction, and the second argument is a `GetPromptRequest` instance.
408409

410+
### Completion Specification
411+
412+
As part of the [Completion capabilities](/specification/2025-03-26/server/utilities/completion), MCP provides a provides a standardized way for servers to offer argument autocompletion suggestions for prompts and resource URIs.
413+
414+
<Tabs>
415+
<Tab title="Sync">
416+
```java
417+
// Sync completion specification
418+
var syncCompletionSpecification = new McpServerFeatures.SyncCompletionSpecification(
419+
new McpSchema.PromptReference("code_review"), (exchange, request) -> {
420+
421+
// completion implementation ...
422+
423+
return new McpSchema.CompleteResult(
424+
new CompleteResult.CompleteCompletion(
425+
List.of("python", "pytorch", "pyside"),
426+
10, // total
427+
false // hasMore
428+
));
429+
}
430+
);
431+
432+
// Create a sync server with completion capabilities
433+
var mcpServer = McpServer.sync(mcpServerTransportProvider)
434+
.capabilities(ServerCapabilities.builder()
435+
.completions() // enable completions support
436+
// ...
437+
.build())
438+
// ...
439+
.completions(new McpServerFeatures.SyncCompletionSpecification( // register completion specification
440+
new McpSchema.PromptReference("code_review"), syncCompletionSpecification))
441+
.build();
442+
443+
```
444+
</Tab>
445+
446+
<Tab title="Async">
447+
```java
448+
// Async prompt specification
449+
var asyncCompletionSpecification = new McpServerFeatures.AsyncCompletionSpecification(
450+
new McpSchema.PromptReference("code_review"), (exchange, request) -> {
451+
452+
// completion implementation ...
453+
454+
return Mono.just(new McpSchema.CompleteResult(
455+
new CompleteResult.CompleteCompletion(
456+
List.of("python", "pytorch", "pyside"),
457+
10, // total
458+
false // hasMore
459+
)));
460+
}
461+
);
462+
463+
// Create a async server with completion capabilities
464+
var mcpServer = McpServer.async(mcpServerTransportProvider)
465+
.capabilities(ServerCapabilities.builder()
466+
.completions() // enable completions support
467+
// ...
468+
.build())
469+
// ...
470+
.completions(new McpServerFeatures.AsyncCompletionSpecification( // register completion specification
471+
new McpSchema.PromptReference("code_review"), asyncCompletionSpecification))
472+
.build();
473+
474+
```
475+
</Tab>
476+
</Tabs>
477+
478+
The `McpSchema.CompletionReference` definition defines the type (`PromptRefernce` or `ResourceRefernce`) and the identifier for the completion specification (e.g handler).
479+
The handler function processes requests and returns the complition response.
480+
The first argument is `McpAsyncServerExchange` for client interaction, and the second argument is a `CompleteRequest` instance.
481+
482+
483+
Check the [using completion](/sdk/java/mcp-client#using-completion) to learn how to use the completion capabilities on the client side.
484+
485+
409486
### Using Sampling from a Server
410487

411488
To use [Sampling capabilities](/specification/2024-11-05/client/sampling/), connect to a client that supports sampling.

0 commit comments

Comments
 (0)