Tool Invocation
This example shows how to:
Register a custom
ToolAgent
withLocalAgentEngine
Define and use a simple Python function as an agent tool
Run a complete async test to ensure the
FunctionTool
works.
1️⃣ Import Dependencies
We first import necessary modules for creating and running the agent engine.
import asyncio
import json
import logging
from agentopera.engine import EVENT_LOGGER_NAME, AgentId, CancellationToken, FunctionCall, LocalAgentEngine
from agentopera.engine.types.models import FunctionExecutionResult
from agentopera.engine.agent.tool_agent import ToolAgent
from agentopera.engine.function_call import FunctionTool
logging.getLogger(EVENT_LOGGER_NAME).setLevel(logging.INFO)
2️⃣ Define the Tool Function
Our agent will execute this simple function when called.
def _pass_function(input: str) -> str:
return "pass"
3️⃣ Register ToolAgent
and Test
ToolAgent
and TestWe will now register the agent and send it a FunctionCall
to check if it responds as expected.
async def test_tool_agent() -> None:
engine = LocalAgentEngine()
await ToolAgent.register(
engine,
"tool_agent",
lambda: ToolAgent(
description="Tool agent",
tools=[FunctionTool(_pass_function, name="pass", description="Pass function")],
),
)
agent = AgentId("tool_agent", "default")
engine.start()
# Test pass function
result = await engine.send_message(
FunctionCall(id="1", arguments=json.dumps({"input": "pass"}), name="pass"), agent
)
print(f"result: {result}")
assert result == FunctionExecutionResult(call_id="1", content="pass", is_error=False, name="pass")
await engine.stop()
4️⃣ Run the Test
Now we trigger the async function to test the agent!
await test_tool_agent()
result: content='pass' name='pass' call_id='1' is_error=False
Last updated