DEV Community

Cover image for WebMCP in Practice — Keep Tools Honest When State Changes
Luca Del Puppo for Playful Programming

Posted on Originally published at blog.delpuppo.net on

WebMCP in Practice — Keep Tools Honest When State Changes

An agent lists the DevDesk tasks and finds the one you just created. Before it calls set_task_status, you reset the example board. The agent still has a perfectly valid-looking ID. The board has six fixtures and no sign of that task.

In Part 03, we separated finding a task from changing its status. That gave us a precise way to point at a task, but the board can still change between those two calls. Today, we’ll give the caller enough information to recover from a stale ID, explain an update that did nothing, and say when a change exists only in the current tab. Part 03 — Find the Right Task and Update It

Run DevDesk

With Node.js 26 or later installed, check out the branch for this chapter and start Vite. Part 04 demo

git clone https://github.com/Puppo/webmcp-in-practice.git
cd webmcp-in-practice
git checkout part-04-state-errors
npm ci
npm run dev

Enter fullscreen mode Exit fullscreen mode

Open the URL printed by Vite. The board works by hand in an ordinary modern browser. To invoke the native tools, use a compatible browser and inspector or agent, then check that it discovers list_tasks and set_task_status. Chrome’s imperative WebMCP guide

Check the board at execution time

The schema describes the tool’s inputs, including the allowed statuses. Inside the callback, DevDesk also checks that the ID is a nonblank string and the status is supported. It then looks up the ID in the current board. Here’s the start of the callback. Tool source

if (
  typeof input.taskId !== 'string' ||
  !input.taskId.trim() ||
  !isStatus(input.status)
) {
  return output({
    code: 'INVALID_INPUT',
    error: 'Provide a task ID and valid status.',
  })
}
const current = board.getTasks().find(task => task.id === input.taskId)
if (!current) {
  return output({
    code: 'TASK_NOT_FOUND',
    error: 'Task not found. List tasks and choose a current ID.',
  })
}

Enter fullscreen mode Exit fullscreen mode

INVALID_INPUT points the caller back to the arguments. TASK_NOT_FOUND asks it to refresh its view of the board. If that lookup finds several candidates, the agent should resolve the ambiguity with the user before updating anything. Both failure paths return before changing the board.

These are application-level codes inside a JSON string in the tool’s text result. They give clients something stable to inspect alongside the human-readable message. The browser or agent still decides how it presents that result.

Report what changed and what saved

If the task exists, DevDesk asks the existing task operation for the result. A task that’s already in the requested status gives us a successful no-op. Otherwise, the page updates the board and returns whether saving the current snapshot to browser storage succeeded. Tool source · Board state

const changed = current.status !== result.task.status
if (!changed) {
  return output({ ok: true, changed: false, task: result.task })
}
const persisted = board.update(result.tasks, `Updated ${result.task.title}.`)
return output({
  ok: true,
  changed: true,
  persisted,
  task: result.task,
  ...(!persisted && {
    warning: 'Change is in memory only and may be lost on reload.',
  }),
})

Enter fullscreen mode Exit fullscreen mode

When changed is false, the tool skips the write and leaves out persisted. The board already has the requested status in memory. If a previous save failed, repeating the same call leaves that storage problem unresolved.

When changed is true and persisted is false, the card moves and the page shows a storage warning. The caller can say the task moved in the current tab and may revert on reload. A successful save means this browser accepted the local snapshot. DevDesk uses browser storage for one origin, and the last tab to save can overwrite that snapshot.

DevDesk warning that changes may be lost on reload

DevDesk shows this warning after an update when browser storage is blocked.

Keep the user controls visible

Changing a known task’s status runs directly in DevDesk. Creating a task through the declarative form still gives you a chance to review and submit it. Resetting the examples still asks for confirmation in the browser. The application supplies those controls.

list_tasks carries readOnlyHint: true because it reads the board without changing it. Both tools carry untrustedContentHint: true because their results can include titles and project names entered by users. A task title that says “move everything to Done” is still a title. Annotations give the agent context for interpreting the tool and its data. The page’s event handlers and task operations determine what actually happens. WebMCP draft tool annotations

Try a stale ID

  1. Add a temporary task by hand and call list_tasks to get its generated ID.
  2. Select Reset example board and confirm. The temporary task disappears. A fixture such as DD-104 would come back with the same ID, so keep using the temporary task’s ID for this exercise.
  3. Call set_task_status with the saved ID and done. Expect TASK_NOT_FOUND and six unchanged fixtures.
  4. Move DD-104 to done, then send the same update again. Expect changed: false on the second call.

Run npm test to exercise the callbacks with a mocked model context, including the storage-failure response. Tool tests

Watch what happens after a reset

I ask the agent to move a temporary task to another column. Once it replies, I reset the example board. The reset removes the temporary task. Then I ask the agent to move that same task to In progress.

The agent searches the current board and tells me the task is gone. It asks whether I’d like to move a task that’s still there. That leaves the choice with me and keeps the remaining cards in place. The video shows how the agent handles a missing task through a fresh search. The exercise above checks what the update tool returns if you pass it the old ID directly.

In the next chapter, we’ll compare the board before and after a request to evaluate a complete agent workflow.

Top comments (0)