Skip to content

feat: Add duck-typed isZodType guard for Zod schemas across bundles #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

bohdan-romanchenko
Copy link

This PR introduces a new runtime type‐guard, isZodType, which uses duck‐typing (inspecting the internal _def.typeName) instead of instanceof or any to reliably detect ZodTypeAny objects—even when multiple copies of Zod are loaded.

Motivation and Context

When bundling or installing Zod in more than one place (e.g. your app plus @vercel/mcp-adapter), using instanceof ZodType can fail because each bundle has its own constructor/prototype.
This change:

  • Eliminates the brittle instanceof check
  • Avoids the use of any by starting from unknown and narrowing step by step
  • Provides clear documentation for future maintainers

How Has This Been Tested?

  • Unit tests covering positive and negative cases for isZodType (various Zod schema instances and non-Zod objects)
  • Manual smoke tests in a sample app with multiple Zod versions loaded

Breaking Changes

None. Existing code using isZodRawShape or instanceof ZodType will continue to work, and consumers who switch to the new guard will see no change in external behavior.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Fix of #467 and vercel-labs/mcp-for-next.js#9

@bhosmer-ant
Copy link
Contributor

@bohdan-romanchenko thank you for this - just landed #468 which addresses this problem in essentially the same way (great minds :)

@bhosmer-ant bhosmer-ant closed this May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants