Skip to main content
Glama
Akash5908

Commerce Ops MCP Server

by Akash5908
README.md
# Commerce Ops MCP Server

An **AI-native, remotely hosted MCP server** written in TypeScript that empowers operations teams and AI agents to investigate and resolve e-commerce order exceptions โ€” damaged items, lost shipments, refunds, and replacement dispatches โ€” safely and autonomously.

---

## ๐ŸŽฏ Problem & Justification

**User:** An operations agent (human or AI) handling post-delivery exceptions in an online commerce business.

**Problem:** When an order is damaged or lost, the ops agent must:
1. Investigate the order and understand what happened (Read)
2. Check if a replacement item is available in inventory (Read)
3. Process a partial or full refund safely (Write)
4. Escalate for a replacement shipment if appropriate (Write)

**Justification:** Resolving post-delivery exceptions (damaged/lost items) touches the three most critical domains of commerce ops (Order Management, Inventory, and Payment). It provides a complete, measurable operational outcome while remaining safely constrainable via programmatic guardrails (e.g., refund limits, idempotency).

## ๐Ÿข Systems Modeled (Strict PostgreSQL Architecture)

To support this workflow, the server connects to a **PostgreSQL Database** modeling three source systems with strict relational integrity:
1. **OMS (Order Management System):** Stores order history, items, and status.
2. **WMS (Warehouse Management System):** Stores WMS inventory and Carrier shipments.
3. **Payment Gateway:** Records processed refunds and enforces ledger protection (`CHECK (amount_refunded <= amount_paid)`).

## ๐Ÿ›ก๏ธ Read vs. Write Boundary

The workflow goes beyond lookup to safely bounded actions.
- **Read Boundary:** Unrestricted lookup of order details and inventory availability.
- **Write Boundary (Atomic):** Actions (Refunds, Replacements) are executed via single atomic `BEGIN/COMMIT` transactions.
  - **Guardrails:** A refund cannot exceed $150, the order must be <30 days old, risk score <70, and carrier exception verified. Failures route automatically to manager review via the `escalations` table.
  - **Idempotency:** Stable-intent idempotency is enforced purely at the database level using `UNIQUE` indexes on `(order_id, sku, action, amount)`. Duplicate requests are caught and gracefully returned without duplicating mutations.
  - **Replacements:** Are never created automatically. They are always routed to `escalations` for human approval.

---

## ๐Ÿ› ๏ธ MCP Tools

| Tool | Description | Inputs | Guardrails |
|---|---|---|---|
| `get_order_details` | Fetch order status, items, tracking, refund history | `orderId` | Returns error if not found |
| `check_inventory` | Check available stock for a SKU | `sku` | Returns error if SKU not found |
| `process_refund` | Apply a partial or full refund atomically | `orderId`, `sku`, `amount`, `action`, `idempotencyKey` | 6 strict guardrails (amount cap, risk, age). Stable-intent idempotency catches duplicates. |
| `request_replacement` | Escalate a request for a replacement item | `orderId`, `sku`, `reason`, `idempotencyKey` | Always escalates to manager. Enforces stable-intent idempotency on retries. |

---

## ๐Ÿš€ Live Hosted URL

- **MCP SSE Endpoint:** `https://diligence-mcp-ops.onrender.com/sse`
- **Health Check:** `https://diligence-mcp-ops.onrender.com/health`

> **Note:** The Render free tier spins down after 15 min of inactivity. Hit the health check URL first to warm it up.

---

## ๐Ÿ—๏ธ Architecture

```
src/
โ”œโ”€โ”€ index.ts          # Express server โ€” SSE + health endpoints
โ”œโ”€โ”€ server.ts         # MCP server factory โ€” reads tool registry, dispatches
โ”œโ”€โ”€ db/
โ”‚   โ”œโ”€โ”€ index.ts      # PostgreSQL connection pool and executeTransaction logic
โ”‚   โ”œโ”€โ”€ schema.sql    # DDL for all 9 tables and UNIQUE constraints
โ”‚   โ””โ”€โ”€ seed.ts       # Database seeding script for testing
โ””โ”€โ”€ tools/
    โ”œโ”€โ”€ index.ts      # Tool registry barrel
    โ”œโ”€โ”€ getOrder.ts   # get_order_details tool
    โ”œโ”€โ”€ checkStock.ts # check_inventory tool
    โ”œโ”€โ”€ processRefund.ts  # process_refund tool
    โ””โ”€โ”€ requestReplacement.ts # request_replacement tool
```

---

## ๐Ÿงช Local Setup & Verification

```bash
# Install dependencies
npm install

# Setup your local PostgreSQL and provide a .env file
# DATABASE_URL=postgresql://localhost:5432/diligence_mcp_test

# Run automated runtime verification (Testing all DB Constraints & Idempotency)
npm run test
```

Expected test output:
```
๐Ÿงช Running MCP Tool Verification Tests...
1. Testing get_order_details...                      โœ…
2. Testing process_refund (Valid Amount)...          โœ…
3. Testing process_refund (Stable Intent - Idempotency)... โœ…
4. Testing process_refund (Distinct Legitimate Intent)... โœ…
5. Testing process_refund (Guardrail: Exceed Balance)... โœ…
6. Testing request_replacement...                    โœ…
7. Testing request_replacement (Stable Intent)...    โœ…
8. Testing process_refund (Stable-Intent Lookup)...  โœ…
โœ… All PostgreSQL verification tests executed successfully!
```

---

## ๐Ÿ“ Product Decisions & Scope

### What's in scope
- Order investigation and status lookup using PostgreSQL JOINs.
- Safe refund processing with strict atomic transactions.
- Hardened database idempotency rules using PostgreSQL `SAVEPOINT`.
- Automated manager escalations for replacements and denied refunds.

### What's intentionally out of scope
- **Authentication / user management** โ€” a production version would use API keys or OAuth
- **Frontend / UI** โ€” the MCP is consumed by AI agents (Claude, Cursor) directly