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

mcp.spec.ts

165 lines5.8 KB

1import { test, expect } from './fixtures'
2​
3/**
4 * MCP server tool discovery + execution inside chat().
5 *
6 * Proves the `@tanstack/ai-mcp` package end-to-end against the real E2E
7 * harness:
8 * - `api.mcp-server` hosts a real in-process MCP server (Streamable HTTP)
9 * exposing one deterministic tool `get_guitar_price` ({ id } → { id,
10 * price: 1999 }).
11 * - `api.mcp-test` connects to it via `createMCPClient`, discovers the tool
12 * with `mcp.tools()`, and runs it inside a real `chat()` agent loop with
13 * the LLM mocked by aimock.
14 * - The aimock fixture (`fixtures/mcp/basic.json`) makes the model emit a
15 * `get_guitar_price` tool call then a final answer.
16 *
17 * The MCP server is the *only* source of the price `1999`: the fixture's
18 * tool-call arguments don't contain it. So asserting `1999` reaches the
19 * streamed `TOOL_CALL_END.result` proves the MCP tool actually executed.
20 */
21​
22type StreamEvent = {
23 type: string
24 toolName?: string
25 toolCallName?: string
26 toolCallId?: string
27 result?: unknown
28 content?: unknown
29 delta?: string
31​
32function parseSse(body: string): Array<StreamEvent> {
33 const events: Array<StreamEvent> = []
34 for (const line of body.split('\n')) {
35 const trimmed = line.trim()
36 if (!trimmed.startsWith('data:')) continue
37 const json = trimmed.slice('data:'.length).trim()
38 if (!json) continue
39 try {
40 events.push(JSON.parse(json) as StreamEvent)
41 } catch {
42 // Ignore non-JSON keepalive lines.
43 }
44 }
45 return events
47​
48test.describe('mcp — server tool discovery + execution in chat()', () => {
49 test('discovers get_guitar_price from the MCP server and the result reaches the transcript', async ({
50 request,
51 testId,
52 aimockPort,
53 }) => {
54 // Minimal valid AG-UI RunAgentInput body (the route parses it via
55 // chatParamsFromRequestBody).
56 const res = await request.post('/api/mcp-test', {
57 headers: { 'Content-Type': 'application/json' },
58 data: {
59 threadId: `mcp-thread-${testId}`,
60 runId: `mcp-run-${testId}`,
61 state: {},
62 messages: [
63 {
64 id: 'mcp-msg-1',
65 role: 'user',
66 content: '[mcp] how much is the strat guitar',
67 },
68 ],
69 tools: [],
70 context: [],
71 forwardedProps: { testId, aimockPort },
72 },
73 })
74​
75 const body = await res.text()
76 expect(res.ok(), `mcp-test route failed (${res.status()}): ${body}`).toBe(
77 true,
78 )
79​
80 const events = parseSse(body)
81​
82 // The agentic loop must have invoked the MCP tool.
83 const toolStart = events.find(
84 (e) =>
85 e.type === 'TOOL_CALL_START' &&
86 (e.toolName === 'get_guitar_price' ||
87 e.toolCallName === 'get_guitar_price'),
88 )
89 expect(
90 toolStart,
91 'expected a TOOL_CALL_START for get_guitar_price',
92 ).toBeTruthy()
93​
94 // The MCP tool result is emitted as the AG-UI TOOL_CALL_RESULT event. The
95 // price 1999 originates ONLY from the real MCP server (the fixture's
96 // tool-call args don't contain it), so finding it here proves the MCP tool
97 // actually executed against the in-process server.
98 const toolResult = events.find((e) => e.type === 'TOOL_CALL_RESULT')
99 expect(toolResult, 'expected a TOOL_CALL_RESULT event').toBeTruthy()
100 const resultStr = JSON.stringify(toolResult?.content ?? '')
101 expect(resultStr).toContain('1999')
102 expect(resultStr).toContain('strat')
103​
104 // And the final assistant text (post tool execution) streamed through.
105 const finalText = events
106 .filter((e) => e.type === 'TEXT_MESSAGE_CONTENT' && e.delta)
107 .map((e) => e.delta)
108 .join('')
109 expect(finalText).toContain('1999')
110​
111 // The run completed cleanly (no RUN_ERROR).
112 expect(events.some((e) => e.type === 'RUN_ERROR')).toBe(false)
113 expect(events.some((e) => e.type === 'RUN_FINISHED')).toBe(true)
114 })
115​
116 test('clientOptions reaches the SDK client — a custom validator changes the outcome', async ({
117 request,
118 testId,
119 aimockPort,
120 }) => {
121 // Same server, same fixture, one flag: the route installs a
122 // `jsonSchemaValidator` that refuses everything via `clientOptions`.
123 //
124 // The mock server's `get_guitar_price` declares an `outputSchema` and
125 // returns `structuredContent` the SDK's default AJV provider accepts, so a
126 // `clientOptions` that was dropped on the floor would leave this run
127 // identical to the test above. It is not: the tool call fails validation.
128 //
129 // That option is how an edge runtime installs a validator that does not
130 // compile schemas with `new Function`, which AJV does and Cloudflare
131 // Workers forbid.
132 const res = await request.post('/api/mcp-test', {
133 headers: { 'Content-Type': 'application/json' },
134 data: {
135 threadId: `mcp-opts-thread-${testId}`,
136 runId: `mcp-opts-run-${testId}`,
137 state: {},
138 messages: [
139 {
140 id: 'mcp-opts-msg-1',
141 role: 'user',
142 content: '[mcp] how much is the strat guitar',
143 },
144 ],
145 tools: [],
146 context: [],
147 forwardedProps: { testId, aimockPort, rejectStructuredOutput: true },
148 },
149 })
150​
151 const body = await res.text()
152 const events = parseSse(body)
153​
154 const toolResult = events.find((e) => e.type === 'TOOL_CALL_RESULT')
155 expect(toolResult, 'expected a TOOL_CALL_RESULT event').toBeTruthy()
156 const resultStr = JSON.stringify(toolResult?.content ?? '')
157​
158 expect(resultStr).toContain('rejected by the injected validator')
159 // The other half of the proof: the payload the default validator accepts
160 // never reaches the tool result. Asserted on the tool result rather than the
161 // whole transcript — the aimock fixture's final answer is a recorded script
162 // that names the price whether or not the tool succeeded.
163 expect(resultStr).not.toContain('1999')
164 })
165})