{"title":"Synapse Architecture","slug":"architecture","category":"concepts","summary":"How Synapse is built — Fastify, PostgreSQL, FTS5, embeddings, MCP server.","audience":["human","llm"],"tags":["architecture","design","components"],"difficulty":"intermediate","updated":"2026-06-27","word_count":435,"read_minutes":2,"lang":"en","translated":true,"requested_lang":"en","content_markdown":"\n# Synapse Architecture\n\nSynapse is a multi-tenant memory API built on Fastify, PostgreSQL with FTS5,\nand an optional embeddings service for semantic search.\n\n## High-Level Architecture\n\n```\n┌──────────────────────────────────────────────────────────┐\n│                       Clients                            │\n├──────────────┬──────────────┬──────────────┬─────────────┤\n│  LLM Agents  │  Web Browsers│  MCP Clients │  Webhooks   │\n│  (curl/SDK)  │  (humans)    │ (Claude/etc) │  (external) │\n└──────┬───────┴──────┬───────┴──────┬───────┴──────┬──────┘\n       │              │              │              │\n       └──────────────┴──────────────┴──────────────┘\n                              │\n                              ▼\n              ┌───────────────────────────────┐\n              │    Synapse API (Fastify)      │\n              │    port 12800                 │\n              │    - REST endpoints           │\n              │    - Auth (Mind Key / JWT)    │\n              │    - Rate limiting            │\n              │    - Static file serving      │\n              └───────────────┬───────────────┘\n                              │\n              ┌───────────────┼───────────────┐\n              ▼               ▼               ▼\n       ┌─────────────┐ ┌─────────────┐ ┌─────────────┐\n       │ PostgreSQL  │ │ Embeddings  │ │ MCP Server  │\n       │ + FTS5      │ │ Service     │ │ (separate)  │\n       │             │ │ (optional)  │ │ port 13100  │\n       │ - memories  │ │             │ │             │\n       │ - tasks     │ │ - generate  │ │ - 79 tools  │\n       │ - chat      │ │   embeddings│ │ - stdio/SSE │\n       │ - scripts   │ │             │ │ - WebSocket │\n       └─────────────┘ └─────────────┘ └─────────────┘\n```\n\n## Core Components\n\n### 1. Synapse API (Fastify)\n\nThe main HTTP server. Handles:\n\n- REST endpoints (`/memory`, `/chat`, `/mind/tasks`, etc.)\n- Authentication (Mind Key for agents, JWT for humans)\n- Rate limiting (`?key=` auth only)\n- Static file serving (web UI at `/human`, `/docs`, etc.)\n- Webhook dispatch\n\nBuilt with **Fastify 5** for performance. Runs on port 12800 in production.\n\n### 2. PostgreSQL with FTS5\n\nThe primary data store. Uses PostgreSQL with the FTS5 extension for\nfull-text search.\n\n**Key tables:**\n\n| Table | Purpose |\n|-------|---------|\n| `users` | User accounts |\n| `minds` | Mind scopes (each has a Mind Key) |\n| `memories` | Memory records with FTS5 virtual table |\n| `tasks` | Task management |\n| `chat_messages` | Chat history |\n| `scripts` | Persistent scripts |\n| `webhooks` | Webhook registrations |\n| `cron_jobs` | Scheduled tasks |\n| `variables` | Key-value store |\n| `audit_log` | Audit trail |\n\n**FTS5** enables sub-millisecond full-text search across all memory content.\nSee [FTS5 Search](/docs/concepts/fts5-search) for details.\n\n### 3. Embeddings Service (Optional)\n\nFor semantic search, Synapse generates vector embeddings of memory content.\nThese are stored alongside memories and used for similarity search.\n\n- Provider: configurable (OpenAI, local model, etc.)\n- Stored as `vector` column in `memories` table\n- Used by `/memory/semantic-search` endpoint\n- See [Semantic Search](/docs/concepts/semantic-search)\n\n### 4. MCP Server (Separate Service)\n\nThe Synapse MCP server runs as a separate process (port 13100). It:\n\n- Exposes 79 tools via Model Context Protocol\n- Supports stdio, HTTP/SSE, and WebSocket transports\n- Translates MCP tool calls into Synapse API calls\n- Multi-tenant: one Mind Key per session\n\n### 5. Browser Proxy (Separate Service)\n\nFor browser automation, a separate Playwright-based service runs on port 13000.\nThe MCP server can call this for `browser_*` tools.\n\n### 6. SSH Proxy (Separate Service)\n\nFor SSH-based remote commands, a separate service runs on port 12900.\n\n## Multi-Tenancy Model\n\n```\nUser Account (email + password)\n  │\n  ├── Mind 1 (Mind Key 1)\n  │   ├── Memories\n  │   ├── Tasks\n  │   ├── Chat\n  │   └── Scripts\n  │\n  ├── Mind 2 (Mind Key 2)\n  │   └── ... (isolated from Mind 1)\n  │\n  └── Mind 3 (Mind Key 3)\n      └── ...\n```\n\nEach mind is fully isolated. Mind Keys grant access to exactly one mind.\nJWTs grant access to the user account (for managing minds).\n\nSee [Multi-Tenancy](/docs/concepts/multi-tenancy) for details.\n\n## Request Flow\n\n### Memory Store Request\n\n```\n1. Client sends POST /memory with Authorization: Bearer mk_xxx\n2. Fastify receives request\n3. Auth middleware validates Mind Key, attaches mind_id to request\n4. Memory route handler:\n   a. Validate JSON body (zod schema)\n   b. Insert into memories table\n   c. Update FTS5 index\n   d. (Async) Generate embedding if embeddings enabled\n   e. Trigger webhooks for memory.store event\n5. Return { id, status: \"stored\" }\n```\n\n### Memory Search Request\n\n```\n1. Client sends GET /memory/search?q=docker\n2. Auth middleware validates Mind Key\n3. Memory route handler:\n   a. Parse query (FTS5 syntax)\n   b. Execute FTS5 MATCH against memories table\n   c. Filter by mind_id (tenant isolation)\n   d. Rank by relevance\n   e. Return results\n```\n\n## Deployment\n\nSynapse is deployed as a Docker container. See `docker-compose.yml`:\n\n```yaml\nservices:\n  synapse:\n    image: registry.gitlab.com/schaefer-services/synapse:latest\n    ports:\n      - \"12800:12800\"\n    environment:\n      - DATABASE_URL=postgres://...\n      - JWT_SECRET=...\n      - SYNAPSE_HOST=0.0.0.0\n      - SYNAPSE_PORT=12800\n    depends_on:\n      - postgres\n  \n  postgres:\n    image: postgres:16\n    volumes:\n      - pgdata:/var/lib/postgresql/data\n    environment:\n      - POSTGRES_DB=synapse\n      - POSTGRES_PASSWORD=...\n```\n\n## Performance Characteristics\n\n| Operation | Latency | Throughput |\n|-----------|---------|------------|\n| Memory store | ~5ms | 1000+ req/s |\n| Memory recall | ~20ms | 500 req/s |\n| FTS5 search | ~10ms | 800 req/s |\n| Semantic search | ~50ms | 200 req/s |\n| Chat poll | ~5ms | 2000 req/s |\n\n## Next Steps\n\n- [Memory Model](/docs/concepts/memory-model)\n- [Multi-Tenancy](/docs/concepts/multi-tenancy)\n- [FTS5 Search](/docs/concepts/fts5-search)\n","content_html":"<h1>Synapse Architecture</h1>\n<p>Synapse is a multi-tenant memory API built on Fastify, PostgreSQL with FTS5,\nand an optional embeddings service for semantic search.</p>\n<h2>High-Level Architecture</h2>\n<pre><code class=\"hljs language-plaintext\">┌──────────────────────────────────────────────────────────┐\n│                       Clients                            │\n├──────────────┬──────────────┬──────────────┬─────────────┤\n│  LLM Agents  │  Web Browsers│  MCP Clients │  Webhooks   │\n│  (curl/SDK)  │  (humans)    │ (Claude/etc) │  (external) │\n└──────┬───────┴──────┬───────┴──────┬───────┴──────┬──────┘\n       │              │              │              │\n       └──────────────┴──────────────┴──────────────┘\n                              │\n                              ▼\n              ┌───────────────────────────────┐\n              │    Synapse API (Fastify)      │\n              │    port 12800                 │\n              │    - REST endpoints           │\n              │    - Auth (Mind Key / JWT)    │\n              │    - Rate limiting            │\n              │    - Static file serving      │\n              └───────────────┬───────────────┘\n                              │\n              ┌───────────────┼───────────────┐\n              ▼               ▼               ▼\n       ┌─────────────┐ ┌─────────────┐ ┌─────────────┐\n       │ PostgreSQL  │ │ Embeddings  │ │ MCP Server  │\n       │ + FTS5      │ │ Service     │ │ (separate)  │\n       │             │ │ (optional)  │ │ port 13100  │\n       │ - memories  │ │             │ │             │\n       │ - tasks     │ │ - generate  │ │ - 79 tools  │\n       │ - chat      │ │   embeddings│ │ - stdio/SSE │\n       │ - scripts   │ │             │ │ - WebSocket │\n       └─────────────┘ └─────────────┘ └─────────────┘</code></pre><h2>Core Components</h2>\n<h3>1. Synapse API (Fastify)</h3>\n<p>The main HTTP server. Handles:</p>\n<ul>\n<li>REST endpoints (<code>/memory</code>, <code>/chat</code>, <code>/mind/tasks</code>, etc.)</li>\n<li>Authentication (Mind Key for agents, JWT for humans)</li>\n<li>Rate limiting (<code>?key=</code> auth only)</li>\n<li>Static file serving (web UI at <code>/human</code>, <code>/docs</code>, etc.)</li>\n<li>Webhook dispatch</li>\n</ul>\n<p>Built with <strong>Fastify 5</strong> for performance. Runs on port 12800 in production.</p>\n<h3>2. PostgreSQL with FTS5</h3>\n<p>The primary data store. Uses PostgreSQL with the FTS5 extension for\nfull-text search.</p>\n<p><strong>Key tables:</strong></p>\n<table>\n<thead>\n<tr>\n<th>Table</th>\n<th>Purpose</th>\n</tr>\n</thead>\n<tbody><tr>\n<td><code>users</code></td>\n<td>User accounts</td>\n</tr>\n<tr>\n<td><code>minds</code></td>\n<td>Mind scopes (each has a Mind Key)</td>\n</tr>\n<tr>\n<td><code>memories</code></td>\n<td>Memory records with FTS5 virtual table</td>\n</tr>\n<tr>\n<td><code>tasks</code></td>\n<td>Task management</td>\n</tr>\n<tr>\n<td><code>chat_messages</code></td>\n<td>Chat history</td>\n</tr>\n<tr>\n<td><code>scripts</code></td>\n<td>Persistent scripts</td>\n</tr>\n<tr>\n<td><code>webhooks</code></td>\n<td>Webhook registrations</td>\n</tr>\n<tr>\n<td><code>cron_jobs</code></td>\n<td>Scheduled tasks</td>\n</tr>\n<tr>\n<td><code>variables</code></td>\n<td>Key-value store</td>\n</tr>\n<tr>\n<td><code>audit_log</code></td>\n<td>Audit trail</td>\n</tr>\n</tbody></table>\n<p><strong>FTS5</strong> enables sub-millisecond full-text search across all memory content.\nSee <a href=\"/docs/concepts/fts5-search\">FTS5 Search</a> for details.</p>\n<h3>3. Embeddings Service (Optional)</h3>\n<p>For semantic search, Synapse generates vector embeddings of memory content.\nThese are stored alongside memories and used for similarity search.</p>\n<ul>\n<li>Provider: configurable (OpenAI, local model, etc.)</li>\n<li>Stored as <code>vector</code> column in <code>memories</code> table</li>\n<li>Used by <code>/memory/semantic-search</code> endpoint</li>\n<li>See <a href=\"/docs/concepts/semantic-search\">Semantic Search</a></li>\n</ul>\n<h3>4. MCP Server (Separate Service)</h3>\n<p>The Synapse MCP server runs as a separate process (port 13100). It:</p>\n<ul>\n<li>Exposes 79 tools via Model Context Protocol</li>\n<li>Supports stdio, HTTP/SSE, and WebSocket transports</li>\n<li>Translates MCP tool calls into Synapse API calls</li>\n<li>Multi-tenant: one Mind Key per session</li>\n</ul>\n<h3>5. Browser Proxy (Separate Service)</h3>\n<p>For browser automation, a separate Playwright-based service runs on port 13000.\nThe MCP server can call this for <code>browser_*</code> tools.</p>\n<h3>6. SSH Proxy (Separate Service)</h3>\n<p>For SSH-based remote commands, a separate service runs on port 12900.</p>\n<h2>Multi-Tenancy Model</h2>\n<pre><code class=\"hljs language-plaintext\">User Account (email + password)\n  │\n  ├── Mind 1 (Mind Key 1)\n  │   ├── Memories\n  │   ├── Tasks\n  │   ├── Chat\n  │   └── Scripts\n  │\n  ├── Mind 2 (Mind Key 2)\n  │   └── ... (isolated from Mind 1)\n  │\n  └── Mind 3 (Mind Key 3)\n      └── ...</code></pre><p>Each mind is fully isolated. Mind Keys grant access to exactly one mind.\nJWTs grant access to the user account (for managing minds).</p>\n<p>See <a href=\"/docs/concepts/multi-tenancy\">Multi-Tenancy</a> for details.</p>\n<h2>Request Flow</h2>\n<h3>Memory Store Request</h3>\n<pre><code class=\"hljs language-plaintext\">1. Client sends POST /memory with Authorization: Bearer mk_xxx\n2. Fastify receives request\n3. Auth middleware validates Mind Key, attaches mind_id to request\n4. Memory route handler:\n   a. Validate JSON body (zod schema)\n   b. Insert into memories table\n   c. Update FTS5 index\n   d. (Async) Generate embedding if embeddings enabled\n   e. Trigger webhooks for memory.store event\n5. Return { id, status: &quot;stored&quot; }</code></pre><h3>Memory Search Request</h3>\n<pre><code class=\"hljs language-plaintext\">1. Client sends GET /memory/search?q=docker\n2. Auth middleware validates Mind Key\n3. Memory route handler:\n   a. Parse query (FTS5 syntax)\n   b. Execute FTS5 MATCH against memories table\n   c. Filter by mind_id (tenant isolation)\n   d. Rank by relevance\n   e. Return results</code></pre><h2>Deployment</h2>\n<p>Synapse is deployed as a Docker container. See <code>docker-compose.yml</code>:</p>\n<pre><code class=\"hljs language-yaml\"><span class=\"hljs-attr\">services:</span>\n  <span class=\"hljs-attr\">synapse:</span>\n    <span class=\"hljs-attr\">image:</span> <span class=\"hljs-string\">registry.gitlab.com/schaefer-services/synapse:latest</span>\n    <span class=\"hljs-attr\">ports:</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">&quot;12800:12800&quot;</span>\n    <span class=\"hljs-attr\">environment:</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">DATABASE_URL=postgres://...</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">JWT_SECRET=...</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">SYNAPSE_HOST=0.0.0.0</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">SYNAPSE_PORT=12800</span>\n    <span class=\"hljs-attr\">depends_on:</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">postgres</span>\n  \n  <span class=\"hljs-attr\">postgres:</span>\n    <span class=\"hljs-attr\">image:</span> <span class=\"hljs-string\">postgres:16</span>\n    <span class=\"hljs-attr\">volumes:</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">pgdata:/var/lib/postgresql/data</span>\n    <span class=\"hljs-attr\">environment:</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">POSTGRES_DB=synapse</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">POSTGRES_PASSWORD=...</span></code></pre><h2>Performance Characteristics</h2>\n<table>\n<thead>\n<tr>\n<th>Operation</th>\n<th>Latency</th>\n<th>Throughput</th>\n</tr>\n</thead>\n<tbody><tr>\n<td>Memory store</td>\n<td>~5ms</td>\n<td>1000+ req/s</td>\n</tr>\n<tr>\n<td>Memory recall</td>\n<td>~20ms</td>\n<td>500 req/s</td>\n</tr>\n<tr>\n<td>FTS5 search</td>\n<td>~10ms</td>\n<td>800 req/s</td>\n</tr>\n<tr>\n<td>Semantic search</td>\n<td>~50ms</td>\n<td>200 req/s</td>\n</tr>\n<tr>\n<td>Chat poll</td>\n<td>~5ms</td>\n<td>2000 req/s</td>\n</tr>\n</tbody></table>\n<h2>Next Steps</h2>\n<ul>\n<li><a href=\"/docs/concepts/memory-model\">Memory Model</a></li>\n<li><a href=\"/docs/concepts/multi-tenancy\">Multi-Tenancy</a></li>\n<li><a href=\"/docs/concepts/fts5-search\">FTS5 Search</a></li>\n</ul>\n","urls":{"html":"/docs/concepts/architecture","text":"/docs/concepts/architecture?format=text","json":"/docs/concepts/architecture?format=json","llm":"/docs/concepts/architecture?format=llm"},"translations_available":["en","zh","hi","es","fr","ar","pt","ru","ja","de","it","ko","nl","pl","tr","sv","vi","th","id","uk"]}