Skip to content

registerTool: accept ZodType<object> for input and output schema #816

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

ksinder
Copy link

@ksinder ksinder commented Jul 28, 2025

Motivation and Context

See #588. This PR adds a backwards-compatible change to registerTool on McpServer to allow any Zod schema extending ZodType<object> instead of requiring inputSchema and outputSchema to conform to ZodRawShape (:= 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 use z.union(...) and z.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 with npm run test.

Also manually tried changing the test locally to use inputSchemas for types that aren't objects at the top level -- e.g. z.number() and z.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 all ZodType<object> to ZodTypeAny!)

image

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 ZodRawShapes being passed in vs. direct ZodType<object> and branch accordingly. ZodType<object> is added as a union type variant for server.registerTool, and isn't mandatory.

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

N/A

@ksinder ksinder requested a review from a team as a code owner July 28, 2025 17:30
@ksinder ksinder requested a review from ihrpr July 28, 2025 17:30
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.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy