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, 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 stop a tool call. The server asks for input.
15
16- A form needs an answer from the user.
17- A sampling request needs a model reply.
18
19You want that request in the UI, and you want the answer to reach the server. `chat()` ends the run with an interrupt. `outcome.type` is `interrupt`. The payload has `kind` and `request`. When the user answers, the next run calls the tool again with the answer.
20
21## Read the pause
22
23Pass the MCP client to `chat()` in `mcp.clients`. [MCP Server Tools](./mcp) has that setup.
24
25The stream ends on one `RUN_FINISHED` chunk. Read the interrupt there:
26
27```ts
28import { chat } from '@tanstack/ai'
29import { openaiText } from '@tanstack/ai-openai'
30import { createMCPClient } from '@tanstack/ai-mcp'
31
32const messages = [{ role: 'user' as const, content: 'Ask the server' }]
33
34const mcp = await createMCPClient({
35 transport: {
36 type: 'http',
37 url: 'https://my-mcp-server.example.com/mcp',
38 },
39})
40
41const stream = chat({
42 adapter: openaiText('gpt-5.5'),
43 messages,
44 mcp: { clients: [mcp] },
45})
46
47for await (const chunk of stream) {
48 if (chunk.type !== 'RUN_FINISHED') continue
49 if (chunk.outcome?.type !== 'interrupt') continue
50
51 for (const interrupt of chunk.outcome.interrupts) {
52 if (!interrupt.id.startsWith('mcp_input_')) continue
53 const payload = interrupt.metadata?.['tanstack:interruptPayload']
54 console.log(interrupt.id, payload)
55 }
56}
57```
58
59The pause follows these rules:
60
61- The input call has no tool result.
62- The run does not emit `RUN_ERROR`.
63- When other tools finish in that turn, the stream has those results.
64
65The interrupt has these fields:
66
67- `id`: `mcp_input_` and the tool call id. Read the pause by this id.
68- `reason`: display text. The default is `mcp_input`. A tool can set another string, for example `auth_required`. An empty reason stays `mcp_input`. These reasons also stay `mcp_input`: `approval_required`, `client_tool_input`, and `tanstack:client_tool_execution`.
69- `message`: `Input required to run` and the tool name
70- `metadata.toolName`: the tool name
71- `metadata['tanstack:interruptPayload']`: `kind` and `request`
72
73`kind` is `form` or `sampling`. `request` is the MCP input body.
74
75## Answer the request
76
77The answer comes back in a new request. Pass `parentRunId` and `resume` from that request to `chat()`:
78
79```ts
80// app/api/chat/route.ts
81import {
82 chat,
83 chatParamsFromRequest,
84 toServerSentEventsResponse,
85} from '@tanstack/ai'
86import { openaiText } from '@tanstack/ai-openai'
87import { createMCPClient } from '@tanstack/ai-mcp'
88
89export async function POST(request: Request) {
90 const params = await chatParamsFromRequest(request)
91 const mcp = await createMCPClient({
92 transport: {
93 type: 'http',
94 url: 'https://my-mcp-server.example.com/mcp',
95 },
96 })
97
98 const stream = chat({
99 adapter: openaiText('gpt-5.5'),
100 messages: params.messages,
101 threadId: params.threadId,
102 runId: params.runId,
103 ...(params.parentRunId ? { parentRunId: params.parentRunId } : {}),
104 ...(params.resume ? { resume: params.resume } : {}),
105 mcp: { clients: [mcp] },
106 })
107 return toServerSentEventsResponse(stream)
108}
109```
110
111Then answer the request in the UI:
112
1131. Read `interrupts` from `useChat`.
1142. Find the item where `reason` is `mcp_input`.
1153. Read `metadata['tanstack:interruptPayload']` and show the request.
1164. Call `resolveInterrupt` with the answer, or call `cancel()`.
117
118```tsx
119import { useState } from 'react'
120import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
121
122function isRecord(value: unknown): value is Record<string, unknown> {
123 return typeof value === 'object' && value !== null && !Array.isArray(value)
124}
125
126function readMcpInput(
127 metadata: Readonly<Record<string, unknown>> | undefined,
128) {
129 const payload = metadata?.['tanstack:interruptPayload']
130 if (!isRecord(payload)) return undefined
131 const kind = payload.kind
132 if (kind !== 'form' && kind !== 'sampling') return undefined
133 if (!isRecord(payload.request)) return undefined
134 return { kind, request: payload.request }
135}
136
137export function McpInputPrompt() {
138 const [answer, setAnswer] = useState('')
139 const { interrupts } = useChat({
140 threadId: 'thread-1',
141 connection: fetchServerSentEvents('/api/chat'),
142 })
143
144 return (
145 <>
146 {interrupts.map((interrupt) => {
147 if (!interrupt.id.startsWith('mcp_input_')) return null
148 if (interrupt.kind !== 'generic') return null
149 const input = readMcpInput(interrupt.metadata)
150 if (!input) return null
151 return (
152 <form
153 key={interrupt.id}
154 onSubmit={(event) => {
155 event.preventDefault()
156 interrupt.resolveInterrupt({ value: answer })
157 }}
158 >
159 <label>
160 {String(input.request.message ?? interrupt.message)}
161 <input
162 value={answer}
163 onChange={(event) => setAnswer(event.target.value)}
164 />
165 </label>
166 <button type="submit">Send</button>
167 <button type="button" onClick={() => interrupt.cancel()}>
168 Cancel
169 </button>
170 </form>
171 )
172 })}
173 </>
174 )
175}
176```
177
178`useChat` sends the answer in a new run. Then `chat()` runs the tool again, and the MCP client sends the answer to the server.
179
180## What to send
181
182The answer depends on `kind`.
183
184- `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.
185- `sampling`: send the model reply as a string. You can also send a full MCP `CreateMessageResult`.
186- `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.
187
188To decline a form, send the full MCP answer: `{ action: 'decline' }`.
189
190A resumed tool gets the answer on `ctx.inputResponse`. The MCP tools read it for you. Your own server tool can read it too.
191
192The client answers one input request per tool call. If the server asks for input again after it gets the answer, the tool call ends with a tool error.
193
194The 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.
195
196## A tool error
197
198If the thrown value is a plain `Error`, the result is a tool error. The run does not pause.
199
200`chat()` pauses only for this shape:
201
202- `name`: `MCPInputRequiredError`
203- `kind`: `form` or `sampling`
204- `request`: the MCP input body
205- `reason` (optional): the `reason` of the interrupt. The default is `'mcp_input'`.
206
207A tool can throw this shape with its own `reason`, for example `auth_required`. The interrupt id stays `mcp_input_<toolCallId>`. Read the pause by that id. `reason` is display text. The resume uses the same id.
208
209## Outside chat()
210
211Outside `chat()`, the client throws `MCPInputRequiredError`. The name `ask` is the tool name on your server.
212
2131. Call `tools()` on the MCP client.
2142. Call `execute` on the tool.
2153. Check the error with `isMCPInputRequiredError`, then read `kind` and `request`.
2164. Close the client after the call.
217
218```ts
219import { createMCPClient, isMCPInputRequiredError } from '@tanstack/ai-mcp'
220
221export async function callAsk() {
222 const mcp = await createMCPClient({
223 transport: {
224 type: 'http',
225 url: 'https://my-mcp-server.example.com/mcp',
226 },
227 })
228
229 try {
230 const tools = await mcp.tools()
231 const ask = tools.find((tool) => tool.name === 'ask')
232 if (!ask?.execute) return
233 await ask.execute({})
234 } catch (error) {
235 if (!isMCPInputRequiredError(error)) throw error
236 console.log(error.kind, error.request)
237 } finally {
238 await mcp.close()
239 }
240}
241```