Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f73f539
INTPYTHON-580 Design and Implement MongoDBVectorSearchTool
blink1073 May 21, 2025
736275e
add implementation
blink1073 May 21, 2025
d824c0c
wip
blink1073 May 21, 2025
65c0a7b
wip
blink1073 May 21, 2025
bcb8130
finish tests
blink1073 May 21, 2025
b408c5c
add todo
blink1073 May 21, 2025
e69ca9c
refactor to wrap langchain-mongodb
blink1073 May 23, 2025
7f93b9a
cleanup
blink1073 May 23, 2025
5c15fbe
address review
blink1073 Jun 3, 2025
15adea6
Merge branch 'main' of github.com:crewAIInc/crewAI-tools into INTPYTH…
blink1073 Jun 3, 2025
1b74a65
Fix usage of EnvVar class
blink1073 Jun 3, 2025
8655a13
inline code
blink1073 Jun 9, 2025
7a522d2
merge from master
blink1073 Jun 9, 2025
69749e6
lint
blink1073 Jun 9, 2025
c8168bb
lint
blink1073 Jun 9, 2025
6c3820a
fix usage of SearchIndexModel
blink1073 Jun 9, 2025
336451c
Refactor: Update EnvVar import path and remove unused tests.utils module
lorenzejay Jun 10, 2025
10bf7a7
update the crewai dep and the lockfile
blink1073 Jun 11, 2025
7fce5f5
Merge branch 'main' of github.com:crewAIInc/crewAI-tools into INTPYTH…
lorenzejay Jul 7, 2025
e810759
chore: update package versions and dependencies in uv.lock
lorenzejay Jul 7, 2025
b6bdfa8
refactor: improve MongoDB vector search tool with enhanced error hand…
lorenzejay Jul 8, 2025
5e3b4b1
address review
blink1073 Jul 8, 2025
8e47c09
update tests
blink1073 Jul 8, 2025
0a11aef
Merge branch 'main' of github.com:crewAIInc/crewAI-tools into INTPYTH…
blink1073 Jul 8, 2025
37d344b
debug
blink1073 Jul 8, 2025
1b73bb5
fix test
blink1073 Jul 8, 2025
12d0d50
fix test
blink1073 Jul 8, 2025
94bd04c
fix test
blink1073 Jul 8, 2025
86f6eb5
support azure openai
blink1073 Jul 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CrewAI provides an extensive collection of powerful tools ready to enhance your
- **File Management**: `FileReadTool`, `FileWriteTool`
- **Web Scraping**: `ScrapeWebsiteTool`, `SeleniumScrapingTool`
- **Database Integrations**: `PGSearchTool`, `MySQLSearchTool`
- **Vector Database Integrations**: `MongoDBVectorSearchTool`, `QdrantVectorSearchTool`, `WeaviateVectorSearchTool`
- **API Integrations**: `SerperApiTool`, `EXASearchTool`
- **AI-powered Tools**: `DallETool`, `VisionTool`, `StagehandTool`

Expand Down Expand Up @@ -226,4 +227,3 @@ Join our rapidly growing community and receive real-time support:
- [Open an Issue](https://github.com/crewAIInc/crewAI/issues)

Build smarter, faster, and more powerful AI solutions—powered by CrewAI Tools.

6 changes: 4 additions & 2 deletions crewai_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .adapters.enterprise_adapter import EnterpriseActionTool
from .adapters.mcp_adapter import MCPServerAdapter
from .adapters.zapier_adapter import ZapierActionTool
from .aws import (
BedrockInvokeAgentTool,
BedrockKBRetrieverTool,
Expand All @@ -23,9 +24,9 @@
DirectorySearchTool,
DOCXSearchTool,
EXASearchTool,
FileCompressorTool,
FileReadTool,
FileWriterTool,
FileCompressorTool,
FirecrawlCrawlWebsiteTool,
FirecrawlScrapeWebsiteTool,
FirecrawlSearchTool,
Expand All @@ -35,6 +36,8 @@
LinkupSearchTool,
LlamaIndexTool,
MDXSearchTool,
MongoDBVectorSearchConfig,
MongoDBVectorSearchTool,
MultiOnTool,
MySQLSearchTool,
NL2SQLTool,
Expand Down Expand Up @@ -76,4 +79,3 @@
YoutubeVideoSearchTool,
ZapierActionTools,
)
from .adapters.zapier_adapter import ZapierActionTool
7 changes: 6 additions & 1 deletion crewai_tools/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from .exa_tools.exa_search_tool import EXASearchTool
from .file_read_tool.file_read_tool import FileReadTool
from .file_writer_tool.file_writer_tool import FileWriterTool
from .files_compressor_tool.files_compressor_tool import FileCompressorTool
from .firecrawl_crawl_website_tool.firecrawl_crawl_website_tool import (
FirecrawlCrawlWebsiteTool,
)
from .files_compressor_tool.files_compressor_tool import FileCompressorTool
from .firecrawl_scrape_website_tool.firecrawl_scrape_website_tool import (
FirecrawlScrapeWebsiteTool,
)
Expand All @@ -30,6 +30,11 @@
from .linkup.linkup_search_tool import LinkupSearchTool
from .llamaindex_tool.llamaindex_tool import LlamaIndexTool
from .mdx_search_tool.mdx_search_tool import MDXSearchTool
from .mongodb_vector_search_tool import (
MongoDBToolSchema,
MongoDBVectorSearchConfig,
MongoDBVectorSearchTool,
)
from .multion_tool.multion_tool import MultiOnTool
from .mysql_search_tool.mysql_search_tool import MySQLSearchTool
from .nl2sql.nl2sql_tool import NL2SQLTool
Expand Down
87 changes: 87 additions & 0 deletions crewai_tools/tools/mongodb_vector_search_tool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# MongoDBVectorSearchTool

## Description
This tool is specifically crafted for conducting vector searches within docs within a MongoDB database. Use this tool to find semantically similar docs to a given query.

MongoDB can act as a vector database that is used to store and query vector embeddings. You can follow the docs here:
https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-overview/

## Installation
Install the crewai_tools package with MongoDB support by executing the following command in your terminal:

```shell
pip install crewai-tools[mongodb]
```

or

```
uv add crewai-tools --extra mongodb
```

## Example
To utilize the MongoDBVectorSearchTool for different use cases, follow these examples:

```python
from crewai_tools import MongoDBVectorSearchTool

# To enable the tool to search any website the agent comes across or learns about during its operation
tool = MongoDBVectorSearchTool(
database_name="example_database',
collection_name='example_collections',
connection_string="<your_mongodb_connection_string>",
)
```

or

```python
from crewai_tools import MongoDBVectorSearchConfig, MongoDBVectorSearchTool

# Setup custom embedding model and customize the parameters.
query_config = MongoDBVectorSearchConfig(limit=10, oversampling_factor=2)
tool = MongoDBVectorSearchTool(
database_name="example_database',
collection_name='example_collections',
connection_string="<your_mongodb_connection_string>",
query_config=query_config,
index_name="my_vector_index",
generative_model="gpt-4o-mini"
)

# Adding the tool to 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="...",
llm="gpt-4o-mini",
tools=[tool],
)
```

Preloading the MongoDB database with documents:

```python
from crewai_tools import MongoDBVectorSearchTool

# Generate the documents and add them to the MongoDB database
test_docs = client.collections.get("example_collections")

# Create the tool.
tool = MongoDBVectorSearchTool(
database_name="example_database',
collection_name='example_collections',
connection_string="<your_mongodb_connection_string>",
)

# Add the text from a set of CrewAI knowledge documents.
texts = []
for d in os.listdir("knowledge"):
with open(os.path.join("knowledge", d), "r") as f:
texts.append(f.read())
tool.add_texts(text)

# Create the vector search index (if it wasn't already created in Atlas).
tool.create_vector_search_index(dimensions=3072)
```
11 changes: 11 additions & 0 deletions crewai_tools/tools/mongodb_vector_search_tool/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .vector_search import (
MongoDBToolSchema,
MongoDBVectorSearchConfig,
MongoDBVectorSearchTool,
)

__all__ = [
"MongoDBVectorSearchConfig",
"MongoDBVectorSearchTool",
"MongoDBToolSchema",
]
120 changes: 120 additions & 0 deletions crewai_tools/tools/mongodb_vector_search_tool/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from __future__ import annotations

from time import monotonic, sleep
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional

if TYPE_CHECKING:
from pymongo.collection import Collection


def _vector_search_index_definition(
dimensions: int,
path: str,
similarity: str,
filters: Optional[List[str]] = None,
**kwargs: Any,
) -> Dict[str, Any]:
# https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type/
fields = [
{
"numDimensions": dimensions,
"path": path,
"similarity": similarity,
"type": "vector",
},
]
if filters:
for field in filters:
fields.append({"type": "filter", "path": field})
definition = {"fields": fields}
definition.update(kwargs)
return definition


def create_vector_search_index(
collection: Collection,
index_name: str,
dimensions: int,
path: str,
similarity: str,
filters: Optional[List[str]] = None,
*,
wait_until_complete: Optional[float] = None,
**kwargs: Any,
) -> None:
"""Experimental Utility function to create a vector search index

Args:
collection (Collection): MongoDB Collection
index_name (str): Name of Index
dimensions (int): Number of dimensions in embedding
path (str): field with vector embedding
similarity (str): The similarity score used for the index
filters (List[str]): Fields/paths to index to allow filtering in $vectorSearch
wait_until_complete (Optional[float]): If provided, number of seconds to wait
until search index is ready.
kwargs: Keyword arguments supplying any additional options to SearchIndexModel.
"""
from pymongo.operations import SearchIndexModel

if collection.name not in collection.database.list_collection_names():
collection.database.create_collection(collection.name)

result = collection.create_search_index(
SearchIndexModel(
definition=_vector_search_index_definition(
dimensions=dimensions,
path=path,
similarity=similarity,
filters=filters,
**kwargs,
),
name=index_name,
type="vectorSearch",
)
)

if wait_until_complete:
_wait_for_predicate(
predicate=lambda: _is_index_ready(collection, index_name),
err=f"{index_name=} did not complete in {wait_until_complete}!",
timeout=wait_until_complete,
)


def _is_index_ready(collection: Collection, index_name: str) -> bool:
"""Check for the index name in the list of available search indexes to see if the
specified index is of status READY

Args:
collection (Collection): MongoDB Collection to for the search indexes
index_name (str): Vector Search Index name

Returns:
bool : True if the index is present and READY false otherwise
"""
for index in collection.list_search_indexes(index_name):
if index["status"] == "READY":
return True
return False


def _wait_for_predicate(
predicate: Callable, err: str, timeout: float = 120, interval: float = 0.5
) -> None:
"""Generic to block until the predicate returns true

Args:
predicate (Callable[, bool]): A function that returns a boolean value
err (str): Error message to raise if nothing occurs
timeout (float, optional): Wait time for predicate. Defaults to TIMEOUT.
interval (float, optional): Interval to check predicate. Defaults to DELAY.

Raises:
TimeoutError: _description_
"""
start = monotonic()
while not predicate():
if monotonic() - start > timeout:
raise TimeoutError(err)
sleep(interval)
Loading
Loading