@limetry/sql 1.2.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # `@limetry/sql`
2
+
3
+ > [!NOTE]
4
+ > **Origin:** A Cursor user asked Claude to clean up test data in their Supabase project. The
5
+ > generated `DELETE` matched far more rows than intended because the agent held `DATABASE_URL`
6
+ > directly. `@limetry/sql` owns the connection string instead: it classifies each statement,
7
+ > evaluates an ActionIntent, and only executes what the policy allows.
8
+
9
+ Postgres / Supabase write gate that **owns** `DATABASE_URL` and evaluates SQL intents with Limetry
10
+ before execute. Agents never receive write credentials from this package.
11
+
12
+ There is no Supabase Marketplace app. Point `DATABASE_URL` (or `SUPABASE_DB_URL`) at any Postgres,
13
+ including a Supabase project, then evaluate `sql.read` / `sql.write` / `sql.ddl` before the
14
+ driver runs.
15
+
16
+ ## Live demo in this repo
17
+
18
+ [`.github/workflows/limetry-sql-gate.yml`](../../.github/workflows/limetry-sql-gate.yml)
19
+ starts Postgres 16 plus a Limetry node, applies [`policies/postgres.json`](policies/postgres.json),
20
+ then:
21
+
22
+ 1. Evaluates `SELECT 1 AS ok` — must **allow** and **execute** on the job's Postgres.
23
+ 2. Evaluates `DROP TABLE customers` — must **deny** (`sql.ddl`) before Postgres is touched.
24
+ 3. Evaluates `DELETE FROM customers` — must **deny** (`sql.write`) before Postgres is touched.
25
+
26
+ Customers copy that policy and run the same gate in-process or as MCP against their own
27
+ Supabase connection string.
28
+
29
+ ## Install
30
+
31
+ ```bash
32
+ yarn add @limetry/sql @limetry/sdk @modelcontextprotocol/sdk pg
33
+ ```
34
+
35
+ ## MCP
36
+
37
+ ```json
38
+ {
39
+ "mcpServers": {
40
+ "limetry-sql": {
41
+ "command": "npx",
42
+ "args": ["-y", "@limetry/sql"],
43
+ "env": {
44
+ "DATABASE_URL": "postgresql://...",
45
+ "LIMETRY_API_KEY": "...",
46
+ "LIMETRY_POLICY_ID": "...",
47
+ "LIMETRY_BASE_URL": "http://localhost:3810",
48
+ "LIMETRY_SQL_DRY_RUN": "true"
49
+ }
50
+ }
51
+ }
52
+ }
53
+ ```
54
+
55
+ Tools:
56
+
57
+ - `limetry_sql_query` — classify → evaluate → optional execute
58
+ - `limetry_sql_list_pending` — statements that returned `approval_required`
59
+
60
+ Classification is a conservative keyword heuristic (`read` / `write` / `ddl` / `unknown`).
61
+ Statements that do not classify cleanly map to `sql.write`, so the failure mode is
62
+ over-gating rather than an unguarded write. Postgres-compatible connection strings only.
63
+
64
+ > [!WARNING]
65
+ > `LIMETRY_SQL_DRY_RUN` defaults to dry-run (on unless explicitly `"false"`). Set it to
66
+ > `"false"` to let allowed reads actually execute.
67
+
68
+ ## Apply the repo policy on your evaluation server
69
+
70
+ ```bash
71
+ limetry setup
72
+ limetry policy apply --file packages/sql/policies/postgres.json
73
+ ```
74
+
75
+ `policies/postgres.json` is read-only by design: `sql.write` and `sql.ddl` are denied
76
+ outright, with no approval path. To use `limetry_sql_list_pending`, add those types to
77
+ `require_approval_action_types` in your own policy.
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Conservative SQL statement classification heuristics for action gating.
3
+ */
4
+ /**
5
+ * Coarse SQL statement class used to select a Limetry `action_type`.
6
+ */
7
+ export type SqlClass = "read" | "write" | "ddl" | "unknown";
8
+ /**
9
+ * Classify a SQL statement with a conservative keyword heuristic.
10
+ * Not a full parser — treat unknown as write-gated.
11
+ *
12
+ * Strips block and line comments, then inspects the first token.
13
+ *
14
+ * @param sql - Raw SQL text to classify.
15
+ * @returns `"read"`, `"write"`, `"ddl"`, or `"unknown"` (empty / unrecognized).
16
+ */
17
+ export declare function classifySql(sql: string): SqlClass;
18
+ /**
19
+ * Map a SQL class onto Limetry action_type values.
20
+ *
21
+ * Unknown classes map to `sql.write` so they remain gated.
22
+ *
23
+ * @param sqlClass - Classification from {@link classifySql}.
24
+ * @returns `sql.read`, `sql.ddl`, or `sql.write`.
25
+ */
26
+ export declare function actionTypeForSqlClass(sqlClass: SqlClass): string;
27
+ //# sourceMappingURL=classify-sql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classify-sql.d.ts","sourceRoot":"","sources":["../src/classify-sql.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,GAAG,SAAS,CAAA;AAmC3D;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAsBjD;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAWhE"}
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Conservative SQL statement classification heuristics for action gating.
3
+ */
4
+ /**
5
+ * Leading tokens classified as read queries.
6
+ */
7
+ const READ_PREFIXES = ["select", "with", "show", "explain", "values"];
8
+ /**
9
+ * Leading tokens classified as DDL (schema / privilege) statements.
10
+ */
11
+ const DDL_PREFIXES = [
12
+ "create",
13
+ "alter",
14
+ "drop",
15
+ "truncate",
16
+ "rename",
17
+ "comment",
18
+ "grant",
19
+ "revoke",
20
+ ];
21
+ /**
22
+ * Leading tokens classified as write / DML statements.
23
+ */
24
+ const WRITE_PREFIXES = [
25
+ "insert",
26
+ "update",
27
+ "delete",
28
+ "merge",
29
+ "upsert",
30
+ "copy",
31
+ "call",
32
+ "do",
33
+ ];
34
+ /**
35
+ * Classify a SQL statement with a conservative keyword heuristic.
36
+ * Not a full parser — treat unknown as write-gated.
37
+ *
38
+ * Strips block and line comments, then inspects the first token.
39
+ *
40
+ * @param sql - Raw SQL text to classify.
41
+ * @returns `"read"`, `"write"`, `"ddl"`, or `"unknown"` (empty / unrecognized).
42
+ */
43
+ export function classifySql(sql) {
44
+ const normalized = sql
45
+ .replace(/\/\*[\s\S]*?\*\//g, " ")
46
+ .replace(/--[^\n]*/g, " ")
47
+ .trim()
48
+ .toLowerCase();
49
+ if (!normalized) {
50
+ return "unknown";
51
+ }
52
+ const firstToken = normalized.split(/\s+/)[0] ?? "";
53
+ if (READ_PREFIXES.includes(firstToken)) {
54
+ return "read";
55
+ }
56
+ if (DDL_PREFIXES.includes(firstToken)) {
57
+ return "ddl";
58
+ }
59
+ if (WRITE_PREFIXES.includes(firstToken)) {
60
+ return "write";
61
+ }
62
+ return "unknown";
63
+ }
64
+ /**
65
+ * Map a SQL class onto Limetry action_type values.
66
+ *
67
+ * Unknown classes map to `sql.write` so they remain gated.
68
+ *
69
+ * @param sqlClass - Classification from {@link classifySql}.
70
+ * @returns `sql.read`, `sql.ddl`, or `sql.write`.
71
+ */
72
+ export function actionTypeForSqlClass(sqlClass) {
73
+ if (sqlClass === "read") {
74
+ return "sql.read";
75
+ }
76
+ if (sqlClass === "ddl") {
77
+ return "sql.ddl";
78
+ }
79
+ if (sqlClass === "write") {
80
+ return "sql.write";
81
+ }
82
+ return "sql.write";
83
+ }
84
+ //# sourceMappingURL=classify-sql.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classify-sql.js","sourceRoot":"","sources":["../src/classify-sql.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH;;GAEG;AACH,MAAM,aAAa,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAA;AAErE;;GAEG;AACH,MAAM,YAAY,GAAG;IACnB,QAAQ;IACR,OAAO;IACP,MAAM;IACN,UAAU;IACV,QAAQ;IACR,SAAS;IACT,OAAO;IACP,QAAQ;CACT,CAAA;AAED;;GAEG;AACH,MAAM,cAAc,GAAG;IACrB,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,MAAM;IACN,MAAM;IACN,IAAI;CACL,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,MAAM,UAAU,GAAG,GAAG;SACnB,OAAO,CAAC,mBAAmB,EAAE,GAAG,CAAC;SACjC,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC;SACzB,IAAI,EAAE;SACN,WAAW,EAAE,CAAA;IAEhB,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;IACnD,IAAI,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;QACvC,OAAO,MAAM,CAAA;IACf,CAAC;IACD,IAAI,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;QACtC,OAAO,KAAK,CAAA;IACd,CAAC;IACD,IAAI,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;QACxC,OAAO,OAAO,CAAA;IAChB,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAkB;IACtD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,OAAO,UAAU,CAAA;IACnB,CAAC;IACD,IAAI,QAAQ,KAAK,KAAK,EAAE,CAAC;QACvB,OAAO,SAAS,CAAA;IAClB,CAAC;IACD,IAAI,QAAQ,KAAK,OAAO,EAAE,CAAC;QACzB,OAAO,WAAW,CAAA;IACpB,CAAC;IACD,OAAO,WAAW,CAAA;AACpB,CAAC"}
package/dist/gate.d.ts ADDED
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Postgres SQL action gate that evaluates intents before optional execution.
3
+ */
4
+ import { type ActionEvaluationResponse, type ActionIntent } from "@limetry/sdk";
5
+ import { type SqlClass } from "./classify-sql.js";
6
+ /**
7
+ * Successful ActionEvaluationResponse (`ok: true`) from `\@limetry/sdk`.
8
+ */
9
+ type OkActionEvaluation = Extract<ActionEvaluationResponse, {
10
+ ok: true;
11
+ }>;
12
+ /**
13
+ * Construction options for {@link SqlActionGate}.
14
+ */
15
+ export type SqlGateOptions = {
16
+ /**
17
+ * Postgres connection string; never returned to callers.
18
+ */
19
+ connectionString: string;
20
+ /**
21
+ * Limetry API key / bearer token.
22
+ */
23
+ apiKey: string;
24
+ /**
25
+ * Limetry API base URL.
26
+ */
27
+ baseUrl?: string;
28
+ /**
29
+ * Tenant id for multi-tenant evaluation.
30
+ */
31
+ tenantId?: string;
32
+ /**
33
+ * ActionPolicy id used for every SQL intent.
34
+ */
35
+ policyId: string;
36
+ /**
37
+ * Agent id recorded on intents; defaults to `sql_agent`.
38
+ */
39
+ agentId?: string;
40
+ /**
41
+ * Resource label on the ActionIntent; defaults to `postgres://*`.
42
+ */
43
+ resourceLabel?: string;
44
+ /**
45
+ * Default dry-run mode; defaults to `true`.
46
+ */
47
+ dryRun?: boolean;
48
+ /**
49
+ * Optional fetch implementation for tests.
50
+ */
51
+ fetch?: typeof globalThis.fetch;
52
+ /**
53
+ * Optional query runner that replaces the default `pg.Client` path.
54
+ */
55
+ queryRunner?: (sql: string) => Promise<unknown>;
56
+ };
57
+ /**
58
+ * Result of classify + evaluate + optional execute.
59
+ */
60
+ export type SqlGateResult = {
61
+ /**
62
+ * Heuristic SQL class for the statement.
63
+ */
64
+ sqlClass: SqlClass;
65
+ /**
66
+ * ActionIntent submitted for evaluation.
67
+ */
68
+ intent: ActionIntent;
69
+ /**
70
+ * Successful remote evaluation response.
71
+ */
72
+ evaluation: OkActionEvaluation;
73
+ /**
74
+ * Whether this call ran in dry-run mode.
75
+ */
76
+ dryRun: boolean;
77
+ /**
78
+ * `true` only when `execute` was requested, dry-run was false, and decision was `allow`.
79
+ */
80
+ executed: boolean;
81
+ /**
82
+ * Query result payload when `executed` is true.
83
+ */
84
+ rows?: unknown;
85
+ };
86
+ /**
87
+ * Owns Postgres credentials and evaluates SQL intents before optional execute.
88
+ *
89
+ * Execution requires `execute: true`, `dryRun: false`, and `decision === "allow"`.
90
+ * `approval_required` decisions are tracked via {@link SqlActionGate.listPending}.
91
+ */
92
+ export declare class SqlActionGate {
93
+ private readonly connectionString;
94
+ private readonly engine;
95
+ private readonly policyId;
96
+ private readonly agentId;
97
+ private readonly resourceLabel;
98
+ private readonly dryRunDefault;
99
+ private readonly queryRunner?;
100
+ private readonly pendingApprovals;
101
+ /**
102
+ * @param options - DB connection, Limetry API settings, and dry-run default.
103
+ */
104
+ constructor(options: SqlGateOptions);
105
+ /**
106
+ * Return a copy of in-process statements that received `approval_required`.
107
+ *
108
+ * @returns Pending approval entries with sql class and full SQL text.
109
+ */
110
+ listPending(): Array<{
111
+ approval_id: string;
112
+ decision_id: string;
113
+ sql: string;
114
+ sqlClass: SqlClass;
115
+ }>;
116
+ /**
117
+ * Classify, evaluate, and optionally execute a SQL statement.
118
+ *
119
+ * Metadata includes `sql_class`, a 200-character `sql_preview`, and `dry_run`.
120
+ *
121
+ * @param input - SQL text plus optional dry-run / execute overrides.
122
+ * @returns Classification, evaluation, and optional row payload.
123
+ * @throws Error When Limetry evaluation returns `ok: false`.
124
+ */
125
+ evaluateAndMaybeExecute(input: {
126
+ sql: string;
127
+ dryRun?: boolean;
128
+ execute?: boolean;
129
+ }): Promise<SqlGateResult>;
130
+ /**
131
+ * Execute SQL via the injected query runner or a short-lived `pg.Client`.
132
+ *
133
+ * @param sql - Statement to run.
134
+ * @returns Query runner result, or `{ rowCount, rows, fields }` from `pg`.
135
+ */
136
+ private runQuery;
137
+ }
138
+ export {};
139
+ //# sourceMappingURL=gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../src/gate.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EACL,KAAK,wBAAwB,EAC7B,KAAK,YAAY,EAElB,MAAM,cAAc,CAAA;AAGrB,OAAO,EAAsC,KAAK,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAErF;;GAEG;AACH,KAAK,kBAAkB,GAAG,OAAO,CAAC,wBAAwB,EAAE;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AC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAmC;IAChE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAK1B;IAEP;;OAEG;gBACS,OAAO,EAAE,cAAc;IAenC;;;;OAIG;IACH,WAAW,IAAI,KAAK,CAAC;QACnB,WAAW,EAAE,MAAM,CAAA;QACnB,WAAW,EAAE,MAAM,CAAA;QACnB,GAAG,EAAE,MAAM,CAAA;QACX,QAAQ,EAAE,QAAQ,CAAA;KACnB,CAAC;IAIF;;;;;;;;OAQG;IACG,uBAAuB,CAAC,KAAK,EAAE;QACnC,GAAG,EAAE,MAAM,CAAA;QACX,MAAM,CAAC,EAAE,OAAO,CAAA;QAChB,OAAO,CAAC,EAAE,OAAO,CAAA;KAClB,GAAG,OAAO,CAAC,aAAa,CAAC;IAwD1B;;;;;OAKG;YACW,QAAQ;CAiBvB"}
package/dist/gate.js ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Postgres SQL action gate that evaluates intents before optional execution.
3
+ */
4
+ import { randomUUID } from "node:crypto";
5
+ import { RemotePolicyEngine, } from "@limetry/sdk";
6
+ import pg from "pg";
7
+ import { actionTypeForSqlClass, classifySql } from "./classify-sql.js";
8
+ /**
9
+ * Owns Postgres credentials and evaluates SQL intents before optional execute.
10
+ *
11
+ * Execution requires `execute: true`, `dryRun: false`, and `decision === "allow"`.
12
+ * `approval_required` decisions are tracked via {@link SqlActionGate.listPending}.
13
+ */
14
+ export class SqlActionGate {
15
+ connectionString;
16
+ engine;
17
+ policyId;
18
+ agentId;
19
+ resourceLabel;
20
+ dryRunDefault;
21
+ queryRunner;
22
+ pendingApprovals = [];
23
+ /**
24
+ * @param options - DB connection, Limetry API settings, and dry-run default.
25
+ */
26
+ constructor(options) {
27
+ this.connectionString = options.connectionString;
28
+ this.policyId = options.policyId;
29
+ this.agentId = options.agentId ?? "sql_agent";
30
+ this.resourceLabel = options.resourceLabel ?? "postgres://*";
31
+ this.dryRunDefault = options.dryRun ?? true;
32
+ this.queryRunner = options.queryRunner;
33
+ this.engine = new RemotePolicyEngine({
34
+ apiKey: options.apiKey,
35
+ baseUrl: options.baseUrl,
36
+ tenantId: options.tenantId,
37
+ fetch: options.fetch,
38
+ });
39
+ }
40
+ /**
41
+ * Return a copy of in-process statements that received `approval_required`.
42
+ *
43
+ * @returns Pending approval entries with sql class and full SQL text.
44
+ */
45
+ listPending() {
46
+ return [...this.pendingApprovals];
47
+ }
48
+ /**
49
+ * Classify, evaluate, and optionally execute a SQL statement.
50
+ *
51
+ * Metadata includes `sql_class`, a 200-character `sql_preview`, and `dry_run`.
52
+ *
53
+ * @param input - SQL text plus optional dry-run / execute overrides.
54
+ * @returns Classification, evaluation, and optional row payload.
55
+ * @throws Error When Limetry evaluation returns `ok: false`.
56
+ */
57
+ async evaluateAndMaybeExecute(input) {
58
+ const sqlClass = classifySql(input.sql);
59
+ const dryRun = input.dryRun ?? this.dryRunDefault;
60
+ const intent = {
61
+ intent_id: randomUUID(),
62
+ policy_id: this.policyId,
63
+ agent_id: this.agentId,
64
+ action_type: actionTypeForSqlClass(sqlClass),
65
+ resource: this.resourceLabel,
66
+ metadata: {
67
+ sql_class: sqlClass,
68
+ sql_preview: input.sql.slice(0, 200),
69
+ dry_run: String(dryRun),
70
+ },
71
+ issued_at: new Date().toISOString(),
72
+ };
73
+ const evaluation = await this.engine.evaluateAction(intent);
74
+ if (!evaluation.ok) {
75
+ throw new Error(evaluation.error ?? "Limetry evaluate failed");
76
+ }
77
+ if (evaluation.decision === "approval_required" && evaluation.approval_id) {
78
+ this.pendingApprovals.push({
79
+ approval_id: evaluation.approval_id,
80
+ decision_id: evaluation.decision_id ?? "",
81
+ sql: input.sql,
82
+ sqlClass,
83
+ });
84
+ }
85
+ const shouldExecute = Boolean(input.execute) &&
86
+ !dryRun &&
87
+ evaluation.decision === "allow";
88
+ if (!shouldExecute) {
89
+ return {
90
+ sqlClass,
91
+ intent,
92
+ evaluation,
93
+ dryRun,
94
+ executed: false,
95
+ };
96
+ }
97
+ const rows = await this.runQuery(input.sql);
98
+ return {
99
+ sqlClass,
100
+ intent,
101
+ evaluation,
102
+ dryRun,
103
+ executed: true,
104
+ rows,
105
+ };
106
+ }
107
+ /**
108
+ * Execute SQL via the injected query runner or a short-lived `pg.Client`.
109
+ *
110
+ * @param sql - Statement to run.
111
+ * @returns Query runner result, or `{ rowCount, rows, fields }` from `pg`.
112
+ */
113
+ async runQuery(sql) {
114
+ if (this.queryRunner) {
115
+ return this.queryRunner(sql);
116
+ }
117
+ const client = new pg.Client({ connectionString: this.connectionString });
118
+ await client.connect();
119
+ try {
120
+ const result = await client.query(sql);
121
+ return {
122
+ rowCount: result.rowCount,
123
+ rows: result.rows,
124
+ fields: result.fields.map((field) => field.name),
125
+ };
126
+ }
127
+ finally {
128
+ await client.end();
129
+ }
130
+ }
131
+ }
132
+ //# sourceMappingURL=gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gate.js","sourceRoot":"","sources":["../src/gate.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,EAGL,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,MAAM,IAAI,CAAA;AAEnB,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAiB,MAAM,mBAAmB,CAAA;AAmFrF;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IACP,gBAAgB,CAAQ;IACxB,MAAM,CAAoB;IAC1B,QAAQ,CAAQ;IAChB,OAAO,CAAQ;IACf,aAAa,CAAQ;IACrB,aAAa,CAAS;IACtB,WAAW,CAAoC;IAC/C,gBAAgB,GAK5B,EAAE,CAAA;IAEP;;OAEG;IACH,YAAY,OAAuB;QACjC,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAA;QAChD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CA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EAAE,CAAC,CAAA;QACzE,MAAM,MAAM,CAAC,OAAO,EAAE,CAAA;QACtB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACtC,OAAO;gBACL,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC;aACjD,CAAA;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,MAAM,CAAC,GAAG,EAAE,CAAA;QACpB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * SQL classification and Postgres action gating (`\@limetry/sql`).
3
+ *
4
+ * Classifies statements, evaluates ActionIntents remotely, and optionally
5
+ * executes allowed queries while keeping connection credentials private.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ export { actionTypeForSqlClass, classifySql, type SqlClass, } from "./classify-sql.js";
10
+ export { SqlActionGate, type SqlGateOptions, type SqlGateResult, } from "./gate.js";
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,KAAK,QAAQ,GACd,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EACL,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,aAAa,GACnB,MAAM,WAAW,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * SQL classification and Postgres action gating (`\@limetry/sql`).
3
+ *
4
+ * Classifies statements, evaluates ActionIntents remotely, and optionally
5
+ * executes allowed queries while keeping connection credentials private.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ export { actionTypeForSqlClass, classifySql, } from "./classify-sql.js";
10
+ export { SqlActionGate, } from "./gate.js";
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,qBAAqB,EACrB,WAAW,GAEZ,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EACL,aAAa,GAGd,MAAM,WAAW,CAAA"}
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Stdio MCP server for governed Postgres/Supabase SQL (`\@limetry/sql`).
4
+ *
5
+ * Owns `DATABASE_URL` (or `SUPABASE_DB_URL`) and never returns credentials to the model.
6
+ * Requires `LIMETRY_API_KEY` and `LIMETRY_POLICY_ID`. Dry-run defaults on unless
7
+ * `LIMETRY_SQL_DRY_RUN=false`.
8
+ */
9
+ export {};
10
+ //# sourceMappingURL=mcp-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":";AACA;;;;;;GAMG"}
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Stdio MCP server for governed Postgres/Supabase SQL (`\@limetry/sql`).
4
+ *
5
+ * Owns `DATABASE_URL` (or `SUPABASE_DB_URL`) and never returns credentials to the model.
6
+ * Requires `LIMETRY_API_KEY` and `LIMETRY_POLICY_ID`. Dry-run defaults on unless
7
+ * `LIMETRY_SQL_DRY_RUN=false`.
8
+ */
9
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
10
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
11
+ import { z } from "zod";
12
+ import { SqlActionGate } from "./gate.js";
13
+ /**
14
+ * Postgres connection string loaded from the process environment.
15
+ */
16
+ const DATABASE_URL = process.env.DATABASE_URL ?? process.env.SUPABASE_DB_URL ?? "";
17
+ /**
18
+ * Limetry API bearer token.
19
+ */
20
+ const API_KEY = process.env.LIMETRY_API_KEY ?? process.env.LIMETRY_BEARER_TOKEN ?? "";
21
+ /**
22
+ * ActionPolicy id used for SQL evaluation.
23
+ */
24
+ const POLICY_ID = process.env.LIMETRY_POLICY_ID ?? "";
25
+ /**
26
+ * Optional Limetry API base URL.
27
+ */
28
+ const BASE_URL = process.env.LIMETRY_BASE_URL;
29
+ /**
30
+ * Agent id for intents created by this MCP server.
31
+ */
32
+ const AGENT_ID = process.env.LIMETRY_AGENT_ID ?? "sql_mcp";
33
+ /**
34
+ * Resource label attached to SQL ActionIntents.
35
+ */
36
+ const RESOURCE_LABEL = process.env.LIMETRY_SQL_RESOURCE ?? "postgres://*";
37
+ if (!DATABASE_URL) {
38
+ process.stderr.write("Error: DATABASE_URL (or SUPABASE_DB_URL) is required.\n");
39
+ process.exit(1);
40
+ }
41
+ if (!API_KEY || !POLICY_ID) {
42
+ process.stderr.write("Error: LIMETRY_API_KEY and LIMETRY_POLICY_ID are required.\n");
43
+ process.exit(1);
44
+ }
45
+ /**
46
+ * Gate instance that holds DB credentials for this MCP process.
47
+ */
48
+ const gate = new SqlActionGate({
49
+ connectionString: DATABASE_URL,
50
+ apiKey: API_KEY,
51
+ baseUrl: BASE_URL,
52
+ policyId: POLICY_ID,
53
+ agentId: AGENT_ID,
54
+ resourceLabel: RESOURCE_LABEL,
55
+ dryRun: process.env.LIMETRY_SQL_DRY_RUN !== "false",
56
+ });
57
+ /**
58
+ * MCP server exposing SQL evaluate / pending-approval tools.
59
+ */
60
+ const server = new McpServer({
61
+ name: "limetry-sql",
62
+ version: "1.0.0",
63
+ });
64
+ server.tool("limetry_sql_query", "Classify and evaluate a Postgres/Supabase SQL statement with Limetry. " +
65
+ "Reads may execute when allowed and dry_run=false. Writes/DDL default to dry-run and may return approval_required.", {
66
+ sql: z.string().min(1),
67
+ dry_run: z.boolean().optional(),
68
+ execute: z.boolean().optional(),
69
+ }, async ({ sql, dry_run, execute }) => {
70
+ const result = await gate.evaluateAndMaybeExecute({
71
+ sql,
72
+ dryRun: dry_run,
73
+ execute,
74
+ });
75
+ return {
76
+ content: [{
77
+ type: "text",
78
+ text: JSON.stringify({
79
+ sql_class: result.sqlClass,
80
+ decision: result.evaluation.decision,
81
+ approved: result.evaluation.approved,
82
+ reasons: result.evaluation.reasons,
83
+ decision_id: result.evaluation.decision_id,
84
+ approval_id: result.evaluation.approval_id,
85
+ dry_run: result.dryRun,
86
+ executed: result.executed,
87
+ rows: result.rows,
88
+ }, null, 2),
89
+ }],
90
+ };
91
+ });
92
+ server.tool("limetry_sql_list_pending", "List SQL statements from this process that returned approval_required.", {}, async () => {
93
+ const pending = gate.listPending().map((entry) => ({
94
+ approval_id: entry.approval_id,
95
+ decision_id: entry.decision_id,
96
+ sql_class: entry.sqlClass,
97
+ sql_preview: entry.sql.slice(0, 200),
98
+ }));
99
+ return {
100
+ content: [{
101
+ type: "text",
102
+ text: JSON.stringify({ pending }, null, 2),
103
+ }],
104
+ };
105
+ });
106
+ /**
107
+ * Stdio transport that connects the MCP server to the host process.
108
+ */
109
+ const transport = new StdioServerTransport();
110
+ await server.connect(transport);
111
+ //# sourceMappingURL=mcp-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-server.js","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":";AACA;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAA;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAEzC;;GAEG;AACH,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,EAAE,CAAA;AAElF;;GAEG;AACH,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe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,SAAS,EAAE,KAAK,CAAC,QAAQ;QACzB,WAAW,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;KACrC,CAAC,CAAC,CAAA;IACH,OAAO;QACL,OAAO,EAAE,CAAC;gBACR,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;aAC3C,CAAC;KACH,CAAA;AACH,CAAC,CACF,CAAA;AAED;;GAEG;AACH,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAA;AAC5C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA"}
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Live CI demo entrypoint that classifies and evaluates SQL through Limetry.
4
+ *
5
+ * Uses `dryRun: false` so allowed reads can execute when `INPUT_EXECUTE` is not `"false"`.
6
+ */
7
+ /**
8
+ * Test overrides for {@link runSqlCiDemo}.
9
+ */
10
+ export type SqlCiDemoOverrides = {
11
+ /**
12
+ * Optional fetch used by the Limetry client.
13
+ */
14
+ fetch?: typeof fetch;
15
+ /**
16
+ * Optional query runner replacing the default Postgres client.
17
+ */
18
+ queryRunner?: (sql: string) => Promise<unknown>;
19
+ };
20
+ /**
21
+ * Classify and evaluate a SQL statement against Limetry for the live CI demo.
22
+ * Allowed reads may execute. Writes and DDL must be denied before Postgres is touched.
23
+ *
24
+ * @param env - Process environment; defaults to `process.env`.
25
+ * @param overrides - Optional fetch / queryRunner overrides for tests.
26
+ * @returns Process exit code (`0` allow, `1` deny / missing inputs).
27
+ * @throws Propagates unexpected evaluation or query errors to the direct-run handler.
28
+ */
29
+ export declare function runSqlCiDemo(env?: NodeJS.ProcessEnv, overrides?: SqlCiDemoOverrides): Promise<number>;
30
+ //# sourceMappingURL=run-ci-demo.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-ci-demo.d.ts","sourceRoot":"","sources":["../src/run-ci-demo.ts"],"names":[],"mappings":";AACA;;;;GAIG;AAIH;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,KAAK,CAAA;IACpB;;OAEG;IACH,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CAChD,CAAA;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,GAAG,GAAE,MAAM,CAAC,UAAwB,EACpC,SAAS,GAAE,kBAAuB,GACjC,OAAO,CAAC,MAAM,CAAC,CA+CjB"}
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Live CI demo entrypoint that classifies and evaluates SQL through Limetry.
4
+ *
5
+ * Uses `dryRun: false` so allowed reads can execute when `INPUT_EXECUTE` is not `"false"`.
6
+ */
7
+ import { SqlActionGate } from "./gate.js";
8
+ /**
9
+ * Classify and evaluate a SQL statement against Limetry for the live CI demo.
10
+ * Allowed reads may execute. Writes and DDL must be denied before Postgres is touched.
11
+ *
12
+ * @param env - Process environment; defaults to `process.env`.
13
+ * @param overrides - Optional fetch / queryRunner overrides for tests.
14
+ * @returns Process exit code (`0` allow, `1` deny / missing inputs).
15
+ * @throws Propagates unexpected evaluation or query errors to the direct-run handler.
16
+ */
17
+ export async function runSqlCiDemo(env = process.env, overrides = {}) {
18
+ const apiKey = env.INPUT_LIMETRY_API_KEY ?? env.LIMETRY_API_KEY ?? "";
19
+ const baseUrl = env.INPUT_LIMETRY_BASE_URL ?? env.LIMETRY_BASE_URL;
20
+ const policyId = env.INPUT_POLICY_ID ?? env.LIMETRY_POLICY_ID ?? "";
21
+ const agentId = env.INPUT_AGENT_ID ?? "sql_agent";
22
+ const sql = env.INPUT_SQL ?? env.LIMETRY_SQL ?? "SELECT 1 AS ok";
23
+ const connectionString = env.DATABASE_URL ?? env.SUPABASE_DB_URL ?? "postgresql://limetry:limetry@127.0.0.1:5432/limetry";
24
+ const execute = (env.INPUT_EXECUTE ?? "true") !== "false";
25
+ if (!apiKey || !policyId) {
26
+ process.stderr.write("Missing required SQL demo inputs (api key or policy id)\n");
27
+ return 1;
28
+ }
29
+ const gate = new SqlActionGate({
30
+ connectionString,
31
+ apiKey,
32
+ baseUrl,
33
+ policyId,
34
+ agentId,
35
+ resourceLabel: env.INPUT_RESOURCE ?? "postgres://limetry-ci/demo",
36
+ dryRun: false,
37
+ fetch: overrides.fetch,
38
+ queryRunner: overrides.queryRunner,
39
+ });
40
+ const result = await gate.evaluateAndMaybeExecute({
41
+ sql,
42
+ dryRun: false,
43
+ execute,
44
+ });
45
+ const decision = result.evaluation.decision ?? "deny";
46
+ process.stdout.write(`Limetry ${decision} for ${result.intent.action_type} (${result.sqlClass})` +
47
+ ` executed=${String(result.executed)}\n`);
48
+ if (decision === "allow") {
49
+ return 0;
50
+ }
51
+ process.stderr.write(`Limetry denied ${result.intent.action_type}: ${(result.evaluation.reasons ?? []).join("; ")}\n`);
52
+ return 1;
53
+ }
54
+ /**
55
+ * True when this module is executed directly rather than imported.
56
+ */
57
+ const isDirectRun = process.argv[1]?.endsWith("run-ci-demo.js") ||
58
+ process.argv[1]?.endsWith("run-ci-demo.ts");
59
+ if (isDirectRun) {
60
+ void runSqlCiDemo()
61
+ .then((code) => {
62
+ process.exit(code);
63
+ })
64
+ .catch((error) => {
65
+ const message = error instanceof Error ? error.message : String(error);
66
+ process.stderr.write(`${message}\n`);
67
+ process.exit(1);
68
+ });
69
+ }
70
+ //# sourceMappingURL=run-ci-demo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-ci-demo.js","sourceRoot":"","sources":["../src/run-ci-demo.ts"],"names":[],"mappings":";AACA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAgBzC;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAAyB,O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+ "description": "Postgres/Supabase MCP write gate that evaluates SQL intents with Limetry",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "bin": {
16
+ "limetry-sql-mcp": "dist/mcp-server.js"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "policies"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/limetry/limetry.git",
29
+ "directory": "packages/sql"
30
+ },
31
+ "scripts": {
32
+ "prepack": "node ../../scripts/rewrite-workspace-protocol.mjs prepare",
33
+ "postpack": "node ../../scripts/rewrite-workspace-protocol.mjs restore",
34
+ "build": "tsc -p tsconfig.json",
35
+ "test": "vitest run --coverage",
36
+ "typecheck": "tsgo -p tsconfig.json --noEmit"
37
+ },
38
+ "dependencies": {
39
+ "pg": "^8.14.1",
40
+ "zod": "^4.5.4"
41
+ },
42
+ "peerDependencies": {
43
+ "@limetry/sdk": "*",
44
+ "@modelcontextprotocol/sdk": "^1.30.0"
45
+ },
46
+ "devDependencies": {
47
+ "@limetry/sdk": "^1.2.49",
48
+ "@modelcontextprotocol/sdk": "^1.30.0",
49
+ "@types/node": "^26.4.1",
50
+ "@types/pg": "^8.11.11",
51
+ "@vitest/coverage-v8": "4.1.11",
52
+ "typescript": "^5.8.2",
53
+ "vitest": "4.1.11"
54
+ },
55
+ "engines": {
56
+ "node": ">=20"
57
+ }
58
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ "policy_id": "55555555-5555-4555-8555-555555555555",
3
+ "version": 1,
4
+ "organization_id": "default",
5
+ "agent_id": "sql_agent",
6
+ "allowed_action_types": [
7
+ "sql.read"
8
+ ],
9
+ "denied_action_types": [
10
+ "sql.write",
11
+ "sql.ddl"
12
+ ],
13
+ "audit_mode": "minimal",
14
+ "status": "active",
15
+ "updated_at": "2026-08-18T00:00:00.000Z",
16
+ "effective_from": "2026-01-01T00:00:00.000Z",
17
+ "expires_at": null
18
+ }