How matiasbattocchia/open-bsp-api works
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: 03_models Core model · server.ts Plugin server · index.ts Agent client · migrations Migrations
Components 19
Database
Supabase Postgres schemas, RLS, and migrations.
- Migrations · Versioned evolution of tables, triggers, RLS, billing, and messaging behavior.
supabase/migrations - Core model · Organizations, contacts, conversations, messages, agents, API keys, webhooks, replies, logs, and onboarding.
supabase/schemas/03_models - Tenant rules · Organization-scoped and key-scoped access control across the data model.
supabase/schemas/05_rls - SQL helpers · Shared JSON, trigger, edge, and access-control helper functions.
supabase/schemas/02_functions - Billing · Plans, subscriptions, usage, ledger, invoices, payments, and billing guards.
supabase/schemas/06_billing - Seed data · Initial dataset for local and reproducible deployments.
supabase/seed.sql
Edge Functions
Deno serverless functions for webhooks, orchestration, and integrations.
- Webhook ingress · Validates Meta traffic, normalizes payloads, and writes WhatsApp state into Postgres.
supabase/functions/whatsapp-webhook/index.ts - Message flow · Reactive flow that links inbound messages, agent reasoning, and outbound dispatch.
supabase/schemas/03_models/03-05_messages.sql - Agent client · Builds conversation context, selects a protocol, serializes tools, and calls an external agent backend.
supabase/functions/agent-client/index.ts - Protocols · Chat-completions and A2A protocol logic plus factory and serializer code.
supabase/functions/agent-client/protocols - Tools · Calculator, HTTP, SQL, attachment, handoff, code, and MCP tool modules.
supabase/functions/agent-client/tools - Dispatcher · Sends outbound messages to the WhatsApp Cloud API and updates delivery state.
supabase/functions/whatsapp-dispatcher/index.ts - Management · Handles embedded signup, templates, and Business Management API administration.
supabase/functions/whatsapp-management/index.ts - Media prep · Processes attachments and documents for OCR, transcription, parsing, or enrichment.
supabase/functions/media-preprocessor/index.ts - MCP server · 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 · Claude Code plugin runtime that queries the API and can relay WhatsApp conversations.
plugin/server.ts - Plugin auth · Authentication and API access helpers for the Claude plugin.
plugin/auth.ts - Skill pack · Claude skill content for workflow guidance and configuration.
plugin/skills/configure/SKILL.md
Ops
Automation, deployment, and repository lifecycle support.
- GitHub Actions · CI and release workflows that deploy schema, functions, and secrets integration.
.github/workflows
Connections 24
- 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.