# matiasbattocchia/open-bsp-api architecture

> Architecture diagram of the GitHub repository matiasbattocchia/open-bsp-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/matiasbattocchia/open-bsp-api
- Repository: https://github.com/matiasbattocchia/open-bsp-api

## Overview

1. **What this repo is**
   OpenBSP API is the backend/control plane for a multi-tenant WhatsApp platform. It is not a monolithic app server; most logic is split between **Supabase Postgres schema/triggers** and **Deno Edge Functions**, with clients talking to Supabase directly through REST/realtime and authenticated API keys.

2. **Primary runtime boundary**
   The system runs on three main surfaces: the **database layer** under `supabase/schemas` and `supabase/migrations`, the **serverless layer** under `supabase/functions`, and the **Claude Code plugin** under `plugin/`. The database is the source of truth; functions react to database changes and external webhooks.

3. **Core data model**
   The central tenant boundary is `organizations`. Around it are `organizations_addresses` (connected WhatsApp phone numbers), `contacts` and `contacts_addresses` (separate lifecycle from contacts), `conversations`, `messages`, `agents`, `api_keys`, `webhooks`, `quick_replies`, `logs`, and onboarding/billing tables. Row-level security is a major architectural boundary: access is scoped by organization and API key/user role.

4. **WhatsApp ingress path**
   Incoming WhatsApp events enter through `whatsapp-webhook`. That function validates Meta webhook traffic, normalizes payloads, and writes message/conversation state into Postgres. From there, table triggers drive downstream behavior instead of the webhook function calling everything directly.

5. **Message processing pipeline**
   The repo’s key reactive flow is: **WhatsApp webhook → messages table → trigger → agent-client → messages table → trigger → whatsapp-dispatcher → WhatsApp API**. This decouples ingestion, agent reasoning, and outbound delivery so each stage can scale and fail independently.

6. **Outbound WhatsApp operations**
   `whatsapp-dispatcher` sends messages to the WhatsApp Cloud API. `whatsapp-management` handles Business Management API tasks such as embedded signup, templates, and phone/account administration. These functions are the integration boundary with Meta, while the database stores the canonical state for accounts and phone-number links.

7. **Agent subsystem**
   `agent-client` is the orchestration layer for AI or external agent backends. It builds conversation context from the database, selects a protocol (`chat-completions` or `a2a`), serializes tools/context, and sends the request to an external agent provider. The design intentionally keeps “agent logic” outside the core messaging pipeline, so advanced agents can live as separate services.

8. **Tools and extensibility**
   The agent layer has built-in tool modules for calculator, HTTP, SQL, attachment handling, handoff, and MCP integration. This makes agent execution a bounded capability set rather than unrestricted application logic. Tool definitions and protocol factories live alongside the agent function, not in the database.

9. **Media processing**
   `media-preprocessor` is a separate processing path for attachments and documents. It exists because media interpretation is operationally different from message transport: files may need OCR, transcription, parsing, or enrichment before they are useful to agents or UI consumers. The README indicates organization-level configuration controls when and how this runs.

10. **Integration surfaces for external clients**
   The `mcp` Edge Function exposes the platform via MCP over SSE for agentic clients such as Claude Desktop. Separately, `plugin/` packages a Claude Code plugin that can query the API and optionally relay WhatsApp conversations in real time. Both are access layers over the same Supabase-backed data, not separate business systems.

11. **Security and tenant isolation**
   Access control is enforced through Supabase auth, organization-scoped RLS policies, API keys, and optional allowlists on contacts/accounts. `api_keys` can be treated like service users with scoped rights, while user roles in organizations distinguish owner/admin/member capabilities. This is the primary boundary preventing cross-tenant data leakage.

12. **Operational and lifecycle concerns**
   Billing, onboarding tokens, invitations, logs, and cron-related jobs are first-class schema concerns rather than external services. The many migrations show the schema is heavily evolved and versioned, while `seed.sql` and the schema folders define reproducible deployments. GitHub Actions and Supabase GitHub integration automate deployment of schema and Edge Functions.

Main entry points: [`supabase/schemas/03_models`](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/03_models) (Core model), [`plugin/server.ts`](https://github.com/matiasbattocchia/open-bsp-api/blob/main/plugin/server.ts) (Plugin server), [`supabase/functions/agent-client/index.ts`](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/agent-client/index.ts) (Agent client), [`supabase/migrations`](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/migrations) (Migrations)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_database["Database"]
  node_migrations["Migrations<br/>schema history"]
  node_core_model[("Core model<br/>postgres schema")]
  node_org_boundary{{"Tenant rules<br/>rls policies"}}
  node_db_helpers["SQL helpers<br/>sql functions"]
  node_billing_schema[("Billing")]
  node_seed_data["Seed data<br/>bootstrap sql<br/>[seed.sql]"]
end

subgraph group_functions["Edge Functions"]
  node_whatsapp_webhook["Webhook ingress<br/>edge function<br/>[index.ts]"]
  node_message_pipeline(("Message flow<br/>db-trigger pipeline<br/>[03-05_messages.sql]"))
  node_agent_client["Agent client<br/>edge function<br/>[index.ts]"]
  node_agent_protocols["Protocols<br/>protocol implementations"]
  node_agent_tools["Tools<br/>bounded capabilities"]
  node_whatsapp_dispatcher["Dispatcher<br/>edge function<br/>[index.ts]"]
  node_whatsapp_management["Management<br/>edge function<br/>[index.ts]"]
  node_media_preprocessor["Media prep<br/>edge function<br/>[index.ts]"]
  node_mcp_server["MCP server<br/>edge function<br/>[index.ts]"]
end

subgraph group_plugin["Claude Plugin"]
  node_plugin_server["Plugin server<br/>plugin runtime<br/>[server.ts]"]
  node_plugin_auth["Plugin auth<br/>plugin client<br/>[auth.ts]"]
  node_plugin_skill["Skill pack<br/>plugin skills<br/>[SKILL.md]"]
end

subgraph group_ops["Ops"]
  node_github_actions["GitHub Actions<br/>automation<br/>[workflows]"]
end

node_migrations -->|"evolves"| node_core_model
node_migrations -->|"evolves"| node_org_boundary
node_migrations -->|"evolves"| node_billing_schema
node_core_model -->|"protected by"| node_org_boundary
node_core_model -->|"uses"| node_db_helpers
node_seed_data -.->|"bootstraps"| node_core_model
node_whatsapp_webhook -->|"writes"| node_core_model
node_core_model -->|"drives"| node_message_pipeline
node_message_pipeline -->|"invokes"| node_agent_client
node_agent_client -->|"selects"| node_agent_protocols
node_agent_client -->|"serializes"| node_agent_tools
node_agent_client -->|"reads/writes"| node_core_model
node_message_pipeline -->|"invokes"| node_whatsapp_dispatcher
node_whatsapp_dispatcher -->|"reads/writes"| node_core_model
node_whatsapp_management -->|"updates"| node_core_model
node_media_preprocessor -->|"reads/writes"| node_core_model
node_mcp_server -->|"reads"| node_core_model
node_plugin_server -->|"uses"| node_plugin_auth
node_plugin_server -->|"uses"| node_plugin_skill
node_plugin_server -->|"reads"| node_core_model
node_plugin_server -.->|"optional relay"| node_mcp_server
node_github_actions -->|"deploys"| node_migrations
node_github_actions -->|"deploys"| node_whatsapp_webhook
node_github_actions -->|"deploys"| node_mcp_server

click node_migrations "https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/migrations"
click node_core_model "https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/03_models"
click node_org_boundary "https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/05_rls"
click node_db_helpers "https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/02_functions"
click node_billing_schema "https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/06_billing"
click node_seed_data "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/seed.sql"
click node_whatsapp_webhook "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/whatsapp-webhook/index.ts"
click node_message_pipeline "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/schemas/03_models/03-05_messages.sql"
click node_agent_client "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/agent-client/index.ts"
click node_agent_protocols "https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/functions/agent-client/protocols"
click node_agent_tools "https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/functions/agent-client/tools"
click node_whatsapp_dispatcher "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/whatsapp-dispatcher/index.ts"
click node_whatsapp_management "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/whatsapp-management/index.ts"
click node_media_preprocessor "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/media-preprocessor/index.ts"
click node_mcp_server "https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/mcp/index.ts"
click node_plugin_server "https://github.com/matiasbattocchia/open-bsp-api/blob/main/plugin/server.ts"
click node_plugin_auth "https://github.com/matiasbattocchia/open-bsp-api/blob/main/plugin/auth.ts"
click node_plugin_skill "https://github.com/matiasbattocchia/open-bsp-api/blob/main/plugin/skills/configure/SKILL.md"
click node_github_actions "https://github.com/matiasbattocchia/open-bsp-api/blob/main/.github/workflows"

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_migrations,node_core_model,node_org_boundary,node_db_helpers,node_billing_schema,node_seed_data toneBlue
class node_whatsapp_webhook,node_message_pipeline,node_agent_client,node_agent_protocols,node_agent_tools,node_whatsapp_dispatcher,node_whatsapp_management,node_media_preprocessor,node_mcp_server toneAmber
class node_plugin_server,node_plugin_auth,node_plugin_skill toneMint
class node_github_actions toneRose
```

## Components

### Database

Supabase Postgres schemas, RLS, and migrations.

- [Migrations](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/migrations): schema history · Versioned evolution of tables, triggers, RLS, billing, and messaging behavior. · `supabase/migrations`
- [Core model](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/03_models): postgres schema · Organizations, contacts, conversations, messages, agents, API keys, webhooks, replies, logs, and onboarding. · `supabase/schemas/03_models`
- [Tenant rules](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/05_rls): rls policies · Organization-scoped and key-scoped access control across the data model. · `supabase/schemas/05_rls`
- [SQL helpers](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/02_functions): sql functions · Shared JSON, trigger, edge, and access-control helper functions. · `supabase/schemas/02_functions`
- [Billing](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/schemas/06_billing): billing schema · Plans, subscriptions, usage, ledger, invoices, payments, and billing guards. · `supabase/schemas/06_billing`
- [Seed data](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/seed.sql): bootstrap sql · Initial dataset for local and reproducible deployments. · `supabase/seed.sql`

### Edge Functions

Deno serverless functions for webhooks, orchestration, and integrations.

- [Webhook ingress](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/whatsapp-webhook/index.ts): edge function · Validates Meta traffic, normalizes payloads, and writes WhatsApp state into Postgres. · `supabase/functions/whatsapp-webhook/index.ts`
- [Message flow](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/schemas/03_models/03-05_messages.sql): db-trigger pipeline · Reactive flow that links inbound messages, agent reasoning, and outbound dispatch. · `supabase/schemas/03_models/03-05_messages.sql`
- [Agent client](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/agent-client/index.ts): edge function · Builds conversation context, selects a protocol, serializes tools, and calls an external agent backend. · `supabase/functions/agent-client/index.ts`
- [Protocols](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/functions/agent-client/protocols): protocol implementations · Chat-completions and A2A protocol logic plus factory and serializer code. · `supabase/functions/agent-client/protocols`
- [Tools](https://github.com/matiasbattocchia/open-bsp-api/tree/main/supabase/functions/agent-client/tools): bounded capabilities · Calculator, HTTP, SQL, attachment, handoff, code, and MCP tool modules. · `supabase/functions/agent-client/tools`
- [Dispatcher](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/whatsapp-dispatcher/index.ts): edge function · Sends outbound messages to the WhatsApp Cloud API and updates delivery state. · `supabase/functions/whatsapp-dispatcher/index.ts`
- [Management](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/whatsapp-management/index.ts): edge function · Handles embedded signup, templates, and Business Management API administration. · `supabase/functions/whatsapp-management/index.ts`
- [Media prep](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/media-preprocessor/index.ts): edge function · Processes attachments and documents for OCR, transcription, parsing, or enrichment. · `supabase/functions/media-preprocessor/index.ts`
- [MCP server](https://github.com/matiasbattocchia/open-bsp-api/blob/main/supabase/functions/mcp/index.ts): edge function · Exposes the platform over MCP/SSE for external agentic clients. · `supabase/functions/mcp/index.ts`

### Claude Plugin

Claude Code plugin and MCP-facing client surface.

- [Plugin server](https://github.com/matiasbattocchia/open-bsp-api/blob/main/plugin/server.ts): plugin runtime · Claude Code plugin runtime that queries the API and can relay WhatsApp conversations. · `plugin/server.ts`
- [Plugin auth](https://github.com/matiasbattocchia/open-bsp-api/blob/main/plugin/auth.ts): plugin client · Authentication and API access helpers for the Claude plugin. · `plugin/auth.ts`
- [Skill pack](https://github.com/matiasbattocchia/open-bsp-api/blob/main/plugin/skills/configure/SKILL.md): plugin skills · Claude skill content for workflow guidance and configuration. · `plugin/skills/configure/SKILL.md`

### Ops

Automation, deployment, and repository lifecycle support.

- [GitHub Actions](https://github.com/matiasbattocchia/open-bsp-api/blob/main/.github/workflows): automation · CI and release workflows that deploy schema, functions, and secrets integration. · `.github/workflows`

## Connections

- Migrations → Core model: evolves · Migrations define and revise the canonical data model.
- Migrations → Tenant rules: evolves · Migrations adjust tenant isolation and access rules.
- Migrations → Billing: evolves · Billing schema changes are versioned through migrations.
- Core model → Tenant rules: protected by · Core tables are guarded by organization- and role-scoped policies.
- Core model → SQL helpers: uses · Tables and triggers depend on shared SQL helper functions.
- Seed data → Core model: bootstraps · Initial rows populate the core tables for deployment or demo.
- Webhook ingress → Core model: writes · Webhook ingestion persists messages, conversations, and related state.
- Core model → Message flow: drives · Message inserts and updates activate the reactive processing pipeline.
- Message flow → Agent client: invokes · Database state transitions trigger agent orchestration.
- Agent client → Protocols: selects · The orchestration layer chooses a protocol implementation.
- Agent client → Tools: serializes · Tools and context are packaged for the external agent provider.
- Agent client → Core model: reads/writes · Agent execution builds context from and writes responses back into Postgres.
- Message flow → Dispatcher: invokes · Outbound message state triggers delivery after database updates.
- Dispatcher → Core model: reads/writes · Dispatcher reads canonical outbound state and updates delivery status.
- Management → Core model: updates · Business management tasks persist phone, template, and account state.
- Media prep → Core model: reads/writes · Attachment processing enriches media-related records.
- MCP server → Core model: reads · MCP clients query the same Supabase-backed business state.
- Plugin server → Plugin auth: uses · The plugin runtime authenticates before querying the API.
- Plugin server → Skill pack: uses · Runtime behavior is guided by bundled skill content.
- Plugin server → Core model: reads · The plugin surfaces organization and conversation data from the backend.
- Plugin server → MCP server: optional relay · The plugin can bridge or relay conversations through the MCP/API surface.
- GitHub Actions → Migrations: deploys · CI workflows apply schema changes through Supabase automation.
- GitHub Actions → Webhook ingress: deploys · Edge functions are released through repository automation.
- GitHub Actions → MCP server: deploys · Automation also ships the MCP function surface.
