mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-23 23:28:15 +00:00
feat: add official way to use MCP Tools within a CrewBase
Added a standard way to define and use MCP server tools inside a CrewBase class. This was necessary because existing methods don't work in this context due to lifecycle mismatches. MCP tools run asynchronously and start an event loop, which causes the instance state to become desynchronized from the crew. This change ensures proper integration by aligning the MCP server lifecycle with the CrewBase instance.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from typing import List
|
||||
|
||||
from unittest.mock import Mock, patch
|
||||
import pytest
|
||||
|
||||
from crewai.agent import Agent
|
||||
@@ -16,7 +16,7 @@ from crewai.project import (
|
||||
task,
|
||||
)
|
||||
from crewai.task import Task
|
||||
|
||||
from crewai.tools import tool
|
||||
|
||||
class SimpleCrew:
|
||||
@agent
|
||||
@@ -85,6 +85,14 @@ class InternalCrew:
|
||||
def crew(self):
|
||||
return Crew(agents=self.agents, tasks=self.tasks, verbose=True)
|
||||
|
||||
@CrewBase
|
||||
class InternalCrewWithMCP(InternalCrew):
|
||||
mcp_server_params = {"host": "localhost", "port": 8000}
|
||||
|
||||
@agent
|
||||
def reporting_analyst(self):
|
||||
return Agent(config=self.agents_config["reporting_analyst"], tools=self.get_mcp_tools()) # type: ignore[index]
|
||||
|
||||
|
||||
def test_agent_memoization():
|
||||
crew = SimpleCrew()
|
||||
@@ -237,3 +245,17 @@ def test_multiple_before_after_kickoff():
|
||||
def test_crew_name():
|
||||
crew = InternalCrew()
|
||||
assert crew._crew_name == "InternalCrew"
|
||||
|
||||
@tool
|
||||
def simple_tool():
|
||||
"""Return 'Hi!'"""
|
||||
return "Hi!"
|
||||
|
||||
def test_internal_crew_with_mcp():
|
||||
mock = Mock()
|
||||
mock.tools = [simple_tool]
|
||||
with patch("crewai_tools.MCPServerAdapter", return_value=mock) as adapter_mock:
|
||||
crew = InternalCrewWithMCP()
|
||||
assert crew.reporting_analyst().tools == [simple_tool]
|
||||
|
||||
adapter_mock.assert_called_once_with({"host": "localhost", "port": 8000})
|
||||
Reference in New Issue
Block a user