Jeremiah Lowin commited on
Commit
b3b3047
·
unverified ·
2 Parent(s): 528490b7b07463

Merge pull request #63 from sd2k/handle-string-args-containing-numbers

Browse files
src/fastmcp/utilities/func_metadata.py CHANGED
@@ -91,7 +91,7 @@ class FuncMetadata(BaseModel):
91
  pre_parsed = json.loads(data[field_name])
92
  except json.JSONDecodeError:
93
  continue # Not JSON - skip
94
- if isinstance(pre_parsed, str):
95
  # This is likely that the raw value is e.g. `"hello"` which we
96
  # Should really be parsed as '"hello"' in Python - but if we parse
97
  # it as JSON it'll turn into just 'hello'. So we skip it.
 
91
  pre_parsed = json.loads(data[field_name])
92
  except json.JSONDecodeError:
93
  continue # Not JSON - skip
94
+ if isinstance(pre_parsed, (str, int, float)):
95
  # This is likely that the raw value is e.g. `"hello"` which we
96
  # Should really be parsed as '"hello"' in Python - but if we parse
97
  # it as JSON it'll turn into just 'hello'. So we skip it.
tests/test_func_metadata.py CHANGED
@@ -174,6 +174,21 @@ def test_str_vs_list_str():
174
  assert result["str_or_list"] == ["hello", "world"]
175
 
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  def test_skip_names():
178
  """Test that skipped parameters are not included in the model"""
179
 
 
174
  assert result["str_or_list"] == ["hello", "world"]
175
 
176
 
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
+
192
  def test_skip_names():
193
  """Test that skipped parameters are not included in the model"""
194