Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

server-tasks.md

150 lines5.1 KB

1---
2title: MCP Server Tasks
3id: server-tasks
4order: 13
5description: "A long MCP tool returns a task handle so the host can poll for the result."
6keywords:
7 - tanstack ai
8 - mcp
9 - model context protocol
10 - mcp server
11 - execution
12 - task handle
13 - createMCPServer
14 - inMemoryTaskStore
15 - waitUntil
16---
17​
18Your long tool holds the HTTP call until the work ends. You have no task id for the host to poll.
19​
20Set `execution: 'task'` on `toolDefinition`.
21​
22The handler returns a task handle before the work ends. The tool function continues in this process. There is no queue.
23​
24```ts
25import { toolDefinition } from '@tanstack/ai'
26import { createMCPServer, inMemoryTaskStore } from '@tanstack/ai-mcp/server'
27​
28const taskStore = inMemoryTaskStore()
29​
30const buildReport = toolDefinition({
31 name: 'build_report',
32 description: 'Build a long report',
33 execution: 'task',
34}).server(async () => {
35 const text = await writeReport()
36 return { text }
37})
38​
39async function writeReport() {
40 await new Promise((resolve) => {
41 setTimeout(resolve, 5000)
42 })
43 return 'Quarterly report'
45​
46export default {
47 async fetch(
48 request: Request,
49 _env: unknown,
50 ctx: { waitUntil(promise: Promise<unknown>): void },
51 ) {
52 const server = createMCPServer({
53 name: 'reports',
54 version: '1.0.0',
55 tools: [buildReport],
56 taskStore,
57 waitUntil: (promise) => ctx.waitUntil(promise),
58 })
59 return server.fetch(request)
60 },
62```
63​
64Your tool function returns the final value. When the tool function ends, the store saves that value.
65​
66## What a task tool can use
67​
68The task keeps running after the call returns the task handle. So the task does not use the request that started it.
69​
70- `ctx.context.requestInput` throws an error. Ask for input in a tool without `execution: 'task'`.
71- `ctx.context.sample` calls the `sample` option of `createMCPServer`. If you do not pass `sample`, `ctx.context.sample` throws an error.
72- `ctx.abortSignal` does not abort when the call request ends.
73​
74## Keep the Worker alive
75​
76If you run on a Worker, pass `waitUntil` from the Worker context.
77​
78The server calls `waitUntil` with the tool promise. After the store saves the tool result or the tool error, this promise settles.
79​
80If you omit `waitUntil`, a Worker can stop before the tool function ends. On one long-lived process, you can omit `waitUntil`.
81​
82## Keep the task record
83​
841. Create the store with `inMemoryTaskStore` outside `fetch`.
852. Pass this store as `taskStore`.
86​
87A new store on each request has no record of the old task.
88​
89The package `@tanstack/ai-mcp/server` exports `inMemoryTaskStore`. The default store keeps each task in memory for this process. If you build the server once, you can omit `taskStore`.
90​
91If more than one instance can receive `tasks/get`, pass a `taskStore` that those instances share.
92​
93- `get`: read one task by id.
94- `set`: save one task.
95- `delete`: remove one task.
96​
97## Spec 2025 and spec 2026
98​
99Tasks are part of spec `2025-11-25`. Spec `2026-07-28` has no tasks yet.
100​
101| Spec | What the call returns |
102| --- | --- |
103| `2025-11-25` | A task handle. The host polls `tasks/get`, then reads `tasks/result`. |
104| `2026-07-28` | The tool result. The tool runs inline, and the call waits for it. |
105​
106On spec 2025, the server sets `execution.taskSupport` to `required`, and advertises the `tasks` capability for `tools/call`.
107​
108## Read the result
109​
110On spec 2025, the call response is the `CreateTaskResult` from the spec. The `task` field is a `Task` with status `working`. This status means that the tool function is still at work.
111​
112```json
114 "content": [{ "type": "text", "text": "task-id" }],
115 "task": {
116 "taskId": "task-id",
117 "status": "working",
118 "ttl": null,
119 "createdAt": "2026-09-22T12:00:00.000Z",
120 "lastUpdatedAt": "2026-09-22T12:00:00.000Z"
121 }
123```
124​
125`tasks/get` returns the same `Task` fields:
126​
127- `taskId`: the id from the call.
128- `status`: `working`, `completed`, or `failed`.
129- `ttl`: `null`.
130- `createdAt`: the time of the first `set`.
131- `lastUpdatedAt`: the time of the last `set`.
132- `statusMessage`: the error text, when the status is `failed`.
133​
134`tasks/result` wraps the value as `content`. When the value is an object, `tasks/result` also returns `structuredContent`. If the status is not `completed`, `tasks/result` returns the error `Task result is not ready`.
135​
136A task uses one status:
137​
138- `working`: the tool function is still at work.
139- `completed`: the store holds the tool result.
140- `failed`: the tool function threw an error.
141​
142If the tool function throws, the task keeps the text from that error in `statusMessage`. If that error has no text, the task keeps `The tool failed.`
143​
144An unknown task id on `tasks/get` returns the error `Task not found`. When the server has `auth`, a task belongs to the caller that started it: the `clientId` of the token plus its `sub` claim. Another caller also gets `Task not found`. [MCP Server Auth](./server-auth) shows how the verifier sets them.
145​
146The host polls until the status is `completed` or `failed`. A TanStack AI host polls until the task ends.
147​
148See [MCP Server Tools](../tools/mcp).
149​
150After the status is `completed`, the host reads `{ text: 'Quarterly report' }` from `structuredContent`.