Skip to main content
Glama
dkautomation23

mcp-data-server

mcp-data-server

展示生产级网页抓取/自动化模式的示例项目。

一个MCP服务器,为Claude(或任何MCP客户端)提供对业务数据库的只读访问——并配备使LLM连接真实公司数据可接受的防护措施:只读连接、表白名单、PII脱敏、行数上限、查询超时和完整审计日志。

在Claude Desktop中询问*"哪些国家订单最多,上个季度退款花了我们多少钱?"*,即可从实际数据库获得答案——模型无法写入、删除、附加或读取未被授权的表。


为什么存在

大多数"将AI连接到我们的数据"项目的障碍不在于连接本身,而在于数据库所有者提出的第一个问题:如何防止它读取或破坏不该碰的东西? 这个服务器用代码回答了这个问题。

Related MCP server: Vela MCP Server

四道独立屏障

#

屏障

阻止的内容

1

连接以 mode=ro 模式打开

任何写入操作,即使绕过上述所有检查

2

语句解析

多条语句,任何非 SELECT / WITH 的内容

3

关键词黑名单

ATTACH、PRAGMA、DDL、VACUUM、GRANT …

4

白名单 + 脱敏 + 上限

未授权的表、PII列、过大的结果、失控的查询

每条执行的语句都会附加到审计日志中,包含行数和执行时长,以便数据所有者准确查看模型请求的内容。

2026-08-18T11:22:41  6 rows in 1ms       SELECT country, COUNT(*) FROM customers GROUP BY 1 LIMIT 201
2026-08-18T11:22:44  error: rejected     DELETE FROM customers

暴露的工具

工具

用途

list_tables()

可读表 + 行数

describe_table(table)

列、类型、哪些被脱敏、3个示例行

run_sql(sql)

一个只读的 SELECT,有上限和审计

search(table, column, term, limit)

无需编写SQL即可进行子串搜索

summarize_column(table, column)

空值、不同值计数、最小/最大值、前5个值

此外还有一个 schema://tables 资源,客户端无需调用工具即可加载整个模式。

快速开始

git clone https://github.com/dkautomation23/mcp-data-server.git
cd mcp-data-server
python -m venv .venv && . .venv/bin/activate      # Windows: .venv\Scripts\activate
pip install -r requirements.txt

python -m mcp_data_server.seed                    # creates demo.db
cp .env.example .env                              # then point DATABASE_PATH at your file
python -m mcp_data_server                         # serves over stdio

Python 3.10+。演示数据库包含 customers、orders、order_items 以及一个故意敏感的 internal_notes 表,用于展示白名单如何阻止访问。

连接到Claude Desktop

添加到 claude_desktop_config.json(完整示例见 examples/claude_desktop_config.json):

{
  "mcpServers": {
    "business-data": {
      "command": "python",
      "args": ["-m", "mcp_data_server"],
      "cwd": "C:/path/to/mcp-data-server",
      "env": {
        "DATABASE_PATH": "C:/path/to/your.db",
        "ALLOWED_TABLES": "customers,orders,order_items",
        "MASKED_COLUMNS": "customers.email,customers.phone"
      }
    }
  }
}

连接到Claude Code

claude mcp add business-data -- python -m mcp_data_server

会话示例

运行中服务器的实际输出(完整记录见 examples/demo_session.md):

// run_sql("SELECT status, COUNT(*) n, ROUND(SUM(total_eur)) revenue FROM orders GROUP BY 1 ORDER BY 3 DESC")
{
  "sql": "SELECT status, COUNT(*) n, ROUND(SUM(total_eur)) revenue FROM orders GROUP BY 1 ORDER BY 3 DESC LIMIT 201",
  "columns": ["status", "n", "revenue"],
  "rows": [["paid", 92, 149914.0], ["pending", 39, 64596.0], ["refunded", 31, 45911.0]],
  "row_count": 3, "truncated": false, "elapsed_ms": 0
}

// run_sql("DELETE FROM customers")
{ "error": "only SELECT (or WITH ... SELECT) statements are allowed" }

// run_sql("SELECT * FROM internal_notes")
{ "error": "table 'internal_notes' is not in the allowlist (allowed: customers, orders, order_items)" }

// run_sql("SELECT id, name, email FROM customers LIMIT 2")
{ "rows": [[1, "Customer 001", "***"], [2, "Customer 002", "***"]] }

配置

变量

默认值

用途

DATABASE_PATH

demo.db

要暴露的SQLite文件(始终以只读方式打开)

ALLOWED_TABLES

全部

逗号分隔的白名单;其他表不可见

MASKED_COLUMNS

–

table.column 列表,每个结果中替换为 ***

MAX_ROWS

200

每次调用的硬性上限;超出结果标记为 truncated

QUERY_TIMEOUT_SECONDS

10

更长的查询将被取消

AUDIT_LOG_PATH

audit.log

每条语句的追加日志;为空则禁用

测试

pytest -q
...............................                                          [100%]
31 passed in 1.77s

三层测试:SQL防护(注入、第二条语句、注释走私、禁止表)、针对真实种子文件的数据库层(包括SQLite自身拒绝的写入尝试),以及七项通过实际MCP协议驱动服务器的测试——与桌面客户端执行的相同握手、list_tools 和 call_tool 流程。

适配客户端技术栈

  • Postgres / MySQL:将 db.py 中的连接替换为池化驱动和 SET TRANSACTION READ ONLY 会话;验证层保持不变。

  • 业务特定工具:在 server.py 中添加带有 @mcp.tool() 的函数——一个命名良好的 top_customers(period) 比让模型编写SQL更好。

  • HTTP传输 替代 stdio:mcp.run(transport="streamable-http"),然后将其置于您自己的认证之后。

许可证

MIT — 参见 LICENSE。

Related MCP Connectors

Related MCP Servers

  • F
    license
    A
    quality
    D
    maintenance
    Enables read-only exploration and querying of PostgreSQL or MySQL databases via MCP, with schema discovery, safe SQL validation, natural language to SQL conversion, and CSV export.
    11
    1
    -
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables governed, agent-agnostic data exploration by allowing users to ask natural language questions through MCP-compatible agents, executing safe, permission-scoped queries against data sources and returning interactive charts.
    3 npm
    Apache 2.0
  • A
    license
    Not graded
    quality
    A
    maintenance
    Provides a secure, policy-driven read-only gateway that exposes multiple databases to AI tools via MCP, with governed select, search, and aggregate operations and no free-form SQL.
    Apache 2.0
  • F
    license
    Not graded
    quality
    C
    maintenance
    Enables natural-language querying of Postgres and MongoDB databases in read-only mode, with PII masking and row/timeout guardrails, from any MCP client.
    -