This repository contains a small demo agent that uses Google Gemini embeddings and an MCP Toolbox MongoDB toolset to implement an "Online Groceries Agent". The agent demonstrates how to search for products in an inventory using vector search, add products to a cart (MongoDB collection) and get the cart total (aggregating the cart collection).
mongodb-groceries-agent/: Contains the demo agent implementation (agent.py) and package init.tools.yaml: Toolbox toolset definition for the MongoDB tools.
Before running the demo, ensure you have the following installed and available on your machine:
- Python 3.10+ (3.11 recommended)
- pip (or an equivalent Python package manager)
- MCP Toolbox Server (follow the instructions)
- A Google API key with access to the GenAI services (set as
GOOGLE_API_KEYenvironment variable) - A MongoDB deployment (Atlas free M0 cluster is supported — instructions below)
You can create a free (M0) MongoDB Atlas cluster. Follow these steps:
-
Open the Atlas registration page in your browser.
-
Sign up for a free MongoDB account or sign in if you already have one.
-
Click "Create a Cluster" and choose the free tier (M0 / Shared).
-
Choose a cloud provider and region (defaults are fine for the demo).
-
Create a database user: go to "Database Access" and add a user with a password. Save this username/password — you'll need it for connection strings.
-
Allow your IP address: go to "Network Access" and add your current IP or 0.0.0.0/0 for testing (not recommended for production).
-
Click "Connect", choose "Connect your application", and copy the provided connection string.
Use the connection string in
tools.yamlas theurior preferrably, set it through an environment variables. An example connection string looks like:mongodb+srv://<username>:<password>@<your-cluster>.mongodb.net/<dbname>?retryWrites=true&w=majorityNote: Replace
<username>,<password>,<your-cluster>and<dbname>with values from Atlas. For local testing you can also use a local MongoDB server. -
Import the sample grocery products data into a collection named
productsin a database namedgroceries. You can use the providedsample_data/groceries.jsonfile and the MongoDB Atlas UI ormongoshto import it.
mongoimport --uri="<your-connection-string>" --db="grocery_store" --collection="inventory" --file="grocery_store.inventory.json" --jsonArray
Set the following environment variables before running the agent:
GOOGLE_API_KEY— your Google GenAI API key
Example (macOS / zsh):
export GOOGLE_API_KEY="your-google-api-key"- Install dependencies (adjust if this repo is part of a larger project). From the project root:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt # if a requirements file exists; otherwise install needed packages manually- Start the toolbox server:
./toolbox --tools-file "tools.yaml"-
Ensure the Toolbox server is running and accessible at
http://127.0.0.1:5000. -
Run the agent code (this demo file is intended to be imported by an orchestration script that starts the agent). To quickly try a simple run or import the module:
adk webAdjust calls as needed to integrate the agent into a runtime environment that manages the Agent lifecycle.
- Reads
GOOGLE_API_KEYfrom the environment and sets it onos.environfor the Google GenAI client. - Creates a
genai.Client()and a Toolbox client targetinghttp://127.0.0.1:5000. - Provides
generate_embeddings(query)to produce embedding vectors usinggemini-embedding-001. - Provides
find_similar_products(query)which runs a vector search via the toolbox toolfind_similar_documentsagainst an index namedvector_indexand the fieldgemini_embedding. - Builds an
Agentconfigured to use those tools and agrocery-shopping-toolsetloaded from the toolbox.
- If you get authentication errors from GenAI, confirm
GOOGLE_API_KEYis set and valid. - If the Toolbox client cannot connect, confirm the ToolboxSync server is running and reachable at the configured address.
- If MongoDB connection issues appear, verify your Atlas IP allowlist and database user credentials.
|
Stanimira Vlaeva |
Use at your own risk; not a supported MongoDB product