cteforge: Trusted SQL for AI Agents
Role Creator · Personal open-source project Stack Python · sqlglot · FastAPI · MCP · SQLAlchemy · DuckDB · React · fastembed Scope Open source (Apache-2.0) · Web GUI, MCP server, HTTP API, and CLI · Eight SQL dialects, from DuckDB and Postgres to Redshift, Snowflake, BigQuery, and Databricks Code github.com/QuinonesJacob/cteforge
Problem
AI agents that write SQL get business questions wrong in ways that look right. They don't know what a metric actually means, which joins are safe, or which parts of a query must never change. Meanwhile every data team already has queries it trusts: the long CTE that finance signed off on, the revenue query everyone checks their numbers against. Agents have no structured way to find and reuse them.
The usual answer is a semantic layer, which asks a team to model its tables, joins, and metrics before an agent gets anything useful. That's a large project, and complex CTE logic rarely fits neatly into measures and dimensions.
The insight behind it: often the most valuable thing a data team has is its library of verified SQL, with plain-language notes on what each query means and how to combine them. cteforge turns that library into something agents can use directly.
Approach
cteforge starts from the SQL a team already trusts and describes only the parts that change. Paste the SQL you already trust; no semantic model required.
- The analyst decides what can change. A trusted query is kept as written. The analyst marks which values (a date range, a list of regions, a threshold) are variables, and which of those agents may change. Nothing is changeable by default.
- Agents pick values; the tool writes the SQL. Agents call a definition by reference and supply values in one strict format per type. cteforge validates them and renders the SQL itself, so verified logic never passes back through the model.
- Proof on import. Every imported query is checked to give the same query back, by its parsed structure and by its results on the warehouse.
- A trust label on every result, from verified down to exploratory. Overriding a fixed value or adding new logic lowers it, so whoever reads the answer knows how far to rely on it.
- Review before anything is trusted. Every change, from a person or an agent, is a proposal checked against the warehouse. Changes to verified definitions wait for a person, and nobody approves their own work.
- Building blocks. Definitions compose into new ones by reference, so fixing one fixes everything built on it, and a join map mined from the library tells agents which joins are known to be safe.
What I owned
I designed and built cteforge on my own: the product definition and its positioning against semantic layers, the architecture (recorded as 29 architecture decision records), the Python server, the MCP tools and HTTP API, the warehouse connectors, the React GUI, and the walkthrough video above.
Where it stands
cteforge is open source on GitHub and runs end to end. One command seeds a sample warehouse and library; another starts the GUI, API, and MCP server. Claude Code or any MCP client can then connect and answer a question like "What was our realization rate in Q4 2025?" from trusted definitions, with the trust label alongside the numbers. DuckDB, Postgres, ClickHouse, and MySQL are tested against real engines on every change.
What I learned
The model is rarely the hard part, and SQL is where that's easiest to see. An agent writing SQL from scratch has to rediscover what a team already knows, every single time. Giving it the team's trusted queries, and letting it change only the parts a person marked as safe, turns that into a much smaller problem: pick the right definition and fill in the values. The other lesson was about trust. A correct answer and an answer people rely on are different things, and the difference is whether they can see where the number came from.