Skip to content

Switch from Zod to Standard Schema#27

Merged
punkpeye merged 4 commits into
punkpeye:mainfrom
jacksteamdev:feat/adopt-standard-schema
Apr 1, 2025
Merged

Switch from Zod to Standard Schema#27
punkpeye merged 4 commits into
punkpeye:mainfrom
jacksteamdev:feat/adopt-standard-schema

Conversation

@jacksteamdev
Copy link
Copy Markdown
Contributor

@jacksteamdev jacksteamdev commented Apr 1, 2025

Overview

This PR replaces direct Zod usage with Standard Schema support, allowing users to choose their preferred validation library while maintaining existing functionality.

Changes

  • Added @standard-schema/spec and xsschema dependencies
  • Updated ToolParameters type to use StandardSchemaV1
  • Replaced zodToJsonSchema with toJsonSchema

Benefits

  • Users can now use any schema library that implements Standard Schema (Zod, Valibot, ArkType)
  • Aligns with ecosystem direction supported by schema library creators
  • Follows the example of libraries like tRPC, TanStack Form, and others adopting Standard Schema

Todo

  • Implement Standard Schema support
  • Update README with examples
  • Add usage examples for different schema libraries

Related Issues

Resolves #26

@jacksteamdev jacksteamdev marked this pull request as draft April 1, 2025 17:22
@jacksteamdev jacksteamdev changed the title feat: implement standard schema spec Switch from Zod to Standard Schema Apr 1, 2025
@jacksteamdev jacksteamdev force-pushed the feat/adopt-standard-schema branch from ca8da64 to 032679e Compare April 1, 2025 17:51
Comment thread package.json
"yargs": "^17.7.2",
"zod": "^3.24.2",
"zod-to-json-schema": "^3.24.3"
"zod-to-json-schema": "^3.24.5"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This library is a peer dependency of xsschema and isn't strictly required anymore but I left it because removing it would be a breaking change for Zod users.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would remove it. The whole idea behind all of this is not to add a new dependency all-together.
And it won't break in runtime but at the install moment. Seems safe enough.

@jacksteamdev jacksteamdev marked this pull request as ready for review April 1, 2025 17:55
@punkpeye punkpeye merged commit ac144df into punkpeye:main Apr 1, 2025
@punkpeye
Copy link
Copy Markdown
Owner

punkpeye commented Apr 1, 2025

@jacksteamdev Awesome contribution. Thank you ❤️

@jacksteamdev
Copy link
Copy Markdown
Contributor Author

@punkpeye Happy to help. Thanks for all your work in the MCP scene!

@jacksteamdev jacksteamdev deleted the feat/adopt-standard-schema branch April 1, 2025 18:49
Comment thread package.json
"yargs": "^17.7.2",
"zod": "^3.24.2",
"zod-to-json-schema": "^3.24.3"
"zod-to-json-schema": "^3.24.5"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would remove it. The whole idea behind all of this is not to add a new dependency all-together.
And it won't break in runtime but at the install moment. Seems safe enough.

Comment thread package.json
"@types/node": "^22.13.5",
"@types/uri-templates": "^0.1.34",
"@types/yargs": "^17.0.33",
"@valibot/to-json-schema": "^1.0.0",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

adding all of these dependencies seems wrong.
They should be optional peer dependencies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These are dev dependencies for src/examples/addition.ts

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I agree @qcho we need to add @valibot/to-json-schema to optional peer deps.
And, I'd love to add zod/zod-to-json-schema to peer deps

@LunreMore
Copy link
Copy Markdown

Core Issue: When a single bundle file is too large, asynchronously importing dependencies can significantly increase loading time. If the server is started before asynchronous loading is complete, toJsonSchema will be undefined.

FastMCP registers multiple tools upon startup. Each tool's parameter definition (such as Zod, ArkType, and Valibot schema) triggers the asynchronous import mechanism of xsschema. xsschema dynamically loads a converter library (such as zod-to-json-schema and @valibot/to-json-schema) based on the schema type. The schema is then converted to JSON Schema for tool parameter validation. Finally, the server is started and the client waits for calls.

It is recommended to precompile the package for static import, or at least check and wait for the package to be imported before calling toJsonSchema.

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.

Replace Zod with Standard Schema for validation

5 participants