Skip to content

Fix isZodRawShape return false on empty object and add test #448

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

Merged
merged 1 commit into from
May 8, 2025

Conversation

yuna0x0
Copy link
Contributor

@yuna0x0 yuna0x0 commented May 5, 2025

Fix isZodRawShape implementation to properly handle the empty object and add the test.

Close #453

Motivation and Context

The isZodRawShape type guard function was incorrectly returning false for the empty object. This caused issues when trying to register tools with empty parameters. The fix ensures that empty objects are correctly identified as valid ZodRawShape instances, which allows for proper tool registration with empty parameter schemas.

How Has This Been Tested?

Added a new test case in mcp.test.ts that specifically tests registering a tool with an empty parameters object. The test verifies that the tool is correctly registered with an empty object schema and that the proper annotations are applied.

Additionally, manual testing regarding types accept by ZodRawShape was done with a simple test snippet:

import { type ZodRawShape } from 'zod';

class Test {
  zodRawShape(input: ZodRawShape) {
    console.log(input);
  }
}

const test = new Test();
test.zodRawShape({}); // type check succeed - now works correctly on our type guard
test.zodRawShape(null); // type check failed as expected

Breaking Changes

None. This is a bug fix that maintains backward compatibility.

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

The fix modifies the isZodRawShape type guard to explicitly handle empty objects as valid ZodRawShape instances. This is done by using Zod's own validation to check if the object is an empty object. If it is, we consider it a valid ZodRawShape. Otherwise, we fall back to the original check which verifies if any property is a ZodType instance.

The issue was particularly important for tools that don't require any parameters but still need to be properly registered with the MCP server.

@yuna0x0 yuna0x0 changed the title Fix isZodRawShape return false on empty object and add test Fix isZodRawShape return false on empty object and add test May 5, 2025
@Cloudkkk
Copy link

Cloudkkk commented May 7, 2025

hope to fix

@bhosmer-ant bhosmer-ant self-requested a review May 8, 2025 16:55
Copy link
Contributor

@bhosmer-ant bhosmer-ant left a comment

Choose a reason for hiding this comment

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

Thank you!

@bhosmer-ant bhosmer-ant merged commit 2a8efaa into modelcontextprotocol:main May 8, 2025
2 checks passed
@yuna0x0 yuna0x0 deleted the fix-zod-raw-shape branch May 9, 2025 02:33
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.

MCP server can’t properly handle tool without args
3 participants