policymesh
PolicyMesh is a governed agentic data API that enforces SQL validation, role-based access control, PII masking, and audit logging at the MCP tool boundary, letting users query governed data through natural language with three MCP tools: query_sql, search_docs (pgvector semantic search), and lookup_metadata (role-filtered schema discovery).
README
PolicyMesh — Governed Data Intelligence
Every query is SQL-validated, access-controlled by role, PII-masked, and audit-logged before a single row is returned. Governance lives at the MCP tool boundary — enforced in code, not in the prompt.
What it does
PolicyMesh is a governed agentic data API. A user asks a natural-language question; an LLM agent decides which tool to call; every tool call passes a server-side enforcement pipeline before touching data.
User question
│
▼
POST /ask ──► Agent loop (LLaMA 3.3 70B via Groq)
│
▼ picks a tool
┌───────────────────────────────────────┐
│ MCP Tool Boundary │
│ │
│ query_sql ──► parse (SQLGlot) │
│ allowlist check │
│ RBAC check │
│ EXPLAIN dry-run │
│ read-only execute │
│ PII mask │
│ audit log ─────────┼──► audit_log table
│ │
│ search_docs ──► embed query │
│ pgvector cosine │
│ build_context [N] │
│ audit log ─────────┼──► audit_log table
│ │
│ lookup_metadata ► role-filtered │
│ schema discovery │
│ audit log ─────────┼──► audit_log table
└───────────────────────────────────────┘
│
▼
Grounded, cited answer
System design
graph TD
U([User]) -->|POST /ask| API[FastAPI]
API -->|run_agent| LOOP[Agent Loop\nLLaMA 3.3 via Groq\nMAX_STEPS=5]
LOOP -->|tool_call| BOUNDARY
subgraph BOUNDARY [MCP Tool Boundary — app/policy.py + app/sql_validator.py]
P[check_access\nrole → tables] --> V[validate_and_run\nparse → allowlist → EXPLAIN → read-only]
V --> M[mask_rows\nPII sentinel ***]
S[search_raw\npgvector cosine] --> C[build_context\nnumbered citations]
META[get_metadata\ninformation_schema + pg_class]
end
M -->|rows| AUDIT[audit.log_call\nfinally block]
C -->|context| AUDIT
META -->|schema| AUDIT
AUDIT -->|INSERT| DB[(audit_log\nappend-only)]
V -->|SELECT| PG[(Neon Postgres\n5 tables)]
S -->|<=> cosine| PG
META -->|system tables| PG
LOOP -->|answer| API
API -->|JSON| U
Why this design
Governance at the boundary, not the prompt. Prompt rules can be jailbroken, ignored, or forgotten across model upgrades. Every access-control decision in PolicyMesh is enforced server-side in app/policy.py — the model never sees data it shouldn't.
Four SQL guards, cheapest first.
parse()— SQLGlot AST; must be exactly one SELECT. Rejects DDL/DML before any network call.check_allowlist()— everyexp.Tableandexp.Columnnode must be in the schema allowlist. Includes scope-alias fix soORDER BY total_revenuepasses whentotal_revenueis a SELECT alias.dry_run()—EXPLAINagainst live DB catches type mismatches the AST walk misses.execute_readonly()—SET TRANSACTION READ ONLYblocks data-modifying CTEs at the engine level.
Why a custom agent loop, not LangChain/LangGraph.
The loop is 80 lines with explicit termination conditions, state as a flat messages list, and a hard step cap. LangGraph adds ~400 lines of framework surface area, a new graph DSL to explain, and hides the termination logic inside abstractions. For a governed system, the control flow must be auditable — a while True is auditable; a graph runtime is not.
Two eval axes (per the CLAUDE.md spec):
- Tool-selection accuracy: 100% (6/6, threshold 83%)
- Groundedness: 80% (LLM-as-judge via Groq, threshold 80%)
Tech stack
| Layer | Choice | Why |
|---|---|---|
| API | FastAPI (async) | Non-blocking I/O for DB + embedding calls |
| LLM agent | LLaMA 3.3 70B (Groq) | Free tier, tool calling, OpenAI-compatible API |
| Embeddings | Gemini embedding-001 | Free tier, 3072-dim, strong semantic quality |
| Database | Neon (serverless Postgres) | pgvector built-in, free tier, matches local dev |
| Vector search | pgvector <=> cosine |
Native Postgres, no separate vector DB needed |
| Tool protocol | MCP 2.0 (SSE transport) | Industry standard for AI tool exposure |
| SQL parsing | SQLGlot | Typed AST, handles aliases, CTEs, subqueries |
| Tests | pytest + pytest-asyncio | 43 unit/integration tests + 2-axis live eval |
Running locally
# Clone and install
git clone https://github.com/deepakmeena61/policymesh
cd policymesh
python -m venv .venv && .venv/bin/pip install -r requirements.txt
# Configure (get free keys at neon.tech, console.groq.com, aistudio.google.com)
cp .env.example .env
# Fill in DATABASE_URL, GROQ_API_KEY, GOOGLE_API_KEY
# Seed database
.venv/bin/python -m app.seed # 5 tables, 25 customers, 67 orders
.venv/bin/python -m app.seed_docs # 6 knowledge-base documents with embeddings
# Start
.venv/bin/uvicorn app.main:app --port 8000 --reload
# Open http://localhost:8000
Running tests
# Unit + integration tests (no API keys needed for unit tests)
.venv/bin/pytest tests/test_policy.py tests/test_sql_validator.py -v
# Audit integration tests (needs DATABASE_URL)
.venv/bin/pytest tests/test_audit.py -v
# Live evals (needs GROQ_API_KEY + GOOGLE_API_KEY)
.venv/bin/pytest tests/test_eval.py -v -m eval -s
Project structure
app/
├── main.py # FastAPI app — /ask, /explore, /audit, /health, env validation
├── agent.py # Agent loop: tool selection, MAX_STEPS cap, messages state, LLM retry
├── mcp_server.py # MCP 2.0 SSE server — 3 tools: query_sql, search_docs, lookup_metadata
├── policy.py # ← ALL governance: ROLE_POLICY, check_access(), mask_rows(), ABAC sketch
├── sql_validator.py # 4-guard SQL pipeline + scope-alias fix for ORDER BY/HAVING/CTE aliases
├── audit.py # Append-only audit log — log_call() context manager fires in finally
├── metadata.py # Role-filtered schema discovery (information_schema + pg_class)
├── docs.py # pgvector cosine search + build_context() with [N] citation markers
├── embed.py # Google Gemini embedding wrapper (asyncio.to_thread, singleton client)
├── explore.py # Data exploration API — same policy masking as MCP tools
├── seed.py # 5 tables, 25 customers, 67 orders, 12k events, 28 tickets (--reset flag)
└── seed_docs.py # 6 KB docs with Gemini embeddings (--reset flag)
tests/
├── test_policy.py # 15 unit tests — RBAC + PII masking
├── test_sql_validator.py # 23 unit tests — 4 guards + scope-alias edge cases
├── test_audit.py # 4 integration tests — hits real Neon DB
└── test_eval.py # 2-axis eval: tool-selection accuracy + groundedness (LLM-as-judge)
Demo scenarios
| Role | Query | What to observe |
|---|---|---|
analyst |
"Who are our top customers by spend?" | email shows *** in MCP enforcement trace |
viewer |
"List all customers" | MCP boundary: RBAC ✗ BLOCKED |
analyst |
"What SLA do enterprise customers get?" | search_docs + [1] citations in answer |
admin |
"What data do I have access to?" | lookup_metadata returns full schema + row counts |
analyst |
"Open critical support tickets?" | tickets.subject shows restricted — PII policy |
| any | Click Explore tab | Role-filtered schema + sample rows + stats per column |
| any | Click Audited pill | Live feed of every tool call with role, latency, row count |
Known limitations / production delta
| Gap | Current state | Production fix |
|---|---|---|
| Auth | caller_role is a POST body param — any caller can claim any role |
Extract role from verified JWT at FastAPI middleware; policy.py doesn't change |
| ABAC | RBAC only (role → tables/columns) | Extend to ABAC via a policy DB table with subject attributes + resource sensitivity tags — sketch in policy.py |
| Alias PII bypass | SELECT email AS contact renames the column, bypassing mask_rows |
Column-level GRANT on a read-only Postgres role — the DB refuses the query regardless of aliasing |
| Dynamic roles | ROLE_POLICY is startup config |
Move to DB-backed policy table; make check_access() async with short cache |
| Data lineage | Not tracked | Extend audit_log with a lineage JSONB column recording upstream table dependencies per query |
| Warehouse scale | Neon Postgres only | search_raw() in docs.py is the only pgvector call — swap for Databricks Vector Search / Pinecone without touching governance layer |
| LLM provider | Groq free tier (100k tokens/day) | Drop-in swap via AGENT_MODEL env var; governance is LLM-agnostic |
Built as a portfolio project demonstrating MCP, governed data access, agentic retrieval, and two-axis eval — the core skills for AI platform engineering on data-mesh infrastructure.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。