Skip to content

Conversation

@felixweinberger
Copy link
Contributor

Use [T] extends [U] pattern on the AnySchema check to prevent distributive conditional types.

Motivation and Context

Fixes #1180. After upgrading from v1.22.0 to v1.23.0, users get TS2589: Type instantiation is excessively deep and possibly infinite when using registerTool. The issue was that AnySchema (z3.ZodTypeAny | z4.$ZodType) as a union caused TypeScript to distribute the conditional type check, leading to exponential type instantiation.

How Has This Been Tested?

  • npm run build passes
  • npm test passes (1129 tests)
  • npm run lint passes

Breaking Changes

None. This is a type-level fix only.

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 27, 2025

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/sdk@1181

commit: 192d981

Use [T] extends [U] pattern on the AnySchema check to prevent
distributive conditional types. This fixes the "Type instantiation
is excessively deep and possibly infinite" error that occurs when
using registerTool after upgrading to v1.23.0.

The issue was that AnySchema (z3.ZodTypeAny | z4.$ZodType) as a union
caused TypeScript to distribute the conditional type check, leading
to exponential type instantiation.

Fixes #1180
@mattzcarey
Copy link
Contributor

I'm not convinced this is the reason. We can revisit if it crops up in future though.

@felixweinberger
Copy link
Contributor Author

I'm not convinced this is the reason. We can revisit if it crops up in future though.

yeah I couldn't actually repro on my machine, and looks like it didn't solve the reporter's issue either so having 2 zods next to each other like you said

@felixweinberger felixweinberger deleted the fweinberger/issue-1180 branch December 15, 2025 18:11
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.

[BUG] TS2589: Type instantiation is excessively deep and possibly infinite

3 participants