Building Your First Agent

Building Your First Agent

This guide walks you through building a Cart Recovery Agent — an AI agent that monitors abandoned carts and orchestrates a personalized recovery sequence across email, SMS, and WhatsApp using Brevo’s MCP tools.

Prerequisites

  • Brevo account with API key (get one here)
  • Brevo MCP Server configured (setup guide)
  • Claude Desktop, Claude Code, or any MCP-compatible client
  • Email templates created in Brevo for cart recovery

How Agents Work

An agent is a markdown file that defines:

  1. Identity — what the agent does and its constraints
  2. Tools — which MCP tools it can access
  3. Instructions — how it should reason and act
  4. Guardrails — what it should never do

When invoked, the agent uses an LLM to reason about the goal, select appropriate tools, and execute actions against Brevo’s API.

Marketer: "Set up cart recovery for carts over $50"
Agent reads its specification (tools, constraints)
Agent reasons: "I need to create a segment, design a sequence, set up tracking"
Agent calls: brevo/create-segment → brevo/send-email → brevo/send-sms → brevo/track-event
Result: 3-step recovery sequence active, tracking events flowing

Step 1: Define the Agent

Create a file called cart-recovery-agent.md:

---
name: cart-recovery-agent
description: Recover abandoned carts with personalized multi-channel sequences
version: 1.0.0
temperature: 0.2
max_tokens: 4096
tools:
- brevo/list-contacts
- brevo/get-contact
- brevo/create-segment
- brevo/send-email
- brevo/send-sms
- brevo/track-event
- brevo/get-email-templates
- brevo/get-email-stats
triggers:
- event: cart_abandoned
conditions:
- cart_value: "> 50"
- time_since_activity: "> 30m"
- schedule: "0 */4 * * *"
permissions:
- contacts:read
- email:send
- sms:send
- events:write
---
# Cart Recovery Agent
You are an e-commerce cart recovery specialist working with Brevo's
engagement platform. Your goal is to recover abandoned carts through
personalized, well-timed multi-channel outreach.
## Strategy
When a cart is abandoned:
1. **Wait 1 hour**, then send a reminder email with cart contents
2. **Wait 24 hours**, if no open → send SMS with urgency message
3. **Wait 48 hours**, if still no recovery → send final email with
incentive (discount code if cart value > $100)
## Decision Framework
- Cart value < $50: Skip (not worth recovery cost)
- Cart value $50-$100: Email only (2 touches)
- Cart value $100-$250: Email + SMS (3 touches)
- Cart value > $250: Email + SMS + personal outreach flag
## Rules
- NEVER send more than 3 messages per abandoned cart
- NEVER contact customers who opted out of marketing
- ALWAYS check if cart was recovered before sending next step
- ALWAYS personalize with customer first name and cart items
- ALWAYS track recovery events for attribution
- Respect quiet hours: no SMS between 9pm-9am customer local time
## Email Templates
Use these Brevo template IDs:
- Reminder (step 1): template_id 101
- Urgency (step 2): template_id 102
- Incentive (step 3): template_id 103
## Metrics to Track
- `cart_recovery_email_sent` — recovery email dispatched
- `cart_recovery_sms_sent` — recovery SMS dispatched
- `cart_recovered` — customer completed purchase
- `cart_recovery_failed` — sequence completed without recovery

Step 2: Register Tools

The agent needs access to specific Brevo MCP tools. The tools field in the frontmatter defines which tools the agent can invoke. When the agent runs, it can only call these tools — everything else is blocked.

Here’s what each tool does in this agent’s context:

tools:
# Read customer data and cart state
- brevo/list-contacts # Find customers with abandoned carts
- brevo/get-contact # Get individual customer details
# Create targeted segments
- brevo/create-segment # Segment by cart value, time, behavior
# Send recovery messages
- brevo/send-email # Transactional recovery emails
- brevo/send-sms # SMS nudges for high-value carts
# Track outcomes
- brevo/track-event # Log recovery attempts and results
- brevo/get-email-stats # Check if emails were opened
- brevo/get-email-templates # Verify templates exist

Step 3: Create the Execution Chain

For complex agents, you can define a multi-step execution chain where specialized sub-agents handle different phases:

chain.yaml
steps:
- agent: analyzer
input: |
Analyze the abandoned cart data for the past 4 hours.
Goal: {task}
Use brevo/list-contacts to find contacts with
CART_ABANDONED event in the last 4 hours.
Segment by cart value tier.
- agent: sequencer
input: |
Based on this analysis, design recovery sequences:
{previous}
For each tier, create the appropriate message sequence
using the decision framework.
- agent: executor
input: |
Execute these recovery sequences via Brevo:
{previous}
Send emails and SMS according to the timing rules.
Track every action with brevo/track-event.
- agent: reporter
input: |
Generate a recovery report from these execution results:
{previous}
Include: carts targeted, messages sent, early recoveries,
projected revenue impact.

Step 4: Test the Agent

With Claude Code

Terminal window
# Point Claude Code at your agent spec
claude --mcp brevo "Run the cart recovery agent for abandoned carts in the last 4 hours"

With Claude Desktop

Once the Brevo MCP server is configured, ask Claude:

Run my cart recovery agent. Check for abandoned carts over $50 in the last 4 hours and execute the recovery sequence.

Claude will:

  1. Read the agent specification
  2. Call brevo/list-contacts to find abandoned carts
  3. Segment by cart value using the decision framework
  4. Send recovery emails via brevo/send-email
  5. Queue SMS follow-ups via brevo/send-sms
  6. Track all events via brevo/track-event

Programmatic Execution

import { TajoAgent } from "@tajo/agent-sdk";
import { BrevoMCPServer } from "@tajo/brevo-mcp-server";
const brevo = new BrevoMCPServer({
apiKey: process.env.BREVO_API_KEY,
});
const agent = new TajoAgent({
specPath: "./cart-recovery-agent.md",
mcpServers: [brevo],
model: "claude-sonnet-4-6",
});
// Run the agent
const result = await agent.run(
"Recover abandoned carts over $50 from the last 4 hours"
);
console.log(result.summary);
// → "Processed 23 abandoned carts. Sent 23 reminder emails,
// 8 SMS messages. 3 carts already recovered during execution."

Step 5: Schedule the Agent

Run the agent on a recurring schedule:

Cron-based

# In your agent spec frontmatter
triggers:
- schedule: "0 */4 * * *" # Every 4 hours
timezone: "America/New_York"

Event-driven

triggers:
- event: cart_abandoned
conditions:
- cart_value: "> 50"
- time_since_activity: "> 30m"
debounce: 5m

Webhook

triggers:
- webhook: /agents/cart-recovery/trigger
method: POST
authentication: api_key

Step 6: Monitor & Iterate

Track agent performance through Brevo events:

-- Recovery rate by cart value tier
SELECT
CASE
WHEN cart_value < 100 THEN '$50-$100'
WHEN cart_value < 250 THEN '$100-$250'
ELSE '$250+'
END as tier,
COUNT(*) as carts_targeted,
SUM(CASE WHEN recovered THEN 1 ELSE 0 END) as recovered,
ROUND(SUM(CASE WHEN recovered THEN 1 ELSE 0 END)::numeric
/ COUNT(*) * 100, 1) as recovery_rate_pct,
SUM(CASE WHEN recovered THEN cart_value ELSE 0 END) as revenue_recovered
FROM cart_recovery_events
WHERE created_at > NOW() - INTERVAL '30 days'
GROUP BY tier
ORDER BY tier;

Example: Complete Agent Session

Here’s a real interaction between a marketer and the Cart Recovery Agent:

Marketer: "Check abandoned carts from today and start recovery"
Agent: Checking for abandoned carts in the last 24 hours...
[Calls brevo/list-contacts with filter CART_ABANDONED_AT > today]
Found 47 abandoned carts:
- 12 carts under $50 (skipping per policy)
- 23 carts $50-$100 (email-only tier)
- 8 carts $100-$250 (email + SMS tier)
- 4 carts over $250 (email + SMS + flag for personal outreach)
Starting recovery sequences...
[Calls brevo/send-email × 35 with template_id 101]
[Calls brevo/track-event × 35 with event "cart_recovery_email_sent"]
✅ Step 1 complete:
- 35 reminder emails sent
- 12 SMS queued for 24h follow-up (high-value carts)
- 4 carts flagged for personal outreach
Next check scheduled in 4 hours.
Recovery tracking events: cart_recovery_email_sent (35),
cart_recovery_sms_queued (12), cart_recovery_escalated (4)

Next Steps

AI Assistant

Hi! Ask me anything about the docs.

Start Free with Brevo