registerTool
: accept ZodType<object> for input and output schema
#816
+250
−13
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.
Motivation and Context
See #588. This PR adds a backwards-compatible change to
registerTool
onMcpServer
to allow any Zod schema extendingZodType<object>
instead of requiringinputSchema
andoutputSchema
to conform toZodRawShape
(:=Record<string, ZodType<any>>
).The underlying schema validation and
zod-to-json-schema
dependency already support this, so adding this plumbing expands SDK expressiveness and flexibility. Specifically, this unblocks MCP servers' ability to define tool schemas that usez.union(...)
andz.intersection(...)
. Historically, some creators have had to wrap existing schemas in some wrapper object that has fixed keys, e.g.{bodyParams: z.union(...)}
to work around this limitation.How Has This Been Tested?
Added automated tests in
mcp.test.ts
and confirmed they pass locally withnpm run test
.Also manually tried changing the test locally to use
inputSchema
s for types that aren't objects at the top level -- e.g.z.number()
andz.union([z.number(), z.object({})])
, and confirmed typechecking fails (which is good, since I'm guessing we still want guardrails to push toward objects for params and responses. If this ever changes in the future it's easy enough to change allZodType<object>
toZodTypeAny
!)Breaking Changes
Should be backwards-compatible in the SDK with existing server code. This is purely additive since we can easily dynamically distinguish between existing
ZodRawShape
s being passed in vs. directZodType<object>
and branch accordingly.ZodType<object>
is added as a union type variant forserver.registerTool
, and isn't mandatory.Types of changes
Checklist
Additional context
N/A