# keygraph

> keygraph is an open-source, read-only schema map that gives data analyst agents, text-to-SQL agents and other LLM agents schema context: join paths, keys, grain and status codes, through an MCP server or CLI. It maps how the tables in your database connect (keys, indexes, declared and inferred foreign keys, business context and value profiles) so people and AI agents can find the right join path and the right data. Open source (MIT), Python 3.11+, read-only, no LLM calls. Works with MySQL/MariaDB, PostgreSQL, SQLite, SQL Server, ClickHouse, Metabase, MongoDB and information_schema dumps; serves agents through MCP, a CLI with --json, a Claude Code skill and markdown files.

## Start here

- [README](https://github.com/alikarami76/keygraph/blob/main/README.md): what keygraph is, when to use it, quickstart in three commands.
- [Why a prebuilt map](https://github.com/alikarami76/keygraph/blob/main/README.md#why-a-prebuilt-map-beats-no-context-or-context-made-on-the-fly): why a deterministic, curated schema map gives agents more correct SQL than no context or context they explore and generate in each session.
- [When to use keygraph](https://github.com/alikarami76/keygraph/blob/main/README.md#when-to-use-keygraph): join paths, missing foreign keys, ER diagrams, schema context for text-to-SQL, status codes.
- [Supported sources](https://github.com/alikarami76/keygraph/blob/main/README.md#supported-sources): what each database source reads and the read-only guarantee.

## For AI agents

- [Agent setup protocol](https://github.com/alikarami76/keygraph/blob/main/setup/SETUP.md): follow this when a user asks you to set up keygraph. You detect, ask up to six questions, install, build, connect MCP and verify with `keygraph doctor`. Per-harness files cover Claude Code, Codex, Cursor, ZCode, OpenCode, Unsloth Studio and others.
- [Bug reports for agents](https://github.com/alikarami76/keygraph/blob/main/setup/BUG_REPORTS.md): how to tell a keygraph bug from a local problem, redact it, and open an issue only after the user says yes.

- [Build a data analyst agent with keygraph](https://github.com/alikarami76/keygraph/blob/main/README.md#how-do-i-build-a-data-analyst-agent-with-keygraph): the search, describe, path, keys, write SQL, check loop and how to plug it into any agent that can call MCP tools or run a CLI.
- [AGENTS.md](https://github.com/alikarami76/keygraph/blob/main/AGENTS.md): the command sequence (search, describe, path, keys), how to read confidence, what not to do.
- [Claude Code skill](https://github.com/alikarami76/keygraph/blob/main/skills/keygraph/SKILL.md): when to use each command and how to build and extend the graph.
- [MCP server setup](https://github.com/alikarami76/keygraph/blob/main/README.md#mcp): twenty tools (fourteen read-only, one that logs queries, five `index_*` tools for LLM indexing; `dev_note` is added only in dev mode) and configs for Claude Code, Claude Desktop, Cursor and Codex.
- [Use with AI agents](https://github.com/alikarami76/keygraph/blob/main/README.md#use-with-ai-agents): MCP, CLI --json and the generated areas/ and tables/ markdown.
- [LLM indexing](https://github.com/alikarami76/keygraph/blob/main/AGENTS.md#llm-indexing): your own agent builds the meaning layer (relations, column and status-code meanings, grain, concepts, metrics, performance notes). `keygraph index start`, `index next`, `index submit UNIT_ID FILE|-`, `index status`, `index finish` (plus `index replay` and `index refresh`), or the MCP tools `index_start`, `index_next`, `index_submit`, `index_status`, `index_finish`. Loop: start, then next and submit until next says `done`, then finish. Three attempts per unit; a rejected unit stays pending and is resubmitted with the same `unit_id`; a failed unit keeps the deterministic map. Probes read a read-only source only through an environment variable named with `--url-env NAME` or `--cmd-env NAME` (for example `KEYGRAPH_PROBE_URL`), never a URL or password on the command line. Status codes a probe finds in the data are reported as `codes_seen`, never as verified, and a code label is given only when the packet supports it. keygraph makes no model calls.
- `context add` merges into the stored row (fields left out keep their values; `codes=` merges code by code; `field=` empty clears one).
- `context add --author NAME` labels who wrote a note, and `context history --json` lists replaced or refused writes.
- `describe` and `neighbors` hide relations below 0.6 confidence and report the hidden count; `--min-confidence 0` (MCP `min_confidence=0`) shows them.
- `describe T --brief` and `search Q --no-paths` (MCP `brief=true`, `with_paths=false`) give smaller answers; `scan --url` keeps comments, enums, row counts and partitions on MySQL, PostgreSQL, SQL Server and ClickHouse.

- [Dev mode](https://github.com/alikarami76/keygraph/blob/main/docs/dev-mode.md): `keygraph dev on|off|status|report|export|clear|note`. Off by default; records how agents use keygraph (tool calls, decisions, build traces), never database rows or secrets. If a `dev_note` tool is listed, call it once per user question, before your first keygraph call for it, with the question in the user's words and no personal data; if it is not listed, do nothing. Without MCP: `keygraph dev note --dir <out> "<question>" --json`.

## Concepts

- [How relations are inferred](https://github.com/alikarami76/keygraph/blob/main/README.md#how-relations-are-inferred): confidence, evidence, hubs, areas, path penalties, mirrors.
- [Business context and value profiles](https://github.com/alikarami76/keygraph/blob/main/README.md#business-context-and-value-profiles): concepts, status codes, metrics, sampled profiles and the PII rule.
- [Code index and query log](https://github.com/alikarami76/keygraph/blob/main/README.md#code-index-and-query-log): link source code (ORM declarations, SQL, property uses) to tables and columns, and log the queries people and agents ran with timing and business question.
- [Comparison](https://github.com/alikarami76/keygraph/blob/main/README.md#comparison): keygraph next to SchemaSpy, dbdocs, DataHub, OpenMetadata, database IDE diagrams, dbdiagram and schema linking.
- [FAQ](https://github.com/alikarami76/keygraph/blob/main/README.md#faq): common questions about join paths, foreign keys, safety and MCP setup.

## Examples and docs

- [keygraph demo](https://github.com/alikarami76/keygraph#quickstart): `uvx keygraph demo` builds the bundled webshop and opens the explorer with no database and no extras (`--dir`, `--no-open`, `--json`).
- [examples/shop](https://github.com/alikarami76/keygraph/blob/main/examples/shop/README.md): a fictional 35-table webshop you can build and query in a minute.
- [How keygraph works (PDF)](https://github.com/alikarami76/keygraph/blob/main/docs/how-it-works.pdf): a guided tour with screenshots of every explorer view.
- [Design system](https://github.com/alikarami76/keygraph/blob/main/design/DESIGN.md): the explorer's visual design rules.

## Optional

- [Changelog](https://github.com/alikarami76/keygraph/blob/main/CHANGELOG.md): release notes.
- [License](https://github.com/alikarami76/keygraph/blob/main/LICENSE): MIT.
