Skip to content

Commit 5abe811

Browse files
committed
fix(fastmcp): handle strings containing numbers correctly
This is a port of jlowin/fastmcp#63 to this repo - I guess the transition was made before this PR was merged into fastmcp, so it wasn't included here. See jlowin/fastmcp#62 for the initial bug report.
1 parent 4770bcd commit 5abe811

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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.

tests/server/fastmcp/test_func_metadata.py

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

178178

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

0 commit comments

Comments
 (0)