You can integrate MongoDB with CrewAI to build autonomous AI agents and multi-agent applications with specialized roles, tools, and tasks. Specifically, you can leverage the MongoDB Vector Search Tool for CrewAI to enable the AI agents in your crews to retrieve relevant information from your data to help them complete tasks.
Get Started
To complete a tutorial using CrewAI and MongoDB, see Build an Agentic RAG Application with CrewAI and MongoDB.
Installation
To install the MongoDB Vector Search Tool for CrewAI, run one of the following commands depending on your Python package manager:
pip install 'crewai-tools[mongodb]'uv add crewai-tools --extra mongodbNote
Python version compatibility might vary from CrewAI's official documentation. At the time of writing, the crewai-tools package depends on embedchain, which requires a Python version between 3.9 and 3.13.2 (inclusive).
Usage
To use the MongoDB Vector Search Tool, initialize it and then pass it to an agent.
To initialize the tool, you must specify the following:
from crewai_tools import MongoDBVectorSearchTool
tool = MongoDBVectorSearchTool( connection_string="<connection-string>", database_name="<database-name>", collection_name="<collection-name>", # Other optional parameters...)
# To test the toolprint(tool.run(query="<test-query>"))
# To use the tool in an agentrag_agent = Agent( name="rag_agent", role="You are a helpful assistant that can answer questions with the help of the MongoDBVectorSearchTool.", goal="...", backstory="...", tools=[tool],)Optionally, you can customize the vector search query for the tool by specifying an instance of MongoDBVectorSearchConfig to the tool's constructor.
To learn more about vector search queries, see Run Vector Search ANN and ENN Queries.
from crewai_tools import MongoDBVectorSearchConfig, MongoDBVectorSearchTool
# Custom query configurationquery_config = MongoDBVectorSearchConfig( limit = 10, oversampling_factor = 2, )
tool = MongoDBVectorSearchTool( database_name="example_database", collection_name="example_collection", connection_string="<connection_string>", query_config=query_config, # Other optional parameters...)
# To test the toolprint(tool.run(query="<test-query>"))
# To use the tool in an agentrag_agent = Agent( name="rag_agent", role="You are a helpful assistant that can answer questions with the help of the MongoDBVectorSearchTool.", goal="...", backstory="...", tools=[tool],)Methods
The MongoDBVectorSearchTool class provides the following methods:
add_texts(): Adds text documents to the specified MongoDB collection.create_vector_search_index(): Creates a vector search index on the collection.run(): Runs a vector search query on your data.
import osfrom crewai_tools import MongoDBVectorSearchTool
tool = MongoDBVectorSearchTool( connection_string="<connection-string>", database_name="<database-name>", collection_name="<collection-name>")
# Example of loading text content from a local foldertexts = []for fname in os.listdir("knowledge"): path = os.path.join("knowledge", fname) if os.path.isfile(path): with open(path, "r", encoding="utf-8") as f: texts.append(f.read())
# Method to add documents to the vector storetool.add_texts(texts)
# Method to create the vector search indextool.create_vector_search_index(dimensions=<number-of-dimensions>)
# Method to test the tool by running a vector search querytool.run(query="<search-query>")