Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ PYMTHOUSE_ALLOW_INSECURE_HTTP=
# PYMTHOUSE_SIGNER_URL=https://signer.pymthouse.com
# Non-prod mint requires PYMTHOUSE_PUBLIC_CLIENT_ID=app_98575870d7ae33589a3f0660

# Neon Postgres for MCP asset recents (media URL + gateway_request_id).
# Apply schema with `pnpm db:migrate` — the app does not run DDL at request time.
# Not OpenMeter. Do not commit the password.
# DATABASE_URL=postgresql://USER:PASSWORD@HOST/neondb?sslmode=require
# MCP_ASSETS_DATABASE_URL=

# Invite-only Console HTML (/home, /usage, /keys, /calls, /settings).
# Empty = no gate. Comma-separated emails, matched case-insensitively.
# CONSOLE_EMAIL_ALLOWLIST=alice@livepeer.org,bob@studio.com
45 changes: 45 additions & 0 deletions db/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { neon, type NeonQueryFunction } from "@neondatabase/serverless";
import { drizzle, type NeonHttpDatabase } from "drizzle-orm/neon-http";
import * as schema from "./schema";

export type AssetDb = NeonHttpDatabase<typeof schema>;

let sqlClient: NeonQueryFunction<false, false> | null = null;
let dbClient: AssetDb | null = null;

export function databaseUrl(): string {
const raw =
process.env.MCP_ASSETS_DATABASE_URL?.trim() ||
process.env.DATABASE_URL?.trim();
if (!raw) {
throw new Error(
"DATABASE_URL (or MCP_ASSETS_DATABASE_URL) is required for MCP assets"
);
}
const url = new URL(raw);
url.searchParams.delete("channel_binding");
if (!url.searchParams.has("sslmode")) {
url.searchParams.set("sslmode", "require");
}
return url.toString();
}

export function assetStoreConfigured(): boolean {
return Boolean(
process.env.MCP_ASSETS_DATABASE_URL?.trim() ||
process.env.DATABASE_URL?.trim()
);
}

export function getAssetDb(): AssetDb {
if (!dbClient) {
sqlClient = neon(databaseUrl());
dbClient = drizzle(sqlClient, { schema });
}
return dbClient;
}

export function resetAssetDbForTests(): void {
sqlClient = null;
dbClient = null;
}
42 changes: 42 additions & 0 deletions db/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
index,
pgTable,
text,
timestamp,
uniqueIndex,
} from "drizzle-orm/pg-core";

/** MCP-produced media URLs, scoped per console/MCP principal (`eu_…`). */
export const mcpAssets = pgTable(
"mcp_assets",
{
id: text("id").primaryKey(),
principalId: text("principal_id").notNull(),
url: text("url").notNull(),
capability: text("capability").notNull(),
gatewayRequestId: text("gateway_request_id").notNull(),
providerRequestId: text("provider_request_id"),
createdAt: timestamp("created_at", { withTimezone: true, mode: "date" })
.notNull()
.defaultNow(),
},
(table) => [
uniqueIndex("mcp_assets_principal_job_url_idx").on(
table.principalId,
table.gatewayRequestId,
table.url
),
index("mcp_assets_principal_created_idx").on(
table.principalId,
table.createdAt
),
index("mcp_assets_principal_capability_idx").on(
table.principalId,
table.capability
),
index("mcp_assets_principal_gateway_idx").on(
table.principalId,
table.gatewayRequestId
),
]
);
13 changes: 13 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "drizzle-kit";

export default defineConfig({
schema: "./db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
dbCredentials: {
url:
process.env.MCP_ASSETS_DATABASE_URL?.trim() ||
process.env.DATABASE_URL?.trim() ||
"postgresql://unused:unused@localhost/unused",
},
});
14 changes: 14 additions & 0 deletions drizzle/0000_narrow_solo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS "mcp_assets" (
"id" text PRIMARY KEY NOT NULL,
"principal_id" text NOT NULL,
"url" text NOT NULL,
"capability" text NOT NULL,
"gateway_request_id" text NOT NULL,
"provider_request_id" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "mcp_assets_principal_job_url_idx" ON "mcp_assets" USING btree ("principal_id","gateway_request_id","url");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "mcp_assets_principal_created_idx" ON "mcp_assets" USING btree ("principal_id","created_at");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "mcp_assets_principal_capability_idx" ON "mcp_assets" USING btree ("principal_id","capability");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "mcp_assets_principal_gateway_idx" ON "mcp_assets" USING btree ("principal_id","gateway_request_id");
166 changes: 166 additions & 0 deletions drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"id": "b194958b-6d47-422b-a5ab-b0eec9a5d704",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.mcp_assets": {
"name": "mcp_assets",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"principal_id": {
"name": "principal_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": true
},
"capability": {
"name": "capability",
"type": "text",
"primaryKey": false,
"notNull": true
},
"gateway_request_id": {
"name": "gateway_request_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"provider_request_id": {
"name": "provider_request_id",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"mcp_assets_principal_job_url_idx": {
"name": "mcp_assets_principal_job_url_idx",
"columns": [
{
"expression": "principal_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "gateway_request_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "url",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"mcp_assets_principal_created_idx": {
"name": "mcp_assets_principal_created_idx",
"columns": [
{
"expression": "principal_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "created_at",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"mcp_assets_principal_capability_idx": {
"name": "mcp_assets_principal_capability_idx",
"columns": [
{
"expression": "principal_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "capability",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"mcp_assets_principal_gateway_idx": {
"name": "mcp_assets_principal_gateway_idx",
"columns": [
{
"expression": "principal_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "gateway_request_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
13 changes: 13 additions & 0 deletions drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1788576402765,
"tag": "0000_narrow_solo",
"breakpoints": true
}
]
}
Loading