Open History to see the last commit
1---
2title: MCP Client Input
3id: mcp-input
4order: 12
5description: "When an MCP server asks for input during a tool call, chat() pauses so the UI can show the request and send the answer."
6keywords:
7 - tanstack ai
8 - mcp
9 - interrupt
10 - form
11 - sampling
12---
13
14An MCP server can pause a tool call and ask for input. It asks for one of these:
15
16- A form that the user fills in.
17- A sampling request that needs a model reply.
18
19You want to show that request in your UI and send the answer back to the server. `chat()` does this with an interrupt: the run pauses, the UI answers, and the next run calls the tool again with the answer.
20
21## Quick setup
22
23You need [MCP Server Tools](./mcp) working first, with the client in `mcp.clients`.
24
251. In your chat route, pass `parentRunId` and `resume` from the request to `chat()`:
26
27```ts
28// app/api/chat/route.ts
29import {
30 chat,
31 chatParamsFromRequest,
32 toServerSentEventsResponse,
33} from '@tanstack/ai'
34import { openaiText } from '@tanstack/ai-openai'
35import { createMCPClient } from '@tanstack/ai-mcp'
36
37export async function POST(request: Request) {
38 const params = await chatParamsFromRequest(request)
39 const mcp = await createMCPClient({
40 transport: {
41 type: 'http',
42 url: 'https://my-mcp-server.example.com/mcp',
43 },
44 })
45
46 const stream = chat({
47 adapter: openaiText('gpt-6.1-sol'),
48 messages: params.messages,
49 threadId: params.threadId,
50 runId: params.runId,
51 ...(params.parentRunId ? { parentRunId: params.parentRunId } : {}),
52 ...(params.resume ? { resume: params.resume } : {}),
53 mcp: { clients: [mcp] },
54 })
55 return toServerSentEventsResponse(stream)
56}
57```
58
592. In the UI, read `interrupts` from `useChat`. For each MCP input interrupt, show the request and call `resolveInterrupt` with the answer, or call `cancel()`:
60
61```tsx
62import { useState } from 'react'
63import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
64
65function isRecord(value: unknown): value is Record<string, unknown> {
66 return typeof value === 'object' && value !== null && !Array.isArray(value)
67}
68
69function readMcpInput(
70 metadata: Readonly<Record<string, unknown>> | undefined,
71) {
72 const payload = metadata?.['tanstack:interruptPayload']
73 if (!isRecord(payload)) return undefined
74 const kind = payload.kind
75 if (kind !== 'form' && kind !== 'sampling') return undefined
76 if (!isRecord(payload.request)) return undefined
77 return { kind, request: payload.request }
78}
79
80export function McpInputPrompt() {
81 const [answer, setAnswer] = useState('')
82 const { interrupts } = useChat({
83 threadId: 'thread-1',
84 connection: fetchServerSentEvents('/api/chat'),
85 })
86
87 return (
88 <>
89 {interrupts.map((interrupt) => {
90 if (!interrupt.id.startsWith('mcp_input_')) return null
91 if (interrupt.kind !== 'generic') return null
92 const input = readMcpInput(interrupt.metadata)
93 if (!input) return null
94 return (
95 <form
96 key={interrupt.id}
97 onSubmit={(event) => {
98 event.preventDefault()
99 interrupt.resolveInterrupt({ value: answer })
100 }}
101 >
102 <label>
103 {String(input.request.message ?? interrupt.message)}
104 <input
105 value={answer}
106 onChange={(event) => setAnswer(event.target.value)}
107 />
108 </label>
109 <button type="submit">Send</button>
110 <button type="button" onClick={() => interrupt.cancel()}>
111 Cancel
112 </button>
113 </form>
114 )
115 })}
116 </>
117 )
118}
119```
120
121Try it. When a tool asks for input, the form shows the question from the server. Type an answer and click **Send**. The tool call continues with your answer.
122
123## How it works
124
1251. The MCP server asks for input during a tool call.
1262. `chat()` ends the run with an interrupt. `outcome.type` is `interrupt`, and the interrupt id starts with `mcp_input_`.
1273. `useChat` shows the interrupt. The user answers.
1284. `useChat` sends the answer in a new run.
1295. `chat()` runs the tool again, and the MCP client sends the answer to the server.
130
131The pause and the resume work on spec 2026. On spec 2025, the server asks the client for input in the middle of the tool call. `chat()` cannot pause that call, so the tool call fails.
132
133## What to send
134
135The answer depends on the `kind` of the request.
136
137- `form`: send an object that matches `request.requestedSchema`. A server from `createMCPServer` asks for `{ value: string }`. The MCP client sends the object as the accepted content.
138- `sampling`: send the model reply as a string. You can also send a full MCP `CreateMessageResult`.
139- `cancel()`: for a form, the server gets `{ action: 'cancel' }`. A `createMCPServer` tool then ends with a tool error. For a sampling request, the tool call ends with an error.
140
141To decline a form, send the full MCP answer: `{ action: 'decline' }`.
142
143## Advanced
144
145### Read the pause on the server
146
147The stream ends with one `RUN_FINISHED` chunk. You can read the interrupt there:
148
149```ts
150import { chat } from '@tanstack/ai'
151import { openaiText } from '@tanstack/ai-openai'
152import { createMCPClient } from '@tanstack/ai-mcp'
153
154const messages = [{ role: 'user' as const, content: 'Ask the server' }]
155
156const mcp = await createMCPClient({
157 transport: {
158 type: 'http',
159 url: 'https://my-mcp-server.example.com/mcp',
160 },
161})
162
163const stream = chat({
164 adapter: openaiText('gpt-6.1-sol'),
165 messages,
166 threadId: 'thread-1',
167 mcp: { clients: [mcp] },
168})
169
170for await (const chunk of stream) {
171 if (chunk.type !== 'RUN_FINISHED') continue
172 if (chunk.outcome?.type !== 'interrupt') continue
173
174 for (const interrupt of chunk.outcome.interrupts) {
175 if (!interrupt.id.startsWith('mcp_input_')) continue
176 const payload = interrupt.metadata?.['tanstack:interruptPayload']
177 console.log(interrupt.id, payload)
178 }
179}
180```
181
182During the pause:
183
184- The input call has no tool result.
185- The run does not emit `RUN_ERROR`.
186- When other tools finish in the same turn, the stream has their results.
187
188### Interrupt fields
189
190| Field | Value |
191|---|---|
192| `id` | `mcp_input_` and the tool call id. Find the pause by this id. |
193| `reason` | Display text. The default is `mcp_input`. See the rules below. |
194| `message` | `Input required to run` and the tool name. |
195| `metadata.toolName` | The tool name. |
196| `metadata['tanstack:interruptPayload']` | `kind` and `request`. `kind` is `form` or `sampling`. `request` is the MCP input body. |
197
198Rules for `reason`:
199
200- A tool can set another string, for example `auth_required`.
201- An empty reason stays `mcp_input`.
202- The reserved reason `tanstack:client_tool_execution` also stays `mcp_input`.
203
204### Answers on the resumed tool
205
206- A resumed tool gets the answer on `ctx.inputResponse`. The MCP tools read it for you. Your own server tool can read it too.
207- The client answers one input request for each tool call. If the server asks for input again after it gets the answer, the tool call ends with a tool error.
208
209### Which errors pause the run
210
211If a tool throws a plain `Error`, the result is a tool error. The run does not pause.
212
213`chat()` pauses only for a thrown value with this shape:
214
215- `name`: `MCPInputRequiredError`
216- `kind`: `form` or `sampling`
217- `request`: the MCP input body
218- `reason` (optional): the `reason` of the interrupt. The default is `'mcp_input'`.
219
220A tool can throw this shape with its own `reason`, for example `auth_required`. The interrupt id stays `mcp_input_<toolCallId>`, and the resume uses the same id. `reason` is only display text, so find the pause by its id.
221
222### Outside chat()
223
224Outside `chat()`, the client throws `MCPInputRequiredError`. In this sample, `ask` is the tool name on your server.
225
2261. Call `tools()` on the MCP client.
2272. Call `execute` on the tool.
2283. Test the error with `isMCPInputRequiredError`, then read `kind` and `request`.
2294. Close the client after the call.
230
231```ts
232import { createMCPClient, isMCPInputRequiredError } from '@tanstack/ai-mcp'
233
234export async function callAsk() {
235 const mcp = await createMCPClient({
236 transport: {
237 type: 'http',
238 url: 'https://my-mcp-server.example.com/mcp',
239 },
240 })
241
242 try {
243 const tools = await mcp.tools()
244 const ask = tools.find((tool) => tool.name === 'ask')
245 if (!ask?.execute) return
246 await ask.execute({})
247 } catch (error) {
248 if (!isMCPInputRequiredError(error)) throw error
249 console.log(error.kind, error.request)
250 } finally {
251 await mcp.close()
252 }
253}
254```