ahvn.tool.mixin module

ahvn.tool.mixin.reg_toolspec(func=None, examples=None, parse_docstring=True, *args, **kwargs)[source]

Decorator to mark a method as a tool that should be registered as a ToolSpec.

This decorator can be used with or without arguments: - @reg_toolspec - @reg_toolspec(parse_docstring=True, description=”Custom description”)

The ToolSpec is created when to_toolspecs() is called on an instance.

Parameters:
  • func (Optional[Callable], optional) – The function to decorate. If None, returns a partial decorator. Defaults to None.

  • examples (Optional[Iterable[ExperienceType]], optional) – Example usages of the tool. Defaults to None.

  • parse_docstring (bool, optional) – Whether to parse the function’s docstring for description. Defaults to True.

  • *args – Additional positional arguments to pass to FastMCPTool.from_function.

  • **kwargs – Additional keyword arguments to pass to FastMCPTool.from_function.

Returns:

The decorated function with tool metadata attached.

Return type:

Callable

Example

```python class MyKLBase(ToolRegistry):

@reg_toolspec(parse_docstring=True) def search(self, query: str) -> str:

“””Search for items.

Args:

query: The search query string.

Returns:

str: The search results.

“”” return f”Results for: {query}”

```

class ahvn.tool.mixin.ToolRegistry[source]

Bases: object

A mixin class that provides tool registration and management capabilities.

This class uses __init_subclass__ to automatically detect and register methods decorated with @reg_toolspec during class definition, storing the metadata for later ToolSpec creation.

The @reg_toolspec decorator is automatically available as a class method on any subclass.

Example

```python class MyCustomKLBase(KLBase):

@reg_toolspec(parse_docstring=True) def search(self, query: str) -> str:

“””Search for items.

Args:

query: The search query string.

Returns:

str: The search results.

“”” return f”Results for: {query}”

# ToolSpecs are created when to_toolspecs() is called kb = MyCustomKLBase() tools = kb.to_toolspecs() # Creates and returns ToolSpec instances ```

classmethod __init_subclass__(**kwargs)[source]

Called when a subclass is created. Scans for decorated methods and stores their metadata.

toolspec(name)[source]
Return type:

ToolSpec

Parameters:

name (str)

to_toolspecs()[source]

Collect all methods decorated with @reg_toolspec and convert them to ToolSpec instances.

This method creates ToolSpec instances from the registered tool metadata, binding ‘self’ to each tool so they can be called as instance methods.

Returns:

A named list of ToolSpec instances for all decorated methods.

Return type:

Dict[ToolSpec]

list_toolspecs()[source]

List the names of all methods decorated with @reg_toolspec.

Returns:

A list of method names that are registered as tools.

Return type:

List[str]