Open Brain System The open-source AI-integrated brain system — pgvector + MCP + Supabase

How to Build an Open Brain From Scratch

A two-hour walkthrough: Supabase Postgres with pgvector, an MCP server, and a minimal ingestion script. Under ten dollars a month in infrastructure.


Prerequisites

Building an open brain requires a specific stack of infrastructure and API access to handle vector storage and model context. The core requirements include a Supabase account; the free tier is sufficient for prototyping and initial deployment.

Technical Requirements

  • Runtime: Python 3.11+ or Node.js for server-side logic.
  • Embeddings API: An OpenAI API key for text-embedding-3-small or an open-source alternative like Nomic Embed via LM Studio.
  • AI Client: A Model Context Protocol (MCP) compatible interface, specifically Claude Desktop, Cursor, or Windsurf.
  • Database: PostgreSQL with the pgvector extension enabled.

Estimated setup time varies by technical proficiency. A first-time builder should allocate 90-120 minutes to configure the environment and API keys. Engineers familiar with Postgres and Python can typically complete the installation in approximately 30 minutes.

Step 1: Supabase + pgvector

The foundation of an open brain is a vector-enabled database capable of performing similarity searches. Supabase provides a managed PostgreSQL instance that supports the pgvector extension, allowing for the storage and querying of high-dimensional embeddings.

Database Initialization

First, create a new project in the Supabase dashboard. Navigate to the SQL Editor and execute the following command to enable vector support:

CREATE EXTENSION IF NOT EXISTS vector;

Schema Design

To store long-term memories, implement a table that pairs raw text with its corresponding vector representation. For OpenAI embeddings, the dimension is set to 1536.

CREATE TABLE memories (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  content TEXT,
  embedding VECTOR(1536),
  metadata JSONB,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

Optimization

As the memory store grows, linear scanning becomes inefficient. To maintain low latency during retrieval, create an IVFFlat index using cosine distance operators:

CREATE INDEX ON memories USING ivfflat (embedding vector_cosine_ops) 
WITH (lists = 100);

This indexing strategy is critical for those learning how to build an open brain, as it ensures the AI can retrieve relevant context in milliseconds regardless of database size.

Step 2: Embedding Pipeline

The embedding pipeline converts unstructured text into numerical vectors that represent semantic meaning. This process allows the system to find "related" concepts even if exact keywords do not match.

Implementation Script

Using the openai and supabase-py libraries, a script can be written to automate the ingestion of memories into the vector store.

import openai
from supabase import create_client

url = "your-supabase-url"
anon_key = "your-anon-key"
supabase = create_client(url, anon_key)
openai.api_key = "your-openai-key"

def add_memory(text, meta={}):
    # Generate embedding using OpenAI's efficient small model
    response = openai.Embedding.create(
        input=text, 
        model="text-embedding-3-small"
    )
    vector = response['data'][0]['embedding']
    
    # Insert into Supabase memories table
    supabase.table("memories").insert({
        "content": text,
        "embedding": vector,
        "metadata": meta
    }).execute()

add_memory("User prefers technical documentation over tutorials", {"category": "pref"})

Cost Analysis

Operating this pipeline is highly cost-effective. The text-embedding-3-small model costs approximately $0.02 per 1 million tokens. For personal knowledge bases or small-scale agents, the monthly expenditure is negligible, often falling within free credit tiers.

Step 3: MCP Server

The Model Context Protocol (MCP), introduced by Anthropic in 2024, standardizes how AI clients interact with external data. By implementing an MCP server, the AI can autonomously decide when to query the vector database to retrieve context.

Developing the Search Tool

The server acts as a bridge between the LLM and Supabase. It exposes a tool that takes a natural language string, converts it to a vector, and performs a cosine similarity search via a PostgreSQL RPC call or direct query.

from mcp.server import Server
import openai
from supabase import create_client

app = Server("open-brain-server")
supabase = create_client("url", "key")

@app.tool()
async def search_memories(query: str):
    """Search the open brain for relevant memories."""
    # Embed query text
    emb = openai.Embedding.create(input=query, model="text-embedding-3-small")['data'][0]['embedding']
    
    # Vector similarity search using pgvector's distance operator (<=>)
    res = supabase.rpc('match_memories', {'query_embedding': emb, 'match_threshold': 0.5, 'match_count': 5})
    return [item['content'] for item in res.data]

if __name__ == "__main__":
    app.run()

Protocol Standards

This architecture follows the MCP specification detailed at modelcontextprotocol.io. By decoupling the data retrieval logic from the LLM, users can swap models (e.g., moving from Claude 3.5 Sonnet to a local Llama model) without rewriting the memory infrastructure. This modularity is essential for anyone learning how to build an open brain that remains future-proof.

Step 4: Wire It In

The final stage involves registering the MCP server with a compatible client. For Claude Desktop, this requires editing the claude_desktop_config.json file to include the server's executable path and environment variables.

Configuration and Testing

Once configured, restart the AI client. Test the integration by prompting: "What are my technical preferences recorded in my open brain?" The client will trigger the search_memories tool, fetch the top-5 results from Supabase, and synthesize a response based on retrieved facts.

Scaling Considerations

Dataset Size Performance Note Optimization Required
< 10k rows Sub-millisecond latency None (Linear scan)
10k - 1M rows Fast retrieval IVFFlat or HNSW Index
> 1M rows Possible latency spikes Partitioning / PgBouncer

On a standard Supabase free tier, the system can handle roughly 100 queries per second with 50k rows before requiring advanced index tuning or connection pooling.

Questions answered

What readers usually ask next.

How do I build an AI second brain with pgvector?
Start by enabling the pgvector extension in a Supabase PostgreSQL database to support vector embeddings. Create a 'memories' table with a VECTOR column (typically 1536 dimensions for OpenAI) and use cosine similarity operators (<=>) to perform semantic searches. This allows your AI to retrieve contextually relevant data based on mathematical proximity rather than keyword matching.
What is the fastest way to set up an open brain system?
The quickest path is using a Supabase project combined with an MCP-compatible client like Claude Desktop or Cursor. By deploying the open-source supabase-mcp server, you can connect your database to your LLM via the Model Context Protocol without writing a custom integration from scratch.
How do I connect Claude Desktop to my Postgres database?
You must use an MCP (Model Context Protocol) server as the bridge. Install the supabase-mcp server and configure your Claude Desktop config file with your Supabase URL and API keys, allowing the LLM to execute SQL queries and retrieve vector data directly from your Postgres instance.
Can I use Nomic Embed instead of OpenAI for embeddings?
Yes. You can replace the OpenAI embedding call in your ingestion script with Nomic Embed or any Hugging Face model. Just ensure that the VECTOR dimension size defined in your pgvector table matches the output dimensions of the Nomic model you choose.
How much data can a Supabase free tier handle for an open brain?
The Supabase free tier is sufficient for prototyping and small-scale personal knowledge bases. While it has storage limits on the database size, pgvector's efficiency allows you to store thousands of embeddings before hitting performance bottlenecks or storage caps.
What is the recommended schema for an open brain memories table?
A robust schema includes a 'memories' table with columns for content (TEXT), embedding (VECTOR), metadata (JSONB), and a session_id (UUID) for multi-user isolation. Additionally, implementing a separate 'facts' table for subject-predicate-object triples helps the AI maintain structured, deterministic knowledge alongside semantic memories.
Do I need to know Python to build an open brain?
While not strictly required, basic proficiency in Python or Node.js is necessary to write the ingestion scripts that send data to your database. However, if you use pre-built MCP servers and low-code tools for data movement, you can manage most of the system via SQL and configuration files.
How do I write a custom MCP server?
Use the Model Context Protocol SDK to define 'tools' that your AI client can call. Each tool should be a function—such as 'search_memories' or 'add_fact'—that executes a specific SQL query against your pgvector database and returns the result as a string to the LLM.
Can I use SQLite instead of Supabase for an open brain?
Yes, provided you use the sqlite-vss extension for vector search. While SQLite is excellent for local-first applications, it lacks the built-in cloud scaling, real-time sync, and managed pgvector environment that Supabase provides out of the box.
What is the difference between pgvector and Pinecone?
pgvector is an extension that adds vector capabilities to a relational PostgreSQL database, allowing you to keep your metadata and embeddings in one place. Pinecone is a specialized, standalone vector database optimized for massive scale but requires managing a separate data silo from your primary application data.
How do I scale an open brain past 100k entries?
To maintain query speed at scale, move from flat scans to an HNSW (Hierarchical Navigable Small World) index in pgvector. This creates a graph-based structure that significantly accelerates nearest-neighbor searches compared to the standard ivfflat index.
Is there a ready-made open brain starter kit?
The most effective starting point is cloning the supabase-mcp repository from GitHub. This provides the necessary server logic to link your Supabase vector store to any MCP-compliant AI client, effectively giving you the infrastructure of an open brain.