Skip to content

bnamatherdhala7/MailIntel

Repository files navigation

MailIntel

Claude Haiku 4.5 + Sonnet 4.6 · ~$0.022/run · No build tools · Vanilla HTML/JS

SMB owners spend 4+ hours a week switching between dashboards that never tell them what to do next. MailIntel runs 5 AI agents against your Shopify data and returns two ready-to-launch campaigns — across Email, Instagram, and TikTok — in 90 seconds, for $0.022.


The Problem in One Screenshot

Welcome Dashboard

Your store data is all there — revenue, orders, product velocity, low-stock alerts — but no dashboard tells you what to do about it. MailIntel does.


How It Works

  1. You ask one question"Give me a business health check and growth strategy for this week."
  2. Five agents run in sequence — Orchestrator plans → Signal Analyst reads your store data → Strategist pulls live market trends → Activation writes the copy → Critic quality-checks everything before it reaches you
  3. Two campaign cards appear — Email + Instagram + TikTok copy, grounded in the specific signal that triggered them, with a live Market Context strip showing the Brave Search trends that shaped the copy
  4. You edit, approve, and launch — one click per platform, on your schedule

The 5 Agents

Agent Model Role Input → Output
Orchestrator Haiku 4.5 Parses your query, sets focus + priority Query → { focus, priority, context }
Signal Analyst Haiku 4.5 Reads store data, finds one Win + one Opportunity Store data → { win, opportunity, confidence }
Strategist Sonnet 4.6 Cross-references signals with live web trends, proposes 2 Next Best Actions Signals + Brave trends → { actions[2] } each with trends[2] citations
Activation Sonnet 4.6 Writes Email + Instagram + TikTok copy in one batch, grounded in trend data Action + trends → { email, instagram, tiktok }
Critic Haiku 4.5 Sequential 5-step reasoning → scores copy 1–10, rewrites if needed Draft → { approved, score, revised_copy }

Architecture: Agentic RAG

MailIntel is a practical implementation of Agentic RAG — where retrieval, reasoning, and generation are split across specialised agents instead of handled by one monolithic prompt. Standard RAG finds relevant text. Agentic RAG turns that text into executable business strategy.

How the RAG "Flow" Works

Agent RAG Role What It Actually Does
Orchestrator Intent detection Classifies query → Chat or Campaign; sets focus + priority for downstream agents
Signal Analyst Data retrieval Calls Shopify MCP tools; extracts Win (high velocity) + Opportunity (low stock, lapsed segment)
Strategist Context augmentation Merges Shopify signals with Brave Search web trends into a coherent strategic rationale
Activation Grounded generation Writes Email + Instagram + TikTok copy anchored to the specific signal + live trend
Critic Self-correction Sequential 5-step reasoning chain scores copy 1–10; rewrites automatically if below 8

Three Techniques That Keep Quality High and Cost Low

1. Context Compaction (Token Optimisation)

Raw Shopify JSON is expensive. Sending 250 full order objects to an LLM costs 14,000 tokens ($0.04 for a single context load).

MailIntel uses a pick() helper to strip 95% of JSON before it reaches any agent — keeping only the fields the reasoning step actually needs:

// Before: ~800 tokens per product (full Shopify object)
// After:  ~40 tokens per product (2 fields)
pick(product, ['title', 'variants.inventory_quantity'])

// Before: get_orders (250 results) → ~14,000 tokens
// After:  get_sales_summary → ~60 tokens (revenue + count only)

Result: Cost drops from ~$0.084/run (naïve) to ~$0.022/run. 74% reduction with no quality loss.

2. Sequential Thinking (Quality Gate)

The Critic agent doesn't score in a single pass. It follows a structured 5-step reasoning chain before producing its JSON output — the same pattern Adobe, Intuit, and other large-scale AI deployments use to prevent check-skipping:

STEP 1 — TONE CHECK        Is language warm? Any jargon or "bot-like" phrasing?
STEP 2 — NAMING VIOLATIONS  "Mailchimp", "based on your data" = automatic cap at score 6
STEP 3 — ACCURACY          Does copy name the specific product, metric, and urgency?
STEP 4 — CONCIERGE QUALITY  Would a trusted advisor who knows this store write this?
STEP 5 — FINAL SCORE       {"approved": true, "score": 9, "revised_copy": null}

If score < 8, the Critic returns a revised_copy in the same JSON structure. Sarah always sees the best version — never a first draft.

3. Hybrid External RAG (Brave Search Thread)

Standard RAG retrieves from your own data. MailIntel adds an external retrieval layer — one Brave Search call per run, threaded through three surfaces at zero additional cost:

1 Brave Search call →  Strategist prompt  (shapes the "why now" rationale)
                    →  Activation prompt  (makes copy trend-specific, not timeless)
                    →  Market Context strip on each campaign card (user-visible citations)

The search result is compressed to ~200 tokens of bullets before agent injection. The Strategist card shows 2 clickable trend citations linked to the real Brave Search source URLs. Copy is grounded in what's happening on the live web today — not templates.

Tech Stack at a Glance

Layer Choice
Reasoning (strategy + copy) Claude Sonnet 4.6
Routing + critique Claude Haiku 4.5
Tool connectivity MCP (Model Context Protocol) — stdio server + npx servers
Commerce data Shopify Admin REST API 2026-01 via local CORS proxy
External trends Brave Search API (1 call/run, threaded across 3 surfaces)
Sequential reasoning @modelcontextprotocol/server-sequential-thinking
Frontend Vanilla HTML + CSS + JS (no build tools)

Screenshots

Command input + welcome dashboard

Command Input

Ask anything — a quick store question gets a Haiku answer in ~1 second. Say "create campaigns" and the full 5-agent loop fires.


Agents running — live trace panel

Agents Running

Every agent call streams into the Intelligence Trace panel in real time. You see what was sent, what came back, and what it cost.


Results — Signal Analyst + Strategist cards

Results Overview

The Analyst card shows WIN and OPP labels with the product name, metric chip (e.g. "4 sold · 8 units left"), and a one-sentence insight. The Strategist shows each action with a trigger chip, causal rationale, and 2 clickable trend citations linking to the live Brave Search sources.


Campaign card — Email (side-by-side editor)

Campaign Email

Left: AI-generated copy (subject, preview text, body, CTA) — always editable, live-synced to the preview. Right: rendered email client preview that updates as you type.


Campaign card — Instagram

Campaign Instagram

Full Instagram dark-mode post mock with caption, hashtags, and best-time recommendation. A/B variant generates an alternative caption with one click.


Campaign card — TikTok

Campaign TikTok

Vertical video card (9:16) with hook, script, and hashtags. A/B variant generates an alternative hook line.


Intelligence Trace — every decision visible

Trace Panel

Each log line is colour-coded by agent. shows what was sent. shows what came back. Toggle visibility with the 🔬 icon.


Chat interface — conversational store intelligence

Chat Welcome

Ask quick store questions without triggering the full agent pipeline. Haiku answers in ~1 second from cached store data.


Chat with live market trends

Chat Question

When the question involves trends or market signals, Brave Search fires in parallel and the answer renders as a structured card: bold headline, numeric stat chips (green for positive, red for alerts), and a one-sentence so-what grounded in live web data.


Key Capabilities

  • Side-by-side email editor — AI copy on the left, rendered preview on the right, live-synced as you type
  • A/B variant generator — alternative subject line, caption, or hook via a focused 120-token Claude call (~$0.001)
  • Critic quality score — every card shows a 1–10 score; copies that score below 8 are auto-revised before reaching you
  • Market Context strip — collapsible teal strip on every campaign card showing Brave Search bullets with clickable source domain links (e.g. bloomberg.com ↗)
  • Trend citations on Strategist — each proposed action shows 2 trend observations with clickable source links pulled from the same single Brave Search call
  • Structured chat responses — Haiku chat answers render as headline + numeric stat chips + context sentence instead of plain text
  • Audience selector — target All contacts · Repeat buyers · Lapsed 30d · New this week; contact count and estimated revenue update live
  • Schedule toggle — Now · Best time ✨ (AI-recommended per platform) · Custom datetime
  • Full Intelligence Trace — every agent call, every response, every token cost, in real time

Chat Interface

MailIntel includes a full conversational layer that separates quick store questions from campaign generation:

  • Ask anything"Where did the revenue come from?" "Who are my best customers?" "Which products are low on stock?" — Haiku answers directly from live store data in ~1 second for ~$0.00015
  • Market trend questions"What's trending in eco products?" "What are people buying?" — Brave Search fires in parallel, answers grounded in live web data
  • Create campaigns — When you say "create campaigns" or "launch an email", the intent detector routes to the 5-agent pipeline with a confirm step first
  • Campaign CTA — When a chat answer surfaces an opportunity (low stock, lapsed customers), a "Create campaigns →" button appears inline

Intent detection is keyword-based: campaign, create, launch, email, instagram, tiktok, generate, promote → agent loop. Everything else → Haiku chat answer.


Live Shopify Integration

MailIntel connects to the Shopify Admin REST API (2026-01) through a local CORS proxy built into dev-server.js. The browser cannot call the Shopify Admin API directly (CORS), so all requests route through /shopify-proxy?path=....

# Add Shopify credentials to .env
SHOPIFY_SHOP_URL=https://your-store.myshopify.com
SHOPIFY_ADMIN_API_KEY=shpat_...

dev-server.js injects both as globals at serve time (__DEV_SHOPIFY_URL__, __DEV_SHOPIFY_TOKEN__), auto-switches the app to Live mode, and populates the metrics bar + header chips from real data on load.


Brave Search Integration

Every campaign run includes a live web trend fetch via Brave Search. One search query is constructed from the winning product + current month, fired in parallel with the Strategist call, and the results are reused across three surfaces — all for a single API call:

1 Brave Search call →  Strategist prompt  (shapes the strategy)
                    →  Activation prompt  (makes copy timely and specific)
                    →  Market Context strip on each campaign card

The Brave Search results are compressed to bullet form before being passed to agents:

• Canvas Tote April 2026 trend ecommerce: Search interest up 28% YoY...
• Eco canvas bags spring 2026: Consumer preference shifting toward...

Zero extra API calls — the same trend string is cached and threaded through the entire pipeline.

The Brave proxy in dev-server.js handles gzip decompression server-side:

# Add to .env
BRAVE_SEARCH_API_KEY=BSA...

MCP Integration

Three MCP servers are registered in .mcp.json (gitignored):

Server Protocol What it does
shopify Local stdio (shopify-mcp.js) 6 tools: get_shop_info, get_products, get_orders, get_customers, get_inventory, get_sales_summary
brave-search npx (@modelcontextprotocol/server-brave-search) Web search for live market trends
sequential-thinking npx (@modelcontextprotocol/server-sequential-thinking) Forces step-by-step reasoning in complex evaluations

Shopify MCP — 6 Tools

Tool What it returns Token-efficient default
get_shop_info Store name, currency, plan fields: "name,currency"
get_products Product list limit: 10, fields: "title,variants.inventory"
get_orders Order list limit: 10, created_at_min: 7d ago
get_customers Customer list limit: 20, fields: "name,orders_count,created_at"
get_inventory Product + variant + stock limit: 20, fields: "product,inventory"
get_sales_summary Aggregated revenue + count Server-side aggregation — no raw order list

Sequential Thinking — Critic Quality Gate

The Critic agent uses a 5-step sequential reasoning chain before producing its final JSON score. This prevents skipping checks and ensures logic is sound before output:

STEP 1 — TONE CHECK        Is language warm? Any jargon?
STEP 2 — NAMING VIOLATIONS  "Mailchimp", "based on your data" = auto-fail
STEP 3 — ACCURACY          Does copy match the signal that triggered it?
STEP 4 — CONCIERGE QUALITY  Would a trusted advisor write this?
STEP 5 — FINAL OUTPUT      {"approved": true, "score": 9, ...}

The parseJ() function handles the mixed reasoning+JSON output: it tries direct parse first, then extracts from the first {, then the last { — so both pure-JSON agents (Analyst, Strategist) and reasoning-prefix agents (Critic) parse correctly.


MCP Token Optimization

Every MCP tool response carries a token tax — the cost of transmitting raw API data through the context window. We eliminated ~90% of this cost through four techniques:

1. Aggregated endpoints over raw lists

❌ get_orders (limit: 250)  →  ~14,000 tokens (full order objects)
✅ get_sales_summary        →  ~60 tokens    (revenue + count only)

Reduction: 99.6%

2. Fields filtering on every call

// shopify-mcp.js — pick() helper filters before returning
pick(product, ['title', 'variants.inventory_quantity'])

 Full product object    ~800 tokens each
 Filtered (2 fields)    ~40 tokens each

Reduction: 95%

3. Time-scoped queries

❌ orders.json (all-time)           →  250+ results, thousands of tokens
✅ orders.json?created_at_min=7d   →  13 results, ~400 tokens

4. Compressed context for agent calls

Agents receive plain-text summaries, not raw JSON:

❌ Full fetchShopify() JSON  →  ~1,200 tokens per agent call
✅ Compressed shopSummary   →  ~150 tokens per agent call

Store: Vendant
Revenue 7d: $1,048 (13 orders, AOV $80.62)
Top products: Canvas Tote Natural [sold:4, inv:8] | Eco Pin Set [sold:2, inv:338]
Alerts: Canvas Tote (CT-NAT-001) — only 8 left
Segments: repeat=0 lapsed=15 new=15

Before / After — Full Run Cost

Version Tokens (input) Cost/run
v1 — all Sonnet, raw JSON context ~28,000 ~$0.084
v2 — model routing + compressed context ~8,400 ~$0.025
v2 + cached store data (no re-fetch) ~7,200 ~$0.022

~74% cost reduction with no change to output quality.


Stack

Layer Choice Why
Frontend Vanilla HTML + CSS + JS Zero setup, deploy anywhere — no build step
AI Claude Sonnet 4.6 + Haiku 4.5 Sonnet for reasoning, Haiku for routing + critique
Commerce data Shopify Admin REST API 2026-01 Live orders, products, customers, inventory
Market trends Brave Search API Real-time web signals for campaign timing
Sequential reasoning @modelcontextprotocol/server-sequential-thinking 5-step Critic chain
MCP Local stdio server (shopify-mcp.js) + 2 npx servers 6 Shopify tools + search + reasoning
CORS proxy dev-server.js (/shopify-proxy, /brave-proxy) Browser can't call Shopify Admin or Brave directly
Fonts Syne + JetBrains Mono + DM Sans Display / trace / body hierarchy
Dev server dev-server.js (Node built-in http) Reads .env, injects keys at serve time

Getting Started

git clone https://github.com/bnamatherdhala7/MailIntel.git
cd MailIntel

# Create .env with your keys (never committed — gitignored)
cat > .env <<EOF
ANTHROPIC_API_KEY=sk-ant-...
SHOPIFY_SHOP_URL=https://your-store.myshopify.com
SHOPIFY_ADMIN_API_KEY=shpat_...
BRAVE_SEARCH_API_KEY=BSA...
EOF

# Start dev server — reads .env, injects keys, proxies Shopify + Brave APIs
node dev-server.js

Open http://localhost:3000. API keys are injected into sessionStorage at page load and cleared when the tab closes — never written to disk.

Optional: seed your store with sample data

node seed-shopify.js
# Creates 8 products, 12 customers, 13 orders spread over 7 days

Cost

Scenario Cost
Chat answer (Haiku, store Q&A) ~$0.00015
Chat answer with Brave Search trend fetch ~$0.00020
Full run — 5 agents, 2 campaign cards ~$0.022
300 full runs/month ~$6.60/month
A/B variant call ~$0.001

Sonnet 4.6: $3/$15 per M tokens · Haiku 4.5: $1/$5 per M tokens
Token optimization (fields filtering + compressed context + model routing) reduces cost ~74% vs naive implementation.


Docs

  • CLAUDE.md — Build spec: Workflows, Actions, Tools (the authoritative source)
  • docs/prd.md — Full product requirements: problem analysis, architecture decisions, roadmap
  • agent-prompts.js — All 5 system prompts
  • mock-data.js — Mock Shopify data + audience segments
  • design-tokens.md — Colour tokens, typography, component specs

Out of Scope (v1)

  • Real email sending (mock success animation only)
  • Real Instagram / TikTok posting
  • User authentication
  • Multi-store support
  • Persistent session history

Built for the 33 million small businesses that are drowning in dashboards and starving for decisions.

About

5-agent MCP + RAG pipeline — Trend Scout → Segment Analyst → Copy → Assembler → QA Critic. Shopify MCP serves live store data; RAG retrieves competitor benchmarks. Autonomous campaign factory: 2 ready-to-send Email + Instagram + TikTok packs in ~90s.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors