Customizing Tools in CrewAI: Expanding Agent Capabilities
-
Custom tools are a powerful and versatile way to extend the functionality of CrewAI agents. They are easy to create, requiring only the inheritance of CrewAI's baseTool class. Once inherited, the tool's name and description can be defined as follows:
from crewai_tools import BaseTool class MyCustomTool(BaseTool): name: str = "Name of my tool" description: str = "What this tool does. It's vital for effective utilization." def _run(self, argument: str) -> str: # Your tool's logic here return "Tool's result"
The _run method can accept multiple arguments, which CrewAI will automatically parse from the user's query. This behavior is similar to OpenAI's tools and can be further customized using an args schema for more precise argument parsing.
In addition to inheriting from BaseTool, a simpler approach involves using the @tool decorator. For instance:from crewai_tools import tool @tool("Tool Name") def my_simple_tool(question: str) -> str: """Tool description for clarity.""" # Tool logic here return "Tool output"
The method's docstring serves as the tool's description, making this approach more concise.
Leveraging custom tools empowers CrewAI to connect with various databases, third-party APIs, and more.