Open History to see the last commit
1import { test, expect } from './fixtures'
2
3/**
4 * An MCP tool that asks for input pauses chat(), and the answer resumes it.
5 *
6 * - `api.mcp-input-server` is a spec 2026 server from `createMCPServer`.
7 * Its `ask_city` tool calls `ctx.context.requestInput`.
8 * - `api.mcp-input-test` runs chat() with that tool. aimock makes the model
9 * call `ask_city`, then answer after the tool result.
10 *
11 * The city `Paris` comes only from the resume payload, so seeing it in the
12 * tool result proves that the answer reached the MCP server.
13 */
14
15type StreamEvent = {
16 type: string
17 toolCallId?: string
18 toolCallName?: string
19 content?: unknown
20 delta?: string
21 outcome?: {
22 type: string
23 interrupts?: Array<{
24 id: string
25 reason: string
26 metadata?: Record<string, unknown>
27 }>
28 }
29}
30
31function parseSse(body: string): Array<StreamEvent> {
32 const events: Array<StreamEvent> = []
33 for (const line of body.split('\n')) {
34 const trimmed = line.trim()
35 if (!trimmed.startsWith('data:')) continue
36 const json = trimmed.slice('data:'.length).trim()
37 if (!json) continue
38 try {
39 events.push(JSON.parse(json) as StreamEvent)
40 } catch {
41 // Ignore non-JSON keepalive lines.
42 }
43 }
44 return events
45}
46
47const question = '[mcp-input] what is the forecast'
48
49test.describe('mcp input: pause and resume chat()', () => {
50 test('pauses on requestInput, then the answer reaches the tool', async ({
51 request,
52 testId,
53 aimockPort,
54 }) => {
55 const threadId = `mcp-input-thread-${testId}`
56 const firstRunId = `mcp-input-run-${testId}`
57
58 const first = await request.post('/api/mcp-input-test', {
59 headers: { 'Content-Type': 'application/json' },
60 data: {
61 threadId,
62 runId: firstRunId,
63 state: {},
64 messages: [{ id: 'mcp-input-msg-1', role: 'user', content: question }],
65 tools: [],
66 context: [],
67 forwardedProps: { testId, aimockPort },
68 },
69 })
70 const firstBody = await first.text()
71 expect(first.ok(), `first run failed: ${firstBody}`).toBe(true)
72 const firstEvents = parseSse(firstBody)
73
74 const toolStart = firstEvents.find(
75 (event) =>
76 event.type === 'TOOL_CALL_START' && event.toolCallName === 'ask_city',
77 )
78 const toolCallId = toolStart?.toolCallId
79 expect(toolCallId, 'expected an ask_city tool call').toBeTruthy()
80
81 const finished = firstEvents.find((event) => event.type === 'RUN_FINISHED')
82 expect(finished?.outcome?.type, firstBody).toBe('interrupt')
83 const interrupt = finished?.outcome?.interrupts?.[0]
84 expect(interrupt).toMatchObject({
85 id: `mcp_input_${toolCallId}`,
86 reason: 'mcp_input',
87 })
88 expect(interrupt?.metadata?.['tanstack:interruptBinding']).toMatchObject({
89 kind: 'generic',
90 interruptedRunId: firstRunId,
91 })
92 expect(firstEvents.some((event) => event.type === 'TOOL_CALL_RESULT')).toBe(
93 false,
94 )
95
96 const second = await request.post('/api/mcp-input-test', {
97 headers: { 'Content-Type': 'application/json' },
98 data: {
99 threadId,
100 runId: `mcp-input-run-2-${testId}`,
101 parentRunId: firstRunId,
102 resume: [
103 {
104 interruptId: `mcp_input_${toolCallId}`,
105 status: 'resolved',
106 payload: { value: 'Paris' },
107 },
108 ],
109 state: {},
110 messages: [
111 { id: 'mcp-input-msg-1', role: 'user', content: question },
112 {
113 id: 'mcp-input-msg-2',
114 role: 'assistant',
115 content: 'Let me ask for the city.',
116 toolCalls: [
117 {
118 id: toolCallId,
119 type: 'function',
120 function: { name: 'ask_city', arguments: '{}' },
121 },
122 ],
123 },
124 ],
125 tools: [],
126 context: [],
127 forwardedProps: { testId, aimockPort },
128 },
129 })
130 const secondBody = await second.text()
131 expect(second.ok(), `resume run failed: ${secondBody}`).toBe(true)
132 const secondEvents = parseSse(secondBody)
133
134 expect(secondEvents.some((event) => event.type === 'RUN_ERROR')).toBe(false)
135 const toolResult = secondEvents.find(
136 (event) => event.type === 'TOOL_CALL_RESULT',
137 )
138 expect(JSON.stringify(toolResult?.content ?? '')).toContain(
139 'Forecast for Paris: sunny',
140 )
141 const finalText = secondEvents
142 .filter((event) => event.type === 'TEXT_MESSAGE_CONTENT' && event.delta)
143 .map((event) => event.delta)
144 .join('')
145 expect(finalText).toContain('sunny')
146 })
147
148 test('a custom reason still resumes by interrupt id', async ({
149 request,
150 testId,
151 aimockPort,
152 }) => {
153 const threadId = `mcp-input-reason-thread-${testId}`
154 const firstRunId = `mcp-input-reason-run-${testId}`
155 const response = await request.post('/api/mcp-input-test', {
156 headers: { 'Content-Type': 'application/json' },
157 data: {
158 threadId,
159 runId: firstRunId,
160 state: {},
161 messages: [{ id: 'mcp-input-msg-1', role: 'user', content: question }],
162 tools: [],
163 context: [],
164 forwardedProps: {
165 testId,
166 aimockPort,
167 interruptReason: 'auth_required',
168 },
169 },
170 })
171 const body = await response.text()
172 expect(response.ok(), `run failed: ${body}`).toBe(true)
173 const events = parseSse(body)
174
175 const toolCallId = events.find(
176 (event) =>
177 event.type === 'TOOL_CALL_START' && event.toolCallName === 'ask_city',
178 )?.toolCallId
179 const finished = events.find((event) => event.type === 'RUN_FINISHED')
180 expect(finished?.outcome?.type, body).toBe('interrupt')
181 expect(finished?.outcome?.interrupts?.[0]).toMatchObject({
182 id: `mcp_input_${toolCallId}`,
183 reason: 'auth_required',
184 })
185
186 const second = await request.post('/api/mcp-input-test', {
187 headers: { 'Content-Type': 'application/json' },
188 data: {
189 threadId,
190 runId: `mcp-input-reason-run-2-${testId}`,
191 parentRunId: firstRunId,
192 resume: [
193 {
194 interruptId: `mcp_input_${toolCallId}`,
195 status: 'resolved',
196 payload: { value: 'Paris' },
197 },
198 ],
199 state: {},
200 messages: [
201 { id: 'mcp-input-msg-1', role: 'user', content: question },
202 {
203 id: 'mcp-input-msg-2',
204 role: 'assistant',
205 content: 'Let me ask for the city.',
206 toolCalls: [
207 {
208 id: toolCallId,
209 type: 'function',
210 function: { name: 'ask_city', arguments: '{}' },
211 },
212 ],
213 },
214 ],
215 tools: [],
216 context: [],
217 forwardedProps: { testId, aimockPort },
218 },
219 })
220 const secondBody = await second.text()
221 expect(second.ok(), `resume run failed: ${secondBody}`).toBe(true)
222 const secondEvents = parseSse(secondBody)
223 const toolResult = secondEvents.find(
224 (event) => event.type === 'TOOL_CALL_RESULT',
225 )
226 expect(JSON.stringify(toolResult?.content ?? '')).toContain(
227 'Forecast for Paris: sunny',
228 )
229 })
230})