fix: handle Optional[str] in pre_parse_json to prevent incorrect type coercion#2554
Open
Maanik23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: handle Optional[str] in pre_parse_json to prevent incorrect type coercion#2554Maanik23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Maanik23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
… coercion
The pre_parse_json check ield_info.annotation is not str fails for union
types like Optional[str] (str | None), causing string values like '1.2' or
'{key: value}' to be incorrectly parsed into float/dict via json.loads,
which then fails Pydantic validation.
Add _annotation_accepts_str() helper that correctly identifies str-only unions
(Optional[str], str | None) as string-accepting annotations, while still
allowing JSON pre-parsing for unions with other complex types (str | list[str]).
Fixes modelcontextprotocol#381
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.
Fixes #381
Summary
The
pre_parse_jsonmethod infunc_metadata.pyusesfield_info.annotation is not strto decide whether to attempt JSON parsing of string values. This check fails for union types likeOptional[str](str | None) because the annotation object isstr | None, notstr.This causes string values like
"1.2"or'{"key": "value"}'to be parsed viajson.loadsintofloat/dict, which then fails Pydantic validation withInput should be a valid string.Reproducer (from issue)
Changes
src/mcp/server/mcpserver/utilities/func_metadata.py_annotation_accepts_str()helper that correctly identifies str-only unions (Optional[str],str | None) as string-accepting annotationsis_union_origin(already imported) andget_origin/get_args(already imported) for introspectionstr | list[str]still gets JSON pre-parsing)tests/server/mcpserver/test_func_metadata.pytest_optional_str_preserves_json_string: numeric strings, JSON objects, and JSON arrays all stay asstrwhen annotation isOptional[str]test_pipe_none_str_preserves_json_string: same coverage forstr | Nonesyntaxtest_optional_str_runtime_validation: end-to-endcall_fn_with_arg_validationwithOptional[str]paramBackward compatibility
strannotations (already handled by PR Avoiding to parse string arguments containing valid JSON if the argument annotation is str #1113)str | list[str]unions (still parses JSON arrays into lists)Optional[str]andstr | Noneto correctly preserve string valuestest_func_metadata.pycontinue to pass