feat: add generate_schema_docs tool for database documentation#278
feat: add generate_schema_docs tool for database documentation#278hasithasandunlakshan wants to merge 2 commits into
Conversation
…on generation in markdown and structured formats
|
Nice tool, been trying it out. Ran it against a few schema shapes and hit something on the foreign key output that seemed worth flagging. For a composite (multi column) foreign key, the FK list looks like it's giving the cartesian product of the source and target columns instead of the actual column pairing. Quick repro with one composite FK, (a, b) references parent (a, b): create table public.parent (a int not null, b int not null, primary key (a, b));
create table public.child (
a int not null, b int not null,
constraint child_parent_fk foreign key (a, b) references public.parent (a, b)
);generate_schema_docs comes back with 4 edges for child_parent_fk: Only a->a and b->b are real. The a->b and b->a rows are relationships that aren't actually in the schema. Since the tool's meant for AI reasoning and security auditing, having it report FKs that don't exist felt worth raising. Here's a failing test if it helps, drops in next to the existing schema docs test: test('composite foreign key is not a cartesian product', async () => {
// ...same setup, plus the two tables above...
const child = result.data.tables.find((t) => t.full_name === 'public.child');
const pairs = child.foreign_key_constraints.map((f) => `${f.source}=>${f.target}`);
expect(pairs).not.toContain('public.child.a=>public.parent.b');
expect(pairs).not.toContain('public.child.b=>public.parent.a');
expect(child.foreign_key_constraints).toHaveLength(2);
});One thing I haven't dug into yet is whether the cross product is coming from the upstream relationships data or from the mapping here, happy to look if that's useful. Also noticed the FK list is the only collection that isn't sorted (tables, policies, triggers, functions all are), so FK order might shift between runs. |
What kind of change does this PR introduce?
Feature
What is the current behavior?
Currently, to understand the full structure of a database (tables, columns, RLS policies, triggers, and functions), an AI agent or developer must call multiple tools (e.g.,
list_tableswithverbose: true,execute_sql, etc.) or perform multiple manual queries. There is no single, consolidated way to get a documentation-ready overview of the database schema.Relevant Issue: #277
What is the new behavior?
This PR introduces a new tool,
generate_schema_docs, which provides a comprehensive, documentation-ready summary of the database schema in a single call.Key Features:
markdown(optimized for humans/AI context windows),json(optimized for programmatic use), orboth.databasefeature group and wired into the MCP server runtime.Additional context
server.test.tscovering various output formats and edge cases. All 172 unit tests are passing.