Skip to content

Commit ec62372

Browse files
authored
Fixes incorrect casting into int for numbers in string format (#384)
1 parent 689c54c commit ec62372

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: src/mcp/server/fastmcp/utilities/func_metadata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def pre_parse_json(self, data: dict[str, Any]) -> dict[str, Any]:
8888
pre_parsed = json.loads(data[field_name])
8989
except json.JSONDecodeError:
9090
continue # Not JSON - skip
91-
if isinstance(pre_parsed, str):
91+
if isinstance(pre_parsed, (str, int, float)):
9292
# This is likely that the raw value is e.g. `"hello"` which we
9393
# Should really be parsed as '"hello"' in Python - but if we parse
9494
# it as JSON it'll turn into just 'hello'. So we skip it.

Diff for: tests/server/fastmcp/test_func_metadata.py

+15
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,18 @@ def test_complex_function_json_schema():
399399
"title": "complex_arguments_fnArguments",
400400
"type": "object",
401401
}
402+
403+
404+
def test_str_vs_int():
405+
"""
406+
Test that string values are kept as strings even when they contain numbers,
407+
while numbers are parsed correctly.
408+
"""
409+
410+
def func_with_str_and_int(a: str, b: int):
411+
return a
412+
413+
meta = func_metadata(func_with_str_and_int)
414+
result = meta.pre_parse_json({"a": "123", "b": 123})
415+
assert result["a"] == "123"
416+
assert result["b"] == 123

0 commit comments

Comments
 (0)