Skip to content

Commit 07fc104

Browse files
committed
fix: handle strings containing numbers correctly
Fixes #62.
1 parent 2212d39 commit 07fc104

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/fastmcp/utilities/func_metadata.py

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

tests/test_func_metadata.py

+15
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@ def func_with_str_types(str_or_list: str | list[str]):
174174
assert result["str_or_list"] == ["hello", "world"]
175175

176176

177+
def test_str_vs_int():
178+
"""
179+
Test that string values are kept as strings even when they contain numbers,
180+
while numbers are parsed correctly.
181+
"""
182+
183+
def func_with_str_and_int(a: str, b: int):
184+
return a
185+
186+
meta = func_metadata(func_with_str_and_int)
187+
result = meta.pre_parse_json({"a": "123", "b": 123})
188+
assert result["a"] == "123"
189+
assert result["b"] == 123
190+
191+
177192
def test_skip_names():
178193
"""Test that skipped parameters are not included in the model"""
179194

0 commit comments

Comments
 (0)