# amzalfoumi/agentic-erp architecture

> Architecture diagram of the GitHub repository amzalfoumi/agentic-erp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/amzalfoumi/agentic-erp
- Repository: https://github.com/amzalfoumi/agentic-erp

## Overview

#### Purpose and Entry Points
AIsle is an agentic supermarket ERP for inventory and purchasing. Its core design exposes one Python domain layer through two backend entry points: FastAPI for the operator UI and MCP tools for AI-driven operations. A separate agent runtime orchestrates model conversations and MCP calls.
- `backend/api/main.py`
- `backend/mcp_server/server.py`
- `agent/app.py`

#### Shared Domain and Persistence Boundary
`backend/core` owns SQLAlchemy models, database setup, configuration, actors, enums, and domain exceptions. PostgreSQL is the system of record; Alembic migrations define products, purchasing, action drafts, credit memos, inventory lots, and row-level security. Services operate on this persistence layer rather than adapter-specific request objects.
- `backend/core/models.py`
- `backend/core/database.py`
- `backend/alembic/versions/20260827_1448-276428c7f1dd_create_purchasing_tables.py`

#### Framework-Free Business Services
`backend/services` is the primary business-logic boundary: it must not depend on HTTP or MCP. It implements product/catalog operations, inventory lots and spoilage, pricing, suppliers, and purchasing workflows. Services raise shared domain exceptions; outer adapters translate them for their protocols.
- `backend/services/products.py`
- `backend/services/inventory.py`
- `backend/services/purchasing/receiving.py`

#### HTTP API Adapter
The FastAPI layer provides thin authenticated HTTP routes and schemas for the frontend. Routes resolve dependencies, invoke services, and convert domain failures into HTTP responses. Purchasing, product, inventory, and draft endpoints are separately routed but converge on the same service layer.
- `backend/api/main.py`
- `backend/api/routes/purchasing.py`
- `backend/api/deps.py`

#### MCP Adapter and Agent Control Flow
The MCP server exposes corresponding backend capabilities as agent-callable tools, with its own authentication and error translation. The `agent` service manages conversations, selects a model provider, connects to MCP, gates tool execution, and persists conversations/approval state. This creates the flow: user/UI → agent → MCP tools → shared services → Postgres, with approval handling as a control boundary for consequential actions.
- `backend/mcp_server/server.py`
- `agent/conversation.py`
- `agent/mcp_client.py`

#### Operator Frontend
The Next.js frontend is an inventory-manager client, not a holder of ERP business rules. App-router pages and server actions support products, stock adjustments, spoilage, suppliers, purchasing/orders/receiving, and approvals. Its agent panel communicates through a catch-all proxy route to the agent runtime, while domain API clients communicate with backend services.
- `frontend/src/app/layout.tsx`
- `frontend/src/app/api/agent/[...path]/route.ts`
- `frontend/src/components/shell/agent-panel.tsx`

#### Runtime and Deployment Topology
Material runtimes are Python 3.12/FastAPI, MCP Python SDK, SQLAlchemy/Alembic, Next.js/TypeScript, and Postgres hosted on Supabase. Dockerfiles exist per frontend, backend, and agent. Deployment assets include Docker Compose and ThunderID identity-provider configuration; database seeding supports the AIsle-box environment.
- `backend/Dockerfile`
- `agent/Dockerfile`
- `deploy/aisle-box/docker-compose.yml`

Main entry points: [`backend/services/products.py`](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/services/products.py) (Business services), [`agent/app.py`](https://github.com/amzalfoumi/agentic-erp/blob/main/agent/app.py) (Agent service), [`deploy/aisle-box/docker-compose.yml`](https://github.com/amzalfoumi/agentic-erp/blob/main/deploy/aisle-box/docker-compose.yml) (AIsle-box deployment), [`backend/api/routes/purchasing.py`](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/api/routes/purchasing.py) (API routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Operator frontend"]
  node_next_app["Next.js operator app<br/>Next.js UI<br/>[layout.tsx]"]
  node_agent_panel["Agent panel<br/>UI component<br/>[agent-panel.tsx]"]
  node_agent_proxy["Agent proxy route<br/>Next.js route<br/>[route.ts]"]
end

subgraph group_backend["ERP backend"]
  node_fastapi["FastAPI API<br/>HTTP adapter<br/>[main.py]"]
  node_api_routes["API routes<br/>route handlers<br/>[purchasing.py]"]
  node_api_dependencies["API dependencies<br/>auth and DI<br/>[deps.py]"]
  node_mcp_server["MCP tool server<br/>MCP adapter<br/>[server.py]"]
  node_business_services["Business services<br/>domain services<br/>[products.py]"]
  node_inventory_service["Inventory operations<br/>domain service<br/>[inventory.py]"]
  node_receiving_service["Purchase receiving<br/>domain service<br/>[receiving.py]"]
  node_domain_core["Domain core<br/>models and persistence<br/>[models.py]"]
  node_database_setup["Database session<br/>SQLAlchemy setup<br/>[database.py]"]
end

subgraph group_agent["Agent runtime"]
  node_agent_runtime["Agent service<br/>conversation orchestrator<br/>[app.py]"]
  node_conversations["Conversation state<br/>agent state<br/>[conversation.py]"]
  node_mcp_client["MCP client<br/>tool client<br/>[mcp_client.py]"]
end

subgraph group_data["Data and deployment"]
  node_postgres[("Supabase Postgres<br/>system of record")]
  node_purchasing_migration["Purchasing migration<br/>Alembic migration"]
  node_aisle_box["AIsle-box deployment<br/>Docker Compose<br/>[docker-compose.yml]"]
end

node_next_app -->|"domain API calls"| node_fastapi
node_agent_panel -->|"agent requests"| node_agent_proxy
node_agent_proxy -->|"forwards"| node_agent_runtime
node_fastapi -->|"mounts"| node_api_routes
node_api_routes -->|"resolves auth and services"| node_api_dependencies
node_api_routes -->|"invokes"| node_business_services
node_business_services -->|"coordinates"| node_inventory_service
node_business_services -->|"coordinates"| node_receiving_service
node_business_services -->|"uses models and exceptions"| node_domain_core
node_domain_core -->|"persists through"| node_database_setup
node_database_setup -->|"connects"| node_postgres
node_agent_runtime -->|"persists context and approvals"| node_conversations
node_agent_runtime -->|"orchestrates tool calls"| node_mcp_client
node_mcp_client -->|"MCP protocol"| node_mcp_server
node_mcp_server -->|"invokes shared operations"| node_business_services
node_purchasing_migration -.->|"defines schema"| node_postgres
node_aisle_box -.->|"deploys"| node_next_app
node_aisle_box -.->|"deploys"| node_fastapi
node_aisle_box -.->|"deploys"| node_agent_runtime

click node_next_app "https://github.com/amzalfoumi/agentic-erp/blob/main/frontend/src/app/layout.tsx"
click node_agent_panel "https://github.com/amzalfoumi/agentic-erp/blob/main/frontend/src/components/shell/agent-panel.tsx"
click node_agent_proxy "https://github.com/amzalfoumi/agentic-erp/blob/main/frontend/src/app/api/agent/%5B...path%5D/route.ts"
click node_fastapi "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/api/main.py"
click node_api_routes "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/api/routes/purchasing.py"
click node_api_dependencies "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/api/deps.py"
click node_mcp_server "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/mcp_server/server.py"
click node_business_services "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/services/products.py"
click node_inventory_service "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/services/inventory.py"
click node_receiving_service "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/services/purchasing/receiving.py"
click node_domain_core "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/core/models.py"
click node_database_setup "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/core/database.py"
click node_agent_runtime "https://github.com/amzalfoumi/agentic-erp/blob/main/agent/app.py"
click node_conversations "https://github.com/amzalfoumi/agentic-erp/blob/main/agent/conversation.py"
click node_mcp_client "https://github.com/amzalfoumi/agentic-erp/blob/main/agent/mcp_client.py"
click node_purchasing_migration "https://github.com/amzalfoumi/agentic-erp/blob/main/backend/alembic/versions/20260827_1448-276428c7f1dd_create_purchasing_tables.py"
click node_aisle_box "https://github.com/amzalfoumi/agentic-erp/blob/main/deploy/aisle-box/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_next_app,node_agent_panel,node_agent_proxy toneBlue
class node_fastapi,node_api_routes,node_api_dependencies,node_mcp_server,node_business_services,node_inventory_service,node_receiving_service,node_domain_core,node_database_setup toneAmber
class node_agent_runtime,node_conversations,node_mcp_client toneMint
class node_postgres,node_purchasing_migration,node_aisle_box toneRose
```

## Components

### Operator frontend

- [Next.js operator app](https://github.com/amzalfoumi/agentic-erp/blob/main/frontend/src/app/layout.tsx): Next.js UI · Inventory-manager UI and server actions. · `frontend/src/app/layout.tsx`
- [Agent panel](https://github.com/amzalfoumi/agentic-erp/blob/main/frontend/src/components/shell/agent-panel.tsx): UI component · `frontend/src/components/shell/agent-panel.tsx`
- [Agent proxy route](https://github.com/amzalfoumi/agentic-erp/blob/main/frontend/src/app/api/agent/%5B...path%5D/route.ts): Next.js route · Catch-all route to the agent runtime. · `frontend/src/app/api/agent/[...path]/route.ts`

### ERP backend

- [FastAPI API](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/api/main.py): HTTP adapter · Authenticated operator-facing API entry point. · `backend/api/main.py`
- [API routes](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/api/routes/purchasing.py): route handlers · Purchasing, product, inventory, and draft endpoints. · `backend/api/routes/purchasing.py`
- [API dependencies](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/api/deps.py): auth and DI · `backend/api/deps.py`
- [MCP tool server](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/mcp_server/server.py): MCP adapter · Agent-callable backend tools with protocol-specific auth and errors. · `backend/mcp_server/server.py`
- [Business services](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/services/products.py): domain services · Framework-free product, inventory, supplier, pricing, and purchasing workflows. · `backend/services/products.py`
- [Inventory operations](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/services/inventory.py): domain service · Lots, adjustments, and spoilage operations. · `backend/services/inventory.py`
- [Purchase receiving](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/services/purchasing/receiving.py): domain service · `backend/services/purchasing/receiving.py`
- [Domain core](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/core/models.py): models and persistence · Shared SQLAlchemy models, actors, enums, configuration, and domain exceptions. · `backend/core/models.py`
- [Database session](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/core/database.py): SQLAlchemy setup · `backend/core/database.py`

### Agent runtime

- [Agent service](https://github.com/amzalfoumi/agentic-erp/blob/main/agent/app.py): conversation orchestrator · Selects models, gates consequential tools, and persists approvals. · `agent/app.py`
- [Conversation state](https://github.com/amzalfoumi/agentic-erp/blob/main/agent/conversation.py): agent state · Conversations and approval state. · `agent/conversation.py`
- [MCP client](https://github.com/amzalfoumi/agentic-erp/blob/main/agent/mcp_client.py): tool client · `agent/mcp_client.py`

### Data and deployment

- **Supabase Postgres**: system of record · ERP transactional data with row-level security.
- [Purchasing migration](https://github.com/amzalfoumi/agentic-erp/blob/main/backend/alembic/versions/20260827_1448-276428c7f1dd_create_purchasing_tables.py): Alembic migration · Defines purchasing-related tables and data controls. · `backend/alembic/versions/20260827_1448-276428c7f1dd_create_purchasing_tables.py`
- [AIsle-box deployment](https://github.com/amzalfoumi/agentic-erp/blob/main/deploy/aisle-box/docker-compose.yml): Docker Compose · Composes frontend, backend, and agent runtimes. · `deploy/aisle-box/docker-compose.yml`

## Connections

- Next.js operator app → FastAPI API: domain API calls
- Agent panel → Agent proxy route: agent requests
- Agent proxy route → Agent service: forwards
- FastAPI API → API routes: mounts
- API routes → API dependencies: resolves auth and services
- API routes → Business services: invokes
- Business services → Inventory operations: coordinates
- Business services → Purchase receiving: coordinates
- Business services → Domain core: uses models and exceptions
- Domain core → Database session: persists through
- Database session → Supabase Postgres: connects
- Agent service → Conversation state: persists context and approvals
- Agent service → MCP client: orchestrates tool calls
- MCP client → MCP tool server: MCP protocol
- MCP tool server → Business services: invokes shared operations
- Purchasing migration → Supabase Postgres: defines schema
- AIsle-box deployment → Next.js operator app: deploys
- AIsle-box deployment → FastAPI API: deploys
- AIsle-box deployment → Agent service: deploys
