Studio Loom: Building a Safe Autonomous Commerce Engine

Inspiration

We are entering the era of Agentic Commerce, where AI agents move beyond recommending products to actively searching, evaluating, reserving inventory, and executing purchases on behalf of users.

However, giving AI agents financial autonomy introduces challenges that traditional e-commerce systems were not designed to handle:

  • Hallucinations with financial consequences: An incorrect recommendation or fabricated product detail can lead to unauthorized or unintended purchases.
  • Inventory race conditions: Multiple agents attempting to purchase the last available unit simultaneously can cause overselling without proper database-level concurrency controls.
  • Unbounded spending: Autonomous agents can overspend or enter repetitive execution loops without deterministic spending limits and human oversight.

We wanted to explore a fundamental question: How can we give AI agents the ability to transact autonomously without compromising financial safety and user trust?

This led us to build Studio Loom, an agent-ready commerce engine that combines conversational AI with deterministic financial controls, atomic inventory reservations, and human-in-the-loop approvals.

Built using Google Gemini 2.5 Flash, MongoDB, PayPal Orders v2, and the Model Context Protocol (MCP), Studio Loom ensures that every autonomous purchase operates within clearly defined safety boundaries.

What It Does

Studio Loom transforms natural-language shopping requests into secure, guarded PayPal checkout flows.

1. Conversational Shopping and Intent Parsing

Users can describe what they want in natural language, such as "Find minimalist sneakers under $150."

A Gemini 2.5 Flash-powered agent extracts structured attributes, including budget, category, and stylistic preferences. It then evaluates available inventory and returns relevant recommendations with explainable reasoning.

2. Atomic Inventory Reservations

To prevent overselling, we implemented atomic conditional stock updates in MongoDB.

Instead of relying on separate read-and-write operations, stock reservations use conditional updates that only succeed when sufficient inventory exists:

{ stock: { $gte: quantity } }

Combined with atomic decrement operations and reservation timeouts, this prevents multiple agents from successfully reserving the same remaining inventory.

3. Autonomous Spending Guardrails

Every transaction passes through deterministic spending policies enforced at the backend API layer.

  • Auto-Proceed (≤ $100): Purchases within the micro-purchase threshold automatically generate a PayPal Orders v2 checkout session.
  • Human-in-the-Loop ($100–$500): Higher-value purchases reserve inventory and enter the manager's Approvals Queue. Managers can inspect the agent's reasoning and either approve the transaction or reject it and release the reserved stock.
  • Hard Safety Cap (> $500): Transactions exceeding the maximum spending limit are rejected automatically, and reserved inventory is released.

4. PayPal Orders v2 Integration

Studio Loom integrates PayPal's Orders v2 REST API to handle the complete checkout lifecycle.

The integration includes OAuth 2.0 access-token caching, retry handling, webhook signature verification, payment reconciliation, and a dedicated /checkout/success route.

After the customer approves payment, the backend captures the PayPal order and confirms the transaction before permanently committing the inventory reservation.

5. Model Context Protocol (MCP) Integration

We wanted Studio Loom to work beyond its own frontend.

We implemented an MCP-compatible server exposing commerce capabilities through a discoverable tool manifest and tool execution endpoints.

External AI clients, including Claude Desktop, Cursor, and custom AI workers, can discover and invoke tools to query product inventory and initiate guarded checkout workflows.

Importantly, external agents cannot bypass the same spending limits and transaction safeguards enforced by the backend.

6. Real-Time Audit Telemetry

Every important operation generates an audit event, including:

  • Agent decisions and product recommendations.
  • Inventory reservation and release events.
  • Spending guardrail evaluations.
  • Human approval and rejection decisions.
  • PayPal order creation and payment events.

These events are maintained in an append-only audit stream, providing visibility into how autonomous decisions translate into actual transactions.

How We Built It

We engineered Studio Loom as a full-stack TypeScript monorepo with separate frontend, backend, AI, payment, and MCP components.

Frontend

  • React 18 and TypeScript
  • Vite and Tailwind CSS
  • Bento Grid dashboard layout
  • Live execution pipeline visualizer
  • Interactive MCP tool sandbox and one-click demo scenarios

Backend

  • Node.js and Express
  • TypeScript and Mongoose
  • MongoDB atomic operations
  • Optimistic concurrency control
  • Inventory reservation and transaction state management

AI Core

  • Google Gemini 2.5 Flash
  • Structured JSON schema-based intent extraction
  • Natural-language product matching
  • Explainable recommendation reasoning

Payment Infrastructure

  • PayPal Orders v2 REST API
  • OAuth 2.0 token caching
  • Exponential backoff and retry handling
  • Webhook signature verification
  • Post-redirect payment capture and reconciliation

MCP Server

  • Model Context Protocol (v2024-11-05)
  • Tool discovery through /api/mcp/manifest
  • Tool execution through /api/mcp/tools/:toolName
  • External agent integration

The dashboard was designed around an obsidian-dark fintech aesthetic, with an execution pipeline that makes the agent's decisions and transaction lifecycle visible to users and developers.

Challenges We Ran Into

1. Preventing Inventory Race Conditions

One of our biggest challenges was ensuring inventory consistency when multiple autonomous agents attempted to purchase the same product simultaneously.

A conventional read-then-write approach creates a race condition: two agents can read the same stock value before either updates it.

We addressed this using MongoDB atomic conditional updates with $inc and a stock availability condition ($gte). This ensures that inventory is decremented only when sufficient stock remains.

We also introduced reservation timeouts to prevent abandoned checkout sessions from locking inventory indefinitely.

2. Designing a Safe Human-in-the-Loop State Machine

Implementing human approval was more complicated than simply adding an approval button.

Inventory needed to remain reserved while a transaction waited for manager approval, but rejection, timeout, or failed payment also needed to release that inventory safely.

We addressed this by designing explicit transaction state transitions:

  • pending_approval → awaiting_payment
  • pending_approval → rejected

This made the approval workflow predictable and helped prevent inventory deadlocks and invalid transaction transitions.

3. Handling PayPal's Post-Redirect Payment Flow

Another challenge was synchronizing the frontend, backend, and PayPal after a customer approved a payment.

We implemented a dedicated /checkout/success route that communicates with the backend capture endpoint. The backend verifies and captures the PayPal order before updating the transaction and inventory state.

This ensured that returning from PayPal did not automatically count as a successful purchase without payment verification.

4. Making Autonomous Commerce Accessible Through MCP

We also wanted external AI agents to interact with Studio Loom without depending on our frontend.

Implementing MCP tool discovery and execution required exposing commerce operations through a standardized interface while preserving the same backend safety checks.

This allowed external agents to interact with the commerce engine without receiving unrestricted access to payment or inventory operations.

Accomplishments We're Proud Of

  • End-to-End Agentic Commerce: Built a complete workflow connecting natural-language shopping, product recommendations, inventory reservation, approval workflows, and PayPal checkout.
  • Deterministic Financial Guardrails: Implemented automatic spending limits and human approval requirements enforced independently of LLM decisions.
  • Concurrency-Safe Inventory: Used atomic MongoDB operations to prevent overselling during simultaneous reservations.
  • MCP Interoperability: Enabled external AI clients to discover and invoke commerce tools through an MCP-compatible interface.
  • Payment Lifecycle Integration: Implemented PayPal OAuth authentication, order creation, retries, webhook verification, and payment capture.
  • Interactive Demonstration: Created one-click scenarios covering the happy path, approval gate, hard spending cap, and out-of-stock conditions.

What We Learned

Building Studio Loom reinforced an important principle: AI autonomy requires deterministic infrastructure underneath it.

Some of our biggest takeaways were:

  • LLMs should interpret intent, not enforce financial policies. Spending limits, authorization checks, and transaction validation must be implemented independently at the application layer.
  • Database guarantees are essential for agentic systems. As multiple autonomous agents operate concurrently, atomic updates and proper state management become critical.
  • Human oversight should be part of the architecture. Approval workflows are not just UI features; they require carefully designed transaction states and resource management.
  • Payment systems need explicit lifecycle management. Creating an order, receiving customer approval, capturing payment, and confirming inventory are separate operations that must remain synchronized.
  • MCP creates opportunities for agent interoperability. Standardized tool discovery and execution allow external agents to interact with applications without being tightly coupled to their frontend.

Most importantly, we learned that building an autonomous system is not just about making an agent capable of taking actions. It is about ensuring those actions remain authorized, bounded, observable, and reversible wherever possible.

What's Next for Studio Loom?

We see Studio Loom as a foundation for more advanced agentic commerce workflows. Our next areas of exploration include:

  • Recurring Agent Subscriptions: Integrating PayPal Subscriptions v1 to enable authorized agents to automatically replenish household essentials.
  • Multi-Merchant Cart Aggregation: Allowing agents to source products from multiple merchants and coordinate purchases through unified checkout experiences.
  • Decentralized Agent Identity: Exploring Decentralized Identifiers (DIDs) and Verifiable Credentials to associate agents with cryptographically verifiable identities and authorized spending allowances.
  • Advanced Agent Observability: Expanding audit telemetry with deeper execution traces, transaction analytics, and agent-level performance monitoring.

Our long-term vision is to make Studio Loom a secure commerce infrastructure layer where AI agents can transact autonomously while users retain control over their money, inventory, and purchasing decisions.

Built With

Share this project:

Updates

Submission history