mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 15:19:10 +02:00
Fix method-bound tools (#1360)
This commit is contained in:
parent
5ee159a4b3
commit
d4fc44feb4
2 changed files with 35 additions and 1 deletions
|
|
@ -101,7 +101,12 @@ def get_cached_typeadapter(cls: T) -> TypeAdapter[T]:
|
|||
new_func.__module__ = cls.__module__
|
||||
new_func.__qualname__ = getattr(cls, "__qualname__", cls.__name__)
|
||||
new_func.__annotations__ = processed_hints
|
||||
return TypeAdapter(new_func)
|
||||
|
||||
if inspect.ismethod(cls):
|
||||
new_method = types.MethodType(new_func, cls.__self__)
|
||||
return TypeAdapter(new_method)
|
||||
else:
|
||||
return TypeAdapter(new_func)
|
||||
|
||||
return TypeAdapter(cls)
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,19 @@ class SomeComplexModel(BaseModel):
|
|||
y: dict[int, str]
|
||||
|
||||
|
||||
class ClassWithMethods:
|
||||
def do_something(self, x: int) -> int:
|
||||
return x
|
||||
|
||||
def do_something_annotated(
|
||||
self, x: Annotated[int, Field(description="A description")]
|
||||
) -> int:
|
||||
return x
|
||||
|
||||
def do_something_return_none(self) -> None:
|
||||
return None
|
||||
|
||||
|
||||
def complex_arguments_fn(
|
||||
an_int: int,
|
||||
must_be_none: None,
|
||||
|
|
@ -242,3 +255,19 @@ def test_str_vs_int():
|
|||
type_adapter = get_cached_typeadapter(func_with_str_and_int)
|
||||
result = type_adapter.validate_python({"a": "123", "b": 123})
|
||||
assert result == "123"
|
||||
|
||||
|
||||
def test_class_with_methods():
|
||||
"""Test that class methods are not included in the schema"""
|
||||
class_with_methods = ClassWithMethods()
|
||||
type_adapter = get_cached_typeadapter(class_with_methods.do_something)
|
||||
schema = type_adapter.json_schema()
|
||||
assert "self" not in schema["properties"]
|
||||
|
||||
type_adapter = get_cached_typeadapter(class_with_methods.do_something_annotated)
|
||||
schema = type_adapter.json_schema()
|
||||
assert "self" not in schema["properties"]
|
||||
|
||||
type_adapter = get_cached_typeadapter(class_with_methods.do_something_return_none)
|
||||
schema = type_adapter.json_schema()
|
||||
assert "self" not in schema["properties"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue