feat: enhance CrewBase MCP tools support to allow selecting multiple tools per agent (#3065)
Some checks failed
Notify Downstream / notify-downstream (push) Has been cancelled

* feat: enhance CrewBase MCP tools support to allow selecting multiple tools per agent

* docs: clarify how to access MCP tools

* build: upgrade crewai-tools
This commit is contained in:
Lucas Gomide
2025-06-25 15:59:55 -03:00
committed by GitHub
parent a50fae3a4b
commit 4d1aabf620
7 changed files with 284 additions and 15 deletions

View File

@@ -87,6 +87,13 @@ This general pattern shows how to integrate tools. For specific examples tailore
## Filtering Tools
There are two ways to filter tools:
1. Accessing a specific tool using dictionary-style indexing.
2. Pass a list of tool names to the `MCPServerAdapter` constructor.
### Accessing a specific tool using dictionary-style indexing.
```python
with MCPServerAdapter(server_params) as mcp_tools:
print(f"Available tools: {[tool.name for tool in mcp_tools]}")
@@ -95,7 +102,24 @@ with MCPServerAdapter(server_params) as mcp_tools:
role="MCP Tool User",
goal="Utilize tools from an MCP server.",
backstory="I can connect to MCP servers and use their tools.",
tools=mcp_tools["tool_name"], # Pass the loaded tools to your agent
tools=[mcp_tools["tool_name"]], # Pass the loaded tools to your agent
reasoning=True,
verbose=True
)
# ... rest of your crew setup ...
```
### Pass a list of tool names to the `MCPServerAdapter` constructor.
```python
with MCPServerAdapter(server_params, "tool_name") as mcp_tools:
print(f"Available tools: {[tool.name for tool in mcp_tools]}")
my_agent = Agent(
role="MCP Tool User",
goal="Utilize tools from an MCP server.",
backstory="I can connect to MCP servers and use their tools.",
tools=mcp_tools, # Pass the loaded tools to your agent
reasoning=True,
verbose=True
)
@@ -132,10 +156,22 @@ class CrewWithMCP:
@agent
def your_agent(self):
return Agent(config=self.agents_config["your_agent"], tools=self.get_mcp_tools()) # you can filter which tool are available also
return Agent(config=self.agents_config["your_agent"], tools=self.get_mcp_tools()) # get all available tools
# ... rest of your crew setup ...
```
You can filter which tools are available to your agent by passing a list of tool names to the `get_mcp_tools` method.
```python
@agent
def another_agent(self):
return Agent(
config=self.agents_config["your_agent"],
tools=self.get_mcp_tools("tool_1", "tool_2") # get specific tools
)
```
## Explore MCP Integrations
<CardGroup cols={2}>

View File

@@ -47,7 +47,7 @@ Documentation = "https://docs.crewai.com"
Repository = "https://github.com/crewAIInc/crewAI"
[project.optional-dependencies]
tools = ["crewai-tools~=0.47.1"]
tools = ["crewai-tools~=0.48.0"]
embeddings = [
"tiktoken~=0.8.0"
]

View File

@@ -86,18 +86,17 @@ def CrewBase(cls: T) -> T:
import types
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:
return []
from crewai_tools import MCPServerAdapter
adapter = getattr(self, '_mcp_server_adapter', None)
if adapter and isinstance(adapter, MCPServerAdapter):
return adapter.tools
if not adapter:
self._mcp_server_adapter = MCPServerAdapter(self.mcp_server_params)
return self._mcp_server_adapter.tools
return self._mcp_server_adapter.tools.filter_by_names(tool_names or None)
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

View File

@@ -93,6 +93,9 @@ class InternalCrewWithMCP(InternalCrew):
def reporting_analyst(self):
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():
crew = SimpleCrew()
@@ -251,11 +254,20 @@ def simple_tool():
"""Return 'Hi!'"""
return "Hi!"
@tool
def another_simple_tool():
"""Return 'Hi!'"""
return "Hi!"
def test_internal_crew_with_mcp():
mock = Mock()
mock.tools = [simple_tool]
from crewai_tools import MCPServerAdapter
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:
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})

8
uv.lock generated
View File

@@ -800,7 +800,7 @@ requires-dist = [
{ name = "blinker", specifier = ">=1.9.0" },
{ name = "chromadb", specifier = ">=0.5.23" },
{ name = "click", specifier = ">=8.1.7" },
{ name = "crewai-tools", marker = "extra == 'tools'", specifier = "~=0.47.1" },
{ name = "crewai-tools", marker = "extra == 'tools'", specifier = "~=0.48.0" },
{ name = "docling", marker = "extra == 'docling'", specifier = ">=2.12.0" },
{ name = "instructor", specifier = ">=1.3.3" },
{ name = "json-repair", specifier = ">=0.25.2" },
@@ -847,7 +847,7 @@ dev = [
[[package]]
name = "crewai-tools"
version = "0.47.1"
version = "0.48.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "chromadb" },
@@ -863,9 +863,9 @@ dependencies = [
{ name = "requests" },
{ name = "tiktoken" },
]
sdist = { url = "https://files.pythonhosted.org/packages/0e/cd/fc5a96be8c108febcc2c767714e3ec9b70cca9be8e6b29bba7c1874fb6d2/crewai_tools-0.47.1.tar.gz", hash = "sha256:4de5ebf320aeae317ffabe2e4704b98b5d791f663196871fb5ad2e7bbea14a82", size = 921418 }
sdist = { url = "https://files.pythonhosted.org/packages/34/57/5bcc70c83a5653eb05db0fbe0d8af4861365de08bab97e0197d0e55e5f76/crewai_tools-0.48.0.tar.gz", hash = "sha256:5ef1bd821bb6d9d55e82ed17fb0b60ce8504c22739497c284cf264ebba876f45", size = 939346 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/e3/2c/05d9fa584d9d814c0c8c4c3793df572222417695fe3d716f14bc274376d6/crewai_tools-0.47.1-py3-none-any.whl", hash = "sha256:4dc9bb0a08e3afa33c6b9efb163e47181801a7906be7241977426e6d3dec0a05", size = 606305 },
{ url = "https://files.pythonhosted.org/packages/c1/a1/94a736ac6f43e23e32adf7524bc6d48631676303536c20c76ecb0051c26d/crewai_tools-0.48.0-py3-none-any.whl", hash = "sha256:c48097b19a86466803577ef9e0a1cb0b2540da332f22cce82f97d6a381dac73e", size = 620546 },
]
[[package]]