Repository navigation
Architecture
guirguispierre edited this page Mar 30, 2026
·
1 revision
| Layer | Technology |
|---|---|
| Runtime | Cloudflare Workers |
| Database | Cloudflare D1 (SQLite) |
| Semantic Index | Cloudflare Vectorize |
| Embeddings | Cloudflare Workers AI (@cf/baai/bge-base-en-v1.5) |
| Rate Limiting | Cloudflare KV |
| Protocol | Model Context Protocol (MCP) over HTTP/SSE |
| Auth | OAuth 2.0 + PKCE, JWT, PBKDF2-SHA256 |
MCP Client (Claude, ChatGPT, etc.)
|
| POST /mcp (JSON-RPC 2.0)
| Authorization: Bearer <token>
v
Cloudflare Worker (memoryvault)
|
+-- D1 (SQLite) ---- memories, links, users, sessions
|
+-- Vectorize ------- semantic embeddings (768-dim)
|
+-- Workers AI ------ embedding generation
|
+-- KV -------------- rate limit counters
The codebase is split into 15 focused modules:
| Module | Lines | Purpose |
|---|---|---|
index.ts |
~210 | Worker entry point — fetch() handler, URL routing, CORS/security wrapping |
types.ts |
~200 | All shared TypeScript types and interfaces |
constants.ts |
~55 | Configuration values (TTLs, limits, model names, defaults) |
| Module | Lines | Purpose |
|---|---|---|
utils.ts |
~360 | Pure helpers: ID generation, cookies, parsing, validation |
crypto.ts |
~115 | PBKDF2 password hashing, JWT sign/verify, HMAC, base64url |
cors.ts |
~95 | CORS headers, origin validation, security headers, CSP |
| Module | Lines | Purpose |
|---|---|---|
db.ts |
~730 | D1 queries, schema migrations, memory/link/changelog CRUD |
vectorize.ts |
~475 | Embedding generation, vector sync, semantic search, RRF fusion |
scoring.ts |
~230 | Dynamic confidence/importance computation with explainable components |
| Module | Lines | Purpose |
|---|---|---|
auth.ts |
~470 | Session lifecycle, rate limiting, signup/login/refresh/logout handlers |
oauth.ts |
~910 | OAuth2 + PKCE: client registration, authorize, token exchange, metadata |
| Module | Lines | Purpose |
|---|---|---|
tools-schema.ts |
~910 | 40+ tool definitions (name, description, inputSchema), release metadata |
tools.ts |
~3,550 |
callTool() dispatcher with all tool handler implementations |
| Module | Lines | Purpose |
|---|---|---|
viewer.ts |
~4,090 | Web viewer HTML + JavaScript (themes, graph, command palette) |
routes.ts |
~1,300 | MCP handler, API endpoints, HTML landing pages, endpoint guides |
-
Entry:
index.tsreceives the request - OPTIONS: Preflight requests return CORS headers immediately
-
Schema:
ensureSchema()runs idempotent migrations (cached after first call) - Routing: URL path determines handler
-
Auth: Most routes call
authenticateRequest()— verifies JWT or legacy token - Handler: Route-specific handler processes the request
- CORS: Response is wrapped with CORS and (for HTML) security headers
Every data query is scoped by brain_id:
- Each user owns one or more brains
-
brain_membershipsmaps users to brains with roles - All memory, link, changelog, policy, and snapshot queries filter by brain_id
- Vectorize namespaces use brain_id for vector isolation
- Legacy bearer token maps to a shared
legacy-default-brain
Runtime (shipped to Cloudflare):
-
@modelcontextprotocol/sdk— MCP protocol types -
zod— schema validation
Dev only:
-
typescript— type checking -
wrangler— Cloudflare Workers CLI -
@cloudflare/workers-types— Worker API types
Reference
Concepts
Features