Skip to content

feat: add optional strict parameter to registerTool for Zod validation #846

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 3 commits into
base: main
Choose a base branch
from

Conversation

abloomston
Copy link

@abloomston abloomston commented Aug 5, 2025

Add optional strict parameter to registerTool() config that enables Zod's strict validation to reject unknown parameters instead of silently ignoring them.

Motivation and Context

The MCP TypeScript SDK currently silently ignores unknown parameters in tool calls, which causes issues where parameter name typos are silently dropped, leading to confusing behavior where tools execute with missing data.

How Has This Been Tested?

  • Added comprehensive test case demonstrating the issue and fix

Breaking Changes

None. The strict parameter defaults to false for backward compatibility. Existing code will continue to work unchanged.

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

You should consider this PR in concert with #792 which makes changes that may be related.

Implementation Details:

  • Added strict?: boolean to registerTool config interface
  • When strict=true, applies z.object(inputSchema).strict() to reject unknown parameters
  • When strict=false (default), maintains current lenient behavior
  • Only available in registerTool() method - legacy tool() method uses lenient validation for compatibility

Use Cases:

  • Development: Catch parameter name typos early
  • Production APIs: Ensure clients send only expected parameters
  • Security-sensitive tools: Prevent injection of unexpected data

The feature follows existing patterns in the codebase and maintains full backward compatibility while solving a real pain point for developers.

LLM Disclosure

This PR was created with the assistance of Claude Code, Claude Haiku 3.5, Claude Sonnet 4.0, and Chat-GPT 4.1 nano.

Adam Bloomston added 3 commits August 5, 2025 11:00
  This test demonstrates the current issue where tools accept unknown
  parameters (e.g. incorrect capitalization) without validation errors.
  The test uses 'username' and 'itemcount' instead of the expected
  'userName' and 'itemCount' parameters, which should be rejected
  but currently aren't.

  This test is expected to fail until strict validation is implemented.
  Add strict parameter to registerTool config that defaults to false for
  backward compatibility. When strict=true, applies .strict() to Zod
  schema creation for tool input validation to throw errors on unknown
  parameters instead of silently ignoring them.

  - Add strict?: boolean to registerTool config interface
  - Modify _createRegisteredTool to accept and use strict parameter
  - Apply z.object(inputSchema).strict() when strict=true
  - Update legacy tool() method to pass strict=false (backward compatibility)
  - Update test to verify strict validation rejects unknown parameters
  - All existing tests continue to pass (no breaking changes)

  This fixes the issue where parameter name typos are silently dropped,
  leading to confusing behavior where tools execute with missing data.
  Add Advanced Usage section explaining the strict parameter for registerTool:
  - Show examples of strict vs lenient validation
  - Explain when to use each mode (development vs production)
  - Document that strict parameter is only available in registerTool()
  - Note that legacy tool() method uses lenient validation for compatibility

  This helps developers understand when and how to use strict validation
  to catch parameter name typos and unexpected data.
@abloomston abloomston requested a review from a team as a code owner August 5, 2025 17:45
@abloomston abloomston requested a review from domdomegg August 5, 2025 17:45
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