Custom tool implementation with @tool decorator

My code not working when I try to implement custom tools for Project Zip and File Write functionality. After investigation, following lines are causing issue for @tool decorator in my custom tool file called file_tool.py.
from crewai_tools.tools import tool
from crewai.tools import tool
from crewai import tool

Below are my complete file_tool.py. Can someone guide me to achieve custom tool implementation?

file_tool.py:
from crewai_tools.tools import tool
import os
import zipfile

@tool
def write_file(path: str, content: str) → str:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, ‘w’) as f:
f.write(content)
return f"File written to {path}"

@tool
def zip_project(folder_path: str, zip_name: str) → str:
zip_path = f"{zip_name}.zip"
with zipfile.ZipFile(zip_path, ‘w’, zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(folder_path):
for file in files:
zipf.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file), folder_path))
return f"Zipped project at {zip_path}"

I am consuming this tool in my agent code
@agent
def DeveloperAgent(self) → Agent:
return Agent(
config=self.agents_config[‘DeveloperAgent’], # type: ignore[index]
verbose=True,
tools=[write_file, zip_project]
)

Let me any other supporting code required for analysis and help with fix

Use the base tool instead of the @tool decorator

1 Like

Hi Ezeokeke,

I’m nearing completion of my POC, and everything has been generated as expected — the CrewAI framework successfully created the required JSON file and saved it to the specified path. The next step involves parsing the JSON and organizing files into appropriate directories. For this purpose, I created a custom_tool, a final agent, and a task to complete the process.

However, I encountered an issue when attempting to include the custom tool in the agent’s signature in crew.py. Specifically, the error occurs immediately after I add the tools. Here’s the relevant code snippet:

python

@agent
def file_writer_agent(self) -> Agent:
    return Agent(
        config=self.agents_config['file_writer_agent'],  # type: ignore[index]
        tools=[file_system_tools.write_file],            # type: ignore[index]
        verbose=True
    )

I prefer not to revert to the older structure and noticed that a similar issue was reported as “Custom tools not working #949.” Is this a known limitation, and are there any workarounds or recommended approaches to make this work with the current structure?

Appreciate your guidance.

The issue is how you are passing the tools to the agent. All you have to do is to do something like this:

Sample Code:

from insure_agent.tools.premium_calculator_tool import PremiumCalculatorTool
from insure_agent.tools.insurance_plan_database_tool import InsurancePlanDatabaseTool

@agent
def coverage_analyst_agent(self) → Agent:
“”“Creates the insurance needs analyst with specialized tools”“”
# Initialize specialized tools for coverage analysis

    return Agent(
        config=self.agents_config["CoverageAnalystAgent"],
        tools=[premium_calculator_tool, insurance_plan_database_tool],
        llm=self.llm_2,
        max_rpm=40,
        max_iter=3,
        verbose=True
    )

From the above code, i am importing my tool from the tool folder where my custom tool files are stored and i am giving it as a tool to the agent. Do you understand?