Open History to see the last commit
1---
2title: Ask for Input
3id: mcp-server-input
4order: 13
5description: "Ask the user for a value inside an MCP server tool with requestInput."
6keywords:
7 - tanstack ai
8 - mcp
9 - model context protocol
10 - requestInput
11 - confirmation
12 - input required
13 - MCPToolContext
14---
15
16Before your tool deletes a file, your tool must get a confirmation from the user. Call `requestInput` on `ctx.context` for that confirmation.
17
18`ctx` is the second argument of the function you pass to `.server()`. Give `.server()` the type `MCPToolContext`, so `ctx.context.requestInput` type-checks.
19
20```ts
21import { toolDefinition } from '@tanstack/ai'
22import { createMCPServer } from '@tanstack/ai-mcp/server'
23import type { MCPToolContext } from '@tanstack/ai-mcp/server'
24import { z } from 'zod'
25
26const deleteFile = toolDefinition({
27 name: 'delete_file',
28 description: 'Delete a file after the user says yes',
29 inputSchema: z.object({
30 path: z.string(),
31 }),
32}).server<MCPToolContext>(async ({ path }, ctx) => {
33 const answer = await ctx.context.requestInput({
34 message: `Delete ${path}? Type yes to continue.`,
35 })
36
37 if (answer !== 'yes') {
38 return `Kept ${path}.`
39 }
40
41 return `Deleted ${path}.`
42})
43
44const server = createMCPServer({
45 name: 'files',
46 version: '1.0.0',
47 tools: [deleteFile],
48})
49
50export function fetch(request: Request) {
51 return server.fetch(request)
52}
53```
54
55`requestInput` returns the string that the user sends.
56
57If the user declines or cancels, `requestInput` throws an Error. The tool call then ends with a tool error.
58
59## Spec 2025 and spec 2026
60
61On spec 2025, `requestInput` waits on the open session. The same tool call then continues with the answer.
62
63On spec 2026, the handler returns `input_required`. Then the client runs the tool again with the answer. On spec 2026, the code before `requestInput` runs on each call, so it can run more than once.
64
65If the work must run once, put that work after `requestInput` returns.
66
67On spec 2026, a tool asks one question per call. A second `requestInput` in the same call throws an Error. To ask two questions, use two tools.
68
69A TanStack `chat()` host pauses on `input_required` and shows the request to the user. [MCP Client Input](../tools/mcp-input) shows how the user answers.
70
71`requestInput` does not work in an `execution: 'task'` tool. [MCP Server Tasks](./server-tasks) has the details.
72
73If the user sends `yes`, the tool returns `Deleted` plus the file path.