Architecture

System Overview

Local-first path:
  MCP Client → retrace-mcp (npm package, stdio) → SQLite (~/.retrace/journal.db)

Cloud path:
  MCP Client → HTTPS (/mcp) → Next.js app (packages/web) → Postgres

Components

MCP Server

A Model Context Protocol server that provides journal tools to AI agents (Copilot, Cursor, Claude, etc.). Runs locally on the user's machine.

Mode selection is automatic based on whether env vars are set.

API Server

A Next.js application serving both the REST API and the web UI. Handles:

Data Flow

  1. User configures MCP server in their editor
  2. AI agent calls MCP tools (e.g., log_entry)
  3. MCP server forwards request to API (cloud mode) or writes to SQLite (local mode)
  4. API authenticates via bearer token, writes to Postgres
  5. User can query via MCP tools or web dashboard

Authentication

Encryption Design

Sensitive entry fields (content, project, people, tags, refs, component) are encrypted at rest using per-user keys derived via HKDF from a server-side ENCRYPTION_KEY + user ID:

Database Schema

Key tables (auto-migrated via instrumentation.ts):

Technology Stack

ComponentTechnologyRationale
MCP ServerTypeScript + MCP SDKOfficial SDK, type-safe
Local DBSQLite (better-sqlite3)Zero config, fast, portable
API + WebNext.js (App Router)Full-stack React, SSR, API routes
Cloud DBPostgreSQLRelational, supports arrays, time-series
Authbcrypt + NextAuth.js + SHA-256 tokensSessions for web, bearer for API
EncryptionAES-256-GCM + HMAC blind indexesEncrypted at rest, searchable
Monoreponpm workspacesBuilt-in, no extra tooling