Architecture Overview
Technology Stack
- Next.js 15 — App Router, Server Components, Server Actions
- Supabase — PostgreSQL database with built-in auth and RLS
- @supabase/ssr — SSR-compatible authentication client
- TypeScript — Full type safety across the stack
- Zod v4 — Schema validation for all inputs
Server Actions Pattern
PromptRepo uses Server Actions instead of traditional REST API routes for all mutations. Server Actions run on the server, provide automatic CSRF protection, and integrate seamlessly with React Server Components.src/features/prompts/actions/save-prompt.ts
- Declared with
'use server'directive - Run on the server only (never exposed to the client)
- Automatically handle authentication via cookies
- Return serializable data structures
Database Access Patterns
PromptRepo uses three distinct Supabase client factories depending on the execution context:| Client | Usage | Authentication |
|---|---|---|
createClient() (server) | Server Components, Server Actions | Session cookies (read/write) |
createMiddlewareClient() | Middleware only | Session cookies (can refresh) |
createBrowserClient() | Client Components | Session cookies (browser) |
createPublicClient() | Public pages (no auth) | Anonymous anon key |
createServiceClient() | Admin operations | Service role key (bypasses RLS) |
Server Component Example
src/app/page.tsx
Client Component Example
src/components/features/example-client.tsx
Row Level Security (RLS)
All database tables enforce Row Level Security policies. Even with direct database access, users can only read/write their own data.Example: Prompts Table Policies
supabase/migrations/20260208000001_prompt_schema.sql
- Direct SQL queries
- Compromised application code
- API key misuse
- Server Action bypasses
Data Model
PromptRepo uses a two-table versioning system:prompts— HEAD state (mutable metadata: title, description, is_public)prompt_versions— Immutable history (version_number, content, version_note)
prompt_versions row and updates the HEAD pointer atomically.
Feature Modules
PromptRepo organizes domain logic into feature modules undersrc/features/:
- actions/ — Server Actions for mutations
- queries/ — Data fetching functions
- types/ — TypeScript types
- components/ — React components
API Endpoints
PromptRepo exposes one HTTP API endpoint for external integrations:MCP Endpoint
POST /api/mcp — JSON-RPC 2.0 Model Context Protocol server
- Authenticates via API keys (not session cookies)
- Allows AI agents (Claude Desktop, Claude Code, etc.) to read and resolve prompts
- Returns public prompts for anonymous requests
- Full documentation: MCP Overview
Middleware
The Next.js middleware (src/middleware.ts) handles:
- Session refresh — Updates Supabase auth tokens on every request
- Route protection — Redirects unauthenticated users to
/auth/login - Public route exceptions — Allows
/auth/*,/p/*(public sharing), and/api/mcpwithout auth
src/middleware.ts
updateSession function (in src/lib/supabase/middleware.ts):
- Creates a middleware-aware Supabase client
- Checks for a valid user session
- Redirects to login if the user is not authenticated and the route is protected
- Allows the request to proceed for authenticated users or public routes
Next Steps
- Authentication — Learn about session-based and API key authentication
- Database Schema — Explore the database structure
- Server Actions — Prompt management actions
- MCP Overview — Model Context Protocol integration