How to create custom tool for CrewAI
Quick answer
To create a custom tool for
CrewAI, build a Python API or function that exposes your tool's capabilities and register it within the CrewAI platform. Use CrewAI's SDK or REST API to integrate your tool, enabling CrewAI to call it dynamically during workflows.PREREQUISITES
Python 3.8+CrewAI account with API accesspip install requestsAPI key or credentials for CrewAI
Setup
Install the requests library to interact with CrewAI's API and set your environment variables for authentication.
- Run
pip install requeststo install dependencies. - Export your CrewAI API key:
export CREWAI_API_KEY='your_api_key'on Unix or set environment variable on Windows.
pip install requests Step by step
This example shows how to create a simple custom tool that CrewAI can call via its API. The tool is a Python Flask app exposing an endpoint CrewAI invokes.
import os
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
CREWAI_API_KEY = os.environ.get('CREWAI_API_KEY')
CREWAI_API_URL = 'https://api.crewai.com/v1/tools/register'
# Define your custom tool logic
@app.route('/custom-tool', methods=['POST'])
def custom_tool():
data = request.json
# Example: echo input text reversed
input_text = data.get('text', '')
result = input_text[::-1]
return jsonify({'result': result})
# Register the tool with CrewAI
def register_tool():
headers = {'Authorization': f'Bearer {CREWAI_API_KEY}', 'Content-Type': 'application/json'}
payload = {
'name': 'ReverseTextTool',
'description': 'Reverses input text',
'endpoint': 'https://your-server.com/custom-tool',
'method': 'POST',
'input_schema': {'type': 'object', 'properties': {'text': {'type': 'string'}}},
'output_schema': {'type': 'object', 'properties': {'result': {'type': 'string'}}}
}
response = requests.post(CREWAI_API_URL, json=payload, headers=headers)
if response.status_code == 200:
print('Tool registered successfully')
else:
print('Failed to register tool:', response.text)
if __name__ == '__main__':
register_tool()
app.run(host='0.0.0.0', port=5000) output
Tool registered successfully * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Common variations
You can implement your custom tool asynchronously using FastAPI or add authentication to your endpoint. Also, CrewAI supports different HTTP methods and payload formats.
from fastapi import FastAPI, Request
import uvicorn
app = FastAPI()
@app.post('/custom-tool')
async def custom_tool(request: Request):
data = await request.json()
input_text = data.get('text', '')
return {'result': input_text.upper()}
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000) output
INFO: Started server process [12345] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Troubleshooting
- If tool registration fails, verify your API key and endpoint URL.
- Ensure your server is publicly accessible for CrewAI to call your tool.
- Check logs for HTTP errors and fix JSON schema mismatches.
Key Takeaways
- Use a web-accessible API endpoint to expose your custom tool for CrewAI integration.
- Register your tool with CrewAI via their API including endpoint and schema details.
- Test your tool locally and ensure it handles expected input/output JSON formats.
- Secure your tool endpoint and validate API keys to prevent unauthorized access.