Skip to main content
Put your APIs behind controlled tools so the agent can fetch data or perform actions while CometChat streams results live.

What you’ll build

  • A CrewAI agent configured with one or more backend tools.
  • Tool functions that validate inputs, call external APIs, and raise exceptions on failure.
  • A /kickoff endpoint that surfaces tool call events to CometChat.

Prerequisites

  • CrewAI project from crew-ai.mdx
  • API keys for any services you call (store them in .env)

Steps

1

Model your tools

Keep each tool focused (single responsibility) and add clear docstrings so the LLM knows when to call them.
2

Validate inputs

Check for required fields and raise exceptions for bad data—don’t return error strings.
3

Register in CrewAI

Add tools to the agent in crew.py and set process=Process.sequential or another flow as needed.
4

Stream via FastAPI

Reuse the NDJSON stream from crew-ai.mdx so CometChat can render tool progress.

Sample tool: fetch deals from a CRM

src/crew_demo/tools/get_deals.py
import os, httpx, json
from crewai.tools import tool


@tool("get_recent_deals")
def get_recent_deals(limit: int = 5) -> str:
    """Fetch recent deals from the CRM."""
    api_key = os.getenv("CRM_API_KEY")
    base_url = os.getenv("CRM_BASE_URL", "https://api.example-crm.com")
    if not api_key:
        raise Exception("CRM_API_KEY not set")

    resp = httpx.get(f"{base_url}/deals", params={"limit": limit}, headers={"Authorization": f"Bearer {api_key}"}, timeout=10.0)
    resp.raise_for_status()
    deals = resp.json().get("deals", [])
    return json.dumps({"deals": deals[:limit]})
Register in crew.py:
from crew_demo.tools.get_deals import get_recent_deals

@agent
def backend(self) -> Agent:
    return Agent(
        config=self.agents_config["backend"],  # type: ignore[index]
        tools=[get_recent_deals],
        verbose=False,
        memory=False,
    )

Agent/task configuration

src/crew_demo/config/agents.yaml
backend:
  role: Backend Integrations Agent
  goal: Call approved APIs and summarize results
  backstory: >
    Always validate inputs, call the correct tool, and summarize results clearly.
    Never expose raw errors—raise exceptions and let the server handle them.
src/crew_demo/config/tasks.yaml
backend_task:
  description: >
    Use the appropriate tool to answer: {user_message}
  expected_output: >
    A short summary plus any key fields (totals, status, links).
  agent: backend

Connect to CometChat

  • Provider: CrewAI
  • Agent ID: backend
  • Deployment URL: your public /kickoff
  • Optional headers: { "Authorization": "Bearer <token>" }
Ensure your FastAPI service returns NDJSON with tool events so users can see when an API call is running.