MCP Tasks
This guide covers the support for the MCP Tasks extension (io.modelcontextprotocol/tasks): how a tool creates a task, how a task is executed and polled, how to report progress and request input while a task is running, and how cancellation works.
What are MCP Tasks?
Not every tool call returns instantly. Some operations (installing a runtime, indexing a workspace, running a CI pipeline, waiting for a human approval) take seconds, minutes or longer. Holding the connection open until the work finishes is impractical: many clients and transport intermediaries impose timeouts, and a dropped connection loses the result.
The Tasks extension lets the server respond to a tools/call request with a durable task handle instead of the final result.
The client then polls the task until it reaches a terminal status and retrieves the final CallToolResult from the task:
sequenceDiagram
participant Client
participant Server
Client->>Server: tools/call (with tasks capability)
Server-->>Client: CreateTaskResult (resultType: "task", taskId, status: working)
loop Poll until terminal
Client->>Server: tasks/get (taskId)
Server-->>Client: Task (status: working, statusMessage)
end
Client->>Server: tasks/get (taskId)
Server-->>Client: Task (status: completed, result)
Task creation is server-directed: the client declares the tasks extension in its capabilities, and the server decides per request whether to create a task.
The extension defines three methods - tasks/get, tasks/update and tasks/cancel - and the notifications/tasks notification.
|
Tasks are kept in memory by default
Even though the extension is designed for the stateless protocol (
You can provide a custom task store to keep the tasks in a persistent storage shared by multiple server instances. |
Installation
The Tasks extension is provided by a separate module. Add the following dependency to your project:
<dependency>
<groupId>io.quarkiverse.mcp</groupId>
<artifactId>quarkus-mcp-server-tasks</artifactId>
<version>2.1.0</version>
</dependency>
The API lives in the io.quarkiverse.mcp.server.tasks package.
As soon as the dependency is present, the io.modelcontextprotocol/tasks extension is advertised under capabilities.extensions of every server configuration, and the tasks/get, tasks/update and tasks/cancel methods are served.
Creating a task
Declare a Tasks parameter in a @Tool method and create a task with a handler that performs the work:
import io.quarkiverse.mcp.server.Tool;
import io.quarkiverse.mcp.server.ToolResponse;
import io.quarkiverse.mcp.server.tasks.Tasks;
public class LspTools {
@Tool(description = "Finds the references of a symbol")
ToolResponse references(String file, Tasks tasks) { (1)
if (!tasks.isSupported()) { (2)
return findReferences(file);
}
throw tasks.newTask()
.setTtl(Duration.ofHours(1)) (3)
.setPollInterval(Duration.ofSeconds(5))
.setHandler(task -> { (4)
task.setStatusMessageAndAwait("Installing the runtime..."); (5)
installRuntime();
task.setStatusMessageAndAwait("Starting the language server...");
startLanguageServer();
return findReferences(file);
}, false)
.createAndAwait(); (6)
}
}
| 1 | Tasks is injected by the extension; it is bound to the current request. |
| 2 | The client may not support tasks; in that case the tool simply does the work synchronously. |
| 3 | The ttl and pollInterval are optional; see Time-to-live and polling interval. |
| 4 | A blocking handler, executed on a worker thread; the TaskContext gives access to the task. |
| 5 | The status message is included in the tasks/get responses. setStatusMessageAndAwait() blocks until the message is stored; setStatusMessage() is the non-blocking variant that returns a Uni. |
| 6 | createAndAwait() durably creates the task, starts the handler asynchronously and returns a CreateTaskException. The exception is thrown from the tool method so that the server responds with the CreateTaskResult. This method blocks until the task is stored and must not be called on an event loop thread. |
When the tool throws the CreateTaskException, the server:
-
responds immediately with a
CreateTaskResult- the exception is anMcpResultException, see Error Handling, -
executes the handler asynchronously, and
-
stores the
ToolResponsereturned by the handler in the task once it completes.
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"resultType": "task",
"taskId": "786512e2-9e0d-44bd-8f29-789f320fe840",
"status": "working",
"createdAt": "2026-09-18T10:30:00Z",
"lastUpdatedAt": "2026-09-18T10:30:00Z",
"ttlMs": 3600000,
"pollIntervalMs": 5000
}
}
Execution model of the handler
The handler determines the execution model, following the same pattern as the programmatic feature definitions such as ToolManager.ToolDefinition:
| Handler | Execution model |
|---|---|
|
Blocking; executed on a worker thread ( |
|
Blocking; executed on a virtual thread ( |
|
Non-blocking; executed on the event loop ( |
A non-blocking tool returns the task with create(), the non-blocking variant of createAndAwait().
The returned Uni never emits an item; it fails with the CreateTaskException once the task is stored:
@Tool(description = "Runs a pipeline")
Uni<ToolResponse> runPipeline(String repo, Tasks tasks) {
if (!tasks.isSupported()) {
return run(repo);
}
return tasks.newTask()
.setAsyncHandler(task -> run(repo))
.create();
}
The handler is executed with its own CDI request context and, if security is enabled, with the SecurityIdentity of the request that created the task.
The identity is captured when the task is created. If proactive authentication is disabled and the identity cannot be resolved at that point, an error is logged and the handler is executed without a SecurityIdentity.
|
Capability negotiation
The tasks extension is advertised under capabilities.extensions of every server configuration:
{
"capabilities": {
"extensions": {
"io.modelcontextprotocol/tasks": {}
}
}
}
Tasks#isSupported() returns true only if the client declared the extension in its own capabilities - in the initialize request, or in the per-request io.modelcontextprotocol/clientCapabilities metadata for stateless clients:
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "references",
"arguments": { "file": "Foo.cs" },
"_meta": {
"io.modelcontextprotocol/clientCapabilities": {
"extensions": {
"io.modelcontextprotocol/tasks": {}
}
}
}
}
}
The tool decides what to do for a client that did not declare the extension: typically it does the work synchronously and returns the standard CallToolResult.
If the tool cannot do so, it calls newTask() anyway; the call then fails with the error code -32021 (Missing Required Client Capability) as mandated by the specification:
{
"jsonrpc": "2.0",
"id": 2,
"error": {
"code": -32021,
"message": "Missing required client capability: io.modelcontextprotocol/tasks",
"data": {
"requiredCapabilities": {
"extensions": { "io.modelcontextprotocol/tasks": {} }
}
}
}
}
Time-to-live and polling interval
| Method | Default | Description |
|---|---|---|
|
|
The time-to-live of the task, measured from its creation and advertised as |
|
|
The polling interval suggested to the client, advertised as |
|
none |
The initial status message of the task. |
| Expired tasks are removed periodically and when they are accessed; a task with an unlimited TTL is never removed. |
Polling and the task lifecycle
The client polls the task with tasks/get.
The response carries the current status and, for terminal statuses, the final result or error:
| Status | Meaning |
|---|---|
|
The handler is being executed. |
|
The tool is waiting for input from the client; see Requesting input while a task is running. |
|
The handler completed; the |
|
The handler failed with a JSON-RPC error (e.g. an |
|
The task was cancelled; see Cancellation. |
completed, failed and cancelled are terminal - once reached, the status does not change and the result is available until the task expires.
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"resultType": "complete",
"taskId": "786512e2-9e0d-44bd-8f29-789f320fe840",
"status": "completed",
"statusMessage": "Starting the language server...",
"createdAt": "2026-09-18T10:30:00Z",
"lastUpdatedAt": "2026-09-18T10:31:12Z",
"ttlMs": 3600000,
"pollIntervalMs": 5000,
"result": {
"content": [ { "type": "text", "text": "3 references found" } ],
"isError": false
}
}
}
A tasks/get, tasks/update or tasks/cancel request for an unknown or expired task results in a -32602 (Invalid params) error.
These methods also require the client to declare the tasks extension capability (-32021 otherwise).
| A task is bound to the server configuration it was created for. Task ids are random UUIDs that act as bearer tokens, as permitted by the specification; a task can therefore be polled after a reconnect, by any client that knows its id. |
Requesting input while a task is running
A running task may need input from the client - a confirmation, a form, an LLM completion or the list of roots.
Rather than sending a server-initiated request over the connection, the handler uses TaskContext#inputRequestBuilder():
import io.quarkiverse.mcp.server.Elicitation;
import io.quarkiverse.mcp.server.ElicitationRequest.StringSchema;
import io.quarkiverse.mcp.server.InputResponses;
import io.quarkiverse.mcp.server.Tool;
import io.quarkiverse.mcp.server.ToolResponse;
import io.quarkiverse.mcp.server.tasks.Tasks;
public class GreetingTools {
@Tool(description = "Greets the user")
ToolResponse greet(Tasks tasks, Elicitation elicitation) {
throw tasks.newTask().setHandler(task -> {
InputResponses responses = task.inputRequestBuilder()
.addElicitationRequest("name", elicitation.requestBuilder() (1)
.setMessage("Please enter your name.")
.addSchemaProperty("name", new StringSchema(true))
.build())
.addRootsRequest("roots")
.build()
.sendAndAwait(); (2)
String name = responses.getElicitationResponse("name").content().getString("name"); (3)
return ToolResponse.success("Hello, " + name + "!");
}, false).createAndAwait();
}
}
| 1 | The elicitation request is built as usual but it is not sent directly; it is added to the input request under a server-assigned key. |
| 2 | The task moves to the input_required status. sendAndAwait() blocks until all the requests are fulfilled; use send() to obtain a Uni<InputResponses> in a non-blocking handler. |
| 3 | The responses are keyed by the same identifiers. |
While the task is in the input_required status, the outstanding requests are included in the tasks/get responses, in the same shape as the inputRequests of a Multi Round-Trip Request:
{
"jsonrpc": "2.0",
"id": 4,
"result": {
"resultType": "complete",
"taskId": "786512e2-9e0d-44bd-8f29-789f320fe840",
"status": "input_required",
"createdAt": "2026-09-18T10:30:00Z",
"lastUpdatedAt": "2026-09-18T10:30:05Z",
"ttlMs": 3600000,
"pollIntervalMs": 5000,
"inputRequests": {
"name": {
"method": "elicitation/create",
"params": {
"mode": "form",
"message": "Please enter your name.",
"requestedSchema": {
"type": "object",
"properties": { "name": { "type": "string" } },
"required": ["name"]
}
}
},
"roots": { "method": "roots/list", "params": {} }
}
}
}
The client fulfills the requests with one or more tasks/update requests; the task moves back to working once all the outstanding requests are answered:
{
"jsonrpc": "2.0",
"id": 5,
"method": "tasks/update",
"params": {
"taskId": "786512e2-9e0d-44bd-8f29-789f320fe840",
"inputResponses": {
"name": { "action": "accept", "content": { "name": "Luca" } },
"roots": { "roots": [ { "uri": "file:///home/luca", "name": "home" } ] }
}
}
}
Responses for unknown or already satisfied keys are ignored. Each request key must be unique over the lifetime of the task.
|
Within a handler, always use |
Cancellation
A client cancels a task with tasks/cancel.
The request is acknowledged immediately and the task moves to the cancelled status.
Cancellation is cooperative: the handler is not forced to stop, but it is notified through the standard Cancellation API available from TaskContext#cancellation():
@Tool(description = "Indexes the workspace")
ToolResponse index(Tasks tasks) {
throw tasks.newTask().setHandler(task -> {
for (Path file : files()) {
task.cancellation().skipProcessingIfCancelled(); (1)
index(file);
}
return ToolResponse.success("Indexed");
}, false).createAndAwait();
}
| 1 | Throws an OperationCancellationException if the task was cancelled; the exception is ignored by the server. |
A result produced by the handler after the cancellation is discarded.
A task waiting for input is cancelled as well: the pending TaskInputRequest#send() fails with an OperationCancellationException.
The notifications/cancelled notification is not used for task cancellation, as mandated by the specification.
|
Tasks can also be inspected and cancelled programmatically with the TaskManager bean.
Its methods are asynchronous and return a Uni:
import io.quarkiverse.mcp.server.tasks.TaskManager;
@Inject
TaskManager taskManager;
Uni<Void> cancelAll() {
return taskManager.getTasks() (1)
.onItem().transformToMulti(tasks -> Multi.createFrom().iterable(tasks))
.select().where(task -> !task.status().isTerminal())
.onItem().transformToUniAndConcatenate(task -> taskManager.cancelTask(task.id(), "Shutting down")) (2)
.collect().last()
.replaceWithVoid();
}
| 1 | getTasks() returns the tasks of all server configurations; use getTasks(String) to obtain the tasks bound to a specific server. |
| 2 | The tasks are cancelled sequentially. Avoid subscribing to many Uni instances at once, e.g. with Uni.join().all(); the operations would hit the task store concurrently, which is not supported by some stores, such as those backed by a Hibernate Reactive session. |
Task store
The state of the tasks is kept in a io.quarkiverse.mcp.server.tasks.spi.TaskStore.
By default, the tasks are kept in memory.
An application can provide a custom implementation as a CDI bean to keep the tasks in a persistent storage, such as Redis or a database.
If the storage is shared by multiple server instances, the tasks survive a restart and the tasks/get, tasks/update and tasks/cancel requests can be served by any instance.
The state of a task is represented by an immutable io.quarkiverse.mcp.server.tasks.spi.TaskRecord.
A store may persist it in any form.
The following example maps the values of a record to table columns; only the payloads - MCP messages such as the tool result - are stored as JSON text:
import io.quarkiverse.mcp.server.tasks.spi.TaskRecord;
import io.quarkiverse.mcp.server.tasks.spi.TaskStore;
import io.quarkus.vertx.VertxContextSupport;
@Singleton
public class JdbcTaskStore implements TaskStore {
@Inject
DataSource dataSource;
@Override
public Uni<TaskRecord> get(String taskId) {
return VertxContextSupport.executeBlocking(() -> { (1)
try (Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement("SELECT * FROM tasks WHERE id = ?")) {
ps.setString(1, taskId);
try (ResultSet rs = ps.executeQuery()) {
return rs.next() ? toRecord(rs) : null;
}
}
});
}
@Override
public Uni<Iterable<TaskRecord>> getAll(Set<String> taskIds) {
return VertxContextSupport.executeBlocking(() -> { (2)
String placeholders = String.join(", ", Collections.nCopies(taskIds.size(), "?"));
try (Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement("SELECT * FROM tasks WHERE id IN (" + placeholders + ")")) {
int idx = 1;
for (String taskId : taskIds) {
ps.setString(idx++, taskId);
}
List<TaskRecord> ret = new ArrayList<>();
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
ret.add(toRecord(rs));
}
}
return ret;
}
});
}
@Override
public Uni<Boolean> replace(TaskRecord expected, TaskRecord updated) {
return VertxContextSupport.executeBlocking(() -> {
try (Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement(
"UPDATE tasks SET version = ?, status = ?, status_message = ?, last_updated_at = ?, "
+ "result = ?, ... WHERE id = ? AND version = ?")) { (3)
ps.setLong(1, updated.version());
ps.setString(2, updated.status().name());
ps.setString(3, updated.statusMessage());
ps.setTimestamp(4, Timestamp.from(updated.lastUpdatedAt()));
ps.setString(5, updated.result() != null ? updated.result().encode() : null);
// ...
ps.setString(6, updated.id());
ps.setLong(7, expected.version());
return ps.executeUpdate() == 1;
}
});
}
@Override
public Uni<Void> removeExpired(Instant now) {
return VertxContextSupport.executeBlocking(() -> {
try (Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement("DELETE FROM tasks WHERE expires_at < ?")) { (4)
ps.setTimestamp(1, Timestamp.from(now));
ps.executeUpdate();
return null;
}
});
}
private TaskRecord toRecord(ResultSet rs) throws SQLException {
String result = rs.getString("result");
return TaskRecord.builder() (5)
.id(rs.getString("id"))
.executionNodeId(rs.getString("execution_node_id"))
.version(rs.getLong("version"))
.status(TaskStatus.valueOf(rs.getString("status")))
.statusMessage(rs.getString("status_message"))
.lastUpdatedAt(rs.getTimestamp("last_updated_at").toInstant())
.result(result != null ? new JsonObject(result) : null)
// ...
.build();
}
// create(), delete() and list() are implemented similarly
}
| 1 | All methods return a Uni and must not block the caller thread; a blocking implementation should offload the work to a worker thread. |
| 2 | getAll() fetches multiple tasks in a single batch. If the store is shared, it is used to check the state of the running tasks periodically; see below. |
| 3 | replace() is a compare-and-set operation: the task is replaced only if the stored version equals the version of the expected task. |
| 4 | TaskRecord#expiresAt() can be stored in a dedicated column so that the expired tasks can be removed with a single statement. A store that removes the expired tasks automatically, e.g. via a native TTL support, does not need to implement removeExpired() at all. |
| 5 | The record is reconstructed via TaskRecord#builder(). All values must be restored; TaskRecord#build() fails if a mandatory value, such as the id of the execution node, is missing. |
A document-oriented store may persist the whole record as a single JSON document instead, using the optional TaskRecord#toJson() and TaskRecord#fromJson() helpers.
|
A TaskRecord is an immutable snapshot of the state of a task.
Every state transition is a read-modify-write cycle: the current state is obtained with get() and the new state, with an incremented version, is stored with replace().
If the task was modified concurrently, e.g. by another server instance, the transition is retried.
The handler of a task is always executed by the server instance that created the task.
If a cancellation or input responses are received by another server instance, the executing instance observes the change when it checks the store, which happens at the polling interval of the task.
A single periodic timer is used for all running tasks of a server instance, and the tasks that are due are fetched in a single batch via TaskStore#getAll().
A store should fetch the tasks efficiently, e.g. via SELECT … WHERE id IN (…) or Redis MGET, rather than calling get() for each task.
A store that is never modified by another server instance should return false from TaskStore#isShared(), so that the periodic checks are skipped altogether; this is the case of the default in-memory store.
If a server instance is stopped while executing a task, the task remains in the working (or input_required) status until it expires. Each task records the identifier of the executing instance (TaskRecord#executionNodeId()); a new identifier is generated at each start of a server instance. However, such orphaned tasks are not detected automatically yet.
|
Limitations
-
By default, the tasks are kept in memory and are only available on the server instance that created them; see Task store.
-
Task status notifications (
notifications/tasksviasubscriptions/listen) are not supported yet; clients poll the task withtasks/get. -
Taskscan only be injected into@Toolmethods; tools registered programmatically viaToolManagercannot create tasks yet.
Streamable HTTP routing headers
Stateless clients using the Streamable HTTP transport must set the Mcp-Name header of tasks/get, tasks/update and tasks/cancel requests to the task id, so that intermediaries can route the requests to the server instance holding the task state (unless a shared task store is used).
The Mcp-Method header is set to the JSON-RPC method name as usual.
Testing
Tasks are tested like any other feature with McpAssured.
Declare the extension in the client capabilities, assert the CreateTaskResult via withRawAssert() and poll the task with a generic request:
import io.quarkiverse.mcp.server.ClientCapability;
import io.quarkiverse.mcp.server.tasks.TaskManager;
import io.quarkiverse.mcp.server.test.McpAssured;
import io.quarkiverse.mcp.server.test.McpAssured.McpStreamableTestClient;
@Test
public void testTask() {
McpStreamableTestClient client = McpAssured.newStreamableClient()
.setClientCapabilities(new ClientCapability(ClientCapability.EXTENSIONS,
Map.of(TaskManager.EXTENSION_ID, Map.of()))) (1)
.build()
.connect();
try (client) {
AtomicReference<String> taskId = new AtomicReference<>();
client.when()
.toolsCall("references")
.withArguments(Map.of("file", "Foo.cs"))
.withRawAssert(response -> { (2)
JsonObject result = response.getJsonObject("result");
assertEquals("task", result.getString("resultType"));
taskId.set(result.getString("taskId"));
})
.send()
.thenAssertResults();
client.when()
.message(client.newRequest("tasks/get") (3)
.put("params", new JsonObject().put("taskId", taskId.get())))
.withAssert(response -> {
JsonObject task = response.getJsonObject("result");
assertNotNull(task.getString("status"));
})
.send()
.thenAssertResults();
}
}
| 1 | The client declares the tasks extension. |
| 2 | The CreateTaskResult is asserted via the raw JSON-RPC response. |
| 3 | The task is polled with a generic request. For a stateless client, use McpAssured.injectStatelessMeta(request, clientCapabilities) to declare the extension on the raw request. |