Db Gateway

io.github.GuardBee由 GitHub 帳號 GuardBee 發布v0.7.1更新於 Nov 3, 2026

KVKK/GDPR-compliant LLM-to-database gateway: PII masking, RBAC, rate limiting, audit log

概覽

AI 產生的概覽

一個本機 MCP 閘道,位於 AI 助理與資料庫之間,自動遮罩敏感欄位、依角色控管資料表存取、限流並稽核每次查詢。

可以做什麼
  • 列出並描述可存取的資料表,包含其遮罩策略與角色限制
  • 依條件查詢資料表資料,敏感欄位自動遮罩
  • 依資料表、工具、操作類型、僅拒絕紀錄或時間範圍查詢閘道自身的稽核歷程
  • 檢視目前閘道設定、欄位規則、資料表策略與稽核設定
  • 在明確開啟寫入後,依資料表與角色權限新增、更新或刪除資料列
適用情境
當助理需要讀取真實資料庫,但你希望以 PII 遮罩、資料表層級存取控管、限流與稽核紀錄取代直接提供資料庫憑證時使用。適合有 KVKK 或 GDPR 合規要求、想為 LLM 提供預設唯讀受控通道的團隊。
執行需求
以本機程序執行,透過 npm(Node.js)安裝或經 Smithery 加入。需要資料庫連線:命令列方式使用 DATABASE_URL,或在程式碼中傳入既有的 Prisma、pg、mysql2、better-sqlite3 或 MongoDB 用戶端。作用中角色以 GATEWAY_ROLE 或設定指定;選用設定包括 GATEWAY_PII_SCAN 與 GUARDBEE_TELEMETRY。
安裝前請注意
閘道持有資料庫憑證,所提供的 DATABASE_URL 會帶來該帳號的全部權限。寫入預設關閉;開啟 writesEnabled 並授予資料表與角色寫入權限後,助理可修改或刪除資料,但空篩選條件與超出上限的符合筆數會被拒絕。稽核歷程保存在記憶體中,程序重啟即清空。遙測預設關閉,但設定 GUARDBEE_TELEMETRY=1 會傳送工具名稱、耗時及檔案路徑等短參數值。

安裝

在 SourceWeft 中

  1. 開啟 儀表板中的 Db Gateway,將其新增到工作區。
  2. 為需要使用其工具的對話啟用該服務。

Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。

其他 MCP 客戶端

參照 儲存庫 中的啟動說明。

README

@guardbee/mcp-db-gateway

🇬🇧 English | 🇹🇷 Türkçe | 🇨🇳 中文 | 🇯🇵 日本語 | 🇰🇷 한국어 | 🇧🇷 Português | 🇪🇸 Español | 🇩🇪 Deutsch

[npm version] [npm downloads] [License: MIT] [Smithery]

A KVKK / GDPR-compliant MCP (Model Context Protocol) server — adds a security layer between an LLM and a database.

Instead of querying your database directly, Claude (or any other LLM) goes through this gateway. Sensitive fields are masked automatically, table access is role-controlled, and every query is written to an audit log.

Telemetry is off by default. With GUARDBEE_TELEMETRY=1 it sends the server, tool, success, duration and short parameter values such as a file path; longer strings and fields like content, token or password are replaced with a placeholder. See @guardbee/mcp-telemetry.

Claude ──► MCP Gateway ──► Database              │              ├─ PII masking     (tcKimlik → [REDACTED])              ├─ Role check      (ai-agent can only reach the products table)              ├─ Rate limiting   (max 100 queries/minute)              └─ Audit log       (every query is recorded)

Features

  • PII Masking — Turkish national ID, IBAN, email, phone, password hashes, etc. are masked automatically
  • Role-Based Access (RBAC) — Per-role table allow/deny lists and field rules
  • Rate Limiting — Global and per-table request windows
  • Audit Log — Writable to console, a file, or an HTTP webhook
  • Prisma / Postgres / MySQL / SQLite / MongoDB Adapters — Plug in an existing PrismaClient, a pg Pool, a mysql2 Pool, a better-sqlite3 Database, or a MongoDB Db directly
  • Write Support (optional) — insert/update/delete, off by default; gated by per-table + per-role permission, protected-field guarding, and a "don't touch the whole table" safety net
  • 195 unit tests — masking, the pipeline, RBAC, the rate limiter, the audit log, and every adapter (read + write) are covered

Recent Changes (2026-09-15)

Three new steps in this package's AI Gateway growth work:

1. The query_audit_log tool

Previously the gateway's audit log was write-only — it landed in one of the console/file/http sinks, but Claude itself had no way to look back and ask "what just happened, which calls got denied." Now it can:

typescript
// Tool called from the Claude side:query_audit_log({ table: "orders", deniedOnly: true, limit: 20 })
  • How it works: AuditLogger gained an always-on, in-memory ring buffer that's completely independent of the configured sink (audit.bufferSize, default 200). Even when the sink is "http" (a fire-and-forget webhook), history is still queryable through this buffer. The buffer resets when the process restarts, and is never populated while audit.enabled: false.
  • Filters: table, tool (the MCP tool name, e.g. "delete_row"), operation (read/insert/update/delete), deniedOnly (denied calls only), since (an ISO 8601 timestamp), limit (default 50, max 200). Results come back newest-first.
  • A real bug fixed along the way: registerDbTools (read tools) and registerWriteTools (write tools) previously each built their own GatewayPipeline/AuditLogger instance. That meant audit events from insert_row/update_row/delete_row calls would never have appeared in the buffer that query_audit_log reads — there were two separate, unaware-of-each-other buffers. server.ts now builds a single GatewayPipeline instance and shares it across both tool groups.
  • Tests: src/__tests__/audit-logger.test.ts (10 tests — buffer capping, every filter, ring-buffer behavior) plus 3 tests added to pipeline.test.ts (verifying that reads and writes land in the same buffer, that deniedOnly + table filters compose correctly, and that the buffer stays empty while audit.enabled: false).

2. The SQLite adapter

The gateway now works with better-sqlite3 alongside Prisma/Postgres/MySQL:

typescript
import Database from "better-sqlite3";import { createServer, createSqliteAdapter } from "@guardbee/mcp-db-gateway";
const db = new Database("./app.db");const server = createServer({}, createSqliteAdapter(db));
  • Same security pattern as pg/mysql: table/column names can't be parameterized, so they're validated before being embedded in SQL — here using SQLite's PRAGMA table_info(table) instead of information_schema. A name that fails the format check (e.g. users"; DROP TABLE users;--) or isn't in the live schema is rejected before any query reaches SQLite.
  • better-sqlite3 is only an optional peerDependency — it was not added to the package's own devDependencies, since it requires a native binding; tests (like the pg/mysql tests) were written against a simple mock object matching the SqliteQueryable interface, without importing the real package.
  • lastInsertRowid instead of RETURNING for insert: since SQLite's RETURNING support is version-dependent, this follows the same approach as the MySQL adapter's insertId — if the table has an id column and it's not already in data, the inserted row gets lastInsertRowid attached.
  • Bonus fix: the AuditEvent/AuditQueryFilter types were not exported from the package root (index.ts) at all — added for consumers who want to type their own calls to GatewayPipeline.queryAuditLog().
  • Tests: src/__tests__/sqlite-adapter.test.ts (21 tests) — the same scenario set as pg-adapter.test.ts: table listing, filtered/unfiltered queries, limits, unknown table/column rejection, injection-attempt rejection, insert (including id assignment), update, delete.

3. The MongoDB adapter

The gateway now also works with a Db instance from the mongodb driver:

typescript
import { MongoClient } from "mongodb";import { createServer, createMongoAdapter } from "@guardbee/mcp-db-gateway";
const client = new MongoClient(process.env.DATABASE_URL!);await client.connect();const server = createServer({}, createMongoAdapter(client.db("mydb")));
  • A different risk class — not SQL injection, but operator injection: for pg/mysql/sqlite the risk was a table/column name being embedded in SQL. MongoDB never embeds identifiers, but if a key in filter/data is a Mongo operator like $where/$ne, or a value is an operator object like { $ne: null }, the "simple key-value equality filter" a tool declares can silently turn into an arbitrary query. This adapter rejects all of the following up front:
    • any key starting with $ (in both filter and data)
    • any key containing . — a dotted path (e.g. "passwordHash.reset") is both an operator-like risk and a way to bypass findProtectedWriteFields's exact-name matching (outside of its glob support)
    • any non-scalar (object/array) filter value — including an operator-object bypass attempt like { status: { $ne: "active" } }
  • mongodb is only an optional peerDependency — same as the SQLite adapter, tests were written against a mock object matching the MongoDatabase interface, without importing the real package.
  • "Table" = collection. tables() maps to listCollections(); insert attaches _id (the driver's insertedId) to the returned row; update runs updateMany with { $set: data }; delete runs deleteMany.
  • Tests: src/__tests__/mongo-adapter.test.ts (19 tests) — collection listing, filtered/unfiltered queries, limits, three operator-injection scenarios (a $-prefixed key, a dotted key, an operator-object value), insert/update/delete.

Quick Start

1. Connect with one click via Smithery

You can add this to Claude Desktop with one click via Smithery.

2. Connect to Claude Desktop via global install

bash
npm install -g @guardbee/mcp-db-gateway

Add to ~/Library/Application Support/Claude/claude_desktop_config.json (macOS):

json
{  "mcpServers": {    "guardbee-db-gateway": {      "command": "guardbee-gateway",      "env": {        "DATABASE_URL": "postgresql://user:pass@localhost:5432/mydb"      }    }  }}

Windows: %APPDATA%\Claude\claude_desktop_config.json Linux: ~/.config/Claude/claude_desktop_config.json

Restart Claude Desktop. The demo database loads automatically, with PII masking active.

3. Use it in your project (Prisma)

bash
npm install @guardbee/mcp-db-gateway
typescript
import { PrismaClient } from "@prisma/client";import { createServer, createPrismaAdapter } from "@guardbee/mcp-db-gateway";
const prisma = new PrismaClient();
const server = createServer(  {    audit: { enabled: true, sink: "file", filePath: "./audit.jsonl" },  },  createPrismaAdapter(prisma));

If you don't use Prisma, you can also pass a raw pg or mysql2 connection directly:

typescript
// Postgresimport { Pool } from "pg";import { createServer, createPgAdapter } from "@guardbee/mcp-db-gateway";
const pool = new Pool({ connectionString: process.env.DATABASE_URL });const server = createServer({}, createPgAdapter(pool /*, { schema: "public" } */));
typescript
// MySQLimport mysql from "mysql2/promise";import { createServer, createMysqlAdapter } from "@guardbee/mcp-db-gateway";
const pool = mysql.createPool(process.env.DATABASE_URL!);const server = createServer({}, createMysqlAdapter(pool /*, { database: "shop" } */));
typescript
// SQLiteimport Database from "better-sqlite3";import { createServer, createSqliteAdapter } from "@guardbee/mcp-db-gateway";
const db = new Database("./app.db");const server = createServer({}, createSqliteAdapter(db));
typescript
// MongoDBimport { MongoClient } from "mongodb";import { createServer, createMongoAdapter } from "@guardbee/mcp-db-gateway";
const client = new MongoClient(process.env.DATABASE_URL!);await client.connect();const server = createServer({}, createMongoAdapter(client.db("mydb")));

Security note: unlike the Prisma adapter, the pg/mysql2/better-sqlite3 adapters generate raw SQL. Because table and column names can't be parameterized, they are validated against the live schema on every query (information_schema, or PRAGMA table_info for SQLite) — a table/column name that isn't in the schema (e.g. an injection attempt) is rejected before it ever reaches SQL. The MongoDB adapter guards against a different risk ("operator injection" instead of SQL) — see Recent Changes.


MCP Tools

The gateway always exposes these 5 read tools to Claude:

ToolDescription
query_tableQuery rows from a table (PII is masked automatically)
list_tablesList accessible tables (role restrictions applied)
describe_tableShow a table's schema and masking policy
query_audit_logQuery the gateway's own audit history (filterable by table/tool/operation/deniedOnly/since) — in-memory, capped by audit.bufferSize, resets on process restart
gateway_statusShow the active config, roles, and rate-limit status

When writesEnabled: true (see Write Support), 3 more write tools are added:

ToolDescription
insert_rowInserts a new row
update_rowUpdates rows matching a filter (an empty filter is rejected)
delete_rowDeletes rows matching a filter (an empty filter is rejected)

Configuration

typescript
createServer({  // PII field rules (the first match applies)  fieldRules: [    { field: "tcKimlik",     strategy: "redact" }, // [REDACTED]    { field: "iban",         strategy: "mask"   }, // TR32***890    { field: "email",        strategy: "mask"   }, // ah***@example.com    { field: "passwordHash", strategy: "redact" },    { field: "*Token*",      strategy: "redact" }, // glob pattern  ],
  // Table access rules  tableRules: [    { table: "audit_logs", access: "deny"  },    { table: "users",      access: "allow", maxRows: 25 },    // write: if unspecified, that table has no write permission at all (off by default)    { table: "orders",     access: "allow", write: { insert: true, update: true, delete: false } },  ],
  // Turn on the write tools (insert_row/update_row/delete_row) — false by default.  // While false, these tools are never visible to Claude.  writesEnabled: true,
  // How many rows update_row/delete_row may affect via one filter.  // If exceeded, the operation is rejected before touching the database ("narrow your filter").  maxAffectedRowsPerWrite: 10,
  // Default max rows  defaultMaxRows: 50,
  // Rate limiting  rateLimit: {    enabled: true,    windowMs: 60_000,          // 1 minute    maxRequests: 100,           // global limit    maxRequestsPerTable: 20,    // per table  },
  // Audit log  audit: {    enabled: true,    sink: "file",              // "console" | "file" | "http"    filePath: "./audit.jsonl",    // webhookUrl: "https://..."  (for sink: "http")    bufferSize: 200,           // size of the in-memory history the `query_audit_log` tool reads  },
  // Roles  roles: [    {      name: "ai-agent",      allowTables: ["products", "orders"],  // only these tables      maxRows: 10,      // If a role doesn't define write (undefined), write is COMPLETELY off for that role,      // even if the table allows it. To permit writes, state it explicitly on the role too:      write: { insert: true, update: true, delete: false },    },    {      name: "analyst",      denyTables: ["audit_logs"],           // this table is blocked      fieldRules: [        { field: "email", strategy: "allow" }, // email unmasked      ],      // write not defined → analyst can write nothing    },  ],
  // Active role (can also be set via the GATEWAY_ROLE env var)  activeRole: "ai-agent",});

Masking Strategies

StrategyDescriptionExample
redactThe field is removed entirely[REDACTED]
maskThe middle of the value is starred outah***@example.com / 530***67
hashSHA-256 (first 16 characters)a665a45920422f9d
allowPassed through unchanged[email protected]

Glob pattern support: *Password*, *Token*, *Secret*

PII inside text columns (piiScan)

Field rules go by column name, so a free-text column (notes, description) can still carry an e-mail, a TC kimlik or an API key. piiScan (off by default) masks those by value in every text field no field rule covers, nested objects and arrays included; an allow rule turns it off for that field. preset / enable / disable pick the detectors, with the same presets as the security proxy: ALL (default), KVKK, IDENTITY, IDENTIFIERS, SECRETS.

ts
createServer({ piiScan: { enabled: true, preset: "KVKK", disable: ["address"] } }, adapter);

From the CLI: GATEWAY_PII_SCAN=true (every default detector) or GATEWAY_PII_SCAN=KVKK (a preset). An unknown preset or detector name fails at startup.

piiScan.blockPatterns and piiScan.allowPatterns add your own patterns, as in the security proxy: block masks your ids as <CATEGORY_n>, allow leaves a detected value whose whole text matches. A block pattern that runs over its 100 ms budget fails the query rather than return rows it may not have masked.

ts
createServer({ piiScan: { enabled: true, blockPatterns: [{ pattern: "CL-\\d{6}", category: "CLIENT_NO" }], allowPatterns: ["destek@acme\\.com\\.tr"] } }, adapter);

Role-Based Access (RBAC)

The role is set when the server starts, via the GATEWAY_ROLE env var or config.activeRole. Each Claude Desktop profile or deployment can run with a different role.

bash
GATEWAY_ROLE=analyst node dist/cli.js

Rule precedence (highest to lowest):

  1. Global tableRules deny
  2. Role denyTables
  3. Role allowTables (a whitelist — if set, the table must be in this list)
  4. Role fieldRules → global fieldRules

Write Support (write/insert/update/delete)

The gateway is fully read-only by default. Letting the LLM modify data requires deliberately unlocking a few gates:

  1. writesEnabled: true — the global kill-switch. While false (default), insert_row/update_row/delete_row are never visible to Claude.
  2. Table permission — tableRules[].write.{insert,update,delete} — per table, off by default for all.
  3. Role permission (when a role is active) — roles[].write.{insert,update,delete}. If a role never defines write (undefined), write is completely off for that role — even if the table permits it. To permit a write, both the table and the role must explicitly say true (AND logic — deliberately stricter than the "role overrides" logic used for masking rules).

Beyond these three gates, there are two more protections that cannot be disabled:

  • Protected-field guarding — the LLM can never write a value into a field marked redact/mask/hash in fieldRules (e.g. tcKimlik, passwordHash); if insert_row/update_row sees such a field in data, the whole request is rejected.
  • maxAffectedRowsPerWrite — before update_row/delete_row runs, the filter is first tried as a read; if the matched row count exceeds this limit (default 10), the operation is rejected before touching anything. update_row/delete_row also always reject an empty filter — "update/delete the whole table" is never possible through this gateway.

Every write attempt (accepted or denied) is written to the audit log; only which fields were written is logged, never data itself (so the audit log itself can't become a PII leak point).

typescript
createServer({  writesEnabled: true,  maxAffectedRowsPerWrite: 10,  tableRules: [    { table: "orders", access: "allow", write: { insert: true, update: true, delete: false } },  ],  roles: [    { name: "ai-agent", allowTables: ["orders"], write: { insert: true, update: true, delete: false } },  ],  activeRole: "ai-agent",});

Prisma Adapter

Pass a PrismaClient directly — table name → model mapping is automatic:

Query tablePrisma model
"users"prisma.user
"audit_logs"prisma.auditLog
"orders"prisma.order
"orderItems"prisma.orderItem

Development

bash
npm run dev          # dev mode via tsxnpm run build        # TypeScript compilenpm test             # 195 unit testsnpm run test:watch   # watch modenpm run type-check   # type-check only

License

MIT — GuardBee

Feedback

Questions, ideas, or a false positive? Start a thread in GitHub Discussions or open an issue. If GuardBee is useful to you, a ⭐ on the repository helps others find it.

來源:packages/db-gateway/README.md,提交 bc5997d

工具

5

query_table

Query rows from a database table with optional filters. All results pass through the KVKK/GDPR gateway — sensitive fields are automatically masked.

輸入參數limittable必填filter

list_tables

List all available database tables. Tables marked as 'deny' in gateway policy are omitted.

無需輸入參數。

describe_table

Get the column names and gateway masking policy for a table — helps the LLM understand what data it can access.

輸入參數table必填

query_audit_log

Query the gateway's own audit log — inspect which tools were called, on which tables, whether they were allowed or denied (and why), and which fields were redacted/written. Covers events since this server process started; count is capped by the gateway's audit.bufferSize (default 200).

輸入參數toollimitsincetableoperationdeniedOnly

gateway_status

Show the active gateway configuration: field masking rules, table policies, and audit settings.

無需輸入參數。

版本歷史

9
  1. v0.7.1最新Oct 30, 2026
  2. v0.6.2Oct 29, 2026
  3. v0.6.1Oct 22, 2026
  4. v0.5.4Oct 21, 2026
  5. v0.5.3Oct 20, 2026
  6. v0.5.1Oct 19, 2026
  7. v0.5.0Oct 16, 2026
  8. v0.4.0Oct 15, 2026
  9. v0.3.6Oct 13, 2026

評分

已滿足適用檢查項的 90%

該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。

  • 近 30 天內可連通25 分
  • 已宣告認證方式10 分

    請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。

  • 已列出工具必需15 分
  • 描述必需15 分
  • 圖示10 分
  • 文件15 分
  • 授權條款10 分