Skip to main content
Glama
egoring

sql-guard-mcp

by egoring

sql-guard-mcp

CI

为 AI 代理提供只读 SQL,并带有多层安全防护。

让 LLM 代理直接访问原始数据库,无异于埋下隐患。这个 MCP 服务器通过四个独立的防护层向代理开放 SQLite 数据库,因此代理最坏的情况也只是读取得慢一些——而连这种情况也会被中止。

韩语文档: README.ko.md

防护层

防护层

阻止的内容

实现方式

查询校验

写入、schema 变更、通过堆叠语句进行的注入

仅允许单个 SELECT/WITH 语句;扫描禁用关键字(INSERTDROPPRAGMAATTACH 等);拒绝多语句

表级白名单

读取敏感表(PII、凭据)

FROM/JOIN 标识符与 SQLGUARD_ALLOWED_TABLES 进行核对;同时过滤 list_tables/describe_table

行数上限

淹没上下文窗口

每个查询都会被包装为带服务端 LIMIT 的子查询——用户提供的 LIMIT 999999 无法覆盖它;截断会被标记

执行上限

失控查询(笛卡尔连接)

SQLite 进度处理器的看门狗在 N 个 VM 步后中止查询,并给出可操作的提示信息

操作系统级只读

以上所有防护失效时

使用 mode=ro 打开连接——这是最后一道防线,由 SQLite 自身强制执行

这一设计原则源自为生产环境中的 LLM 代理构建决策防护的经验:不要指望模型会小心谨慎——要让粗心大意变得不可能,并让每一条拒绝消息都告诉代理应该怎么做。

Related MCP server: sqlite-analyst

工具

  • sql_list_tables — 可见的表(已应用白名单)

  • sql_describe_table — 列、类型、行数

  • sql_query — 受防护的只读查询

  • sql_guard_status — 当前防护配置(调试时透明可见)

演示

已连接 Claude Desktop,正在查询内置的广告活动演示数据库——并拒绝了一个删除请求:

sql-guard-mcp 演示:CTR 排名问题已作答,DELETE 请求被拒绝

代理可以自由探索和聚合(“哪个活跃广告活动的 CTR 最高?”),但当被要求清空 campaigns 表时,防护层会拒绝请求,代理会解释原因——只读是设计使然,由代码强制执行,而非依赖提示词。

安装

除 MCP SDK 之外零依赖——内置一个合成的广告活动演示数据库,首次运行时自动创建。

pip install -e .

# optional configuration
export SQLGUARD_DB="/path/to/your.db"                      # default: bundled demo
export SQLGUARD_ALLOWED_TABLES="campaigns,daily_stats"     # default: all tables
export SQLGUARD_MAX_ROWS="200"

Claude Desktop

{
  "mcpServers": {
    "sql-guard-mcp": {
      "command": "sql-guard-mcp",
      "env": { "SQLGUARD_ALLOWED_TABLES": "campaigns,daily_stats" }
    }
  }
}

然后这样问:“上周哪个活跃广告活动的 CTR 最高?”——代理会在防护框架内探索 schema 并发起查询。试着让它删除某些内容;看看它的拒绝说明。

测试

pip install -e ".[dev]"
pytest   # guard validation + execution enforcement, no external DB needed

测试涵盖对抗性用例:堆叠语句、以 SELECT 开头的写入语句、通过 JOIN 绕过白名单、尝试用用户提供的 LIMIT 覆盖限制,以及被 VM 步看门狗中止的笛卡尔连接失控查询。

许可证

MIT

A
license - permissive license
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    Not graded
    quality
    C
    maintenance
    Enables AI assistants to explore and query SQLite databases through read-only tools, with defense-in-depth sandboxing preventing any data modifications.
    MIT
  • F
    license
    A
    quality
    C
    maintenance
    Enables AI agents to safely explore and query a SQLite database in read-only mode, allowing them to inspect schema and run analytical SQL queries without risking data modification.
    3

View all related MCP servers

Related MCP Connectors

  • Explore, query, and inspect SQLite databases with ease. List tables, preview results, and view det…

  • Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.

  • Read-only bank access for your AI agent. Connects Claude, ChatGPT, Cursor, Gemini, Codex.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/egoring/sql-guard-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server