Implementation:Neuml Txtai ToolFactory Create
| Knowledge Sources | |
|---|---|
| Domains | NLP, Agent |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete tool for dynamically assembling a unified list of smolagents Tool objects from heterogeneous specifications, provided by the txtai library.
Description
ToolFactory.create is a static method that takes an agent configuration dictionary, pops the "tools" list from it, and iterates over each element to produce a flat list[smolagents.Tool]. It serves as the single entry point for all tool creation in the txtai agent framework.
The method handles six distinct input types:
- smolagents.Tool instance -- passed through unchanged.
- Callable (function, method, or object with __call__) -- wrapped via ToolFactory.createtool, which first attempts smolagents.tool() (automatic creation from type annotations) and falls back to ToolFactory.fromdocs (parsing Google-style docstrings and wrapping in FunctionTool).
- Dictionary with Embeddings target or path/container keys -- creates an EmbeddingsTool.
- Dictionary with a generic callable target -- delegates to ToolFactory.createtool(target, dict).
- String matching a default alias ("python", "websearch", "webview") -- resolves to the corresponding built-in tool from ToolFactory.DEFAULTS.
- String starting with "http" -- fetches MCP (Model Context Protocol) tool collections via mcpadapt and extends the tools list.
- String ending with ".md" -- creates a SkillTool from a Markdown skill file.
The helper method createtool attempts automatic tool creation first. If that raises a TypeHintParsingException or TypeError, it falls back to fromdocs, which extracts the function name, docstring, and parameter signatures to build a FunctionTool configuration dictionary.
Usage
Use ToolFactory.create when you are building an agent and need to convert a heterogeneous list of tool specifications into a uniform list of smolagents.Tool objects. This is called automatically by ProcessFactory.create during agent construction, but can also be invoked directly for testing or custom agent assembly.
Code Reference
Source Location
- Repository: txtai
- File:
src/python/txtai/agent/tool/factory.py - Lines: 29-139
Signature
class ToolFactory:
DEFAULTS = {
"python": PythonInterpreterTool(),
"websearch": WebSearchTool(),
"webview": VisitWebpageTool(),
}
@staticmethod
def create(config):
"""
Creates a new list of tools from config["tools"].
Returns: list of smolagents.Tool
"""
tools = []
for tool in config.pop("tools", []):
# ... dispatch logic ...
return tools
@staticmethod
def createtool(target, config=None):
"""Try smolagents.tool(), fallback to fromdocs()."""
...
@staticmethod
def fromdocs(target, config):
"""Create FunctionTool from docstring and signature."""
...
Import
from txtai.agent.tool import ToolFactory
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | dict | Yes | Agent configuration dictionary. The "tools" key is popped and consumed. |
| config["tools"] | list | No | List of tool specifications. Each element can be one of the types described above. Defaults to an empty list if absent. |
| config["tools"][i] | Tool / callable / dict / str | Yes (per element) | Individual tool specification. See Description for dispatch rules. |
Outputs
| Name | Type | Description |
|---|---|---|
| tools | list[smolagents.Tool] | Flat list of instantiated Tool objects ready for use by a smolagents agent. |
Usage Examples
Basic Example
from txtai.agent.tool import ToolFactory
def summarize(text):
"""Summarize the given text.
Args:
text: The text to summarize
"""
return text[:200] + "..."
config = {
"tools": [
"websearch",
"python",
summarize,
{
"name": "calculator",
"description": "Perform arithmetic calculations",
"inputs": {
"expression": {
"type": "string",
"description": "Math expression to evaluate",
}
},
"target": lambda expression: eval(expression),
},
],
"model": "huggingface-hub/Meta-Llama-3-8B-Instruct",
}
tools = ToolFactory.create(config)
# tools is a list of 4 smolagents.Tool instances:
# [WebSearchTool, PythonInterpreterTool, FunctionTool(summarize), FunctionTool(calculator)]
With Embeddings and MCP
from txtai.agent.tool import ToolFactory
from txtai.embeddings import Embeddings
embeddings = Embeddings(path="sentence-transformers/all-MiniLM-L6-v2")
embeddings.index([{"id": "0", "text": "Sample document"}])
config = {
"tools": [
{
"name": "docs",
"description": "Search documentation",
"target": embeddings,
},
"http://localhost:8080/mcp", # MCP tool server
]
}
tools = ToolFactory.create(config)
# tools includes EmbeddingsTool + all tools from the MCP server