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

server-input.md

92 lines2.9 KB

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, for example a yes before a delete."
6keywords:
7 - tanstack ai
8 - mcp
9 - model context protocol
10 - requestInput
11 - confirmation
12 - input required
13 - MCPToolContext
14---
15​
16An MCP tool can stop and ask the user before it runs. Use that when the tool deletes a file or sends data.
17​
18Call `ctx.context.requestInput` inside the tool. The host shows your message to the user, and the call returns the answer.
19​
20## Quick setup
21​
22You need an MCP server first. If you do not have one, start with [Serve Tools over HTTP](./server).
23​
241. Give `.server()` the type `MCPToolContext`, so `ctx.context.requestInput` type-checks.
252. Call `requestInput` with the message for the user.
263. Act on the answer.
27​
28```ts
29import { toolDefinition } from '@tanstack/ai'
30import { createMCPServer } from '@tanstack/ai-mcp/server'
31import type { MCPToolContext } from '@tanstack/ai-mcp/server'
32import { z } from 'zod'
33​
34const deleteFile = toolDefinition({
35 name: 'delete_file',
36 description: 'Delete a file after the user says yes',
37 inputSchema: z.object({
38 path: z.string(),
39 }),
40}).server<MCPToolContext>(async ({ path }, ctx) => {
41 const answer = await ctx.context.requestInput({
42 message: `Delete ${path}? Type yes to continue.`,
43 })
44​
45 if (answer !== 'yes') {
46 return `Kept ${path}.`
47 }
48​
49 return `Deleted ${path}.`
50})
51​
52const server = createMCPServer({
53 name: 'files',
54 version: '1.0.0',
55 tools: [deleteFile],
56})
57​
58export function fetch(request: Request) {
59 return server.fetch(request)
61```
62​
63If the user sends `yes`, the tool returns `Deleted` plus the file path. Any other answer returns `Kept` plus the path.
64​
65## How it works
66​
67- `ctx` is the second argument of the function that you pass to `.server()`.
68- `requestInput` returns the string that the user sends.
69- If the user declines or cancels, `requestInput` throws an Error. The tool call then ends with a tool error.
70​
71A TanStack `chat()` host pauses on the request and shows it to the user. [MCP Client Input](../tools/mcp-input) shows how the user answers.
72​
73## Advanced
74​
75### Spec 2025 and spec 2026
76​
77The two MCP spec versions ask for input in different ways.
78​
79On spec 2025:
80​
81- `requestInput` needs `sessions: 'memory'`. Without a session, it throws. See [MCP Server Sessions](./server-sessions).
82- `requestInput` waits on the open session. The same tool call then continues with the answer.
83​
84On spec 2026:
85​
86- The handler returns `input_required`. Then the client runs the tool again with the answer.
87- The code before `requestInput` runs on each call, so it can run more than once. If work must run once, put it after `requestInput` returns.
88- A tool asks one question per call. A second `requestInput` in the same call throws an Error. To ask two questions, use two tools.
89​
90### Task tools
91​
92`requestInput` does not work in an `execution: 'task'` tool. [MCP Server Tasks](./server-tasks) has the details.