Open History to see the last commit
An MCP tool can stop and ask the user before it runs. Use that when the tool deletes a file or sends data.
Call ctx.context.requestInput inside the tool. The host shows your message to the user, and the call returns the answer.
Quick setup#
You need an MCP server first. If you do not have one, start with Serve Tools over HTTP.
- Give
.server()the typeMCPToolContext, soctx.context.requestInputtype-checks. - Call
requestInputwith the message for the user. - Act on the answer.
import { toolDefinition } from '@tanstack/ai'
import { createMCPServer } from '@tanstack/ai-mcp/server'
import type { MCPToolContext } from '@tanstack/ai-mcp/server'
import { z } from 'zod'
const deleteFile = toolDefinition({
name: 'delete_file',
description: 'Delete a file after the user says yes',
inputSchema: z.object({
path: z.string(),
}),
}).server<MCPToolContext>(async ({ path }, ctx) => {
const answer = await ctx.context.requestInput({
message: `Delete ${path}? Type yes to continue.`,
})
if (answer !== 'yes') {
return `Kept ${path}.`
}
return `Deleted ${path}.`
})
const server = createMCPServer({
name: 'files',
version: '1.0.0',
tools: [deleteFile],
})
export function fetch(request: Request) {
return server.fetch(request)
}
If the user sends yes, the tool returns Deleted plus the file path. Any other answer returns Kept plus the path.
How it works#
ctxis the second argument of the function that you pass to.server().requestInputreturns the string that the user sends.- If the user declines or cancels,
requestInputthrows an Error. The tool call then ends with a tool error.
A TanStack chat() host pauses on the request and shows it to the user. MCP Client Input shows how the user answers.
Advanced#
Spec 2025 and spec 2026#
The two MCP spec versions ask for input in different ways.
On spec 2025:
requestInputneedssessions: 'memory'. Without a session, it throws. See MCP Server Sessions.requestInputwaits on the open session. The same tool call then continues with the answer.
On spec 2026:
- The handler returns
input_required. Then the client runs the tool again with the answer. - The code before
requestInputruns on each call, so it can run more than once. If work must run once, put it afterrequestInputreturns. - A tool asks one question per call. A second
requestInputin the same call throws an Error. To ask two questions, use two tools.
Task tools#
requestInput does not work in an execution: 'task' tool. MCP Server Tasks has the details.