mcpgov

mcpgov

A production-grade MCP server over Postgres, providing secure data operations with tenant isolation, exact-once mutations, loop-aware rate limiting, and a tamper-evident audit trail.

Category
访问服务器

README

mcpgov

A production-grade MCP server over Postgres, where the hard 20% is the point: authentication, tenant isolation, provably idempotent mutations, loop-aware rate limiting, and a tamper-evident audit trail — each claim pinned by a test that runs against a real database, and an end-to-end adversarial demo that attacks the live server over real HTTP in CI.

Why

Every company is wrapping internal systems in MCP servers so agents can use them. Most wrappers are demos: the tools work, and nothing stops a retried mutation from applying twice, a token from reading another tenant's rows, or an agent loop from hammering the same failing call all night. This repo is the missing 80-to-100 stretch, built as five controls that a tool author cannot forget, because they live in the middleware chain and the database rather than in tool bodies.

The controls, and the evidence for each

1. Identity is verified, not asserted. OAuth-style short-lived Bearer tokens (HS256, exp/aud/iss/jti all required), plus RFC 7523 jwt-bearer federation: an IdP-issued assertion is exchanged for a local token after signature-by-kid, audience, issuer, expiry-with-bounded-skew, and single-use jti checks. Group-to-team mapping is a declarative allowlist; an unmapped group grants nothing, including a group that happens to be named after a real team. 19 tests, including forged signatures, alg=none, replayed assertions, and cross-audience confusion.

2. Tenant isolation is enforced by the database, not the queries. Postgres row-level security with FORCE, keyed on a GUC populated only from verified token claims — there is no request field through which a client names a tenant. The server runs as a role that is neither owner nor superuser (either would bypass RLS silently; a test asserts this about the running role). Teams are jsonb, not CSV, after measuring that CSV encoding let a principal entitled to gamma,delta read the distinct team literally named gamma,delta. Cross-tenant reads return not_found byte-identical to genuinely absent ids.

3. Mutations are exactly-once under retry storms. A claim-then-execute ledger: the idempotency claim and the business write commit in one transaction, duplicates replay the stored response marked _replayed, a key reused with different arguments is refused, and only non-retryable failures are cached (caching a transient one would convert a blip into a permanent failure that looks healthy). Pinned by a 16-thread concurrent-duplicates test repeated 5 times, plus crash-recovery: an unclean death does not poison the key.

4. Rate limiting distinguishes a runaway loop from a legitimate burst. Two layers, because "too fast" and "stuck" are different problems: a GCRA shaper per (principal, tool_class) that delays, and a loop breaker that looks for repetition without progress and opens a per-tool circuit. On the seeded evaluation (200 trials/family, reproduced in CI byte-for-byte):

workload outcome
5 runaway families (same-error, cycle, no-op write, infinite transient retry, idempotent replay) broken in 100% of trials, median 8 wasted calls
6 legitimate families (poll, paginate, fan-out, backoff retry, burst, small worklist) 0 false breaks
declared long poll bounded by its declared budget, by design
undeclared poll known false positive, documented — without a declaration it is indistinguishable from a no-progress loop
token-bucket baseline denies 41/80 of the runaway and 41/80 of the legitimate burst at identical arrival rates — its verdict is a function of rate, the label is a function of shape; it never breaks a loop, only slows it

5. The audit trail is tamper-evident, including truncation. Every attempt — allowed, denied, unauthenticated — is one row in an HMAC hash chain, with arguments redacted to digests. The chain head lives in a singleton row read under FOR UPDATE (the naive ORDER BY seq DESC LIMIT 1 head forked under concurrency: 16 concurrent appends produced 9 distinct predecessors, and verification cried tamper on clean traffic). Verification requires a separate auditor login the server never holds, because the writer being unable to read the whole log — and the reader being unable to write — is what makes "the chain verified" a statement about the data rather than the writer's self-report. Deleting the tail is detected too, which hash-chaining alone cannot see.

The demo: the live server, attacked over real HTTP

demos/session.py boots mcpgov serve, mints tokens with the CLI, and drives 13 acts through the official MCP client — no token (401 with the RFC 9728 challenge), forged signature, scope escalation, a retry storm of duplicated mutations, key reuse with different arguments, cross-tenant probing, a runaway loop broken on attempt 7 while other tools keep answering, tenant-scoped audit reads, chain verification, and a superuser rewriting one row's deny to allow — caught with the exact seq. Each act asserts its expected outcome; CI fails if any deviates. Transcript: results/demo-session.json.

To point Claude Code or Cursor at it interactively: docs/clients.md.

Run it

Needs Python 3.12+, uv, and any Postgres 14+.

uv sync

# the full suite: 100 tests, most against the real database
MCPGOV_TEST_DSN='postgresql://postgres@127.0.0.1:5432/postgres' uv run pytest

# the adversarial demo (creates its own scratch database)
MCPGOV_DEMO_DSN='postgresql://postgres@127.0.0.1:5432/mcpgov_demo' \
  uv run python demos/session.py

# the limiter evaluation (seeded; CI diffs the output against the committed file)
uv run python scripts/eval_limiter.py

Design notes worth stealing

  • Controls in middleware, not tool bodies. A check inside a tool is a check a new tool can forget, silently. The guard wraps every inbound message, so it also sees calls to tools that do not exist and calls that fail schema validation — attempts worth auditing that a per-tool check never sees.
  • Middleware sees the wire format. call_next returns a JSON-RPC dict (isError, camelCase), not the typed CallToolResult. The attribute spelling returned its default forever: every refusal was audited as allow, and loop detection was structurally dead in the live server while the offline harness scored it at full recall. The tests now drive a real client session.
  • INSERT ... RETURNING re-evaluates the SELECT policy on the new row — so the unscoped audit appender could not record tenant-tagged events at all.
  • Permissive RLS policies apply by role membership, not the active role. Granting the app role membership of the auditor role (the convenient wiring) switched the full-read policy on for every app query and removed the tenant boundary from audit reads without a single error.
  • Retryability is declared once, per error code, and consulted by both the idempotency cache and the loop breaker's thresholds — a permanent failure misfiled as transient would let a runaway run 20 calls instead of 6.

Limitations, honestly

  • The IdP in the federation tests is a local fixture with published keys, not a live Okta/Entra tenant; the validation logic is real, the network hop is not.
  • LocalTokenVerifier is symmetric-key (HS256) — right for a single-server deployment, not for a fleet where issuers and verifiers must not share a secret.
  • Loop detection keys on the verified (principal, client_id, tool); a principal that can provision many client_ids can fan out across buckets. That is a provisioning-quota problem, stated rather than solved.
  • The GitHub write path (src/mcpgov/github.py) is at-least-once made effectively-once by reconciliation — exactly-once across two systems with no shared transaction does not exist, and its list endpoint lags a successful create by ~5-6s (measured), which is exactly why its reconciler polls past the lag and refuses to create after a short-deadline miss. Its suite runs against a recorded fake with an offline guard test.

Layout

src/mcpgov/
  auth.py         tokens, RFC 7523 assertion exchange, group->team mapping
  db.py           pool, tenant-scoped connections, migration + grants
  schema.sql      tables, FORCE RLS policies, the audit chain head
  idempotency.py  the claim-then-execute ledger
  limiter.py      GCRA shaper + loop breaker (the two-layer argument)
  audit.py        HMAC hash chain: append, verify, truncation detection
  server.py       the five tools and the guard middleware
  github.py       writing to a second system that has no idempotency
  cli.py          migrate / seed / token / serve / verify-audit
tests/            100 tests; Postgres-backed ones run against a real database
demos/session.py  the 13-act adversarial session over real HTTP
scripts/          the seeded limiter evaluation
results/          committed evidence: eval numbers, demo transcript

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选