Skip to content
cezar.garcia
All projects

System in real use · 2026

FBA Management

Back office for an Amazon FBA operation in Brazil, with an AI agent and an MCP server.

PythonFastAPISQLAlchemyPostgreSQLRedisReactTypeScriptMCP
Private code
~25
agent tools, the same in the chat and over MCP
190
database migrations (Alembic)
100%
of AI writes go through user approval

Overview

FBA Management is the internal system of an Amazon FBA operation in Brazil. I built it and maintain it, and it is in real use. The repository is private, so here I describe the product and the architecture without any operational data.

The goal is that each partner knows, without leaving the system, where every unit is (local, in transit or at Amazon), how much it cost, what is due and whether Amazon's numbers match the system's.

Home screen of the FBA Management AI assistant, with suggested queries and jobs
The assistant: query the operation or run a job in the same thread.

The problem

A generic ERP doesn't understand FBA, and FBA tools don't understand the Brazilian e-invoice. In Brazil a purchase starts as an NF-e, and the real cost of each unit includes freight, taxes and prep, prorated across the items.

The system takes the NF-e as the origin of the purchase, computes that full landed cost, keeps stock as a single identity across three places (local, in transit and at Amazon) and uses Amazon only as a read source for reconciliation.

  • NF-e XML import with a review step before it becomes a purchase order, payables and stock.
  • Costing with prorated freight, taxes and prep, and weighted average cost.
  • Shipments to Amazon tracked from the warehouse to the fulfilment centre.
  • Finance with payables and receivables, a calendar, recurring entries and cash flow.
  • Read-only sync with Amazon: listings, orders, inventory, finances and ads.

Architecture

The back end is an async FastAPI service with SQLAlchemy 2.0 on PostgreSQL, organised into routes, schemas and services that hold the business rules. The front end is a React app with TypeScript, TanStack Query and shadcn/ui.

Every business record carries the company identifier, so isolation between companies already exists in the model. The Amazon integrations are read-only by rule: creating or changing anything on Amazon still happens in Seller Central.

An AI agent on top of the product's own API

The chat has a single agent: a hand-written loop over OpenRouter, with streaming, a step limit and a daily cost cap. The user picks the model, and a fallback model takes over if the chosen one fails.

Tools are defined once and call the product's own API routes as the user. That way the numbers in the conversation are the ones on screen and the permissions are the ones in the UI. The schemas come from the same Pydantic models as the routes.

Every write goes through approval. What runs is the arguments stored when approval was requested, never what the client sends back, and each approval is valid once and expires.

  • No free-form SQL: queries come from a metrics registry and from the route views.
  • A stable prefix (tools, prompt and history) to benefit from the provider's prompt cache.
  • Long conversations are compacted into a summary past 60% of the model's context window.
  • The user sees and controls what the AI remembers between conversations.

MCP server with OAuth 2.1

The same tools as the chat are available to external AIs through an MCP server (stateless Streamable HTTP). You can connect Claude, ChatGPT or Cursor and ask about sales, stock or finance from there.

Authorisation is OAuth 2.1 with its own authorisation server: dynamic client registration, PKCE and refresh with rotation, with the consent screen inside the app. The access token is only valid for the MCP and does not work as a login on the regular API. For scripts there is also a personal token.

Over MCP, a write returns a preview and a confirmation id. The action only happens after the person confirms.

Decisions and delivery

The AI layer has already been rewritten once. The first version had intent routing, a separate ads agent, delegated specialists and about 40 tools maintained by hand in four places. I replaced all of it with a single agent and one tool registry, and wrote down why in an ADR.

Every push to the main branch runs the tests, publishes the images and deploys with Docker Compose and Caddy. Before bringing up the new version, the deploy script backs up the database and aborts if the backup fails.

Full stack

Back end
Python 3.12FastAPI (async)SQLAlchemy 2.0Pydantic v2Alembic
Front end
React 18TypeScriptViteTanStack QueryTailwind CSSshadcn/ui
Data
PostgreSQL 16Redis (Streams)
AI
OpenRouterMCP (Streamable HTTP)OAuth 2.1 + PKCEAI SDK (useChat)
Integrations
Amazon SP-APIAmazon Ads APINF-e (XML)
Infrastructure
Docker ComposeCaddyGitHub ActionsGHCR