Skip to content

Tool Class

The Tool class represents a generated tool and provides methods for execution and framework adaptation.

PropertyTypeDescription
namestrTool identifier.
intentstrNatural language description.
inputslist[InputParam]Input parameter definitions.

Execute the tool and return the result.

result = tool.run(**kwargs) -> Any

Accepts keyword arguments matching the tool’s input parameters.

The result from the tool’s run() function.

RuntimeError - If execution fails and self-healing is disabled or exhausted.

result = tool.run(query="python tutorials", limit=10)
print(result)

Execute the tool without raising exceptions.

result = tool.run_safe(**kwargs) -> ToolResult

ToolResult - Object with success, data, and error fields.

result = tool.run_safe(query="test")
if result.success:
print(result.data)
else:
print(f"Error: {result.error}")

Execute the tool asynchronously.

result = await tool.run_async(**kwargs) -> Any
import asyncio
async def main():
result = await tool.run_async(query="test")
print(result)
asyncio.run(main())

Execute the tool asynchronously without raising exceptions.

result = await tool.run_safe_async(**kwargs) -> ToolResult
async def main():
result = await tool.run_safe_async(query="test")
if result.success:
print(result.data)

Collect inputs via CLI prompts and execute.

result = tool.run_interactive() -> Any

Prompts the user for each input parameter, then executes the tool.

# Prompts user for inputs
result = tool.run_interactive()
# Enter query: python tutorials
# Enter limit [10]:
# Result: {...}

Convert to a LangChain BaseTool.

lc_tool = tool.to_langchain() -> BaseTool

BaseTool - LangChain-compatible tool.

Terminal window
pip install "anvil-agent[langchain]"
from langchain.agents import create_tool_calling_agent
lc_tool = tool.to_langchain()
agent = create_tool_calling_agent(llm, [lc_tool], prompt)

Convert to a CrewAI BaseTool.

crew_tool = tool.to_crewai() -> BaseTool

BaseTool - CrewAI-compatible tool.

Terminal window
pip install "anvil-agent[crewai]"
from crewai import Agent
crew_tool = tool.to_crewai()
agent = Agent(role="Researcher", tools=[crew_tool])

Convert to an AutoGen FunctionTool.

ag_tool = tool.to_autogen() -> FunctionTool

FunctionTool - AutoGen-compatible tool.

Terminal window
pip install "anvil-agent[autogen]"
from autogen_agentchat.agents import AssistantAgent
ag_tool = tool.to_autogen()
agent = AssistantAgent(name="assistant", tools=[ag_tool])

Convert to an OpenAI Agents SDK FunctionTool.

oai_tool = tool.to_openai_agents() -> FunctionTool

FunctionTool - OpenAI Agents-compatible tool.

Terminal window
pip install "anvil-agent[openai-agents]"
from agents import Agent
oai_tool = tool.to_openai_agents()
agent = Agent(name="assistant", tools=[oai_tool])

Chain this tool with another tool.

chain = tool.pipe(other: Tool) -> ToolChain
ParameterTypeDescription
otherToolTool to chain after this one.

ToolChain - A chain of tools for sequential execution.

chain = search_tool.pipe(summarize_tool).pipe(translate_tool)
result = chain.run(query="AI news")

Get the list of input parameters.

inputs = tool.get_inputs() -> list[InputParam]

list[InputParam] - List of input parameter definitions.

for param in tool.get_inputs():
print(f"{param.name}: {param.param_type} (required={param.required})")

Check if the tool has any required inputs.

has_required = tool.has_required_inputs() -> bool

bool - True if any input is required.

Returned by run_safe() methods.

from anvil.models import ToolResult
result = ToolResult(
success: bool,
data: Any = None,
error: str | None = None,
)
FieldTypeDescription
successboolWhether execution succeeded.
dataAnyResult data on success.
errorstr | NoneError message on failure.
result = tool.run_safe(query="test")
if result.success:
process(result.data)
else:
log_error(result.error)

Created by pipe() method.

MethodDescription
run(**kwargs)Execute chain, raising on error.
run_safe(**kwargs)Execute chain safely.
run_async(**kwargs)Execute chain asynchronously.
run_safe_async(**kwargs)Execute chain async safely.
pipe(tool)Add another tool to the chain.
PropertyTypeDescription
len(chain)intNumber of tools in chain.
str(chain)strString representation.
from anvil import ToolChain
# Create chain
chain = ToolChain([tool1, tool2, tool3])
# Or use pipe
chain = tool1.pipe(tool2).pipe(tool3)
# Execute
result = chain.run(initial="data")
print(len(chain)) # 3
print(chain) # ToolChain(tool1 -> tool2 -> tool3)