Skip to content

Commit b8b5f20

Browse files
committed
chore: fix server documentation for getTask/getTaskResult
Also added taskId convenience overloads.
1 parent 8487b03 commit b8b5f20

File tree

5 files changed

+270
-31
lines changed

5 files changed

+270
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,7 @@ public Mono<McpSchema.GetTaskResult> getTask(String taskId) {
16531653
*
16541654
* <p>
16551655
* This method mirrors
1656-
* {@link io.modelcontextprotocol.server.McpSyncServerExchange#getTaskResult(McpSchema.GetTaskPayloadRequest, TypeRef)},
1656+
* {@link io.modelcontextprotocol.server.McpAsyncServerExchange#getTaskResult(McpSchema.GetTaskPayloadRequest, TypeRef)},
16571657
* which is used for when the server has initiated a task with the client.
16581658
*
16591659
* <p>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ public McpSchema.GetTaskResult getTask(McpSchema.GetTaskRequest getTaskRequest)
542542
*
543543
* <p>
544544
* This method mirrors
545-
* {@link io.modelcontextprotocol.server.McpSyncServerExchange#getTask(McpSchema.GetTaskRequest)},
546-
* which is used for when the server has initiated a task with the client.
545+
* {@link io.modelcontextprotocol.server.McpSyncServerExchange#getTask(String)}, which
546+
* is used for when the server has initiated a task with the client.
547547
*
548548
* <p>
549549
* This is a convenience overload that creates a {@link McpSchema.GetTaskRequest} with
@@ -620,7 +620,7 @@ public <T extends McpSchema.ServerTaskPayloadResult> T getTaskResult(
620620
*
621621
* <p>
622622
* This method mirrors
623-
* {@link io.modelcontextprotocol.server.McpSyncServerExchange#getTaskResult(McpSchema.GetTaskPayloadRequest, TypeRef)},
623+
* {@link io.modelcontextprotocol.server.McpSyncServerExchange#getTaskResult(String, TypeRef)},
624624
* which is used for when the server has initiated a task with the client.
625625
*
626626
* <p>

mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java

Lines changed: 132 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,30 @@ void setMinLoggingLevel(LoggingLevel minLoggingLevel) {
530530
// --------------------------
531531

532532
/**
533-
* Get the status of a task hosted by the client. This is used when the server has
534-
* sent a task-augmented request to the client and needs to poll for status updates.
535-
* @param getTaskRequest The request containing the task ID
536-
* @return A Mono that emits the task status
533+
* Retrieves a task previously initiated by the server with the client.
534+
*
535+
* <p>
536+
* This method mirrors
537+
* {@link io.modelcontextprotocol.client.McpAsyncClient#getTask(McpSchema.GetTaskRequest)},
538+
* which is used for when the client has initiated a task with the server.
539+
*
540+
* <p>
541+
* Example usage:
542+
*
543+
* <pre>{@code
544+
* var result = exchange.getTask(GetTaskRequest.builder()
545+
* .taskId(taskId)
546+
* .build())
547+
* .block();
548+
* }</pre>
549+
*
550+
* <p>
551+
* <strong>Note:</strong> This is an experimental feature that may change in future
552+
* releases.
553+
* @param getTaskRequest The request containing the task ID.
554+
* @return A Mono that completes with the task information.
555+
* @see McpSchema.GetTaskRequest
556+
* @see McpSchema.GetTaskResult
537557
*/
538558
public Mono<McpSchema.GetTaskResult> getTask(McpSchema.GetTaskRequest getTaskRequest) {
539559
if (this.clientCapabilities == null) {
@@ -547,11 +567,69 @@ public Mono<McpSchema.GetTaskResult> getTask(McpSchema.GetTaskRequest getTaskReq
547567
}
548568

549569
/**
550-
* Get the result of a completed task hosted by the client.
551-
* @param <T> The expected result type
552-
* @param getTaskPayloadRequest The request containing the task ID
553-
* @param resultTypeRef Type reference for deserializing the result
554-
* @return A Mono that emits the task result
570+
* Retrieves a task previously initiated by the server with the client by its ID.
571+
*
572+
* <p>
573+
* This method mirrors
574+
* {@link io.modelcontextprotocol.client.McpAsyncClient#getTask(String)}, which is
575+
* used for when the client has initiated a task with the server.
576+
*
577+
* <p>
578+
* This is a convenience overload that creates a {@link McpSchema.GetTaskRequest} with
579+
* the given task ID.
580+
*
581+
* <p>
582+
* Example usage:
583+
*
584+
* <pre>{@code
585+
* var result = exchange.getTask(taskId);
586+
* }</pre>
587+
*
588+
* <p>
589+
* <strong>Note:</strong> This is an experimental feature that may change in future
590+
* releases.
591+
* @param taskId The task identifier to query.
592+
* @return A Mono that completes with the task status and metadata.
593+
*/
594+
public Mono<McpSchema.GetTaskResult> getTask(String taskId) {
595+
return this.getTask(McpSchema.GetTaskRequest.builder().taskId(taskId).build());
596+
}
597+
598+
/**
599+
* Get the result of a completed task previously initiated by the server with the
600+
* client.
601+
*
602+
* <p>
603+
* The result type depends on the original request that created the task. For sampling
604+
* requests, use {@code new TypeRef<McpSchema.CreateMessageResult>(){}}. For
605+
* elicitation requests, use {@code new TypeRef<McpSchema.ElicitResult>(){}}.
606+
*
607+
* <p>
608+
* This method mirrors
609+
* {@link io.modelcontextprotocol.client.McpAsyncClient#getTaskResult(McpSchema.GetTaskPayloadRequest, TypeRef)},
610+
* which is used for when the client has initiated a task with the server.
611+
*
612+
* <p>
613+
* Example usage:
614+
*
615+
* <pre>{@code
616+
* // For sampling task results:
617+
* var result = exchange.getTaskResult(
618+
* new GetTaskPayloadRequest(taskId, null),
619+
* new TypeRef<McpSchema.CreateMessageResult>(){})
620+
* .block();
621+
* }</pre>
622+
*
623+
* <p>
624+
* <strong>Note:</strong> This is an experimental feature that may change in future
625+
* releases.
626+
* @param <T> The expected result type, must extend
627+
* {@link McpSchema.ClientTaskPayloadResult}
628+
* @param getTaskPayloadRequest The request containing the task ID.
629+
* @param resultTypeRef Type reference for deserializing the result.
630+
* @return A Mono that completes with the task result.
631+
* @see McpSchema.GetTaskPayloadRequest
632+
* @see McpSchema.ClientTaskPayloadResult
555633
*/
556634
public <T extends McpSchema.ClientTaskPayloadResult> Mono<T> getTaskResult(
557635
McpSchema.GetTaskPayloadRequest getTaskPayloadRequest, TypeRef<T> resultTypeRef) {
@@ -565,6 +643,51 @@ public <T extends McpSchema.ClientTaskPayloadResult> Mono<T> getTaskResult(
565643
return this.session.sendRequest(McpSchema.METHOD_TASKS_RESULT, getTaskPayloadRequest, resultTypeRef);
566644
}
567645

646+
/**
647+
* Get the result of a completed task previously initiated by the server with the
648+
* client by its task ID.
649+
*
650+
* <p>
651+
* This is a convenience overload that creates a
652+
* {@link McpSchema.GetTaskPayloadRequest} from the task ID.
653+
*
654+
* <p>
655+
* The result type depends on the original request that created the task. For sampling
656+
* requests, use {@code new TypeRef<McpSchema.CreateMessageResult>(){}}. For
657+
* elicitation requests, use {@code new TypeRef<McpSchema.ElicitResult>(){}}.
658+
*
659+
* <p>
660+
* This method mirrors
661+
* {@link io.modelcontextprotocol.client.McpAsyncClient#getTaskResult(String, TypeRef)},
662+
* which is used for when the client has initiated a task with the server.
663+
*
664+
* <p>
665+
* Example usage:
666+
*
667+
* <pre>{@code
668+
* // For sampling task results:
669+
* var result = exchange.getTaskResult(
670+
* taskId,
671+
* new TypeRef<McpSchema.CreateMessageResult>(){})
672+
* .block();
673+
* }</pre>
674+
*
675+
* <p>
676+
* <strong>Note:</strong> This is an experimental feature that may change in future
677+
* releases.
678+
* @param <T> The expected result type, must extend
679+
* {@link McpSchema.ClientTaskPayloadResult}
680+
* @param taskId The task identifier.
681+
* @param resultTypeRef Type reference for deserializing the result.
682+
* @return A Mono that completes with the task result.
683+
* @see McpSchema.GetTaskPayloadRequest
684+
* @see McpSchema.ClientTaskPayloadResult
685+
*/
686+
public <T extends McpSchema.ClientTaskPayloadResult> Mono<T> getTaskResult(String taskId,
687+
TypeRef<T> resultTypeRef) {
688+
return this.getTaskResult(McpSchema.GetTaskPayloadRequest.builder().taskId(taskId).build(), resultTypeRef);
689+
}
690+
568691
/**
569692
* List all tasks hosted by the client.
570693
*

mcp-core/src/main/java/io/modelcontextprotocol/server/McpSyncServerExchange.java

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,146 @@ public Object ping() {
212212
// --------------------------
213213

214214
/**
215-
* Get the status of a task hosted by the client. This is used when the server has
216-
* sent a task-augmented request to the client and needs to poll for status updates.
217-
* @param getTaskRequest The request containing the task ID
218-
* @return The task status
215+
* Retrieves a task previously initiated by the server with the client.
216+
*
217+
* <p>
218+
* This method mirrors
219+
* {@link io.modelcontextprotocol.client.McpSyncClient#getTask(McpSchema.GetTaskRequest)},
220+
* which is used for when the client has initiated a task with the server.
221+
*
222+
* <p>
223+
* Example usage:
224+
*
225+
* <pre>{@code
226+
* var result = exchange.getTask(GetTaskRequest.builder()
227+
* .taskId(taskId)
228+
* .build());
229+
* }</pre>
230+
*
231+
* <p>
232+
* <strong>Note:</strong> This is an experimental feature that may change in future
233+
* releases.
234+
* @param getTaskRequest The request containing the task ID.
235+
* @return The task information.
236+
* @see McpSchema.GetTaskRequest
237+
* @see McpSchema.GetTaskResult
219238
*/
220239
public McpSchema.GetTaskResult getTask(McpSchema.GetTaskRequest getTaskRequest) {
221240
return this.exchange.getTask(getTaskRequest).block();
222241
}
223242

224243
/**
225-
* Get the result of a completed task hosted by the client.
226-
* @param <T> The expected result type
227-
* @param getTaskPayloadRequest The request containing the task ID
228-
* @param resultTypeRef Type reference for deserializing the result
229-
* @return The task result
244+
* Retrieves a task previously initiated by the server with the client by its ID.
245+
*
246+
* <p>
247+
* This method mirrors
248+
* {@link io.modelcontextprotocol.client.McpSyncClient#getTask(String)}, which is used
249+
* for when the client has initiated a task with the server.
250+
*
251+
* <p>
252+
* This is a convenience overload that creates a {@link McpSchema.GetTaskRequest} with
253+
* the given task ID.
254+
*
255+
* <p>
256+
* Example usage:
257+
*
258+
* <pre>{@code
259+
* var result = exchange.getTask(taskId);
260+
* }</pre>
261+
*
262+
* <p>
263+
* <strong>Note:</strong> This is an experimental feature that may change in future
264+
* releases.
265+
* @param taskId The task identifier to query.
266+
* @return The task status and metadata.
267+
*/
268+
public McpSchema.GetTaskResult getTask(String taskId) {
269+
return this.exchange.getTask(McpSchema.GetTaskRequest.builder().taskId(taskId).build()).block();
270+
}
271+
272+
/**
273+
* Get the result of a completed task previously initiated by the server with the
274+
* client.
275+
*
276+
* <p>
277+
* The result type depends on the original request that created the task. For sampling
278+
* requests, use {@code new TypeRef<McpSchema.CreateMessageResult>(){}}. For
279+
* elicitation requests, use {@code new TypeRef<McpSchema.ElicitResult>(){}}.
280+
*
281+
* <p>
282+
* This method mirrors
283+
* {@link io.modelcontextprotocol.client.McpSyncClient#getTaskResult(McpSchema.GetTaskPayloadRequest, TypeRef)},
284+
* which is used for when the client has initiated a task with the server.
285+
*
286+
* <p>
287+
* Example usage:
288+
*
289+
* <pre>{@code
290+
* // For sampling task results:
291+
* var result = exchange.getTaskResult(
292+
* new GetTaskPayloadRequest(taskId, null),
293+
* new TypeRef<McpSchema.CreateMessageResult>(){});
294+
* }</pre>
295+
*
296+
* <p>
297+
* <strong>Note:</strong> This is an experimental feature that may change in future
298+
* releases.
299+
* @param <T> The expected result type, must extend
300+
* {@link McpSchema.ClientTaskPayloadResult}
301+
* @param getTaskPayloadRequest The request containing the task ID.
302+
* @param resultTypeRef Type reference for deserializing the result.
303+
* @return The task result.
304+
* @see McpSchema.GetTaskPayloadRequest
305+
* @see McpSchema.ClientTaskPayloadResult
230306
*/
231307
public <T extends McpSchema.ClientTaskPayloadResult> T getTaskResult(
232308
McpSchema.GetTaskPayloadRequest getTaskPayloadRequest, TypeRef<T> resultTypeRef) {
233309
return this.exchange.getTaskResult(getTaskPayloadRequest, resultTypeRef).block();
234310
}
235311

312+
/**
313+
* Get the result of a completed task previously initiated by the server with the
314+
* client by its task ID.
315+
*
316+
* <p>
317+
* This is a convenience overload that creates a
318+
* {@link McpSchema.GetTaskPayloadRequest} from the task ID.
319+
*
320+
* <p>
321+
* The result type depends on the original request that created the task. For sampling
322+
* requests, use {@code new TypeRef<McpSchema.CreateMessageResult>(){}}. For
323+
* elicitation requests, use {@code new TypeRef<McpSchema.ElicitResult>(){}}.
324+
*
325+
* <p>
326+
* This method mirrors
327+
* {@link io.modelcontextprotocol.client.McpSyncClient#getTaskResult(String, TypeRef)},
328+
* which is used for when the client has initiated a task with the server.
329+
*
330+
* <p>
331+
* Example usage:
332+
*
333+
* <pre>{@code
334+
* // For sampling task results:
335+
* var result = exchange.getTaskResult(
336+
* taskId,
337+
* new TypeRef<McpSchema.CreateMessageResult>(){});
338+
* }</pre>
339+
*
340+
* <p>
341+
* <strong>Note:</strong> This is an experimental feature that may change in future
342+
* releases.
343+
* @param <T> The expected result type, must extend
344+
* {@link McpSchema.ClientTaskPayloadResult}
345+
* @param taskId The task identifier.
346+
* @param resultTypeRef Type reference for deserializing the result.
347+
* @return The task result.
348+
* @see McpSchema.GetTaskPayloadRequest
349+
* @see McpSchema.ClientTaskPayloadResult
350+
*/
351+
public <T extends McpSchema.ClientTaskPayloadResult> T getTaskResult(String taskId, TypeRef<T> resultTypeRef) {
352+
return this.getTaskResult(McpSchema.GetTaskPayloadRequest.builder().taskId(taskId).build(), resultTypeRef);
353+
}
354+
236355
/**
237356
* List all tasks hosted by the client.
238357
*

mcp-core/src/test/java/io/modelcontextprotocol/server/AbstractMcpClientServerIntegrationTests.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,15 +2573,12 @@ void testClientSideTaskHostingForElicitation(String clientType) throws Interrupt
25732573
*/
25742574
private <T extends McpSchema.ClientTaskPayloadResult> Mono<T> pollForTaskCompletion(McpAsyncServerExchange exchange,
25752575
String taskId, TypeRef<T> resultTypeRef) {
2576-
return Mono.defer(() -> exchange.getTask(McpSchema.GetTaskRequest.builder().taskId(taskId).build()))
2577-
.flatMap(task -> {
2578-
if (task.status().isTerminal()) {
2579-
return exchange.getTaskResult(McpSchema.GetTaskPayloadRequest.builder().taskId(taskId).build(),
2580-
resultTypeRef);
2581-
}
2582-
return Mono.delay(Duration.ofMillis(100)).then(pollForTaskCompletion(exchange, taskId, resultTypeRef));
2583-
})
2584-
.timeout(Duration.ofSeconds(30));
2576+
return Mono.defer(() -> exchange.getTask(taskId)).flatMap(task -> {
2577+
if (task.status().isTerminal()) {
2578+
return exchange.getTaskResult(taskId, resultTypeRef);
2579+
}
2580+
return Mono.delay(Duration.ofMillis(100)).then(pollForTaskCompletion(exchange, taskId, resultTypeRef));
2581+
}).timeout(Duration.ofSeconds(30));
25852582
}
25862583

25872584
/**

0 commit comments

Comments
 (0)