Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { toolDefinition } from '@tanstack/ai'
3import { createMCPClient, MCPTaskRequiredToolError } from '@tanstack/ai-mcp'
4import { z } from 'zod'
5
6const SCENARIOS = [
7 'abort',
8 'skip-unsupported',
9 'call-unsupported',
10 'bind-unsupported',
11] as const
12
13type Scenario = (typeof SCENARIOS)[number]
14
15function isScenario(value: string | null): value is Scenario {
16 return value !== null && (SCENARIOS as ReadonlyArray<string>).includes(value)
17}
18
19const needsTasksDef = toolDefinition({
20 name: 'needs_tasks',
21 description: 'Requires tasks the server cannot execute',
22 inputSchema: z.object({}),
23})
24
25/**
26 * Unhappy-path MCP task probes. No LLM. Query `?scenario=` selects one case:
27 * abort a hanging task, skip a task-required tool without capability,
28 * callTool/bind that same tool and get MCPTaskRequiredToolError.
29 */
30export const Route = createFileRoute('/api/mcp-task-errors')({
31 server: {
32 handlers: {
33 GET: async ({ request }) => {
34 const url = new URL(request.url)
35 const scenario = url.searchParams.get('scenario')
36 if (!isScenario(scenario)) {
37 return Response.json({ error: 'Unknown scenario' }, { status: 400 })
38 }
39 const origin = url.origin
40 if (scenario === 'abort') {
41 return runAbort(origin)
42 }
43 return runUnsupported(origin, scenario)
44 },
45 },
46 },
47})
48
49async function runAbort(origin: string) {
50 const client = await createMCPClient({
51 transport: { type: 'http', url: `${origin}/api/mcp-server` },
52 })
53 const started = Date.now()
54 try {
55 const outcome = await Promise.race([
56 client
57 .callTool(
58 'hanging_appraisal',
59 { ids: ['strat'] },
60 { signal: AbortSignal.timeout(100) },
61 )
62 .then((result) => ({ kind: 'result' as const, result }))
63 .catch((error: unknown) => ({
64 kind: 'error' as const,
65 errorName: error instanceof Error ? error.name : 'unknown',
66 message: error instanceof Error ? error.message : String(error),
67 })),
68 new Promise<{ kind: 'timeout' }>((resolve) =>
69 setTimeout(() => resolve({ kind: 'timeout' }), 3000),
70 ),
71 ])
72 if (outcome.kind === 'result') {
73 return Response.json({
74 aborted: false,
75 elapsedMs: Date.now() - started,
76 errorName: 'none',
77 message: JSON.stringify(outcome.result),
78 })
79 }
80 if (outcome.kind === 'timeout') {
81 return Response.json({
82 aborted: false,
83 elapsedMs: Date.now() - started,
84 errorName: 'timeout',
85 message: 'callTool did not abort within 3000ms',
86 })
87 }
88 return Response.json({
89 aborted: outcome.errorName !== 'none',
90 elapsedMs: Date.now() - started,
91 errorName: outcome.errorName,
92 message: outcome.message,
93 })
94 } finally {
95 void client.close().catch(() => {})
96 }
97}
98
99async function runUnsupported(
100 origin: string,
101 scenario: Exclude<Scenario, 'abort'>,
102) {
103 const client = await createMCPClient({
104 transport: { type: 'http', url: `${origin}/api/mcp-no-tasks-server` },
105 })
106 try {
107 if (scenario === 'skip-unsupported') {
108 const tools = await client.tools()
109 return Response.json({ tools: tools.map((tool) => tool.name) })
110 }
111 if (scenario === 'call-unsupported') {
112 try {
113 await client.callTool('needs_tasks')
114 return Response.json({ errorName: 'none' })
115 } catch (error) {
116 return Response.json({
117 errorName: error instanceof Error ? error.name : 'unknown',
118 isTaskRequired: error instanceof MCPTaskRequiredToolError,
119 })
120 }
121 }
122 try {
123 await client.tools([needsTasksDef])
124 return Response.json({ errorName: 'none' })
125 } catch (error) {
126 return Response.json({
127 errorName: error instanceof Error ? error.name : 'unknown',
128 isTaskRequired: error instanceof MCPTaskRequiredToolError,
129 })
130 }
131 } finally {
132 await client.close()
133 }
134}