mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-28 09:38:17 +00:00
feat: enhance CrewBase MCP tools support to allow selecting multiple tools per agent
This commit is contained in:
@@ -86,18 +86,17 @@ def CrewBase(cls: T) -> T:
|
|||||||
import types
|
import types
|
||||||
return types.MethodType(_close_mcp_server, self)
|
return types.MethodType(_close_mcp_server, self)
|
||||||
|
|
||||||
def get_mcp_tools(self) -> List[BaseTool]:
|
def get_mcp_tools(self, *tool_names: list[str]) -> List[BaseTool]:
|
||||||
if not self.mcp_server_params:
|
if not self.mcp_server_params:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
from crewai_tools import MCPServerAdapter
|
from crewai_tools import MCPServerAdapter
|
||||||
|
|
||||||
adapter = getattr(self, '_mcp_server_adapter', None)
|
adapter = getattr(self, '_mcp_server_adapter', None)
|
||||||
if adapter and isinstance(adapter, MCPServerAdapter):
|
if not adapter:
|
||||||
return adapter.tools
|
self._mcp_server_adapter = MCPServerAdapter(self.mcp_server_params)
|
||||||
|
|
||||||
self._mcp_server_adapter = MCPServerAdapter(self.mcp_server_params)
|
return self._mcp_server_adapter.tools.filter_by_names(tool_names or None)
|
||||||
return self._mcp_server_adapter.tools
|
|
||||||
|
|
||||||
|
|
||||||
def load_configurations(self):
|
def load_configurations(self):
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -93,6 +93,9 @@ class InternalCrewWithMCP(InternalCrew):
|
|||||||
def reporting_analyst(self):
|
def reporting_analyst(self):
|
||||||
return Agent(config=self.agents_config["reporting_analyst"], tools=self.get_mcp_tools()) # type: ignore[index]
|
return Agent(config=self.agents_config["reporting_analyst"], tools=self.get_mcp_tools()) # type: ignore[index]
|
||||||
|
|
||||||
|
@agent
|
||||||
|
def researcher(self):
|
||||||
|
return Agent(config=self.agents_config["researcher"], tools=self.get_mcp_tools("simple_tool")) # type: ignore[index]
|
||||||
|
|
||||||
def test_agent_memoization():
|
def test_agent_memoization():
|
||||||
crew = SimpleCrew()
|
crew = SimpleCrew()
|
||||||
@@ -251,11 +254,20 @@ def simple_tool():
|
|||||||
"""Return 'Hi!'"""
|
"""Return 'Hi!'"""
|
||||||
return "Hi!"
|
return "Hi!"
|
||||||
|
|
||||||
|
@tool
|
||||||
|
def another_simple_tool():
|
||||||
|
"""Return 'Hi!'"""
|
||||||
|
return "Hi!"
|
||||||
|
|
||||||
|
|
||||||
def test_internal_crew_with_mcp():
|
def test_internal_crew_with_mcp():
|
||||||
mock = Mock()
|
from crewai_tools import MCPServerAdapter
|
||||||
mock.tools = [simple_tool]
|
from crewai_tools.adapters.mcp_adapter import ToolCollection
|
||||||
|
mock = Mock(spec=MCPServerAdapter)
|
||||||
|
mock.tools = ToolCollection([simple_tool, another_simple_tool])
|
||||||
with patch("crewai_tools.MCPServerAdapter", return_value=mock) as adapter_mock:
|
with patch("crewai_tools.MCPServerAdapter", return_value=mock) as adapter_mock:
|
||||||
crew = InternalCrewWithMCP()
|
crew = InternalCrewWithMCP()
|
||||||
assert crew.reporting_analyst().tools == [simple_tool]
|
assert crew.reporting_analyst().tools == [simple_tool, another_simple_tool]
|
||||||
|
assert crew.researcher().tools == [simple_tool]
|
||||||
|
|
||||||
adapter_mock.assert_called_once_with({"host": "localhost", "port": 8000})
|
adapter_mock.assert_called_once_with({"host": "localhost", "port": 8000})
|
||||||
Reference in New Issue
Block a user