> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Run a tool as a task

> Offer a mutation that submits a task as an MCP task, with progress, input and cancellation.

A mutation tool whose function submits a task declares it with `task`. The
server then needs `tasks.key`, a MAC key that binds each task ID to the
caller who started it.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, http, label, mutation, role, secret, t, target } from "@bijectionhq/bijection";
import { mcp, tool } from "@bijectionhq/library-mcp";

const analyst = role({ clear: [] });
const rendererToken = secret();

export const render = target({
  label: label.public,
  via: http("https://render.example.com/jobs", { bearer: rendererToken }),
  args: { quarter: t.string },
  returns: { url: t.string },
  within: { hours: 1 },
});

export const start = mutation(
  { allow: [analyst], args: { quarter: t.string }, returns: t.task(render) },
  (ctx, { quarter }) => ctx.submit(render, { quarter }, { once: ["render", quarter] }),
);

const tasksKey = secret({ alg: "hmac-sha256", uses: ["mac", "equal"] });
const server = mcp({
  server: { name: "reports", version: "1.0.0" },
  tools: { render: tool(start, { description: "Render the quarterly report", task: {} }) },
  tasks: { key: tasksKey, lifetime: { days: 7 } },
});

export default app({
  access: { analyst },
  reports: { render, start, rendererToken, tasksKey },
  mcp: server,
});
```

Set both secrets as an operator before the server takes task calls:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection secret set reports.tasksKey
bijection secret set reports.rendererToken
```

`task` takes these options, all optional:

| Option | Meaning |
| - | - |
| `of(value)` | The task in the function's value. By default the value is the task. |
| `status(ctx, task)` | `{ progress, total, message }` for a working task, read in the caller's frame. |
| `inputs(ctx, task)` | The pending tasks of role-performed targets the client may answer. |
| `cancel(ctx, task)` | Cancel the task. By default `ctx.withdraw(task, { message })`. |
| `mode: "required"` | Refuse a client without the Tasks extension with `-32021`. |
| `present(value)` | Extra content blocks for the task's value. |

`tasks.lifetime` bounds how long a task ID stays valid and sets `ttlMs`.
Without it, `ttlMs` is `null`. `tasks.poll` suggests the polling interval,
five seconds by default. Both are `Temporal` durations.

## Task states

When the client declares the `io.modelcontextprotocol/tasks` extension, the
call returns a task handle once the submit commits. `tasks/get` reads
`ctx.get(task)` in the caller's frame:

| Task | MCP state |
| - | - |
| `pending` | `working`, with `status` progress when declared |
| `pending`, with an open declared input | `input_required` |
| settled with `{ ok: true, value }` | `completed`, with the value presented |
| settled `Withdrawn` | `cancelled` |
| settled with any other refusal | `completed` with `isError: true` and the typed refusal |

`tasks/cancel` runs `cancel`. Cancellation is cooperative: a task that has
already settled keeps its result. A client can also list `taskIds` on
`subscriptions/listen` to receive `notifications/tasks`
([Request lifecycle](https://docs.bijection.com/mcp/sessions.md)).

## Ask the client for input

A task that waits on a person names that wait as a target performed by
roles, `target({ via: [roles], within })`. List the pending tasks in
`inputs`, each with a form message and a flat `requestedSchema`:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const manager = role({ clear: [] });

export const approve = target({
  via: [manager],
  args: { quarter: t.string },
  returns: { approved: t.boolean },
  within: { days: 2 },
});

// One fact per report task: the approval it waits on, inserted by `start`.
export const waits = table({ report: t.task(render), approval: t.task(approve) }, { key: ["report"] });

tool(start, {
  description: "Render the quarterly report after approval",
  task: {
    inputs: async (ctx, task) => {
      const wait = await ctx.get(waits(task));
      return wait === null ? [] : [{
        task: wait.approval,
        message: "Approve the quarterly report?",
        requestedSchema: { type: "object", properties: { approved: { type: "boolean" } }, required: ["approved"] },
      }];
    },
  },
});
```

While one is open, the MCP task is `input_required` and its `inputRequests`
are keyed by the input's slot. `tasks/update` answers it as
`ctx.answer(task, { ok: true, value })`, with the form content or what the
input's `answer(response)` maps it to. The answer is an ordinary act: the
caller must hold one of the target's roles, and guards on
`act.answer(approve)` judge it. Keys that are unknown or already answered
are ignored. A pending task the tool does not list keeps the MCP task
`working`.

## Clients without the extension

A client that does not declare the extension receives the function's own
value. If it accepts a streamed response and sends
`params._meta.progressToken`, the response stays open and reports
`notifications/progress` from `status` before that value. A tool that runs
as one transaction reports no progress.

## Follow a task from a view

A view's protocol subset guarantees `tools/call` but not `tasks/*`. When a
server has a task tool and a tool that views can reach, it adds two tools
that only views see: `task_status({ taskId })`, which answers what
`tasks/get` answers as `structuredContent`, and `task_cancel({ taskId })`.
Both use the same caller-bound task ID. A task tool that views can reach
carries that ID in `_meta["com.bijection/taskId"]`. The names `task_status`
and `task_cancel` are reserved on such a server.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.