fix(fetch): accept null for optional parameters#3982
Open
rohitg00 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(fetch): accept null for optional parameters#3982rohitg00 wants to merge 1 commit intomodelcontextprotocol:mainfrom
rohitg00 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Closes modelcontextprotocol#2035. Clients like LibreChat send explicit `null` for optional params instead of omitting them. The Fetch model declared `max_length: int`, `start_index: int`, and `raw: bool`, so Pydantic rejected `null` at the type-coercion step before field defaults could apply. Adds a `mode="before"` field_validator that maps `None` back to the field's declared default. Keeps field types as `int`/`bool`, preserves the `gt=0`/`lt=1_000_000`/`ge=0` validators on resolved values, and does not rely on falsy-coalescing (which would have replaced valid `start_index=0` or explicit `raw=false`). Adds 9 tests covering null handling, default preservation, explicit zero/false preservation, and validator behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #2035.
Clients like LibreChat send explicit
nullfor optional Pydantic parameters. The Fetch model typed them asint/bool, so Pydantic rejectednullat type coercion before field defaults could apply.Fix
Adds a
mode="before"field_validator onmax_length,start_index, andrawthat mapsNoneto the field's declared default. Fields keep theirint/booltypes, validators (gt=0,lt=1_000_000,ge=0) continue to enforce on resolved values, and falsy-coalescing is avoided so validstart_index=0and explicitraw=falsepass through unchanged.Tests
9 new tests covering:
start_index=0,raw=false)Full suite: 29 passed, ruff clean, pyright 0 errors.
AI disclosure
Human-led, drafted with Claude Code assistance. I chose the validator pattern after reviewing closed PR #2631 (which used
orcoalescing that silently discards valid falsy values likestart_index=0). The fix was tested locally before opening the PR.