tai42-dynamic-postgres-mcp

tai42-dynamic-postgres-mcp

Generates safe, scoped PostgreSQL DML tools for FastMCP agents, enabling controlled database interactions without raw SQL.

Category
访问服务器

README

tai42-dynamic-postgres-mcp

CI License: Apache 2.0

Schema-driven generator for safe, scoped PostgreSQL DML tools in FastMCP agent systems.

Point it at a PostgreSQL database and it introspects the schema and generates one typed MCP tool per DML operation per table — insert, select, update, delete, plus optional select_joined. The agent gets exactly those tools and nothing else: no raw SQL, no schema changes, no access to tables you didn't expose.

Why

Giving an agent database access usually means choosing between read-only access or risking that the agent runs arbitrary SQL and changes data or settings you never intended. This project gives you a controlled middle ground: a fixed set of generated, validated, parameterized tools scoped to the tables and operations you choose.

What it does

  • Introspects your schema (tables, columns, types, foreign keys, pgvector).
  • Generates a FastMCP tool per DML operation per table, each with a dedicated Pydantic input model derived from the table.
  • Supports rich, injection-safe filtering (WhereFilter) and ordering, including vector KNN search.
  • Excludes chosen columns from generated tool inputs (e.g. id, created_at).
  • Exposes only the generated tools — never raw SQL or DDL.

Security model

[!IMPORTANT] The generated tool layer controls which operations exist; the PostgreSQL role you connect as controls what those operations can physically do. Connect as a dedicated least-privilege role, never a superuser. Read SECURITY.md before deploying.

Highlights, in full detail in SECURITY.md:

  • Filter/order field names are validated against real columns and emitted only as quoted identifiers; values are always bound parameters. There is no path for SQL injection through field names.
  • update/delete require a WHERE filter unless the server is started with --allow-unfiltered.
  • The tool layer does not provide row-level scoping. Use PostgreSQL Row-Level Security and GRANT/REVOKE for that.

Installation

Run directly from Git with uvx, or install into an environment:

uvx --from git+https://github.com/tai42ai/tai-dynamic-postgres-mcp.git tai42-postgres-mcp
# or
pip install git+https://github.com/tai42ai/tai-dynamic-postgres-mcp.git

Configuration

Connection and pooling are configured via environment variables:

Variable Default Description
PG_HOST localhost PostgreSQL host
PG_PORT 5432 PostgreSQL port
PG_DB required Database name (no default; startup fails if unset)
PG_USER required Database user, use a least-privilege role (no default)
PG_PASSWORD required Database password (no default; startup fails if unset)
PG_STATEMENT_TIMEOUT 30000 Per-connection statement_timeout in ms (0 disables)
PG_POOL_MIN_SIZE 1 Minimum pooled connections
PG_POOL_MAX_SIZE 10 Maximum pooled connections
PG_POOL_TIMEOUT 10 Pool acquire timeout (seconds)
PG_POOL_MAX_LIFETIME 300 Max connection lifetime (seconds)
TOOLS_DIR ~/.cache/tai42-dynamic-postgres-mcp/tools Where generated tool files are written

CLI options

Flag Default Description
--overwrite / --no-overwrite on Regenerate the tool files on startup so they reflect the current schema. Pass --no-overwrite to reuse existing generated files
--readonly off Generate only select/select_joined tools
--allow-unfiltered off Allow update/delete to run without a WHERE filter (affects every row)
--select-joined a,b,c Generate a joined select over the given tables (repeatable)
--ignore-insert-column id Column to exclude from insert inputs (repeatable)
--ignore-update-column id Column to exclude from update inputs (repeatable)
--ignore-select-column Column to exclude from select output models (repeatable)
--ignore-select-joined-column Column to exclude from joined select models (repeatable)
-t, --transport stdio stdio, http, sse, or streamable-http
--host 127.0.0.1 Bind host (HTTP/SSE transports only)
--port 8000 Bind port (HTTP/SSE transports only)

Usage with an MCP client

{
  "mcpServers": {
    "postgres": {
      "command": "uvx",
      "args": [
        "--from",
        "git+https://github.com/tai42ai/tai-dynamic-postgres-mcp.git",
        "tai42-postgres-mcp",
        "--readonly"
      ],
      "env": {
        "PG_HOST": "localhost",
        "PG_PORT": "5432",
        "PG_DB": "dbname",
        "PG_USER": "agent",
        "PG_PASSWORD": "password"
      }
    }
  }
}

Generated tools

For a table public.orders you get (unless --readonly):

  • select_public_orders(where, order_by, limit, offset)
  • insert_public_orders(params, raise_on_conflict)
  • update_public_orders(data, where)
  • delete_public_orders(where)

Column types map to native Python: temporal columns to datetime/date/time, uuid to uuid.UUID, numeric/decimal to Decimal, json/jsonb to Any, and array columns to list[...]. insert returns the table's real primary key (a scalar list for a single-column key, a list of lists for a composite key, or the affected row count when the table has no primary key); columns with a database default are omittable. order_by items accept an optional nulls (FIRST/LAST); when unset PostgreSQL's default applies.

Filtering — WhereFilter

select, update, and delete accept a where argument. Field names must be real columns of the table; unknown fields are rejected.

// Simple field filters (implicitly ANDed)
{ "status": { "eq": "open" }, "total": { "gte": 100 } }

// Logical composition
{ "AND": [ { "status": { "eq": "open" } },
           { "OR": [ { "total": { "gt": 1000 } }, { "vip": { "eq": true } } ] } ] }

Supported operators: eq, ne, gt, gte, lt, lte, like, not_like, ilike, not_ilike, in, not_in, between, is_null, and knn (pgvector). Logical keys: AND, OR, NOT.

Vector search (pgvector)

When a column is a vector, filter or order by similarity:

{ "embedding": { "knn": { "query": [0.1, 0.2, 0.3],
                          "distance": "cosine",   // l2 | inner_product | cosine
                          "threshold": 0.5 } } }

Requires the pgvector extension enabled in the database.

Docker

docker build -t tai42-postgres-mcp .
docker run --rm -e PG_HOST=... -e PG_DB=... -e PG_USER=... -e PG_PASSWORD=... \
  tai42-postgres-mcp tai42-postgres-mcp --readonly

Development

See CONTRIBUTING.md.

uv sync --extra dev --extra test-integration
uv run ruff check .
uv run ruff format --check .
uv run pyright
uv run pytest                 # unit tests
uv run pytest -m integration  # CRUD tests against real Postgres (needs Docker)
uv run --group docs mkdocs build --strict

License

Apache-2.0. See LICENSE and NOTICE.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选