feat(ai): check JSON Schema and MCP tool input with @cfworker/json-schema #1751
feat/tool-input-final-check fromrefs/pull/1751/head·merged by AlemTuzlak View on GitHubA tool with a raw JSON Schema inputSchema now gets its input checked before it runs: plain JSON Schema tools and every MCP tool. Input that does not fit becomes a tool error that names each failed field, so the model can repair the call. OpenAPI 3.0 schemas (nullable, boolean bounds) work as written. The error does not include the page URL.
This piece is split out of #1555 (wave 3, final tool-input validation, part 2 of 3). It is stacked on #1750. #1752 (tool input repair, chat({ repairToolInput })) is merged into this branch.
🎯 Changes#
validateToolInputchecks JSON Schema with@cfworker/json-schema. It runs after approval edits andonBeforeToolCall. The validator does not useeval, so it runs on Cloudflare Workers.- The draft comes from
$schema(draft-04, draft-07, 2019-09, 2020-12). With no$schema, the check uses 2020-12. OpenAPI 3.0 works:nullable: truelets a field benull(next totype,enum,$ref,allOf,anyOf,oneOf, ornot), and a booleanexclusiveMinimumorexclusiveMaximumworks in every draft. - The error names each failed location (at most 10) and shows the received arguments. It keeps the first line of a validator failure and removes the validator base URI. In a browser that URI is the page, including its query.
- One validator per schema object, cached in a
WeakMap. The validator gets a JSON copy, so the tool schema does not change.formatis not checked, but aformatkey insideconstorenumdata still counts. A key set toundefinedcounts as a missing key. - New dependency:
"@cfworker/json-schema": "^4.1.1"on@tanstack/ai. Docs:docs/tools/server-tools.md. Changeset: minor for@tanstack/ai.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Public API change#
The types do not change. The behavior does.
Before
const countItems = toolDefinition({
name: 'count_items',
description: 'Count the items',
inputSchema: {
type: 'object',
properties: { count: { type: 'integer' } },
required: ['count'],
},
}).server((input) => /* runs with { count: 'many' } */ 'counted')
After
// Same tool. With { count: 'many' } the tool does not run. The model gets:
// { error: 'Input validation failed for tool count_items: #/count: Instance type "string" is invalid. Expected "integer".\nReceived arguments: ...' }
Migration: if a JSON Schema or MCP tool must accept input that its schema rejects, widen the schema.
Testing#
Commands run. All passed:
- After the merge of #1750:
vitest runinpackages/ai(2541 passed), andnx run-many -t test:types,test:oxlint -p @tanstack/ai @tanstack/ai-e2e. - Before the merge:
pnpm --filter @tanstack/ai-e2e test:e2e --grep "JSON Schema tool"(1 passed) andpnpm test:docs.
pnpm test:pr was not run. The E2E spec was not run again after the merge.
Manual test.
- Run
pnpm --filter @tanstack/ai-e2e test:e2e --grep "JSON Schema tool". - The route
/api/json-schema-tool-inputusesfakeTextto call two tools: a draft-07 tool with{ count: 'many' }, and an OpenAPI 3.0 tool with{ label: null }for anullablefield. - Make sure that
count_itemsdid not run and the model gotInput validation failed for tool count_itemswith#/count. - Make sure that
label_itemran with{ label: null }.
How this PR makes testing easy.
packages/ai/tests/tool-input-validation.test.tscovers valid and invalid input, error text,$refandif/then, drafts, boolean schemas, a missing local$ref(no page URL in the error),format,undefinedkeys, middleware and approval edits, and a run withFunctionblocked.- The same file covers OpenAPI 3.0:
nullableontype,enum,allOf,oneOf, and a root schema,$refs that point into a nullable schema (raw and percent-encoded), and boolean bounds under every$schema. - E2E:
testing/e2e/tests/json-schema-tool-input.spec.tswith afakeTextroute (no network).
Risk / rollback#
- JSON Schema and MCP tools that got invalid input and still worked now return a tool error.
- A schema that the validator cannot use gives a tool error on every call.
- The check wraps a nullable
$ref/allOf/anyOf/oneOfschema in its copy and moves local$refs with it. An absolute$refinto that schema, or a$refinside a nullable schema with its own$id, is not moved and fails as an unresolved$ref. - The validator caches by schema object. A schema changed in place after the first call keeps the first version.
- Rollback: revert this PR.
- 1
Important
Review skipped#
Auto reviews are disabled on base/target branches other than the default branch.
Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
-
- Configuration used: Repository: TanStack/ai/.coderabbit.yaml
- Review profile: CHILL
- Plan: Advanced
- Run ID:
c3affd08-4b9b-4a7d-a7ca-74873fbe9f0f
You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- 🔍 Trigger review
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.-
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
Diff Package Supply Chain
SecurityVulnerability Quality Maintenance License npm/@cfworker/json-schema@4.1.1 View your CI Pipeline Execution ↗ for commit e147eb0dbfb7fc009bdba1b051f64ad4b17c442e
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 7m 24s View ↗ nx run-many --targets=build --exclude=examples/...✅ Succeeded 3s View ↗ nx run-many --targets=test:coverage --projects=...✅ Succeeded 1s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-13 20:06:15UTC@tanstack/ai
npm i https://pkg.pr.new/@tanstack/ai@1751@tanstack/ai-acp
npm i https://pkg.pr.new/@tanstack/ai-acp@1751@tanstack/ai-angular
npm i https://pkg.pr.new/@tanstack/ai-angular@1751@tanstack/ai-anthropic
npm i https://pkg.pr.new/@tanstack/ai-anthropic@1751@tanstack/ai-bedrock
npm i https://pkg.pr.new/@tanstack/ai-bedrock@1751@tanstack/ai-byteplus
npm i https://pkg.pr.new/@tanstack/ai-byteplus@1751@tanstack/ai-claude-code
npm i https://pkg.pr.new/@tanstack/ai-claude-code@1751@tanstack/ai-client
npm i https://pkg.pr.new/@tanstack/ai-client@1751@tanstack/ai-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-cloudflare@1751@tanstack/ai-code-mode
npm i https://pkg.pr.new/@tanstack/ai-code-mode@1751@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/@tanstack/ai-code-mode-snippets@1751@tanstack/ai-codex
npm i https://pkg.pr.new/@tanstack/ai-codex@1751@tanstack/ai-cohere
npm i https://pkg.pr.new/@tanstack/ai-cohere@1751@tanstack/ai-compaction
npm i https://pkg.pr.new/@tanstack/ai-compaction@1751@tanstack/ai-devtools-core
npm i https://pkg.pr.new/@tanstack/ai-devtools-core@1751@tanstack/ai-durable-stream
npm i https://pkg.pr.new/@tanstack/ai-durable-stream@1751@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@1751@tanstack/ai-event-client
npm i https://pkg.pr.new/@tanstack/ai-event-client@1751@tanstack/ai-fal
npm i https://pkg.pr.new/@tanstack/ai-fal@1751@tanstack/ai-gemini
npm i https://pkg.pr.new/@tanstack/ai-gemini@1751@tanstack/ai-grok
npm i https://pkg.pr.new/@tanstack/ai-grok@1751@tanstack/ai-grok-build
npm i https://pkg.pr.new/@tanstack/ai-grok-build@1751@tanstack/ai-groq
npm i https://pkg.pr.new/@tanstack/ai-groq@1751@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@1751@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/@tanstack/ai-isolate-daytona@1751@tanstack/ai-isolate-e2b
npm i https://pkg.pr.new/@tanstack/ai-isolate-e2b@1751@tanstack/ai-isolate-node
npm i https://pkg.pr.new/@tanstack/ai-isolate-node@1751@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@1751@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs-bun@1751@tanstack/ai-llmgateway
npm i https://pkg.pr.new/@tanstack/ai-llmgateway@1751@tanstack/ai-lovable
npm i https://pkg.pr.new/@tanstack/ai-lovable@1751@tanstack/ai-mcp
npm i https://pkg.pr.new/@tanstack/ai-mcp@1751@tanstack/ai-memory
npm i https://pkg.pr.new/@tanstack/ai-memory@1751@tanstack/ai-mistral
npm i https://pkg.pr.new/@tanstack/ai-mistral@1751@tanstack/ai-octane
npm i https://pkg.pr.new/@tanstack/ai-octane@1751@tanstack/ai-ollama
npm i https://pkg.pr.new/@tanstack/ai-ollama@1751@tanstack/ai-ollaya
npm i https://pkg.pr.new/@tanstack/ai-ollaya@1751@tanstack/ai-openai
npm i https://pkg.pr.new/@tanstack/ai-openai@1751@tanstack/ai-opencode
npm i https://pkg.pr.new/@tanstack/ai-opencode@1751@tanstack/ai-openrouter
npm i https://pkg.pr.new/@tanstack/ai-openrouter@1751@tanstack/ai-perplexity
npm i https://pkg.pr.new/@tanstack/ai-perplexity@1751@tanstack/ai-persistence
npm i https://pkg.pr.new/@tanstack/ai-persistence@1751@tanstack/ai-preact
npm i https://pkg.pr.new/@tanstack/ai-preact@1751@tanstack/ai-react
npm i https://pkg.pr.new/@tanstack/ai-react@1751@tanstack/ai-react-ui
npm i https://pkg.pr.new/@tanstack/ai-react-ui@1751@tanstack/ai-reactor
npm i https://pkg.pr.new/@tanstack/ai-reactor@1751@tanstack/ai-remix
npm i https://pkg.pr.new/@tanstack/ai-remix@1751@tanstack/ai-sandbox
npm i https://pkg.pr.new/@tanstack/ai-sandbox@1751@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-blaxel@1751@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/@tanstack/ai-sandbox-boxd@1751@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-sandbox-cloudflare@1751@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/@tanstack/ai-sandbox-daytona@1751@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/@tanstack/ai-sandbox-docker@1751@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/@tanstack/ai-sandbox-e2b@1751@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/@tanstack/ai-sandbox-local-process@1751@tanstack/ai-sandbox-railway
npm i https://pkg.pr.new/@tanstack/ai-sandbox-railway@1751@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/@tanstack/ai-sandbox-sprites@1751@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/@tanstack/ai-sandbox-upstash-box@1751@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-vercel@1751@tanstack/ai-skills
npm i https://pkg.pr.new/@tanstack/ai-skills@1751@tanstack/ai-solid
npm i https://pkg.pr.new/@tanstack/ai-solid@1751@tanstack/ai-solid-ui
npm i https://pkg.pr.new/@tanstack/ai-solid-ui@1751@tanstack/ai-svelte
npm i https://pkg.pr.new/@tanstack/ai-svelte@1751@tanstack/ai-typesafe
npm i https://pkg.pr.new/@tanstack/ai-typesafe@1751@tanstack/ai-utils
npm i https://pkg.pr.new/@tanstack/ai-utils@1751@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/@tanstack/ai-vercel-gateway@1751@tanstack/ai-vertex
npm i https://pkg.pr.new/@tanstack/ai-vertex@1751@tanstack/ai-vue
npm i https://pkg.pr.new/@tanstack/ai-vue@1751@tanstack/ai-vue-ui
npm i https://pkg.pr.new/@tanstack/ai-vue-ui@1751@tanstack/ai-worldlabs
npm i https://pkg.pr.new/@tanstack/ai-worldlabs@1751@tanstack/openai-base
npm i https://pkg.pr.new/@tanstack/openai-base@1751@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@1751@tanstack/react-ai-devtools
npm i https://pkg.pr.new/@tanstack/react-ai-devtools@1751@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@1751@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/@tanstack/svelte-ai-devtools@1751commit:
e147eb0github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: author
Coverage#
✅ Coverage held across 68 compared package(s).
Each package is measured twice in this job — on this PR and on its merge-base with
main— and compared. A metric fails only when it is at or above 60% on the merge-base and under 60% on this PR. Other drops are listed but do not fail. Packages your PR doesn't affect are not measured and not listed.package statements branches functions lines ai 91.68% (+0.16) 85.43% (+0.15) 92.39% (+0.19) 93.27% (+0.17) ok ai-acp 78.41% (0.00) 66.79% (0.00) 70.27% (0.00) 80.13% (0.00) ok ai-angular 73.37% (0.00) 60.66% (0.00) 62.14% (0.00) 75.13% (0.00) ok ai-anthropic 92.00% (+0.40) 86.73% (+0.49) 95.57% (0.00) 92.41% (+0.42) ok ai-bedrock 90.11% (+0.01) 82.82% (0.00) 94.20% (0.00) 91.33% (+0.01) ok ai-byteplus 97.06% (0.00) 91.66% (0.00) 98.27% (0.00) 98.49% (0.00) ok ai-claude-code 88.93% (0.00) 76.88% (0.00) 86.88% (0.00) 90.40% (0.00) ok ai-client 86.47% (0.00) 79.23% (0.00) 84.62% (0.00) 88.41% (0.00) ok ai-cloudflare 85.55% (0.00) 76.44% (0.00) 85.48% (0.00) 86.79% (0.00) ok ai-code-mode 91.71% (0.00) 83.12% (0.00) 86.27% (0.00) 92.63% (0.00) ok ai-code-mode-snippets 87.13% (0.00) 75.67% (0.00) 91.11% (0.00) 88.39% (0.00) ok ai-codex 94.55% (0.00) 85.10% (0.00) 89.28% (0.00) 95.56% (0.00) ok ai-cohere 88.97% (0.00) 88.37% (0.00) 85.71% (0.00) 91.80% (0.00) ok ai-compaction 97.38% (0.00) 92.75% (0.00) 100.00% (0.00) 98.88% (0.00) ok ai-devtools 83.13% (0.00) 72.77% (0.00) 87.07% (0.00) 85.22% (0.00) ok ai-durable-stream 86.70% (0.00) 82.25% (0.00) 92.30% (0.00) 88.72% (0.00) ok ai-elevenlabs 74.68% (0.00) 69.59% (0.00) 59.13% (0.00) 75.46% (0.00) ok ai-event-client 56.83% (0.00) 46.87% (0.00) 68.96% (0.00) 57.89% (0.00) ok ai-fal 91.10% (0.00) 82.63% (0.00) 92.40% (0.00) 91.60% (0.00) ok ai-gemini 68.33% (+0.24) 67.88% (+0.44) 64.62% (+0.26) 68.93% (+0.14) ok ai-grok 48.85% (0.00) 53.49% (0.00) 56.46% (0.00) 49.67% (0.00) ok ai-grok-build 79.19% (0.00) 63.56% (0.00) 77.39% (0.00) 81.02% (0.00) ok ai-groq 51.31% (0.00) 47.17% (0.00) 65.11% (0.00) 53.42% (0.00) ok ai-isolate-cloudflare 94.11% (0.00) 70.66% (0.00) 100.00% (0.00) 93.96% (0.00) ok ai-isolate-daytona 90.25% (0.00) 80.59% (0.00) 100.00% (0.00) 90.15% (0.00) ok ai-isolate-e2b 92.11% (0.00) 81.48% (-0.74) 97.36% (0.00) 95.05% (0.00) ok ai-isolate-node 84.00% (0.00) 43.39% (0.00) 100.00% (0.00) 83.78% (0.00) ok ai-isolate-quickjs 89.07% (0.00) 76.41% (0.00) 90.90% (0.00) 90.30% (0.00) ok ai-isolate-quickjs-bun 7.76% (0.00) 8.08% (0.00) 5.00% (0.00) 8.33% (0.00) ok ai-llmgateway 97.67% (0.00) 100.00% (0.00) 88.88% (0.00) 97.67% (0.00) ok ai-lovable 73.21% (0.00) 55.63% (0.00) 85.07% (0.00) 73.96% (0.00) ok ai-mcp 96.91% (0.00) 92.43% (0.00) 97.33% (0.00) 98.72% (0.00) ok ai-memory 73.87% (0.00) 52.70% (0.00) 79.74% (0.00) 76.04% (0.00) ok ai-mistral 84.82% (-0.17) 79.88% (-0.09) 92.59% (0.00) 85.46% (-0.18) ok ai-octane 90.58% (0.00) 71.48% (0.00) 87.29% (0.00) 91.24% (0.00) ok ai-ollama 92.73% (+0.97) 78.15% (+1.68) 90.62% (0.00) 92.68% (+0.97) ok ai-ollaya 86.84% (0.00) 89.28% (0.00) 85.71% (0.00) 91.66% (0.00) ok ai-openai 65.34% (0.00) 65.72% (0.00) 66.66% (0.00) 65.63% (0.00) ok ai-opencode 68.77% (0.00) 60.82% (0.00) 64.04% (0.00) 70.78% (0.00) ok ai-openrouter 87.78% (+1.07) 72.80% (+0.92) 90.52% (0.00) 89.70% (+1.05) ok ai-perplexity 91.34% (0.00) 90.43% (0.00) 100.00% (0.00) 93.87% (0.00) ok ai-persistence 91.33% (0.00) 81.07% (0.00) 97.44% (0.00) 94.85% (0.00) ok ai-preact 87.80% (0.00) 75.87% (0.00) 88.73% (0.00) 89.66% (0.00) ok ai-react 73.13% (0.00) 52.16% (0.00) 75.54% (0.00) 73.08% (0.00) ok ai-reactor 94.91% (0.00) 83.33% (0.00) 93.33% (0.00) 96.55% (0.00) ok ai-remix 39.39% (0.00) 23.35% (0.00) 35.51% (0.00) 41.58% (0.00) ok ai-sandbox 77.45% (0.00) 82.80% (0.00) 74.47% (0.00) 77.97% (0.00) ok ai-sandbox-blaxel 87.64% (0.00) 77.18% (0.00) 82.26% (0.00) 89.71% (0.00) ok ai-sandbox-boxd 95.86% (0.00) 87.12% (0.00) 95.31% (0.00) 98.17% (0.00) ok ai-sandbox-cloudflare 67.91% (0.00) 57.47% (0.00) 71.79% (0.00) 69.62% (0.00) ok ai-sandbox-daytona 82.70% (0.00) 81.89% (0.00) 67.14% (0.00) 85.18% (0.00) ok ai-sandbox-docker 80.15% (0.00) 71.49% (0.00) 77.38% (0.00) 82.13% (0.00) ok ai-sandbox-e2b 94.42% (0.00) 88.97% (0.00) 93.24% (0.00) 98.63% (0.00) ok ai-sandbox-local-process 68.00% (0.00) 53.59% (0.00) 75.86% (0.00) 68.88% (0.00) ok ai-sandbox-railway 94.75% (0.00) 92.68% (0.00) 90.90% (0.00) 97.59% (0.00) ok ai-sandbox-sprites 73.23% (0.00) 59.70% (0.00) 73.83% (0.00) 76.17% (0.00) ok ai-sandbox-upstash-box 94.82% (0.00) 91.52% (0.00) 91.04% (0.00) 97.76% (0.00) ok ai-sandbox-vercel 44.67% (0.00) 38.18% (0.00) 35.18% (0.00) 45.76% (0.00) ok ai-skills 80.38% (0.00) 64.05% (0.00) 84.86% (0.00) 82.44% (0.00) ok ai-solid 53.73% (0.00) 40.45% (0.00) 51.94% (0.00) 57.78% (0.00) ok ai-svelte 70.44% (0.00) 54.50% (0.00) 69.86% (0.00) 75.99% (0.00) ok ai-typesafe 78.04% (0.00) 84.61% (0.00) 66.66% (0.00) 82.05% (0.00) ok ai-utils 76.38% (0.00) 74.00% (0.00) 92.30% (0.00) 76.56% (0.00) ok ai-vercel-gateway 75.97% (0.00) 55.63% (0.00) 85.41% (0.00) 79.38% (0.00) ok ai-vertex 96.77% (0.00) 97.22% (0.00) 100.00% (0.00) 96.77% (0.00) ok ai-vue 60.70% (0.00) 36.17% (0.00) 62.00% (0.00) 62.95% (0.00) ok ai-worldlabs 79.62% (0.00) 71.50% (0.00) 91.42% (0.00) 82.66% (0.00) ok openai-base 89.46% (+0.34) 77.26% (+0.12) 97.57% (0.00) 91.51% (+0.36) ok github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
github-actions[bot](GitHub profile, opens in a new tab) removed labelswaiting-on: author
AlemTuzlak merged this6307284
AlemTuzlak closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: f7aa7c5b1f513a68617bbaec567a1029140e1c3b
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 51 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR