{"title":"Architettura di Synapse","slug":"architecture","category":"concepts","summary":"Come è costruito Synapse — Fastify, PostgreSQL, FTS5, embedding, server MCP.","audience":["human","llm"],"tags":["architecture","design","components"],"difficulty":"intermediate","updated":"2026-06-27","word_count":462,"read_minutes":2,"lang":"it","translated":true,"requested_lang":"it","content_markdown":"\n# Architettura di Synapse\n\nSynapse è una API di memoria multi-tenant costruita su Fastify, PostgreSQL con\nFTS5 e un servizio embedding opzionale per la ricerca semantica.\n\n## Architettura di alto livello\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## Componenti principali\n\n### 1. Synapse API (Fastify)\n\nIl server HTTP principale. Gestisce:\n\n- Endpoint REST (`/memory`, `/chat`, `/mind/tasks`, ecc.)\n- Autenticazione (Mind Key per agenti, JWT per umani)\n- Rate limiting (solo auth `?key=`)\n- Servizio di file statici (UI web su `/human`, `/docs`, ecc.)\n- Dispatch dei webhook\n\nCostruito con **Fastify 5** per le prestazioni. In produzione gira sulla porta\n12800.\n\n### 2. PostgreSQL con FTS5\n\nLo store dati principale. Usa PostgreSQL con l'estensione FTS5 per la ricerca\nfull-text.\n\n**Tabelle chiave:**\n\n| Tabella | Scopo |\n|-------|---------|\n| `users` | Account utente |\n| `minds` | Scope delle menti (ciascuno ha una Mind Key) |\n| `memories` | Record delle memorie con tabella virtuale FTS5 |\n| `tasks` | Gestione attività |\n| `chat_messages` | Cronologia chat |\n| `scripts` | Script persistenti |\n| `webhooks` | Registrazioni webhook |\n| `cron_jobs` | Attività pianificate |\n| `variables` | Key-value store |\n| `audit_log` | Traccia di audit |\n\n**FTS5** abilita la ricerca full-text sub-millisecond su tutti i contenuti\ndelle memorie. Veda [Ricerca FTS5](/docs/concepts/fts5-search) per i dettagli.\n\n### 3. Servizio Embedding (opzionale)\n\nPer la ricerca semantica, Synapse genera embedding vettoriali del contenuto\ndelle memorie. Vengono salvati insieme alle memorie e usati per la ricerca di\nsimilarità.\n\n- Provider: configurabile (OpenAI, modello locale, ecc.)\n- Salvato come colonna `vector` nella tabella `memories`\n- Usato dall'endpoint `/memory/semantic-search`\n- Veda [Ricerca semantica](/docs/concepts/semantic-search)\n\n### 4. Server MCP (servizio separato)\n\nIl server MCP di Synapse gira come processo separato (porta 13100). Esso:\n\n- Espone 79 strumenti tramite Model Context Protocol\n- Supporta transport stdio, HTTP/SSE e WebSocket\n- Traduce le chiamate MCP in chiamate API Synapse\n- Multi-tenant: una Mind Key per sessione\n\n### 5. Browser Proxy (servizio separato)\n\nPer l'automazione del browser, un servizio separato basato su Playwright gira\nsulla porta 13000. Il server MCP può chiamarlo per gli strumenti `browser_*`.\n\n### 6. SSH Proxy (servizio separato)\n\nPer comandi remoti via SSH, un servizio separato gira sulla porta 12900.\n\n## Modello multi-tenant\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\nOgni mente è completamente isolata. Le Mind Key concedono accesso a esattamente\nuna mente. I JWT concedono accesso all'account utente (per gestire le menti).\n\nVeda [Multi-tenancy](/docs/concepts/multi-tenancy) per i dettagli.\n\n## Flusso delle richieste\n\n### Richiesta di memorizzazione\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### Richiesta di ricerca\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 viene deployato come container Docker. Veda `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## Caratteristiche di prestazione\n\n| Operazione | Latenza | 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## Prossimi passi\n\n- [Modello di memoria](/docs/concepts/memory-model)\n- [Multi-tenancy](/docs/concepts/multi-tenancy)\n- [Ricerca FTS5](/docs/concepts/fts5-search)\n","content_html":"<h1>Architettura di Synapse</h1>\n<p>Synapse è una API di memoria multi-tenant costruita su Fastify, PostgreSQL con\nFTS5 e un servizio embedding opzionale per la ricerca semantica.</p>\n<h2>Architettura di alto livello</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>Componenti principali</h2>\n<h3>1. Synapse API (Fastify)</h3>\n<p>Il server HTTP principale. Gestisce:</p>\n<ul>\n<li>Endpoint REST (<code>/memory</code>, <code>/chat</code>, <code>/mind/tasks</code>, ecc.)</li>\n<li>Autenticazione (Mind Key per agenti, JWT per umani)</li>\n<li>Rate limiting (solo auth <code>?key=</code>)</li>\n<li>Servizio di file statici (UI web su <code>/human</code>, <code>/docs</code>, ecc.)</li>\n<li>Dispatch dei webhook</li>\n</ul>\n<p>Costruito con <strong>Fastify 5</strong> per le prestazioni. In produzione gira sulla porta\n12800.</p>\n<h3>2. PostgreSQL con FTS5</h3>\n<p>Lo store dati principale. Usa PostgreSQL con l&#39;estensione FTS5 per la ricerca\nfull-text.</p>\n<p><strong>Tabelle chiave:</strong></p>\n<table>\n<thead>\n<tr>\n<th>Tabella</th>\n<th>Scopo</th>\n</tr>\n</thead>\n<tbody><tr>\n<td><code>users</code></td>\n<td>Account utente</td>\n</tr>\n<tr>\n<td><code>minds</code></td>\n<td>Scope delle menti (ciascuno ha una Mind Key)</td>\n</tr>\n<tr>\n<td><code>memories</code></td>\n<td>Record delle memorie con tabella virtuale FTS5</td>\n</tr>\n<tr>\n<td><code>tasks</code></td>\n<td>Gestione attività</td>\n</tr>\n<tr>\n<td><code>chat_messages</code></td>\n<td>Cronologia chat</td>\n</tr>\n<tr>\n<td><code>scripts</code></td>\n<td>Script persistenti</td>\n</tr>\n<tr>\n<td><code>webhooks</code></td>\n<td>Registrazioni webhook</td>\n</tr>\n<tr>\n<td><code>cron_jobs</code></td>\n<td>Attività pianificate</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>Traccia di audit</td>\n</tr>\n</tbody></table>\n<p><strong>FTS5</strong> abilita la ricerca full-text sub-millisecond su tutti i contenuti\ndelle memorie. Veda <a href=\"/docs/concepts/fts5-search\">Ricerca FTS5</a> per i dettagli.</p>\n<h3>3. Servizio Embedding (opzionale)</h3>\n<p>Per la ricerca semantica, Synapse genera embedding vettoriali del contenuto\ndelle memorie. Vengono salvati insieme alle memorie e usati per la ricerca di\nsimilarità.</p>\n<ul>\n<li>Provider: configurabile (OpenAI, modello locale, ecc.)</li>\n<li>Salvato come colonna <code>vector</code> nella tabella <code>memories</code></li>\n<li>Usato dall&#39;endpoint <code>/memory/semantic-search</code></li>\n<li>Veda <a href=\"/docs/concepts/semantic-search\">Ricerca semantica</a></li>\n</ul>\n<h3>4. Server MCP (servizio separato)</h3>\n<p>Il server MCP di Synapse gira come processo separato (porta 13100). Esso:</p>\n<ul>\n<li>Espone 79 strumenti tramite Model Context Protocol</li>\n<li>Supporta transport stdio, HTTP/SSE e WebSocket</li>\n<li>Traduce le chiamate MCP in chiamate API Synapse</li>\n<li>Multi-tenant: una Mind Key per sessione</li>\n</ul>\n<h3>5. Browser Proxy (servizio separato)</h3>\n<p>Per l&#39;automazione del browser, un servizio separato basato su Playwright gira\nsulla porta 13000. Il server MCP può chiamarlo per gli strumenti <code>browser_*</code>.</p>\n<h3>6. SSH Proxy (servizio separato)</h3>\n<p>Per comandi remoti via SSH, un servizio separato gira sulla porta 12900.</p>\n<h2>Modello multi-tenant</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>Ogni mente è completamente isolata. Le Mind Key concedono accesso a esattamente\nuna mente. I JWT concedono accesso all&#39;account utente (per gestire le menti).</p>\n<p>Veda <a href=\"/docs/concepts/multi-tenancy\">Multi-tenancy</a> per i dettagli.</p>\n<h2>Flusso delle richieste</h2>\n<h3>Richiesta di memorizzazione</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>Richiesta di ricerca</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 viene deployato come container Docker. Veda <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>Caratteristiche di prestazione</h2>\n<table>\n<thead>\n<tr>\n<th>Operazione</th>\n<th>Latenza</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>Prossimi passi</h2>\n<ul>\n<li><a href=\"/docs/concepts/memory-model\">Modello di memoria</a></li>\n<li><a href=\"/docs/concepts/multi-tenancy\">Multi-tenancy</a></li>\n<li><a href=\"/docs/concepts/fts5-search\">Ricerca FTS5</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"]}