Skip to content

Remove zod inference and use explicit TypeScript types #612

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

benjaminjkraft
Copy link

@benjaminjkraft benjaminjkraft commented Jun 11, 2025

Fixes #119, wherein more context on why these types are bad for users, but in short, this makes things readable by humans, and also probably improves TS perf and even in some cases correctness.

To do so, I replaced all the zod.infer stuff, which is a mess as described in the issue, with explicit types. I moved the JSDoc to the new types, since that's where humans will read now -- and indeed they will actually be able to read! I added return types explicitly to the client, there may be other places to do this as well which I'm happy to add or can be added over time. Type assertions ensure the new types match the schemas. (There are no runtime changes.)

This is a bit more annoying to maintain, of course -- the ideal would be to generate the schemas from the types and I assume there are packages to do so although I haven't explored the Zod universe lately. But in practice, Claude did all the work, so he can probably keep doing it as the types evolve.

🤖 Generated with Claude Code

Breaking Changes

This may fail users' TS builds when they upgrade, if the bad types were hiding bugs (as, in our codebase, they were). Those are already bugs for them, just ones TS couldn't catch. There are no runtime changes.

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 (n/a)
  • I have added or updated documentation as needed (n/a)

benjaminjkraft and others added 2 commits June 10, 2025 20:21
Eliminate all zod.infer usage throughout the codebase and replace with explicit TypeScript interfaces and types. Move comprehensive JSDoc documentation from zod schemas to corresponding TypeScript types. Add type assertions to ensure schema-type compatibility and update ESLint config to handle assertion types.

This makes things readable by humans, and also will probably improve TS perf, and in some cases even correctness, in callers.

Fixes modelcontextprotocol#119.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ihrpr
Copy link
Contributor

ihrpr commented Jun 12, 2025

@benjaminjkraft, thank you for working on this! Have you tried removing passthrough ?

@benjaminjkraft
Copy link
Author

I haven't -- it seems like that might be a deeper change (e.g. affecting runtime) and have less value (i.e. the types still won't be fully readable), but I'm not sure if I really know what it would mean concretely so I may be wrong!

@benjaminjkraft
Copy link
Author

Generally, though: I don't care if this is the approach. I care that the problem -- making this stuff readable for humans and TypeScript itself -- gets solved, and wrote the PR to show one way to solve it. If another approach can solve the problem better, I have zero attachment to the code in this PR!

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.

simplify return types
2 participants