Open History to see the last commit
1---
2title: MCP Server Tasks
3id: server-tasks
4order: 13
5description: "Let a long MCP tool return a task handle at once, 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
18A task tool returns a task id and finishes later. The host polls that id instead of holding the HTTP call open.
19
20Set `execution: 'task'` on the tool. The call returns a task handle at once, and the host polls for the result.
21
22## Quick setup
23
24You need an MCP server first. If you do not have one, start with [Serve Tools over HTTP](./server). This setup is for one long-lived process, such as a Node server. For a Worker, see [Run tasks on a Worker](#run-tasks-on-a-worker).
25
261. Set `execution: 'task'` on the tool definition.
272. Return the final value from the tool function.
283. Create the server once, outside the request handler.
29
30```ts
31import { toolDefinition } from '@tanstack/ai'
32import { createMCPServer } from '@tanstack/ai-mcp/server'
33
34const buildReport = toolDefinition({
35 name: 'build_report',
36 description: 'Build a long report',
37 execution: 'task',
38}).server(async () => {
39 const text = await writeReport()
40 return { text }
41})
42
43async function writeReport() {
44 await new Promise((resolve) => {
45 setTimeout(resolve, 5000)
46 })
47 return 'Quarterly report'
48}
49
50const server = createMCPServer({
51 name: 'reports',
52 version: '1.0.0',
53 tools: [buildReport],
54})
55
56export function handleMcp(request: Request) {
57 return server.fetch(request)
58}
59```
60
61The host calls `build_report` and gets a task handle at once. After 5 seconds, the status is `completed`, and the host reads `{ text: 'Quarterly report' }` from `structuredContent`.
62
63## How it works
64
651. The host calls the tool.
662. The handler returns a task handle before the work ends.
673. The tool function continues in this process. There is no queue.
684. When the tool function ends, the task store saves the value.
695. The host polls until the status is `completed` or `failed`.
70
71The default task store keeps each task in memory for this process. A TanStack AI host polls until the task ends. See [MCP Server Tools](../tools/mcp).
72
73## What a task tool can use
74
75The task keeps running after the call returns the task handle. As a result, the task cannot use the request that started it:
76
77- `ctx.context.requestInput` throws an error. Ask for input in a tool without `execution: 'task'`.
78- `ctx.context.sample` calls the `sample` option of `createMCPServer`. If you do not pass `sample`, it throws an error.
79- `ctx.abortSignal` does not abort when the call request ends.
80
81## Run tasks on a Worker
82
83A Worker can stop when the response is sent. It can also build a new server for each request. Pass two more options:
84
85- `taskStore`: create it once with `inMemoryTaskStore`, outside `fetch`. A new store on each request has no record of the old task.
86- `waitUntil`: pass `waitUntil` from the Worker context. Without it, a Worker can stop before the tool function ends.
87
88```ts
89import { toolDefinition } from '@tanstack/ai'
90import { createMCPServer, inMemoryTaskStore } from '@tanstack/ai-mcp/server'
91
92const taskStore = inMemoryTaskStore()
93
94const buildReport = toolDefinition({
95 name: 'build_report',
96 description: 'Build a long report',
97 execution: 'task',
98}).server(async () => {
99 const text = await writeReport()
100 return { text }
101})
102
103async function writeReport() {
104 await new Promise((resolve) => {
105 setTimeout(resolve, 5000)
106 })
107 return 'Quarterly report'
108}
109
110export default {
111 async fetch(
112 request: Request,
113 _env: unknown,
114 ctx: { waitUntil(promise: Promise<unknown>): void },
115 ) {
116 const server = createMCPServer({
117 name: 'reports',
118 version: '1.0.0',
119 tools: [buildReport],
120 taskStore,
121 waitUntil: (promise) => ctx.waitUntil(promise),
122 })
123 return server.fetch(request)
124 },
125}
126```
127
128The server calls `waitUntil` with the tool promise. This promise settles after the store saves the tool result or the tool error.
129
130## Advanced
131
132### Share the store across instances
133
134If more than one instance can receive `tasks/get`, pass a `taskStore` that those instances share. A task store has three methods:
135
136- `get`: read one task by id. Return `null` when the id is absent.
137- `set`: save one task.
138- `delete`: remove one task.
139
140`@tanstack/ai-mcp/server` exports `inMemoryTaskStore` and the `TaskStore` type.
141
142### Spec 2025 and spec 2026
143
144Tasks are part of spec `2025-11-25`. Spec `2026-07-28` has no tasks yet.
145
146| Spec | What the call returns |
147| --- | --- |
148| `2025-11-25` | A task handle. The host polls `tasks/get`, then reads `tasks/result`. |
149| `2026-07-28` | The tool result. The tool runs inline, and the call waits for it. |
150
151On spec 2025, the server sets `execution.taskSupport` to `required`. It also advertises the `tasks` capability for `tools/call`.
152
153### The task handle
154
155On spec 2025, the call response is the `CreateTaskResult` from the spec. The `task` field is a `Task` with status `working`.
156
157```json
158{
159 "content": [{ "type": "text", "text": "task-id" }],
160 "task": {
161 "taskId": "task-id",
162 "status": "working",
163 "ttl": null,
164 "createdAt": "2026-09-22T12:00:00.000Z",
165 "lastUpdatedAt": "2026-09-22T12:00:00.000Z"
166 }
167}
168```
169
170`tasks/get` returns the same `Task` fields:
171
172| Field | Value |
173| --- | --- |
174| `taskId` | The id from the call. |
175| `status` | `working`, `completed`, or `failed`. |
176| `ttl` | `null`. |
177| `createdAt` | The time of the first `set`. |
178| `lastUpdatedAt` | The time of the last `set`. |
179| `statusMessage` | The error text, when the status is `failed`. |
180
181### Task status
182
183| Status | Meaning |
184| --- | --- |
185| `working` | The tool function is still at work. |
186| `completed` | The store holds the tool result. |
187| `failed` | The tool function threw an error. |
188
189If the tool function throws, the task keeps the text of that error in `statusMessage`. If that error has no text, the task keeps `The tool failed.`
190
191### Read the result
192
193`tasks/result` wraps the value as `content`. When the value is an object, `tasks/result` also returns `structuredContent`.
194
195Errors:
196
197- If the status is not `completed`, `tasks/result` returns the error `Task result is not ready`.
198- An unknown task id on `tasks/get` returns the error `Task not found`.
199- 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 these values.