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 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).
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 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], )
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 Queries.
from crewai_tools import MongoDBVectorSearchConfig, MongoDBVectorSearchTool # Custom query configuration query_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 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], )
Parameter | Necessity | Description | |
---|---|---|---|
| Required | The connection string for your MongoDB instance. To learn more about finding your connection string, see Connect to a Cluster via Drivers. NoteFor a local deployment, your connection string should use the following format:
To learn more about connection strings, see Connection Strings. | |
| Required | The name of the MongoDB database. | |
| Required | The name of the MongoDB collection. | |
| Optional | An instance of | |
| Optional | The OpenAI embedding model used to generate vector embeddings. Defaults to | |
| Optional | The name of the MongoDB Vector Search index. Defaults to | |
| Optional | The document field containing the text content. Defaults to | |
| Optional | The document field where the vector embedding is stored. Defaults to | |
| Optional | The number of dimensions for the vector embedding. Defaults to |
Parameter | Necessity | Description |
---|---|---|
| Optional | The maximum number of documents to return. Defaults to |
| Optional | A MongoDB |
| Optional | A list of MongoDB aggregation stages to apply after the vector search. |
| Optional | A multiplier for |
| bool | If True, the vector embedding of each result is included in the output. Defaults to |
Tip
Also see the CrewAI MongoDB Vector Search Tool Docs
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 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>")
Parameter | Necessity | Description |
---|---|---|
| Required | The number of dimensions for the embedding vector. |
| Optional | The similarity metric. Either |
| Optional | The time in seconds to wait for the index to become ready. Defaults to |
To learn more about vector search indexes, see How to Index Fields for Vector Search.
Parameter | Necessity | Description |
---|---|---|
| Required | Array of iterable text documents to add. |
| Optional | A list of metadata documents, one for each text document. |
| Optional | A list of unique IDs for each document. |
| Optional | The number of documents to process and insert in a single batch. Defaults to |