Make zod types strip instead of passthrough #792
+1,875
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
This addresses issues raised in:
.strict()
The Problem
MCP is an extensible protocol. Servers can add custom fields. But our Zod schemas using
.passthrough()
were too loose - TypeScript couldn't catch typos or provide good autocomplete.There solutions with
.strict()
schemas (see mcp-types), but that breaks when talking to servers with extensions - you get runtime errors for any unknown field.This PR approach
We now generate a
strictTypes.ts
file that uses Zod's.strip()
method instead of.passthrough()
or.strict()
:Benefits
.strict()
, this approach doesn't throw errors for unknown fieldsExample
We can't just change it now, we need to wait for the major release v2. But we still can deprecate the types and start using typesafe eversion.
Migration Guide
Example for new projects
Use
strictTypes.js
by default:For Existing Projects
types.js
tostrictTypes.js
If You Need Extensibility
If you need to preserve additional fields, you have options:
types.js
(deprecated but still available)Implementation Details
The
strictTypes.ts
file is auto-generated fromtypes.ts
by:.passthrough()
with.strip()
.passthrough()
Until the v2 version, we need to make sure we have backwards compatibility so the generation script runs automatically during the build process to ensure both files stay in sync.
V2 version of the SDK:
types.js
file is deprecated and will be removed in a future major versionstrictTypes.js