For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Integrate MongoDB with CrewAI

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.

To complete a tutorial using CrewAI and MongoDB, see Build an Agentic RAG Application with CrewAI and MongoDB.

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]'

Note

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).

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 tool
print(tool.run(query="<test-query>"))
# To use the tool in an agent
rag_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],
)

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 os
from 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 folder
texts = []
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 store
tool.add_texts(texts)
# Method to create the vector search index
tool.create_vector_search_index(dimensions=<number-of-dimensions>)
# Method to test the tool by running a vector search query
tool.run(query="<search-query>")