Step 5 of 7
Reconnect to a task and cancel work
Save a task ID, return to the same batch and request cancellation.
In this step you will check that a task keeps running after the MCP client that started it has gone, and then you will cancel a task while its batch is still running. These are two different actions, and they end differently:
The project you copied includes a command-line wrapper, src/client.ts, with run,
watch and cancel commands. It uses the same connection helper and polling
loop that you just wrote, and it saves task IDs in output/last-task.json.
1. Leave a task running
Keep terminal A running with pnpm run server --demo-delay-ms 1000. In terminal B:
pnpm run client run --detachExpect:
Task ID: <task-id>
Detached. Run: pnpm run client watch lastThe command saves the task ID and exits without cancelling anything. You can close terminal B if you like, because the MCP server in terminal A keeps processing the images.
2. Reconnect using the saved ID
Reopen a terminal in the same project folder and run:
pnpm run client watch last
pnpm run verify --outputEventually you should see completed: All thumbnails are ready., followed by
Created 18 thumbnails. and a successful verification. The batch may already be
finished by the time you reconnect. You can also run
pnpm run client watch <task-id>, replacing the placeholder with the ID that was
printed.
The watch command sends tasks/get, and it does not call create_thumbnails
again. Check that its output folder is the one from the same batch. The stored
MCP server URL and the task ID together identify which work you are picking up
again, and the official
recovery guide
uses the same idea when it hands tasks from one connection to another.
If the task is reported as unknown or expired, check the ID, the URL and the project folder. This example keeps tasks for one hour. Also note that restarting the MCP server marks any unfinished batches as failed, because a saved task record on its own does not restart an interrupted image job.
3. Request cancellation
Start a new batch with the delay enabled, and ask for it to be cancelled quickly:
pnpm run client run --detach
pnpm run client cancel lastThe first message acknowledges your request:
Cancellation requested. Checking the actual outcome…When the worker stops, you will see:
cancelled: Cancelled; completed files remain in output.The wrapper calls connection.cancelTask(id) and then runs your watchTask()
loop. The worker only checks for cancellation between Sharp operations, so the
operation in progress may finish first. If the batch had already finished, you
may see completed instead, so try again with the delay enabled. Any files
created before the cancellation stay where they are.
Closing an MCP client and cancelling a task are different actions, and the official lifecycle API keeps detaching and cancelling separate as well.
Checkpoint: you retrieved the same batch after the MCP client had exited, and you saw the task's final state after asking for cancellation. The acknowledgement on its own is not proof that processing stopped.
Step 5
Ready to continue?
Mark this step complete when you have finished the checks above.
Checking saved progress…

