Open History to see the last commit
1import { validateWithStandardSchema } from '@tanstack/ai'
2
3/**
4 * Internal. Parses a tool output with its `outputSchema`, like chat() does,
5 * so the result matches the advertised output view. A result that fails
6 * the schema but that the tool built itself (`isOwnResult`) is kept.
7 * This module loads no MCP SDK, so the direct client can use it.
8 */
9export async function parseToolOutput(
10 tool: { name: string; outputSchema?: unknown },
11 output: unknown,
12 isOwnResult: (value: unknown) => boolean,
13) {
14 const parsed = await validateWithStandardSchema(tool.outputSchema, output)
15 if (parsed.success) return parsed.data
16 if (isOwnResult(output)) return output
17 const issues = parsed.issues.map((issue) => issue.message).join(', ')
18 throw new Error(
19 `Tool ${tool.name} returned output that does not match its outputSchema: ${issues}`,
20 )
21}