Skip to main content
Glama
agentic-catalog

agentic-catalog

by Renato425636
README.md
# agentic-data-catalog

[![ci](https://github.com/Renato425636/agentic-data-catalog/actions/workflows/ci.yml/badge.svg)](https://github.com/Renato425636/agentic-data-catalog/actions/workflows/ci.yml)
![Python](https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13-3776AB?logo=python&logoColor=white)
![MCP](https://img.shields.io/badge/Model_Context_Protocol-server-000000)
![License](https://img.shields.io/badge/license-MIT-informational)

A metadata catalog that AI agents can **search, understand and query safely**, served over the Model Context Protocol (MCP). The agent finds the right table from business words, reads what each column means and who owns it, and runs SQL through a gateway that enforces the same column-level permissions a human analyst has.

Everything here is a from-scratch reference implementation on a synthetic warehouse (DuckDB). It reflects a pattern I work with: using the catalog, not a prompt, as the context layer for agents.

## The problem

Give an LLM agent a SQL connection and a list of table names, and three things go wrong:

1. **It picks the wrong table.** `raw.orders_events` and `analytics.fct_orders` both "have orders"; only one is deduplicated and safe for revenue.
2. **It does not know what columns mean.** Is `total_brl` gross or net? Which time zone is `order_date`?
3. **It reads what it should not.** Nothing stops `SELECT email FROM customers` except a hope that the prompt said not to.

The fix for all three is the same: put meaning, lineage and access rules in a catalog, and make the agent go through it.

## How it works

```mermaid
flowchart LR
  A[AI agent] -- MCP --> T1
  subgraph S[MCP server, runs as one principal]
    T1[search_tables] --> C[(catalog.yaml: descriptions, owners, lineage, sensitivity per column)]
    T2[describe_table] --> C
    T3[get_lineage] --> C
    T4[run_sql] --> G[Query gateway]
    G -- parse with sqlglot --> P{policy check per column}
    P -- clear --> W[(DuckDB warehouse)]
    P -- masked --> R[rewrite to sha256] --> W
    P -- denied --> X[reject with reason]
  end
```

| Tool | What the agent gets |
|---|---|
| `search_tables(query)` | Ranked tables (BM25 over names, descriptions and column docs), with curated marts boosted over raw logs |
| `describe_table(table)` | Columns, meaning, owner, freshness, quality rules, and **the caller's access to each column** |
| `get_lineage(table)` | Upstream sources and downstream consumers |
| `run_sql(sql)` | Rows, with masked columns hashed; a clear error when a column is denied |

## Security model

The gateway works on the **parsed SQL tree**, not on text, so the common bypasses fail:

| Attempt | Result |
|---|---|
| `SELECT * FROM crm.customers` | `*` is expanded to real columns; `cpf`, `email`, `full_name` come back hashed |
| `SELECT c.email AS contact ...` | the alias keeps its name, the value is still hashed |
| `... WHERE email = 'x@y.com'` on a masked column | rejected: filtering on a masked column would leak it through comparisons |
| `SELECT AVG(credit_limit) ...` as an analyst | rejected: `restricted` column, no masked grant |
| `DROP`, `DELETE`, `INSERT`, two statements | rejected: one read-only `SELECT` only |
| `information_schema.tables` | rejected: only catalogued tables are queryable |
| A query with no `LIMIT` | capped at 200 rows |

The principal comes from the `CATALOG_PRINCIPAL` environment variable set by whoever launches the server, never from the model. An agent cannot talk its way into more access.

### Same table, three agents

`describe_table("analytics.dim_customer")`:

| Column | `analyst-agent` | `support-agent` | `finance-agent` |
|---|---|---|---|
| `customer_id` | clear | clear | clear |
| `email` (pii) | masked | clear | masked |
| `credit_limit` (restricted) | denied | denied | clear |

## Run it

```bash
pip install -e ".[dev]"
pytest

# start the MCP server over stdio as the analyst agent
CATALOG_PRINCIPAL=analyst-agent agentic-catalog
```

To use it from an MCP client such as Claude Desktop or Claude Code, register the command:

```json
{
  "mcpServers": {
    "data-catalog": {
      "command": "agentic-catalog",
      "env": { "CATALOG_PRINCIPAL": "analyst-agent" }
    }
  }
}
```

Then ask: *"What was the cancellation rate by state last month?"* The agent searches, finds `analytics.fct_orders` and `analytics.dim_customer`, reads that they join on `customer_id`, and queries them without ever seeing an e-mail.

## Design decisions

| Decision | Alternative rejected | Why |
|---|---|---|
| Catalog as code (YAML), reviewed in pull requests | Metadata edited in a UI | Descriptions and sensitivity change with the schema, in the same diff |
| BM25 search | Embeddings from day one | Deterministic and testable: the suite asserts "revenue by order date" ranks `analytics.fct_orders` first. Embeddings can sit behind the same interface later |
| Mask by hashing at query time | Separate masked copies of tables | One copy of the data; the masking decision depends on who is asking |
| Reject a query that touches a denied column | Silently drop the column | The agent gets a reason it can act on, instead of a misleading result |

## Synthetic warehouse

`warehouse.py` generates customers, credit limits and order events from a fixed seed, including duplicate events to mimic at-least-once delivery. `analytics.fct_orders` deduplicates them, and a test asserts it.

## Quality

30 tests (search ranking, lineage, the access matrix, every bypass above, the MCP tools end to end), 95% coverage, `ruff`, `mypy --strict`, CI on Python 3.11 to 3.13.

## License

MIT