AegisQuery
A governed analytics MCP server that provides LLM agents with safe, read-only access to data warehouses through a layered safety pipeline including AST validation, column/row governance, PII masking, cost limits, and audit.
README
AegisQuery — a governed analytics MCP server
A production-grade Model Context Protocol server that gives LLM agents safe, read-only access to an analytics data warehouse — mediated by a layered safety pipeline (AST validation, column/row governance, PII masking, cost limits, result truncation, audit) over both stdio and an OAuth-secured Streamable HTTP transport.
Built in TypeScript against the official @modelcontextprotocol/sdk, targeting MCP spec revision 2025-11-25 and designed stateless-first for the direction the protocol is heading.
Its differentiator is the security story: it reproduces the class of bug that defeated Anthropic's own reference Postgres MCP server — a stacked-statement injection (COMMIT; DROP SCHEMA public CASCADE;) that bypassed a naive READ ONLY transaction — and then defeats it with structural AST validation plus defense-in-depth. See docs/ for the full SRS / HLD / LLD / Design documents.
🔴 Live demo
web-beta-rosy-0ovpsr1q8m.vercel.app — a coffee-themed web console (light + dark) that runs the same governance engine on Vercel serverless. Switch roles (analyst / regional / finance / admin) and watch the same query behave differently: PII masked for an analyst but raw for an admin, cost denied for an analyst but visible to finance, and adversarial SQL rejected on the spot. Source in web/.
Quickstart
npm install
npm run seed # build a local SQLite warehouse (deterministic synthetic data)
npm run smoke # end-to-end verification incl. an adversarial SQL corpus
npm run eval # golden-task scorecard vs an independent oracle
# run the server
npm run build && npm start # stdio (local, env-key auth)
npm start -- --http # Streamable HTTP (remote, bearer/OAuth auth)
Point an MCP client (e.g. Claude Desktop, MCP Inspector) at node dist/index.js, with AEGIS_API_KEY=devkey in the environment.
What it exposes
Tools (workflow-oriented, not endpoint mirrors):
| Tool | Purpose |
|---|---|
search_catalog |
Rank tables/columns relevant to a natural-language question (with access levels). |
revenue_by_period |
Curated metric — gross/net revenue by day/week/month. No model-authored SQL. |
preview_query_cost |
Validate a query and return plan + cost + violations without executing. |
run_analytics_query |
Governed ad-hoc read-only SELECT. Full safety pipeline applies. |
Resources — the warehouse schema (aegis://catalog, aegis://schema/{table}), annotated per-policy with allow / mask / deny.
Prompts — analyze_question, steering agents to discover schema, prefer curated metrics, and treat results as data.
The safety pipeline
Every query passes the same ordered stages before a single row is returned:
parse (AST) → read-only guard → reference extraction → table/column allowlist
→ PII masking → row-policy injection → cost guard (LIMIT/scan budget)
→ execute (read-only role) → truncate to token budget → audit
- AST read-only guard — only a single
SELECT/WITH/UNIONpasses; multiple statements parse to an array and are rejected. This structurally prevents the stacked-statement bypass. - Defense in depth — the SQLite connection is opened read-only, so even a parser gap cannot mutate data.
- Governance — deny-by-default table/column allowlists; PII columns masked (
substr(x,1,2)||'***') and refused if used outside the projection; restricted columns (cost, url) require an elevated role. - Row-level policy — mandatory
WHEREpredicates injected from principal attributes (e.g. aregional_analystis scoped to their region). - Cost & context — planner-based scan estimate rejects runaway queries; every result is
LIMIT-capped and truncated to a ~25k-token budget. - Audit — one structured JSON record per invocation (principal, raw + rewritten SQL, decision, rows, ms).
Auth
- stdio — credentials from the environment (
AEGIS_API_KEY), per the spec (stdio servers should not use OAuth). - HTTP — bearer token validated and mapped to a role/policy; unauthenticated requests get
401with RFC 9728 Protected Resource Metadata. Stateless (no session id) so it scales behind a round-robin load balancer.
Key map format (AEGIS_API_KEYS): key:role[:attr=val;attr=val], e.g. devkey:analyst, eu-key:regional_analyst:region=EU, admin-key:admin.
Configuration
| Env var | Default | Meaning |
|---|---|---|
AEGIS_TRANSPORT / --http |
stdio |
Transport selection. |
AEGIS_DB_PATH |
data/warehouse.db |
SQLite warehouse path. |
AEGIS_MAX_ROWS |
200 |
Hard row cap (injected LIMIT). |
AEGIS_MAX_SCAN_ROWS |
500000 |
Reject queries whose plan exceeds this. |
AEGIS_RESPONSE_TOKEN_BUDGET |
25000 |
Result truncation budget. |
AEGIS_API_KEYS |
devkey:analyst |
Principal key map. |
AEGIS_HTTP_PORT |
3333 |
HTTP bind port. |
Architecture
transports/{stdio,http} → server (McpServer) → tools ─┐
│ ├─ pipeline ─ sql/analyzer (AST)
auth (principal) │ policy (governance)
catalog (resources) │ db/adapter → sqlite
└─ audit
The warehouse sits behind a read-only ReadOnlyAdapter interface — SQLite locally, Postgres in production — so governance logic never touches a specific engine.
Learning-ordered roadmap
This repo is built as a learning vehicle; each milestone is independently demoable and teaches one MCP / AI-engineering competency.
| M | Build | Learn |
|---|---|---|
| M0 | stdio server + a tool | JSON-RPC lifecycle, tools/list·call |
| M1 | read-only query + schema resources | resources vs tools, zod I/O, structured content |
| M2 | reproduce the CVE → AST + read-only enforcement | why appsec applies; defense-in-depth |
| M3 | allowlist, PII masking, row policy | data governance |
| M4 | cost guard, truncation, pagination | context engineering, token economics |
| M5 | curated metrics + catalog search | Anthropic tool-authoring principles |
| M6 | audit log, error taxonomy | production ops/observability |
| M7 | Streamable HTTP + OAuth 2.1, stateless | remote MCP, auth |
| M8 | golden-task eval harness | eval — the top hiring signal |
Verification
npm run typecheck # strict TS, src + scripts
npm run smoke # 20 assertions incl. 9 adversarial rejections + integrity check
npm run eval # dev + held-out scorecard (accuracy, tokens, latency, errors)
Documentation
Full design suite (generated, colorful .docx) in docs/: SRS, HLD, LLD, Design. Regenerate with npm run docs. All four are driven by a single source of truth in scripts/docgen/spec.ts.
License
MIT.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。