← Back to Dashboard
1. Sampling & Roots2. Async Tasks (2025)3. Elicitation & HITL4. Structured Content & Tool Annotations5. Completions & Metadata

Async Tasks (2025)

📚 Advanced Features⏱ 8 min⭐ 100 XP⌨ Hands-on lab

Long-Running Operations & Non-Blocking Asynchronous Tasks

Standard MCP tools are strictly synchronous: the host issues a tools/call request and blocks waiting for the response. When an agent triggers operations requiring minutes or hours (such as building container images, fine-tuning a model, or executing extensive database migrations), synchronous execution risks socket disconnections, proxy timeouts, and frozen user interfaces.

The MCP Task Lifecycle
  1. Immediate Handle Return: The tool initializes background work and immediately returns a unique taskId with status "running".
  2. Progress Stream: The server emits out-of-band progress notifications (notifications/progress) containing a progressToken, percentage complete, and status messages.
  3. Asynchronous Polling & Completion: The host polls tasks/get or listens to server-sent events (SSE). When finished, the task resolves into standard CallToolResult content.
  4. Cooperative Cancellation: The client can issue tasks/cancel if the user aborts the workflow, cleanly reclaiming compute resources.
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";

const server = new McpServer({ name: "cloud-builder", version: "1.0.0" });

server.tool(
  "run_container_build",
  { repoUrl: z.string().url(), tag: z.string() },
  async ({ repoUrl, tag }, extra) => {
    const taskId = "build_" + Date.now();
    
    // Spawn background worker process
    spawnBuildWorker(taskId, repoUrl, tag, async (current, total, msg) => {
      // Stream real-time progress back to the host client
      if (extra._meta?.progressToken) {
        await extra.sendProgressNotification({
          progressToken: extra._meta.progressToken,
          progress: current,
          total: total
        });
      }
    });

    return {
      content: [{ type: "text", text: "Build started asynchronously. Task ID: " + taskId }]
    };
  }
);
⌨ HANDS-ON LABQuery Async Background Task Status
⭐ +150 XP

Query the execution status of an asynchronous background task on the MCP server.

1Send tasks/get method for a long-running task.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 1 — type "hint" if stuck
🧪 Knowledge Check
Press 1-4 to select1 of 3
Why were Tasks introduced into the MCP spec in 2025?
To replace JSON with XML.
To handle long-running tool executions asynchronously without blocking the client connection.
To improve text rendering.
To manage memory garbage collection.