LogCarverGuard

io.github.caiderek由 GitHub 帳號 caiderek 發布v0.1.0更新於 Oct 24, 2026

Guardrail for AI agents on SQL Server: statement blocking, optional read-only mode and an audit log.

已驗證STDIO僅桌面資料庫安全與監控

概覽

AI 產生的概覽

一個本機 MCP 代理,讓 AI 代理對 SQL Server 執行受管控的 T-SQL,攔截危險語句並寫入稽核記錄。

可以做什麼
透過單一的 execute_sql 工具對 SQL Server 執行 SQL,SELECT 會回傳結果(顯示前 100 列並附上總數),寫入語句執行後記入稽核。使用真正的 T-SQL 解析器而非關鍵字比對,在語句送達資料庫前攔截危險操作。可啟用唯讀模式,僅放行不含 INTO 的 SELECT 以及 SET、DECLARE、PRINT。依 schema.table 或 schema.procedure 白名單放行指定的 TRUNCATE TABLE、ALTER TABLE DROP COLUMN 與具名 EXEC。在本機 SQLite 稽核記錄中記下誰執行了哪條 SQL、是被攔截或已執行。
適用情境
適合 AI 程式代理需要讀寫真實 SQL Server(含正式環境資料庫),而你希望防止誤操作造成破壞並保留稽核紀錄的情境。它只是防護層而非安全邊界,應搭配最小權限的 SQL 登入使用。
執行需求
以 .NET 工具形式透過 stdio 在本機執行,可用 dotnet tool install 安裝或經 dnx 執行。需要 LOGCARVERGUARD_CONNECTION_STRING(或 _FILE 變體)提供 SQL Server 連接字串;選用設定包括唯讀模式、稽核資料庫路徑與三個白名單。已在 Windows 上以整合式驗證測試。
安裝前請注意
連接字串含有真實資料庫憑證,存放在你的機器與 AI 工具設定中;能讀取該設定或執行 shell 指令的代理可繞過此代理。所有語句都以該單一登入執行,稽核記錄是記下作業系統使用者名稱的本機檔案。攔截清單刻意收窄:DROP INDEX、sp_rename、BACKUP DATABASE 與 OPENQUERY 未特別處理,唯讀模式也無法阻止有副作用的函式、高成本查詢與鎖定。

安裝

在 SourceWeft 中

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

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

其他 MCP 客戶端

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

README

LogCarverGuard

English | 繁體中文

An MCP proxy that lets an AI coding agent (Claude Code, Cursor, …) read and write a production SQL Server without handing it the real credentials or a free hand. Every statement runs under the caller's own SQL login and grants, dangerous statements are blocked before they reach the database, and everything is written to an audit log.

Status: early (v0.1).

What it does

AI agent --(MCP, API key)--> LogCarverGuard proxy --(EXECUTE AS the caller's own login)--> SQL Server
  • One tool, execute_sql. Reads and writes go through it. A SELECT returns its rows (first 100 shown, with a count of the rest). A write is executed and audited.
  • A real T-SQL parser decides what is allowed, not keyword matching, so a fake keyword inside a comment or string does nothing.
  • The AI never sees the database password. The proxy holds one service login. Each person gets an API key that maps to their own SQL login, and the proxy impersonates it, so their existing GRANT/DENY still decide what they can do.
  • The service login does not need sysadmin. It needs IMPERSONATE on each user's login, and a database user in the target database so it can connect. The host tests run SELECT, UPDATE and a permission-denied DELETE through a service account that is not sysadmin.
  • Data changes run in a transaction the proxy controls. A batch containing INSERT / UPDATE / DELETE is rolled back as a whole if any statement fails. DDL (ALTER TABLE…), TRUNCATE and EXEC run as they are and are not rolled back as part of a batch. BEGIN TRAN / COMMIT / ROLLBACK from the caller is blocked.
  • Audit log in a local SQLite file: who, when, which SQL, and whether it was blocked or executed. Executed statements are recorded after they commit. A statement that SQL Server rejects (for example permission denied) or a request with a bad key is not recorded.

Always blocked

DROP TABLE, DROP DATABASE, RESTORE DATABASE / RESTORE LOG, ALTER DATABASE … SET, sp_executesql, xp_cmdshell, dynamic EXEC of a variable or string, identity switching (REVERT, EXECUTE AS, SETUSER), permission changes (GRANT, DENY, REVOKE), and caller-managed transactions (BEGIN TRAN, COMMIT, ROLLBACK, SAVE TRANSACTION).

Blocked unless the admin allow-lists it

TRUNCATE TABLE, ALTER TABLE … DROP COLUMN, and EXEC of a named stored procedure (including INSERT … EXEC). Allow-list entries are written as schema.name; a statement that names a database or a server (OtherDb.dbo.T, [srv].db.dbo.P) is never matched against them.

This list is deliberately narrow. It is not "everything an AI could break": DROP INDEX, sp_rename, BACKUP DATABASE, or a Linked Server OPENQUERY are not specially handled and run if the real login may run them. The real security boundary is the SQL login's own grants, so issue each API key against an account with the least privilege you can accept, never sysadmin or db_owner.

Local mode

Runs on your own machine, started by your AI tool over stdio. No server, no Docker, no API keys. Settings come only from environment variables (not from appsettings.json or the command line). Tested on Windows with integrated authentication.

dotnet tool install -g LogCarverGuard.Localclaude mcp add logcarverguard --env LOGCARVERGUARD_CONNECTION_STRING="Server=localhost;Database=MyDb;Integrated Security=true;TrustServerCertificate=true" -- logcarverguard

Or run it without installing, through dnx (end the command with -- dotnet dnx LogCarverGuard.Local --yes).

VariablePurposeDefault
LOGCARVERGUARD_CONNECTION_STRING (or _FILE)The login the statements run asrequired
LOGCARVERGUARD_READ_ONLYtrue allows queries onlyfalse
LOGCARVERGUARD_AUDIT_DB_PATHAudit log SQLite fileLogCarverGuard/audit.db under your local application data folder
LOGCARVERGUARD_TRUNCATE_ALLOWLIST, LOGCARVERGUARD_EXEC_WHITELIST, LOGCARVERGUARD_ALTER_DROP_COLUMN_ALLOWLISTSame allow-lists as the central serviceempty (all blocked)

Read-only mode lets SELECT (without INTO) through, plus SET, DECLARE and PRINT. Everything else is blocked with a reason, including EXEC even when it is allow-listed, NEXT VALUE FOR, and the remote or file sources OPENQUERY, OPENROWSET and OPENDATASOURCE (they can run a write on another server). What read-only mode does not cover: a SELECT that calls a CLR or extended-procedure function with side effects, long-running or expensive queries, and locks. Also give the login only the permissions it needs; the guard is a second layer, not the first.

What local mode is, and is not. It is a guardrail against an agent doing something destructive by accident. It is not a security boundary:

  • The connection string, and so the credentials, live on your machine and in your AI tool's configuration. An agent that can run shell commands or read that configuration can connect to SQL Server directly and skip this proxy.
  • Every statement runs as the one login in the connection string. There is no per-person identity; the audit log records the operating-system user name and is a local file.
  • For a real boundary use the central service below: SQL Server only accepts connections from the proxy, and each person has their own login.

Install the central service

Needs a Linux host with Docker, and a SQL Server with TCP/IP enabled and mixed-mode authentication.

  1. Create the service login and let it impersonate each user's own login:

    sql
    CREATE LOGIN logcarverguard_svc WITH PASSWORD = '…';GRANT IMPERSONATE ON LOGIN::[alice_sql_login] TO logcarverguard_svc;

    In the target database, create a database user for the service login and one for each person's login, then give each person the permissions they normally have; the proxy never widens them:

    sql
    CREATE USER logcarverguard_svc FOR LOGIN logcarverguard_svc;CREATE USER alice_sql_login FOR LOGIN alice_sql_login;GRANT SELECT, UPDATE ON dbo.Orders TO alice_sql_login;
  2. Clone and configure

    bash
    git clone https://github.com/caiderek/LogCarverGuardcd LogCarverGuard

    Edit docker-compose.yml and set LOGCARVERGUARD_SQLSERVER_CONNECTION_STRING. For production prefer a mounted secret file and LOGCARVERGUARD_SQLSERVER_CONNECTION_STRING_FILE.

  3. Start

    bash
    docker compose up -d --builddocker compose logs -f
  4. Issue an API key (--login is the user's own SQL login, not the service login). The key is printed once:

    bash
    docker compose exec logcarverguard dotnet cli/LogCarverGuard.Cli.dll \  issue-key --owner alice --login alice_sql_login
  5. Point your MCP client at http://<host>:8080/ and send the key in the header X-LogCarverGuard-Key.

Other key commands: list-keys (never prints the key itself) and revoke-key --id <n>.

Configuration

All settings are environment variables; they take priority over appsettings.json.

VariablePurposeDefault
LOGCARVERGUARD_SQLSERVER_CONNECTION_STRING (or _FILE)The proxy's service loginrequired
LOGCARVERGUARD_AUDIT_DB_PATHAudit log SQLite filelogcarverguard-audit.db
LOGCARVERGUARD_APIKEYS_DB_PATHAPI key SQLite filelogcarverguard-apikeys.db
LOGCARVERGUARD_TRUNCATE_ALLOWLISTTables where TRUNCATE is allowedempty (all blocked)
LOGCARVERGUARD_EXEC_WHITELISTStored procedures that may be EXEC'dempty (all blocked)
LOGCARVERGUARD_ALTER_DROP_COLUMN_ALLOWLISTTables where DROP COLUMN is allowedempty (all blocked)
LOGCARVERGUARD_ALLOWED_CLIENT_IPSIPs / CIDR ranges allowed to connect, checked before the keyempty (no restriction)
LOGCARVERGUARD_TRUSTED_PROXIESPeers trusted to set X-Forwarded-For (only behind a reverse proxy)empty (header never read)
ASPNETCORE_URLSListen addressdeployment dependent

Set Initial Catalog in the connection string to the database the agent should work in.

Hardening for production

  • Do not leave TrustServerCertificate=true in production. The sample connection string uses it for convenience; it turns off validation of SQL Server's certificate, so install a trusted certificate and drop it.
  • Use TLS. The key travels in an HTTP header; over plain HTTP it can be sniffed and gives whoever holds it that person's whole SQL access. Put a TLS-terminating reverse proxy in front. The compose file binds to 127.0.0.1 by default for that reason.
  • Restrict who can connect with LOGCARVERGUARD_ALLOWED_CLIENT_IPS, in addition to the host firewall. Behind a reverse proxy also set LOGCARVERGUARD_TRUSTED_PROXIES, otherwise every request appears to come from the proxy.
  • Back up the /data volume. It holds the audit log and the key hashes.
  • Keep the .NET runtime and the MCP SDK updated.

Known limits

  • SQL Server only. The central service runs on Linux (Docker); local mode was tested on Windows with integrated authentication.
  • It only governs SQL sent through this MCP connection. If the agent has other tools (files, APIs, cloud consoles), this proxy cannot see or stop those.
  • The API key is a single static secret: no mTLS, no device binding, no short-lived tokens.
  • No mask-on-read: if an account can SELECT sensitive data, the agent can read it.
  • SQL Server's own error text is returned to the caller as it is. It can name databases, objects and logins, and features such as OPENROWSET / OPENQUERY are not blocked, so an agent holding a key with enough permission can use them to probe what the SQL Server can reach.
  • A SELECT keeps at most 1000 rows in memory (the first 100 are shown, the true count is reported).
  • The audit log is a SQLite file read directly; there is no MCP tool for browsing it.

Build and test

bash
dotnet builddotnet test

The integration tests need a local SQL Server and a Windows login that is sysadmin; they create and drop their own LogCarverGuard_* databases and logins.

Related

LogCarver (free, MIT) reads deleted rows from a SQL Server transaction log. LogCarver.Mcp exposes it to AI assistants over MCP.

License

MIT, see LICENSE.

來源:README.md,提交 618b808

工具

0
SourceWeft 尚未在沙箱中啟動過該軟體包,因此其工具未知。

版本歷史

1
  1. v0.1.0最新Oct 12, 2026

評分

已滿足適用檢查項的 50%

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

  • 近 30 天內可連通25 分

    請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。

  • 已宣告認證方式10 分

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

  • 已列出工具15 分

    請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。

  • 描述必需15 分
  • 圖示10 分
  • 文件15 分
  • 授權條款10 分