Expand description
SQL schema definitions for the mcpr storage engine.
All table and index definitions live here as constants. The migration runner
in super::db executes these on first open and on version upgrades.
§Schema design decisions
- Two tables:
requests(one row per MCP request) andsessions(one row per MCP session). Client identity lives insessionsonly — no denormalization. - Soft foreign keys:
requests.session_idreferencessessions.session_idbut without a FOREIGN KEY constraint. SQLite FK enforcement requires per-connection pragmas and can cause constraint violations on ordering edge cases in async writes. - No body storage: Request/response bodies can be MB-scale. Only metadata is stored.
- Timestamps as unix milliseconds: Sufficient resolution, avoids i64 overflow, and is the natural unit for latency math.
- UUIDv7 for request_id: Time-ordered for efficient indexing, globally unique for cloud sink correlation.
Constants§
- CLOSE_
SESSION_ SQL - Mark a session as ended (clean transport close). Only updates if not already ended (idempotent).
- GET_
SCHEMA_ HASH_ SQL - Fetch the current schema_hash and payload for a given proxy+upstream+method. Used by the writer to detect changes before upserting. Parameters: ?1=proxy, ?2=upstream_url, ?3=method.
- INSERT_
REQUEST_ SQL - INSERT a new request row. All parameters are positional (?1 .. ?14).
- INSERT_
SCHEMA_ CHANGE_ SQL - Insert a schema change record into the append-only log. Parameters: ?1=proxy, ?2=upstream_url, ?3=method, ?4=change_type, ?5=item_name, ?6=old_hash, ?7=new_hash, ?8=detected_at.
- INSERT_
SESSION_ SQL - INSERT a new session row. Uses INSERT OR IGNORE because a reconnecting
client may re-send
initializewith the same session ID. - SCHEMA_
VERSION - Current schema version. Stored in the
metatable and checked on startup. Bump this when adding migrations. - UPDATE_
SESSION_ COUNTERS_ SQL - UPDATE session counters and last_seen_at. Executed in the same transaction as the request INSERT to keep counters consistent.
- UPSERT_
MCPR_ VERSION - SQL to insert or update the mcpr_version meta key on every startup.
- UPSERT_
SERVER_ SCHEMA_ SQL - UPSERT a server_schema row. ON CONFLICT updates the existing row. Parameters: ?1=proxy, ?2=upstream_url, ?3=method, ?4=payload, ?5=captured_at, ?6=schema_hash.
- V1_
META_ SEED - SQL to insert the initial meta rows after schema creation.
- V1_
SCHEMA - Initial schema: requests table, sessions table, meta table, and all indexes.
- V2_
SCHEMA - V1 → V2 migration: add server_schema and schema_changes tables.
- V3_
SCHEMA - V2 → V3 migration: add
proxycolumn to server_schema and schema_changes. - V4_
SCHEMA - V3 → V4 migration: rename
latency_ms→latency_usand convert existing values from milliseconds to microseconds for sub-ms precision. - V5_
SCHEMA - V4 → V5 migration: add
resource_uriandprompt_namecolumns to therequeststable. Captured by the proxy’sTargetExtractMiddleware: resources/{read,subscribe,unsubscribe} → params.uri → resource_uri prompts/get → params.name → prompt_name