Skip to content

fix(core): preserve jsonSchema adapter after Zod v4 tool schema injection - #21187

Merged
wardpeet merged 3 commits into
mastra-ai:mainfrom
Souravrajvi0:feat/coretoolbuilder-zod-v4-schema-adapter-091f
Aug 12, 2026
Merged

fix(core): preserve jsonSchema adapter after Zod v4 tool schema injection#21187
wardpeet merged 3 commits into
mastra-ai:mainfrom
Souravrajvi0:feat/coretoolbuilder-zod-v4-schema-adapter-091f

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Description

CoreToolBuilder injects _background / suspendedToolRunId / resumeData onto Zod v4 tool input schemas via safeExtendZodObject. That returns a fresh ZodObject which can drop a custom ~standard.jsonSchema adapter installed by toStandardSchema / createTool().

When downstream code calls standardSchemaToJSONSchema on the mutated schema, invalid tool input can crash with Cannot read properties of undefined (reading 'input') instead of returning a structured validation error.

This mirrors the normalization already done in the non-Zod-v4 JSON Schema fallback path and in validateToolInput (#19030), but applies it at the point where the schema is mutated.

Related issue(s)

Fixes #21170

Type of change

  • Bug fix (non-breaking change that fixes an issue)

Checklist

  • I have linked the related issue(s) in the description above
  • I have added tests that prove my fix is effective or that my feature works

ELI5

When the tool adds extra input fields, it now keeps the schema’s validation helper. Invalid inputs return structured validation errors instead of causing a crash.

Changes

  • Re-wrap extended Zod v4 schemas with toStandardSchema.
  • Preserve custom ~standard.jsonSchema adapters.
  • Add a regression test for malformed background input.
  • Add a patch changeset for @mastra/core.

…tion

Re-wrap extended Zod v4 input schemas with toStandardSchema after
CoreToolBuilder injects background/resume override fields. Without this,
safeExtendZodObject can drop a custom ~standard.jsonSchema adapter and
schema-invalid tool calls crash instead of returning validation errors.

Fixes mastra-ai#21170
@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

@Souravrajvi0 is attempting to deploy a commit to the Mastra Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a5c5898

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 25 packages
Name Type
@mastra/core Patch
@mastra/code-sdk Patch
mastracode Patch
@mastra/factory Patch
@mastra/mcp-docs-server Patch
@internal/playground Patch
@mastra/client-js Patch
@mastra/opencode Patch
@mastra/longmemeval Patch
mastra Patch
@mastra/deployer-cloud Patch
@mastra/react Patch
@mastra/playground-ui Patch
@mastra/server Patch
@mastra/deployer Patch
create-mastra Patch
create-factory Patch
@mastra/express Patch
@mastra/fastify Patch
@mastra/hono Patch
@mastra/koa Patch
@mastra/nestjs Patch
@mastra/next Patch
@mastra/tanstack-start Patch
@mastra/temporal Patch

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

@dane-ai-mastra dane-ai-mastra Bot added the complexity: low Low-complexity PR label Aug 10, 2026
@dane-ai-mastra

dane-ai-mastra Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

PR triage

Linked issue check passed (#21170).

Mastra uses CodeRabbit for automated code reviews. Please address all feedback from CodeRabbit by either making changes to your PR or leaving a comment explaining why you disagree with the feedback. Since CodeRabbit is an AI, it may occasionally provide incorrect feedback.


PR complexity score

Factor Value Score impact
Files changed 3 +6
Lines changed 47 +0
Author merged PRs 1 -1
Test files changed Yes -10
Final score -5

Applied label: complexity: low


Changed test gate

Changed Test Gate is pending. The Changed Test Gate / changed-tests check will update the test label when it completes.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 39d3fb15-2bc3-4e49-af22-33c9482b886c

📥 Commits

Reviewing files that changed from the base of the PR and between 796feee and a5c5898.

📒 Files selected for processing (3)
  • .changeset/coretoolbuilder-zod-v4-jsonschema-adapter.md
  • packages/core/src/tools/tool-builder/background-override-injection.test.ts
  • packages/core/src/tools/tool-builder/builder.ts

Walkthrough

This change re-wraps extended Zod v4 input schemas with toStandardSchema during background or resume field injection. Regression coverage verifies JSON Schema support, injected properties, and structured validation errors.

Changes

Zod v4 adapter preservation

Layer / File(s) Summary
Schema re-wrapping and regression coverage
packages/core/src/tools/tool-builder/builder.ts, packages/core/src/tools/tool-builder/background-override-injection.test.ts, .changeset/coretoolbuilder-zod-v4-jsonschema-adapter.md
CoreToolBuilder wraps extended Zod v4 schemas with toStandardSchema before storage. The regression test verifies JSON Schema metadata, injected properties, malformed input handling, and executor suppression. The changeset documents the patch.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: danielslew, wardpeet

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes preserving the JSON Schema adapter after Zod v4 schema injection, despite exceeding the ideal length.
Linked Issues check ✅ Passed The code re-wraps extended Zod v4 schemas and adds regression coverage for adapter preservation and structured validation errors [#21170].
Out of Scope Changes check ✅ Passed All changes support the linked bug fix through the implementation, regression test, and changeset documentation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/tools/tool-builder/background-override-injection.test.ts`:
- Around line 155-180: Extend the regression test around CoreToolBuilder to
invoke the built tool’s validation path with malformed _background data such as
enabled: 'yes', and assert it returns a structured validation error rather than
throwing a TypeError. If resumable-tool coverage is absent, add equivalent
invalid-input checks for suspendedToolRunId and resumeData.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d84bc5b6-c221-4859-a0b7-17f48593ed29

📥 Commits

Reviewing files that changed from the base of the PR and between e3a0621 and 0f71bbf.

📒 Files selected for processing (3)
  • .changeset/coretoolbuilder-zod-v4-jsonschema-adapter.md
  • packages/core/src/tools/tool-builder/background-override-injection.test.ts
  • packages/core/src/tools/tool-builder/builder.ts

Comment thread packages/core/src/tools/tool-builder/background-override-injection.test.ts Outdated

@mastra-platform mastra-platform Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. The one-line normalization at packages/core/src/tools/tool-builder/builder.ts:310 correctly restores a Mastra ~standard.jsonSchema adapter after Zod v4 .extend() injection while preserving the existing Zod validator. The regression test directly covers adapter retention and injected JSON Schema properties, and the existing suite covers valid execution plus resume/background field composition.\n\nVerification (credentials stripped):\n- pnpm build:core — passed (14/14 tasks)\n- focused Vitest background-override-injection.test.ts — passed (11/11)\n- git diff --check — passed\n- package check was attempted but is currently blocked by broad pre-existing workspace/type-resolution errors unrelated to this 3-file change; after the required build, the focused test reports no type errors.\n\nSecurity: no dependency, lockfile, workflow, prompt-processing, install-hook, or test-time execution changes. The patch only wraps an in-memory schema and adds a unit test/changeset.\n\nNon-blocking: CodeRabbit's suggestion to exercise malformed _background through the built tool would strengthen end-to-end regression coverage, but adapter presence plus the existing validation path is sufficient for this surgical fix. The two failing Vercel checks are authorization-gated previews for unrelated docs/playground targets.

@mastra-platform

Copy link
Copy Markdown
Contributor

Factory review handoff: APPROVE at head 0f71bbf2d66b64173661e41644178939ce7db8f8.\n\nReviewed issue #21170, PR/changed-file history, current review threads and checks, implementation consistency, tests, changeset, and security/install-time risk. The fix is scoped and correct. Local credential-stripped verification passed the core build and all 11 focused tool-builder tests. Remaining Vercel failures require deployment authorization and are unrelated to this core-only patch; the broad core check is presently noisy from unrelated workspace/type-resolution failures. No blocking findings.

Ehindero Israel and others added 2 commits August 11, 2026 10:25
Exercise the built tool validation path so the adapter regression cannot return as an unstructured TypeError.\n\nCo-Authored-By: Mastra Code (openai/gpt-5.6-sol) <noreply@mastra.ai>
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@wardpeet
wardpeet merged commit b2f0013 into mastra-ai:main Aug 12, 2026
91 of 94 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

complexity: low Low-complexity PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] CoreToolBuilder drops the ~standard.jsonSchema adapter when injecting background/resume fields on a Zod v4 schema

3 participants