Skip to content
mentoor

Topic 3 of 5 · 6 min read

Long-running work with the MCP Tasks extension

Goal: Decide which tools should return tasks and trace a task from creation through input_required to completion or failure.

A tool that may run for minutes should return a durable task handle instead of holding the connection open; the client polls the task, answers any questions it raises, and reads the result when it finishes. Tasks live in an official extension named io.modelcontextprotocol/tasks, not in the core protocol.

Check yourself · 1 of 3

A tool call returns a CreateTaskResult with a taskId. What should the client do next?

Choose one answer

Why it matters

A CI pipeline, a batch export or an approval step can take far longer than a connection or a proxy timeout. If the client disconnects, blocked work is lost to it. A task ID is a durable handle: after a crash or restart the client resumes with the same ID. The overview also lists status messages for progress, and mid-flight input without a second connection.

How it works

  1. Opt in. The client lists io.modelcontextprotocol/tasks in its per-request capabilities, and the server advertises it in server/discover.
  2. The server decides. For a supported request such as tools/call, the server may return a CreateTaskResult (resultType: "task") with a taskId, a status, a TTL and a polling interval. A client must therefore handle two result shapes for the same call.
  3. Poll. The client calls tasks/get with the taskId and respects pollIntervalMs. Servers can also send notifications/tasks to clients that subscribed.
  4. Answer questions. If the status is input_required, the tasks/get response includes inputRequests. The client responds with tasks/update.
  5. Finish. completed carries the result the call would have returned. failed carries the JSON-RPC error. cancelled is the third end state. All three are terminal and never change again.
  6. Cancel. tasks/cancel is a request, not a command: the server may keep working.

Which tools return tasks

Return a task when the work can take longer than a few seconds, needs a human approval gate, or must survive a reconnect. Keep fast lookups synchronous. A tool that always returns a task for a 50 ms read only adds polling.

Before and after

Weak: run_pipeline blocks for 12 minutes. The proxy times out at 60 seconds and the user sees an error while the pipeline keeps running.

Fixed: it answers at once.

{ "resultType": "task", "taskId": "t_7f2", "status": "working", "pollIntervalMs": 5000 }

The client polls tasks/get every 5 seconds and reads the final result when the status is completed.

What goes wrong

  • Ignoring the interval. Polling every 100 ms against a server that asked for 5000 wastes both sides.
  • Losing the ID. A client that keeps the taskId only in memory cannot resume.
  • Assuming cancel stopped it. Check the final status before telling the user that nothing ran.

Examples

A deploy with an approval gate

tools/call deploy          -> task t_91, working
tasks/get t_91             -> working, "building image"
tasks/get t_91             -> input_required, inputRequests: { approve: elicitation "Deploy to production?" }
tasks/update t_91          -> inputResponses: { approve: { action: "accept" } }
tasks/get t_91             -> working, "rolling out"
tasks/get t_91             -> completed, result: { url: "https://app.example.com" }

The approval needed no second connection, and the user could close the laptop between steps.

Choosing per tool

get_build_status returns in 40 ms and stays synchronous. export_all_invoices takes 2 to 20 minutes depending on the account, so the server returns a task when the estimate is long and answers directly when the account is small. The client code handles both.

Key points

  • A task is a durable handle, so a client can resume polling after a disconnect or restart.
  • The server decides per request whether to return a task, and the client must handle both result shapes.
  • The statuses are working, input_required, completed, failed and cancelled, and the last three are terminal.
  • Questions during a task arrive in inputRequests on tasks/get and are answered with tasks/update.
  • tasks/cancel is cooperative, so the server is not obliged to stop the work.
  • Return tasks for long, approval-gated or reconnect-sensitive work and keep quick reads synchronous.

Common mistakes

  • Holding the connection open for slow work and relying on a long timeout.
  • Treating a task result as the final tool result without checking the status.
  • Polling faster than the server's suggested interval.
  • Treating a cancelled task as proof the work never ran.

Check yourself · 2 of 3

Which statuses are terminal for an MCP task?

Choose one answer

Check yourself · 3 of 3

A task reaches input_required with an approval question. How does the client answer it?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic