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.

Implementation:CrewAIInc CrewAI Tool Assignment Config

From Leeroopedia

Overview

Concrete configuration fields on Agent and Task classes for assigning tools at different scopes provided by the CrewAI framework.

Source Reference

  • Repository: crewAIInc/crewAI
  • Files:
    • lib/crewai/src/crewai/agents/agent_builder/base_agent.py -- Lines L140-142 (Agent.tools)
    • lib/crewai/src/crewai/task.py -- Lines L162-165 (Task.tools)

Fields

Agent-Level Fields

Field Type Default Description
tools None None List of tools persistently available to the agent for all tasks
mcp_servers None None List of MCP server configurations whose discovered tools are added to the agent's tool set

Task-Level Fields

Field Type Default Description
tools None Field(default_factory=list) List of tools available only during this specific task execution; supplements the agent's tools

Import

from crewai import Agent, Task

How It Works

Agent Tool Assignment

When an Agent is created with tools=[...], those tools are stored as part of the agent's configuration. Every time the agent executes a task, its persistent tools are included in the set of tools presented to the LLM.

When mcp_servers=[...] is specified, the framework connects to each MCP server during agent initialization, discovers the available tools, and adds them to the agent's tool set. These MCP-sourced tools are treated identically to locally defined tools.

Task Tool Assignment

When a Task is created with tools=[...], those tools are scoped to that task. They are only made available when an agent is executing that specific task. The task's tools are merged with the executing agent's persistent tools to form the complete tool set for that execution.

Resolution Order

  1. Agent's tools list
  2. Tools discovered from agent's mcp_servers
  3. Task's tools list (merged in during task execution)

Example

Agent with Persistent Tools

from crewai import Agent
from crewai_tools import SerperDevTool, FileReadTool

# These tools are available for every task this agent executes
research_agent = Agent(
    role="Research Analyst",
    goal="Find and analyze information from multiple sources",
    backstory="An experienced analyst with strong research skills.",
    tools=[SerperDevTool(), FileReadTool()],
)

Task with Scoped Tools

from crewai import Task
from crewai_tools import PDFSearchTool, CSVSearchTool

# These tools are ONLY available during this specific task
analyze_report_task = Task(
    description="Analyze the Q4 financial report and extract key metrics",
    expected_output="A summary of key financial metrics from the report",
    agent=research_agent,
    tools=[
        PDFSearchTool(pdf="reports/q4_financials.pdf"),
        CSVSearchTool(csv="data/revenue.csv"),
    ],
)

Agent with MCP Servers and Local Tools

from crewai import Agent
from crewai.mcp import MCPServerStdio
from crewai_tools import SerperDevTool

# Combine local tools with MCP server tools
filesystem_server = MCPServerStdio(
    command="npx",
    args=["-y", "@modelcontextprotocol/server-filesystem", "/data"],
)

agent = Agent(
    role="Data Processor",
    goal="Search the web and process local files",
    backstory="A versatile agent that combines web and local capabilities.",
    tools=[SerperDevTool()],
    mcp_servers=[filesystem_server],
)

Complete Workflow Example

from crewai import Agent, Task, Crew
from crewai_tools import SerperDevTool, FileReadTool, PDFSearchTool

# Agent with persistent tools (available for all tasks)
agent = Agent(
    role="Research Analyst",
    goal="Research and analyze documents",
    backstory="A thorough researcher.",
    tools=[SerperDevTool(), FileReadTool()],  # persistent
)

# Task 1: Only uses the agent's persistent tools
web_research_task = Task(
    description="Research recent developments in AI safety",
    expected_output="A summary of recent AI safety developments",
    agent=agent,
    # No task-level tools; agent uses SerperDevTool and FileReadTool
)

# Task 2: Agent's persistent tools PLUS task-scoped PDFSearchTool
report_analysis_task = Task(
    description="Analyze the attached PDF report for key findings",
    expected_output="Key findings from the report",
    agent=agent,
    tools=[PDFSearchTool(pdf="report.pdf")],  # scoped to this task
)

crew = Crew(agents=[agent], tasks=[web_research_task, report_analysis_task])

Principle Link

Principle:CrewAIInc_CrewAI_Tool_Assignment

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment