Architecture

Architecture

System Overview

┌─────────────┐     ┌──────────────┐     ┌─────────────┐
│   Frontend  │────▶│   REST API   │────▶│  PostgreSQL  │
│  (Next.js)  │     │   (Go/Gin)   │     │             │
└─────────────┘     └──────┬───────┘     └─────────────┘

                    ┌──────┴───────┐
                    │  Event Bus   │
                    └──────┬───────┘

┌─────────────┐     ┌──────┴───────┐     ┌─────────────┐
│ MCP Clients │────▶│  MCP Agent   │────▶│  Ngamia AI   │
│ (Claude/Cursor)   │  (Python)    │     │  Gateway     │
└─────────────┘     └──────┬───────┘     └─────────────┘

                    ┌──────┴───────┐
                    │  External    │
                    │  Services    │
                    └──────────────┘
                    SendAfrica (SMS)
                    MailAfrica (Email)
                    MinIO (Storage)
                    Wikimedia (Images)

API Modules (Go Backend)

The Go API is organized into 17 domain modules, each with routes, handlers, services, and models:

ModuleTablesDescription
accountsorganisations, stores, users, phone_otps, email_verification_tokens, ai_creditsAuth, registration, org management
inventorycategories, products, stock_adjustmentsProduct catalog and stock
transactionstransactions, transaction_lines, txn_countersPOS sales, refunds, voids
customerscustomersCustomer profiles
creditscredit_tabs, credit_payments, credit_messages, credit_notesMadeni/tabs system
supplierssuppliers, supplier_deliveries, supplier_paymentsVendor management
expensesexpensesOperating expenses
staff(uses users table)Employee management
stores(uses stores table)Multi-store management
analytics(reads shared schema)Dashboard KPIs
reportsscheduled_reports, report_exportsReport generation
notebooknotesStaff memos
subscriptionssubscription_plans, subscriptionsBilling
reviewsstore_reviewsLanding page reviews
notificationsnotificationsIn-app notifications
aiai_conversations, ai_messagesAI chat history
uploads(files in MinIO)File storage

Event Bus

The internal event bus decouples side effects from transaction handlers:

EventHandlerAction
user.registeredaccounts, subscriptionsSend welcome email, create trial
staff.createdaccountsSMS temp password
transaction.createdcustomers, creditsUpdate stats, auto-create credit tab
transaction.refundedcustomers, creditsReduce stats, settle tab
transaction.voidedcreditsSettle credit tab

Agent Architecture

The MCP Agent acts as a bridge between natural language and the REST API:

  1. User sends a question (Swahili/English)
  2. LLM (via Ngamia gateway) interprets intent
  3. Agent calls the appropriate MCP tools (which map to API endpoints)
  4. API executes business logic
  5. Agent formats the response and returns it

The agent supports multi-step operations (up to 8 tool calls per query) for complex tasks like "compare last week's sales with this week and suggest promotions."