Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Workflow:CrewAIInc CrewAI Custom Tool Integration

From Leeroopedia
Knowledge Sources
Domains Multi_Agent_Systems, Tool_Development, API_Integration
Last Updated 2026-02-11 18:00 GMT

Overview

End-to-end process for creating custom tools, integrating third-party tool libraries, and configuring MCP server connections to extend CrewAI agent capabilities.

Description

This workflow covers how to extend CrewAI agents with custom functionality through the tool system. CrewAI provides three approaches: the @tool decorator for wrapping plain Python functions, the BaseTool class for full object-oriented tool definitions, and StructuredTool for function-based tools with explicit schema validation. Beyond custom tools, the workflow covers integrating the 80+ built-in tools from the crewai-tools package (search engines, web scrapers, database connectors, RAG tools, etc.), connecting to MCP (Model Context Protocol) servers for access to community-built tools, and configuring tool-level features such as caching, usage limits, and result handling.

Usage

Execute this workflow when agents need to interact with external systems, APIs, databases, or perform computations beyond what the LLM can do natively. Typical triggers include: agents need to search the web, read files, query databases, call external APIs, execute code, or perform any action that requires structured input/output handling.

Execution Steps

Step 1: Tool Design

Determine what external capability the agent needs and design the tool interface. Define the tool name, description (used by the LLM to decide when to call the tool), input parameters with types and descriptions, and the expected return format. The description is critical as it guides the LLM's tool selection during autonomous execution.

Key considerations:

  • Tool descriptions should clearly state what the tool does and when to use it
  • Input parameters should have descriptive names and type annotations
  • Return values should be strings that the LLM can interpret
  • Keep tools focused on a single action for reliable LLM usage

Step 2: Tool Implementation

Implement the tool using one of three patterns. The @tool decorator wraps a plain Python function with automatic schema inference from type hints. The BaseTool subclass provides full control with separate _run() and _arun() methods for sync and async execution. The StructuredTool class wraps functions with explicit Pydantic args_schema for complex input validation.

Key considerations:

  • @tool is the simplest approach for straightforward functions
  • BaseTool is preferred for tools with complex state or initialization
  • StructuredTool bridges function-based tools with explicit schemas
  • All tools must return strings (the LLM processes the text result)

Step 3: Built-in Tool Selection

For common capabilities, select from the 80+ tools in the crewai-tools package. Categories include search (SerperDev, Tavily, EXA, Brave), web scraping (Firecrawl, Selenium, Stagehand), RAG search (PDF, CSV, DOCX, TXT, JSON, XML search), databases (MySQL, PostgreSQL, Snowflake, MongoDB), file operations (read, write, compress), and AI services (DALL-E, vision). Install the tools package and import the needed tool classes.

Key considerations:

  • Install crewai[tools] for the full tool library
  • Most tools require API keys set as environment variables
  • RAG-based tools use the embedding system for semantic search
  • Tool classes are instantiated with configuration before agent assignment

Step 4: MCP Server Connection

Connect to MCP (Model Context Protocol) servers for access to community-built tools. Configure MCPServerConfig with the server transport type (stdio or SSE) and connection parameters. MCP tools are dynamically discovered from the server and converted to CrewAI BaseTool instances. This enables access to thousands of community tools without manual integration.

Key considerations:

  • MCP supports stdio (local process) and SSE (HTTP) transports
  • Tools are discovered at runtime from the MCP server
  • MCP tools follow the same interface as native CrewAI tools
  • Multiple MCP servers can be connected to a single agent

Step 5: Tool Assignment

Assign tools to agents via the tools parameter or to specific tasks via the task tools parameter. Agent-level tools are available for all tasks that agent executes. Task-level tools override or supplement agent tools for that specific task. Configure cache=True on tools to avoid redundant executions, and set usage_limit to restrict how many times a tool can be called.

Key considerations:

  • Agent tools apply to all tasks the agent executes
  • Task tools apply only to that specific task execution
  • Caching avoids repeated calls with identical inputs
  • result_as_answer=True makes the tool output the direct task answer

Step 6: Tool Execution and Monitoring

During crew execution, agents autonomously decide when and how to use tools based on the task description and tool descriptions. The tool system handles input parsing, validation against the schema, execution, error handling, and result formatting. Tool usage events are emitted through the event bus for monitoring. Hook decorators (@before_tool_call, @after_tool_call) enable custom logic around tool invocations.

Key considerations:

  • The LLM decides tool usage based on the tool description
  • Input validation catches malformed tool calls from the LLM
  • Tool errors are retried up to the agent's max_iter limit
  • Hook decorators enable logging, auditing, and approval workflows

Execution Diagram

GitHub URL

Workflow Repository