Knowledge-Grounded AI Agent for E-Commerce Analytics
This project demonstrates the AI Context Stack pattern from Google OKF + MCP: Explained The New "AI Context Stack" — combining two layers to build AI agents that produce accurate, real-time answers:
| Layer | Technology | Role |
|---|---|---|
| Knowledge Layer | OKF (Open Knowledge Format) | Tells the agent what things mean — metric definitions, table schemas, SQL formulas |
| Access Layer | MCP-style Tools | Lets the agent query live data — read-only SQL against the database |
| LLM | Google Gemini | Reasons over both layers to answer questions |
- Without OKF: The agent guesses what "weekly active users" means and writes incorrect SQL
- Without MCP: The agent knows the definition but can't fetch live data
- With both: The agent reads the exact metric definition from OKF, then queries the database using the correct formula
ai-context-stack-agent/
├── agent.py # Main agent (Gemini + OKF + tool calling)
├── mcp_server.py # FastMCP server with database query tools
├── seed_db.py # Seeds SQLite with sample e-commerce data
├── requirements.txt
└── knowledge/ # OKF Knowledge Bundle
├── index.md
├── datasets/
│ └── ecommerce_db.md
├── tables/
│ ├── orders.md
│ └── customers.md
└── metrics/
├── weekly_active_users.md
└── monthly_revenue.md
# 1. Install dependencies
pip install -r requirements.txt
# 2. Seed the database
python seed_db.py
# 3. Set your Gemini API key
export GOOGLE_API_KEY=your-api-key # Get one at https://aistudio.google.com/apikey
# 4. Run the agent
python agent.pyYou: What are our weekly active users?
Agent: There are 15 weekly active users right now.
You: Show me the monthly revenue trend
Agent: Here is the monthly revenue trend:
- 2026-04: $5,499.01 (36 orders)
- 2026-05: $9,332.38 (65 orders)
- 2026-06: $6,596.61 (52 orders)
- 2026-07: $1,901.67 (16 orders)
- OKF Knowledge Bundle is loaded at startup — Markdown files with YAML frontmatter define every table schema, metric formula, and business concept
- When you ask a question, the agent consults the relevant OKF concept document to understand the exact definition
- Gemini's function calling invokes the database tools with the correct SQL from the OKF definition
- The agent presents results grounded in the OKF definition — no hallucinated metric formulas
Each concept is a Markdown file with YAML frontmatter following the OKF v0.1 spec:
---
type: Metric
title: Weekly Active Users (WAU)
description: Count of unique customers who placed at least one completed order in the last 7 days.
tags: [metric, engagement, users]
---
# Definition
Weekly Active Users (WAU) is the count of unique customers...
# SQL
SELECT COUNT(DISTINCT customer_id) AS weekly_active_users
FROM orders
WHERE status = 'completed'
AND order_date >= date('now', '-7 days');- Google Gemini 2.5 Flash — LLM with native function calling
- google-genai — Google's Python SDK for Gemini
- FastMCP — MCP server framework (standalone server included)
- SQLite — Zero-setup database
- OKF v0.1 — Open Knowledge Format by Google Cloud
google-okf, mcp, model-context-protocol, open-knowledge-format, ai-context-stack, gemini-ai, google-gemini, ai-agent, llm-agent, function-calling, tool-use, ecommerce-analytics, knowledge-grounded-ai, agentic-ai, python, sqlite, fastmcp, okf-knowledge-bundle, data-analytics, ai-orchestration